dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Albert Esteve" <aesteve@redhat.com>
Cc: dri-devel@lists.freedesktop.org, tj@kernel.org,
	hannes@cmpxchg.org, mkoutny@suse.com
Subject: Re: [PATCH v7 1/3] selftests: cgroup: Add dmem_selftest helper module
Date: Tue, 17 Nov 2026 13:46:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-218900@kernel.org> (raw)
In-Reply-To: <20261117-kunit_cgroups-v7-1-5bcbfe17f323@redhat.com>

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 <aesteve@redhat.com>

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/20261117-kunit_cgroups-v7-0-5bcbfe17f323@redhat.com?part=1

  reply	other threads:[~2026-11-17 13:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 13:27 [PATCH v7 0/3] cgroup: dmem: add selftest helper and coverage Albert Esteve
2026-11-17 13:27 ` [PATCH v7 1/3] selftests: cgroup: Add dmem_selftest helper module Albert Esteve
2026-11-17 13:46   ` sashiko-bot [this message]
2026-11-17 13:27 ` [PATCH v7 2/3] selftests: cgroup: Add KB() helper Albert Esteve
2026-11-17 13:27 ` [PATCH v7 3/3] selftests: cgroup: Add dmem selftest coverage Albert Esteve
2026-11-17 13:33   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-218900@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aesteve@redhat.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hannes@cmpxchg.org \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox