Message ID | 20231114133346.30489-1-hejunhao3@huawei.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp1860312vqg; Tue, 14 Nov 2023 05:34:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNszeZsoL1sisbvWXp4IxYu6bFG2VAnzswJRNE786PUCRfdwncWgLNlrtpv10S62bQvQLR X-Received: by 2002:a17:903:1108:b0:1cc:50f6:7fc1 with SMTP id n8-20020a170903110800b001cc50f67fc1mr2584484plh.56.1699968867961; Tue, 14 Nov 2023 05:34:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699968867; cv=none; d=google.com; s=arc-20160816; b=qY7UmkTsoJQRCJZjit8PkTn0LJZkDnfEL0d41gmra1k9Yc2jIOu0xpgwxwJ7YhCVj7 /lI/ndkAD4VYBTN0WH4crQKOeHY6oUfyITdp5/AfaQKUcnHZedVVwrkuXXkmJj67janB Y3o40KcNB4s+2r06jzuPBY6w2t/G5yG5oStBQLWuo0UA2UaCWc+pzST1v8H3f7W2dJnD INyPm+lUwQV46HXx29lunYTbVsjr3NLBAGjvfj+90jZgwkw4MiXswXG6tjZ6F9tJwQCP Jm99Opa76IWhVUqrGpyxWa+YYMHUCfi9xdRHJrBvYDqvBJ+5UXA7YG30QrfPYObWQBXi m7bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=WR6X7xQdy3O0Yxu0QL6Rc0awwFTf2wFZjDCHp6h/lk0=; fh=UHVmWfYpaOpxa5MpZWIHTe5DQ4x7tAMNH9bJWvQ5U9A=; b=V97F9FMZlYZjbsZ4+fs4sTrSNgzW6CHJp1O8ICwBtARM6i5rR+YResJsRiv1JexMvd KRnQeSYg30LoIzJfhTn923FYBcpKSr/1jmLJn2rihMPCsAo/QiM1iPwsuoZuA5/TkRHx cKb2BwbMJr6ZyxCvzZpSZIEcsgPzjIwYsyty0ffFrHmor4qAL4nxO6HAZr+T8fzxa4ot XHAYrtBsHsiDSyplQ6M5N5YT+QtIoaCNM/ur/dHUi0VLSZpsS0pe1mytu3HyIY5Urxpx GH2+7i03EKx/f+CBvTuk/lly9ga+MWXi61/oyaCFagWj/n3wuQpLl860mxG9ZNy4lBVf qe+g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id w13-20020a170902d3cd00b001cc7e43f6f2si7510996plb.310.2023.11.14.05.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 05:34:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id F10BA80A9B25; Tue, 14 Nov 2023 05:34:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233262AbjKNNdm (ORCPT <rfc822;lhua1029@gmail.com> + 29 others); Tue, 14 Nov 2023 08:33:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230229AbjKNNdl (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 14 Nov 2023 08:33:41 -0500 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B32E1B9 for <linux-kernel@vger.kernel.org>; Tue, 14 Nov 2023 05:33:37 -0800 (PST) Received: from dggpeml500002.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4SV6c84nrcz1P7Vg; Tue, 14 Nov 2023 21:30:16 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by dggpeml500002.china.huawei.com (7.185.36.158) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Tue, 14 Nov 2023 21:33:35 +0800 From: Junhao He <hejunhao3@huawei.com> To: <suzuki.poulose@arm.com>, <james.clark@arm.com> CC: <coresight@lists.linaro.org>, <linux-arm-kernel@lists.infradead.org>, <linux-kernel@vger.kernel.org>, <linuxarm@huawei.com>, <jonathan.cameron@huawei.com>, <yangyicong@huawei.com>, <prime.zeng@hisilicon.com>, <hejunhao3@huawei.com>, <u.kleine-koenig@pengutronix.de> Subject: [PATCH v3 0/4] Fixed some issues and cleanup of ultrasoc-smb Date: Tue, 14 Nov 2023 21:33:42 +0800 Message-ID: <20231114133346.30489-1-hejunhao3@huawei.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500002.china.huawei.com (7.185.36.158) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 14 Nov 2023 05:34:12 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782546555648257711 X-GMAIL-MSGID: 1782546555648257711 |
Series |
Fixed some issues and cleanup of ultrasoc-smb
|
|
Message
hejunhao
Nov. 14, 2023, 1:33 p.m. UTC
Fix the Three issues listed below and use guards to cleanup a) Fixed the BUG of atomic-sleep b) Fixed uninitialized before use buf_hw_base c) Fixed use unreset SMB buffer Changes since V2: * Ignore the return value of smb_config_inport() Link to V2: https://lore.kernel.org/lkml/20231021083822.18239-1-hejunhao3@huawei.com/ Changes since V1: * Add comment for remove lock from smb_read() * Move reset buffer to before register sink * Remove patch "simplify the code for check to_copy valid" * Add two new patches Link to V1: https://lore.kernel.org/lkml/20231012094706.21565-1-hejunhao3@huawei.com/ Junhao He (4): coresight: ultrasoc-smb: Fix sleep while close preempt in enable_smb coresight: ultrasoc-smb: Config SMB buffer before register sink coresight: ultrasoc-smb: Fix uninitialized before use buf_hw_base coresight: ultrasoc-smb: Use guards to cleanup drivers/hwtracing/coresight/ultrasoc-smb.c | 108 +++++++-------------- drivers/hwtracing/coresight/ultrasoc-smb.h | 6 +- 2 files changed, 38 insertions(+), 76 deletions(-)
Comments
On 14/11/2023 13:33, Junhao He wrote: > When we to enable the SMB by perf, the perf sched will call perf_ctx_lock() > to close system preempt in event_function_call(). But SMB::enable_smb() use > mutex to lock the critical section, which may sleep. > > BUG: sleeping function called from invalid context at kernel/locking/mutex.c:580 > in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 153023, name: perf > preempt_count: 2, expected: 0 > RCU nest depth: 0, expected: 0 > INFO: lockdep is turned off. > irq event stamp: 0 > hardirqs last enabled at (0): [<0000000000000000>] 0x0 > hardirqs last disabled at (0): [<ffffa2983f5c5f40>] copy_process+0xae8/0x2b48 > softirqs last enabled at (0): [<ffffa2983f5c5f40>] copy_process+0xae8/0x2b48 > softirqs last disabled at (0): [<0000000000000000>] 0x0 > CPU: 2 PID: 153023 Comm: perf Kdump: loaded Tainted: G W O 6.5.0-rc4+ #1 > > Call trace: > ... > __mutex_lock+0xbc/0xa70 > mutex_lock_nested+0x34/0x48 > smb_update_buffer+0x58/0x360 [ultrasoc_smb] > etm_event_stop+0x204/0x2d8 [coresight] > etm_event_del+0x1c/0x30 [coresight] > event_sched_out+0x17c/0x3b8 > group_sched_out.part.0+0x5c/0x208 > __perf_event_disable+0x15c/0x210 > event_function+0xe0/0x230 > remote_function+0xb4/0xe8 > generic_exec_single+0x160/0x268 > smp_call_function_single+0x20c/0x2a0 > event_function_call+0x20c/0x220 > _perf_event_disable+0x5c/0x90 > perf_event_for_each_child+0x58/0xc0 > _perf_ioctl+0x34c/0x1250 > perf_ioctl+0x64/0x98 > ... > > Use spinlock to replace mutex to control driver data access to one at a > time. The function copy_to_user() may sleep, it cannot be in a spinlock > context, so we can't simply replace it in smb_read(). But we can ensure > that only one user gets the SMB device fd by smb_open(), so remove the > locks from smb_read() and buffer synchronization is guaranteed by the user. > > Fixes: 06f5c2926aaa ("drivers/coresight: Add UltraSoc System Memory Buffer driver") > Signed-off-by: Junhao He <hejunhao3@huawei.com> Reviewed-by: James Clark <james.clark@arm.com> > --- > drivers/hwtracing/coresight/ultrasoc-smb.c | 35 +++++++++------------- > drivers/hwtracing/coresight/ultrasoc-smb.h | 6 ++-- > 2 files changed, 17 insertions(+), 24 deletions(-) > > diff --git a/drivers/hwtracing/coresight/ultrasoc-smb.c b/drivers/hwtracing/coresight/ultrasoc-smb.c > index e9a32a97fbee..0a0fe9fcc57f 100644 > --- a/drivers/hwtracing/coresight/ultrasoc-smb.c > +++ b/drivers/hwtracing/coresight/ultrasoc-smb.c > @@ -99,7 +99,7 @@ static int smb_open(struct inode *inode, struct file *file) > struct smb_drv_data, miscdev); > int ret = 0; > > - mutex_lock(&drvdata->mutex); > + spin_lock(&drvdata->spinlock); > > if (drvdata->reading) { > ret = -EBUSY; > @@ -115,7 +115,7 @@ static int smb_open(struct inode *inode, struct file *file) > > drvdata->reading = true; > out: > - mutex_unlock(&drvdata->mutex); > + spin_unlock(&drvdata->spinlock); > > return ret; > } > @@ -132,10 +132,8 @@ static ssize_t smb_read(struct file *file, char __user *data, size_t len, > if (!len) > return 0; > > - mutex_lock(&drvdata->mutex); > - > if (!sdb->data_size) > - goto out; > + return 0; > > to_copy = min(sdb->data_size, len); > > @@ -145,20 +143,15 @@ static ssize_t smb_read(struct file *file, char __user *data, size_t len, > > if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) { > dev_dbg(dev, "Failed to copy data to user\n"); > - to_copy = -EFAULT; > - goto out; > + return -EFAULT; > } > > *ppos += to_copy; > - > smb_update_read_ptr(drvdata, to_copy); > - > - dev_dbg(dev, "%zu bytes copied\n", to_copy); > -out: > if (!sdb->data_size) > smb_reset_buffer(drvdata); > - mutex_unlock(&drvdata->mutex); > > + dev_dbg(dev, "%zu bytes copied\n", to_copy); > return to_copy; > } > > @@ -167,9 +160,9 @@ static int smb_release(struct inode *inode, struct file *file) > struct smb_drv_data *drvdata = container_of(file->private_data, > struct smb_drv_data, miscdev); > > - mutex_lock(&drvdata->mutex); > + spin_lock(&drvdata->spinlock); > drvdata->reading = false; > - mutex_unlock(&drvdata->mutex); > + spin_unlock(&drvdata->spinlock); > > return 0; > } > @@ -262,7 +255,7 @@ static int smb_enable(struct coresight_device *csdev, enum cs_mode mode, > struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); > int ret = 0; > > - mutex_lock(&drvdata->mutex); > + spin_lock(&drvdata->spinlock); > > /* Do nothing, the trace data is reading by other interface now */ > if (drvdata->reading) { > @@ -294,7 +287,7 @@ static int smb_enable(struct coresight_device *csdev, enum cs_mode mode, > > dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n"); > out: > - mutex_unlock(&drvdata->mutex); > + spin_unlock(&drvdata->spinlock); > > return ret; > } > @@ -304,7 +297,7 @@ static int smb_disable(struct coresight_device *csdev) > struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent); > int ret = 0; > > - mutex_lock(&drvdata->mutex); > + spin_lock(&drvdata->spinlock); > > if (drvdata->reading) { > ret = -EBUSY; > @@ -327,7 +320,7 @@ static int smb_disable(struct coresight_device *csdev) > > dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n"); > out: > - mutex_unlock(&drvdata->mutex); > + spin_unlock(&drvdata->spinlock); > > return ret; > } > @@ -408,7 +401,7 @@ static unsigned long smb_update_buffer(struct coresight_device *csdev, > if (!buf) > return 0; > > - mutex_lock(&drvdata->mutex); > + spin_lock(&drvdata->spinlock); > > /* Don't do anything if another tracer is using this sink. */ > if (atomic_read(&csdev->refcnt) != 1) > @@ -432,7 +425,7 @@ static unsigned long smb_update_buffer(struct coresight_device *csdev, > if (!buf->snapshot && lost) > perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED); > out: > - mutex_unlock(&drvdata->mutex); > + spin_unlock(&drvdata->spinlock); > > return data_size; > } > @@ -590,7 +583,7 @@ static int smb_probe(struct platform_device *pdev) > return ret; > } > > - mutex_init(&drvdata->mutex); > + spin_lock_init(&drvdata->spinlock); > drvdata->pid = -1; > > ret = smb_register_sink(pdev, drvdata); > diff --git a/drivers/hwtracing/coresight/ultrasoc-smb.h b/drivers/hwtracing/coresight/ultrasoc-smb.h > index d2e14e8d2c8a..82a44c14a882 100644 > --- a/drivers/hwtracing/coresight/ultrasoc-smb.h > +++ b/drivers/hwtracing/coresight/ultrasoc-smb.h > @@ -8,7 +8,7 @@ > #define _ULTRASOC_SMB_H > > #include <linux/miscdevice.h> > -#include <linux/mutex.h> > +#include <linux/spinlock.h> > > /* Offset of SMB global registers */ > #define SMB_GLB_CFG_REG 0x00 > @@ -105,7 +105,7 @@ struct smb_data_buffer { > * @csdev: Component vitals needed by the framework. > * @sdb: Data buffer for SMB. > * @miscdev: Specifics to handle "/dev/xyz.smb" entry. > - * @mutex: Control data access to one at a time. > + * @spinlock: Control data access to one at a time. > * @reading: Synchronise user space access to SMB buffer. > * @pid: Process ID of the process being monitored by the > * session that is using this component. > @@ -116,7 +116,7 @@ struct smb_drv_data { > struct coresight_device *csdev; > struct smb_data_buffer sdb; > struct miscdevice miscdev; > - struct mutex mutex; > + spinlock_t spinlock; > bool reading; > pid_t pid; > enum cs_mode mode;
On Tue, 14 Nov 2023 21:33:42 +0800, Junhao He wrote: > Fix the Three issues listed below and use guards to cleanup > a) Fixed the BUG of atomic-sleep > b) Fixed uninitialized before use buf_hw_base > c) Fixed use unreset SMB buffer > > Changes since V2: > * Ignore the return value of smb_config_inport() > > [...] Applied, first 3 patches to fixes [1/4] coresight: ultrasoc-smb: Fix sleep while close preempt in enable_smb https://git.kernel.org/coresight/c/b8411287aef4 [2/4] coresight: ultrasoc-smb: Config SMB buffer before register sink https://git.kernel.org/coresight/c/830a7f54db10 [3/4] coresight: ultrasoc-smb: Fix uninitialized before use buf_hw_base https://git.kernel.org/coresight/c/862c135bde8b And the last one to next branch [4/4] coresight: ultrasoc-smb: Use guards to cleanup https://git.kernel.org/coresight/c/60e5f23dc5d6 Best regards,