erofs: lazily initialize per-CPU workers and CPU hotplug hooks
Sandeep Dhavale
reviewerPATCH v4 1/1 erofs: lazily initialize per-CPU workers and CPU hotplug hooks
Currently, when EROFS is built with per-CPU workers, the workers are started and CPU hotplug hooks are registered during module initialization. This leads to unnecessary worker start/stop cycles during CPU hotplug events, particularly on Android devices that frequently suspend and resume.
This change defers the initialization of per-CPU workers and the registration of CPU hotplug hooks until the first EROFS mount. This ensures that these resources are only allocated and managed when EROFS is actually in use.
The tear down of per-CPU workers and unregistration of CPU hotplug hooks still occurs during z_erofs_exit_subsystem(), but only if they were initialized.
---
v3: https://lore.kernel.org/linux-erofs/[email protected]/
Changes since v3:
- fold z_erofs_init_pcpu_workers() in the caller and rename the caller
fs/erofs/zdata.c | 61 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-)
fs/erofs/zdata.c +45 -16
@@ -291,6 +291,9 @@ static struct workqueue_struct *z_erofs_workqueue __read_mostly;#ifdef CONFIG_EROFS_FS_PCPU_KTHREAD static struct kthread_worker __rcu **z_erofs_pcpu_workers; +static atomic_t erofs_percpu_workers_initialized =ATOMIC_INIT (0 ); +static int erofs_cpu_hotplug_init (void ); +static void erofs_cpu_hotplug_destroy (void );static void erofs_destroy_percpu_workers (void ) {
@@ -336,9 +339,40 @@ static int erofs_init_percpu_workers(void) }return 0 ; } + +static int z_erofs_init_pcpu_workers (void ) +{ +int err; + +if (atomic_xchg (&erofs_percpu_workers_initialized,1 )) +return 0 ; + + err =erofs_init_percpu_workers (); +if (err) + goto err_init_percpu_workers; + + err =erofs_cpu_hotplug_init (); +if (err <0 ) + goto err_cpuhp_init; +return err; + +err_cpuhp_init: +erofs_destroy_percpu_workers (); +err_init_percpu_workers: +atomic_set (&erofs_percpu_workers_initialized,0 ); +return err; +} + +static void z_erofs_destroy_pcpu_workers (void ) +{ +if (!atomic_xchg (&erofs_percpu_workers_initialized,0 )) +return ; +erofs_cpu_hotplug_destroy (); +erofs_destroy_percpu_workers (); +}#else -static inline void erofs_destroy_percpu_workers (void ) {} -static inline int erofs_init_percpu_workers (void ) {return 0 ; } +static inline int z_erofs_init_pcpu_workers (void ) {return 0 ; } +static inline void z_erofs_destroy_pcpu_workers (void ) {}#endif #if defined(CONFIG_HOTPLUG_CPU ) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD )
@@ -405,8 +439,7 @@ static inline void erofs_cpu_hotplug_destroy(void) {}void z_erofs_exit_subsystem (void ) { -erofs_cpu_hotplug_destroy (); -erofs_destroy_percpu_workers (); +z_erofs_destroy_pcpu_workers ();destroy_workqueue (z_erofs_workqueue);z_erofs_destroy_pcluster_pool ();z_erofs_exit_decompressor ();
@@ -430,19 +463,8 @@ int __init z_erofs_init_subsystem(void) goto err_workqueue_init; } - err =erofs_init_percpu_workers (); -if (err) - goto err_pcpu_worker; - - err =erofs_cpu_hotplug_init (); -if (err <0 ) - goto err_cpuhp_init;return err; -err_cpuhp_init: -erofs_destroy_percpu_workers (); -err_pcpu_worker: -destroy_workqueue (z_erofs_workqueue); err_workqueue_init:z_erofs_destroy_pcluster_pool (); err_pcluster_pool:
@@ -644,10 +666,17 @@ static const struct address_space_operations z_erofs_cache_aops = {int z_erofs_init_super (struct super_block *sb) { -struct inode *const inode =new_inode (sb); +struct inode *inode; +int err; + err =z_erofs_init_pcpu_workers (); +if (err) +return err; + + inode =new_inode (sb);if (!inode)return -ENOMEM ; +set_nlink (inode,1 ); inode->i_size =OFFSET_MAX ; inode->i_mapping->a_ops = &z_erofs_cache_aops;
Signature · 1 line
2.49.0.805.g082f7c87e0-goog
Quoted 10 lines · Sandeep Dhavale
Currently, when EROFS is built with per-CPU workers, the workers are started and CPU hotplug hooks are registered during module initialization. This leads to unnecessary worker start/stop cycles during CPU hotplug events, particularly on Android devices that frequently suspend and resume.
This change defers the initialization of per-CPU workers and the registration of CPU hotplug hooks until the first EROFS mount. This ensures that these resources are only allocated and managed when EROFS is actually in use.
Quoted 5 lines
The tear down of per-CPU workers and unregistration of CPU hotplug hooks still occurs during
z_erofs_exit_subsystem(), but only if they were initialized.Signed-off-by Sandeep Dhavale <[email protected]>
Quoted 1 line
---
...
Quoted 4 lines
int z_erofs_init_super(struct super_block *sb) {fs/erofs/zdata.c +1
-1-struct inode *const inode =new_inode (sb); +struct inode *inode;
fs/erofs/zdata.c static const struct address_space_operations z_erofs_cache_aops = { · line 679
Quoted 10 lines
fs/erofs/zdata.c +7
-0+int err; + err =z_erofs_init_pcpu_workers (); +if (err) +return err; + + inode =new_inode (sb);if (!inode)return -ENOMEM ; +
I think the new blank line is redundant, the setup part should be next to new_inode().
I could fix up this part manually if you don't have strong opinion on this.
Thanks,
Gao Xiang
Sandeep Dhavale
reviewerQuoted 3 lines
I could fix up this part manually if you don't have strong opinion on this.
Hi Gao,
I don't have any objection to styling as it is subjective. I am ok with the fixup.
Thanks,
Sandeep.
Quoted 3 lines
Thanks,
Gao Xiang
Quoted 7 lines · Sandeep Dhavale
Quoted 3 lines
I could fix up this part manually if you don't have strong opinion on this.
Hi Gao,
I don't have any objection to styling as it is subjective. I am ok with the fixup.
Ok, thanks for the confirmation!
Thanks,
Gao Xiang
Quoted 112 lines · Sandeep Dhavale
Currently, when EROFS is built with per-CPU workers, the workers are started and CPU hotplug hooks are registered during module initialization. This leads to unnecessary worker start/stop cycles during CPU hotplug events, particularly on Android devices that frequently suspend and resume.
This change defers the initialization of per-CPU workers and the registration of CPU hotplug hooks until the first EROFS mount. This ensures that these resources are only allocated and managed when EROFS is actually in use.
The tear down of per-CPU workers and unregistration of CPU hotplug hooks still occurs during
z_erofs_exit_subsystem(), but only if they were initialized.Signed-off-by Sandeep Dhavale <[email protected]>---
v3: https://lore.kernel.org/linux-erofs/[email protected]/
Changes since v3:
- foldz_erofs_init_pcpu_workers()in the caller and rename the callerfs/erofs/zdata.c | 61 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-)fs/erofs/zdata.c +37
-15@@ -291,6 +291,9 @@ static struct workqueue_struct *z_erofs_workqueue __read_mostly;#ifdef CONFIG_EROFS_FS_PCPU_KTHREAD static struct kthread_worker __rcu **z_erofs_pcpu_workers; +static atomic_t erofs_percpu_workers_initialized =ATOMIC_INIT (0 ); +static int erofs_cpu_hotplug_init (void ); +static void erofs_cpu_hotplug_destroy (void );static void erofs_destroy_percpu_workers (void ) {@@ -336,9 +339,40 @@ static int erofs_init_percpu_workers(void) }return 0 ; } + +static int z_erofs_init_pcpu_workers (void ) +{ +int err; + +if (atomic_xchg (&erofs_percpu_workers_initialized,1 )) +return 0 ; + + err =erofs_init_percpu_workers (); +if (err) + goto err_init_percpu_workers; + + err =erofs_cpu_hotplug_init (); +if (err <0 ) + goto err_cpuhp_init; +return err; + +err_cpuhp_init: +erofs_destroy_percpu_workers (); +err_init_percpu_workers: +atomic_set (&erofs_percpu_workers_initialized,0 ); +return err; +} + +static void z_erofs_destroy_pcpu_workers (void ) +{ +if (!atomic_xchg (&erofs_percpu_workers_initialized,0 )) +return ; +erofs_cpu_hotplug_destroy (); +erofs_destroy_percpu_workers (); +}#else -static inline void erofs_destroy_percpu_workers (void ) {} -static inline int erofs_init_percpu_workers (void ) {return 0 ; } +static inline int z_erofs_init_pcpu_workers (void ) {return 0 ; } +static inline void z_erofs_destroy_pcpu_workers (void ) {}#endif #if defined(CONFIG_HOTPLUG_CPU ) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD )@@ -405,8 +439,7 @@ static inline void erofs_cpu_hotplug_destroy(void) {}void z_erofs_exit_subsystem (void ) { -erofs_cpu_hotplug_destroy (); -erofs_destroy_percpu_workers (); +z_erofs_destroy_pcpu_workers ();destroy_workqueue (z_erofs_workqueue);z_erofs_destroy_pcluster_pool ();z_erofs_exit_decompressor ();@@ -430,19 +463,8 @@ int __init z_erofs_init_subsystem(void) goto err_workqueue_init; } - err =erofs_init_percpu_workers (); -if (err) - goto err_pcpu_worker; - - err =erofs_cpu_hotplug_init (); -if (err <0 ) - goto err_cpuhp_init;return err; -err_cpuhp_init: -erofs_destroy_percpu_workers (); -err_pcpu_worker: -destroy_workqueue (z_erofs_workqueue); err_workqueue_init:z_erofs_destroy_pcluster_pool (); err_pcluster_pool:@@ -644,10 +666,17 @@ static const struct address_space_operations z_erofs_cache_aops = {
fs/erofs/zdata.c static const struct address_space_operations z_erofs_cache_aops = { · line 675
Quoted 10 lines
fs/erofs/zdata.c +6
-1int z_erofs_init_super (struct super_block *sb) { -struct inode *const inode =new_inode (sb); +struct inode *inode; +int err; + err =z_erofs_init_pcpu_workers (); +if (err) +return err; +
This will slow down the first mount action, but it seems unavoidable for now. :)
Thanks,
Hongbo
Quoted 7 lines
fs/erofs/zdata.c +2
-0+ inode =new_inode (sb);if (!inode)return -ENOMEM ; +set_nlink (inode,1 ); inode->i_size =OFFSET_MAX ; inode->i_mapping->a_ops = &z_erofs_cache_aops;
Chao Yu
maintainerQuoted 54 lines · Sandeep Dhavale
Currently, when EROFS is built with per-CPU workers, the workers are started and CPU hotplug hooks are registered during module initialization. This leads to unnecessary worker start/stop cycles during CPU hotplug events, particularly on Android devices that frequently suspend and resume.
This change defers the initialization of per-CPU workers and the registration of CPU hotplug hooks until the first EROFS mount. This ensures that these resources are only allocated and managed when EROFS is actually in use.
The tear down of per-CPU workers and unregistration of CPU hotplug hooks still occurs during
z_erofs_exit_subsystem(), but only if they were initialized.Signed-off-by Sandeep Dhavale <[email protected]>---
v3: https://lore.kernel.org/linux-erofs/[email protected]/
Changes since v3:
- foldz_erofs_init_pcpu_workers()in the caller and rename the callerfs/erofs/zdata.c | 61 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 16 deletions(-)fs/erofs/zdata.c +16
-0@@ -291,6 +291,9 @@ static struct workqueue_struct *z_erofs_workqueue __read_mostly;#ifdef CONFIG_EROFS_FS_PCPU_KTHREAD static struct kthread_worker __rcu **z_erofs_pcpu_workers; +static atomic_t erofs_percpu_workers_initialized =ATOMIC_INIT (0 ); +static int erofs_cpu_hotplug_init (void ); +static void erofs_cpu_hotplug_destroy (void );static void erofs_destroy_percpu_workers (void ) {@@ -336,9 +339,40 @@ static int erofs_init_percpu_workers(void) }return 0 ; } + +static int z_erofs_init_pcpu_workers (void ) +{ +int err; + +if (atomic_xchg (&erofs_percpu_workers_initialized,1 )) +return 0 ; + + err =erofs_init_percpu_workers (); +if (err) + goto err_init_percpu_workers; + + err =erofs_cpu_hotplug_init ();
fs/erofs/zdata.c static int erofs_init_percpu_workers(void) · line 364
Quoted 10 lines
fs/erofs/zdata.c +10
-0+if (err <0 ) + goto err_cpuhp_init; +return err; + +err_cpuhp_init: +erofs_destroy_percpu_workers (); +err_init_percpu_workers: +atomic_set (&erofs_percpu_workers_initialized,0 ); +return err; +}
- mount #1 - mount #2
- z_erofs_init_pcpu_workers
- atomic_xchg(, 1)
- z_erofs_init_pcpu_workers
- atomic_xchg(, 1)
: return 0 since atomic variable is 1 it will run w/o percpu workers and hotplug
: update atomic variable to 1
- erofs_init_percpu_workers
: fail
- atomic_set(, 0)
: update atomic variable to 0 & fail the mount
Can we add some logs to show we succeed/fail to initialize workers or hotplugs? As for mount #2, it expects it will run w/ them, but finally it may not. So we'd better have a simple way to know?
Thanks,
Quoted 65 lines
fs/erofs/zdata.c +19
-16+ +static void z_erofs_destroy_pcpu_workers (void ) +{ +if (!atomic_xchg (&erofs_percpu_workers_initialized,0 )) +return ; +erofs_cpu_hotplug_destroy (); +erofs_destroy_percpu_workers (); +}#else -static inline void erofs_destroy_percpu_workers (void ) {} -static inline int erofs_init_percpu_workers (void ) {return 0 ; } +static inline int z_erofs_init_pcpu_workers (void ) {return 0 ; } +static inline void z_erofs_destroy_pcpu_workers (void ) {}#endif #if defined(CONFIG_HOTPLUG_CPU ) && defined(CONFIG_EROFS_FS_PCPU_KTHREAD )@@ -405,8 +439,7 @@ static inline void erofs_cpu_hotplug_destroy(void) {}void z_erofs_exit_subsystem (void ) { -erofs_cpu_hotplug_destroy (); -erofs_destroy_percpu_workers (); +z_erofs_destroy_pcpu_workers ();destroy_workqueue (z_erofs_workqueue);z_erofs_destroy_pcluster_pool ();z_erofs_exit_decompressor ();@@ -430,19 +463,8 @@ int __init z_erofs_init_subsystem(void) goto err_workqueue_init; } - err =erofs_init_percpu_workers (); -if (err) - goto err_pcpu_worker; - - err =erofs_cpu_hotplug_init (); -if (err <0 ) - goto err_cpuhp_init;return err; -err_cpuhp_init: -erofs_destroy_percpu_workers (); -err_pcpu_worker: -destroy_workqueue (z_erofs_workqueue); err_workqueue_init:z_erofs_destroy_pcluster_pool (); err_pcluster_pool:@@ -644,10 +666,17 @@ static const struct address_space_operations z_erofs_cache_aops = {int z_erofs_init_super (struct super_block *sb) { -struct inode *const inode =new_inode (sb); +struct inode *inode; +int err; + err =z_erofs_init_pcpu_workers (); +if (err) +return err; + + inode =new_inode (sb);if (!inode)return -ENOMEM ; +set_nlink (inode,1 ); inode->i_size =OFFSET_MAX ; inode->i_mapping->a_ops = &z_erofs_cache_aops;
Sandeep Dhavale
reviewerHi Chao,
Quoted 14 lines
- mount #1 - mount #2
-z_erofs_init_pcpu_workers
-atomic_xchg(, 1)
-z_erofs_init_pcpu_workers
-atomic_xchg(, 1)
: return 0 since atomic variable is 1 it will run w/o percpu workers and hotplug
: update atomic variable to 1
-erofs_init_percpu_workers
: fail
-atomic_set(, 0)
: update atomic variable to 0 & fail the mount
Quoted 6 lines
Can we add some logs to show we succeed/fail to initialize workers or hotplugs? As for mount #2, it expects it will run w/ them, but finally it may not. So we'd better have a simple way to know?
Thanks,
What you have laid out as race, indeed can happen if erofs_init_percpu_workers() fails with ENOMEM. For me that is still not catastrophic as workqueue fallback is in place so the filesystem is still functional. And at the next mount, the logic will be reattempted as the atomic variable is reset to 0 after failure.
If you still think we need to have a log message, I will be happy to spin up the next revision with logging for ENOMEM.
Thanks for the review!
Regards,
Sandeep.
Chao Yu
maintainerHi Sandeep,
Quoted 20 lines · Sandeep Dhavale
Hi Chao,
Quoted 18 lines
- mount #1 - mount #2
-z_erofs_init_pcpu_workers
-atomic_xchg(, 1)
-z_erofs_init_pcpu_workers
-atomic_xchg(, 1)
: return 0 since atomic variable is 1 it will run w/o percpu workers and hotplug
: update atomic variable to 1
-erofs_init_percpu_workers
: fail
-atomic_set(, 0)
: update atomic variable to 0 & fail the mountCan we add some logs to show we succeed/fail to initialize workers or hotplugs? As for mount #2, it expects it will run w/ them, but finally it may not. So we'd better have a simple way to know?
Quoted 7 lines
Quoted 2 lines
Thanks,
What you have laid out as race, indeed can happen if
erofs_init_percpu_workers()fails with ENOMEM. For me that is still not catastrophic as workqueue fallback is in place so the filesystem is still functional. And at the next mount, the logic will be reattempted as the atomic variable is reset to 0 after failure.
Yeah, correct.
Quoted 3 lines
If you still think we need to have a log message, I will be happy to spin up the next revision with logging for ENOMEM.
I guess it will be good to add log for such case, thanks. :)
Thanks,
Quoted 5 lines
Thanks for the review!
Regards,
Sandeep.