Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2153536iof; Tue, 7 Jun 2022 21:32:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyiAit/axzvQZWY+8yeMbgc6Wg45OabPLevGFU9vCn1BrzYdJKTV+YXuHCmfQ4hpGiT/esg X-Received: by 2002:a17:903:32c8:b0:166:3674:b2d4 with SMTP id i8-20020a17090332c800b001663674b2d4mr32378803plr.173.1654662759423; Tue, 07 Jun 2022 21:32:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654662759; cv=none; d=google.com; s=arc-20160816; b=FViFJ5EcpD9DMfqkKvJg81Z2/6duZcE7USHVKGTPDGuxSe176QQYzxV/xJT21O1kp9 qLQTd/gQNTdydZlnHmFHQVxHhLoP1M4yxBs/nDcXFubZcQ0RwI1VQkTn/vZ1q+w/jf9V +/MEVFYzLWwEgBCpDSZRxTtedLThv2CDTwgZEBGriagIAVuaDoUhbToLthdFX84b+wmL njAmt/o8eJcZFc0odAa9j6YuYY/p8L4ZW94dmityvO8Cu7e8MAh6xfW/2PU3U9Cp6u9S H4TigTptTA32DrJ7nlatZ9TZ+Y+Q7L5t9NeAcAbFAkFbHOTpnaJpW8X/XSgyvB2nFX4c FsgQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fesEBAZ2Pf7mpxhJkQFfpiuY+pZd3+E25U5KuJBqQBI=; b=mpcGWVikkr/ZPf0eaK0KGfrtiLVIaEc6U2OL0w5d+RRFmOzc2V+JMJwHjohjiFAelq 3tEF8dkcsq/km/QU9yZBpe52oBx31APJCf6FqkOEKDF7Ff0+p6jAat0hq3VAIYzeNqKc 8JXgLsxnpAHOMxuA4Y0mQ17+RMmsVXIKrKLNlxfo/cBE7aSkY7zMy/ORGZn9bgt/fgrd 5DG00VDuw+TKVM+Abb2sUmO6mZMvcf09ULYHLFWAwoWOihVxBA2v8MgLUp/VKJAsXrq1 GfvozpNjzPEVN8qZFjhHbitk05M2mQSPsBYBI5jdaBkunVPD0qtHznkwFTbQgOxLAcij u6Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=an7TzVJ9; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id iw6-20020a170903044600b0015f0063568bsi23184404plb.340.2022.06.07.21.32.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:32:39 -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=@linuxfoundation.org header.s=korg header.b=an7TzVJ9; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AAC0D42B0AA; Tue, 7 Jun 2022 21:01:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348662AbiFGRt7 (ORCPT + 99 others); Tue, 7 Jun 2022 13:49:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346849AbiFGR3x (ORCPT ); Tue, 7 Jun 2022 13:29:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F089660E9; Tue, 7 Jun 2022 10:25:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8704960906; Tue, 7 Jun 2022 17:25:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A152C385A5; Tue, 7 Jun 2022 17:25:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654622709; bh=6WufMX3W+3Mz4YL4KNh5xMOCfFOf8Su4lg0ID/P/nmY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=an7TzVJ9ff5KRKdAKIp5sBd6eeAaO1qZNBh8ySFRnuE6WVhr8jwuspCxqLsum3RaL u4F/xCaIqgAWvRqlpQWtpvomHu382hdaikeNtXUZSJsRmnsnmkSRLxisvaUWZ8/JO+ 90WKAkmWLY3VOZhUDI9lJqieFC5nCPWBwfv3YotI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Schspa Shi , "Rafael J. Wysocki" , Sasha Levin Subject: [PATCH 5.10 154/452] cpufreq: Fix possible race in cpufreq online error path Date: Tue, 7 Jun 2022 19:00:11 +0200 Message-Id: <20220607164913.148292956@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164908.521895282@linuxfoundation.org> References: <20220607164908.521895282@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Schspa Shi [ Upstream commit f346e96267cd76175d6c201b40f770c0116a8a04 ] When cpufreq online fails, the policy->cpus mask is not cleared and policy->rwsem is released too early, so the driver can be invoked via the cpuinfo_cur_freq sysfs attribute while its ->offline() or ->exit() callbacks are being run. 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); } Address this by modifying cpufreq_online() to release policy->rwsem in the error path after the driver callbacks have run and to clear policy->cpus before releasing the semaphore. Fixes: 7106e02baed4 ("cpufreq: release policy->rwsem on error") Signed-off-by: Schspa Shi [ rjw: Subject and changelog edits ] Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- 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 30dafe8fc505..3540ea93b6f1 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1515,8 +1515,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); @@ -1525,6 +1523,9 @@ static int cpufreq_online(unsigned int cpu) if (cpufreq_driver->exit) cpufreq_driver->exit(policy); + cpumask_clear(policy->cpus); + up_write(&policy->rwsem); + out_free_policy: cpufreq_policy_free(policy); return ret; -- 2.35.1