Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp2382689rdb; Mon, 28 Aug 2023 02:35:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFv7ITLzp60AiUS514HnEKWQ6dsFi0gWI6bvCVvJIwuYRT0G4N0uzq4Va+u7ewnxrmb/4y7 X-Received: by 2002:a05:6402:1508:b0:522:31d5:ee8e with SMTP id f8-20020a056402150800b0052231d5ee8emr19652397edw.8.1693215351614; Mon, 28 Aug 2023 02:35:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693215351; cv=none; d=google.com; s=arc-20160816; b=av97nldF3e2Y4YfoLdyAcaRNVkpsW/15YjaWyXnif3/1C7qU7hBbfIoqhl9XhoZQks f78fSoyWIaP264a3pD7V9xGvTZ76pnBP0tl1T6IJSiFm2vN6fqOkVDEdgJK6XbIb7aQn V04bpZYWqRqN3XcSjt+7NrF5McUOAKxs8D5shghrb1VAXnGEp5Y2acBvM4C+dFebfMSp KmLiknWDRHB5l2G2S69HPGawemz49YKNFsDNuD7c4Ov/iiDirhLTN57OYLRI0Gc9CnVx HU6F/cdJIcRIm0H4owtO6ig9ktruSpTTxGEfJgj0FrWiF6Z9GOQ5mqTgfz6zEBKooNuH qGLw== 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:from :references:cc:to:subject:user-agent:mime-version:date:message-id; bh=Uh1wzXeNOc9ZzfVqap0SG7v+auhtQGZfy3pEpYxA6Ts=; fh=BXgNwsKBdNw4+PyderE2aC2thSk9WHbnhQ2Ybs0B6UI=; b=i4JrwYBBwTQCsse04gj4poBKjIfXPDnRv52LoQ37LK8bZiB1I0/DJL2yMa17Qv5qRZ Hbc9zjL5NXIbLF9Q0vtmFfcqEi3i72wCXDx6djZ8bxCZ/iK4r7EqsWh5BZqOEAodXhXr ZMAK+VlK9BRdpKiAbhFxGHjZJYEM2g/LBWjipeGPwAj3iFwmmSdZYFt/jFkKy5wNT0Jy lbqtA3V/C1c68qxy6rljD18d519f1GRoLbVKdJlG6c8x+LzU9PDIJrLehsfCgOYKqDzh ECQsqkeSrtjnCxVOpcexx/SWUwivwe1kFr1OvJPgw72PFQ7Glpv9/Zg2p7RfvNKbq8D+ jQZg== 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w4-20020a056402128400b005254008c34asi4486073edv.4.2023.08.28.02.35.12; Mon, 28 Aug 2023 02:35:51 -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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229896AbjH1J3M (ORCPT + 99 others); Mon, 28 Aug 2023 05:29:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230177AbjH1J3K (ORCPT ); Mon, 28 Aug 2023 05:29:10 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6836B9E; Mon, 28 Aug 2023 02:29:07 -0700 (PDT) Received: from kwepemd100002.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4RZ4sR3p4KzhZHm; Mon, 28 Aug 2023 17:25:15 +0800 (CST) Received: from [10.67.110.108] (10.67.110.108) by kwepemd100002.china.huawei.com (7.221.188.184) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1258.23; Mon, 28 Aug 2023 17:29:04 +0800 Message-ID: <6583cf83-3598-7b7c-4753-611951c5d09b@huawei.com> Date: Mon, 28 Aug 2023 17:29:04 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.2 Subject: Re: [PATCH] cpufreq: Fix the race condition while updating the transition_task of policy To: Viresh Kumar CC: , , , References: <20230826095836.1138608-1-liaochang1@huawei.com> <20230828072347.ly23mbptu3yw4zkv@vireshk-i7> <20230828085248.sz6aljr5aln7j435@vireshk-i7> From: "Liao, Chang" In-Reply-To: <20230828085248.sz6aljr5aln7j435@vireshk-i7> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.110.108] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemd100002.china.huawei.com (7.221.188.184) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS 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 在 2023/8/28 16:52, Viresh Kumar 写道: > 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); I think it is more straightforward, I will use it in next revision. Thanks. > > wake_up(&policy->transition_wait); > } > -- BR Liao, Chang