Skip to content

Commit 85d5b70

Browse files
committed
Merge tag 'upstream-3.8-rc1' of git://git.infradead.org/linux-ubi
Pull UBI update from Artem Bityutskiy: "Nothing exciting, just clean-ups and nicification. Oh, and one small optimization which makes UBI to use less RAM." * tag 'upstream-3.8-rc1' of git://git.infradead.org/linux-ubi: UBI: embed ubi_debug_info field in ubi_device struct UBI: introduce helpers dbg_chk_{io, gen} UBI: replace memcpy with struct assignment UBI: remove spurious comment UBI: gluebi: rename misleading variables UBI: do not allocate the memory unnecessarily UBI: use list_move_tail instead of list_del/list_add_tail
2 parents 43260ad + eab7377 commit 85d5b70

File tree

12 files changed

+98
-135
lines changed

12 files changed

+98
-135
lines changed

drivers/mtd/ubi/attach.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai,
322322
int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
323323
int pnum, const struct ubi_vid_hdr *vid_hdr)
324324
{
325-
void *buf;
326325
int len, err, second_is_newer, bitflips = 0, corrupted = 0;
327326
uint32_t data_crc, crc;
328327
struct ubi_vid_hdr *vh = NULL;
@@ -393,18 +392,14 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
393392
/* Read the data of the copy and check the CRC */
394393

395394
len = be32_to_cpu(vid_hdr->data_size);
396-
buf = vmalloc(len);
397-
if (!buf) {
398-
err = -ENOMEM;
399-
goto out_free_vidh;
400-
}
401395

402-
err = ubi_io_read_data(ubi, buf, pnum, 0, len);
396+
mutex_lock(&ubi->buf_mutex);
397+
err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len);
403398
if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
404-
goto out_free_buf;
399+
goto out_unlock;
405400

406401
data_crc = be32_to_cpu(vid_hdr->data_crc);
407-
crc = crc32(UBI_CRC32_INIT, buf, len);
402+
crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len);
408403
if (crc != data_crc) {
409404
dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
410405
pnum, crc, data_crc);
@@ -415,8 +410,8 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
415410
dbg_bld("PEB %d CRC is OK", pnum);
416411
bitflips = !!err;
417412
}
413+
mutex_unlock(&ubi->buf_mutex);
418414

419-
vfree(buf);
420415
ubi_free_vid_hdr(ubi, vh);
421416

422417
if (second_is_newer)
@@ -426,8 +421,8 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
426421

427422
return second_is_newer | (bitflips << 1) | (corrupted << 2);
428423

429-
out_free_buf:
430-
vfree(buf);
424+
out_unlock:
425+
mutex_unlock(&ubi->buf_mutex);
431426
out_free_vidh:
432427
ubi_free_vid_hdr(ubi, vh);
433428
return err;
@@ -1453,7 +1448,7 @@ int ubi_attach(struct ubi_device *ubi, int force_scan)
14531448
goto out_wl;
14541449

