Commit Graph

79 Commits

Author SHA1 Message Date
Viktor Malik 565c35b3f1
module, bpf: Store BTF base pointer in struct module
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit d4e48e3dd45017abdd69a19285d197de897ef44f
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Thu Jun 20 10:17:29 2024 +0100

    module, bpf: Store BTF base pointer in struct module
    
    ...as this will allow split BTF modules with a base BTF
    representation (rather than the full vmlinux BTF at time of
    BTF encoding) to resolve their references to kernel types in a
    way that is more resilient to small changes in kernel types.
    
    This will allow modules that are not built every time the kernel
    is to provide more resilient BTF, rather than have it invalidated
    every time BTF ids for core kernel types change.
    
    Fields are ordered to avoid holes in struct module.
    
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240620091733.1967885-3-alan.maguire@oracle.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:10 +01:00
Nico Pache d6b2c538d9 kunit: add KUNIT_INIT_TABLE to init linker section
commit d81f0d7b8b23ec79f80be602ed6129ded27862e8
Author: Rae Moar <rmoar@google.com>
Date:   Wed Dec 13 19:44:17 2023 +0000

    kunit: add KUNIT_INIT_TABLE to init linker section

    Add KUNIT_INIT_TABLE to the INIT_DATA linker section.

    Alter the KUnit macros to create init tests:
    kunit_test_init_section_suites

    Update lib/kunit/executor.c to run both the suites in KUNIT_TABLE and
    KUNIT_INIT_TABLE.

    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Rae Moar <rmoar@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:32:28 -06:00
Donald Dutile aaaa438fc7 modules: wait do_free_init correctly
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 8f8cd6c0a43ed637e620bbe45a8d0e0c2f4d5130
Author: Changbin Du <changbin.du@huawei.com>
Date:   Tue Feb 27 10:35:46 2024 +0800

    modules: wait do_free_init correctly

    The synchronization here is to ensure the ordering of freeing of a module
    init so that it happens before W+X checking.  It is worth noting it is not
    that the freeing was not happening, it is just that our sanity checkers
    raced against the permission checkers which assume init memory is already
    gone.

    Commit 1a7b7d9220 ("modules: Use vmalloc special flag") moved calling
    do_free_init() into a global workqueue instead of relying on it being
    called through call_rcu(..., do_free_init), which used to allowed us call
    do_free_init() asynchronously after the end of a subsequent grace period.
    The move to a global workqueue broke the gaurantees for code which needed
    to be sure the do_free_init() would complete with rcu_barrier().  To fix
    this callers which used to rely on rcu_barrier() must now instead use
    flush_work(&init_free_wq).

    Without this fix, we still could encounter false positive reports in W+X
    checking since the rcu_barrier() here can not ensure the ordering now.

    Even worse, the rcu_barrier() can introduce significant delay.  Eric
    Chanudet reported that the rcu_barrier introduces ~0.1s delay on a
    PREEMPT_RT kernel.

      [    0.291444] Freeing unused kernel memory: 5568K
      [    0.402442] Run /sbin/init as init process

    With this fix, the above delay can be eliminated.

    Link: https://lkml.kernel.org/r/20240227023546.2490667-1-changbin.du@huawei.com
    Fixes: 1a7b7d9220 ("modules: Use vmalloc special flag")
    Signed-off-by: Changbin Du <changbin.du@huawei.com>
    Tested-by: Eric Chanudet <echanude@redhat.com>
    Acked-by: Luis Chamberlain <mcgrof@kernel.org>
    Cc: Xiaoyi Su <suxiaoyi@huawei.com>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:30 -04:00
Donald Dutile b17d15cce3 module: Expose module_init_layout_section()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 2abcc4b5a64a65a2d2287ba0be5c2871c1552416
Author: James Morse <james.morse@arm.com>
Date:   Tue Aug 1 14:54:07 2023 +0000

    module: Expose module_init_layout_section()

    module_init_layout_section() choses whether the core module loader
    considers a section as init or not. This affects the placement of the
    exit section when module unloading is disabled. This code will never run,
    so it can be free()d once the module has been initialised.

    arm and arm64 need to count the number of PLTs they need before applying
    relocations based on the section name. The init PLTs are stored separately
    so they can be free()d. arm and arm64 both use within_module_init() to
    decide which list of PLTs to use when applying the relocation.

    Because within_module_init()'s behaviour changes when module unloading
    is disabled, both architecture would need to take this into account when
    counting the PLTs.

    Today neither architecture does this, meaning when module unloading is
    disabled there are insufficient PLTs in the init section to load some
    modules, resulting in warnings:
    | WARNING: CPU: 2 PID: 51 at arch/arm64/kernel/module-plts.c:99 module_emit_plt_entry+0x184/0x1cc
    | Modules linked in: crct10dif_common
    | CPU: 2 PID: 51 Comm: modprobe Not tainted 6.5.0-rc4-yocto-standard-dirty #15208
    | Hardware name: QEMU KVM Virtual Machine, BIOS 0.0.0 02/06/2015
    | pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
    | pc : module_emit_plt_entry+0x184/0x1cc
    | lr : module_emit_plt_entry+0x94/0x1cc
    | sp : ffffffc0803bba60
    [...]
    | Call trace:
    |  module_emit_plt_entry+0x184/0x1cc
    |  apply_relocate_add+0x2bc/0x8e4
    |  load_module+0xe34/0x1bd4
    |  init_module_from_file+0x84/0xc0
    |  __arm64_sys_finit_module+0x1b8/0x27c
    |  invoke_syscall.constprop.0+0x5c/0x104
    |  do_el0_svc+0x58/0x160
    |  el0_svc+0x38/0x110
    |  el0t_64_sync_handler+0xc0/0xc4
    |  el0t_64_sync+0x190/0x194

    Instead of duplicating module_init_layout_section()s logic, expose it.

    Reported-by: Adam Johnston <adam.johnston@arm.com>
    Fixes: 055f23b74b ("module: check for exit sections in layout_sections() instead of module_init_section()")
    Cc: stable@vger.kernel.org
    Signed-off-by: James Morse <james.morse@arm.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:29 -04:00
