Skip to content

Commit 2153fc3

Browse files
committed
Merge tag 'ubifs-for-linus-6.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rw/ubifs
Pull UBI and UBIFS updates from Richard Weinberger: - UBI Fastmap improvements - Minor issues found by static analysis bots in both UBI and UBIFS - Fix for wrong dentry length UBIFS in fscrypt mode * tag 'ubifs-for-linus-6.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rw/ubifs: ubifs: ubifs_link: Fix wrong name len calculating when UBIFS is encrypted ubi: block: Fix use-after-free in ubiblock_cleanup ubifs: fix possible dereference after free ubi: fastmap: Add control in 'UBI_IOCATT' ioctl to reserve PEBs for filling pools ubi: fastmap: Add module parameter to control reserving filling pool PEBs ubi: fastmap: Fix lapsed wear leveling for first 64 PEBs ubi: fastmap: Get wl PEB even ec beyonds the 'max' if free PEBs are run out ubi: fastmap: may_reserve_for_fm: Don't reserve PEB if fm_anchor exists ubi: fastmap: Remove unneeded break condition while filling pools ubi: fastmap: Wait until there are enough free PEBs before filling pools ubi: fastmap: Use free pebs reserved for bad block handling ubi: Replace erase_block() with sync_erase() ubi: fastmap: Allocate memory with GFP_NOFS in ubi_update_fastmap ubi: fastmap: erase_block: Get erase counter from wl_entry rather than flash ubi: fastmap: Fix missed ec updating after erasing old fastmap data block ubifs: Fix missing error code err ubifs: Fix memory leak of bud->log_hash ubifs: Fix some kernel-doc comments
2 parents 1c41041 + 7569049 commit 2153fc3

File tree

15 files changed

+179
-116
lines changed

15 files changed

+179
-116
lines changed

drivers/mtd/ubi/block.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,15 @@ int ubiblock_create(struct ubi_volume_info *vi)
447447

448448
static void ubiblock_cleanup(struct ubiblock *dev)
449449
{
450+
int id = dev->gd->first_minor;
451+
450452
/* Stop new requests to arrive */
451453
del_gendisk(dev->gd);
452454
/* Finally destroy the blk queue */
453455
dev_info(disk_to_dev(dev->gd), "released");
454456
put_disk(dev->gd);
455457
blk_mq_free_tag_set(&dev->tag_set);
456-
idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
458+
idr_remove(&ubiblock_minor_idr, id);
457459
}
458460

459461
int ubiblock_remove(struct ubi_volume_info *vi)

drivers/mtd/ubi/build.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#define MTD_PARAM_LEN_MAX 64
3636

3737
/* Maximum number of comma-separated items in the 'mtd=' parameter */
38-
#define MTD_PARAM_MAX_COUNT 5
38+
#define MTD_PARAM_MAX_COUNT 6
3939

4040
/* Maximum value for the number of bad PEBs per 1024 PEBs */
4141
#define MAX_MTD_UBI_BEB_LIMIT 768
@@ -54,13 +54,15 @@
5454
* @vid_hdr_offs: VID header offset
5555
* @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
5656
* @enable_fm: enable fastmap when value is non-zero
57+
* @need_resv_pool: reserve pool->max_size pebs when value is none-zero
5758
*/
5859
struct mtd_dev_param {
5960
char name[MTD_PARAM_LEN_MAX];
6061
int ubi_num;
6162
int vid_hdr_offs;
6263
int max_beb_per1024;
6364
int enable_fm;
65+
int need_resv_pool;
6466
};
6567

6668
/* Numbers of elements set in the @mtd_dev_param array */
@@ -825,6 +827,7 @@ static int autoresize(struct ubi_device *ubi, int vol_id)
825827
* @vid_hdr_offset: VID header offset
826828
* @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
827829
* @disable_fm: whether disable fastmap
830+
* @need_resv_pool: whether reserve pebs to fill fm_pool
828831
*
829832
* This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
830833
* to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
@@ -840,7 +843,8 @@ static int autoresize(struct ubi_device *ubi, int vol_id)
840843
* @ubi_devices_mutex.
841844
*/
842845
int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
843-
int vid_hdr_offset, int max_beb_per1024, bool disable_fm)
846+
int vid_hdr_offset, int max_beb_per1024, bool disable_fm,
847+
bool need_resv_pool)
844848
{
845849
struct ubi_device *ubi;
846850
int i, err;
@@ -951,6 +955,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
951955
UBI_FM_MIN_POOL_SIZE);
952956