14551450
#ifdef CONFIG_MTD_UBI_FASTMAP
1456-
if (ubi->fm && ubi->dbg->chk_gen) {
1451+
if (ubi->fm && ubi_dbg_chk_gen(ubi)) {
14571452
struct ubi_attach_info *scan_ai;
14581453

14591454
scan_ai = alloc_ai("ubi_ckh_aeb_slab_cache");
@@ -1503,7 +1498,7 @@ static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
15031498
struct ubi_ainf_peb *aeb, *last_aeb;
15041499
uint8_t *buf;
15051500

1506-
if (!ubi->dbg->chk_gen)
1501+
if (!ubi_dbg_chk_gen(ubi))
15071502
return 0;
15081503

15091504
/*

drivers/mtd/ubi/build.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,7 @@ static int autoresize(struct ubi_device *ubi, int vol_id)
825825
* No available PEBs to re-size the volume, clear the flag on
826826
* flash and exit.
827827
*/
828-
memcpy(&vtbl_rec, &ubi->vtbl[vol_id],
829-
sizeof(struct ubi_vtbl_record));
828+
vtbl_rec = ubi->vtbl[vol_id];
830829
err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
831830
if (err)
832831
ubi_err("cannot clean auto-resize flag for volume %d",
@@ -986,14 +985,10 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
986985
if (!ubi->fm_buf)
987986
goto out_free;
988987
#endif
989-
err = ubi_debugging_init_dev(ubi);
990-
if (err)
991-
goto out_free;
992-
993988
err = ubi_attach(ubi, 0);
994989
if (err) {
995990
ubi_err("failed to attach mtd%d, error %d", mtd->index, err);
996-
goto out_debugging;
991+
goto out_free;
997992
}
998993

999994
if (ubi->autoresize_vol_id != -1) {
@@ -1060,8 +1055,6 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
10601055
ubi_wl_close(ubi);
10611056
ubi_free_internal_volumes(ubi);
10621057
vfree(ubi->vtbl);
1063-
out_debugging:
1064-
ubi_debugging_exit_dev(ubi);
10651058
out_free:
10661059
vfree(ubi->peb_buf);
10671060
vfree(ubi->fm_buf);
@@ -1139,7 +1132,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
11391132
ubi_free_internal_volumes(ubi);
11401133
vfree(ubi->vtbl);
11411134
put_mtd_device(ubi->mtd);
1142-
ubi_debugging_exit_dev(ubi);
11431135
vfree(ubi->peb_buf);
11441136
vfree(ubi->fm_buf);
11451137
ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);

drivers/mtd/ubi/debug.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,6 @@ void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req)
217217
pr_err("\t1st 16 characters of name: %s\n", nm);
218218
}
219219

220-
/**
221-
* ubi_debugging_init_dev - initialize debugging for an UBI device.
222-
* @ubi: UBI device description object
223-
*
224-
* This function initializes debugging-related data for UBI device @ubi.
225-
* Returns zero in case of success and a negative error code in case of
226-
* failure.
227-
*/
228-
int ubi_debugging_init_dev(struct ubi_device *ubi)
229-
{
230-
ubi->dbg = kzalloc(sizeof(struct ubi_debug_info), GFP_KERNEL);
231-
if (!ubi->dbg)
232-
return -ENOMEM;
233-
234-
return 0;
235-
}
236-
237-
/**
238-
* ubi_debugging_exit_dev - free debugging data for an UBI device.
239-
* @ubi: UBI device description object
240-
*/
241-
void ubi_debugging_exit_dev(struct ubi_device *ubi)
242-
{
243-
kfree(ubi->dbg);
244-
}
245-
246220
/*
247221
* Root directory for UBI stuff in debugfs. Contains sub-directories which
248222
* contain the stuff specific to particular UBI devices.
@@ -295,7 +269,7 @@ static ssize_t dfs_file_read(struct file *file, char __user *user_buf,
295269
ubi = ubi_get_device(ubi_num);
296270
if (!ubi)
297271
return -ENODEV;
298-
d = ubi->dbg;
272+
d = &ubi->dbg;
299273

300274
if (dent == d->dfs_chk_gen)
301275
val = d->chk_gen;
@@ -341,7 +315,7 @@ static ssize_t dfs_file_write(struct file *file, const char __user *user_buf,
341315
ubi = ubi_get_device(ubi_num);
342316
if (!ubi)
343317
return -ENODEV;
344-
d = ubi->dbg;
318+
d = &ubi->dbg;
345319

346320
buf_size = min_t(size_t, count, (sizeof(buf) - 1));
347321
if (copy_from_user(buf, user_buf, buf_size)) {
@@ -398,7 +372,7 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi)
398372
unsigned long ubi_num = ubi->ubi_num;
399373
const char *fname;
400374
struct dentry *dent;
401-
struct ubi_debug_info *d = ubi->dbg;
375+
struct ubi_debug_info *d = &ubi->dbg;
402376

403377
if (!IS_ENABLED(CONFIG_DEBUG_FS))
404378
return 0;
@@ -471,5 +445,5 @@ int ubi_debugfs_init_dev(struct ubi_device *ubi)
471445
void ubi_debugfs_exit_dev(struct ubi_device *ubi)
472446
{
473447
if (IS_ENABLED(CONFIG_DEBUG_FS))
474-
debugfs_remove_recursive(ubi->dbg->dfs_dir);
448+
debugfs_remove_recursive(ubi->dbg.dfs_dir);
475449
}

drivers/mtd/ubi/debug.h

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,51 +60,11 @@ void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type);
6060
void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req);
6161
int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
6262
int len);
63-
int ubi_debugging_init_dev(struct ubi_device *ubi);
64-
void ubi_debugging_exit_dev(struct ubi_device *ubi);
6563
int ubi_debugfs_init(void);
6664
void ubi_debugfs_exit(void);
6765
int ubi_debugfs_init_dev(struct ubi_device *ubi);
6866
void ubi_debugfs_exit_dev(struct ubi_device *ubi);
6967

70-
/*
71-
* The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
72-
* + 2 for the number plus 1 for the trailing zero byte.
73-
*/
74-
#define UBI_DFS_DIR_NAME "ubi%d"
75-
#define UBI_DFS_DIR_LEN (3 + 2 + 1)
76-
77-
/**
78-
* struct ubi_debug_info - debugging information for an UBI device.
79-
*
80-
* @chk_gen: if UBI general extra checks are enabled
81-
* @chk_io: if UBI I/O extra checks are enabled
82-
* @disable_bgt: disable the background task for testing purposes
83-
* @emulate_bitflips: emulate bit-flips for testing purposes
84-
* @emulate_io_failures: emulate write/erase failures for testing purposes
85-
* @dfs_dir_name: name of debugfs directory containing files of this UBI device
86-
* @dfs_dir: direntry object of the UBI device debugfs directory
87-
* @dfs_chk_gen: debugfs knob to enable UBI general extra checks
88-
* @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
89-
* @dfs_disable_bgt: debugfs knob to disable the background task
90-
* @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
91-
* @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
92-
*/
93-
struct ubi_debug_info {
94-
unsigned int chk_gen:1;
95-
unsigned int chk_io:1;
96-
unsigned int disable_bgt:1;
97-
unsigned int emulate_bitflips:1;
98-
unsigned int emulate_io_failures:1;
99-
char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
100-
struct dentry *dfs_dir;
101-
struct dentry *dfs_chk_gen;
102-
struct dentry *dfs_chk_io;
103-
struct dentry *dfs_disable_bgt;
104-
struct dentry *dfs_emulate_bitflips;
105-
struct dentry *dfs_emulate_io_failures;
106-
};
107-
10868
/**
10969
* ubi_dbg_is_bgt_disabled - if the background thread is disabled.
11070
* @ubi: UBI device description object
@@ -114,7 +74,7 @@ struct ubi_debug_info {
11474
*/
11575
static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
11676
{
117-
return ubi->dbg->disable_bgt;
77+
return ubi->dbg.disable_bgt;
11878
}
11979

12080
/**
@@ -125,7 +85,7 @@ static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
12585
*/
12686
static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
12787
{
128-
if (ubi->dbg->emulate_bitflips)
88+
if (ubi->dbg.emulate_bitflips)
12989
return !(random32() % 200);
13090
return 0;
13191
}
@@ -139,7 +99,7 @@ static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
13999
*/
140100
static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
141101
{
142-
if (ubi->dbg->emulate_io_failures)
102+
if (ubi->dbg.emulate_io_failures)
143103
return !(random32() % 500);
144104
return 0;
145105
}
@@ -153,9 +113,18 @@ static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
153113
*/
154114
static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
155115
{
156-
if (ubi->dbg->emulate_io_failures)
116+
if (ubi->dbg.emulate_io_failures)
157117
return !(random32() % 400);
158118
return 0;
159119
}
160120

121+
static inline int ubi_dbg_chk_io(const struct ubi_device *ubi)
122+
{
123+
return ubi->dbg.chk_io;
124+
}
125+
126+
static inline int ubi_dbg_chk_gen(const struct ubi_device *ubi)
127+
{
128+
return ubi->dbg.chk_gen;
129+
}
161130
#endif /* !__UBI_DEBUG_H__ */

drivers/mtd/ubi/fastmap.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,8 @@ static int ubi_attach_fastmap(struct ubi_device *ubi,
814814
if (max_sqnum > ai->max_sqnum)
815815
ai->max_sqnum = max_sqnum;
816816

817-
list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) {
818-
list_del(&tmp_aeb->u.list);
819-
list_add_tail(&tmp_aeb->u.list, &ai->free);
820-
}
817+
list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list)
818+
list_move_tail(&tmp_aeb->u.list, &ai->free);
821819

