Skip to content

Commit 744983d

Browse files
committed
Merge tag 'for-linus-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rw/ubifs
Pull JFFS2, UBI and UBIFS updates from Richard Weinberger: "JFFS2: - Fixes for a memory leak UBI: - Fixes for fastmap (UAF, high CPU usage) UBIFS: - Minor cleanups" * tag 'for-linus-5.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rw/ubifs: ubi: ubi_create_volume: Fix use-after-free when volume creation failed ubi: fastmap: Check wl_pool for free peb before wear leveling ubi: fastmap: Fix high cpu usage of ubi_bgt by making sure wl_pool not empty ubifs: Use NULL instead of using plain integer as pointer ubifs: Simplify the return expression of run_gc() jffs2: fix memory leak in jffs2_do_fill_super jffs2: Use kzalloc instead of kmalloc/memset
2 parents 4e583ff + 8c03a1c commit 744983d

File tree

10 files changed

+129
-59
lines changed

10 files changed

+129
-59
lines changed

drivers/mtd/ubi/fastmap-wl.c

Lines changed: 98 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,33 @@ struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
9797
return e;
9898
}
9999

100+
/*
101+
* has_enough_free_count - whether ubi has enough free pebs to fill fm pools
102+
* @ubi: UBI device description object
103+
* @is_wl_pool: whether UBI is filling wear leveling pool
104+
*
105+
* This helper function checks whether there are enough free pebs (deducted
106+
* by fastmap pebs) to fill fm_pool and fm_wl_pool, above rule works after
107+
* there is at least one of free pebs is filled into fm_wl_pool.
108+
* For wear leveling pool, UBI should also reserve free pebs for bad pebs
109+
* handling, because there maybe no enough free pebs for user volumes after
110+
* producing new bad pebs.
111+
*/
112+
static bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool)
113+
{
114+
int fm_used = 0; // fastmap non anchor pebs.
115+
int beb_rsvd_pebs;
116+
117+
if (!ubi->free.rb_node)
118+
return false;
119+
120+
beb_rsvd_pebs = is_wl_pool ? ubi->beb_rsvd_pebs : 0;
121+
if (ubi->fm_wl_pool.size > 0 && !(ubi->ro_mode || ubi->fm_disabled))
122+
fm_used = ubi->fm_size / ubi->leb_size - 1;
123+
124+
return ubi->free_count - beb_rsvd_pebs > fm_used;
125+
}
126+
100127
/**
101128
* ubi_refill_pools - refills all fastmap PEB pools.
102129
* @ubi: UBI device description object
@@ -120,21 +147,17 @@ void ubi_refill_pools(struct ubi_device *ubi)
120147
wl_tree_add(ubi->fm_anchor, &ubi->free);
121148
ubi->free_count++;
122149
}
123-
if (ubi->fm_next_anchor) {
124-
wl_tree_add(ubi->fm_next_anchor, &ubi->free);
125-
ubi->free_count++;
126-
}
127150

128-
/* All available PEBs are in ubi->free, now is the time to get
151+
/*
152+
* All available PEBs are in ubi->free, now is the time to get
129153
* the best anchor PEBs.
130154
*/
131155
ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1);
132-
ubi->fm_next_anchor = ubi_wl_get_fm_peb(ubi, 1);
133156

