Message ID | tencent_705C16DF25978ACAEBD1E83E228881901006@qq.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3987795vqb; Tue, 17 Oct 2023 01:51:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDLZu1JjoeWhen6jmmeAVHsg72jR+kFM0GCbMCK8YQeeCgeAgW+S5pfBs9DwKa52dDmhWA X-Received: by 2002:a54:448e:0:b0:3a9:f25d:d917 with SMTP id v14-20020a54448e000000b003a9f25dd917mr1840145oiv.4.1697532681987; Tue, 17 Oct 2023 01:51:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697532681; cv=none; d=google.com; s=arc-20160816; b=gt/oFmjEZSx5UgyT02IZjMY9UrBul4+9yRPQhxWLwFKMe3CveW1zdn4kR9I6fRvg+I ZXuiQDIWaMZemcoEsLruZq+TGagp/gB/AnHv25e6uHJL3yPqKZ1xJ+Cy6pLcz0pqdygX efrTzjJFgtg7RoGMi/aN3BWZp0x/4wMp21Bw+n3xFuoQnnvz2kAooX6fWjiXgEsRM5mk uJX9FKzH4x3S0ZZKWdogCr/3rcaJ6a/XRawUqNFl8bC0YjxvWnb9y6kfZ3JZaw0ATmOZ /KP06h609ySnLeAxE/0G0YTl67/49ZBtxTn1F0L5AtzLN8tmJnM1w4TJ81ftaZ6HK+X/ CsJg== 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:date :subject:to:from:message-id:dkim-signature; bh=9uFm1swz/WuySLvKGXHbXWNCQTdIg19qp5EJ/O34ajM=; fh=RUQVdVZH2FR18CrjX8PCifUFNIJLTj54dOHt1R32Ep4=; b=w47l0/n7+2NK6qimxH/f2rZfO3pAc2NA6jns35QaprR4MSJTJHclkbgzYbRCGOn1c2 vUNXl3S4P3NHAG8Te9tFPRt9aiKvRL2MtK6WOf7FJ6+CixiQR+/tHm0nO7GymBC9bqGC IizARSdPoARVQR+I5sYeX/uLCpEvbTIDqEw9aV0fHYRnCS70JFYyAGHOLaclR4pc+9HU 5GcZcda6vqJGU0uuDue/mhQmZS91iulA7pYz+34uJNuOL4n74kX+YugbD8wjGIf9as4A YTwbt0sNj3oSZM0f60wTFNNVHxCbXwo4bhy1pvNatDUj6Iwt2hcLan1egvvDBKRK3sBS y1QQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=BKRcoewo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id l4-20020a655604000000b005859e224624si1244310pgs.823.2023.10.17.01.51.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 01:51:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=BKRcoewo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C8F8E803F616; Tue, 17 Oct 2023 01:51:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234145AbjJQIvF (ORCPT <rfc822;hjfbswb@gmail.com> + 19 others); Tue, 17 Oct 2023 04:51:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37472 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234620AbjJQIvD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 17 Oct 2023 04:51:03 -0400 Received: from out203-205-221-192.mail.qq.com (out203-205-221-192.mail.qq.com [203.205.221.192]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8F50ED for <linux-kernel@vger.kernel.org>; Tue, 17 Oct 2023 01:51:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1697532658; bh=9uFm1swz/WuySLvKGXHbXWNCQTdIg19qp5EJ/O34ajM=; h=From:To:Subject:Date; b=BKRcoewoyh47KFeApgMNZ1G63acF/V+DNlaQ1/0h/GrGh2BprmZP3ySAX0eRrw9RR G/ZQNGAleU3e2LSg2llJx9JCSaIsDH8/W9n9xuvj2PRkz9xskTRSgWybxmc+iyogOg fosNjUDAnhV4jCxE/xdqQAf+OvEqBM6IRwT5Ku5I= Received: from RT-NUC.. ([39.156.73.12]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id CB837072; Tue, 17 Oct 2023 16:50:56 +0800 X-QQ-mid: xmsmtpt1697532656ti2ncg7rd Message-ID: <tencent_705C16DF25978ACAEBD1E83E228881901006@qq.com> X-QQ-XMAILINFO: MyIXMys/8kCtt4y1uxNGwZpRB3usmzxHRUaiJ+PZ0Y6gfxbVLShGIS5D6Yyldu aWbS43+6OvJjA9YQ3nK4x8QBLmqvkx3a9iPvIBrXfO1WiQuzHq/WhTlqvLcIlk0pzk3o62bJ+3o9 wiXzBI0+i+L2t70AmnUW6C80wO6izz6Hwh6h0qscGqRJB5g7edjcE9h/7xJnlCOuBd5PWEv775jG Vq6eDiRRPZj81D63BiX5nEkF5dGUpNs6NYx0Cz86foBbQtHpk7aBCjXSF0h4lc8Oaw3byjJAi34y nNC+IpKFnVGFiKRFtGiDS3YdIq/0EktKed+lUfNcZ2mlJ3KtchAqnPDMFWOfDJ6XKRhmYQIDTqgc po5iHAawCC2DHvkquY+cj/swh2YNCVzC/DdvJIuRRDt7CPfFsFs5yNUS7D26CN6yO0NZ0eyiztqE 8ijyplOxawiTUgP6os1l+5Ry/zBsa+12D7r/AZFHaZJqoe/q9HEphNf8pOUQuLBxWKUKRaFoPIl9 Lyw/S94gFR9smyjBeVlAA6A44n0tBF/e0bzGqUBVTssmmL0CCzsTXryGMWFJAFTHFLz2prvoXIEI YUDnN/Q4iiIoDQSMkHHKi1f0pVUATWegwqF5LOn2WPrKHIb3mcMye5s31Fn1UCqsNevOLBarR492 oCa2t8i+B+NCLvIl2ZE2U4yiS8L6IJKIzIJTrA8rdVWrvd0xi1f0UgZF/DmrCjyecFxaaB18MPa4 4yg+x7jtDX2Fv6udOTcEHhkmo3nalpaE5xVJ6X9/GBL/7Y4l5cCpNCSL6yueGc0ji6InaMluL7jp sHOz1+5XzqY1qzlO2g9yljiQCbF1x+mz2j1ShTUu0BjoZXiuLYsY54satbG7AR+S2q8ADOOISM+I JW5PDwakYLPQO7HSScp3Jq4bEWjr948udXtSsoOioMuYcERKQduHreIoVY0DPWu/0TXhCR5n6LQq wyk9KRkNXlBq/W/oMu7Q== X-QQ-XMRINFO: MPJ6Tf5t3I/ycC2BItcBVIA= From: Rong Tao <rtoax@foxmail.com> To: mark.rutland@arm.com, elver@google.com, tglx@linutronix.de, peterz@infradead.org, Rong Tao <rongtao@cestc.cn>, linux-kernel@vger.kernel.org (open list) Subject: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state Date: Tue, 17 Oct 2023 16:50:52 +0800 X-OQ-MSGID: <20231017085053.263070-1-rtoax@foxmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,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 howler.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 (howler.vger.email [0.0.0.0]); Tue, 17 Oct 2023 01:51:19 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779992029611741651 X-GMAIL-MSGID: 1779992029611741651 |
Series |
stop_machine: Avoid potential race behaviour of multi_stop_data::state
|
|
Commit Message
Rong Tao
Oct. 17, 2023, 8:50 a.m. UTC
From: Rong Tao <rongtao@cestc.cn> In commit b1fc58333575 ("stop_machine: Avoid potential race behaviour") fix both multi_cpu_stop() and set_state() access multi_stop_data::state, We should ensure that multi_stop_data::state is accessed using the rwonce method. Signed-off-by: Rong Tao <rongtao@cestc.cn> --- kernel/stop_machine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Tue, Oct 17, 2023 at 04:50:52PM +0800, Rong Tao wrote: > From: Rong Tao <rongtao@cestc.cn> > > In commit b1fc58333575 ("stop_machine: Avoid potential race behaviour") > fix both multi_cpu_stop() and set_state() access multi_stop_data::state, > We should ensure that multi_stop_data::state is accessed using the rwonce > method. > > Signed-off-by: Rong Tao <rongtao@cestc.cn> > --- > kernel/stop_machine.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c > index cedb17ba158a..73de9ab77132 100644 > --- a/kernel/stop_machine.c > +++ b/kernel/stop_machine.c > @@ -191,7 +191,7 @@ static void set_state(struct multi_stop_data *msdata, > static void ack_state(struct multi_stop_data *msdata) > { > if (atomic_dec_and_test(&msdata->thread_ack)) > - set_state(msdata, msdata->state + 1); > + set_state(msdata, READ_ONCE(msdata->state) + 1); IIUC this is bening, as the state machine only ever has a single writer to msdata->state (though which thread is the writer may change per iteration of the loop). At this point we know that the active thread *is* the writer, and so no other threads can write to msdata->state, so there is no race and reading that non-atomically is fine. I'm not opposed to making this use READ_ONCE(), but I don't think that it's strictly necessary to do so. That said, if we really want to avoid the non-atomic read, it's probably better to have multi_cpu_stop() pass curstate as a paramter to ack_state. That or fold ack_state() into multi_cpu_stop() and use curstate directly. Mark. > } > > notrace void __weak stop_machine_yield(const struct cpumask *cpumask) > -- > 2.42.0 >
On Tue, Oct 17, 2023 at 11:24:40AM +0100, Mark Rutland wrote: > That said, if we really want to avoid the non-atomic read, it's probably better > to have multi_cpu_stop() pass curstate as a paramter to ack_state. That or fold > ack_state() into multi_cpu_stop() and use curstate directly. Right. Also, that smp_wmb() needs a comment. Or perhaps smp_store_release().
Thanks, Mark and Peter, I just submit v2, please review. Rong Tao
diff --git a/kernel/stop_machine.c b/kernel/stop_machine.c index cedb17ba158a..73de9ab77132 100644 --- a/kernel/stop_machine.c +++ b/kernel/stop_machine.c @@ -191,7 +191,7 @@ static void set_state(struct multi_stop_data *msdata, static void ack_state(struct multi_stop_data *msdata) { if (atomic_dec_and_test(&msdata->thread_ack)) - set_state(msdata, msdata->state + 1); + set_state(msdata, READ_ONCE(msdata->state) + 1); } notrace void __weak stop_machine_yield(const struct cpumask *cpumask)