822820
/*
823821
* If fastmap is leaking PEBs (must not happen), raise a

drivers/mtd/ubi/gluebi.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,29 +171,29 @@ static void gluebi_put_device(struct mtd_info *mtd)
171171
static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
172172
size_t *retlen, unsigned char *buf)
173173
{
174-
int err = 0, lnum, offs, total_read;
174+
int err = 0, lnum, offs, bytes_left;
175175
struct gluebi_device *gluebi;
176176

177177
gluebi = container_of(mtd, struct gluebi_device, mtd);
178178
lnum = div_u64_rem(from, mtd->erasesize, &offs);
179-
total_read = len;
180-
while (total_read) {
179+
bytes_left = len;
180+
while (bytes_left) {
181181
size_t to_read = mtd->erasesize - offs;
182182

183-
if (to_read > total_read)
184-
to_read = total_read;
183+
if (to_read > bytes_left)
184+
to_read = bytes_left;
185185

186186
err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
187187
if (err)
188188
break;
189189

190190
lnum += 1;
191191
offs = 0;
192-
total_read -= to_read;
192+
bytes_left -= to_read;
193193
buf += to_read;
194194
}
195195

196-
*retlen = len - total_read;
196+
*retlen = len - bytes_left;
197197
return err;
198198
}
199199

@@ -211,7 +211,7 @@ static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
211211
static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
212212
size_t *retlen, const u_char *buf)
213213
{
214-
int err = 0, lnum, offs, total_written;
214+
int err = 0, lnum, offs, bytes_left;
215215
struct gluebi_device *gluebi;
216216

217217
gluebi = container_of(mtd, struct gluebi_device, mtd);
@@ -220,24 +220,24 @@ static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
220220
if (len % mtd->writesize || offs % mtd->writesize)
221221
return -EINVAL;
222222

223-
total_written = len;
224-
while (total_written) {
223+
bytes_left = len;
224+
while (bytes_left) {
225225
size_t to_write = mtd->erasesize - offs;
226226

227-
if (to_write > total_written)
228-
to_write = total_written;
227+
if (to_write > bytes_left)
228+
to_write = bytes_left;
229229

230230
err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
231231
if (err)
232232
break;
233233

234234
lnum += 1;
235235
offs = 0;
236-
total_written -= to_write;
236+
bytes_left -= to_write;
237237
buf += to_write;
238238
}
239239

240-
*retlen = len - total_written;
240+
*retlen = len - bytes_left;
241241
return err;
242242
}
243243

0 commit comments

Comments
 (0)