OSDN Git Service

Revert "module: error out early on concurrent load of the same module file"
authorLinus Torvalds <torvalds@linux-foundation.org>
Mon, 29 May 2023 10:40:33 +0000 (06:40 -0400)
committerLinus Torvalds <torvalds@linux-foundation.org>
Mon, 29 May 2023 10:40:33 +0000 (06:40 -0400)
This reverts commit 9828ed3f695a138f7add89fa2a186ababceb8006.

Sadly, it does seem to cause failures to load modules. Johan Hovold reports:

 "This change breaks module loading during boot on the Lenovo Thinkpad
  X13s (aarch64).

  Specifically it results in indefinite probe deferral of the display
  and USB (ethernet) which makes it a pain to debug. Typing in the dark
  to acquire some logs reveals that other modules are missing as well"

Since this was applied late as a "let's try this", I'm reverting it
asap, and we can try to figure out what goes wrong later.  The excessive
parallel module loading problem is annoying, but not noticeable in
normal situations, and this was only meant as an optimistic workaround
for a user-space bug.

One possible solution may be to do the optimistic exclusive open first,
and then use a lock to serialize loading if that fails.

Reported-by: Johan Hovold <johan@kernel.org>
Link: https://lore.kernel.org/lkml/ZHRpH-JXAxA6DnzR@hovoldconsulting.com/
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/fs.h
kernel/module/main.c

index 86b5027..133f064 100644 (file)
@@ -2566,12 +2566,6 @@ static inline int deny_write_access(struct file *file)
        struct inode *inode = file_inode(file);
        return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
 }
-static inline int exclusive_deny_write_access(struct file *file)
-{
-       int old = 0;
-       struct inode *inode = file_inode(file);
-       return atomic_try_cmpxchg(&inode->i_writecount, &old, -1) ? 0 : -ETXTBSY;
-}
 static inline void put_write_access(struct inode * inode)
 {
        atomic_dec(&inode->i_writecount);
index b4c7e92..044aa2c 100644 (file)
@@ -3057,13 +3057,25 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
        return load_module(&info, uargs, 0);
 }
 
-static int file_init_module(struct file *file, const char __user * uargs, int flags)
+SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 {
        struct load_info info = { };
        void *buf = NULL;
        int len;
+       int err;
+
+       err = may_init_module();
+       if (err)
+               return err;
+
+       pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
 
-       len = kernel_read_file(file, 0, &buf, INT_MAX, NULL,
+       if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
+                     |MODULE_INIT_IGNORE_VERMAGIC
+                     |MODULE_INIT_COMPRESSED_FILE))
+               return -EINVAL;
+
+       len = kernel_read_file_from_fd(fd, 0, &buf, INT_MAX, NULL,
                                       READING_MODULE);
        if (len < 0) {
                mod_stat_inc(&failed_kreads);
@@ -3072,7 +3084,7 @@ static int file_init_module(struct file *file, const char __user * uargs, int fl
        }
 
        if (flags & MODULE_INIT_COMPRESSED_FILE) {
-               int err = module_decompress(&info, buf, len);
+               err = module_decompress(&info, buf, len);
                vfree(buf); /* compressed data is no longer needed */
                if (err) {
                        mod_stat_inc(&failed_decompress);
@@ -3087,46 +3099,6 @@ static int file_init_module(struct file *file, const char __user * uargs, int fl
        return load_module(&info, uargs, flags);
 }
 
-/*
- * kernel_read_file() will already deny write access, but module
- * loading wants _exclusive_ access to the file, so we do that
- * here, along with basic sanity checks.
- */
-static int prepare_file_for_module_load(struct file *file)
-{
-       if (!file || !(file->f_mode & FMODE_READ))
-               return -EBADF;
-       if (!S_ISREG(file_inode(file)->i_mode))
-               return -EINVAL;
-       return exclusive_deny_write_access(file);
-}
-
-SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
-{
-       struct fd f;
-       int err;
-
-       err = may_init_module();
-       if (err)
-               return err;
-
-       pr_debug("finit_module: fd=%d, uargs=%p, flags=%i\n", fd, uargs, flags);
-
-       if (flags & ~(MODULE_INIT_IGNORE_MODVERSIONS
-                     |MODULE_INIT_IGNORE_VERMAGIC
-                     |MODULE_INIT_COMPRESSED_FILE))
-               return -EINVAL;
-
-       f = fdget(fd);
-       err = prepare_file_for_module_load(f.file);
-       if (!err) {
-               err = file_init_module(f.file, uargs, flags);
-               allow_write_access(f.file);
-       }
-       fdput(f);
-       return err;
-}
-
 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */
 char *module_flags(struct module *mod, char *buf, bool show_state)
 {