Donald Dutile 70f40ed8e2 module: fix init_module_from_file() error handling
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit f1962207150c8b602e980616f04b37ea4e64bb9f
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Tue Jul 4 06:37:32 2023 -0700

    module: fix init_module_from_file() error handling

    Vegard Nossum pointed out two different problems with the error handling
    in init_module_from_file():

     (a) the idempotent loading code didn't clean up properly in some error
         cases, leaving the on-stack 'struct idempotent' element still in
         the hash table

     (b) failure to read the module file would nonsensically update the
         'invalid_kread_bytes' stat counter with the error value

    The first error is quite nasty, in that it can then cause subsequent
    idempotent loads of that same file to access stale stack contents of the
    previous failure.  The case may not happen in any normal situation
    (explaining all the "Tested-by's on the original change), and requires
    admin privileges, but syzkaller triggers random bad behavior as a
    result:

        BUG: soft lockup in sys_finit_module
        BUG: unable to handle kernel paging request in init_module_from_file
        general protection fault in init_module_from_file
        INFO: task hung in init_module_from_file
        KASAN: out-of-bounds Read in init_module_from_file
        KASAN: slab-out-of-bounds Read in init_module_from_file
        ...

    The second error is fairly benign and just leads to nonsensical stats
    (and has been around since the debug stats were added).

    Vegard also provided a patch for the idempotent loading issue, but I'd
    rather re-organize the code and make it more legible using another level
    of helper functions than add the usual "goto out" error handling.

    Link: https://lore.kernel.org/lkml/20230704100852.23452-1-vegard.nossum@oracle.com/
    Fixes: 9b9879fc0327 ("modules: catch concurrent module loads, treat them as idempotent")
    Reported-by: Vegard Nossum <vegard.nossum@oracle.com>
    Reported-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
    Reported-by: syzbot+9c2bdc9d24e4a7abe741@syzkaller.appspotmail.com
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile d8420b4b83 modules: catch concurrent module loads, treat them as idempotent
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 9b9879fc03275ffe0da328cf5b864d9e694167c8
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Mon May 29 21:39:51 2023 -0400

    modules: catch concurrent module loads, treat them as idempotent

    This is the new-and-improved attempt at avoiding huge memory load spikes
    when the user space boot sequence tries to load hundreds (or even
    thousands) of redundant duplicate modules in parallel.

    See commit 9828ed3f695a ("module: error out early on concurrent load of
    the same module file") for background and an earlier failed attempt that
    was reverted.

    That earlier attempt just said "concurrently loading the same module is
    silly, just open the module file exclusively and return -ETXTBSY if
    somebody else is already loading it".

    While it is true that concurrent module loads of the same module is
    silly, the reason that earlier attempt then failed was that the
    concurrently loaded module would often be a prerequisite for another
    module.

    Thus failing to load the prerequisite would then cause cascading
    failures of the other modules, rather than just short-circuiting that
    one unnecessary module load.

    At the same time, we still really don't want to load the contents of the
    same module file hundreds of times, only to then wait for an eventually
    successful load, and have everybody else return -EEXIST.

    As a result, this takes another approach, and treats concurrent module
    loads from the same file as "idempotent" in the inode.  So if one module
    load is ongoing, we don't start a new one, but instead just wait for the
    first one to complete and return the same return value as it did.

    So unlike the first attempt, this does not return early: the intent is
    not to speed up the boot, but to avoid a thundering herd problem in
    allocating memory (both physical and virtual) for a module more than
    once.

    Also note that this does change behavior: it used to be that when you
    had concurrent loads, you'd have one "winner" that would return success,
    and everybody else would return -EEXIST.

    In contrast, this idempotent logic goes all Oprah on the problem, and
    says "You are a winner! And you are a winner! We are ALL winners".  But
    since there's no possible actual real semantic difference between "you
    loaded the module" and "somebody else already loaded the module", this
    is more of a feel-good change than an actual honest-to-goodness semantic
    change.

    Of course, any true Johnny-come-latelies that don't get caught in the
    concurrency filter will still return -EEXIST.  It's no different from
    not even getting a seat at an Oprah taping.  That's life.

    See the long thread on the kernel mailing list about this all, which
    includes some numbers for memory use before and after the patch.

    Link: https://lore.kernel.org/lkml/20230524213620.3509138-1-mcgrof@kernel.org/
    Reviewed-by: Johan Hovold <johan@kernel.org>
    Tested-by: Johan Hovold <johan@kernel.org>
    Tested-by: Luis Chamberlain <mcgrof@kernel.org>
    Tested-by: Dan Williams <dan.j.williams@intel.com>
    Tested-by: Rudi Heitbaum <rudi@heitbaum..com>
    Tested-by: David Hildenbrand <david@redhat.com>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile 9e9e6cbdd2 module: split up 'finit_module()' into init_module_from_file() helper
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 054a73009c22a5fb8bbeee5394980809276bc9fe
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Mon May 29 20:55:13 2023 -0400

    module: split up 'finit_module()' into init_module_from_file() helper

    This will simplify the next step, where we can then key off the inode to
    do one idempotent module load.

    Let's do the obvious re-organization in one step, and then the new code
    in another.

    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:28 -04:00
Donald Dutile a2e3e61a15 module: fix module load for ia64
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit db3e33dd8bd956f165436afdbdbf1c653fb3c8e6
Author: Song Liu <song@kernel.org>
Date:   Sun May 28 16:00:41 2023 -0700

    module: fix module load for ia64

    Frank reported boot regression in ia64 as:

    ELILO v3.16 for EFI/IA-64
    ..
    Uncompressing Linux... done
    Loading file AC100221.initrd.img...done
    [    0.000000] Linux version 6.4.0-rc3 (root@x4270) (ia64-linux-gcc
    (GCC) 12.2.0, GNU ld (GNU Binutils) 2.39) #1 SMP Thu May 25 15:52:20
    CEST 2023
    [    0.000000] efi: EFI v1.1 by HP
    [    0.000000] efi: SALsystab=0x3ee7a000 ACPI 2.0=0x3fe2a000
    ESI=0x3ee7b000 SMBIOS=0x3ee7c000 HCDP=0x3fe28000
    [    0.000000] PCDP: v3 at 0x3fe28000
    [    0.000000] earlycon: uart8250 at MMIO 0x00000000f4050000 (options
    '9600n8')
    [    0.000000] printk: bootconsole [uart8250] enabled
    [    0.000000] ACPI: Early table checksum verification disabled
    [    0.000000] ACPI: RSDP 0x000000003FE2A000 000028 (v02 HP    )
    [    0.000000] ACPI: XSDT 0x000000003FE2A02C 0000CC (v01 HP     rx2620
    00000000 HP   00000000)
    [...]
    [    3.793350] Run /init as init process
    Loading, please wait...
    Starting systemd-udevd version 252.6-1
    [    3.951100] ------------[ cut here ]------------
    [    3.951100] WARNING: CPU: 6 PID: 140 at kernel/module/main.c:1547
    __layout_sections+0x370/0x3c0
    [    3.949512] Unable to handle kernel paging request at virtual address
    1000000000000000
    [    3.951100] Modules linked in:
    [    3.951100] CPU: 6 PID: 140 Comm: (udev-worker) Not tainted 6.4.0-rc3 #1
    [    3.956161] (udev-worker)[142]: Oops 11003706212352 [1]
    [    3.951774] Hardware name: hp server rx2620                   , BIOS
    04.29
    11/30/2007
    [    3.951774]
    [    3.951774] Call Trace:
    [    3.958339] Unable to handle kernel paging request at virtual address
    1000000000000000
    [    3.956161] Modules linked in:
    [    3.951774]  [<a0000001000156d0>] show_stack.part.0+0x30/0x60
    [    3.951774]                                 sp=e000000183a67b20
    bsp=e000000183a61628
    [    3.956161]
    [    3.956161]

    which bisect to module_memory change [1].

    Debug showed that ia64 uses some special sections:

    __layout_sections: section .got (sh_flags 10000002) matched to MOD_INVALID
    __layout_sections: section .sdata (sh_flags 10000003) matched to MOD_INVALID
    __layout_sections: section .sbss (sh_flags 10000003) matched to MOD_INVALID

    All these sections are loaded to module core memory before [1].

    Fix ia64 boot by loading these sections to MOD_DATA (core rw data).

    [1] commit ac3b43283923 ("module: replace module_layout with module_memory")

    Fixes: ac3b43283923 ("module: replace module_layout with module_memory")
    Reported-by: Frank Scheiner <frank.scheiner@web.de>
    Closes: https://lists.debian.org/debian-ia64/2023/05/msg00010.html
    Closes: https://marc.info/?l=linux-ia64&m=168509859125505
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: Song Liu <song@kernel.org>
    Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:27 -04:00
Donald Dutile b30cf49e7c module: Remove preempt_disable() from module reference counting.
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit cb0b50b813f6198b7d44ae8e169803440333577a
Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Date:   Tue May 9 15:49:02 2023 +0200

    module: Remove preempt_disable() from module reference counting.

    The preempt_disable() section in module_put() was added in commit
       e1783a240f ("module: Use this_cpu_xx to dynamically allocate counters")

    while the per-CPU counter were switched to another API. The API requires
    that during the RMW operation the CPU remained the same.

    This counting API was later replaced with atomic_t in commit
       2f35c41f58 ("module: Replace module_ref with atomic_t refcnt")

    Since this atomic_t replacement there is no need to keep preemption
    disabled while the reference counter is modified.

    Remove preempt_disable() from module_put(), __module_get() and
    try_module_get().

    Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:26 -04:00
Donald Dutile 2988d369f3 module: avoid allocation if module is already present and ready
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 064f4536d13939b6e8cdb71298ff5d657f4f8caa
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Fri Mar 10 20:48:03 2023 -0800

    module: avoid allocation if module is already present and ready

    The finit_module() system call can create unnecessary virtual memory
    pressure for duplicate modules. This is because load_module() can in
    the worse case allocate more than twice the size of a module in virtual
    memory. This saves at least a full size of the module in wasted vmalloc
    space memory by trying to avoid duplicates as soon as we can validate
    the module name in the read module structure.

    This can only be an issue if a system is getting hammered with userspace
    loading modules. There are two ways to load modules typically on systems,
    one is the kernel moduile auto-loading (*request_module*() calls in-kernel)
    and the other is things like udev. The auto-loading is in-kernel, but that
    pings back to userspace to just call modprobe. We already have a way to
    restrict the amount of concurrent kernel auto-loads in a given time, however
    that still allows multiple requests for the same module to go through
    and force two threads in userspace racing to call modprobe for the same
    exact module. Even though libkmod which both modprobe and udev does check
    if a module is already loaded prior calling finit_module() races are
    still possible and this is clearly evident today when you have multiple
    CPUs.

    To avoid memory pressure for such stupid cases put a stop gap for them.
    The *earliest* we can detect duplicates from the modules side of things
    is once we have blessed the module name, sadly after the first vmalloc
    allocation. We can check for the module being present *before* a secondary
    vmalloc() allocation.

    There is a linear relationship between wasted virtual memory bytes and
    the number of CPU counts. The reason is that udev ends up racing to call
    tons of the same modules for each of the CPUs.

    We can see the different linear relationships between wasted virtual
    memory and CPU count during after boot in the following graph:

             +----------------------------------------------------------------------------+
        14GB |-+          +            +            +           +           *+          +-|
             |                                                          ****              |
             |                                                       ***                  |
             |                                                     **                     |
        12GB |-+                                                 **                     +-|
             |                                                 **                         |
             |                                               **                           |
             |                                             **                             |
             |                                           **                               |
        10GB |-+                                       **                               +-|
             |                                       **                                   |
             |                                     **                                     |
             |                                   **                                       |
         8GB |-+                               **                                       +-|
    waste    |                               **                             ###           |
             |                             **                           ####              |
             |                           **                      #######                  |
         6GB |-+                     ****                    ####                       +-|
             |                      *                    ####                             |
             |                     *                 ####                                 |
             |                *****              ####                                     |
         4GB |-+            **               ####                                       +-|
             |            **             ####                                             |
             |          **           ####                                                 |
             |        **         ####                                                     |
         2GB |-+    **      #####                                                       +-|
             |     *    ####                                                              |
             |    * ####                                                   Before ******* |
             |  **##      +            +            +           +           After ####### |
             +----------------------------------------------------------------------------+
             0            50          100          150         200          250          300
                                              CPUs count

    On the y-axis we can see gigabytes of wasted virtual memory during boot
    due to duplicate module requests which just end up failing. Trying to
    infer the slope this ends up being about ~463 MiB per CPU lost prior
    to this patch. After this patch we only loose about ~230 MiB per CPU, for
    a total savings of about ~233 MiB per CPU. This is all *just on bootup*!

    On a 8vcpu 8 GiB RAM system using kdevops and testing against selftests
    kmod.sh -t 0008 I see a saving in the *highest* side of memory
    consumption of up to ~ 84 MiB with the Linux kernel selftests kmod
    test 0008. With the new stress-ng module test I see a 145 MiB difference
    in max memory consumption with 100 ops. The stress-ng module ops tests can be
    pretty pathalogical -- it is not realistic, however it was used to
    finally successfully reproduce issues which are only reported to happen on
    system with over 400 CPUs [0] by just usign 100 ops on a 8vcpu 8 GiB RAM
    system. Running out of virtual memory space is no surprise given the
    above graph, since at least on x86_64 we're capped at 128 MiB, eventually
    we'd hit a series of errors and once can use the above graph to
    guestimate when. This of course will vary depending on the features
    you have enabled. So for instance, enabling KASAN seems to make this
    much worse.

    The results with kmod and stress-ng can be observed and visualized below.
    The time it takes to run the test is also not affected.

    The kmod tests 0008:

    The gnuplot is set to a range from 400000 KiB (390 Mib) - 580000 (566 Mib)
    given the tests peak around that range.

    cat kmod.plot
    set term dumb
    set output fileout
    set yrange [400000:580000]
    plot filein with linespoints title "Memory usage (KiB)"

    Before:
    root@kmod ~ # /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
    root@kmod ~ # free -k -s 1 -c 40 | grep Mem | awk '{print $3}' > log-0008-before.txt ^C
    root@kmod ~ # sort -n -r log-0008-before.txt | head -1
    528732

    So ~516.33 MiB

    After:

    root@kmod ~ # /data/linux-next/tools/testing/selftests/kmod/kmod.sh -t 0008
    root@kmod ~ # free -k -s 1 -c 40 | grep Mem | awk '{print $3}' > log-0008-after.txt ^C

    root@kmod ~ # sort -n -r log-0008-after.txt | head -1
    442516

    So ~432.14 MiB

    That's about 84 ~MiB in savings in the worst case. The graphs:

    root@kmod ~ # gnuplot -e "filein='log-0008-before.txt'; fileout='graph-0008-before.txt'" kmod.plot
    root@kmod ~ # gnuplot -e "filein='log-0008-after.txt';  fileout='graph-0008-after.txt'"  kmod.plot

    root@kmod ~ # cat graph-0008-before.txt

      580000 +-----------------------------------------------------------------+
             |       +        +       +       +       +        +       +       |
      560000 |-+                                    Memory usage (KiB) ***A***-|
             |                                                                 |
      540000 |-+                                                             +-|
             |                                                                 |
             |        *A     *AA*AA*A*AA          *A*AA    A*A*A *AA*A*AA*A  A |
      520000 |-+A*A*AA  *AA*A           *A*AA*A*AA     *A*A     A          *A+-|
             |*A                                                               |
      500000 |-+                                                             +-|
             |                                                                 |
      480000 |-+                                                             +-|
             |                                                                 |
      460000 |-+                                                             +-|
             |                                                                 |
             |                                                                 |
      440000 |-+                                                             +-|
             |                                                                 |
      420000 |-+                                                             +-|
             |       +        +       +       +       +        +       +       |
      400000 +-----------------------------------------------------------------+
             0       5        10      15      20      25       30      35      40

    root@kmod ~ # cat graph-0008-after.txt

      580000 +-----------------------------------------------------------------+
             |       +        +       +       +       +        +       +       |
      560000 |-+                                    Memory usage (KiB) ***A***-|
             |                                                                 |
      540000 |-+                                                             +-|
             |                                                                 |
             |                                                                 |
      520000 |-+                                                             +-|
             |                                                                 |
      500000 |-+                                                             +-|
             |                                                                 |
      480000 |-+                                                             +-|
             |                                                                 |
      460000 |-+                                                             +-|
             |                                                                 |
             |          *A              *A*A                                   |
      440000 |-+A*A*AA*A  A       A*A*AA    A*A*AA*A*AA*A*AA*A*AA*AA*A*AA*A*AA-|
             |*A           *A*AA*A                                             |
      420000 |-+                                                             +-|
             |       +        +       +       +       +        +       +       |
      400000 +-----------------------------------------------------------------+
             0       5        10      15      20      25       30      35      40

    The stress-ng module tests:

    This is used to run the test to try to reproduce the vmap issues
    reported by David:

      echo 0 > /proc/sys/vm/oom_dump_tasks
      ./stress-ng --module 100 --module-name xfs

    Prior to this commit:
    root@kmod ~ # free -k -s 1 -c 40 | grep Mem | awk '{print $3}' > baseline-stress-ng.txt
    root@kmod ~ # sort -n -r baseline-stress-ng.txt | head -1
    5046456

    After this commit:
    root@kmod ~ # free -k -s 1 -c 40 | grep Mem | awk '{print $3}' > after-stress-ng.txt
    root@kmod ~ # sort -n -r after-stress-ng.txt | head -1
    4896972

    5046456 - 4896972
    149484
    149484/1024
    145.98046875000000000000

    So this commit using stress-ng reveals saving about 145 MiB in memory
    using 100 ops from stress-ng which reproduced the vmap issue reported.

    cat kmod.plot
    set term dumb
    set output fileout
    set yrange [4700000:5070000]
    plot filein with linespoints title "Memory usage (KiB)"

    root@kmod ~ # gnuplot -e "filein='baseline-stress-ng.txt'; fileout='graph-stress-ng-before.txt'"  kmod-simple-stress-ng.plot
    root@kmod ~ # gnuplot -e "filein='after-stress-ng.txt'; fileout='graph-stress-ng-after.txt'"  kmod-simple-stress-ng.plot

    root@kmod ~ # cat graph-stress-ng-before.txt

               +---------------------------------------------------------------+
      5.05e+06 |-+     + A     +       +       +       +       +       +     +-|
               |         *                          Memory usage (KiB) ***A*** |
               |         *                             A                       |
         5e+06 |-+      **                            **                     +-|
               |        **                            * *    A                 |
      4.95e+06 |-+      * *                          A  *   A*               +-|
               |        * *      A       A           *  *  *  *             A  |
               |       *  *     * *     * *        *A   *  *  *      A      *  |
       4.9e+06 |-+     *  *     * A*A   * A*AA*A  A      *A    **A   **A*A  *+-|
               |       A  A*A  A    *  A       *  *      A     A *  A    * **  |
               |      *      **      **         * *              *  *    * * * |
      4.85e+06 |-+   A       A       A          **               *  *     ** *-|
               |     *                           *               * *      ** * |
               |     *                           A               * *      *  * |
       4.8e+06 |-+   *                                           * *      A  A-|
               |     *                                           * *           |
      4.75e+06 |-+  *                                            * *         +-|
               |    *                                            **            |
               |    *  +       +       +       +       +       + **    +       |
       4.7e+06 +---------------------------------------------------------------+
               0       5       10      15      20      25      30      35      40

    root@kmod ~ # cat graph-stress-ng-after.txt

               +---------------------------------------------------------------+
      5.05e+06 |-+     +       +       +       +       +       +       +     +-|
               |                                    Memory usage (KiB) ***A*** |
               |                                                               |
         5e+06 |-+                                                           +-|
               |                                                               |
      4.95e+06 |-+                                                           +-|
               |                                                               |
               |                                                               |
       4.9e+06 |-+                                      *AA                  +-|
               |  A*AA*A*A  A  A*AA*AA*A*AA*A  A  A  A*A   *AA*A*A  A  A*AA*AA |
               |  *      * **  *            *  *  ** *            ***  *       |
      4.85e+06 |-+*       ***  *            * * * ***             A *  *     +-|
               |  *       A *  *             ** * * A               *  *       |
               |  *         *  *             *  **                  *  *       |
       4.8e+06 |-+*         *  *             A   *                  *  *     +-|
               | *          * *                  A                  * *        |
      4.75e+06 |-*          * *                                     * *      +-|
               | *          * *                                     * *        |
               | *     +    * *+       +       +       +       +    * *+       |
       4.7e+06 +---------------------------------------------------------------+
               0       5       10      15      20      25      30      35      40

    [0] https://lkml.kernel.org/r/20221013180518.217405-1-david@redhat.com

    Reported-by: David Hildenbrand <david@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile e41f7154cf module: add debug stats to help identify memory pressure
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
   Adding RHEL-only MODULE_STATS set to n for now. Possibly
   add in future -debug kernels, to be determined.
   Add rest of commit(s) to enable clean backport for further commits.

commit df3e764d8e5cd416efee29e0de3c93917dff5d33
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Tue Mar 28 20:03:19 2023 -0700

    module: add debug stats to help identify memory pressure

    Loading modules with finit_module() can end up using vmalloc(), vmap()
    and vmalloc() again, for a total of up to 3 separate allocations in the
    worst case for a single module. We always kernel_read*() the module,
    that's a vmalloc(). Then vmap() is used for the module decompression,
    and if so the last read buffer is freed as we use the now decompressed
    module buffer to stuff data into our copy module. The last allocation is
    specific to each architectures but pretty much that's generally a series
    of vmalloc() calls or a variation of vmalloc to handle ELF sections with
    special permissions.

    Evaluation with new stress-ng module support [1] with just 100 ops
    is proving that you can end up using GiBs of data easily even with all
    care we have in the kernel and userspace today in trying to not load modules
    which are already loaded. 100 ops seems to resemble the sort of pressure a
    system with about 400 CPUs can create on module loading. Although issues
    relating to duplicate module requests due to each CPU inucurring a new
    module reuest is silly and some of these are being fixed, we currently lack
    proper tooling to help diagnose easily what happened, when it happened
    and who likely is to blame -- userspace or kernel module autoloading.

    Provide an initial set of stats which use debugfs to let us easily scrape
    post-boot information about failed loads. This sort of information can
    be used on production worklaods to try to optimize *avoiding* redundant
    memory pressure using finit_module().

    There's a few examples that can be provided:

    A 255 vCPU system without the next patch in this series applied:

    Startup finished in 19.143s (kernel) + 7.078s (userspace) = 26.221s
    graphical.target reached after 6.988s in userspace

    And 13.58 GiB of virtual memory space lost due to failed module loading:

    root@big ~ # cat /sys/kernel/debug/modules/stats
             Mods ever loaded       67
         Mods failed on kread       0
    Mods failed on decompress       0
      Mods failed on becoming       0
          Mods failed on load       1411
            Total module size       11464704
          Total mod text size       4194304
           Failed kread bytes       0
      Failed decompress bytes       0
        Failed becoming bytes       0
            Failed kmod bytes       14588526272
     Virtual mem wasted bytes       14588526272
             Average mod size       171115
        Average mod text size       62602
      Average fail load bytes       10339140
    Duplicate failed modules:
                  module-name        How-many-times                    Reason
                    kvm_intel                   249                      Load
                          kvm                   249                      Load
                    irqbypass                     8                      Load
             crct10dif_pclmul                   128                      Load
          ghash_clmulni_intel                    27                      Load
                 sha512_ssse3                    50                      Load
               sha512_generic                   200                      Load
                  aesni_intel                   249                      Load
                  crypto_simd                    41                      Load
                       cryptd                   131                      Load
                        evdev                     2                      Load
                    serio_raw                     1                      Load
                   virtio_pci                     3                      Load
                         nvme                     3                      Load
                    nvme_core                     3                      Load
        virtio_pci_legacy_dev                     3                      Load
        virtio_pci_modern_dev                     3                      Load
                       t10_pi                     3                      Load
                       virtio                     3                      Load
                 crc32_pclmul                     6                      Load
               crc64_rocksoft                     3                      Load
                 crc32c_intel                    40                      Load
                  virtio_ring                     3                      Load
                        crc64                     3                      Load

    The following screen shot, of a simple 8vcpu 8 GiB KVM guest with the
    next patch in this series applied, shows 226.53 MiB are wasted in virtual
    memory allocations which due to duplicate module requests during boot.
    It also shows an average module memory size of 167.10 KiB and an an
    average module .text + .init.text size of 61.13 KiB. The end shows all
    modules which were detected as duplicate requests and whether or not
    they failed early after just the first kernel_read*() call or late after
    we've already allocated the private space for the module in
    layout_and_allocate(). A system with module decompression would reveal
    more wasted virtual memory space.

    We should put effort now into identifying the source of these duplicate
    module requests and trimming these down as much possible. Larger systems
    will obviously show much more wasted virtual memory allocations.

    root@kmod ~ # cat /sys/kernel/debug/modules/stats
             Mods ever loaded       67
         Mods failed on kread       0
    Mods failed on decompress       0
      Mods failed on becoming       83
          Mods failed on load       16
            Total module size       11464704
          Total mod text size       4194304
           Failed kread bytes       0
      Failed decompress bytes       0
        Failed becoming bytes       228959096
            Failed kmod bytes       8578080
     Virtual mem wasted bytes       237537176
             Average mod size       171115
        Average mod text size       62602
      Avg fail becoming bytes       2758544
      Average fail load bytes       536130
    Duplicate failed modules:
                  module-name        How-many-times                    Reason
                    kvm_intel                     7                  Becoming
                          kvm                     7                  Becoming
                    irqbypass                     6           Becoming & Load
             crct10dif_pclmul                     7           Becoming & Load
          ghash_clmulni_intel                     7           Becoming & Load
                 sha512_ssse3                     6           Becoming & Load
               sha512_generic                     7           Becoming & Load
                  aesni_intel                     7                  Becoming
                  crypto_simd                     7           Becoming & Load
                       cryptd                     3           Becoming & Load
                        evdev                     1                  Becoming
                    serio_raw                     1                  Becoming
                         nvme                     3                  Becoming
                    nvme_core                     3                  Becoming
                       t10_pi                     3                  Becoming
                   virtio_pci                     3                  Becoming
                 crc32_pclmul                     6           Becoming & Load
               crc64_rocksoft                     3                  Becoming
                 crc32c_intel                     3                  Becoming
        virtio_pci_modern_dev                     2                  Becoming
        virtio_pci_legacy_dev                     1                  Becoming
                        crc64                     2                  Becoming
                       virtio                     2                  Becoming
                  virtio_ring                     2                  Becoming

    [0] https://github.com/ColinIanKing/stress-ng.git
    [1] echo 0 > /proc/sys/vm/oom_dump_tasks
        ./stress-ng --module 100 --module-name xfs

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 5d28ce51db module: extract patient module check into helper
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit f71afa6a420111da90657fe999a8e32c42d5c7d6
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Fri Mar 10 20:05:52 2023 -0800

    module: extract patient module check into helper

    The patient module check inside add_unformed_module() is large
    enough as we need it. It is a bit hard to read too, so just
    move it to a helper and do the inverse checks first to help
    shift the code and make it easier to read. The new helper then
    is module_patient_check_exists().

    To make this work we need to mvoe the finished_loading() up,
    we do that without making any functional changes to that routine.

    Reviewed-by: David Hildenbrand <david@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 0e519b399a module: fix kmemleak annotations for non init ELF sections
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 430bb0d1c3376c988982f14bcbe71f917c89e1ab
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Tue Apr 4 18:52:47 2023 -0700

    module: fix kmemleak annotations for non init ELF sections

    Commit ac3b43283923 ("module: replace module_layout with module_memory")
    reworked the way to handle memory allocations to make it clearer. But it
    lost in translation how we handled kmemleak_ignore() or kmemleak_not_leak()
    for different ELF sections.

    Fix this and clarify the comments a bit more. Contrary to the old way
    of using kmemleak_ignore() for init.* ELF sections we stick now only to
    kmemleak_not_leak() as per suggestion by Catalin Marinas so to avoid
    any false positives and simplify the code.

    Fixes: ac3b43283923 ("module: replace module_layout with module_memory")
    Reported-by: Jim Cromie <jim.cromie@gmail.com>
    Acked-by: Song Liu <song@kernel.org>
    Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
    Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 58c52f78c0 module: already_uses() - reduce pr_debug output volume
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 33c951f62920d144ca89daa0560180a49afb6f1e
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Tue Mar 21 19:36:23 2023 -0600

    module: already_uses() - reduce pr_debug output volume

    already_uses() is unnecessarily chatty.

    `modprobe i915` yields 491 messages like:

      [   64.108744] i915 uses drm!

    This is a normal situation, and isn't worth all the log entries.

    NOTE: I've preserved the "does not use %s" messages, which happens
    less often, but does happen.  Its not clear to me what it tells a
    reader, or what info might improve the pr_debug's utility.

    [ 6847.584999] main:already_uses:569: amdgpu does not use ttm!
    [ 6847.585001] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585014] main:already_uses:569: amdgpu does not use drm!
    [ 6847.585016] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585024] main:already_uses:569: amdgpu does not use drm_display_helper!
    [ 6847.585025] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585084] main:already_uses:569: amdgpu does not use drm_kms_helper!
    [ 6847.585086] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585175] main:already_uses:569: amdgpu does not use drm_buddy!
    [ 6847.585176] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585202] main:already_uses:569: amdgpu does not use i2c_algo_bit!
    [ 6847.585204] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585249] main:already_uses:569: amdgpu does not use gpu_sched!
    [ 6847.585250] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585314] main:already_uses:569: amdgpu does not use video!
    [ 6847.585315] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585409] main:already_uses:569: amdgpu does not use iommu_v2!
    [ 6847.585410] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6847.585816] main:already_uses:569: amdgpu does not use drm_ttm_helper!
    [ 6847.585818] main:add_module_usage:584: Allocating new usage for amdgpu.
    [ 6848.762268] dyndbg: add-module: amdgpu.2533 sites

    no functional changes.

    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 5bcdd7cfaf module: add section-size to move_module pr_debug
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 66a2301edf313d630c2ece4f3721c5b3402653ee
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Tue Mar 21 19:36:22 2023 -0600

    module: add section-size to move_module pr_debug

    move_module() pr_debug's "Final section addresses for $modname".
    Add section addresses to the message, for anyone looking at these.

    no functional changes.

    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile db74bac793 module: add symbol-name to pr_debug Absolute symbol
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit b10addf37bbcaee66672eb54c15532266c8daea6
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Tue Mar 21 19:36:21 2023 -0600

    module: add symbol-name to pr_debug Absolute symbol

    The pr_debug("Absolute symbol" ..) reports value, (which is usually
    0), but not the name, which is more informative.  So add it.

    no functional changes

    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:25 -04:00
