The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jia He <justin.he@arm.com>
To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-perf-users@vger.kernel.org,
	linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	kasan-dev@googlegroups.com, linux-mm@kvack.org
Cc: Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Brendan Higgins <brendan.higgins@linux.dev>,
	David Gow <david@davidgow.net>, Rae Moar <raemoar63@gmail.com>,
	Alexander Potapenko <glider@google.com>,
	Marco Elver <elver@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jia He <justin.he@arm.com>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Petr Mladek <pmladek@suse.com>, Kees Cook <kees@kernel.org>,
	David Disseldorp <ddiss@suse.de>
Subject: [PATCH 4/7] kunit: platform: catch duplicate (name, id) in kunit_platform_device_add()
Date: Thu, 14 May 2026 05:04:52 +0000	[thread overview]
Message-ID: <20260514050455.2954509-5-justin.he@arm.com> (raw)
In-Reply-To: <20260514050455.2954509-1-justin.he@arm.com>

kunit_platform_device_add_twice_fails_test registers the same device
twice and expects failure. Without an early check the second
platform_device_add() reaches sysfs_create_dir_ns() and dumps a
backtrace via sysfs_warn_dup() on every run.

Walk the platform bus for a device with matching name and id and return
-EEXIST. PLATFORM_DEVID_AUTO assigns its id inside add() and can't
collide, so skip it.

To: Brendan Higgins <brendan.higgins@linux.dev>
To: David Gow <david@davidgow.net>
To: Rae Moar <raemoar63@gmail.com>
Cc: linux-kselftest@vger.kernel.org
Cc: kunit-dev@googlegroups.com

Signed-off-by: Jia He <justin.he@arm.com>
---
 lib/kunit/platform.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/lib/kunit/platform.c b/lib/kunit/platform.c
index 0b518de26065..515f7c859014 100644
--- a/lib/kunit/platform.c
+++ b/lib/kunit/platform.c
@@ -7,6 +7,7 @@
 #include <linux/device/bus.h>
 #include <linux/device/driver.h>
 #include <linux/platform_device.h>
+#include <linux/string.h>
 
 #include <kunit/platform_device.h>
 #include <kunit/resource.h>
@@ -80,6 +81,20 @@ kunit_platform_device_alloc_match(struct kunit *test,
 
 KUNIT_DEFINE_ACTION_WRAPPER(platform_device_unregister_wrapper,
 			    platform_device_unregister, struct platform_device *);
+
+struct kunit_pdev_dup_match {
+	const char *name;
+	int id;
+};
+
+static int kunit_pdev_dup_match_fn(struct device *dev, void *data)
+{
+	struct kunit_pdev_dup_match *m = data;
+	struct platform_device *p = to_platform_device(dev);
+
+	return p->id == m->id && p->name && !strcmp(p->name, m->name);
+}
+
 /**
  * kunit_platform_device_add() - Register a KUnit test managed platform device
  * @test: test context
@@ -95,6 +110,22 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev)
 	struct kunit_resource *res;
 	int ret;
 
+	/*
+	 * Detect duplicate (name, id) registrations early, before
+	 * platform_device_add() reaches sysfs_create_dir_ns() and
+	 * unconditionally dumps a stack trace via sysfs_warn_dup(). This keeps
+	 * tests that intentionally exercise the duplicate-add failure path
+	 * (e.g. kunit_platform_device_add_twice_fails_test) quiet without
+	 * losing the negative return value they assert on.
+	 */
+	if (pdev->name && pdev->id != PLATFORM_DEVID_AUTO) {
+		struct kunit_pdev_dup_match m = { pdev->name, pdev->id };
+
+		if (bus_for_each_dev(&platform_bus_type, NULL, &m,
+				     kunit_pdev_dup_match_fn))
+			return -EEXIST;
+	}
+
 	ret = platform_device_add(pdev);
 	if (ret)
 		return ret;
-- 
2.34.1


  parent reply	other threads:[~2026-05-14  5:07 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-14  5:04 [PATCH 0/7] Silence spurious warnings and crashes from kunit test suites Jia He
2026-05-14  5:04 ` [PATCH 1/7] init/initramfs_test: wait_for_initramfs() before running Jia He
2026-05-14  5:58   ` David Disseldorp
2026-05-14  5:04 ` [PATCH 2/7] kfence: kunit: skip when no pool is available Jia He
2026-05-14  7:54   ` Marco Elver
2026-05-14  5:04 ` [PATCH 3/7] lib/math/int_log: drop WARN_ON for value == 0 Jia He
2026-05-14  5:04 ` Jia He [this message]
2026-05-14  5:04 ` [PATCH 5/7] misc: reject duplicate names in misc_register() Jia He
2026-05-14  7:00   ` Greg Kroah-Hartman
2026-05-14  7:25     ` Justin He
2026-05-14  5:04 ` [PATCH 6/7] hw_breakpoint_test: fix test_many_cpus failure on large systems Jia He
2026-05-14  8:01   ` Marco Elver
2026-05-14  5:04 ` [PATCH 7/7] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak Jia He
2026-05-14  7:02 ` [PATCH 0/7] Silence spurious warnings and crashes from kunit test suites Greg Kroah-Hartman
2026-05-14  7:17   ` Justin He

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=20260514050455.2954509-5-justin.he@arm.com \
    --to=justin.he@arm.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=brauner@kernel.org \
    --cc=brendan.higgins@linux.dev \
    --cc=david@davidgow.net \
    --cc=ddiss@suse.de \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=irogers@google.com \
    --cc=jack@suse.cz \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kasan-dev@googlegroups.com \
    --cc=kees@kernel.org \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=raemoar63@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    /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