Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp925219pxb; Fri, 22 Apr 2022 14:28:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6Pcgo6fmL8m/HNCVJP0r2ig+kxYhAgaFKenVnz+nlKkeiGNR9GfSt4JJPP/VDvtz5415q X-Received: by 2002:aa7:93b2:0:b0:50c:e673:a259 with SMTP id x18-20020aa793b2000000b0050ce673a259mr6671507pff.36.1650662930872; Fri, 22 Apr 2022 14:28:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650662930; cv=none; d=google.com; s=arc-20160816; b=Pj878E+RtO1Jx1VbBwN+qO+US8GV3ACiUus5qj0Ty3Wz5CiMPByk3XdW2vvzQTGn2c wE4Ns1vYXn9BkDkF4H8nK6JxsIaefWW15vaNJyLDzmAQHnAPi8zOfvZKTRj49RYj3Vkr I0V81x0Eh3zfHrdRJ5yLs9iDHIV8XJpkPxnrkImMd01VupWXnIXaud7m2C1c/C1mz+Jg cpmgt0YypaD+ws47B0wGcxSQHn+LPgBvzu2EWLrs9shmphXHlnBNyU9uqPuGTmGwWc2N oN1kdJmDETovuQTp18Po5HhLq6E3+Bd24H6bBxSXwTNiK5YQICpsIV8xZYjDbAKoWWe8 hZmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=y0Vftok5Nvg9cisIqL5OFcU6eRNBtw4NVj7c2gCi/PY=; b=ERLupXozSDFBHX9GGoZk2Aog1DtuEGl80OqLvGHm2v5igtPJ7LHYBkrU0xt6r8XRnF vmG/Q8//CSjca6J5LHh9pYnKz6KWBtIfnmK5iZbujZQjUG5MLh6IMuORZ0LL1jL2WfuF JUzNRva+oDT+HK6TvkFELGTEM8Y6E6gfu5eALZBNPzDEYcUvkDejjolQaG0FM+7FNQWj g+ulwxXNrIs+R+Q7RguwBHX99SYJvIoe6eTgySffLqcXnIjcdQ8jHEAFJWTwNlqpfYZH SJmc9uJmBgWZXJv3eDtmQpckOUgplRM7wXL1Ic4NqFSCn/k/8ydJTvb34iTBsf8ZERZc Vorg== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id s2-20020a17090a1c0200b001c6855ece3esi4965676pjs.108.2022.04.22.14.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 14:28:50 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C85CA1EE8A3; Fri, 22 Apr 2022 12:36:08 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1448777AbiDVOla (ORCPT + 99 others); Fri, 22 Apr 2022 10:41:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1444460AbiDVOl2 (ORCPT ); Fri, 22 Apr 2022 10:41:28 -0400 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 621015BE45; Fri, 22 Apr 2022 07:38:35 -0700 (PDT) Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-2ebf4b91212so87470967b3.8; Fri, 22 Apr 2022 07:38:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=y0Vftok5Nvg9cisIqL5OFcU6eRNBtw4NVj7c2gCi/PY=; b=xNBx5ixEw9m0VQD0QmEuq2v1scW9Rdq/IViSkIFqkLsJar+psOm4l6VvlQBBxwcLpr 7+DFFbeTKP9czNVSKLIN4s19UZLhT+UYrMonc15/sZlAanQ9fyGsQ1zzraTxW4sy3GsK 55SteBudpS4QhrE+0JmmKnOAm2V6mXhxsmhNa/CxtDi5jeGG/NLR5dlEJzoVl6kY5E9J 9z/Ljl4686uxpjInGdB8hq1ycrlABYSjnD8lLFVdUG4jeXk3vbGmhdRz5aKi5ysxik6W xJw2y9jDLwSGRt2NaMqqxVlkjEMpNaN5+GGj3EYwQpxLf16lU6KEaaDdbhbamm+PCvPA RGAg== X-Gm-Message-State: AOAM533HaJOUdnJnFr4ieV8VBbJPbnBcfIYzgIJFauL1K480X63eO4ff KpUdN8GYJsC5+NV0Ftc+e7R2JdsTV+8oO2JYNm8= X-Received: by 2002:a81:1096:0:b0:2ec:4a46:7e5a with SMTP id 144-20020a811096000000b002ec4a467e5amr5328668ywq.196.1650638314653; Fri, 22 Apr 2022 07:38:34 -0700 (PDT) MIME-Version: 1.0 References: <20220420191541.99528-1-schspa@gmail.com> In-Reply-To: <20220420191541.99528-1-schspa@gmail.com> From: "Rafael J. Wysocki" Date: Fri, 22 Apr 2022 16:38:23 +0200 Message-ID: Subject: Re: [PATCH] cpufreq: fix race on cpufreq online To: Schspa Shi Cc: "Rafael J. Wysocki" , Viresh Kumar , Linux PM , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE 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 Wed, Apr 20, 2022 at 9:16 PM Schspa Shi wrote: > > When cpufreq online failed, policy->cpus are not empty while > cpufreq sysfs file available, we may access some data freed. > > Take policy->clk as an example: > > static int cpufreq_online(unsigned int cpu) > { > ... > // policy->cpus != 0 at this time > down_write(&policy->rwsem); > ret = cpufreq_add_dev_interface(policy); > up_write(&policy->rwsem); > > return 0; > > out_destroy_policy: > for_each_cpu(j, policy->real_cpus) > remove_cpu_dev_symlink(policy, get_cpu_device(j)); > up_write(&policy->rwsem); > ... > out_exit_policy: > if (cpufreq_driver->exit) > cpufreq_driver->exit(policy); > clk_put(policy->clk); > // policy->clk is a wild pointer > ... > ^ > | > Another process access > __cpufreq_get > cpufreq_verify_current_freq > cpufreq_generic_get > // acces wild pointer of policy->clk; > | > | > out_offline_policy: | > cpufreq_policy_free(policy); | > // deleted here, and will wait for no body reference > cpufreq_policy_put_kobj(policy); > } > > Signed-off-by: Schspa Shi > --- > drivers/cpufreq/cpufreq.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index 80f535cc8a75..0d58b0f8f3af 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -1533,8 +1533,6 @@ static int cpufreq_online(unsigned int cpu) > for_each_cpu(j, policy->real_cpus) > remove_cpu_dev_symlink(policy, get_cpu_device(j)); > > - up_write(&policy->rwsem); > - > out_offline_policy: > if (cpufreq_driver->offline) > cpufreq_driver->offline(policy); > @@ -1543,6 +1541,9 @@ static int cpufreq_online(unsigned int cpu) > if (cpufreq_driver->exit) > cpufreq_driver->exit(policy); > > + cpumask_clear(policy->cpus); > + up_write(&policy->rwsem); This change is correct AFAICS, but it doesn't really fix the race, because show_cpuinfo_cur_freq() uses __cpufreq_get() directly without locking. It should use cpufreq_get() instead. > + > out_free_policy: > cpufreq_policy_free(policy); > return ret; > --