Skip to contentSkip to conversation

kernfs: use nofail allocation for global locks

PATCH In review Ruoyu Wang 2 participants 2 messages Jun 8

Subsystems (2)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 kernfs: use nofail allocation for global locks

kernfs_lock_init() allocates the global kernfs lock storage during init and then calls kernfs_mutex_init(), which initializes mutexes through the allocated pointer. A WARN_ON() does not stop execution, so allocation failure would still dereference NULL.

The lock storage is required for kernfs to operate. Use a nofail GFP_KERNEL allocation so the init path does not continue without the required object.

---
 fs/kernfs/mount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/kernfs/mount.c +2 -2
index 6e3217b6e4811..6cf8e71e5bd08 100644
@@ -451,8 +451,8 @@ static void __init kernfs_mutex_init(void)
 
 static void __init kernfs_lock_init(void)
 {
-	kernfs_locks = kmalloc_obj(struct kernfs_global_locks);
-	WARN_ON(!kernfs_locks);
+	kernfs_locks = kmalloc_obj(struct kernfs_global_locks,
+				   GFP_KERNEL | __GFP_NOFAIL);
 
 	kernfs_mutex_init();
 }
Signature · 1 line
2.51.0
#1 lore raw
Quoted 4 lines · Ruoyu Wang

kernfs_lock_init() allocates the global kernfs lock storage during init and then calls kernfs_mutex_init(), which initializes mutexes through the allocated pointer. A WARN_ON() does not stop execution, so allocation failure would still dereference NULL.

Yes, but as this never has failed, perhaps it's not a real issue? :)

Quoted 3 lines

The lock storage is required for kernfs to operate. Use a nofail GFP_KERNEL allocation so the init path does not continue without the required object.

Have you ever triggered this code path? If so, how?

Quoted 11 lines
---
 fs/kernfs/mount.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/kernfs/mount.c +0 -0
index 6e3217b6e4811..6cf8e71e5bd08 100644

fs/kernfs/mount.c static void __init kernfs_mutex_init(void) · line 455

Quoted 5 lines
fs/kernfs/mount.c +0 -2
@@ -451,8 +451,8 @@ static void __init kernfs_mutex_init(void)
 
 static void __init kernfs_lock_init(void)
 {
-	kernfs_locks = kmalloc_obj(struct kernfs_global_locks);
-	WARN_ON(!kernfs_locks);

As this is at boot time, if this fails, and triggers and oops, the system has much worse problems. I do not think that:

fs/kernfs/mount.c static void __init kernfs_mutex_init(void) · line 455

Quoted 2 lines
fs/kernfs/mount.c +2 -0
+	kernfs_locks = kmalloc_obj(struct kernfs_global_locks,
+				   GFP_KERNEL | __GFP_NOFAIL);

Will help out any, do you?

thanks,

greg k-h