Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2114059iof; Tue, 7 Jun 2022 20:12:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHMhqb8G4QTBD5AwzkKNx8CFsLeLdVPN+1e9klTSGy4S7D01UMZwj/9mNItOB3PwklZkVx X-Received: by 2002:a17:90a:e68a:b0:1e3:252f:24e0 with SMTP id s10-20020a17090ae68a00b001e3252f24e0mr46976068pjy.122.1654657930580; Tue, 07 Jun 2022 20:12:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654657930; cv=none; d=google.com; s=arc-20160816; b=TcnT+KN55FamDMsH4darOCwKhJHq8hKijpDrIKQrY95DKpmSaM71Rs2B//0JXKeEOt vaAEMJ1v/B5vfbit1Wgy1E4K9tiTGlqLlYzILEstmZ/fcQpA1samCN4tGlcs100VGIIA +V2AsZN+DcIdei0W84dZaHABi/2oECQ/zXexchMKUjBeJUqyCT+J8QBzz/ridgSd944X NXIBziQttt+ezX9+OXNK6PtNT9rBaK6adPF/MclRtT6miPJz3OSWcn5QWwLCzWXB5aZM Kzrly7n/M/QgWJxwE4u24uwjDoenoRtWgIzHPav6ff84ertlWtHDpmFjJ4Jdl8oH69DC wnHg== 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=nnTEUkCDNnx8c7O5IA3qhXAaPy+Wl6wYrrwyQcd0Zjk=; b=JGqeJ+IqN4XdBl5wXSl8kc4UhMMrGt3S2XYSJq5U3m5Tn0XzCN8MykspdybEJYZXCb bOlOkKfgbid2tMVHwE2Rqh9pRZAnfBYongTWXCg/WS5kxTY4v6P0Vl0GQ8vZQvVK92ow BxBmkeeKwtJ99AGIsR0JWMb3OFEBVctYHt0UU5n8D0Wm6v2+zjHEyxVNhM2FaoHgMmOR T70URVgPxqVe+1x5ZuqVePVibaUYvuPUlHD6zgG9vvdACtsKL8Xq1RoJCY0KXqRRx48v Uh+fm5xG3ellKnO0wvN2z+YICP3Zc1DGRtbVfpnqZ3IUBnAHrleOLyJb9gleyvsAJR1+ kw7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RVIeWXH5; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d184-20020a6368c1000000b003fd023128f8si21195037pgc.851.2022.06.07.20.12.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 20:12:10 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RVIeWXH5; 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=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 88C5937B503; Tue, 7 Jun 2022 19:17:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384785AbiFGWgU (ORCPT + 99 others); Tue, 7 Jun 2022 18:36:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378647AbiFGVX4 (ORCPT ); Tue, 7 Jun 2022 17:23:56 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 211C6227CC0; Tue, 7 Jun 2022 12:00:53 -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 ams.source.kernel.org (Postfix) with ESMTPS id 4BADAB8220B; Tue, 7 Jun 2022 19:00:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F2B5C385A5; Tue, 7 Jun 2022 19:00:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654628451; bh=owJ9BugnB1NO6H2htMXN/OwXTT9AtJOgxw4uMNv8eg8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RVIeWXH51Ec0BoHHuxPGp2EdRlTzAtVwbsvwGtVMYD+D6XJ1YfmAH706u+TZHUQK5 5/rj6kRtREPK5pfPAY+q1lNUzSW45lK//tYJwT3aQHNs29QHvwQfqQ2bqMVowwatws Y826bmzx/RNMvY2fTLgAQ6bibfZvibj1TZNYDSvs= 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.18 335/879] cpufreq: Fix possible race in cpufreq online error path Date: Tue, 7 Jun 2022 18:57:33 +0200 Message-Id: <20220607165012.581142596@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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 fbaa8e6c7d23..233e8af48848 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -1534,8 +1534,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); @@ -1544,6 +1542,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