Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] selftests: cgroup: default KDIR in test_modules/Makefile breaks build - [Medium] selftests: cgroup: race on selftest_region in dmem_selftest_exit() - [Medium] selftests: cgroup: ERR_PTR stored in selftest_region in dmem_selftest_init() --
commit ff1dfd0d8169df4ccb406a2e9530169608e94814 Author: Albert Esteve <[email protected]> selftests: cgroup: Add dmem_selftest helper module This commit adds an out-of-tree dmem_selftest kernel module under tools/testing/selftests/cgroup/test_modules/ that registers a synthetic dmem cgroup region and exposes alloc and free module parameters for selftests. > diff --git a/tools/testing/selftests/cgroup/test_modules/Makefile > b/tools/testing/selftests/cgroup/test_modules/Makefile > new file mode 100644 > index 0000000000000..dd8a17313cec2 > --- /dev/null > +++ b/tools/testing/selftests/cgroup/test_modules/Makefile > @@ -0,0 +1,17 @@ > +# SPDX-License-Identifier: GPL-2.0 > +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) > +KDIR ?= /lib/modules/$(shell uname -r)/build > + > +obj-m += dmem_selftest.o > + > +# Ensure that KDIR exists, otherwise skip the compilation > +modules: > +ifneq ("$(wildcard $(KDIR))", "") > + $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR) > +endif [Severity: Medium] Does defaulting KDIR to /lib/modules/$(shell uname -r)/build break the cgroup selftest build when the host's running kernel headers are older than the target kernel tree or when cross-compiling? Because tools/testing/selftests/cgroup/Makefile sets TEST_GEN_MODS_DIR, the default all target invokes this Makefile unconditionally. When /lib/modules/$(uname -r)/build exists for an older host kernel that lacks linux/cgroup_dmem.h or struct dmem_cgroup_init, Kbuild compiles dmem_selftest.c against the host kernel headers instead of the tree under test and fails the build. In addition, checking $(wildcard $(KDIR)) without checking $(wildcard $(KDIR)/Module.symvers) does not verify that the kernel tree has been built. Defaulting KDIR to $(if $(O),$(O),$(realpath ../../../../..)) and checking $(wildcard $(KDIR)/Module.symvers), as in tools/testing/selftests/sched_ext/test_modules/Makefile, would build against the target kernel tree when available. [ ... ] > diff --git a/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c > b/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c > new file mode 100644 > index 0000000000000..a763a156f13f3 > --- /dev/null > +++ b/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c > @@ -0,0 +1,146 @@ [ ... ] > +static struct dmem_cgroup_region *selftest_region; > +static struct dmem_cgroup_pool_state *alloc_pool; > +static u64 alloc_size; > +static DEFINE_MUTEX(alloc_lock); > + > +static int param_set_alloc(const char *val, const struct kernel_param *kp) > +{ > + struct dmem_cgroup_pool_state *pool = NULL, *limit = NULL; > + u64 size; > + int ret; > + > + if (!selftest_region) > + return -ENODEV; > + > + ret = kstrtou64(val, 0, &size); > + if (ret) > + return ret; > + if (!size || size > DM_SELFTEST_REGION_SIZE) > + return -EINVAL; > + > + mutex_lock(&alloc_lock); > + if (alloc_pool) { > + mutex_unlock(&alloc_lock); > + return -EBUSY; > + } > + > + ret = dmem_cgroup_try_charge(selftest_region, size, &pool, &limit); > + if (ret == -EAGAIN && limit) > + dmem_cgroup_pool_state_put(limit); > + if (ret) { > + mutex_unlock(&alloc_lock); > + return ret; > + } > + > + alloc_pool = pool; > + alloc_size = size; > + mutex_unlock(&alloc_lock); > + return 0; > +} [ ... ] > +static int __init dmem_selftest_init(void) > +{ > + static const struct dmem_cgroup_init init = { > + .size = DM_SELFTEST_REGION_SIZE, > + }; > + > + selftest_region = dmem_cgroup_register_region(&init, > DM_SELFTEST_REGION_NAME); > + if (IS_ERR(selftest_region)) > + return PTR_ERR(selftest_region); [Severity: Medium] If dmem_cgroup_register_region() returns an error pointer, can a concurrent write to the alloc parameter dereference that ERR_PTR in dmem_cgroup_try_charge()? Module parameter sysfs attributes are created by mod_sysfs_setup() before dmem_selftest_init() runs and remain accessible until do_init_module() tears them down on error. Storing the return value directly into selftest_region leaves a non-NULL ERR_PTR(-ENOMEM) visible to param_set_alloc(), which only checks !selftest_region before passing it to dmem_cgroup_try_charge() -> get_cg_pool_unlocked(). Checking the return value in a local variable before assigning selftest_region avoids exposing the error pointer. [ ... ] > +static void __exit dmem_selftest_exit(void) > +{ > + mutex_lock(&alloc_lock); > + if (alloc_pool) { > + dmem_cgroup_uncharge(alloc_pool, alloc_size); > + alloc_pool = NULL; > + alloc_size = 0; > + } > + mutex_unlock(&alloc_lock); > + > + if (selftest_region) { > + dmem_cgroup_unregister_region(selftest_region); > + selftest_region = NULL; > + } > + pr_info("unloaded.\n"); > +} [Severity: Medium] Can param_set_alloc() race with dmem_selftest_exit() over selftest_region and alloc_pool? In delete_module(), dmem_selftest_exit() runs before free_module() removes the module parameter sysfs files, and param_attr_store() does not hold a module reference: CPU0 CPU1 param_set_alloc() dmem_selftest_exit() if (!selftest_region) mutex_lock(&alloc_lock) ... mutex_unlock(&alloc_lock) dmem_cgroup_unregister_region(...) selftest_region = NULL mutex_lock(&alloc_lock) dmem_cgroup_try_charge(selftest_region, ...) get_cg_pool_unlocked(NULL, ...) <-- NULL pointer dereference If CPU0 instead acquires alloc_lock right after CPU1 unlocks it and before dmem_cgroup_unregister_region() marks the region unregistered, dmem_cgroup_try_charge() succeeds and sets alloc_pool after dmem_selftest_exit() has already finished uncharging alloc_pool, leaking the charge, pool state, and cgroup CSS reference. Checking selftest_region under alloc_lock in param_set_alloc() and detaching selftest_region under the same lock in dmem_selftest_exit() before unregistering the region would close both races. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=1
