Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp320667rdb; Thu, 19 Oct 2023 05:45:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBQ3NkfQEOMF2leEiny1VjnjMuJRXnMbe7RME6HvLD5nDDwjnlTsHh94m8YOH4y33kR4+W X-Received: by 2002:a05:6a00:938c:b0:68b:e29c:b69 with SMTP id ka12-20020a056a00938c00b0068be29c0b69mr2361160pfb.9.1697719533988; Thu, 19 Oct 2023 05:45:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697719533; cv=none; d=google.com; s=arc-20160816; b=WDrMW3KmPQpQGfK8VFoZnWmnKa5cifKPTb8N5IKiX76EDxPcXAgH5kZvSVvXKD0P9v oyquuEmq2i2Rt2P93mgAaT/d/Kqh6ipe0nyQgMwN/+2cTVrf+6HKzGc3Q1zEUCF4GBRG isAj6xY7skqOzRCTFfoFiWOmtdnFYXumW1JTEpLg6vUQ0WQuM8doiwsPbUumncq8fjZz 6gS/kAiRccJ3X0PKA7cVZaFs52+AwHB3EGFEhnTxBf/TIAHI7pndHVz0pGs7OjLaVFiQ hctpoJy8YFpO7xS28cetH8bBEZtfq/uTy8WgireBMdiYNasXU6Y5f9TAymBH/gAFqc4X UqIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=xHWXjeG7pBfXK7eLipLtNHZh6zFBxnrbrlemf/HbgqM=; fh=+DM0Y/eIUiUfHncfSWbyFbYTC9E9/HnS4GY/SKhqsnY=; b=wZCz8rtAf8oMm9AMVxEKbHGn8Gxkv6lem4iZjF+IxBwm9dONDNI9IznaWD01ymALfN iN5WzspEkJxJIK+3c6eXduQRshDXS8zrXQt23QWkwqokxeT7DrclpzRe6aKNGDChSVVJ Ca726qE4tyI8CZs4MpMMyEhHcgFEa/n0BqOW7Y0J/+0uDLu5jyDI1exF1Qv6ps0QmRA7 Jw0XXgbP4NjotraPe1fAPriwDG4rZSX5UfmcI5YXxMl0dlFbSTAAMRZmHblKLul4QMHX gJNVKN+AA4VMZ88hLwGudIGFsGDfeO3DTrgTCa2zJADuyW3L53nHwWQ4xLCftWlzzdmn Hqsg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id u204-20020a6279d5000000b00690d695b113si5920966pfc.337.2023.10.19.05.45.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 05:45:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 2E9748034611; Thu, 19 Oct 2023 05:45:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345577AbjJSMpL (ORCPT + 99 others); Thu, 19 Oct 2023 08:45:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345442AbjJSMpK (ORCPT ); Thu, 19 Oct 2023 08:45:10 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 046AFA4 for ; Thu, 19 Oct 2023 05:45:07 -0700 (PDT) Received: from dggpeml500002.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4SB6mq4Xs6z15NVP; Thu, 19 Oct 2023 20:42:19 +0800 (CST) Received: from [10.67.120.218] (10.67.120.218) 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; Thu, 19 Oct 2023 20:45:03 +0800 Subject: Re: [PATCH 1/3] coresight: ultrasoc-smb: fix sleep while close preempt in enable_smb To: Yicong Yang References: <20231012094706.21565-1-hejunhao3@huawei.com> <20231012094706.21565-2-hejunhao3@huawei.com> CC: , , , , , , , , From: hejunhao Message-ID: Date: Thu, 19 Oct 2023 20:45:03 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.120.218] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500002.china.huawei.com (7.185.36.158) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 19 Oct 2023 05:45:26 -0700 (PDT) Hi Yicong, On 2023/10/19 11:05, Yicong Yang wrote: > On 2023/10/12 17:47, Junhao He wrote: > > > Use spinlock replace mutex to control driver data access to one at a > time. But the function copy_to_user() may sleep so spinlock do not to > lock it. > > Fixes: 06f5c2926aaa ("drivers/coresight: Add UltraSoc System Memory Buffer driver") > Signed-off-by: Junhao He > --- > drivers/hwtracing/coresight/ultrasoc-smb.c | 36 ++++++++++------------ > drivers/hwtracing/coresight/ultrasoc-smb.h | 6 ++-- > 2 files changed, 19 insertions(+), 23 deletions(-) > > diff --git a/drivers/hwtracing/coresight/ultrasoc-smb.c b/drivers/hwtracing/coresight/ultrasoc-smb.c > index e9a32a97fbee..b08a619d1116 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,18 @@ 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; > } > > + spin_lock(&drvdata->spinlock); > *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); > + spin_unlock(&drvdata->spinlock); > Do we still need the lock here? If we get here, we should have the exclusive > access to the file, which is protected in open(). Or any other cases? This is something I've also considered. If someone opens an SMB device and reads it using multithreading, The SMB device will got out of sync. I've seen other coresight sink drivers such as etb do not use locks in the read function. Maybe it's not necessary here, the buffer synchronization is guaranteed by the user. Best regards, Junhao.