From nobody Fri Sep 25 12:34:04 2026 Received: from mail.zeus03.de (zeus03.de [194.117.254.33]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 910213A6F09 for ; Sat, 12 Sep 2026 19:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.117.254.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; cv=none; b=E0xoldcJZ7cLkP2wKUXqTOa51ZMNosbDAD/4hwZuoUS4cCiq9lfAWZiBvdAddSjGxM53gaSFuGigR/c4qGwVcHbeOrz4aSz99o/OpFrkwXkSgi7ZC1POGg/PaNPa/OrgJeKT0dIr1jh8E0ST2zzd8SabDxCeD8w+uOZIZg1G+ak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; c=relaxed/simple; bh=g2OlAyGIZhV1BsjkNZjWxvFvD5hfEZl+LPj+baHDxvo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UE1q0RzUxaXZV7DMh/72eVL0teVBbvb8pFnVBMpvjVZa6X3RmU3oI66Cn8aUyVgfvAJsfuADFmcNs539KAjL4Ep/fMwTIQSk/70MUv2KsAtipMUnNqguvEfD0wQlvR48NsJBsvg4wPzpKCoi8OsaepcM/T7mdkroV2MRL2N1wqk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com; spf=pass smtp.mailfrom=sang-engineering.com; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b=VtHzbjHt; arc=none smtp.client-ip=194.117.254.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b="VtHzbjHt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=oKNzB0ykC78dixpgq9ZZ5mFwGGg9bDpNVa6wSyleTuY=; b=VtHzbj HtWpozsqVKGRWSyoeMFNnIOLMe8/WEIynp+5BacS/ZjLszPaA7YJmWoWgHvqB+0M QVHtCUs/GzDpoGMHPgVQ3k71paePFiizKlUoQWoZB91xGVWKanM8KleVeKVozm/j Ow0n28u1fUAfhi3C4kM1mTVW1EymA/8hTrSYh0MXGDrbx6w72CWqwJ98D7wyYd3C /0zvZlfVOfX8wHy8riL3Bj7njPEVwyc6XwOCdRkc5oDWVWt4qmEhFtZVDKuYvFNu xSoc0YzLStxXOF7FHQag+o/kpwABg8z7ZR+mDCAFO5QK4qLXW+VBkH+UiHO07LUb tMVjoFFtrmEtfivA== Received: (qmail 105222 invoked from network); 12 Sep 2026 21:14:50 +0200 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 12 Sep 2026 21:14:50 +0200 X-UD-Smtp-Session: l3s3148p1@szXYA05bqrYujnsz From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: linux-kernel@vger.kernel.org, "Matthew Wilcox (Oracle)" , Wolfram Sang , Bjorn Andersson , Baolin Wang , linux-remoteproc@vger.kernel.org Subject: [PATCH v7 1/3] hwspinlock: Convert to XArray Date: Sat, 12 Sep 2026 21:14:39 +0200 Message-ID: <20260912191442.99453-2-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> References: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Matthew Wilcox (Oracle)" The radix tree is deprecated. The XArray uses the same data structure with a nicer interface. The hwspinlock_tree_lock is not needed as the spinlock built into the XArray is sufficient for all these cases after we split a larger critical section in hwspin_lock_request_specific() into two smaller ones. Signed-off-by: Matthew Wilcox (Oracle) Co-developed-by: Wolfram Sang Signed-off-by: Wolfram Sang --- drivers/hwspinlock/hwspinlock_core.c | 170 +++++++++++---------------- 1 file changed, 71 insertions(+), 99 deletions(-) diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwsp= inlock_core.c index a509b73da190..22e6c7690f1a 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -27,35 +27,21 @@ /* retry delay used in atomic context */ #define HWSPINLOCK_RETRY_DELAY_US 100 =20 -/* radix tree tags */ -#define HWSPINLOCK_UNUSED (0) /* tags an hwspinlock as unused */ +/* XArray search mark */ +#define HWSPINLOCK_UNUSED XA_MARK_0 /* marks a hwspinlock as unused */ =20 /* - * A radix tree is used to maintain the available hwspinlock instances. + * An XArray is used to maintain the available hwspinlock instances. * The tree associates hwspinlock pointers with their integer key id, * and provides easy-to-use API which makes the hwspinlock core code simple * and easy to read. * - * Radix trees are quick on lookups, and reasonably efficient in terms of + * XArrays are quick on lookups, and reasonably efficient in terms of * storage, especially with high density usages such as this framework * requires (a continuous range of integer keys, beginning with zero, is - * used as the ID's of the hwspinlock instances). - * - * The radix tree API supports tagging items in the tree, which this - * framework uses to mark unused hwspinlock instances (see the - * HWSPINLOCK_UNUSED tag above). As a result, the process of querying the - * tree, looking for an unused hwspinlock instance, is now reduced to a - * single radix tree API call. + * used as the ID of the hwspinlock instances). */ -static RADIX_TREE(hwspinlock_tree, GFP_KERNEL); - -/* - * Synchronization of access to the tree is achieved using this mutex, - * as the radix-tree API requires that users provide all synchronisation. - * A mutex is needed because we're using non-atomic radix tree allocations. - */ -static DEFINE_MUTEX(hwspinlock_tree_lock); - +static DEFINE_XARRAY(hwspinlocks); =20 /** * __hwspin_trylock() - attempt to lock a specific hwspinlock @@ -369,10 +355,9 @@ of_hwspin_lock_simple_xlate(const struct of_phandle_ar= gs *hwlock_spec) */ int of_hwspin_lock_get_id(struct device_node *np, int index) { + XA_STATE(xas, &hwspinlocks, 0); struct of_phandle_args args; struct hwspinlock *hwlock; - struct radix_tree_iter iter; - void **slot; int id; int ret; =20 @@ -389,15 +374,9 @@ int of_hwspin_lock_get_id(struct device_node *np, int = index) /* Find the hwspinlock device: we need its base_id */ ret =3D -EPROBE_DEFER; rcu_read_lock(); - radix_tree_for_each_slot(slot, &hwspinlock_tree, &iter, 0) { - hwlock =3D radix_tree_deref_slot(slot); - if (unlikely(!hwlock)) + xas_for_each(&xas, hwlock, ULONG_MAX) { + if (xas_retry(&xas, hwlock)) continue; - if (radix_tree_deref_retry(hwlock)) { - slot =3D radix_tree_iter_retry(&iter); - continue; - } - if (device_match_of_node(hwlock->bank->dev, args.np)) { ret =3D 0; break; @@ -452,51 +431,50 @@ EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id_byname); =20 static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id) { - struct hwspinlock *tmp; + XA_STATE(xas, &hwspinlocks, id); + struct hwspinlock *existing; int ret; =20 - mutex_lock(&hwspinlock_tree_lock); - - ret =3D radix_tree_insert(&hwspinlock_tree, id, hwlock); - if (ret) { - if (ret =3D=3D -EEXIST) + do { + xas_lock(&xas); + existing =3D xas_load(&xas); + if (existing) { pr_err("hwspinlock id %d already exists!\n", id); - goto out; - } - - /* mark this hwspinlock as available */ - tmp =3D radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); + xas_set_err(&xas, -EBUSY); + } + xas_store(&xas, hwlock); =20 - /* self-sanity check which should never fail */ - WARN_ON(tmp !=3D hwlock); + /* mark this hwspinlock as available */ + xas_set_mark(&xas, HWSPINLOCK_UNUSED); + ret =3D xas_error(&xas); + xas_unlock(&xas); + } while (xas_nomem(&xas, GFP_KERNEL)); =20 -out: - mutex_unlock(&hwspinlock_tree_lock); return ret; } =20 static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id) { + XA_STATE(xas, &hwspinlocks, id); struct hwspinlock *hwlock =3D NULL; - int ret; + bool unused; =20 - mutex_lock(&hwspinlock_tree_lock); + xas_lock(&xas); + xas_load(&xas); =20 - /* make sure the hwspinlock is not in use (tag is set) */ - ret =3D radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); - if (ret =3D=3D 0) { + /* make sure the hwspinlock is not in use (mark is set) */ + unused =3D xas_get_mark(&xas, HWSPINLOCK_UNUSED); + if (!unused) { pr_err("hwspinlock %d still in use (or not present)\n", id); goto out; } =20 - hwlock =3D radix_tree_delete(&hwspinlock_tree, id); - if (!hwlock) { - pr_err("failed to delete hwspinlock %d\n", id); - goto out; - } + hwlock =3D xas_store(&xas, NULL); + if (!hwlock) + pr_err("hwspinlock %d already deleted\n", id); =20 out: - mutex_unlock(&hwspinlock_tree_lock); + xas_unlock(&xas); return hwlock; } =20 @@ -666,20 +644,18 @@ int devm_hwspin_lock_register(struct device *dev, EXPORT_SYMBOL_GPL(devm_hwspin_lock_register); =20 /** - * __hwspin_lock_request() - tag an hwspinlock as used and power it up + * hwspin_lock_prepare() - prepare a hwspinlock * @hwlock: the target hwspinlock * * This is an internal function that prepares an hwspinlock instance - * before it is given to the user. The function assumes that - * hwspinlock_tree_lock is taken. + * before it is given to the user. * * Returns: %0 or positive to indicate success, and a negative value to * indicate an error (with the appropriate error code) */ -static int __hwspin_lock_request(struct hwspinlock *hwlock) +static int hwspin_lock_prepare(struct hwspinlock *hwlock) { struct device *dev =3D hwlock->bank->dev; - struct hwspinlock *tmp; int ret; =20 /* prevent underlying implementation from being removed */ @@ -697,16 +673,7 @@ static int __hwspin_lock_request(struct hwspinlock *hw= lock) return ret; } =20 - ret =3D 0; - - /* mark hwspinlock as used, should not fail */ - tmp =3D radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock), - HWSPINLOCK_UNUSED); - - /* self-sanity check that should never fail */ - WARN_ON(tmp !=3D hwlock); - - return ret; + return 0; } =20 /** @@ -724,36 +691,43 @@ static int __hwspin_lock_request(struct hwspinlock *h= wlock) */ struct hwspinlock *hwspin_lock_request_specific(unsigned int id) { + XA_STATE(xas, &hwspinlocks, id); struct hwspinlock *hwlock; + bool unused; int ret; =20 - mutex_lock(&hwspinlock_tree_lock); + xas_lock(&xas); =20 - /* make sure this hwspinlock exists */ - hwlock =3D radix_tree_lookup(&hwspinlock_tree, id); + hwlock =3D xas_load(&xas); if (!hwlock) { + xas_unlock(&xas); pr_warn("hwspinlock %u does not exist\n", id); - goto out; + return NULL; } =20 /* sanity check (this shouldn't happen) */ WARN_ON(hwlock_to_id(hwlock) !=3D id); =20 - /* make sure this hwspinlock is unused */ - ret =3D radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); - if (ret =3D=3D 0) { + unused =3D xas_get_mark(&xas, HWSPINLOCK_UNUSED); + if (!unused) { + xas_unlock(&xas); pr_warn("hwspinlock %u is already in use\n", id); - hwlock =3D NULL; - goto out; + return NULL; } =20 - /* mark as used and power up */ - ret =3D __hwspin_lock_request(hwlock); - if (ret < 0) - hwlock =3D NULL; + xas_clear_mark(&xas, HWSPINLOCK_UNUSED); + xas_unlock(&xas); + + ret =3D hwspin_lock_prepare(hwlock); + if (ret < 0) { + xas_lock(&xas); + xas_set(&xas, hwlock_to_id(hwlock)); + xas_load(&xas); + xas_set_mark(&xas, HWSPINLOCK_UNUSED); + xas_unlock(&xas); + return NULL; + } =20 -out: - mutex_unlock(&hwspinlock_tree_lock); return hwlock; } EXPORT_SYMBOL_GPL(hwspin_lock_request_specific); @@ -772,9 +746,10 @@ EXPORT_SYMBOL_GPL(hwspin_lock_request_specific); */ int hwspin_lock_free(struct hwspinlock *hwlock) { + XA_STATE(xas, &hwspinlocks, 0); struct device *dev; - struct hwspinlock *tmp; - int ret; + bool unused; + int ret =3D 0; =20 if (!hwlock) { pr_err("invalid hwlock\n"); @@ -782,12 +757,13 @@ int hwspin_lock_free(struct hwspinlock *hwlock) } =20 dev =3D hwlock->bank->dev; - mutex_lock(&hwspinlock_tree_lock); + xas_lock(&xas); + xas_set(&xas, hwlock_to_id(hwlock)); + xas_load(&xas); =20 /* make sure the hwspinlock is used */ - ret =3D radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock), - HWSPINLOCK_UNUSED); - if (ret =3D=3D 1) { + unused =3D xas_get_mark(&xas, HWSPINLOCK_UNUSED); + if (unused) { dev_err(dev, "%s: hwlock is already free\n", __func__); dump_stack(); ret =3D -EINVAL; @@ -798,16 +774,12 @@ int hwspin_lock_free(struct hwspinlock *hwlock) pm_runtime_put(dev); =20 /* mark this hwspinlock as available */ - tmp =3D radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock), - HWSPINLOCK_UNUSED); - - /* sanity check (this shouldn't happen) */ - WARN_ON(tmp !=3D hwlock); + xas_set_mark(&xas, HWSPINLOCK_UNUSED); =20 module_put(dev->driver->owner); =20 out: - mutex_unlock(&hwspinlock_tree_lock); + xas_unlock(&xas); return ret; } EXPORT_SYMBOL_GPL(hwspin_lock_free); --=20 2.53.0 From nobody Fri Sep 25 12:34:04 2026 Received: from mail.zeus03.de (zeus03.de [194.117.254.33]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85FD23ACEFF for ; Sat, 12 Sep 2026 19:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.117.254.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; cv=none; b=jFgxZJvI7wn0SJLPNdNrTwO71dN1GK+8OXYhGDaMm1q3lr+5gH64xzw9pW1PxuMvYR0TTlVTpHoUmnrBOrtAc60Qb3yuU/phOy9Bl8cE23QxQkBrgLDebmZlDnaXVxDsAbDCkpYVTns1jcNOfpWJR7lvfLtFGi0LU0j91JMT768= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; c=relaxed/simple; bh=kUfGKQJUM1bjIgwGz5HbFEddUBfOn+xvu7p7leeI2CE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JxYFXbANu3XDfPs95rtaEKJRoBowah2R/HtyPJH/3aiyHmsrWAnHDqHAmBcrnDjmk9z1cqedyzI+p50SjOhm4KX89C1eDv/fohVwzuPo/E42WiRaO+A+K3A4UUlzy6fADGUBXDTslGkxzxTcV7GP54vN94/1l5GJnZjrvoan/xw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com; spf=pass smtp.mailfrom=sang-engineering.com; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b=UyoQn4sa; arc=none smtp.client-ip=194.117.254.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b="UyoQn4sa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=2ejGIxvhgYYXpPcXPvycx1UBrvu5TXq0T73D1y/llc0=; b=UyoQn4 sakC9hw3KJ2qfMTATs6lsUdSwW5ba2LGDUP111D1BI502BSmGRqWwTpRTgEyGH6b j+rf2ORhpPPQFC3uDqbXSC+tv/C8JfezqSCicssxu2R9OqAMNHSVaELXJLMpedYv 9/yNqM1Ihg3kxWIFbVV7mQbfyx8diG/DdueXB39v+8KudC7Xl+PsIA7jvL+AvuJQ 5+AIinCXSI4Gahr26pOOTAPT25638uXZ1+jBubQ/24Oi0IzDew2dnouoQpeYFuf+ Tjl50pUUtbclf7MmztrKVlbTXxxCyCZ4h5pIoWsSss+Kk47pzPJ5QI8TgjdkkpUa HRLuLNXq1mYk1zDg== Received: (qmail 105254 invoked from network); 12 Sep 2026 21:14:51 +0200 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 12 Sep 2026 21:14:51 +0200 X-UD-Smtp-Session: l3s3148p1@TiniA05btrYujnsz From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Wolfram Sang , Bjorn Andersson , Baolin Wang , linux-remoteproc@vger.kernel.org Subject: [PATCH v7 2/3] hwspinlock: add list of locks to debugfs Date: Sat, 12 Sep 2026 21:14:40 +0200 Message-ID: <20260912191442.99453-3-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> References: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To help debugging, offer a list of registered hwspinlocks in debugfs. It will also print the responsible hwspinlock_device and if the spinlock is currently used. It does not show if the lock is taken because of side-effects. Often, reading a lock means actually taking it. Signed-off-by: Wolfram Sang Reviewed-by: Geert Uytterhoeven Tested-by: Geert Uytterhoeven --- drivers/hwspinlock/hwspinlock_core.c | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwsp= inlock_core.c index 22e6c7690f1a..947b4d4fa153 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -9,6 +9,7 @@ =20 #define pr_fmt(fmt) "%s: " fmt, __func__ =20 +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include =20 #include "hwspinlock_internal.h" =20 @@ -860,5 +862,70 @@ struct hwspinlock *devm_hwspin_lock_request_specific(s= truct device *dev, } EXPORT_SYMBOL_GPL(devm_hwspin_lock_request_specific); =20 +#ifdef CONFIG_DEBUG_FS +static void *hwspin_lock_seq_start(struct seq_file *s, loff_t *ppos) +{ + unsigned long index =3D *ppos; + struct hwspinlock *hwlock; + + rcu_read_lock(); + hwlock =3D xa_find(&hwspinlocks, &index, ULONG_MAX, XA_PRESENT); + *ppos =3D index; + + return hwlock; +} + +static void *hwspin_lock_seq_next(struct seq_file *s, void *v, loff_t *ppo= s) +{ + /* Increase ppos here to avoid endless loops. Don't use xa_find_after() */ + unsigned long index =3D *ppos + 1; + struct hwspinlock *hwlock; + + hwlock =3D xa_find(&hwspinlocks, &index, ULONG_MAX, XA_PRESENT); + *ppos =3D index; + + return hwlock; +} + +static void hwspin_lock_seq_stop(struct seq_file *s, void *v) +{ + rcu_read_unlock(); +} + +static int hwspin_lock_seq_show(struct seq_file *s, void *v) +{ + struct hwspinlock *hwlock =3D v; + bool unused =3D xa_get_mark(&hwspinlocks, s->index, HWSPINLOCK_UNUSED); + + seq_printf(s, "%4llu:\t%s\t%s\n", s->index, unused ? "free" : "in use", + dev_name(hwlock->bank->dev)); + return 0; +} + +static const struct seq_operations hwspinlock_sops =3D { + .start =3D hwspin_lock_seq_start, + .next =3D hwspin_lock_seq_next, + .stop =3D hwspin_lock_seq_stop, + .show =3D hwspin_lock_seq_show, +}; +DEFINE_SEQ_ATTRIBUTE(hwspinlock); + +/* + * subsys_initcall() is used here but controllers may already have been + * registered earlier or will be later. The rationale is that debugfs is + * accessed only late, i.e. from userspace. So, files created here must ma= ke no + * assumptions about initcall ordering. + */ +static int __init hwspin_lock_init(void) +{ + struct dentry *hwspinlock_debugfs =3D debugfs_create_dir("hwspinlock", NU= LL); + + debugfs_create_file("hwspinlock_summary", 0444, hwspinlock_debugfs, + NULL, &hwspinlock_fops); + return 0; +} +subsys_initcall(hwspin_lock_init); +#endif /* DEBUG_FS */ + MODULE_DESCRIPTION("Hardware spinlock interface"); MODULE_AUTHOR("Ohad Ben-Cohen "); --=20 2.53.0 From nobody Fri Sep 25 12:34:04 2026 Received: from mail.zeus03.de (zeus03.de [194.117.254.33]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1B4048D88E for ; Sat, 12 Sep 2026 19:14:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.117.254.33 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; cv=none; b=q+mwiSmGhRItoKX9DnihODB4zf8IfyLb/+K8qPb+QUD43ZmVBHGVj+rFmcUouvsGHrh4CK4RS9pA2dm502VodO4w18l35x1U/ST/oVshdJtUYC95szDKkyyYFz2P7eVajxxMqpLG0Ooj8PZwBUO6wEAeSIiQ2aBLdCeWdbt+IKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789240497; c=relaxed/simple; bh=lDN34E1cSMytRNznJ9Oh9/pMmYog5phgJEbH2gZSCGg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qiYGUvvn1Nef7FOy1K0NUU+QdD3BMzH+MFjNey9mjxXtJQ1GaUVX4VzxsmvKpx0abXqS7wlOz0RpnYrMoSK/rgtGnNyIiLXjAn5Vfi/ZLkemn/AwZO9Tl1weo/e//jDwZa9F97ki7gNfoj995AMvK1vvztVr7utAhsoe8HfFMxg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com; spf=pass smtp.mailfrom=sang-engineering.com; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b=SiBdiW5H; arc=none smtp.client-ip=194.117.254.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sang-engineering.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sang-engineering.com header.i=@sang-engineering.com header.b="SiBdiW5H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= sang-engineering.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=k1; bh=qwhdoD0R8EFPgpLc6tXFj3KTuVR3dVvJTYzNRHyNGd0=; b=SiBdiW 5HKJAU3yOoa6uHGNN+5IUaAyB+Xf6KV7sZ04dKIgbxZNxHMoNZ1Cqqlz+vGn4G4R SZVLm29ziNrR19xIasiJwe0EP2boat0kmPxMPdSJtr/SHkHMEfYbGi8XY1y7tNmN CvBvrwomMx/RgDb/kVXYS9q47VOgIVgxL5nEMokCAOWzbR/Vlv5RLOexMwx42ZcN GukYF4OroNLRTg+cXhNPRhmmianAzpRZJ75pwe51EHv0Wsy+gNeEtK6F+bn5UsEi 2n6keQwwshy0+d/aB8CWNU5EsnZeiojwhoMlUNJZtby3ZQBXs23Qf24vW69qoxaB MtdepmPUNDnaRbFA== Received: (qmail 105282 invoked from network); 12 Sep 2026 21:14:51 +0200 Received: by mail.zeus03.de with ESMTPSA (TLS_AES_256_GCM_SHA384 encrypted, authenticated); 12 Sep 2026 21:14:51 +0200 X-UD-Smtp-Session: l3s3148p1@UJzsA05bwrYujnsz From: Wolfram Sang To: linux-renesas-soc@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Wolfram Sang , Bjorn Andersson , Baolin Wang , linux-remoteproc@vger.kernel.org Subject: [PATCH v7 3/3] hwspinlock: add low level debug capabilities Date: Sat, 12 Sep 2026 21:14:41 +0200 Message-ID: <20260912191442.99453-4-wsa+renesas@sang-engineering.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> References: <20260912191442.99453-1-wsa+renesas@sang-engineering.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" While developing the hwspinlock core or a device driver, it can be really helpful to change states from userspace. Add a debugsfs interface to request, free, lock, unlock, and test hwspinlocks. Because this is a potentially dangerous interface, it can only be enabled via source code. Similar to writeable attributes for clocks where this idea stems from. The created files take a single id for a single lock. To test all locks in a system, one could use this oneliner while in the debugfs directory: for l in $(cut -d: -f1 hwspinlock_summary); do echo $l > test_lock; done Signed-off-by: Wolfram Sang Reviewed-by: Geert Uytterhoeven --- drivers/hwspinlock/hwspinlock_core.c | 132 +++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwsp= inlock_core.c index 947b4d4fa153..6fb2670873a2 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -910,6 +910,120 @@ static const struct seq_operations hwspinlock_sops = =3D { }; DEFINE_SEQ_ATTRIBUTE(hwspinlock); =20 +/* + * This can be dangerous, therefore don't provide any real compile time + * configuration option for this feature. + * People who want to use this will need to modify the source code directl= y. + */ +#undef HWSPINLOCK_LOW_LEVEL_DEBUG +#ifdef HWSPINLOCK_LOW_LEVEL_DEBUG + +static int fops_request_set(void *data, u64 id) +{ + if (id > UINT_MAX) + return -EINVAL; + + return hwspin_lock_request_specific(id) ? 0 : -ENOENT; +} +DEFINE_DEBUGFS_ATTRIBUTE(fops_request, NULL, fops_request_set, "%llu\n"); + +static int fops_free_set(void *data, u64 id) +{ + struct hwspinlock *hwlock; + + if (id > UINT_MAX) + return -EINVAL; + + hwlock =3D xa_load(&hwspinlocks, id); + + return hwlock ? hwspin_lock_free(hwlock) : -ENOENT; +} +DEFINE_DEBUGFS_ATTRIBUTE(fops_free, NULL, fops_free_set, "%llu\n"); + +/* + * It is intentionally allowed to lock/free hwspinlocks which have not been + * requested before. Like error injection, these inconsistent states can be + * very useful for debugging. Be aware that you might need another properly + * requested lock on a provider to ensure that its clocks etc are enabled! + */ +static int fops_trylock_raw_set(void *data, u64 id) +{ + struct hwspinlock *hwlock; + + if (id > UINT_MAX) + return -EINVAL; + + hwlock =3D xa_load(&hwspinlocks, id); + + return hwlock ? hwspin_trylock_raw(hwlock) : -ENOENT; +} +DEFINE_DEBUGFS_ATTRIBUTE(fops_trylock_raw, NULL, fops_trylock_raw_set, "%l= lu\n"); + +static int fops_unlock_raw_set(void *data, u64 id) +{ + struct hwspinlock *hwlock; + + if (id > UINT_MAX) + return -EINVAL; + + hwlock =3D xa_load(&hwspinlocks, id); + if (hwlock) + hwspin_unlock_raw(hwlock); + + return 0; +} +DEFINE_DEBUGFS_ATTRIBUTE(fops_unlock_raw, NULL, fops_unlock_raw_set, "%llu= \n"); + +static int fops_test_set(void *data, u64 id) +{ + struct hwspinlock *hwlock; + int ret; + + if (id > UINT_MAX) + return -EINVAL; + + hwlock =3D hwspin_lock_request_specific(id); + if (!hwlock) + return -ENOENT; + + /* Try twice to see if unlocking was also successful */ + for (int i =3D 0; i < 2; i++) { + ret =3D hwspin_trylock_raw(hwlock); + if (ret) { + hwspin_lock_free(hwlock); + return ret; + } + + ret =3D hwspin_trylock_raw(hwlock); + if (ret !=3D -EBUSY) { + if (ret =3D=3D 0) + hwspin_unlock_raw(hwlock); + hwspin_lock_free(hwlock); + return -EACCES; + } + + hwspin_unlock_raw(hwlock); + } + + ret =3D hwspin_lock_free(hwlock); + if (ret) + return ret; + + pr_info("Successfully tested lock %llu\n", id); + return 0; +} +DEFINE_DEBUGFS_ATTRIBUTE(fops_test, NULL, fops_test_set, "%llu\n"); + +static void hwspin_lock_low_level_debug_init(struct dentry *rootdir) +{ + debugfs_create_file("request", 0200, rootdir, NULL, &fops_request); + debugfs_create_file("free", 0200, rootdir, NULL, &fops_free); + debugfs_create_file("lock", 0200, rootdir, NULL, &fops_trylock_raw); + debugfs_create_file("unlock", 0200, rootdir, NULL, &fops_unlock_raw); + debugfs_create_file("test_lock", 0200, rootdir, NULL, &fops_test); +} +#endif + /* * subsys_initcall() is used here but controllers may already have been * registered earlier or will be later. The rationale is that debugfs is @@ -922,6 +1036,24 @@ static int __init hwspin_lock_init(void) =20 debugfs_create_file("hwspinlock_summary", 0444, hwspinlock_debugfs, NULL, &hwspinlock_fops); + +#ifdef HWSPINLOCK_LOW_LEVEL_DEBUG + pr_warn("**********************************************************\n"); + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); + pr_warn("** **\n"); + pr_warn("** Low Level Debug for hwspinlocks is compiled in! **\n"); + pr_warn("** **\n"); + pr_warn("** This means safety, security, stability can be easily **\n"); + pr_warn("** compromised from userspace! **\n"); + pr_warn("** **\n"); + pr_warn("** If you see this message and you are not debugging **\n"); + pr_warn("** the kernel, report this immediately to your vendor! **\n"); + pr_warn("** **\n"); + pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); + pr_warn("**********************************************************\n"); + hwspin_lock_low_level_debug_init(hwspinlock_debugfs); +#endif + return 0; } subsys_initcall(hwspin_lock_init); --=20 2.53.0