Skip to content

Commit ffad560

Browse files
baymotionmiquelraynal
authored andcommitted
mtd: phram: Allow the user to set the erase page size.
Permit the user to specify the erase page size as a parameter. This solves two problems: - phram can access images made by mkfs.jffs2. mkfs.jffs2 won't create images with erase sizes less than 8KiB; many architectures define PAGE_SIZE as 4KiB. - Allows more effective use of small capacity devices. JFFS2 needs somewhere between 2 and 5 empty pages for garbage collection; and for an NVRAM part with only 32KiB of space, a smaller erase page allows much better utilization in applications where garbage collection is important. Signed-off-by: Patrick O'Grady <patrick@baymotion.com> Reviewed-by: Joern Engel <joern@logfs.org> Link: https://lore.kernel.org/lkml/CAJ7m5OqYv_=JB9NhHsqBsa8YU0DFRoP7C+W10PY22wonAGJK=A@mail.gmail.com/ [Guohua Zhong: fix token array index out of bounds and update patch for kernel master branch] Signed-off-by: Guohua Zhong <zhongguohua1@huawei.com> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Link: https://lore.kernel.org/linux-mtd/20201207095529.20896-1-zhongguohua1@huawei.com
1 parent 1ca7141 commit ffad560

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

drivers/mtd/devices/phram.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* Usage:
77
*
88
* one commend line parameter per device, each in the form:
9-
* phram=<name>,<start>,<len>
9+
* phram=<name>,<start>,<len>[,<erasesize>]
1010
* <name> may be up to 63 characters.
11-
* <start> and <len> can be octal, decimal or hexadecimal. If followed
11+
* <start>, <len>, and <erasesize> can be octal, decimal or hexadecimal. If followed
1212
* by "ki", "Mi" or "Gi", the numbers will be interpreted as kilo, mega or
13-
* gigabytes.
13+
* gigabytes. <erasesize> is optional and defaults to PAGE_SIZE.
1414
*
1515
* Example:
16-
* phram=swap,64Mi,128Mi phram=test,900Mi,1Mi
16+
* phram=swap,64Mi,128Mi phram=test,900Mi,1Mi,64Ki
1717
*/
1818

1919
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -26,6 +26,7 @@
2626
#include <linux/moduleparam.h>
2727
#include <linux/slab.h>
2828
#include <linux/mtd/mtd.h>
29+
#include <asm/div64.h>
2930

3031
struct phram_mtd_list {
3132
struct mtd_info mtd;
@@ -88,7 +89,7 @@ static void unregister_devices(void)
8889
}
8990
}
9091

91-
static int register_device(char *name, phys_addr_t start, size_t len)
92+
static int register_device(char *name, phys_addr_t start, size_t len, uint32_t erasesize)
9293
{
9394
struct phram_mtd_list *new;
9495
int ret = -ENOMEM;
@@ -115,7 +116,7 @@ static int register_device(char *name, phys_addr_t start, size_t len)
115116
new->mtd._write = phram_write;
116117
new->mtd.owner = THIS_MODULE;
117118
new->mtd.type = MTD_RAM;
118-
new->mtd.erasesize = PAGE_SIZE;
119+
new->mtd.erasesize = erasesize;
119120
new->mtd.writesize = 1;
120121

121122
ret = -EAGAIN;
@@ -204,22 +205,23 @@ static inline void kill_final_newline(char *str)
204205
static int phram_init_called;
205206
/*
206207
* This shall contain the module parameter if any. It is of the form:
207-
* - phram=<device>,<address>,<size> for module case
208-
* - phram.phram=<device>,<address>,<size> for built-in case
209-
* We leave 64 bytes for the device name, 20 for the address and 20 for the
210-
* size.
211-
* Example: phram.phram=rootfs,0xa0000000,512Mi
208+
* - phram=<device>,<address>,<size>[,<erasesize>] for module case
209+
* - phram.phram=<device>,<address>,<size>[,<erasesize>] for built-in case
210+
* We leave 64 bytes for the device name, 20 for the address , 20 for the
211+
* size and 20 for the erasesize.
212+
* Example: phram.phram=rootfs,0xa0000000,512Mi,65536
212213
*/
213-
static char phram_paramline[64 + 20 + 20];
214+
static char phram_paramline[64 + 20 + 20 + 20];
214215
#endif
215216

216217
static int phram_setup(const char *val)
217218
{
218-
char buf[64 + 20 + 20], *str = buf;
219-
char *token[3];
219+
char buf[64 + 20 + 20 + 20], *str = buf;
220+
char *token[4];
220221
char *name;
221222
uint64_t start;
222223
uint64_t len;
224+
uint64_t erasesize = PAGE_SIZE;
223225
int i, ret;
224226

225227
if (strnlen(val, sizeof(buf)) >= sizeof(buf))
@@ -228,7 +230,7 @@ static int phram_setup(const char *val)
228230
strcpy(str, val);
229231
kill_final_newline(str);
230232

231-
for (i = 0; i < 3; i++)
233+
for (i = 0; i < 4; i++)
232234
token[i] = strsep(&str, ",");
233235

234236
if (str)
@@ -253,11 +255,25 @@ static int phram_setup(const char *val)
253255
goto error;
254256
}
255257

256-
ret = register_device(name, start, len);
258+
if (token[3]) {
259+
ret = parse_num64(&erasesize, token[3]);
260+
if (ret) {
261+
parse_err("illegal erasesize\n");
262+
goto error;
263+
}
264+
}
265+
266+
if (len == 0 || erasesize == 0 || erasesize > len
267+
|| erasesize > UINT_MAX || do_div(len, (uint32_t)erasesize) != 0) {
268+
parse_err("illegal erasesize or len\n");
269+
goto error;
270+
}
271+
272+
ret = register_device(name, start, len, (uint32_t)erasesize);
257273
if (ret)
258274
goto error;
259275

260-
pr_info("%s device: %#llx at %#llx\n", name, len, start);
276+
pr_info("%s device: %#llx at %#llx for erasesize %#llx\n", name, len, start, erasesize);
261277
return 0;
262278

263279
error:
@@ -298,7 +314,7 @@ static int phram_param_call(const char *val, const struct kernel_param *kp)
298314
}
299315

300316
module_param_call(phram, phram_param_call, NULL, NULL, 0200);
301-
MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>\"");
317+
MODULE_PARM_DESC(phram, "Memory region to map. \"phram=<name>,<start>,<length>[,<erasesize>]\"");
302318

303319

304320
static int __init init_phram(void)

0 commit comments

Comments
 (0)