From patchwork Mon May 16 03:02:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Schspa Shi X-Patchwork-Id: 573196 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D6E7C433F5 for ; Mon, 16 May 2022 03:03:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239527AbiEPDDH (ORCPT ); Sun, 15 May 2022 23:03:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239532AbiEPDDF (ORCPT ); Sun, 15 May 2022 23:03:05 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 809F65F56; Sun, 15 May 2022 20:03:03 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id a22so11412295qkl.5; Sun, 15 May 2022 20:03:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3tBgExR0vnd7YeIN6exy+FZqzAtBcN823ZwmLJgoZM4=; b=Dt0n/o78G8O5CmeL15OxCRf1vH0qc/qDLtvTljzjUaXl4UHcuvQcOoBciZJWAoj8w0 IUU5EkKv8fRae6PUk/8chmxu+4q5P+mSeV3n1MsAVFR7KYjUTgwIfV91vgj5SxyJoSa1 lgfs0pZ6OdRLlU7Bh3liLsxkfl9PMwYSUm+LeIV8ojKcxTDPVcfD/m4rJz9hHEU+HvO+ fubbLv+DS0n2mI8woNBYWIyEjKxtLzjzpKvsQm8xRVort9ZBoacpPxokRF+qXwVSCjy8 e93zU7mIC1+Ah9buZT1uonJUdGV5q4SSlw99MKh08hkQpgn4HhNTP3vNLyKC8bAKC8+y AtmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3tBgExR0vnd7YeIN6exy+FZqzAtBcN823ZwmLJgoZM4=; b=abdvdGIm0nwljTRnZNJFwLl02oSKKiBthAeEYGp6F8yafra9WYoGzwEtBj/z/Z0qFQ Kf44uPWk/RTTcRoK3HCTEX1ZVXbjB18vT8Adp8WWGlehU0HjWLaqUpSa7MCHfjq9fTSk rqaLFbPMiAMW+jHfHTxfROinn0dfyL3L3Q/7+sQYKBMU0ePbnESoaI58NeZTq6hQVD74 zLcHILZmSOVP4NCtFT1ZrOqh1kll6VjHi2brGu/QT0Lfjcj8G+YhBc2dZHIQUd6kWvUn 6R391pYRUhsckWfS/lFXFj6QyFf0nmrMDPOK+pWq7EBHjXoFgGiDUVSVzUzFdzYr3yTJ 5v/w== X-Gm-Message-State: AOAM531Hf+SUFS2D3CFN+lFTTXx+a+U4T8ZDfqbn3O7f1592lykaPRuC qRcRWbmHXRjbGyaHNl2BY6U= X-Google-Smtp-Source: ABdhPJygMrdqZ83DlnmDrSrFKeY7Pnw2vBpB8+lhU8+IzsjP/UxNQpyB6rqifHN24jn+Guf9CtE//g== X-Received: by 2002:a05:620a:993:b0:69f:d0fb:48a5 with SMTP id x19-20020a05620a099300b0069fd0fb48a5mr11019650qkx.562.1652670182328; Sun, 15 May 2022 20:03:02 -0700 (PDT) Received: from MBP.hobot.cc (ec2-13-59-0-164.us-east-2.compute.amazonaws.com. [13.59.0.164]) by smtp.gmail.com with ESMTPSA id 186-20020a370bc3000000b0069fcabfdaebsm4993072qkl.70.2022.05.15.20.02.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 May 2022 20:03:01 -0700 (PDT) From: Schspa Shi To: rafael@kernel.org, viresh.kumar@linaro.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, schspa@gmail.com Subject: [PATCH v6 1/2] cpufreq: Abort show/store for half initialized policy Date: Mon, 16 May 2022 11:02:50 +0800 Message-Id: <20220516030251.42323-1-schspa@gmail.com> X-Mailer: git-send-email 2.29.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org If policy initialization fails after the sysfs files are created, there is a possibility that we may end up running show()/store() callbacks for half initialized policies, which may have unpredictable outcomes. Abort show/store in such a case by making sure the policy is active. Also inactivate the policy on such failures. Signed-off-by: Schspa Shi Acked-by: Viresh Kumar --- Changelog: v1 -> v2: - Fix bad critical region enlarge which causes uninitialized unlock. - Move cpumask_clear(policy->cpus); before out_offline_policy v2 -> v3: - Remove the missed down_write() before cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); v3 -> v4: - Seprate to two patchs. - Add policy_is_inactive check before sysfs access v4 -> v5: - Change the commit message as Viresh advised. - Initialize ret to -EBUSY to get rid of the else part. v5 -> v6: - Change up_write(&policy->rwsem); to after out_free_policy; - Fixes: https://lore.kernel.org/all/20220515095313.GE10578@xsang-OptiPlex-9020/ Signed-off-by: Schspa Shi --- drivers/cpufreq/cpufreq.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index 80f535cc8a75..ba73be6f0490 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -947,13 +947,14 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) { struct cpufreq_policy *policy = to_policy(kobj); struct freq_attr *fattr = to_attr(attr); - ssize_t ret; + ssize_t ret = -EBUSY; if (!fattr->show) return -EIO; down_read(&policy->rwsem); - ret = fattr->show(policy, buf); + if (likely(!policy_is_inactive(policy))) + ret = fattr->show(policy, buf); up_read(&policy->rwsem); return ret; @@ -964,7 +965,7 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr, { struct cpufreq_policy *policy = to_policy(kobj); struct freq_attr *fattr = to_attr(attr); - ssize_t ret = -EINVAL; + ssize_t ret = -EBUSY; if (!fattr->store) return -EIO; @@ -978,7 +979,8 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr, if (cpu_online(policy->cpu)) { down_write(&policy->rwsem); - ret = fattr->store(policy, buf, count); + if (likely(!policy_is_inactive(policy))) + ret = fattr->store(policy, buf, count); up_write(&policy->rwsem); } @@ -1533,6 +1535,7 @@ static int cpufreq_online(unsigned int cpu) for_each_cpu(j, policy->real_cpus) remove_cpu_dev_symlink(policy, get_cpu_device(j)); + cpumask_clear(policy->cpus); up_write(&policy->rwsem); out_offline_policy: From patchwork Mon May 16 03:02:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Schspa Shi X-Patchwork-Id: 574125 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2825BC433F5 for ; Mon, 16 May 2022 03:12:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233227AbiEPDMP (ORCPT ); Sun, 15 May 2022 23:12:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231238AbiEPDMO (ORCPT ); Sun, 15 May 2022 23:12:14 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AD4611C01; Sun, 15 May 2022 20:12:13 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id k8so11414572qki.8; Sun, 15 May 2022 20:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Iknl9FGNjcSKriVQ8ROc164iV8qluWzwBBYuxxLunGQ=; b=Bsbk8Y0yg5VxbQEpNpD579fQ1Jdbh8Tc00NPMDYN65Kd6bwr6CayognJQIG7jL7Z/o MHH5OkbN57AW6FSdXW9EOCyJbg/uQ+hT8uT7ZkSRM3xwsOKsJnjSJQ9MkUKgsU15criK NUgjyzxFUT+23G+ri+8Yotz8JKrE9073pl2GUL28wBvelugkksLGfCMPwU5tWLL2le8G 9fHFnVZOgBInIPTAXJ/fl4YjYGNOHgUStQM5ImS+mOEKCH4PDnaiaZDmDcoo0zYJjTs4 DaVia075alP46+fk/Cs+S6cmqPxyu2+l+GDl47nPqZagp1tKlMIKGGTBkn26ggmiYh+3 qC2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Iknl9FGNjcSKriVQ8ROc164iV8qluWzwBBYuxxLunGQ=; b=WadKUsooTwzoqZZTKFaRQD+oTH9U53W4s4dndlqGf0bZBNCu6aWapPcgCT/5NwtGuB 4pea9cNopzZAB8BzW1rLCzEVVwJdSAlQfPezrtNn/scXcQGeRb/ccwjAZa6oJ6pQ8nDp xVTq4w1Gf5FR9xS31APwMD5ML+r3oaZiS0kW9QpbcS8xIoX1CGi7KeIubdb4qbZdt8U+ Vt7Lac0oSkEv8zFpDIgQOADylooUdDcnv6fwKfZmxB2jHJfTq6dxb//a3OHIhkbRdhJD CedVyCqYXES/dKYWWYlFn583ct0gB2S8UetMyDB7iEcWAOh3zg146Qk3f+tlJYtgWSrN eC9A== X-Gm-Message-State: AOAM530CPyqe8AVi/PgM4B1gipf+5+aZ8L5xIsqUj/Goe17qdJNrrDeB i5j51SB5b9EVuFHP1aT45/QB3IDeE1/0GYAZ X-Google-Smtp-Source: ABdhPJxzeagnTfennb3tUjiwr3t6TASF6USpu1oN2N0cKvtXfHTLhLn4KYzsX3gNxz2eFkw9IEiaKQ== X-Received: by 2002:ae9:df46:0:b0:69f:b616:762b with SMTP id t67-20020ae9df46000000b0069fb616762bmr11030804qkf.600.1652670732521; Sun, 15 May 2022 20:12:12 -0700 (PDT) Received: from MBP.hobot.cc (ec2-13-59-0-164.us-east-2.compute.amazonaws.com. [13.59.0.164]) by smtp.gmail.com with ESMTPSA id 186-20020a370bc3000000b0069fcabfdaebsm4993072qkl.70.2022.05.15.20.12.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 May 2022 20:12:12 -0700 (PDT) From: Schspa Shi To: rafael@kernel.org, viresh.kumar@linaro.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, schspa@gmail.com Subject: [PATCH v6 2/2] cpufreq: make interface functions and lock holding state clear Date: Mon, 16 May 2022 11:02:51 +0800 Message-Id: <20220516030251.42323-2-schspa@gmail.com> X-Mailer: git-send-email 2.29.0 In-Reply-To: <20220516030251.42323-1-schspa@gmail.com> References: <20220516030251.42323-1-schspa@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org cpufreq_offline() calls offline() and exit() under the policy rwsem But they are called outside the rwsem in cpufreq_online(). This patch move the offline(), exit(), online(), init() to be inside of policy rwsem to achieve a clear lock relationship. All the init() online() implement only initialize policy object without holding this lock and won't call cpufreq APIs need to hold this lock. Signed-off-by: Schspa Shi --- drivers/cpufreq/cpufreq.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index ba73be6f0490..ccb774e02934 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1339,12 +1339,12 @@ static int cpufreq_online(unsigned int cpu) down_write(&policy->rwsem); policy->cpu = cpu; policy->governor = NULL; - up_write(&policy->rwsem); } else { new_policy = true; policy = cpufreq_policy_alloc(cpu); if (!policy) return -ENOMEM; + down_write(&policy->rwsem); } if (!new_policy && cpufreq_driver->online) { @@ -1384,7 +1384,6 @@ static int cpufreq_online(unsigned int cpu) cpumask_copy(policy->related_cpus, policy->cpus); } - down_write(&policy->rwsem); /* * affected cpus must always be the one, which are online. We aren't * managing offline cpus here. @@ -1536,7 +1535,6 @@ static int cpufreq_online(unsigned int cpu) remove_cpu_dev_symlink(policy, get_cpu_device(j)); cpumask_clear(policy->cpus); - up_write(&policy->rwsem); out_offline_policy: if (cpufreq_driver->offline) @@ -1547,6 +1545,8 @@ static int cpufreq_online(unsigned int cpu) cpufreq_driver->exit(policy); out_free_policy: + up_write(&policy->rwsem); + cpufreq_policy_free(policy); return ret; }