From nobody Thu Oct 2 00:46:03 2025 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 DDE612E2667 for ; Thu, 25 Sep 2025 15:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812644; cv=none; b=oBT3xGNU+8+/Vhe2AjW8H+kTQPLdokV78KixYak605SpMI4SikzGqAmh2zxw1a1fpBDeFHt9MLdJ9Z8EZXLCdM/hfj3nQLycFp2YcFeFfdVjPb7zmQbkwo+9d4q5bgyvheVhVLdCBfox2QlZuotVS1HLbygvOrOAb+bRBLUC7Pg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812644; c=relaxed/simple; bh=iPtudmCoZ1Y2YRs+oxivEfgiwzAncbwAgskDeIoNQPQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R9V2b2Qtb6qR+xb0HwT2Jn6antIxW7HUrZCfhf0gg1I/LxAy515RJRXkifMLQfdptxATyrt0q0Qt3sxDSfRYmDsnzWak/cl+ehkBIerABCErZwnq1FJzTChvfynaLWb1WVfCrpgA1HIiQy2BFfUb/zuwFVMKaKVktoAGxJV56Cw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Pg23J2YQ; arc=none smtp.client-ip=95.215.58.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Pg23J2YQ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1758812640; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QmLfEMR0Vw9wPWJkVM3oKv7COMTxVJz60j3r9iGyw40=; b=Pg23J2YQzR/u3/d6RmJbd+JFQmaFrm1PUKwptCHKoxr9JCjx7OrMjRncrUGClJYFezae0p AvQbA+Eqp98MuIv8akkafKCvwmlYO3tm2ZyENDBVZx+wlys5VAMQjoKvG2IJkBMtJBc17O xpaHDoUn+W3H/tfbNKkXeAnfskccvjE= From: Sean Anderson To: Suzuki K Poulose , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: Leo Yan , Yeoreum Yun , Linu Cherian , Mike Leach , linux-kernel@vger.kernel.org, Alexander Shishkin , James Clark , Sean Anderson Subject: [PATCH v5 1/3] coresight: Fix fwnode leak in coresight_register error path Date: Thu, 25 Sep 2025 11:03:40 -0400 Message-Id: <20250925150342.1845615-2-sean.anderson@linux.dev> In-Reply-To: <20250925150342.1845615-1-sean.anderson@linux.dev> References: <20250925150342.1845615-1-sean.anderson@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" If registering the CPU map fails, we need to put the fwnode. free_percpu works when called with a NULL pointer, so just use coresight_device_release. Fixes: 5ad628a76176 ("coresight: Use per-sink trace ID maps for Perf sessio= ns") Signed-off-by: Sean Anderson Reviewed-by: Mike Leach --- (no changes since v4) Changes in v4: - New drivers/hwtracing/coresight/coresight-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtraci= ng/coresight/coresight-core.c index fa758cc21827..022c8384b98d 100644 --- a/drivers/hwtracing/coresight/coresight-core.c +++ b/drivers/hwtracing/coresight/coresight-core.c @@ -1352,7 +1352,7 @@ struct coresight_device *coresight_register(struct co= resight_desc *desc) raw_spin_lock_init(&csdev->perf_sink_id_map.lock); csdev->perf_sink_id_map.cpu_map =3D alloc_percpu(atomic_t); if (!csdev->perf_sink_id_map.cpu_map) { - kfree(csdev); + coresight_device_release(&csdev->dev); ret =3D -ENOMEM; goto err_out; } --=20 2.35.1.1320.gc452695387.dirty From nobody Thu Oct 2 00:46:03 2025 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 EEA232E284B for ; Thu, 25 Sep 2025 15:04:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812649; cv=none; b=DYtpEOwYPMoWzBHCt+r6Vc8ifcJ9QB6WmUF0RdmdK2ko7uYaG48dfVfWVxSZOeWQdCehwY6R0Qvn5CuS25JFNexVER/FNf8DknrVgxHHsg/mo1eF5dMK2oI6FLMLrLNeegLUkVtnnpugUvpXjsAkYcUXKEJEEcXuwS9zIAuvX4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812649; c=relaxed/simple; bh=tDau7Z+nk2acTzxWhsEJO3psQ296tGp1GHVngSyH9O4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SkQTgaB4WdSQ2QenEixxozQQ/bFMkWkZU+dvAIuM9K/52fYklyc8+jVzPPaA6z5B5nSfEfrmkW2y/41CWPDvCgcE5xFVJoactYPXJd36v++Mb0Rn4Vnff3WXV3lRpU3xN8rZMxKgOM7B7pGfKwLycYhR0kPHCzRuMJknOJqIJ3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=TEvzXx6e; arc=none smtp.client-ip=95.215.58.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="TEvzXx6e" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1758812641; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pQfOvvpVeHu2haioKIlLlNw0O7AmtYvMNB5o/K0IIVw=; b=TEvzXx6ei1B2c2+lW41m/X5MmbR4yRgiJMJajXSQOD3eyLHfhNLjUBheaWJ4BRRgKowzCF 6vtBucMjyQX2Wg6Okh2aoaWddHg6oh+NOENuVQ9+jXEAx8Y3kL68YL4gw7q5J1T9TZ2yeW P4BVhqE1ZN/ZscO/zpB7S39g2BLxpf4= From: Sean Anderson To: Suzuki K Poulose , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: Leo Yan , Yeoreum Yun , Linu Cherian , Mike Leach , linux-kernel@vger.kernel.org, Alexander Shishkin , James Clark , Sean Anderson Subject: [PATCH v5 2/3] coresight: Reorder coresight_device_release to match coresight_register Date: Thu, 25 Sep 2025 11:03:41 -0400 Message-Id: <20250925150342.1845615-3-sean.anderson@linux.dev> In-Reply-To: <20250925150342.1845615-1-sean.anderson@linux.dev> References: <20250925150342.1845615-1-sean.anderson@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" To make it easier to determine where to add new release actions, reorder the actions in coresight_device_release to be the reverse of coresight_register. Signed-off-by: Sean Anderson Reviewed-by: Mike Leach --- Changes in v5: - New drivers/hwtracing/coresight/coresight-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtraci= ng/coresight/coresight-core.c index 022c8384b98d..305b1773cfbe 100644 --- a/drivers/hwtracing/coresight/coresight-core.c +++ b/drivers/hwtracing/coresight/coresight-core.c @@ -1046,8 +1046,8 @@ static void coresight_device_release(struct device *d= ev) { struct coresight_device *csdev =3D to_coresight_device(dev); =20 - fwnode_handle_put(csdev->dev.fwnode); free_percpu(csdev->perf_sink_id_map.cpu_map); + fwnode_handle_put(csdev->dev.fwnode); kfree(csdev); } =20 --=20 2.35.1.1320.gc452695387.dirty From nobody Thu Oct 2 00:46:03 2025 Received: from out-172.mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (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 DCABD2E2665 for ; Thu, 25 Sep 2025 15:04:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812651; cv=none; b=uW6KnwQE1TcjWuyn0yAlHbxC9sIPZMyoQjS1vSBIVktgWmQimmmF7itdPjDvBnHNrGURUDoN5pbf+NigGnBprKZbD/LHecJ0S8vhVamw+pDJLQoE//HQXuYdSusSmnhoebJtFsrngVAcyy4j6+ypr8tB6QbRMovJv5H0kXzUPZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758812651; c=relaxed/simple; bh=3UPzGr2VB/rAfD5+8oPaVoN9X2KDA2+tATtdTBIfH+8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SUeKHjtwduaNNxmKjpSy04Co9irWiLDoCHpUqmeR5xUdhJRqr+hz1X8LuDcbBMu9XV8wfu16V4bc9gQSCRuArJXCjZWT/BOI9WsrA7FguLIAoA6qvibUzl/JG2lFtLi/8rxJOnINTWCejnQrdVS/dWK6y4SL6ZqcdwPEyLVkcGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JyqpvJj0; arc=none smtp.client-ip=95.215.58.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JyqpvJj0" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1758812644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rFsFtt/dYubWASJgvWs3g9b5WimRx2r0jDM0W9mimZ4=; b=JyqpvJj0isPclEJyeW7fLiPwKRipWqHOvgNoVwokn7yAbKMSE5TCY5PmT1etG8xI8ZCGSk RYaGfcnVN2eZsW4f7LlSsVQj5QpQSYhO0TAfEPPkUinvgfiB4SJskHiSCx4GhNjQ/IioWf PkNRCPYpEyuLxt1TcIj6/N83QfV+LfE= From: Sean Anderson To: Suzuki K Poulose , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: Leo Yan , Yeoreum Yun , Linu Cherian , Mike Leach , linux-kernel@vger.kernel.org, Alexander Shishkin , James Clark , Sean Anderson Subject: [PATCH v5 3/3] coresight: Fix possible deadlock in coresight_panic_cb Date: Thu, 25 Sep 2025 11:03:42 -0400 Message-Id: <20250925150342.1845615-4-sean.anderson@linux.dev> In-Reply-To: <20250925150342.1845615-1-sean.anderson@linux.dev> References: <20250925150342.1845615-1-sean.anderson@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Panics can occur at any time, so taking locks may cause a deadlock (such as if the panicking CPU held the lock). coresight_panic_cb uses bus_for_each_dev, but that calls bus_to_subsys which takes bus_kset->list_lock. Instead of registering a single panic notifier and iterating over coresight devices, register a panic notifier for each coresight device that requires it (letting the atomic notifier list handle iteration). atomic_notifier_chain_unregister will just return -ENOENT if a notifier block isn't on the list, so it's safe to call when we haven't registered a notifier. Fixes: 46006ceb5d02 ("coresight: core: Add provision for panic callbacks") Signed-off-by: Sean Anderson Reviewed-by: Leo Yan --- Changes in v5: - Check csdev mode before calling sync() Changes in v4: - Move the panic notifier into csdev and restore the panic sync API Changes in v3: - Rewrite patch to remove the panic sync API entirely Changes in v2: - Add a comment describing csdev_lock/list - Consolidate list removal in coresight_device_release drivers/hwtracing/coresight/coresight-core.c | 67 ++++++++------------ include/linux/coresight.h | 2 + 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-core.c b/drivers/hwtraci= ng/coresight/coresight-core.c index 305b1773cfbe..01303348fc0e 100644 --- a/drivers/hwtracing/coresight/coresight-core.c +++ b/drivers/hwtracing/coresight/coresight-core.c @@ -1046,6 +1046,9 @@ static void coresight_device_release(struct device *d= ev) { struct coresight_device *csdev =3D to_coresight_device(dev); =20 + if (panic_ops(csdev)) + atomic_notifier_chain_unregister(&panic_notifier_list, + &csdev->panic_notifier); free_percpu(csdev->perf_sink_id_map.cpu_map); fwnode_handle_put(csdev->dev.fwnode); kfree(csdev); @@ -1315,6 +1318,18 @@ void coresight_release_platform_data(struct coresigh= t_device *csdev, coresight_remove_conns_sysfs_group(csdev); } =20 +static int coresight_panic_notifier(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct coresight_device *csdev =3D + container_of(nb, struct coresight_device, panic_notifier); + int mode =3D coresight_get_mode(csdev); + + if (mode =3D=3D CS_MODE_SYSFS || mode =3D=3D CS_MODE_PERF) + panic_ops(csdev)->sync(csdev); + return NOTIFY_DONE; +} + struct coresight_device *coresight_register(struct coresight_desc *desc) { int ret; @@ -1357,6 +1372,17 @@ struct coresight_device *coresight_register(struct c= oresight_desc *desc) goto err_out; } } + + if (panic_ops(csdev)) { + csdev->panic_notifier.notifier_call =3D coresight_panic_notifier; + ret =3D atomic_notifier_chain_register(&panic_notifier_list, + &csdev->panic_notifier); + if (ret) { + coresight_device_release(&csdev->dev); + goto err_out; + } + } + /* * Make sure the device registration and the connection fixup * are synchronised, so that we don't see uninitialised devices @@ -1563,36 +1589,6 @@ const struct bus_type coresight_bustype =3D { .name =3D "coresight", }; =20 -static int coresight_panic_sync(struct device *dev, void *data) -{ - int mode; - struct coresight_device *csdev; - - /* Run through panic sync handlers for all enabled devices */ - csdev =3D container_of(dev, struct coresight_device, dev); - mode =3D coresight_get_mode(csdev); - - if ((mode =3D=3D CS_MODE_SYSFS) || (mode =3D=3D CS_MODE_PERF)) { - if (panic_ops(csdev)) - panic_ops(csdev)->sync(csdev); - } - - return 0; -} - -static int coresight_panic_cb(struct notifier_block *self, - unsigned long v, void *p) -{ - bus_for_each_dev(&coresight_bustype, NULL, NULL, - coresight_panic_sync); - - return 0; -} - -static struct notifier_block coresight_notifier =3D { - .notifier_call =3D coresight_panic_cb, -}; - static int __init coresight_init(void) { int ret; @@ -1605,20 +1601,11 @@ static int __init coresight_init(void) if (ret) goto exit_bus_unregister; =20 - /* Register function to be called for panic */ - ret =3D atomic_notifier_chain_register(&panic_notifier_list, - &coresight_notifier); - if (ret) - goto exit_perf; - /* initialise the coresight syscfg API */ ret =3D cscfg_init(); if (!ret) return 0; =20 - atomic_notifier_chain_unregister(&panic_notifier_list, - &coresight_notifier); -exit_perf: etm_perf_exit(); exit_bus_unregister: bus_unregister(&coresight_bustype); @@ -1628,8 +1615,6 @@ static int __init coresight_init(void) static void __exit coresight_exit(void) { cscfg_exit(); - atomic_notifier_chain_unregister(&panic_notifier_list, - &coresight_notifier); etm_perf_exit(); bus_unregister(&coresight_bustype); } diff --git a/include/linux/coresight.h b/include/linux/coresight.h index 4ac65c68bbf4..a7aaf9d3d01d 100644 --- a/include/linux/coresight.h +++ b/include/linux/coresight.h @@ -280,6 +280,7 @@ struct coresight_trace_id_map { * @config_csdev_list: List of system configurations added to the device. * @cscfg_csdev_lock: Protect the lists of configurations and features. * @active_cscfg_ctxt: Context information for current active system conf= iguration. + * @panic_notifier: Notifier block used to clean up during a panic */ struct coresight_device { struct coresight_platform_data *pdata; @@ -304,6 +305,7 @@ struct coresight_device { struct list_head config_csdev_list; raw_spinlock_t cscfg_csdev_lock; void *active_cscfg_ctxt; + struct notifier_block panic_notifier; }; =20 /* --=20 2.35.1.1320.gc452695387.dirty