From nobody Wed Dec 17 00:02:04 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F4FD253933 for ; Wed, 14 May 2025 16:20:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239605; cv=none; b=eu1iq2/kLeBGdQOjmLA4aNsn4N+QnOFrqn0bVrT/gDhzvKXrjKnecxBBaP3mFTusupktx3VOYhTsK75tnvGpDdWpZjLlZ+kI1GYeWPYcwon94BEfrJ/Idsrhk5oY1rgyqJUmwvF91m0ikabWMh7zyXre+NTprJe0NyLh+d/YPLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239605; c=relaxed/simple; bh=eCKQAuKZBd/ahYdfc7Bii5ANvee75EpDuuQz0FuOxyc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZDVb9k4TzoL2xqC/AQVEYAysEAN2quS2mo6IomT1h68gMsyHf++ME+xk6GwXB2eZMcEKNzVZYnIANE6fUrdBB7yxLDey6oYNaz5ltX6AyUGd/YYFukFifpHvLMT8ImyVomzh2cEOew1jVNoRFpO2PtkmHEzFwi9SeuMOr9yMI3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BAEE915A1; Wed, 14 May 2025 09:19:50 -0700 (PDT) Received: from e129823.cambridge.arm.com (e129823.arm.com [10.1.197.6]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9CB8D3F63F; Wed, 14 May 2025 09:20:00 -0700 (PDT) From: Yeoreum Yun To: suzuki.poulose@arm.com, mike.leach@linaro.org, james.clark@linaro.org, alexander.shishkin@linux.intel.com, leo.yan@arm.com Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Yeoreum Yun Subject: [PATCH v6 1/3] coresight/etm4: fix missing disable active config Date: Wed, 14 May 2025 17:19:49 +0100 Message-Id: <20250514161951.3427590-2-yeoreum.yun@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250514161951.3427590-1-yeoreum.yun@arm.com> References: <20250514161951.3427590-1-yeoreum.yun@arm.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" When etm4 device is disabled via sysfs, it should disable its active count. Fixes: 7ebd0ec6cf94 ("coresight: configfs: Allow configfs to activate confi= guration") Signed-off-by: Yeoreum Yun Reviewed-by: Leo Yan --- drivers/hwtracing/coresight/coresight-etm4x-core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/h= wtracing/coresight/coresight-etm4x-core.c index 2b8f10463840..f008f87f0801 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x-core.c +++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c @@ -1020,6 +1020,9 @@ static void etm4_disable_sysfs(struct coresight_devic= e *csdev) smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1); =20 raw_spin_unlock(&drvdata->spinlock); + + cscfg_csdev_disable_active_config(csdev); + cpus_read_unlock(); =20 /* --=20 LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7} From nobody Wed Dec 17 00:02:04 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E0D827A44E for ; Wed, 14 May 2025 16:20:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239606; cv=none; b=oP+oE7BL5ZV7Sw/G5wMaf98umJQWaVGAiNqqdkaDeGV6cvmoQMT104ruozISWWjKOkjEi7NREC3cH9n+4VRlMRCVc3YcdnK+w9Q1kum+5XEA5ammjGNEIPJSCo6VFTD7e6pt4+6MB6+Jd1IHfv5LrWbf6gnO7lN6+SD0IPziTz0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239606; c=relaxed/simple; bh=GjB0bfqhUj1DgxPVr876GZC1RBk/GArqU8FS0b8NG7w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Wz0sKU+AV54FQe42zXI1L4I3Tl+jwBFaIN6J9SQr3VmXG7RPWHAnxtyQ9enizcTsRWW6jicyLfiKFlc1VwcXe8l4m8XZsbdKut3DMmp905EpGWQFVI/ofugB91gX4mZSP1IJECTeUaefOJ7AQKDQX/BvQQ72p8pWJKhAGuXxZhY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 66E5A150C; Wed, 14 May 2025 09:19:52 -0700 (PDT) Received: from e129823.cambridge.arm.com (e129823.arm.com [10.1.197.6]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 5191A3F63F; Wed, 14 May 2025 09:20:02 -0700 (PDT) From: Yeoreum Yun To: suzuki.poulose@arm.com, mike.leach@linaro.org, james.clark@linaro.org, alexander.shishkin@linux.intel.com, leo.yan@arm.com Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Yeoreum Yun Subject: [PATCH v6 2/3] coresight: holding cscfg_csdev_lock while removing cscfg from csdev Date: Wed, 14 May 2025 17:19:50 +0100 Message-Id: <20250514161951.3427590-3-yeoreum.yun@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250514161951.3427590-1-yeoreum.yun@arm.com> References: <20250514161951.3427590-1-yeoreum.yun@arm.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" There'll be possible race scenario for coresight config: CPU0 CPU1 (perf enable) load module cscfg_load_config_sets() activate config. // sysfs (sys_active_cnt =3D=3D 1) ... cscfg_csdev_enable_active_config() lock(csdev->cscfg_csdev_lock) deactivate config // sysfs (sys_activec_cnt =3D=3D 0) cscfg_unload_config_sets() cscfg_remove_owned_csdev_conf= igs() // here load config activate by CPU1 unlock(csdev->cscfg_csdev_lock) iterating config_csdev_list could be raced with config_csdev_list's entry delete. To resolve this race , hold csdev->cscfg_csdev_lock() while cscfg_remove_owned_csdev_configs() Fixes: 02bd588e12df ("coresight: configuration: Update API to permit dynami= c load/unload") Signed-off-by: Yeoreum Yun Reviewed-by: Leo Yan --- drivers/hwtracing/coresight/coresight-syscfg.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtra= cing/coresight/coresight-syscfg.c index a70c1454b410..23017612f2ea 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.c +++ b/drivers/hwtracing/coresight/coresight-syscfg.c @@ -395,6 +395,8 @@ static void cscfg_remove_owned_csdev_configs(struct cor= esight_device *csdev, voi if (list_empty(&csdev->config_csdev_list)) return; =20 + guard(raw_spinlock_irqsave)(&csdev->cscfg_csdev_lock); + list_for_each_entry_safe(config_csdev, tmp, &csdev->config_csdev_list, no= de) { if (config_csdev->config_desc->load_owner =3D=3D load_owner) list_del(&config_csdev->node); --=20 LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7} From nobody Wed Dec 17 00:02:04 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9B7027BF76 for ; Wed, 14 May 2025 16:20:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239607; cv=none; b=uFID9/ynbxU743lgoBTFp7hmV1/r+lEwHJS2v60FoHdzLyq0KRNJLS1mhmk1XSqELQnAjDEZSczZTKU+mAaqxMuKWpQk47nczg8U6aCvCn90+4Mztie4Wg5d1mQMA4StatrED5G+bd8ZJRB2jkJNScWvXauZAAA3stIPq2MlXIs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747239607; c=relaxed/simple; bh=2MGwrV2yYFkDF75pfs4mDP3czfjmK1CkpRGMGg++Bjc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q3xi9GVCy86nxgmelzTiHqlQ2lkYQaY26Z1WpS1TvehKU2ilCGE5V7mlRBa4U+E7tZXLrJ9K31IukgPGtlZACNIi4ftHphGAV4Vq44GHPe3bFoNiAbp8B4XJprP/C2hadKOxAlZ/+JdXQUuKMH6P3RfPyyaVpakYkbaojb8eHKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 12E6415A1; Wed, 14 May 2025 09:19:54 -0700 (PDT) Received: from e129823.cambridge.arm.com (e129823.arm.com [10.1.197.6]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id F1C863F63F; Wed, 14 May 2025 09:20:03 -0700 (PDT) From: Yeoreum Yun To: suzuki.poulose@arm.com, mike.leach@linaro.org, james.clark@linaro.org, alexander.shishkin@linux.intel.com, leo.yan@arm.com Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Yeoreum Yun Subject: [PATCH v6 3/3] coresight: prevent deactivate active config while enabling the config Date: Wed, 14 May 2025 17:19:51 +0100 Message-Id: <20250514161951.3427590-4-yeoreum.yun@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250514161951.3427590-1-yeoreum.yun@arm.com> References: <20250514161951.3427590-1-yeoreum.yun@arm.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 enable active config via cscfg_csdev_enable_active_config(), active config could be deactivated via configfs' sysfs interface. This could make UAF issue in below scenario: CPU0 CPU1 (sysfs enable) load module cscfg_load_config_sets() activate config. // sysfs (sys_active_cnt =3D=3D 1) ... cscfg_csdev_enable_active_config() lock(csdev->cscfg_csdev_lock) // here load config activate by CPU1 unlock(csdev->cscfg_csdev_lock) deactivate config // sysfs (sys_activec_cnt =3D=3D 0) cscfg_unload_config_sets() unload module // access to config_desc which freed // while unloading module. cscfg_csdev_enable_config To address this, use cscfg_config_desc's active_cnt as a reference count which will be holded when - activate the config. - enable the activated config. and put the module reference when config_active_cnt =3D=3D 0. Fixes: f8cce2ff3c04 ("coresight: syscfg: Add API to activate and enable con= figurations") Signed-off-by: Yeoreum Yun Reviewed-by: Leo Yan --- .../hwtracing/coresight/coresight-config.h | 2 +- .../hwtracing/coresight/coresight-syscfg.c | 49 +++++++++++++------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-config.h b/drivers/hwtra= cing/coresight/coresight-config.h index b9ebc9fcfb7f..90fd937d3bd8 100644 --- a/drivers/hwtracing/coresight/coresight-config.h +++ b/drivers/hwtracing/coresight/coresight-config.h @@ -228,7 +228,7 @@ struct cscfg_feature_csdev { * @feats_csdev:references to the device features to enable. */ struct cscfg_config_csdev { - const struct cscfg_config_desc *config_desc; + struct cscfg_config_desc *config_desc; struct coresight_device *csdev; bool enabled; struct list_head node; diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtra= cing/coresight/coresight-syscfg.c index 23017612f2ea..83dad24e0116 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg.c +++ b/drivers/hwtracing/coresight/coresight-syscfg.c @@ -869,6 +869,25 @@ void cscfg_csdev_reset_feats(struct coresight_device *= csdev) } EXPORT_SYMBOL_GPL(cscfg_csdev_reset_feats); =20 +static bool cscfg_config_desc_get(struct cscfg_config_desc *config_desc) +{ + if (!atomic_fetch_inc(&config_desc->active_cnt)) { + /* must ensure that config cannot be unloaded in use */ + if (unlikely(cscfg_owner_get(config_desc->load_owner))) { + atomic_dec(&config_desc->active_cnt); + return false; + } + } + + return true; +} + +static void cscfg_config_desc_put(struct cscfg_config_desc *config_desc) +{ + if (!atomic_dec_return(&config_desc->active_cnt)) + cscfg_owner_put(config_desc->load_owner); +} + /* * This activate configuration for either perf or sysfs. Perf can have mul= tiple * active configs, selected per event, sysfs is limited to one. @@ -892,22 +911,17 @@ static int _cscfg_activate_config(unsigned long cfg_h= ash) if (config_desc->available =3D=3D false) return -EBUSY; =20 - /* must ensure that config cannot be unloaded in use */ - err =3D cscfg_owner_get(config_desc->load_owner); - if (err) + if (!cscfg_config_desc_get(config_desc)) { + err =3D -EINVAL; break; + } + /* * increment the global active count - control changes to * active configurations */ atomic_inc(&cscfg_mgr->sys_active_cnt); =20 - /* - * mark the descriptor as active so enable config on a - * device instance will use it - */ - atomic_inc(&config_desc->active_cnt); - err =3D 0; dev_dbg(cscfg_device(), "Activate config %s.\n", config_desc->name); break; @@ -922,9 +936,8 @@ static void _cscfg_deactivate_config(unsigned long cfg_= hash) =20 list_for_each_entry(config_desc, &cscfg_mgr->config_desc_list, item) { if ((unsigned long)config_desc->event_ea->var =3D=3D cfg_hash) { - atomic_dec(&config_desc->active_cnt); atomic_dec(&cscfg_mgr->sys_active_cnt); - cscfg_owner_put(config_desc->load_owner); + cscfg_config_desc_put(config_desc); dev_dbg(cscfg_device(), "Deactivate config %s.\n", config_desc->name); break; } @@ -1049,7 +1062,7 @@ int cscfg_csdev_enable_active_config(struct coresight= _device *csdev, unsigned long cfg_hash, int preset) { struct cscfg_config_csdev *config_csdev_active =3D NULL, *config_csdev_it= em; - const struct cscfg_config_desc *config_desc; + struct cscfg_config_desc *config_desc; unsigned long flags; int err =3D 0; =20 @@ -1064,8 +1077,8 @@ int cscfg_csdev_enable_active_config(struct coresight= _device *csdev, raw_spin_lock_irqsave(&csdev->cscfg_csdev_lock, flags); list_for_each_entry(config_csdev_item, &csdev->config_csdev_list, node) { config_desc =3D config_csdev_item->config_desc; - if ((atomic_read(&config_desc->active_cnt)) && - ((unsigned long)config_desc->event_ea->var =3D=3D cfg_hash)) { + if (((unsigned long)config_desc->event_ea->var =3D=3D cfg_hash) && + cscfg_config_desc_get(config_desc)) { config_csdev_active =3D config_csdev_item; csdev->active_cscfg_ctxt =3D (void *)config_csdev_active; break; @@ -1099,7 +1112,11 @@ int cscfg_csdev_enable_active_config(struct coresigh= t_device *csdev, err =3D -EBUSY; raw_spin_unlock_irqrestore(&csdev->cscfg_csdev_lock, flags); } + + if (err) + cscfg_config_desc_put(config_desc); } + return err; } EXPORT_SYMBOL_GPL(cscfg_csdev_enable_active_config); @@ -1138,8 +1155,10 @@ void cscfg_csdev_disable_active_config(struct coresi= ght_device *csdev) raw_spin_unlock_irqrestore(&csdev->cscfg_csdev_lock, flags); =20 /* true if there was an enabled active config */ - if (config_csdev) + if (config_csdev) { cscfg_csdev_disable_config(config_csdev); + cscfg_config_desc_put(config_csdev->config_desc); + } } EXPORT_SYMBOL_GPL(cscfg_csdev_disable_active_config); =20 --=20 LEVI:{C3F47F37-75D8-414A-A8BA-3980EC8A46D7}