Donald Dutile 38e20bcc3c module: in layout_sections, move_module: add the modname
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 6ed81802d4d1b037ad2d1657511ff0c2e9aeda14
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Tue Mar 21 19:36:20 2023 -0600

    module: in layout_sections, move_module: add the modname

    layout_sections() and move_module() each issue ~50 messages for each
    module loaded.  Add mod-name into their 2 header lines, to help the
    reader find his module.

    no functional changes.

    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile d97fc0bedf module: merge remnants of setup_load_info() to elf validation
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 3d40bb903ed1f654707d34bdd61ee2c332000e4b
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:35:41 2023 -0700

    module: merge remnants of setup_load_info() to elf validation

    The setup_load_info() was actually had ELF validation checks of its
    own. To later cache useful variables as an secondary step just means
    looping again over the ELF sections we just validated. We can simply
    keep tabs of the key sections of interest as we validate the module
    ELF section in one swoop, so do that and merge the two routines
    together.

    Expand a bit on the documentation / intent / goals.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 724139c3b1 module: move more elf validity checks to elf_validity_check()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 1bb49db9919a4d4186cba288930e7026d8f7ec96
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:35:40 2023 -0700

    module: move more elf validity checks to elf_validity_check()

    The symbol and strings section validation currently happen in
    setup_load_info() but since they are also doing validity checks
    move this to elf_validity_check().

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 33c789403d module: add stop-grap sanity check on module memcpy()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit c7ee8aebf6c0588c0aab76538aff395c3abf811c
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:35:39 2023 -0700

    module: add stop-grap sanity check on module memcpy()

    The integrity of the struct module we load is important, and although
    our ELF validator already checks that the module section must match
    struct module, add a stop-gap check before we memcpy() the final minted
    module. This also makes those inspecting the code what the goal is.

    While at it, clarify the goal behind updating the sh_addr address.
    The current comment is pretty misleading.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile d6be2f8a79 module: add sanity check for ELF module section
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 46752820f9abc013b6bd8172562b642376723313
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:35:38 2023 -0700

    module: add sanity check for ELF module section

    The ELF ".gnu.linkonce.this_module" section is special, it is what we
    use to construct the struct module __this_module, which THIS_MODULE
    points to. When userspace loads a module we always deal first with a
    copy of the userspace buffer, and twiddle with the userspace copy's
    version of the struct module. Eventually we allocate memory to do a
    memcpy() of that struct module, under the assumption that the module
    size is right. But we have no validity checks against the size or
    the requirements for the section.

    Add some validity checks for the special module section early and while
    at it, cache the module section index early, so we don't have to do that
    later.

    While at it, just move over the assigment of the info->mod to make the
    code clearer. The validity checker also adds an explicit size check to
    ensure the module section size matches the kernel's run time size for
    sizeof(struct module). This should prevent sloppy loads of modules
    which are built today *without* actually increasing the size of
    the struct module. A developer today can for example expand the size
    of struct module, rebuild a directoroy 'make fs/xfs/' for example and
    then try to insmode the driver there. That module would in effect have
    an incorrect size. This new size check would put a stop gap against such
    mistakes.

    This also makes the entire goal of ".gnu.linkonce.this_module" pretty
    clear. Before this patch verification of the goal / intent required some
    Indian Jones whips, torches and cleaning up big old spider webs.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 57edbf0df7 module: rename check_module_license_and_versions() to check_export_symbol_versions()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 419e1a20f7bdef5380fde5ed73f05c98c28a598b
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:46 2023 -0700

    module: rename check_module_license_and_versions() to check_export_symbol_versions()

    This makes the routine easier to understand what the check its checking for.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 4f4151e7ba module: converge taint work together
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 72f08b3cc631f4ebcaa9f373d18fc0b877fb6458
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:45 2023 -0700

    module: converge taint work together

    Converge on a compromise: so long as we have a module hit our linked
    list of modules we taint. That is, the module was about to become live.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 88c5188c62 module: move signature taint to module_augment_kernel_taints()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit c3bbf62ebf8c9e87cea875cfa146f44f46af4145
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:44 2023 -0700

    module: move signature taint to module_augment_kernel_taints()

    Just move the signature taint into the helper:

      module_augment_kernel_taints()

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile d3fa8f2f9b module: move tainting until after a module hits our linked list
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit a12b94511cf36855cd731c16005bd535e2007552
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:43 2023 -0700

    module: move tainting until after a module hits our linked list

    It is silly to have taints spread out all over, we can just compromise
    and add them if the module ever hit our linked list. Our sanity checkers
    should just prevent crappy drivers / bogus ELF modules / etc and kconfig
    options should be enough to let you *not* load things you don't want.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile a24c049ebc module: split taint adding with info checking
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 437c1f9cc61fd37829eaf12d8ae2f7dcc5dddce0
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:42 2023 -0700

    module: split taint adding with info checking

    check_modinfo() actually does two things:

     a) sanity checks, some of which are fatal, and so we
        prevent the user from completing trying to load a module
     b) taints the kernel

    The taints are pretty heavy handed because we're tainting the kernel
    *before* we ever even get to load the module into the modules linked
    list. That is, it it can fail for other reasons later as we review the
    module's structure.

    But this commit makes no functional changes, it just makes the intent
    clearer and splits the code up where needed to make that happen.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile e714cf9a02 module: split taint work out of check_modinfo_livepatch()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit ed52cabecb7a7e4242cee9c370c2622a47177d5d
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:41 2023 -0700

    module: split taint work out of check_modinfo_livepatch()

    The work to taint the kernel due to a module should be split
    up eventually. To aid with this, split up the tainting on
    check_modinfo_livepatch().

    This let's us bring more early checks together which do return
    a value, and makes changes easier to read later where we stuff
    all the work to do the taints in one single routine.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile 8e754a6a03 module: rename set_license() to module_license_taint_check()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit ad8d3a36e981064f8a646531cddca30516894457
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:40 2023 -0700

    module: rename set_license() to module_license_taint_check()

    The set_license() routine would seem to a reader to do some sort of
    setting, but it does not. It just adds a taint if the license is
    not set or proprietary.

    This makes what the code is doing clearer, so much we can remove
    the comment about it.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile f0137dd418 module: move check_modinfo() early to early_mod_check()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 02da2cbab452a236fa67abc9fc9e47430934e652
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:39 2023 -0700

    module: move check_modinfo() early to early_mod_check()

    This moves check_modinfo() to early_mod_check(). This
    doesn't make any functional changes either, as check_modinfo()
    was the first call on layout_and_allocate(), so we're just
    moving it back one routine and at the end.

    This let's us keep separate the checkers from the allocator.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile ddd3fcade0 module: move early sanity checks into a helper
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
  Source diff due to RHEL-only commit d2857e67 .

