Skip to content

Commit 91f4285

Browse files
Boris Brezillonrichardweinberger
authored andcommitted
UBI: provide helpers to allocate and free aeb elements
This not only hides the aeb allocation internals (which is always good in case we ever want to change the allocation system), but also helps us factorize the initialization of some common fields (ec and pnum). Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent fcbb6af commit 91f4285

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed

drivers/mtd/ubi/attach.c

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,47 @@ static struct ubi_ainf_volume *ubi_find_or_add_av(struct ubi_attach_info *ai,
181181
return find_or_add_av(ai, vol_id, AV_FIND_OR_ADD, created);
182182
}
183183

184+
/**
185+
* ubi_alloc_aeb - allocate an aeb element
186+
* @ai: attaching information
187+
* @pnum: physical eraseblock number
188+
* @ec: erase counter of the physical eraseblock
189+
*
190+
* Allocate an aeb object and initialize the pnum and ec information.
191+
* vol_id and lnum are set to UBI_UNKNOWN, and the other fields are
192+
* initialized to zero.
193+
* Note that the element is not added in any list or RB tree.
194+
*/
195+
struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
196+
int ec)
197+
{
198+
struct ubi_ainf_peb *aeb;
199+
200+
aeb = kmem_cache_zalloc(ai->aeb_slab_cache, GFP_KERNEL);
201+
if (!aeb)
202+
return NULL;
203+
204+
aeb->pnum = pnum;
205+
aeb->ec = ec;
206+
aeb->vol_id = UBI_UNKNOWN;
207+
aeb->lnum = UBI_UNKNOWN;
208+
209+
return aeb;
210+
}
211+
212+
/**
213+
* ubi_free_aeb - free an aeb element
214+
* @ai: attaching information
215+
* @aeb: the element to free
216+
*
217+
* Free an aeb object. The caller must have removed the element from any list
218+
* or RB tree.
219+
*/
220+
void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb)
221+
{
222+
kmem_cache_free(ai->aeb_slab_cache, aeb);
223+
}
224+
184225
/**
185226
* add_to_list - add physical eraseblock to a list.
186227
* @ai: attaching information
@@ -217,14 +258,12 @@ static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id,
217258
} else
218259
BUG();
219260

220-
aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
261+
aeb = ubi_alloc_aeb(ai, pnum, ec);
221262
if (!aeb)
222263
return -ENOMEM;
223264

224-
aeb->pnum = pnum;
225265
aeb->vol_id = vol_id;
226266
aeb->lnum = lnum;
227-
aeb->ec = ec;
228267
if (to_head)
229268
list_add(&aeb->u.list, list);
230269
else
@@ -249,13 +288,11 @@ static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec)
249288

250289
dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
251290

252-
aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
291+
aeb = ubi_alloc_aeb(ai, pnum, ec);
253292
if (!aeb)
254293
return -ENOMEM;
255294

256295
ai->corr_peb_count += 1;
257-
aeb->pnum = pnum;
258-
aeb->ec = ec;
259296
list_add(&aeb->u.list, &ai->corr);
260297
return 0;
261298
}
@@ -278,14 +315,12 @@ static int add_fastmap(struct ubi_attach_info *ai, int pnum,
278315
{
279316
struct ubi_ainf_peb *aeb;
280317

281-
aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
318+
aeb = ubi_alloc_aeb(ai, pnum, ec);
282319
if (!aeb)
283320
return -ENOMEM;
284321

285-
aeb->pnum = pnum;
286322
aeb->vol_id = be32_to_cpu(vid_hdr->vol_id);
287323
aeb->sqnum = be64_to_cpu(vid_hdr->sqnum);
288-
aeb->ec = ec;
289324
list_add(&aeb->u.list, &ai->fastmap);
290325

291326
dbg_bld("add to fastmap list: PEB %d, vol_id %d, sqnum: %llu", pnum,
@@ -667,12 +702,10 @@ int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
667702
if (err)
668703
return err;
669704

670-
aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
705+
aeb = ubi_alloc_aeb(ai, pnum, ec);
671706
if (!aeb)
672707
return -ENOMEM;
673708

674-
aeb->ec = ec;
675-
aeb->pnum = pnum;
676709
aeb->vol_id = vol_id;
677710
aeb->lnum = lnum;
678711
aeb->scrub = bitflips;
@@ -1278,7 +1311,7 @@ static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av,
12781311
if (list)
12791312
list_add_tail(&aeb->u.list, list);
12801313
else
1281-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1314+
ubi_free_aeb(ai, aeb);
12821315
}
12831316
}
12841317
kfree(av);
@@ -1296,23 +1329,23 @@ static void destroy_ai(struct ubi_attach_info *ai)
12961329

12971330
list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) {
12981331
list_del(&aeb->u.list);
1299-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1332+
ubi_free_aeb(ai, aeb);
13001333
}
13011334
list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) {
13021335
list_del(&aeb->u.list);
1303-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1336+
ubi_free_aeb(ai, aeb);
13041337
}
13051338
list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) {
13061339
list_del(&aeb->u.list);
1307-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1340+
ubi_free_aeb(ai, aeb);
13081341
}
13091342
list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) {
13101343
list_del(&aeb->u.list);
1311-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1344+
ubi_free_aeb(ai, aeb);
13121345
}
13131346
list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) {
13141347
list_del(&aeb->u.list);
1315-
kmem_cache_free(ai->aeb_slab_cache, aeb);
1348+
ubi_free_aeb(ai, aeb);
13161349
}
13171350

13181351
/* Destroy the volume RB-tree */

