mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Håkon Bugge" <haakon.bugge@oracle.com>
To: linux-kernel@vger.kernel.org,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun@kernel.org>, Waiman Long <longman@redhat.com>,
	Chris Wilson <chris@chris-wilson.co.uk>,
	John Stultz <jstultz@google.com>
Cc: "Håkon Bugge" <haakon.bugge@oracle.com>,
	"Ingo Molnar" <mingo@kernel.org>
Subject: [PATCH v3 1/3] test-ww_mutex: Fix module cleanup
Date: Tue,  4 Aug 2026 18:31:34 +0200	[thread overview]
Message-ID: <20260804163139.3121781-2-haakon.bugge@oracle.com> (raw)
In-Reply-To: <20260804163139.3121781-1-haakon.bugge@oracle.com>

When test-ww_mutex is compiled as a module, there are two bugs. One is
related to the case where the module fails to load. In that case, some
or all of the allocated objects are not released. Also, if loaded OK
and thereafter unloaded, the sysfs file is not removed.

The latter bug gives the following splat (trimmed for better brevity):

sysfs: cannot create duplicate filename '/kernel/test_ww_mutex'
 dump_stack_lvl+0x5d/0x90
 sysfs_warn_dup.cold+0x17/0x23
 sysfs_create_dir_ns+0xe2/0x100
 kobject_add_internal+0xba/0x270
 kobject_add+0x95/0xd0
 kobject_create_and_add+0xa3/0xd0
 test_ww_mutex_init+0x90/0xff0 [test_ww_mutex]
 do_one_initcall+0x5c/0x330
 do_init_module+0x90/0x290
 __do_sys_init_module+0x195/0x1d0
 do_syscall_64+0xe8/0x550
 entry_SYSCALL_64_after_hwframe+0x76/0x7e

Fixes: 2a0c11282881 ("locking/ww_mutex: Add kselftests for ww_mutex stress")
Fixes: de2c5a1523fd ("test-ww_mutex: Allow test to be run (and re-run) from userland")
Signed-off-by: Håkon Bugge <haakon.bugge@oracle.com>
Acked-by: John Stultz <jstultz@google.com>

---

v1 -> v2:
      * Keep the sysfs file if module is built-in even when the test
        fails. This in order to be able to run the test multiple
        times even in case of errors. Suggested by John Stultz
---
 kernel/locking/test-ww_mutex.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c
index 838d631544ed2..8311ff38ab683 100644
--- a/kernel/locking/test-ww_mutex.c
+++ b/kernel/locking/test-ww_mutex.c
@@ -734,7 +734,7 @@ static struct kobject *test_ww_mutex_kobj;
 
 static int __init test_ww_mutex_init(void)
 {
-	int ret;
+	int ret = -ENOMEM;
 
 	prandom_seed_state(&rng, get_random_u64());
 
@@ -743,28 +743,35 @@ static int __init test_ww_mutex_init(void)
 		return -ENOMEM;
 
 	test_ww_mutex_kobj = kobject_create_and_add("test_ww_mutex", kernel_kobj);
-	if (!test_ww_mutex_kobj) {
-		destroy_workqueue(wq);
-		return -ENOMEM;
-	}
+	if (!test_ww_mutex_kobj)
+		goto kobj_err;
 
 	/* Create the files associated with this kobject */
 	ret = sysfs_create_group(test_ww_mutex_kobj, &attr_group);
-	if (ret) {
-		kobject_put(test_ww_mutex_kobj);
-		destroy_workqueue(wq);
-		return ret;
-	}
+	if (ret)
+		goto sysfs_err;
 
 	mutex_lock(&run_lock);
 	ret = run_test_classes();
 	mutex_unlock(&run_lock);
 
+	if (!ret || IS_BUILTIN(CONFIG_WW_MUTEX_SELFTEST))
+		return 0;
+
+	sysfs_remove_group(test_ww_mutex_kobj, &attr_group);
+
+sysfs_err:
+	kobject_put(test_ww_mutex_kobj);
+
+kobj_err:
+	destroy_workqueue(wq);
+
 	return ret;
 }
 
 static void __exit test_ww_mutex_exit(void)
 {
+	sysfs_remove_group(test_ww_mutex_kobj, &attr_group);
 	kobject_put(test_ww_mutex_kobj);
 	destroy_workqueue(wq);
 }
-- 
2.43.5


  reply	other threads:[~2026-08-04 16:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 16:31 [PATCH v3 0/3] test-ww_mutex: Miscellaneous fixes Håkon Bugge
2026-08-04 16:31 ` Håkon Bugge [this message]
2026-08-04 16:31 ` [PATCH v3 2/3] test-ww_mutex: Report errors from stress workers Håkon Bugge
2026-08-04 16:31 ` [PATCH v3 3/3] test-ww_mutex: Handle transient -EDEADLK in test_cycle_work Håkon Bugge
2026-08-18  8:35   ` Haakon Bugge

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=20260804163139.3121781-2-haakon.bugge@oracle.com \
    --to=haakon.bugge@oracle.com \
    --cc=boqun@kernel.org \
    --cc=chris@chris-wilson.co.uk \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=will@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

all inboxes | Powered by JetHome®