Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp1554894rdb; Sat, 26 Aug 2023 07:20:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGR9cSDl0qZremL8bm9BdcH4Iu5byPmOFM7QHXVnNuNxjMBKIN31Ua5tToSAiPFydfsvwDp X-Received: by 2002:aa7:cd50:0:b0:525:4471:6b5d with SMTP id v16-20020aa7cd50000000b0052544716b5dmr15853071edw.19.1693059618699; Sat, 26 Aug 2023 07:20:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693059618; cv=none; d=google.com; s=arc-20160816; b=EeL1eMy3ZbjDXV4ZgL+nKSNjBOQMeiH9XTMzIqRf9fVCt8zOezIJNzPK3Rhnc5xmpV +kJhnlC03rieV/NIKtL6L8jeZoPD2Yn+BgBgXU7qqA3/xaSnUCw7JqkCU4VFQgzTKkhV uYf1XAlSGpgL8XsXr9FB/ZR8mVBQ8Q7rjeMzVmd+L60Nf+YVPgFv5uZPB+vGQadG3YmV Ilr8afsSyodDOSrUNTeNNxHrdY4l/wdzEspQX4UKLxL5aNDutNrCBsbJPEEq9EDVaLxg 5MJ0ykdZsWGnRjof6i6Aqt8XFsMWb1+V4sY29F3jdllz0yYP+9OL2nSrpKxA1xTbWRir ctig== 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 :message-id:date:subject:cc:to:from; bh=JUsTDkY3XbJcAj/rkdeOOJAIZkihHA+kNI32VmgqEME=; fh=K23wg4EVF23R1OZ+xeLqze7bN7tXX7tibn8/sdYKuVo=; b=0y/mbW2oTDgGmp0g2SkfWeGmANL4dxeKkt5RuYKH9YFNLRJy6JLpwFj92Pm3jPdQGs 2JxqoVT8MHF2sBRRQQdwIwj+9SDX9sxYv6bCZJG0JJF7lvgEqcHk2yAmWDb2MD8xC9jZ FvJ1fkzut7IjPTUbEJc1vIksw8VElPJzYQSu80aPPUE68cMfweHv9FrV4UEjdiYN0e8D HQ2EW2mEDE9re7HYp916NuuW/BG7XzUh1czohZhSNFKFNHoiAe1pTKLfZWI8WZa1SAIT hpJSbPjxX2rVFCiA69jvDBDzX+gEceRw70/am4IFncuHMf/1DaSq9lSdLf4mnDTF8co1 ahvw== 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 l13-20020aa7c3cd000000b0052370772030si2268624edr.164.2023.08.26.07.19.40; Sat, 26 Aug 2023 07:20:18 -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 S229875AbjHZKBH (ORCPT + 99 others); Sat, 26 Aug 2023 06:01:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231280AbjHZKAi (ORCPT ); Sat, 26 Aug 2023 06:00:38 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5C1DE5; Sat, 26 Aug 2023 03:00:35 -0700 (PDT) Received: from kwepemd100002.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4RXsgS4nnHzJrsx; Sat, 26 Aug 2023 17:57:24 +0800 (CST) Received: from huawei.com (10.67.174.28) 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; Sat, 26 Aug 2023 18:00:32 +0800 From: Liao Chang To: , , CC: , Subject: [PATCH] cpufreq: Fix the race condition while updating the transition_task of policy Date: Sat, 26 Aug 2023 09:58:36 +0000 Message-ID: <20230826095836.1138608-1-liaochang1@huawei.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.67.174.28] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemd100002.china.huawei.com (7.221.188.184) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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 The field 'transition_task' of policy structure is used to track the task which is performing the frequency transition. Using this field to print a warning once detect a case where the same task is calling _begin() again before completing the preivous frequency transition via the _end(). However, there is a potential race condition in _end() and _begin() APIs while updating the field 'transition_task' of policy, the scenario is depicted below: Task A Task B /* 1st freq transition */ Invoke _begin() { ... ... } /* 2nd freq transition */ Invoke _begin() { ... //waiting for A to ... //clear ... //transition_ongoing ... //in _end() for ... //the 1st transition | Change the frequency | | Invoke _end() { | ... | ... | transition_ongoing = false; V transition_ongoing = true; transition_task = current; transition_task = NULL; ... //A overwrites the task ... //performing the transition ... //result in error warning. } To fix this race condition, the order of the updates to the 'transition_task' and 'transition_ongoing' fields has been changed, the 'transition_task' field is now cleared before the 'transition_ongoing' field, which ensure that only one task can update the 'transition_task' field at a time. Fixes: ca654dc3a93d ("cpufreq: Catch double invocations of cpufreq_freq_transition_begin/end") Signed-off-by: Liao Chang --- drivers/cpufreq/cpufreq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index a757f90aa9d6..f8eb6dde57f2 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -455,8 +455,8 @@ void cpufreq_freq_transition_end(struct cpufreq_policy *policy, policy->cur, policy->cpuinfo.max_freq); - policy->transition_ongoing = false; policy->transition_task = NULL; + policy->transition_ongoing = false; wake_up(&policy->transition_wait); } -- 2.34.1