commit 85e6f61c134f111232d27d3f63667c1bccbbc12d
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:38 2023 -0700

    module: move early sanity checks into a helper

    Move early sanity checkers for the module into a helper.
    This let's us make it clear when we are working with the
    local copy of the module prior to allocation.

    This produces no functional changes, it just makes subsequent
    changes easier to read.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:24 -04:00
Donald Dutile b7f1a70598 module: add a for_each_modinfo_entry()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 1e684172358453df1cb783d7c101a09ff08ceee1
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:37 2023 -0700

    module: add a for_each_modinfo_entry()

    Add a for_each_modinfo_entry() to make it easier to read and use.
    This produces no functional changes but makes this code easiert
    to read as we are used to with loops in the kernel and trims more
    lines of code.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile 0932ad05e1 module: rename next_string() to module_next_tag_pair()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit feb5b784a26363b690f618213450faf244c1c58e
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:36 2023 -0700

    module: rename next_string() to module_next_tag_pair()

    This makes it clearer what it is doing. While at it,
    make it available to other code other than main.c.
    This will be used in the subsequent patch and make
    the changes easier to read.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile 592e213d4a module: move get_modinfo() helpers all above
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit b66973b82d4426b318f78a20c6b39ddd977a508a
Author: Luis Chamberlain <mcgrof@kernel.org>
Date:   Sun Mar 19 14:27:35 2023 -0700

    module: move get_modinfo() helpers all above

    Instead of forward declaring routines for get_modinfo() just move
    everything up. This makes no functional changes.

    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile d92b090692 dyndbg: use the module notifier callbacks
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 7deabd67498869640c937c9bd83472574b7dea0b
Author: Jason Baron <jbaron@akamai.com>
Date:   Fri Mar 3 11:50:56 2023 -0500

    dyndbg: use the module notifier callbacks

    Bring dynamic debug in line with other subsystems by using the module
    notifier callbacks. This results in a net decrease in core module
    code.

    Additionally, Jim Cromie has a new dynamic debug classmap feature,
    which requires that jump labels be initialized prior to dynamic debug.
    Specifically, the new feature toggles a jump label from the existing
    dynamic_debug_setup() function. However, this does not currently work
    properly, because jump labels are initialized via the
    'module_notify_list' notifier chain, which is invoked after the
    current call to dynamic_debug_setup(). Thus, this patch ensures that
    jump labels are initialized prior to dynamic debug by setting the
    dynamic debug notifier priority to 0, while jump labels have the
    higher priority of 1.

    Tested by Jim using his new test case, and I've verfied the correct
    printing via: # modprobe test_dynamic_debug dyndbg.

    Link: https://lore.kernel.org/lkml/20230113193016.749791-21-jim.cromie@gmail.com/
    Reported-by: kernel test robot <lkp@intel.com>
    Link: https://lore.kernel.org/oe-kbuild-all/202302190427.9iIK2NfJ-lkp@intel.com/
    Tested-by: Jim Cromie <jim.cromie@gmail.com>
    Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
    Cc: Peter Zijlstra <peterz@infradead.org>
    CC: Jim Cromie <jim.cromie@gmail.com>
    Cc: Luis Chamberlain <mcgrof@kernel.org>
    Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Signed-off-by: Jason Baron <jbaron@akamai.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile 792e9a4375 module: Remove the unused function within
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflict: Minor source diff due to dropping CFI_CLANG patch