953957
ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
958+
ubi->fm_pool_rsv_cnt = need_resv_pool ? ubi->fm_pool.max_size : 0;
954959
ubi->fm_disabled = (!fm_autoconvert || disable_fm) ? 1 : 0;
955960
if (fm_debug)
956961
ubi_enable_dbg_chk_fastmap(ubi);
@@ -1273,7 +1278,8 @@ static int __init ubi_init(void)
12731278
mutex_lock(&ubi_devices_mutex);
12741279
err = ubi_attach_mtd_dev(mtd, p->ubi_num,
12751280
p->vid_hdr_offs, p->max_beb_per1024,
1276-
p->enable_fm == 0);
1281+
p->enable_fm == 0,
1282+
p->need_resv_pool != 0);
12771283
mutex_unlock(&ubi_devices_mutex);
12781284
if (err < 0) {
12791285
pr_err("UBI error: cannot attach mtd%d\n",
@@ -1482,6 +1488,18 @@ static int ubi_mtd_param_parse(const char *val, const struct kernel_param *kp)
14821488
} else
14831489
p->enable_fm = 0;
14841490

1491+
token = tokens[5];
1492+
if (token) {
1493+
int err = kstrtoint(token, 10, &p->need_resv_pool);
1494+
1495+
if (err) {
1496+
pr_err("UBI error: bad value for need_resv_pool parameter: %s\n",
1497+
token);
1498+
return -EINVAL;
1499+
}
1500+
} else
1501+
p->need_resv_pool = 0;
1502+
14851503
mtd_devs += 1;
14861504
return 0;
14871505
}
@@ -1495,6 +1513,7 @@ MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|pa
14951513
__stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
14961514
"Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
14971515
"Optional \"enable_fm\" parameter determines whether to enable fastmap during attach. If the value is non-zero, fastmap is enabled. Default value is 0.\n"
1516+
"Optional \"need_resv_pool\" parameter determines whether to reserve pool->max_size pebs during attach. If the value is non-zero, peb reservation is enabled. Default value is 0.\n"
14981517
"\n"
14991518
"Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
15001519
"Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"

drivers/mtd/ubi/cdev.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,8 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd,
10411041
*/
10421042
mutex_lock(&ubi_devices_mutex);
10431043
err = ubi_attach_mtd_dev(mtd, req.ubi_num, req.vid_hdr_offset,
1044-
req.max_beb_per1024, !!req.disable_fm);
1044+
req.max_beb_per1024, !!req.disable_fm,
1045+
!!req.need_resv_pool);
10451046
mutex_unlock(&ubi_devices_mutex);
10461047
if (err < 0)
10471048
put_mtd_device(mtd);

drivers/mtd/ubi/eba.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
#include <linux/err.h>
3434
#include "ubi.h"
3535

36-
/* Number of physical eraseblocks reserved for atomic LEB change operation */
37-
#define EBA_RESERVED_PEBS 1
38-
3936
/**
4037
* struct ubi_eba_entry - structure encoding a single LEB -> PEB association
4138
* @pnum: the physical eraseblock number attached to the LEB

drivers/mtd/ubi/fastmap-wl.c

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
7676
{
7777
struct ubi_wl_entry *e = NULL;
7878

79-
if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
79+
if (!ubi->free.rb_node)
8080
goto out;
8181

8282
if (anchor)
@@ -98,43 +98,104 @@ struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
9898
}
9999

100100
/*
101-
* has_enough_free_count - whether ubi has enough free pebs to fill fm pools
101+
* wait_free_pebs_for_pool - wait until there enough free pebs
102+
* @ubi: UBI device description object
103+
*
104+
* Wait and execute do_work until there are enough free pebs, fill pool
105+
* as much as we can. This will reduce pool refilling times, which can
106+
* reduce the fastmap updating frequency.
107+
*/
108+
static void wait_free_pebs_for_pool(struct ubi_device *ubi)
109+
{
110+
struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
111+
struct ubi_fm_pool *pool = &ubi->fm_pool;
112+
int free, expect_free, executed;
113+
/*
114+
* There are at least following free pebs which reserved by UBI:
115+
* 1. WL_RESERVED_PEBS[1]
116+
* 2. EBA_RESERVED_PEBS[1]
117+
* 3. fm pebs - 1: Twice fastmap size deducted by fastmap and fm_anchor
118+
* 4. beb_rsvd_pebs: This value should be get under lock ubi->wl_lock
119+
*/
120+
int reserved = WL_RESERVED_PEBS + EBA_RESERVED_PEBS +
121+
ubi->fm_size / ubi->leb_size - 1 + ubi->fm_pool_rsv_cnt;
122+
123+
do {
124+
spin_lock(&ubi->wl_lock);
125+
free = ubi->free_count;
126+
free += pool->size - pool->used + wl_pool->size - wl_pool->used;
127+
expect_free = reserved + ubi->beb_rsvd_pebs;
128+
spin_unlock(&ubi->wl_lock);
129+
130+
/*
131+
* Break out if there are no works or work is executed failure,
132+
* given the fact that erase_worker will schedule itself when
133+
* -EBUSY is returned from mtd layer caused by system shutdown.
134+
*/
135+
if (do_work(ubi, &executed) || !executed)
136+
break;
137+
} while (free < expect_free);
138+
}
139+
140+
/*
141+
* left_free_count - returns the number of free pebs to fill fm pools
102142
* @ubi: UBI device description object
103-
* @is_wl_pool: whether UBI is filling wear leveling pool
104143
*
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.
144+
* This helper function returns the number of free pebs (deducted
145+
* by fastmap pebs) to fill fm_pool and fm_wl_pool.
111146
*/
112-
static bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool)
147+
static int left_free_count(struct ubi_device *ubi)
113148
{
114149
int fm_used = 0; // fastmap non anchor pebs.
115-
int beb_rsvd_pebs;
116150

117151
if (!ubi->free.rb_node)
118-
return false;
152+
return 0;
119153

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))
154+
if (!ubi->ro_mode && !ubi->fm_disabled)
122155
fm_used = ubi->fm_size / ubi->leb_size - 1;
123156

