Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp2377852rdb; Mon, 28 Aug 2023 02:21:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGCwxMlI2kqKZ1fbxLR+apDxU2Mvx9gSA/Mp3qTeipUJdk6mloe4OzSW4jt+qFXY38hg2Qx X-Received: by 2002:a17:902:8216:b0:1b9:e937:9763 with SMTP id x22-20020a170902821600b001b9e9379763mr16820805pln.12.1693214513691; Mon, 28 Aug 2023 02:21:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693214513; cv=none; d=google.com; s=arc-20160816; b=hgR20gZBoqsf2s/1X/PhBBUMDr5Dk0JDd+huVR9C1nmbf8/+jDwZxw0d7MOFBJoc6G gDUGQKROya+0IfB2ykO36VO1zCxDptsG48Kauk8uZ4DOH8LuLHjqFL7M2o5+RvFVlSYP ocvvcxXdnw0Odd+MJ1P4BhGl3phdz2fDUCcTQ4TsXDOy1HDWCwaDq8zjGg/B4Ej8YWQj clj06fJiwQ1F8hMYWtWLgOM/Heer7vNx6kS6i2gNz9f566+5rabSg/nm+Eb2tPtdViKG WaL/rMaP5u4zlPJYq6EPXNLqeHWsv2+VxRAwgQNIoUCATBl8bRJyfzIgjtr9ARLdUGFc XRcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=abTZygfi+evdhrxNCJXREzWZsLvGRkbZ7wXgZJIvU+A=; fh=0inl2L5EWuN5mtsigvwXMHKERjyC2ww1DvvKeWnFtio=; b=y05s13JqV0xqa0fp3/tDLOj2wZRzUr6rG5HiO3byaYC5f0bNxfzfsyamuffAMEV+HQ eDP3qLNkS21qjh5qFDKulBRkSsbvlRI3Ei7clxtjnjbrnV2hYFTOYSTLhH6OvJwySJmN ADieI1Xpv6Lv+JxnD8by10p9p5bH2oJmIvr6CnDbknyw4NCdR1scvqdhUv/PyJxjGNYH x51q/g1hESJuzqnLdNKn+Wo8iANduDt8a9pc/qW7ejnQ8O4pa5FnhVwmmLoDamiC96MC hByGjTvreyxN+/IhOYP9ESZKqw39gtB9Y1HfL9Y2QNnUCc30RLdcTSiogLPkHSxVdgUZ T5Pw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v18-20020a170902d09200b001bee6def27esi6361976plv.260.2023.08.28.02.21.22; Mon, 28 Aug 2023 02:21:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbjH1I7V convert rfc822-to-8bit (ORCPT + 99 others); Mon, 28 Aug 2023 04:59:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229851AbjH1I6s (ORCPT ); Mon, 28 Aug 2023 04:58:48 -0400 Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00C3102; Mon, 28 Aug 2023 01:58:45 -0700 (PDT) Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-57328758a72so828336eaf.1; Mon, 28 Aug 2023 01:58:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693213125; x=1693817925; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g3FZcTZH37FUS4WWZ6lFpJNi4D7JTyraivRiz2TeBYU=; b=U1ipCtiNFyWzWpBocewkjMS3ZFazqtgVu3IcByP4H9gWeqPXtCD1dHNWMJtSqXXDKT IYrxc9FCyauNT0jyt8BV1n8o9GqgnU8hGJK5xEDPtDCiqZxGckk7k6DsCc9DAb6I397h Jql5oVNR3PKKTGvfJ6pe/F8cv/OTDvR4L+EFVff4UURbyKdIVwZO8ZbVVIqZSxGL/BaX Qzkw5uPJ+vRnOlHd+Ks0z80tKClDl6+rSF15D0NShrBvh+jV4FNf+xCzzem3/igx4b3u vTefdAN6z/PEyjAHWYzBrZ+p/zXvRS1XCkceKA9GWd731uAr62AmIGUyb3BQT8pYkEDY FeGg== X-Gm-Message-State: AOJu0YzDEeTfftdiYwYMdBbraGNh3xdLLox8z7BlX8y3j5ONvAlPKlrG WDTuFjl0Jqrvrsrny0LQoHgGSw3jqG9gnQ9jJlo= X-Received: by 2002:a4a:d137:0:b0:571:1906:47f0 with SMTP id n23-20020a4ad137000000b00571190647f0mr13319897oor.1.1693213125014; Mon, 28 Aug 2023 01:58:45 -0700 (PDT) MIME-Version: 1.0 References: <20230826095836.1138608-1-liaochang1@huawei.com> <20230828072347.ly23mbptu3yw4zkv@vireshk-i7> <20230828085248.sz6aljr5aln7j435@vireshk-i7> In-Reply-To: <20230828085248.sz6aljr5aln7j435@vireshk-i7> From: "Rafael J. Wysocki" Date: Mon, 28 Aug 2023 10:58:30 +0200 Message-ID: Subject: Re: [PATCH] cpufreq: Fix the race condition while updating the transition_task of policy To: Viresh Kumar Cc: "Liao, Chang" , rafael@kernel.org, srivatsa.bhat@linux.vnet.ibm.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no 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 On Mon, Aug 28, 2023 at 10:52 AM Viresh Kumar wrote: > > On 28-08-23, 16:29, Liao, Chang wrote: > > Task B does not necessarily go to sleep when it calls wait_event(), it depends on > > the condition to wait for evaluate false or not. So there is a small race window > > where Task A already set 'transition_ongoing' to false and Task B can cross wait_event() > > immediately. > > > > wait_event: > > do { > > might_sleep(); > > if (condition) // !transition_ongoing > > break; > > __wait_event(); > > }; > > > > I hope I do not miss something important in the code above. > > > Yes, if the CPU uses weak memroy model, it is possible for the instructions to be reordered. > > therefore, it is a good idea to insert an smb() between these two lines if there is race here. > > Maybe it would be better to do this instead ? > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index 6b52ebe5a890..f11b01b25e8d 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -455,8 +455,10 @@ void cpufreq_freq_transition_end(struct cpufreq_policy *policy, > policy->cur, > policy->cpuinfo.max_freq); > > + spin_lock(&policy->transition_lock); > policy->transition_ongoing = false; > policy->transition_task = NULL; > + spin_unlock(&policy->transition_lock); > > wake_up(&policy->transition_wait); > } > > -- I was about to suggest the same thing. wake_up() is a full memory barrier only if it actually wakes up a task and if it doesn't do that, without the locking the other task may see a state in which transition_ongoing is false already and transition_task is still NULL regardless of the relative ordering of the statements before the wake_up() call.