Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3057887rdg; Tue, 17 Oct 2023 03:24:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHF1nsXfNW52g4r6qBzyzWVgjeiz/WnxR4MpZcbZP/0RZOyzCDAaQ8hQd1ZOphI6Qanp4BH X-Received: by 2002:a05:6a21:7187:b0:159:fe1d:2f32 with SMTP id wq7-20020a056a21718700b00159fe1d2f32mr1463526pzb.42.1697538291010; Tue, 17 Oct 2023 03:24:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697538290; cv=none; d=google.com; s=arc-20160816; b=MoSknEShRolb44MoIDeVMBtLqBXx5QnC1Iuwp5djRmzWyBfH5WwTsAqAZAHBEn8JmH vzCSEw+kGAZ6H1UJuMlXeLwL2r6y2CXd5uVoSVKVglW9QlrJ7OuZnK9xzz2g0DdwwVX1 nu1TXcefAdG06Enb+kSFkVeTChc8FNNl2RjIt+WkcOG25A5UZejBeup9uAIruFTGqtnw jkpoLyOG47gYoFptHMKRbTjOOhtPyiiFssn60zwt2hFhd3MoXFSjWszGPr/0zSLtS0ap LUqlzioZuOMuC3fQ8dy5+jXneZsSl3lcpMgmUuWnUma9RoUHUS8vVzBsEckP358FwA8S +tmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=85Sk0tOe6LwkqbmTCkktS/QMbIf4SzIPu3cOXuvN+gM=; fh=spI2KyfIu31qZnhRU8oQrYJALZIgqrdEbGqsK9TQ4lI=; b=Kt6aXL6o2U1wGAgBowBXffVP8tC36wHxUmU1snL+UxDnZM3pS3xPzMlsb8/w+OrZJ4 SDyWttCbTPxQKIVuHENynB+egykUne8UqrnYyjzIb4Jyr4y0Alp6rcAZSv6x+M8YchR2 fBemA/FvtB6ktRx8fLZ+wyK0ze9UkwUUkyvrskkXxiSidoKzxJPRDfY8NLdfT4+CLvQR mc7B28rmiQ/xopw2ff7GUMSY9ar7PKO55Z+0AV9QIUgh7Igcm3f8Cg3IJnexZWgNbk7W mAz1TeGWheOJnYmlsSUIs6Wt3TThmV9I+6bws0PE0jDaAkdflLGfhc8YYS4HKA8+O6jm 7hMw== 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:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id jj13-20020a170903048d00b001c9ca0167e2si1362851plb.420.2023.10.17.03.24.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 03:24:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 285D680309CB; Tue, 17 Oct 2023 03:24:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343590AbjJQKYs (ORCPT + 99 others); Tue, 17 Oct 2023 06:24:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343534AbjJQKYp (ORCPT ); Tue, 17 Oct 2023 06:24:45 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 01FFD9F for ; Tue, 17 Oct 2023 03:24:44 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 472E52F4; Tue, 17 Oct 2023 03:25:24 -0700 (PDT) Received: from FVFF77S0Q05N (unknown [10.57.68.232]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A2A7A3F762; Tue, 17 Oct 2023 03:24:42 -0700 (PDT) Date: Tue, 17 Oct 2023 11:24:40 +0100 From: Mark Rutland To: Rong Tao Cc: elver@google.com, tglx@linutronix.de, peterz@infradead.org, Rong Tao , open list Subject: Re: [PATCH] stop_machine: Avoid potential race behaviour of multi_stop_data::state Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 17 Oct 2023 03:24:50 -0700 (PDT) On Tue, Oct 17, 2023 at 04:50:52PM +0800, Rong Tao wrote: > From: Rong Tao > > 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 > --- > 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 >