From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 44DFECDB482 for ; Thu, 19 Oct 2023 13:30:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Subject:CC:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gznDWUEaW+jCbzijp0iYliNwKAa7q/UnOZXRELMz/hU=; b=Uf77VHOYuzgVWo MuOoeuY9QvA2qW4uYVvPwV8riPhjEJf+0QUM9F2xX/3jlclxDKS0T7bUDxRQDY+g561l2sBqssvAf gPziuRGmWDjaTskbI+ag/4mU2FDfHUrx48yZdyMNfiIr3lDFNRwAYJzh45r/1oaLtaN0iCwK4xH/u zHYNgkICT0U7MFCUmVw4l/3hZhTitg/q6ERayKP61lnrtCzTQ4dMu7Gae27RB7LCIKDKbV9GFsIwQ x8YBoTu4/UcinOsDj7UVfK+00ylygb056ViwrqH7rOw97QvdV3tAxR1iK/fAbZYo6zAsoiq/lX3kd LvXHNoxYhcoqr/A/Oz/Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qtT6e-00HWm6-2I; Thu, 19 Oct 2023 13:30:32 +0000 Received: from frasgout.his.huawei.com ([185.176.79.56]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qtT6b-00HWlK-0D for linux-arm-kernel@lists.infradead.org; Thu, 19 Oct 2023 13:30:31 +0000 Received: from lhrpeml500005.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4SB7mF0lVhz6HJdH; Thu, 19 Oct 2023 21:26:53 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) 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 14:30:18 +0100 Date: Thu, 19 Oct 2023 14:30:16 +0100 From: Jonathan Cameron To: Junhao He , CC: , , , , , , , Subject: Re: [PATCH 1/3] coresight: ultrasoc-smb: fix sleep while close preempt in enable_smb Message-ID: <20231019142956.00005a3b@huawei.com> In-Reply-To: <20231012094706.21565-2-hejunhao3@huawei.com> References: <20231012094706.21565-1-hejunhao3@huawei.com> <20231012094706.21565-2-hejunhao3@huawei.com> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) MIME-Version: 1.0 X-Originating-IP: [10.202.227.76] X-ClientProxiedBy: lhrpeml500006.china.huawei.com (7.191.161.198) To lhrpeml500005.china.huawei.com (7.191.163.240) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231019_063029_486981_D4E8F790 X-CRM114-Status: GOOD ( 17.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, 12 Oct 2023 17:47:04 +0800 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): [] copy_process+0xae8/0x2b48 > softirqs last enabled at (0): [] 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 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. I'd like to see a comment on why we no longer need to lock over the copy_to_user rather than simply that we can't. > > Fixes: 06f5c2926aaa ("drivers/coresight: Add UltraSoc System Memory Buffer driver") > Signed-off-by: Junhao He A follow up patch could change a lot of this to use the new cleanup.h (don't want that in the fix though as will make back porting trickier.). That should let you do guard(spin_lock)(&drvdata->spinlock); and then use direct returns instead of goto complexity. Jonathan > @@ -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; > - Unrelated white space change that shouldn't be here. > 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); > > + dev_dbg(dev, "%zu bytes copied\n", to_copy); > return to_copy; > } _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5991FCDB465 for ; Thu, 19 Oct 2023 13:30:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345959AbjJSNa2 (ORCPT ); Thu, 19 Oct 2023 09:30:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235403AbjJSNaX (ORCPT ); Thu, 19 Oct 2023 09:30:23 -0400 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7836131 for ; Thu, 19 Oct 2023 06:30:20 -0700 (PDT) Received: from lhrpeml500005.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4SB7mF0lVhz6HJdH; Thu, 19 Oct 2023 21:26:53 +0800 (CST) Received: from localhost (10.202.227.76) by lhrpeml500005.china.huawei.com (7.191.163.240) 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 14:30:18 +0100 Date: Thu, 19 Oct 2023 14:30:16 +0100 From: Jonathan Cameron To: Junhao He , CC: , , , , , , , Subject: Re: [PATCH 1/3] coresight: ultrasoc-smb: fix sleep while close preempt in enable_smb Message-ID: <20231019142956.00005a3b@huawei.com> In-Reply-To: <20231012094706.21565-2-hejunhao3@huawei.com> References: <20231012094706.21565-1-hejunhao3@huawei.com> <20231012094706.21565-2-hejunhao3@huawei.com> Organization: Huawei Technologies Research and Development (UK) Ltd. X-Mailer: Claws Mail 4.1.0 (GTK 3.24.33; x86_64-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.202.227.76] X-ClientProxiedBy: lhrpeml500006.china.huawei.com (7.191.161.198) To lhrpeml500005.china.huawei.com (7.191.163.240) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 12 Oct 2023 17:47:04 +0800 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): [] copy_process+0xae8/0x2b48 > softirqs last enabled at (0): [] 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 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. I'd like to see a comment on why we no longer need to lock over the copy_to_user rather than simply that we can't. > > Fixes: 06f5c2926aaa ("drivers/coresight: Add UltraSoc System Memory Buffer driver") > Signed-off-by: Junhao He A follow up patch could change a lot of this to use the new cleanup.h (don't want that in the fix though as will make back porting trickier.). That should let you do guard(spin_lock)(&drvdata->spinlock); and then use direct returns instead of goto complexity. Jonathan > @@ -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; > - Unrelated white space change that shouldn't be here. > 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); > > + dev_dbg(dev, "%zu bytes copied\n", to_copy); > return to_copy; > }