Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp925967pxb; Fri, 22 Apr 2022 14:30:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTSVIrzakLj9cmXXNPiKGCTnynuyK1LwdPiq5DaeKs9pCn+dG+60bV4kkkSfV1pw9Cvry1 X-Received: by 2002:a63:6987:0:b0:3aa:eec1:1587 with SMTP id e129-20020a636987000000b003aaeec11587mr804408pgc.144.1650663009848; Fri, 22 Apr 2022 14:30:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650663009; cv=none; d=google.com; s=arc-20160816; b=bFbGkWw1mlZVK2q9Mjtnd/sPPdXEdcBmacgigStH5FiiMu23KkDSOvkbhHeLoWwg5P 6jQQ+5h7kcGIItxSoF1WLPRH7Ao+mYK6He4IqbjICNk+GqA9md8V7yKQgo4euDBTikRB 2MtR1r2UOsV65fihc+fazYEGwA74P7EmGg/KixQNiRstzMfwJsuhtsnG1UQztoN+B0WW inUXvOLEZERq1ppe/ErPWcDQjOabhOMHrnJ5j3AN8H6WLmIPK8ah+voKLn4nFPLZaLX3 UAeOOcneOLWvYQa89OG7Kf9aZ9houOO0YBh89AsYnnexTU188u9gmtdfZb5/o+LYrek8 clLg== 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:dkim-signature; bh=oDJ25/TckqjulH3Kz4Oc6VlKVfCbM4oJIYil6OeGKgs=; b=UygGqv8H8x2C3KTyPHqaG/lV0QYYgqS+tOIpKzJziqItkPXnwK8X0thKmTZWzSZTsB 1kOpEDGZH+ZXlRRcjrvfccZWkNNIb/A/6Up682Vz531fZnBO7b1T5JDG6Ng1UegfGRyO fGO75fK2r/iILZ0Q91lxc6NADNR76LT+ayQ/QiigxU6JTjceUUDbfu4IwAn5DSMIYMco FuxQLXre6pQMf18prtbWEeLhJ6pdQTUI1oBJEoyCaHS2QaBPPDk/3M6b0F178YEeK6/a CqsuPTB4qw9R6SAHTcEOolhkp8kyBN9MQTuRstLlLNz60fMcKS7AdJHz0Ghx18bZBm/z hqUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qf+tYKgk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u8-20020a632348000000b003aa8919b7fbsi6470717pgm.800.2022.04.22.14.30.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 14:30:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qf+tYKgk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D2F311F9E14; Fri, 22 Apr 2022 12:37:36 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1449278AbiDVPOB (ORCPT + 99 others); Fri, 22 Apr 2022 11:14:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354558AbiDVPNs (ORCPT ); Fri, 22 Apr 2022 11:13:48 -0400 Received: from mail-vk1-xa2b.google.com (mail-vk1-xa2b.google.com [IPv6:2607:f8b0:4864:20::a2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A88325D5D1; Fri, 22 Apr 2022 08:10:51 -0700 (PDT) Received: by mail-vk1-xa2b.google.com with SMTP id bc42so3956664vkb.12; Fri, 22 Apr 2022 08:10:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oDJ25/TckqjulH3Kz4Oc6VlKVfCbM4oJIYil6OeGKgs=; b=qf+tYKgkgctIGlv3XQUSJG8kmbBYRFP1CtoIpeLiC3ROiDcWqMZc5fwfeNsHvQaOIv GrttfdFBuJ88iHN/2Cp+jZO1yFJQb4VBt0XkdNBuoO2QLu2GlDK0Pqaa0yGMKlV4D2pI 1TuyvXqOg+zlLW1zlju9UgIaZUAFphl6N1RQzBzDbQzoo4c7Vhap80K/NKLPQuRFGD2w Ee8q7f3tiEQU8igesrCEsEWbPPsBQnln8TM1M0E4osrUKR7ANN2eF8Brc1J1U+bM0UBg s4bYXKtR+lHbQ9s3jiEqRR10WLsQdgAY/L4zu/Y7X6tgoRMCFCpR7uSpIg0k3HQCIje9 6M8g== 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=oDJ25/TckqjulH3Kz4Oc6VlKVfCbM4oJIYil6OeGKgs=; b=RtBJ7lq/8gARipWQj7Ce1LGSxvtKgXUSFKCnXyg6uer8Iefen8NOVOCiArAxuV23ps CP/XlCSFT1pzUbld4igWzkxdW6RR2mRJ3FUsBgo5EcJEx+FhvJf1buoN+cZp3Eff6RZi ohDUCeTxPB3ITtYpoCY/Vk7dG9bS2aY6PAhiesWRPba1buB8n3cv2HMR7jI66AUOwlGq oXd4iBJ8ZzQWYieLVGD+HiRN296AI4lp9Ob7wlIUf6XXWPQRHGPNMoK2kquZkvR7Mhjd OtOjTLT9/Zc7gIFDjCuFimBCJD33uGV+83Z9X0UQVuoWfNiwU2sAYdUDY+5oktZ46gaJ E+zA== X-Gm-Message-State: AOAM533CZDhMQF5xLiL2pftRYQmr014GIY0fzITBLd+mLbwp0S3Si0lL S5PH0WzDU9/WAiOyS+aFTlf/gQScDnklYPR4UJrw1abz X-Received: by 2002:a1f:2494:0:b0:348:491:d046 with SMTP id k142-20020a1f2494000000b003480491d046mr1967180vkk.22.1650640250738; Fri, 22 Apr 2022 08:10:50 -0700 (PDT) MIME-Version: 1.0 References: <20220420191541.99528-1-schspa@gmail.com> In-Reply-To: From: Schspa Shi Date: Fri, 22 Apr 2022 23:10:39 +0800 Message-ID: Subject: Re: [PATCH] cpufreq: fix race on cpufreq online To: "Rafael J. Wysocki" Cc: Viresh Kumar , Linux PM , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,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 "Rafael J. Wysocki" writes: > 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. There is a lock outside of show_cpuinfo_cur_freq(). Please check about static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) { ...... down_read(&policy->rwsem); ret = fattr->show(policy, buf); up_read(&policy->rwsem); ...... } > > It should use cpufreq_get() instead. > >> + >> out_free_policy: >> cpufreq_policy_free(policy); >> return ret; >> -- -- Schspa Shi BRs