124-
return ubi->free_count - beb_rsvd_pebs > fm_used;
157+
return ubi->free_count - fm_used;
158+
}
159+
160+
/*
161+
* can_fill_pools - whether free PEBs will be left after filling pools
162+
* @ubi: UBI device description object
163+
* @free: current number of free PEBs
164+
*
165+
* Return %1 if there are still left free PEBs after filling pools,
166+
* otherwise %0 is returned.
167+
*/
168+
static int can_fill_pools(struct ubi_device *ubi, int free)
169+
{
170+
struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
171+
struct ubi_fm_pool *pool = &ubi->fm_pool;
172+
int pool_need = pool->max_size - pool->size +
173+
wl_pool->max_size - wl_pool->size;
174+
175+
if (free - pool_need < 1)
176+
return 0;
177+
178+
return 1;
125179
}
126180

127181
/**
128-
* ubi_refill_pools - refills all fastmap PEB pools.
182+
* ubi_refill_pools_and_lock - refills all fastmap PEB pools and takes fm locks.
129183
* @ubi: UBI device description object
130184
*/
131-
void ubi_refill_pools(struct ubi_device *ubi)
185+
void ubi_refill_pools_and_lock(struct ubi_device *ubi)
132186
{
133187
struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
134188
struct ubi_fm_pool *pool = &ubi->fm_pool;
135189
struct ubi_wl_entry *e;
136190
int enough;
137191

192+
if (!ubi->ro_mode && !ubi->fm_disabled)
193+
wait_free_pebs_for_pool(ubi);
194+
195+
down_write(&ubi->fm_protect);
196+
down_write(&ubi->work_sem);
197+
down_write(&ubi->fm_eba_sem);
198+
138199
spin_lock(&ubi->wl_lock);
139200

140201
return_unused_pool_pebs(ubi, wl_pool);
@@ -159,7 +220,7 @@ void ubi_refill_pools(struct ubi_device *ubi)
159220
for (;;) {
160221
enough = 0;
161222
if (pool->size < pool->max_size) {
162-
if (!has_enough_free_count(ubi, false))
223+
if (left_free_count(ubi) <= 0)
163224
break;
164225

165226
e = wl_get_wle(ubi);
@@ -172,10 +233,13 @@ void ubi_refill_pools(struct ubi_device *ubi)
172233
enough++;
173234

174235
if (wl_pool->size < wl_pool->max_size) {
175-
if (!has_enough_free_count(ubi, true))
236+
int left_free = left_free_count(ubi);
237+
238+
if (left_free <= 0)
176239
break;
177240

178-
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
241+
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF,
242+
!can_fill_pools(ubi, left_free));
179243
self_check_in_wl_tree(ubi, e, &ubi->free);
180244
rb_erase(&e->u.rb, &ubi->free);
181245
ubi->free_count--;
@@ -210,7 +274,7 @@ static int produce_free_peb(struct ubi_device *ubi)
210274

211275
while (!ubi->free.rb_node && ubi->works_count) {
212276
dbg_wl("do one work synchronously");
213-
err = do_work(ubi);
277+
err = do_work(ubi, NULL);
214278

215279
if (err)
216280
return err;
@@ -315,12 +379,12 @@ static bool need_wear_leveling(struct ubi_device *ubi)
315379
if (!e) {
316380
if (!ubi->free.rb_node)
317381
return false;
318-
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
382+
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
319383
ec = e->ec;
320384
} else {
321385
ec = e->ec;
322386
if (ubi->free.rb_node) {
323-
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
387+
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
324388
ec = max(ec, e->ec);
325389
}
326390
}
@@ -481,7 +545,7 @@ static void ubi_fastmap_close(struct ubi_device *ubi)
481545
static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
482546
struct ubi_wl_entry *e,
483547
struct rb_root *root) {
484-
if (e && !ubi->fm_disabled && !ubi->fm &&
548+
if (e && !ubi->fm_disabled && !ubi->fm && !ubi->fm_anchor &&
485549
e->pnum < UBI_FM_MAX_START)
486550
e = rb_entry(rb_next(root->rb_node),
487551
struct ubi_wl_entry, u.rb);

0 commit comments

Comments
 (0)