drivers/mtd/ubi/fastmap.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,10 @@ static int add_aeb(struct ubi_attach_info *ai, struct list_head *list,
145145
{
146146
struct ubi_ainf_peb *aeb;
147147

148-
aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL);
148+
aeb = ubi_alloc_aeb(ai, pnum, ec);
149149
if (!aeb)
150150
return -ENOMEM;
151151

152-
aeb->pnum = pnum;
153-
aeb->ec = ec;
154152
aeb->lnum = -1;
155153
aeb->scrub = scrub;
156154
aeb->copy_flag = aeb->sqnum = 0;
@@ -276,7 +274,7 @@ static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
276274
*/
277275
if (aeb->pnum == new_aeb->pnum) {
278276
ubi_assert(aeb->lnum == new_aeb->lnum);
279-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
277+
ubi_free_aeb(ai, new_aeb);
280278

281279
return 0;
282280
}
@@ -287,13 +285,10 @@ static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
287285

288286
/* new_aeb is newer */
289287
if (cmp_res & 1) {
290-
victim = kmem_cache_alloc(ai->aeb_slab_cache,
291-
GFP_KERNEL);
288+
victim = ubi_alloc_aeb(ai, aeb->ec, aeb->pnum);
292289
if (!victim)
293290
return -ENOMEM;
294291

295-
victim->ec = aeb->ec;
296-
victim->pnum = aeb->pnum;
297292
list_add_tail(&victim->u.list, &ai->erase);
298293

299294
if (av->highest_lnum == be32_to_cpu(new_vh->lnum))
@@ -307,7 +302,7 @@ static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai,
307302
aeb->pnum = new_aeb->pnum;
308303
aeb->copy_flag = new_vh->copy_flag;
309304
aeb->scrub = new_aeb->scrub;
310-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
305+
ubi_free_aeb(ai, new_aeb);
311306

312307
/* new_aeb is older */
313308
} else {
@@ -353,7 +348,7 @@ static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
353348
struct ubi_ainf_volume *av;
354349

355350
if (vol_id == UBI_FM_SB_VOLUME_ID || vol_id == UBI_FM_DATA_VOLUME_ID) {
356-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
351+
ubi_free_aeb(ai, new_aeb);
357352

358353
return 0;
359354
}
@@ -362,7 +357,7 @@ static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai,
362357
av = ubi_find_av(ai, vol_id);
363358
if (!av) {
364359
ubi_err(ubi, "orphaned volume in fastmap pool!");
365-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
360+
ubi_free_aeb(ai, new_aeb);
366361
return UBI_BAD_FASTMAP;
367362
}
368363

@@ -390,7 +385,7 @@ static void unmap_peb(struct ubi_attach_info *ai, int pnum)
390385
if (aeb->pnum == pnum) {
391386
rb_erase(&aeb->u.rb, &av->root);
392387
av->leb_count--;
393-
kmem_cache_free(ai->aeb_slab_cache, aeb);
388+
ubi_free_aeb(ai, aeb);
394389
return;
395390
}
396391
}
@@ -485,15 +480,12 @@ static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
485480
if (err == UBI_IO_BITFLIPS)
486481
scrub = 1;
487482

488-
new_aeb = kmem_cache_alloc(ai->aeb_slab_cache,
489-
GFP_KERNEL);
483+
new_aeb = ubi_alloc_aeb(ai, pnum, be64_to_cpu(ech->ec));
490484
if (!new_aeb) {
491485
ret = -ENOMEM;
492486
goto out;
493487
}
494488

495-
new_aeb->ec = be64_to_cpu(ech->ec);
496-
new_aeb->pnum = pnum;
497489
new_aeb->lnum = be32_to_cpu(vh->lnum);
498490
new_aeb->sqnum = be64_to_cpu(vh->sqnum);
499491
new_aeb->copy_flag = vh->copy_flag;
@@ -800,11 +792,11 @@ static int ubi_attach_fastmap(struct ubi_device *ubi,
800792
fail:
801793
list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) {
802794
list_del(&tmp_aeb->u.list);
803-
kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
795+
ubi_free_aeb(ai, tmp_aeb);
804796
}
805797
list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
806798
list_del(&tmp_aeb->u.list);
807-
kmem_cache_free(ai->aeb_slab_cache, tmp_aeb);
799+
ubi_free_aeb(ai, tmp_aeb);
808800
}
809801

810802
return ret;

drivers/mtd/ubi/ubi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,9 @@ extern struct mutex ubi_devices_mutex;
792792
extern struct blocking_notifier_head ubi_notifiers;
793793

794794
/* attach.c */
795+
struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
796+
int ec);
797+
void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
795798
int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
796799
int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
797800
struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);

drivers/mtd/ubi/vtbl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
338338
* of this LEB as it will be deleted and freed in 'ubi_add_to_av()'.
339339
*/
340340
err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
341-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
341+
ubi_free_aeb(ai, new_aeb);
342342
ubi_free_vid_hdr(ubi, vid_hdr);
343343
return err;
344344

@@ -351,7 +351,7 @@ static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
351351
list_add(&new_aeb->u.list, &ai->erase);
352352
goto retry;
353353
}
354-
kmem_cache_free(ai->aeb_slab_cache, new_aeb);
354+
ubi_free_aeb(ai, new_aeb);
355355
out_free:
356356
ubi_free_vid_hdr(ubi, vid_hdr);
357357
return err;

0 commit comments

Comments
 (0)