Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp116991rda; Sat, 21 Oct 2023 00:25:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQvXxWHMXkFE3six3+T+QqvwIPhmMYmHEGROa/ppRFYTg94vWYt32o98OfQ+E01E5oPsZc X-Received: by 2002:aca:240a:0:b0:3ab:74a2:ab2b with SMTP id n10-20020aca240a000000b003ab74a2ab2bmr4050072oic.48.1697873126741; Sat, 21 Oct 2023 00:25:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697873126; cv=none; d=google.com; s=arc-20160816; b=gVYgkpO27UFRAtg6TzHgHzVPQI8Hf96jO1Hi8IUKP1Q5wglIkZRML8tbnGfzZC1LXU A7kmYEbvT7Mxb5UF1ejMUipkzvE1NDuSLc/Py+TrCB/J27fJyrNWY+sjrE4W7E/nwrr7 jjDOP2eGYAqx8Gah6JGLrmE+1R8PoAaCvwSIG0CmtOBO8WAuNdKNtWD4FTMjf8XvRJ5M U5p5z3p0bu8NsD4esG7GwYJovCAJTAcddIUEE6SrlEd8a0VH/gglza76NV+PXSh7fYmo YBpK38Ew0euGRPQ25yFvOoh4zDCAM1TyTG7dp14/Ne2MF+KiECHLhuRslaQoNQ+LdVGC 3zFA== 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=zuq5fryg3TQD5VthEVUjIlPnBPF/syEO2aHaB7HKhKA=; fh=8+aKvmml2qC3nyoA6s8C50FAEBr2Lwoj1boW5JLPzcY=; b=NrLYQR7nAffTWMwL+qZbmY9XolXRFKz/kh2GvPnCc0qQftMUAhpUTADell3DCEoKje slje5bSuht9GJsKvYZN2FMW9vh80FwRTIxU7ZUgcCXT1wEOU4ty0QKod2oPD+BSF3HF+ KK3fC3a4KOkocIXlTFo1IkBkmniCBl1KwhDd4MIDnK8nGeAv53JPmS8dUxuiiNDnwVwu rC658xKcC0Afa7Qt5Tx6Ln/liJXd6o75AfC5W5+z7J5Ach6EuPcEdVdWTECIEz4A7Dh6 4eJXMyNQaULIhivviKfZWagK/ih8o8hwU9zV17zTulh29YvTOTz5agztHuSeSOMOLQIa kw/g== 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 d6-20020a170903230600b001ca6abecb27si3473124plh.498.2023.10.21.00.25.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 00:25:26 -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 453E482E296C; Sat, 21 Oct 2023 00:25:24 -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 S229678AbjJUHZR (ORCPT + 99 others); Sat, 21 Oct 2023 03:25:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbjJUHZQ (ORCPT ); Sat, 21 Oct 2023 03:25:16 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51B56D65 for ; Sat, 21 Oct 2023 00:25:13 -0700 (PDT) Received: from dggpeml500002.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4SCCYJ6GSYzNmNP; Sat, 21 Oct 2023 15:21:08 +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; Sat, 21 Oct 2023 15:25:09 +0800 Subject: Re: [PATCH 1/3] coresight: ultrasoc-smb: fix sleep while close preempt in enable_smb To: Jonathan Cameron , References: <20231012094706.21565-1-hejunhao3@huawei.com> <20231012094706.21565-2-hejunhao3@huawei.com> <20231019142956.00005a3b@huawei.com> CC: , , , , , , From: hejunhao Message-ID: <2f6c2af0-b867-5be5-ed37-c5d71b1ed1ce@huawei.com> Date: Sat, 21 Oct 2023 15:25:09 +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: <20231019142956.00005a3b@huawei.com> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.67.120.218] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) 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]); Sat, 21 Oct 2023 00:25:24 -0700 (PDT) Hi Jonathan, On 2023/10/19 21:30, Jonathan Cameron wrote: > 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. Yes, I will do that. >> 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 Thanks for sharing. I will append up a new patch to use guards to reduce gotos. > >> @@ -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. Ok, i will drop this white space Thanks for the comments! Best regards, Junhao. > >> 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; >> } > > . >