134157
for (;;) {
135158
enough = 0;
136159
if (pool->size < pool->max_size) {
137-
if (!ubi->free.rb_node)
160+
if (!has_enough_free_count(ubi, false))
138161
break;
139162

140163
e = wl_get_wle(ubi);
@@ -147,8 +170,7 @@ void ubi_refill_pools(struct ubi_device *ubi)
147170
enough++;
148171

149172
if (wl_pool->size < wl_pool->max_size) {
150-
if (!ubi->free.rb_node ||
151-
(ubi->free_count - ubi->beb_rsvd_pebs < 5))
173+
if (!has_enough_free_count(ubi, true))
152174
break;
153175

154176
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
@@ -253,6 +275,58 @@ int ubi_wl_get_peb(struct ubi_device *ubi)
253275
return ret;
254276
}
255277

278+
/**
279+
* next_peb_for_wl - returns next PEB to be used internally by the
280+
* WL sub-system.
281+
*
282+
* @ubi: UBI device description object
283+
*/
284+
static struct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi)
285+
{
286+
struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
287+
int pnum;
288+
289+
if (pool->used == pool->size)
290+
return NULL;
291+
292+
pnum = pool->pebs[pool->used];
293+
return ubi->lookuptbl[pnum];
294+
}
295+
296+
/**
297+
* need_wear_leveling - checks whether to trigger a wear leveling work.
298+
* UBI fetches free PEB from wl_pool, we check free PEBs from both 'wl_pool'
299+
* and 'ubi->free', because free PEB in 'ubi->free' tree maybe moved into
300+
* 'wl_pool' by ubi_refill_pools().
301+
*
302+
* @ubi: UBI device description object
303+
*/
304+
static bool need_wear_leveling(struct ubi_device *ubi)
305+
{
306+
int ec;
307+
struct ubi_wl_entry *e;
308+
309+
if (!ubi->used.rb_node)
310+
return false;
311+
312+
e = next_peb_for_wl(ubi);
313+
if (!e) {
314+
if (!ubi->free.rb_node)
315+
return false;
316+
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
317+
ec = e->ec;
318+
} else {
319+
ec = e->ec;
320+
if (ubi->free.rb_node) {
321+
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
322+
ec = max(ec, e->ec);
323+
}
324+
}
325+
e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
326+
327+
return ec - e->ec >= UBI_WL_THRESHOLD;
328+
}
329+
256330
/* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
257331
*
258332
* @ubi: UBI device description object
@@ -286,20 +360,26 @@ static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
286360
int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
287361
{
288362
struct ubi_work *wrk;
363+
struct ubi_wl_entry *anchor;
289364

290365
spin_lock(&ubi->wl_lock);
291366

292-
/* Do we have a next anchor? */
293-
if (!ubi->fm_next_anchor) {
294-
ubi->fm_next_anchor = ubi_wl_get_fm_peb(ubi, 1);
295-
if (!ubi->fm_next_anchor)
296-
/* Tell wear leveling to produce a new anchor PEB */
297-
ubi->fm_do_produce_anchor = 1;
367+
/* Do we already have an anchor? */
368+
if (ubi->fm_anchor) {
369+
spin_unlock(&ubi->wl_lock);
370+
return 0;
298371
}
299372

300-
/* Do wear leveling to get a new anchor PEB or check the
301-
* existing next anchor candidate.
302-
*/
373+
/* See if we can find an anchor PEB on the list of free PEBs */
374+
anchor = ubi_wl_get_fm_peb(ubi, 1);
375+
if (anchor) {
376+
ubi->fm_anchor = anchor;
377+
spin_unlock(&ubi->wl_lock);
378+
return 0;
379+
}
380+
381+
ubi->fm_do_produce_anchor = 1;
382+
/* No luck, trigger wear leveling to produce a new anchor PEB. */
303383
if (ubi->wl_scheduled) {
304384
spin_unlock(&ubi->wl_lock);
305385
return 0;
@@ -381,11 +461,6 @@ static void ubi_fastmap_close(struct ubi_device *ubi)
381461
ubi->fm_anchor = NULL;
382462
}
383463

384-
if (ubi->fm_next_anchor) {
385-
return_unused_peb(ubi, ubi->fm_next_anchor);
386-
ubi->fm_next_anchor = NULL;
387-
}
388-
389464
if (ubi->fm) {
390465
for (i = 0; i < ubi->fm->used_blocks; i++)
391466
kfree(ubi->fm->e[i]);

drivers/mtd/ubi/fastmap.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,17 +1230,6 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
12301230
fm_pos += sizeof(*fec);
12311231
ubi_assert(fm_pos <= ubi->fm_size);
12321232
}
1233-
if (ubi->fm_next_anchor) {
1234-
fec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
1235-
1236-
fec->pnum = cpu_to_be32(ubi->fm_next_anchor->pnum);
1237-
set_seen(ubi, ubi->fm_next_anchor->pnum, seen_pebs);
1238-
fec->ec = cpu_to_be32(ubi->fm_next_anchor->ec);
1239-
1240-
free_peb_count++;
1241-
fm_pos += sizeof(*fec);
1242-
ubi_assert(fm_pos <= ubi->fm_size);
1243-
}
12441233
fmh->free_peb_count = cpu_to_be32(free_peb_count);
12451234

12461235
ubi_for_each_used_peb(ubi, wl_e, tmp_rb) {

drivers/mtd/ubi/ubi.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,7 @@ struct ubi_debug_info {
489489
* @fm_work: fastmap work queue
490490
* @fm_work_scheduled: non-zero if fastmap work was scheduled
491491
* @fast_attach: non-zero if UBI was attached by fastmap
492-
* @fm_anchor: The new anchor PEB used during fastmap update
493-
* @fm_next_anchor: An anchor PEB candidate for the next time fastmap is updated
492+
* @fm_anchor: The next anchor PEB to use for fastmap
494493
* @fm_do_produce_anchor: If true produce an anchor PEB in wl
495494
*
496495
* @used: RB-tree of used physical eraseblocks
@@ -601,7 +600,6 @@ struct ubi_device {
601600
int fm_work_scheduled;
602601
int fast_attach;
603602
struct ubi_wl_entry *fm_anchor;
604-
struct ubi_wl_entry *fm_next_anchor;
605603
int fm_do_produce_anchor;
606604

607605
/* Wear-leveling sub-system's stuff */

drivers/mtd/ubi/vmt.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
309309
ubi->volumes[vol_id] = NULL;
310310
ubi->vol_count -= 1;
311311
spin_unlock(&ubi->volumes_lock);
312-
ubi_eba_destroy_table(eba_tbl);
313312
out_acc:
314313
spin_lock(&ubi->volumes_lock);
315314
ubi->rsvd_pebs -= vol->reserved_pebs;

drivers/mtd/ubi/wl.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,11 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
670670
ubi_assert(!ubi->move_from && !ubi->move_to);
671671
ubi_assert(!ubi->move_to_put);
672672

673+
#ifdef CONFIG_MTD_UBI_FASTMAP
674+
if (!next_peb_for_wl(ubi) ||
675+
#else
673676
if (!ubi->free.rb_node ||
677+
#endif
674678
(!ubi->used.rb_node && !ubi->scrub.rb_node)) {
675679
/*
676680
* No free physical eraseblocks? Well, they must be waiting in
@@ -689,16 +693,16 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
689693

690694
#ifdef CONFIG_MTD_UBI_FASTMAP
691695
e1 = find_anchor_wl_entry(&ubi->used);
692-
if (e1 && ubi->fm_next_anchor &&
693-
(ubi->fm_next_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) {
696+
if (e1 && ubi->fm_anchor &&
697+
(ubi->fm_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) {
694698
ubi->fm_do_produce_anchor = 1;
695-
/* fm_next_anchor is no longer considered a good anchor
696-
* candidate.
699+
/*
700+
* fm_anchor is no longer considered a good anchor.
697701
* NULL assignment also prevents multiple wear level checks
698702
* of this PEB.
699703
*/
700-
wl_tree_add(ubi->fm_next_anchor, &ubi->free);
701-
ubi->fm_next_anchor = NULL;
704+
wl_tree_add(ubi->fm_anchor, &ubi->free);
705+
ubi->fm_anchor = NULL;
702706
ubi->free_count++;
703707
}
704708

@@ -1003,8 +1007,6 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
10031007
static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
10041008
{
10051009
int err = 0;
1006-
struct ubi_wl_entry *e1;
1007-
struct ubi_wl_entry *e2;
10081010
struct ubi_work *wrk;
10091011

10101012
spin_lock(&ubi->wl_lock);
@@ -1017,6 +1019,13 @@ static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
10171019
* the WL worker has to be scheduled anyway.
10181020
*/
10191021
if (!ubi->scrub.rb_node) {
1022+
#ifdef CONFIG_MTD_UBI_FASTMAP
1023+
if (!need_wear_leveling(ubi))
1024+
goto out_unlock;
1025+
#else
1026+
struct ubi_wl_entry *e1;
1027+
struct ubi_wl_entry *e2;
1028+
10201029
if (!ubi->used.rb_node || !ubi->free.rb_node)
10211030
/* No physical eraseblocks - no deal */
10221031
goto out_unlock;
@@ -1032,6 +1041,7 @@ static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
10321041

10331042
if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
10341043
goto out_unlock;
1044+
#endif
10351045
dbg_wl("schedule wear-leveling");
10361046
} else
10371047
dbg_wl("schedule scrubbing");
@@ -1085,12 +1095,13 @@ static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
10851095
if (!err) {
10861096
spin_lock(&ubi->wl_lock);
10871097

1088-
if (!ubi->fm_disabled && !ubi->fm_next_anchor &&
1098+
if (!ubi->fm_disabled && !ubi->fm_anchor &&
10891099
e->pnum < UBI_FM_MAX_START) {
1090-
/* Abort anchor production, if needed it will be
1100+
/*
1101+
* Abort anchor production, if needed it will be
10911102
* enabled again in the wear leveling started below.
10921103
*/
1093-
ubi->fm_next_anchor = e;
1104+
ubi->fm_anchor = e;
10941105
ubi->fm_do_produce_anchor = 0;
10951106
} else {
10961107
wl_tree_add(e, &ubi->free);

drivers/mtd/ubi/wl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
static void update_fastmap_work_fn(struct work_struct *wrk);
66
static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root);
77
static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi);
8+
static struct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi);
9+
static bool need_wear_leveling(struct ubi_device *ubi);
810
static void ubi_fastmap_close(struct ubi_device *ubi);
911
static inline void ubi_fastmap_init(struct ubi_device *ubi, int *count)
1012
{

fs/jffs2/erase.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ static void jffs2_erase_block(struct jffs2_sb_info *c,
4343
jffs2_dbg(1, "%s(): erase block %#08x (range %#08x-%#08x)\n",
4444
__func__,
4545
jeb->offset, jeb->offset, jeb->offset + c->sector_size);
46-
instr = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
46+
instr = kzalloc(sizeof(struct erase_info), GFP_KERNEL);
4747
if (!instr) {
48-
pr_warn("kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
48+
pr_warn("kzalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
4949
mutex_lock(&c->erase_free_sem);
5050
spin_lock(&c->erase_completion_lock);
5151
list_move(&jeb->list, &c->erase_pending_list);
@@ -57,8 +57,6 @@ static void jffs2_erase_block(struct jffs2_sb_info *c,
5757
return;
5858
}
5959

60-
memset(instr, 0, sizeof(*instr));
61-
6260
instr->addr = jeb->offset;
6361
instr->len = c->sector_size;
6462

fs/jffs2/fs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ int jffs2_do_fill_super(struct super_block *sb, struct fs_context *fc)
604604
jffs2_free_raw_node_refs(c);
605605
kvfree(c->blocks);
606606
jffs2_clear_xattr_subsystem(c);
607+
jffs2_sum_exit(c);
607608
out_inohash:
608609
kfree(c->inocache_list);
609610
out_wbuf:

fs/ubifs/budget.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void shrink_liability(struct ubifs_info *c, int nr_to_write)
6565
*/
6666
static int run_gc(struct ubifs_info *c)
6767
{
68-
int err, lnum;
68+
int lnum;
6969

7070
/* Make some free space by garbage-collecting dirty space */
7171
down_read(&c->commit_sem);
@@ -76,10 +76,7 @@ static int run_gc(struct ubifs_info *c)
7676

7777
/* GC freed one LEB, return it to lprops */
7878
dbg_budg("GC freed LEB %d", lnum);
79-
err = ubifs_return_leb(c, lnum);
80-
if (err)
81-
return err;
82-
return 0;
79+
return ubifs_return_leb(c, lnum);
8380
}
8481

8582
/**

fs/ubifs/xattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ int ubifs_init_security(struct inode *dentry, struct inode *inode,
677677
int err;
678678

679679
err = security_inode_init_security(inode, dentry, qstr,
680-
&init_xattrs, 0);
680+
&init_xattrs, NULL);
681681
if (err) {
682682
struct ubifs_info *c = dentry->i_sb->s_fs_info;
683683
ubifs_err(c, "cannot initialize security for inode %lu, error %d",

0 commit comments

Comments
 (0)