commit 9e07f161717ab8e8ac1206bf82546511e24cbb7b
Author: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
Date:   Fri Feb 10 14:42:43 2023 +0800

    module: Remove the unused function within

    The function within is defined in the main.c file, but not called
    elsewhere, so remove this unused function.

    This routine became no longer used after commit ("module: replace
    module_layout with module_memory").

    kernel/module/main.c:3007:19: warning: unused function 'within'.

    Reported-by: Abaci Robot <abaci@linux.alibaba.com>
    Link: https://bugzilla.openanolis.cn/show_bug.cgi?id=4035
    Signed-off-by: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
    [mcgrof: adjust commit log to explain why this change is needed]
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:23 -04:00
Donald Dutile 985fe82319 module: replace module_layout with module_memory
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflict: Remove PPC's module_32.c hunk since not supported in RHEL.

commit ac3b43283923440900b4f36ca5f9f0b1ca43b70e
Author: Song Liu <song@kernel.org>
Date:   Mon Feb 6 16:28:02 2023 -0800

    module: replace module_layout with module_memory

    module_layout manages different types of memory (text, data, rodata, etc.)
    in one allocation, which is problematic for some reasons:

    1. It is hard to enable CONFIG_STRICT_MODULE_RWX.
    2. It is hard to use huge pages in modules (and not break strict rwx).
    3. Many archs uses module_layout for arch-specific data, but it is not
       obvious how these data are used (are they RO, RX, or RW?)

    Improve the scenario by replacing 2 (or 3) module_layout per module with
    up to 7 module_memory per module:

            MOD_TEXT,
            MOD_DATA,
            MOD_RODATA,
            MOD_RO_AFTER_INIT,
            MOD_INIT_TEXT,
            MOD_INIT_DATA,
            MOD_INIT_RODATA,

    and allocating them separately. This adds slightly more entries to
    mod_tree (from up to 3 entries per module, to up to 7 entries per
    module). However, this at most adds a small constant overhead to
    __module_address(), which is expected to be fast.

    Various archs use module_layout for different data. These data are put
    into different module_memory based on their location in module_layout.
    IOW, data that used to go with text is allocated with MOD_MEM_TYPE_TEXT;
    data that used to go with data is allocated with MOD_MEM_TYPE_DATA, etc.

    module_memory simplifies quite some of the module code. For example,
    ARCH_WANTS_MODULES_DATA_IN_VMALLOC is a lot cleaner, as it just uses a
    different allocator for the data. kernel/module/strict_rwx.c is also
    much cleaner with module_memory.

    Signed-off-by: Song Liu <song@kernel.org>
    Cc: Luis Chamberlain <mcgrof@kernel.org>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Guenter Roeck <linux@roeck-us.net>
    Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
    Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
    Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
    Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile a6ed52cfeb module: Use kstrtobool() instead of strtobool()
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit fbed4fea6422a237382bf317db88a37993955f3b
Author: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Date:   Tue Nov 1 22:14:06 2022 +0100

    module: Use kstrtobool() instead of strtobool()

    strtobool() is the same as kstrtobool().
    However, the latter is more used within the kernel.

    In order to remove strtobool() and slightly simplify kstrtox.h, switch to
    the other function name.

    While at it, include the corresponding header file (<linux/kstrtox.h>)

    Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
    Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
    Reviewed-by: Aaron Tomlin <atomlin@atomlin.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile 4ce2725b0a module: add module_elf_check_arch for module-specific checks
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit f9231a996e229c13d23f907352c2cea84bd1c30a
Author: Nicholas Piggin <npiggin@gmail.com>
Date:   Mon Nov 28 14:15:36 2022 +1000

    module: add module_elf_check_arch for module-specific checks

    The elf_check_arch() function is also used to test compatibility of
    usermode binaries. Kernel modules may have more specific requirements,
    for example powerpc would like to test for ABI version compatibility.

    Add a weak module_elf_check_arch() that defaults to true, and call it
    from elf_validity_check().

    Signed-off-by: Jessica Yu <jeyu@kernel.org>
    [np: added changelog, adjust name, rebase]
    Acked-by: Luis Chamberlain <mcgrof@kernel.org>
    Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
    Reviewed-by: Joel Stanley <joel@jms.id.au>
    Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
    Link: https://lore.kernel.org/r/20221128041539.1742489-2-npiggin@gmail.com

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:22 -04:00
Donald Dutile b7f09a83fe module: Remove unused macros module_addr_min/max
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 89a6b5917650edd1542194fd5b5ada64fb94a790
Author: Chen Zhongjin <chenzhongjin@huawei.com>
Date:   Sat Sep 24 15:22:16 2022 +0800

    module: Remove unused macros module_addr_min/max

    Unused macros reported by [-Wunused-macros].

    These macros are introduced to record the bound address of modules.

    Commit 80b8bf436990 ("module: Always have struct mod_tree_root") made
    "struct mod_tree_root" always present and its members addr_min and
    addr_max can be directly accessed.

    Macros module_addr_min and module_addr_min are not used anymore, so remove
    them.

    Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
    Reviewed-by: Miroslav Benes <mbenes@suse.cz>
    Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu>
    [mcgrof: massaged the commit messsage as suggested by Miroslav]
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:21 -04:00
Donald Dutile 332de072de cfi: Remove CONFIG_CFI_CLANG_SHADOW
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 9fca7115827b2e5f48d84e50bceb4edfd4cb6375
Author: Sami Tolvanen <samitolvanen@google.com>
Date:   Thu Sep 8 14:54:45 2022 -0700

    cfi: Remove CONFIG_CFI_CLANG_SHADOW

    In preparation to switching to -fsanitize=kcfi, remove support for the
    CFI module shadow that will no longer be needed.

    Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
    Reviewed-by: Kees Cook <keescook@chromium.org>
    Tested-by: Kees Cook <keescook@chromium.org>
    Tested-by: Nathan Chancellor <nathan@kernel.org>
    Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Tested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Signed-off-by: Kees Cook <keescook@chromium.org>
    Link: https://lore.kernel.org/r/20220908215504.3686827-4-samitolvanen@google.com

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:20 -04:00
Donald Dutile 887fc8b989 kernel/module: add __dyndbg_classes section
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
  RHEL-9 backported upstream commit 9b351be25360c in
  RHEL-9 commit abfc8d9dd1 to resolve
  CVE-2023-20569.  The similar CVE fix for dyndbg_classes
  has to be applied to original start/stop being replacement by
  BOUNDED_SECTION macro.
commit 66f4006b6ace1a1a1a1dca4225972f79a298e251
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Sun Sep 4 15:40:52 2022 -0600

    kernel/module: add __dyndbg_classes section

    Add __dyndbg_classes section, using __dyndbg as a model. Use it:

    vmlinux.lds.h:

    KEEP the new section, which also silences orphan section warning on
    loadable modules.  Add (__start_/__stop_)__dyndbg_classes linker
    symbols for the c externs (below).

    kernel/module/main.c:
    - fill new fields in find_module_sections(), using section_objs()
    - extend callchain prototypes
      to pass classes, length
      load_module(): pass new info to dynamic_debug_setup()
      dynamic_debug_setup(): new params, pass through to ddebug_add_module()

    dynamic_debug.c:
    - add externs to the linker symbols.

    ddebug_add_module():
    - It currently builds a debug_table, and *will* find and attach classes.

    dynamic_debug_init():
    - add class fields to the _ddebug_info cursor var: di.

    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Link: https://lore.kernel.org/r/20220904214134.408619-16-jim.cromie@gmail.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:20 -04:00
Donald Dutile c91fd16d3d dyndbg: gather __dyndbg[] state into struct _ddebug_info
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit b7b4eebdba7b6aea6b34dc29691b71c39d1dbd6a
Author: Jim Cromie <jim.cromie@gmail.com>
Date:   Sun Sep 4 15:40:48 2022 -0600

    dyndbg: gather __dyndbg[] state into struct _ddebug_info

    This new struct composes the linker provided (vector,len) section,
    and provides a place to add other __dyndbg[] state-data later:

      descs - the vector of descriptors in __dyndbg section.
      num_descs - length of the data/section.

    Use it, in several different ways, as follows:

    In lib/dynamic_debug.c:

    ddebug_add_module(): Alter params-list, replacing 2 args (array,index)
    with a struct _ddebug_info * containing them both, with room for
    expansion.  This helps future-proof the function prototype against the
    looming addition of class-map info into the dyndbg-state, by providing
    a place to add more member fields later.

    NB: later add static struct _ddebug_info builtins_state declaration,
    not needed yet.

    ddebug_add_module() is called in 2 contexts:

    In dynamic_debug_init(), declare, init a struct _ddebug_info di
    auto-var to use as a cursor.  Then iterate over the prdbg blocks of
    the builtin modules, and update the di cursor before calling
    _add_module for each.

    Its called from kernel/module/main.c:load_info() for each loaded
    module:

    In internal.h, alter struct load_info, replacing the dyndbg array,len
    fields with an embedded _ddebug_info containing them both; and
    populate its members in find_module_sections().

    The 2 calling contexts differ in that _init deals with contiguous
    subranges of __dyndbgs[] section, packed together, while loadable
    modules are added one at a time.

    So rename ddebug_add_module() into outer/__inner fns, call __inner
    from _init, and provide the offset into the builtin __dyndbgs[] where
    the module's prdbgs reside.  The cursor provides start, len of the
    subrange for each.  The offset will be used later to pack the results
    of builtin __dyndbg_sites[] de-duplication, and is 0 and unneeded for
    loadable modules,

    Note:

    kernel/module/main.c includes <dynamic_debug.h> for struct
    _ddeubg_info.  This might be prone to include loops, since its also
    included by printk.h.  Nothing has broken in robot-land on this.

    cc: Luis Chamberlain <mcgrof@kernel.org>
    Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
    Link: https://lore.kernel.org/r/20220904214134.408619-12-jim.cromie@gmail.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:20 -04:00
Donald Dutile 6fe228534f module: Show the last unloaded module's taint flag(s)
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 6f1dae1d84b6d08541d8e12edd1c8677ab279dea
Author: Aaron Tomlin <atomlin@redhat.com>
Date:   Thu Jul 14 16:39:33 2022 +0100

    module: Show the last unloaded module's taint flag(s)

    For diagnostic purposes, this patch, in addition to keeping a record/or
    track of the last known unloaded module, we now will include the
    module's taint flag(s) too e.g: " [last unloaded: fpga_mgr_mod(OE)]"

    Signed-off-by: Aaron Tomlin <atomlin@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile 4758c09808 module: Use strscpy() for last_unloaded_module
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit dbf0ae65bce48bf4c2b6d114cac10193ef050294
Author: Aaron Tomlin <atomlin@redhat.com>
Date:   Thu Jul 14 16:39:32 2022 +0100

    module: Use strscpy() for last_unloaded_module

    The use of strlcpy() is considered deprecated [1].
    In this particular context, there is no need to remain with strlcpy().
    Therefore we transition to strscpy().

    [1]: https://www.kernel.org/doc/html/latest/process/deprecated.html#strlcpy

    Signed-off-by: Aaron Tomlin <atomlin@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile 76837e4c34 module: Modify module_flags() to accept show_state argument
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 17dd25c29cda98c370f8d5a4ae3daee33fac1669
Author: Aaron Tomlin <atomlin@redhat.com>
Date:   Thu Jul 14 16:39:31 2022 +0100

    module: Modify module_flags() to accept show_state argument

    No functional change.

    With this patch a given module's state information (i.e. 'mod->state')
    can be omitted from the specified buffer. Please note that this is in
    preparation to include the last unloaded module's taint flag(s),
    if available.

    Signed-off-by: Aaron Tomlin <atomlin@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile b3afc5ba53 module: panic: Taint the kernel when selftest modules load
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 74829ddf5977567d77440150d72d4c0c5c427446
Author: David Gow <davidgow@google.com>
Date:   Fri Jul 8 12:48:45 2022 +0800

    module: panic: Taint the kernel when selftest modules load

    Taint the kernel with TAINT_TEST whenever a test module loads, by adding
    a new "TEST" module property, and setting it for all modules in the
    tools/testing directory. This property can also be set manually, for
    tests which live outside the tools/testing directory with:
    MODULE_INFO(test, "Y");

    Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
    Reviewed-by: Aaron Tomlin <atomlin@redhat.com>
    Acked-by: Brendan Higgins <brendanhiggins@google.com>
    Signed-off-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile 95b20fb23b module: Use vzalloc() instead of vmalloc()/memset(0)
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit 2b9401e90d369b5fbb8a62e9034ad97297594475
Author: Yang Yingliang <yangyingliang@huawei.com>
Date:   Mon Jul 4 20:03:37 2022 +0800

    module: Use vzalloc() instead of vmalloc()/memset(0)

    Use vzalloc() instead of vmalloc() and memset(0) to simpify the code.

    Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
    Reviewed-by: Aaron Tomlin <atomlin@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile 7ba5b273ad module: Fix ERRORs reported by checkpatch.pl
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
  Minor source diff due to RHEL9 3bade5d33 backport out of order
  to upstream.

commit ecc726f1458e7aa50e120ff334f0a3be5cccd94c
Author: Christophe Leroy <christophe.leroy@csgroup.eu>
Date:   Mon Jun 13 08:02:01 2022 +0200

    module: Fix ERRORs reported by checkpatch.pl

    Checkpatch reports following errors:

    ERROR: do not use assignment in if condition
    +       if ((colon = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {

    ERROR: do not use assignment in if condition
    +               if ((mod = find_module_all(name, colon - name, false)) != NULL)

    ERROR: do not use assignment in if condition
    +                       if ((ret = find_kallsyms_symbol_value(mod, name)) != 0)

    ERROR: do not initialise globals to 0
    +int modules_disabled = 0;

    Fix them.

    The following one has to remain, because the condition has to be evaluated
    multiple times by the macro wait_event_interruptible_timeout().

    ERROR: do not use assignment in if condition
    +       if (wait_event_interruptible_timeout(module_wq,

    Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile ca8f0d3fa6 module: Add support for default value for module async_probe
JIRA: https://issues.redhat.com/browse/RHEL-28063

commit ae39e9ed964f8e450d0de410b5a757e19581dfc5
Author: Saravana Kannan <saravanak@google.com>
Date:   Fri Jun 3 18:01:00 2022 -0700

    module: Add support for default value for module async_probe

    Add a module.async_probe kernel command line option that allows enabling
    async probing for all modules. When this command line option is used,
    there might still be some modules for which we want to explicitly force
    synchronous probing, so extend <modulename>.async_probe to take an
    optional bool input so that async probing can be disabled for a specific
    module.

    Signed-off-by: Saravana Kannan <saravanak@google.com>
    Reviewed-by: Aaron Tomlin <atomlin@redhat.com>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00
Donald Dutile bbec585621 module: Fix "warning: variable 'exit' set but not used"
JIRA: https://issues.redhat.com/browse/RHEL-28063

Conflicts:
  Minor source diff due to RHEL9 3bade5d33 backport out of order
  to upstream.

commit f963ef123900ac534aeb6141642e5351989ac14c
Author: Christophe Leroy <christophe.leroy@csgroup.eu>
Date:   Sun Jun 12 17:33:20 2022 +0200

    module: Fix "warning: variable 'exit' set but not used"

    When CONFIG_MODULE_UNLOAD is not selected, 'exit' is
    set but never used.

    It is not possible to replace the #ifdef CONFIG_MODULE_UNLOAD by
    IS_ENABLED(CONFIG_MODULE_UNLOAD) because mod->exit doesn't exist
    when CONFIG_MODULE_UNLOAD is not selected.

    And because of the rcu_read_lock_sched() section it is not easy
    to regroup everything in a single #ifdef. Let's regroup partially
    and add missing #ifdef to completely opt out the use of
    'exit' when CONFIG_MODULE_UNLOAD is not selected.

    Reported-by: kernel test robot <lkp@intel.com>
    Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
    Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>

Signed-off-by: Donald Dutile <ddutile@redhat.com>
2024-06-17 14:17:18 -04:00