Skip to content

Commit eab7377

Browse files
ezequielgarciadedekind
authored andcommitted
UBI: embed ubi_debug_info field in ubi_device struct
ubi_debug_info struct was dynamically allocated which is always suboptimal, for it tends to fragment memory and make the code error-prone. Fix this by embedding it in ubi_device struct. Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
1 parent 6457557 commit eab7377

File tree

4 files changed

+50
-85
lines changed

4 files changed

+50
-85
lines changed

drivers/mtd/ubi/build.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -985,14 +985,10 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
985985
if (!ubi->fm_buf)
986986
goto out_free;
987987
#endif
988-
err = ubi_debugging_init_dev(ubi);
989-
if (err)
990-
goto out_free;
991-
992988
err = ubi_attach(ubi, 0);
993989
if (err) {
994990
ubi_err("failed to attach mtd%d, error %d", mtd->index, err);
995-
goto out_debugging;
991+
goto out_free;
996992
}
997993

998994
if (ubi->autoresize_vol_id != -1) {
@@ -1059,8 +1055,6 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
10591055
ubi_wl_close(ubi);
10601056
ubi_free_internal_volumes(ubi);
10611057
vfree(ubi->vtbl);
1062-
out_debugging:
1063-
ubi_debugging_exit_dev(ubi);
10641058
out_free:
10651059
vfree(ubi->peb_buf);
10661060
vfree(ubi->fm_buf);
@@ -1138,7 +1132,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
11381132
ubi_free_internal_volumes(ubi);
11391133
vfree(ubi->vtbl);
11401134
put_mtd_device(ubi->mtd);
1141-
ubi_debugging_exit_dev(ubi);
11421135
vfree(ubi->peb_buf);
11431136
vfree(ubi->fm_buf);
11441137
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: 6 additions & 46 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,18 +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

161121
static inline int ubi_dbg_chk_io(const struct ubi_device *ubi)
162122
{
163-
return ubi->dbg->chk_io;
123+
return ubi->dbg.chk_io;
164124
}
165125

166126
static inline int ubi_dbg_chk_gen(const struct ubi_device *ubi)
167127
{
168-
return ubi->dbg->chk_gen;
128+
return ubi->dbg.chk_gen;
169129
}
170130
#endif /* !__UBI_DEBUG_H__ */

drivers/mtd/ubi/ubi.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
/* The volume ID/LEB number/erase counter is unknown */
8585
#define UBI_UNKNOWN -1
8686

87+
/*
88+
* The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
89+
* + 2 for the number plus 1 for the trailing zero byte.
90+
*/
91+
#define UBI_DFS_DIR_NAME "ubi%d"
92+
#define UBI_DFS_DIR_LEN (3 + 2 + 1)
93+
8794
/*
8895
* Error codes returned by the I/O sub-system.
8996
*
@@ -341,6 +348,37 @@ struct ubi_volume_desc {
341348

342349
struct ubi_wl_entry;
343350

351+
/**
352+
* struct ubi_debug_info - debugging information for an UBI device.
353+
*
354+
* @chk_gen: if UBI general extra checks are enabled
355+
* @chk_io: if UBI I/O extra checks are enabled
356+
* @disable_bgt: disable the background task for testing purposes
357+
* @emulate_bitflips: emulate bit-flips for testing purposes
358+
* @emulate_io_failures: emulate write/erase failures for testing purposes
359+
* @dfs_dir_name: name of debugfs directory containing files of this UBI device
360+
* @dfs_dir: direntry object of the UBI device debugfs directory
361+
* @dfs_chk_gen: debugfs knob to enable UBI general extra checks
362+
* @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
363+
* @dfs_disable_bgt: debugfs knob to disable the background task
364+
* @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
365+
* @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
366+
*/
367+
struct ubi_debug_info {
368+
unsigned int chk_gen:1;
369+
unsigned int chk_io:1;
370+
unsigned int disable_bgt:1;
371+
unsigned int emulate_bitflips:1;
372+
unsigned int emulate_io_failures:1;
373+
char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
374+
struct dentry *dfs_dir;
375+
struct dentry *dfs_chk_gen;
376+
struct dentry *dfs_chk_io;
377+
struct dentry *dfs_disable_bgt;
378+
struct dentry *dfs_emulate_bitflips;
379+
struct dentry *dfs_emulate_io_failures;
380+
};
381+
344382
/**
345383
* struct ubi_device - UBI device description structure
346384
* @dev: UBI device object to use the the Linux device model
@@ -545,7 +583,7 @@ struct ubi_device {
545583
struct mutex buf_mutex;
546584
struct mutex ckvol_mutex;
547585

548-
struct ubi_debug_info *dbg;
586+
struct ubi_debug_info dbg;
549587
};
550588

551589
/**

0 commit comments

Comments
 (0)