mtd: ubi: attach MTD partition from device-tree
Subsystems (2)
PATCH 1/1 mtd: ubi: attach MTD partition from device-tree
Split ubi_init() function into early function to be called by device_initcall() and keep cmdline attachment in late_initcall(). (when building ubi as module, both is still done in a single module_init() call)
Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g. drivers/nvmem/u-boot-env.c can now be extended to support U-Boot environment stored in UBI volumes.
--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)
drivers/mtd/ubi/block.c +7 -13
@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;
@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;
@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) { -pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT ) +pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }
@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes.
drivers/mtd/ubi/build.c +121 -42
240 lines not on this page · raw message
drivers/mtd/ubi/cdev.c +1 -1
@@ -1065,7 +1065,7 @@ static long ctrl_cdev_ioctl(struct file *file, unsigned int cmd, }mutex_lock (&ubi_devices_mutex); - err =ubi_detach_mtd_dev (ubi_num,0 ); + err =ubi_detach_mtd_dev (ubi_num,0 , false);mutex_unlock (&ubi_devices_mutex);break ; }
drivers/mtd/ubi/ubi.h +3 -1
@@ -939,7 +939,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,int ubi_attach_mtd_dev (struct mtd_info *mtd,int ubi_num,int vid_hdr_offset,int max_beb_per1024,bool disable_fm); -int ubi_detach_mtd_dev (int ubi_num,int anyway); +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock);struct ubi_device *ubi_get_device (int ubi_num);void ubi_put_device (struct ubi_device *ubi);struct ubi_device *ubi_get_by_major (int major);
@@ -976,11 +976,13 @@ static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}/* block.c */ #ifdef CONFIG_MTD_UBI_BLOCK +void ubiblock_create_from_param (void );int ubiblock_init (void );void ubiblock_exit (void );int ubiblock_create (struct ubi_volume_info *vi);int ubiblock_remove (struct ubi_volume_info *vi);#else +static inline void ubiblock_create_from_param (void ) {}static inline int ubiblock_init (void ) {return 0 ; }static inline void ubiblock_exit (void ) {}static inline int ubiblock_create (struct ubi_volume_info *vi)
base-commit: 67d5d9f013d6c3829383c08162939cabff14fccc
Signature · 1 line
2.40.0
Zhihao Cheng
reviewerHi Daniel,
Quoted 17 lines
Split
ubi_init()function into early function to be called bydevice_initcall()and keep cmdline attachment inlate_initcall(). (when building ubi as module, both is still done in a singlemodule_init()call)Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
Quoted 8 lines
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g.
drivers/nvmem/u-boot-env.ccan now be extended to support U-Boot environment stored in UBI volumes.
Let me try to understand this patch, the main purpose is to add a new way to load ubi according to device tree, am I right?
Quoted 35 lines
Signed-off-by Daniel Golle <[email protected]>--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)drivers/mtd/ubi/block.c +3
-3@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;
drivers/mtd/ubi/block.c static void __init ubiblock_create_from_param(void) · line 614
Quoted 7 lines
drivers/mtd/ubi/block.c +1
-3@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) { -pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT )
The EEXIST and ENOENT types of error messages are filtered, why? After this patch applied, the user won't be aware of which ubi volume failed to create ubiblock.
Quoted 50 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);
Why do you skip updating fastmap if ubi is detached in mtd notification way?
[...]> +static void ubi_notify_remove(struct mtd_info *mtd)
Quoted 6 lines
drivers/mtd/ubi/build.c +2
-3+{ +int i; - ubi_wl_entry_slab =kmem_cache_create ("ubi_wl_entry_slab" , -sizeof (struct ubi_wl_entry ), -0 ,0 ,NULL );
drivers/mtd/ubi/build.c static struct mtd_info * __init open_mtd_device(const char *mtd_dev) · line 1253
Quoted 10 lines
drivers/mtd/ubi/build.c +6
-4-if (!ubi_wl_entry_slab) { - err = -ENOMEM ; - goto out_dev_unreg; - } +/* called while holding mtd_table_mutex */ +mutex_lock_nested (&ubi_devices_mutex,SINGLE_DEPTH_NESTING ); +for (i =0 ; i <UBI_MAX_DEVICES ; i++) +if (ubi_devices[i] && + ubi_devices[i]->mtd->index == mtd->index) +ubi_detach_mtd_dev (ubi_devices[i]->ubi_num,1 , true);
Add a 'break'?
Hi Zhihao,
thank you for taking the time to review this patch.
Quoted 22 lines · Zhihao Cheng
Hi Daniel,
Quoted 21 lines
Split
ubi_init()function into early function to be called bydevice_initcall()and keep cmdline attachment inlate_initcall(). (when building ubi as module, both is still done in a singlemodule_init()call)Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Quoted 7 lines
Quoted 4 lines
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g.
drivers/nvmem/u-boot-env.ccan now be extended to support U-Boot environment stored in UBI volumes.Let me try to understand this patch, the main purpose is to add a new way to load ubi according to device tree, am I right?
You are right about one aspect of this patch, yes it does add another way to attach MTD devices, by utilizing a devicetree compatible 'linux,ubi' which can be set for MTD devices (or partitions). The other, and maybe even more significant aspect is that it moves attaching the MTD device from being a late_initcall() to rather register an MTD user driver instead and hence happen as a direct consequence of the MTD device showing up.
This will be important when using UBI e.g. as an NVMEM provider:
E.g. on Wi-Fi routers it has become common for vendors to store the bootloader environment, Ethernet MAC addresses or Wi-Fi calibration data inside an UBI volume (and I don't mean as a file inside a filesystem, but really just the UBI volume exposing nvmem cells for other drivers). See [1] for an example.
Quoted 34 lines
Quoted 34 lines
Signed-off-by Daniel Golle <[email protected]>--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)drivers/mtd/ubi/block.c +3
-3@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) {
Quoted 8 lines
drivers/mtd/ubi/block.c static void __init ubiblock_create_from_param(void) · line 614
Quoted 4 lines
drivers/mtd/ubi/block.c +1
-3-pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT )The EEXIST and ENOENT types of error messages are filtered, why? After this patch applied, the user won't be aware of which ubi volume failed to create ubiblock.
The EEXIST and ENOENT errors as filtered as they may happen now without that actually being an error.ubiblock_create_from_param is potentially being called multiple times, each time a new UBI device is attached in the "new" MTD user/device-tree way, and also once in "classic" late_initcall way.
One way to partially resolve this could be to add a boolean parameter to ubiblock_create_from_param which indicates if -ENOENT should be treated as an error, and set that parameter only when calling from late_initcall.
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Quoted 49 lines
Quoted 49 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Quoted 11 lines
[...]> +static void
ubi_notify_remove(struct mtd_info *mtd)Quoted 9 lines
drivers/mtd/ubi/build.c +2
-7+{ +int i; - ubi_wl_entry_slab =kmem_cache_create ("ubi_wl_entry_slab" , -sizeof (struct ubi_wl_entry ), -0 ,0 ,NULL ); -if (!ubi_wl_entry_slab) { - err = -ENOMEM ; - goto out_dev_unreg; - }
Quoted 8 lines
drivers/mtd/ubi/build.c static struct mtd_info * __init open_mtd_device(const char *mtd_dev) · line 1253
Quoted 6 lines
drivers/mtd/ubi/build.c +6
-0+/* called while holding mtd_table_mutex */ +mutex_lock_nested (&ubi_devices_mutex,SINGLE_DEPTH_NESTING ); +for (i =0 ; i <UBI_MAX_DEVICES ; i++) +if (ubi_devices[i] && + ubi_devices[i]->mtd->index == mtd->index) +ubi_detach_mtd_dev (ubi_devices[i]->ubi_num,1 , true);Add a 'break'?
Good point :)
Will add 'break' statement here.
[1]: https://forum.openwrt.org/t/asus-tuf-ax4200-support/155738/44?u=daniel
Zhihao Cheng
reviewerQuoted 35 lines · Daniel Golle
Hi Zhihao,
thank you for taking the time to review this patch.
Quoted 22 lines · Zhihao Cheng
Hi Daniel,
Quoted 21 lines
Split
ubi_init()function into early function to be called bydevice_initcall()and keep cmdline attachment inlate_initcall(). (when building ubi as module, both is still done in a singlemodule_init()call)Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Quoted 7 lines
Quoted 4 lines
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g.
drivers/nvmem/u-boot-env.ccan now be extended to support U-Boot environment stored in UBI volumes.Let me try to understand this patch, the main purpose is to add a new way to load ubi according to device tree, am I right?
Quoted 7 lines
You are right about one aspect of this patch, yes it does add another way to attach MTD devices, by utilizing a devicetree compatible 'linux,ubi' which can be set for MTD devices (or partitions). The other, and maybe even more significant aspect is that it moves attaching the MTD device from being a
late_initcall()to rather register an MTD user driver instead and hence happen as a direct consequence of the MTD device showing up.
It means mtd device probing and loading will autimically triggers ubi attaching by ubi_notify_add(), right?
If so, I thought of a scenario like this: The mtd device probing/removing will autimically loading/removing UBI device, now it can be done without any orders(cmdlines/ioctl) in running time. I think it's valuable.
Quoted 7 lines
This will be important when using UBI e.g. as an NVMEM provider:
E.g. on Wi-Fi routers it has become common for vendors to store the bootloader environment, Ethernet MAC addresses or Wi-Fi calibration data inside an UBI volume (and I don't mean as a file inside a filesystem, but really just the UBI volume exposing nvmem cells for other drivers). See [1] for an example.
Thanks for introducing the background.
Quoted 43 lines
Quoted 34 lines
Quoted 34 lines
Signed-off-by Daniel Golle <[email protected]>--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)drivers/mtd/ubi/block.c +3
-3@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) {Quoted 8 lines
drivers/mtd/ubi/block.c static void __init ubiblock_create_from_param(void) · line 614
Quoted 4 lines
drivers/mtd/ubi/block.c +1
-3-pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT )The EEXIST and ENOENT types of error messages are filtered, why? After this patch applied, the user won't be aware of which ubi volume failed to create ubiblock.
Quoted 6 lines
The EEXIST and ENOENT errors as filtered as they may happen now without that actually being an error.
ubiblock_create_from_paramis potentially being called multiple times, each time a new UBI device is attached in the "new" MTD user/device-tree way, and also once in "classic"late_initcallway.
I agree, the second caller could trigger EEXIST. But how ENOENT happens, do you mean ubi_notify_add->ubiblock_create_from_param->open_volume_desc? Could you show me path?
Quoted 5 lines
One way to partially resolve this could be to add a boolean parameter to
ubiblock_create_from_paramwhich indicates if-ENOENTshould be treated as an error, and set that parameter only when calling fromlate_initcall.
Quoted 8 lines
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Emm, I prefer to fix it, it's weird that ubiblock_create_from_param() being executed multiple times(Each ubi_notify_add() will call once, late_initcall will call it again). User could see many false positive error messages.
Quoted 59 lines
Quoted 50 lines
Quoted 50 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
Quoted 7 lines
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Another path still operate mtd device:ubi_detach_mtd_dev -> ubi_wl_close -> shutdown_work -> wrk->func -> erase_worker -> sync_erase -> ubi_io_sync_erase/ubi_io_write_ec_hdr.
Besides, can we remove any mtd specific driver(eg. phram/nandsim) by rmmod? I notice that get_mtd_device(called before ubi attaching) will increase specific mtd driver's module refcnt. If we physically remove the mtd device, will it trigger del_mtd_device()? For example, when is phram_remove() called?
Quoted 110 lines · Zhihao Cheng
Quoted 35 lines · Daniel Golle
Hi Zhihao,
thank you for taking the time to review this patch.
Quoted 22 lines · Zhihao Cheng
Hi Daniel,
Quoted 21 lines
Split
ubi_init()function into early function to be called bydevice_initcall()and keep cmdline attachment inlate_initcall(). (when building ubi as module, both is still done in a singlemodule_init()call)Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Quoted 7 lines
Quoted 4 lines
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g.
drivers/nvmem/u-boot-env.ccan now be extended to support U-Boot environment stored in UBI volumes.Let me try to understand this patch, the main purpose is to add a new way to load ubi according to device tree, am I right?
Quoted 7 lines
You are right about one aspect of this patch, yes it does add another way to attach MTD devices, by utilizing a devicetree compatible 'linux,ubi' which can be set for MTD devices (or partitions). The other, and maybe even more significant aspect is that it moves attaching the MTD device from being a
late_initcall()to rather register an MTD user driver instead and hence happen as a direct consequence of the MTD device showing up.It means mtd device probing and loading will autimically triggers ubi attaching by
ubi_notify_add(), right?
If so, I thought of a scenario like this: The mtd device probing/removing will autimically loading/removing UBI device, now it can be done without any orders(cmdlines/ioctl) in running time. I think it's valuable.Quoted 7 lines
This will be important when using UBI e.g. as an NVMEM provider:
E.g. on Wi-Fi routers it has become common for vendors to store the bootloader environment, Ethernet MAC addresses or Wi-Fi calibration data inside an UBI volume (and I don't mean as a file inside a filesystem, but really just the UBI volume exposing nvmem cells for other drivers). See [1] for an example.Thanks for introducing the background.
Quoted 43 lines
Quoted 34 lines
Quoted 34 lines
Signed-off-by Daniel Golle <[email protected]>--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)drivers/mtd/ubi/block.c +3
-3@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) {Quoted 8 lines
drivers/mtd/ubi/block.c static void __init ubiblock_create_from_param(void) · line 614
Quoted 4 lines
drivers/mtd/ubi/block.c +1
-3-pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT )The EEXIST and ENOENT types of error messages are filtered, why? After this patch applied, the user won't be aware of which ubi volume failed to create ubiblock.
Quoted 6 lines
The EEXIST and ENOENT errors as filtered as they may happen now without that actually being an error.
ubiblock_create_from_paramis potentially being called multiple times, each time a new UBI device is attached in the "new" MTD user/device-tree way, and also once in "classic"late_initcallway.
Quoted 3 lines
I agree, the second caller could trigger EEXIST. But how ENOENT happens, do you mean
ubi_notify_add->ubiblock_create_from_param->open_volume_desc? Could you show me path?
You are right, EEXIST will actually never happen. If at all then we should check if open_volume_desc returns ERR_PTR(-ENODEV). However, we can never know if the UBI device containing the volume we want to have exposed as ubiblock device is still going to be added later when the driver module for the MTD device is loaded. Hence I suggest to simply not call pr_err(...) in case open_volume_desc returns ERR_PTR(-ENODEV) as well as for the case that ubiblock_create returns -EEXIST.
Quoted 14 lines
Quoted 5 lines
One way to partially resolve this could be to add a boolean parameter to
ubiblock_create_from_paramwhich indicates if-ENOENTshould be treated as an error, and set that parameter only when calling fromlate_initcall.Quoted 8 lines
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Quoted 3 lines
Emm, I prefer to fix it, it's weird that
ubiblock_create_from_param()being executed multipletimes(Each ubi_notify_add() will call once, late_initcall will call it again). User could see many false positive error messages.
Given my suggestion above (not printing error in case the UBI device or volume doesn't exist as well as in case in case the ubiblock device has already been created) there won't be any false positives. What is still true is that other errors (eg. invalid string format of the ubiblock parameter) will be printed multiple times.
Do you think it is worth putting much effort into avoiding that? If so, any idea how?
Quoted 68 lines
Quoted 60 lines
Quoted 51 lines
Quoted 51 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
Quoted 7 lines
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Quoted 3 lines
Another path still operate mtd device:
ubi_detach_mtd_dev->ubi_wl_close->shutdown_work->wrk->func->erase_worker->sync_erase-> ubi_io_sync_erase/ubi_io_write_ec_hdr.
True, but I didn't see this being a problem, at least in the sense that I didn't see a kernel panic. We could try to make ubi_update_fastmap more robust to gracefully return in case the device is already gone.
Quoted 2 lines
Besides, can we remove any mtd specific driver(eg. phram/nandsim) by rmmod?
My test-case was to remove the SPI bus driver on which the SPI-NAND chip is connected to. Afaik there is also no way to prevent this at this point, we'd have to propagate the usecount of the MTD device down to the SPI bus hosting it.
Quoted 3 lines
I notice that
get_mtd_device(called before ubi attaching)will increase specific mtd driver's module refcnt.
If we physically remove the mtd device, will it triggerdel_mtd_device()?
Yes, in case what you mean is an SPI-NAND flash connected to an SPI-USB adapter, and then pulling the USB device.
Quoted 1 line
For example, when is
phram_remove()called?
From what I can see:phram_remove -> mtd_device_unregister -> del_mtd_device -> [for all elements of list mtd_notifiers call their 'remove' function]
After this we should always end up with mtd->usecount == 0 and hence proceed. If usecount != 0 at this point there is currently no meaningful error handling in mtdcore.c as far as my interpretation goes.
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Zhihao Cheng
reviewerQuoted 141 lines · Daniel Golle
Quoted 110 lines · Zhihao Cheng
Quoted 35 lines · Daniel Golle
Hi Zhihao,
thank you for taking the time to review this patch.
Quoted 22 lines · Zhihao Cheng
Hi Daniel,
Quoted 21 lines
Split
ubi_init()function into early function to be called bydevice_initcall()and keep cmdline attachment inlate_initcall(). (when building ubi as module, both is still done in a singlemodule_init()call)Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed.
Keep behavior regarding ubiblock creation and attaching of UBI device from kernel or module cmdline unchanged.
For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed.
For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time.
Quoted 7 lines
Quoted 4 lines
Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g.
drivers/nvmem/u-boot-env.ccan now be extended to support U-Boot environment stored in UBI volumes.Let me try to understand this patch, the main purpose is to add a new way to load ubi according to device tree, am I right?
Quoted 7 lines
You are right about one aspect of this patch, yes it does add another way to attach MTD devices, by utilizing a devicetree compatible 'linux,ubi' which can be set for MTD devices (or partitions). The other, and maybe even more significant aspect is that it moves attaching the MTD device from being a
late_initcall()to rather register an MTD user driver instead and hence happen as a direct consequence of the MTD device showing up.It means mtd device probing and loading will autimically triggers ubi attaching by
ubi_notify_add(), right?
If so, I thought of a scenario like this: The mtd device probing/removing will autimically loading/removing UBI device, now it can be done without any orders(cmdlines/ioctl) in running time. I think it's valuable.Quoted 7 lines
This will be important when using UBI e.g. as an NVMEM provider:
E.g. on Wi-Fi routers it has become common for vendors to store the bootloader environment, Ethernet MAC addresses or Wi-Fi calibration data inside an UBI volume (and I don't mean as a file inside a filesystem, but really just the UBI volume exposing nvmem cells for other drivers). See [1] for an example.Thanks for introducing the background.
Quoted 43 lines
Quoted 34 lines
Quoted 34 lines
Signed-off-by Daniel Golle <[email protected]>--- drivers/mtd/ubi/block.c | 20 ++--- drivers/mtd/ubi/build.c | 163 +++++++++++++++++++++++++++++----------- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 +- 4 files changed, 132 insertions(+), 57 deletions(-)drivers/mtd/ubi/block.c +3
-3@@ -65,10 +65,10 @@ struct ubiblock_pdu { };/* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs__initdata ; +static int ubiblock_devs;/* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ]__initdata ; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES ];struct ubiblock {struct ubi_volume_desc *desc;@@ -582,7 +582,7 @@ open_volume_desc(const char *name, int ubi_num, int vol_id)return ubi_open_volume (ubi_num, vol_id,UBI_READONLY ); } -static void __init ubiblock_create_from_param (void ) +void ubiblock_create_from_param (void ) {int i, ret =0 ;struct ubiblock_param *p;@@ -611,9 +611,10 @@ static void __init ubiblock_create_from_param(void) ret =ubiblock_create (&vi);if (ret) {Quoted 8 lines
drivers/mtd/ubi/block.c static void __init ubiblock_create_from_param(void) · line 614
Quoted 4 lines
drivers/mtd/ubi/block.c +1
-3-pr_err ( -"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , - vi.name, p->ubi_num, p->vol_id, ret); +if (ret != -EEXIST && ret != -ENOENT )The EEXIST and ENOENT types of error messages are filtered, why? After this patch applied, the user won't be aware of which ubi volume failed to create ubiblock.
Quoted 6 lines
The EEXIST and ENOENT errors as filtered as they may happen now without that actually being an error.
ubiblock_create_from_paramis potentially being called multiple times, each time a new UBI device is attached in the "new" MTD user/device-tree way, and also once in "classic"late_initcallway.Quoted 3 lines
I agree, the second caller could trigger EEXIST. But how ENOENT happens, do you mean
ubi_notify_add->ubiblock_create_from_param->open_volume_desc? Could you show me path?You are right, EEXIST will actually never happen. If at all then we should check if
open_volume_descreturnsERR_PTR(-ENODEV). However, we can never know if the UBI device containing the volume we want to have exposed as ubiblock device is still going to be added later when the driver module for the MTD device is loaded. Hence I suggest to simply not callpr_err(...)in caseopen_volume_descreturnsERR_PTR(-ENODEV)as well as for the case thatubiblock_createreturns-EEXIST.Quoted 14 lines
Quoted 5 lines
One way to partially resolve this could be to add a boolean parameter to
ubiblock_create_from_paramwhich indicates if-ENOENTshould be treated as an error, and set that parameter only when calling fromlate_initcall.Quoted 8 lines
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Quoted 3 lines
Emm, I prefer to fix it, it's weird that
ubiblock_create_from_param()being executed multipletimes(Each ubi_notify_add() will call once, late_initcall will call it again). User could see many false positive error messages.
Quoted 9 lines
Given my suggestion above (not printing error in case the UBI device or volume doesn't exist as well as in case in case the ubiblock device has already been created) there won't be any false positives. What is still true is that other errors (eg. invalid string format of the ubiblock parameter) will be printed multiple times.
Do you think it is worth putting much effort into avoiding that? If so, any idea how?
Currently, there are two timings to load ubiblock:
1. boot cmdline: One-time loading
2. ioctl UBI_IOCVOLCRBLK: Can be called multiple times at runtime
PS: ubiblock cannot be compiled in module, there are only two options: Y and N
How about deleting 'ubiblock_create_from_param' from ubi_notify_add(), we only call ubiblock_create_from_param() once in ubi_init_attach(). This can keep the ubiblock loading timings are unchanged, and ENODEV and EEXIST won't be false positive.
If someone later want to loading ubiblock automically, UBI_VOLUME_ADDED case in ubiblock_notify() is suggested to be implemented.
Quoted 68 lines
Quoted 68 lines
Quoted 67 lines
Quoted 51 lines
Quoted 51 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Quoted 9 lines
Quoted 3 lines
Another path still operate mtd device:
ubi_detach_mtd_dev->ubi_wl_close->shutdown_work->wrk->func->erase_worker->sync_erase-> ubi_io_sync_erase/ubi_io_write_ec_hdr.True, but I didn't see this being a problem, at least in the sense that I didn't see a kernel panic. We could try to make
ubi_update_fastmapmore robust to gracefully return in case the device is already gone.
You mean ubi_update_fastmap() could trigger panic in ubi_notify_remove() when you remove the SPI bus dirver? May I have the error message?
I thought both ubi_update_fastmap() and ubi_wl_close() could operate the removed mtd device, it will be better to notify user error messages of ubi_update_fastmap() casued by removing under layer device.
Quoted 21 lines
Quoted 2 lines
Besides, can we remove any mtd specific driver(eg. phram/nandsim) by rmmod?
My test-case was to remove the SPI bus driver on which the SPI-NAND chip is connected to. Afaik there is also no way to prevent this at this point, we'd have to propagate the usecount of the MTD device down to the SPI bus hosting it.
Quoted 3 lines
I notice that
get_mtd_device(called before ubi attaching)will increase specific mtd driver's module refcnt.
If we physically remove the mtd device, will it triggerdel_mtd_device()?Yes, in case what you mean is an SPI-NAND flash connected to an SPI-USB adapter, and then pulling the USB device.
Quoted 3 lines
For example, when is
phram_remove()called?From what I can see:
phram_remove->mtd_device_unregister->del_mtd_device-> [for all elements of listmtd_notifierscall their 'remove' function]
Quoted 7 lines
After this we should always end up with
mtd->usecount== 0 and hence proceed. If usecount != 0 at this point there is currently no meaningful error handling inmtdcore.cas far as my interpretation goes..
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Quoted 31 lines · Zhihao Cheng
Quoted 20 lines · Daniel Golle
Quoted 15 lines · Zhihao Cheng
Quoted 6 lines
[...]
One way to partially resolve this could be to add a boolean parameter toubiblock_create_from_paramwhich indicates if-ENOENTshould be treated as an error, and set that parameter only when calling fromlate_initcall.Quoted 8 lines
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Quoted 3 lines
Emm, I prefer to fix it, it's weird that
ubiblock_create_from_param()being executed multipletimes(Each ubi_notify_add() will call once, late_initcall will call it again). User could see many false positive error messages.Quoted 9 lines
Given my suggestion above (not printing error in case the UBI device or volume doesn't exist as well as in case in case the ubiblock device has already been created) there won't be any false positives. What is still true is that other errors (eg. invalid string format of the ubiblock parameter) will be printed multiple times.
Do you think it is worth putting much effort into avoiding that? If so, any idea how?
Quoted 5 lines
Currently, there are two timings to load ubiblock:
1. boot cmdline: One-time loading
2. ioctlUBI_IOCVOLCRBLK: Can be called multiple times at runtime
PS: ubiblock cannot be compiled in module, there are only two options: Y and N
It's true that ubiblock is a boolean option, however, Y or N decides whether support for ubiblock will be included with the ubi driver, which can well be a kernel module. In that case, ubiblock is loaded and removed together with the rest of the ubi driver.
Quoted 5 lines
How about deleting '
ubiblock_create_from_param' fromubi_notify_add(), we only callubiblock_create_from_param()once inubi_init_attach(). This can keep the ubiblock loading timings are unchanged, and ENODEV and EEXIST won't be false positive.
I also thought that and it works well under the assumption that all drivers providing MTD devices are built-in or inserted before the ubi driver is inserted. However, this may not always be true. Think of, for example, an SPI bus driver which is built as a module and loaded during boot (eg. from initramfs). Your suggestion below solves that.
Quoted 3 lines
If someone later want to loading ubiblock automically,
UBI_VOLUME_ADDEDcase inubiblock_notify()is suggested to be implemented.
I've implemented your suggestion and it works fine, to me even looks better than the current code which gets/puts the UBI volume just to compare if its info matches the cmdline parameter. I will post this as a preliminary patch in the series, together with an other fix and an updated version of this patch.
Quoted 73 lines
Quoted 73 lines
Quoted 72 lines
Quoted 61 lines
Quoted 52 lines
Quoted 52 lines
drivers/mtd/ubi/block.c +3
-7+pr_err ( +"UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n" , + vi.name, p->ubi_num, p->vol_id, ret);continue ; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void)if (ubiblock_major <0 )return ubiblock_major; -/* -* Attach block devices from 'block=' module param. -* Even if one block device in the param list fails to come up, -* still allow the module to load and leave any others up. -*/ -ubiblock_create_from_param (); -/* * Block devices are only created upon user requests, so we ignore * existing volumes. drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
Quoted 7 lines
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Another path still operate mtd device:
ubi_detach_mtd_dev->ubi_wl_close->shutdown_work->wrk->func->erase_worker->sync_erase-> ubi_io_sync_erase/ubi_io_write_ec_hdr.
Quoted 8 lines
Quoted 5 lines
True, but I didn't see this being a problem, at least in the sense that I didn't see a kernel panic. We could try to make
ubi_update_fastmapmore robust to gracefully return in case the device is already gone.You mean
ubi_update_fastmap()could trigger panic inubi_notify_remove()when you remove the SPI bus dirver? May I have the error message?
I spent a day stairing at the kernel Oops and found that it was unrelated to UBI, but rather a bug in the SPI driver. I've posted the fix:
https://patchwork.kernel.org/project/spi-devel-general/patch/[email protected]/
Quoted 3 lines
I thought both
ubi_update_fastmap()andubi_wl_close()could operate the removed mtd device, it will be better to notify user error messages ofubi_update_fastmap()casued by removing under layer device.
You were right, there is no problem there and we don't need to skip ubi_update_fastmap() now that the SPI driver makes sure to complete any ongoing operation before unloading (see above).
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
Besides, can we remove any mtd specific driver(eg. phram/nandsim) by rmmod?
My test-case was to remove the SPI bus driver on which the SPI-NAND chip is connected to. Afaik there is also no way to prevent this at this point, we'd have to propagate the usecount of the MTD device down to the SPI bus hosting it.
Quoted 3 lines
I notice that
get_mtd_device(called before ubi attaching)will increase specific mtd driver's module refcnt.
If we physically remove the mtd device, will it triggerdel_mtd_device()?Yes, in case what you mean is an SPI-NAND flash connected to an SPI-USB adapter, and then pulling the USB device.
Quoted 1 line
For example, when is
phram_remove()called?
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
From what I can see:
phram_remove->mtd_device_unregister->del_mtd_device-> [for all elements of listmtd_notifierscall their 'remove' function]After this we should always end up with
mtd->usecount== 0 and hence proceed. If usecount != 0 at this point there is currently no meaningful error handling inmtdcore.cas far as my interpretation goes.
I've found a similar issue with "fake error handling" on removal which should rather be skipped in drivers/mtd/ubi/block.c. I will post the fix also in the upcoming series which I'm preparing right now.
Zhihao Cheng
reviewerQuoted 38 lines · Daniel Golle
Quoted 31 lines · Zhihao Cheng
Quoted 20 lines · Daniel Golle
Quoted 15 lines · Zhihao Cheng
Quoted 6 lines
[...]
One way to partially resolve this could be to add a boolean parameter toubiblock_create_from_paramwhich indicates if-ENOENTshould be treated as an error, and set that parameter only when calling fromlate_initcall.Quoted 8 lines
Being able to warn users if they added the same ubiblock device more than once **on the cmdline or module parameter** is more tricky and would require tracking why a ubiblock device has previously been created. However, I don't think that emitting a warning in this case is crucial at all, the user definitely wanted the ubiblock device to be created and saying that more than once is unneccesary but yet also doesn't leave room for any interpretation other than just that the user *really* wants the ubiblock to be created.
Quoted 3 lines
Emm, I prefer to fix it, it's weird that
ubiblock_create_from_param()being executed multipletimes(Each ubi_notify_add() will call once, late_initcall will call it again). User could see many false positive error messages.Quoted 9 lines
Given my suggestion above (not printing error in case the UBI device or volume doesn't exist as well as in case in case the ubiblock device has already been created) there won't be any false positives. What is still true is that other errors (eg. invalid string format of the ubiblock parameter) will be printed multiple times.
Do you think it is worth putting much effort into avoiding that? If so, any idea how?
Quoted 5 lines
Currently, there are two timings to load ubiblock:
1. boot cmdline: One-time loading
2. ioctlUBI_IOCVOLCRBLK: Can be called multiple times at runtime
PS: ubiblock cannot be compiled in module, there are only two options: Y and N
Quoted 4 lines
It's true that ubiblock is a boolean option, however, Y or N decides whether support for ubiblock will be included with the ubi driver, which can well be a kernel module. In that case, ubiblock is loaded and removed together with the rest of the ubi driver.
You are right, ubiblock will be loaded in module way if UBI driver is compiled in module format, my mistake.
Quoted 14 lines
Quoted 6 lines
How about deleting '
ubiblock_create_from_param' fromubi_notify_add(), we only callubiblock_create_from_param()once inubi_init_attach(). This can keep the ubiblock loading timings are unchanged, and ENODEV and EEXIST won't be false positive.I also thought that and it works well under the assumption that all drivers providing MTD devices are built-in or inserted before the ubi driver is inserted. However, this may not always be true. Think of, for example, an SPI bus driver which is built as a module and loaded during boot (eg. from initramfs). Your suggestion below solves that.
Quoted 1 line
Quoted 8 lines
Quoted 2 lines
If someone later want to loading ubiblock automically,
UBI_VOLUME_ADDEDcase inubiblock_notify()is suggested to be implemented.I've implemented your suggestion and it works fine, to me even looks better than the current code which gets/puts the UBI volume just to compare if its info matches the cmdline parameter. I will post this as a preliminary patch in the series, together with an other fix and an updated version of this patch.
I'll take a look at this series.
Quoted 88 lines
Quoted 74 lines
Quoted 74 lines
Quoted 73 lines
Quoted 62 lines
Quoted 53 lines
Quoted 53 lines
(quoted diff) +3
-7+ pr_err( + "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n", + vi.name, p->ubi_num, p->vol_id, ret); continue; } }@@ -644,13 +645,6 @@ int __init ubiblock_init(void) if (ubiblock_major < 0) return ubiblock_major; - /* - * Attach block devices from 'block=' module param. - * Even if one block device in the param list fails to come up, - * still allow the module to load and leave any others up. - */ - ubiblock_create_from_param(); - /* * Block devices are only created upon user requests, so we ignore * existing volumes.drivers/mtd/ubi/build.c +3
-1@@ -27,6 +27,7 @@#include <linux/log2.h> #include <linux/kthread.h> #include <linux/kernel.h> +#include <linux/of.h> #include <linux/slab.h> #include <linux/major.h> #include "ubi.h" @@ -1065,6 +1066,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero +* @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if the @@ -1074,7 +1076,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,* Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev (int ubi_num,int anyway) +int ubi_detach_mtd_dev (int ubi_num,int anyway,bool have_lock) {struct ubi_device *ubi;@@ -1108,7 +1110,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)Quoted 8 lines
drivers/mtd/ubi/build.c int ubi_detach_mtd_dev(int ubi_num, int anyway) · line 1114
Quoted 6 lines
drivers/mtd/ubi/build.c +1
-1* EC updates that have been made since the last written fastmap. * In case of fastmap debugging we omit the update to simulate an * unclean shutdown. */ -if (!ubi_dbg_chk_fastmap (ubi)) +if (!have_lock && !ubi_dbg_chk_fastmap (ubi))ubi_update_fastmap (ubi);Why do you skip updating fastmap if ubi is detached in mtd notification way?
Quoted 7 lines
The reason here is simple: Once we receive a notification about the MTD device being removed it is too late to update fastmap. The device is gone, nothing we can do about that any more. Just like removing a device holding a filesystem without having priorly unmounted it, e.g. if you 'rmmod usb-storage' (just that block devices unfortunately still lack notifications about removal or size changes...)
Another path still operate mtd device:
ubi_detach_mtd_dev->ubi_wl_close->shutdown_work->wrk->func->erase_worker->sync_erase-> ubi_io_sync_erase/ubi_io_write_ec_hdr.Quoted 8 lines
Quoted 5 lines
True, but I didn't see this being a problem, at least in the sense that I didn't see a kernel panic. We could try to make
ubi_update_fastmapmore robust to gracefully return in case the device is already gone.You mean
ubi_update_fastmap()could trigger panic inubi_notify_remove()when you remove the SPI bus dirver? May I have the error message?I spent a day stairing at the kernel Oops and found that it was unrelated to UBI, but rather a bug in the SPI driver. I've posted the fix:
https://patchwork.kernel.org/project/spi-devel-general/patch/[email protected]/
Quoted 8 lines
Quoted 3 lines
I thought both
ubi_update_fastmap()andubi_wl_close()could operate the removed mtd device, it will be better to notify user error messages ofubi_update_fastmap()casued by removing under layer device.You were right, there is no problem there and we don't need to skip
ubi_update_fastmap()now that the SPI driver makes sure to complete any ongoing operation before unloading (see above).
Great.
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
Besides, can we remove any mtd specific driver(eg. phram/nandsim) by rmmod?
My test-case was to remove the SPI bus driver on which the SPI-NAND chip is connected to. Afaik there is also no way to prevent this at this point, we'd have to propagate the usecount of the MTD device down to the SPI bus hosting it.
Quoted 3 lines
I notice that
get_mtd_device(called before ubi attaching)will increase specific mtd driver's module refcnt.
If we physically remove the mtd device, will it triggerdel_mtd_device()?Yes, in case what you mean is an SPI-NAND flash connected to an SPI-USB adapter, and then pulling the USB device.
Quoted 1 line
For example, when is
phram_remove()called?Quoted 7 lines
Quoted 7 lines
Quoted 1 line
From what I can see:
phram_remove->mtd_device_unregister->del_mtd_device-> [for all elements of listmtd_notifierscall their 'remove' function]After this we should always end up with
mtd->usecount== 0 and hence proceed. If usecount != 0 at this point there is currently no meaningful error handling inmtdcore.cas far as my interpretation goes.
Quoted 6 lines
I've found a similar issue with "fake error handling" on removal which should rather be skipped in
drivers/mtd/ubi/block.c. I will post the fix also in the upcoming series which I'm preparing right now..
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/