Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp53042pxb; Tue, 12 Apr 2022 16:34:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzHdqkkt2wsFTz9K7eNKScDEBb65+7EOEjyeC7z5y3wdHK12sybauJhwboCwdKvWwTcgDFh X-Received: by 2002:a17:90a:e7cc:b0:1cb:be43:2be with SMTP id kb12-20020a17090ae7cc00b001cbbe4302bemr7700552pjb.10.1649806477349; Tue, 12 Apr 2022 16:34:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649806477; cv=none; d=google.com; s=arc-20160816; b=rAsL2AZ4MgE/fkqUGACXp+CgYpXWuCFcs30Eq4b1owNk74ctDY5/1L2bZAF1h+mUHZ I323iNwe1/748pQlDyXln5kfPhlVfdOrQoEKJU5NHmnvymfvjWEnr0P24K3Lx27IlDJi vpcHFbuQ8N2JemD47mTj90oZK6piyEKEW0MUpFJFUd14/F8165a3y3tzBxjIPRHIJY1P t7W2sXlA2jS5lI5BGB4cbbY+7xlz4Rn6yB1eAIa9FTJfjlaehJCrwf9HbC4ywYzHy0qC 8aoBiWyeW5nfpdIN6b+TBBTXiUE4/hhMg/Vtn6jIgc41gRreuV6EMpylGwxfOQ48eV83 ia9A== 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=u8+/aml9Ly7lh98O8cN4uCkRZLHm7WhoqFafyguxIEE=; b=UzY9XTNU0mgAY4+skHeLUvyL8l+0Catx/CYzo9nUk3wsNhS+plsy2lpHE3oO4UHfO7 MIWs2xcINRiyBIWmcJjGY1NqbA4k0yMhbMOCdTHouKYJYA7+pFZ4AuFzo326dGTW6zxM iIn7rUCTZDO+UMa3W5UtkoyzH2PUs1ZK6qGzhyJB8LVdCda8DDfoipJCHUIqQqlcd2JM MElqmG8ULMrerSB+L3Ex2fQW4pyJMo75oR8Xkei92ZJ5EgLXqskUWQ5Ee8JP2f/g/q2v SjVyJk+BmbFgOyp3lwVjBAclj1opUBhf6ZWD4+KRtrVYOX2banlL8jnkBYn25pUzfXlJ hvTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="arCca/9F"; 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 f4-20020a63dc44000000b0039d91c7f0c6si3310060pgj.16.2022.04.12.16.34.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:34:37 -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="arCca/9F"; 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 ED9A41B2C68; Tue, 12 Apr 2022 14:25:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243870AbiDLGqR (ORCPT + 99 others); Tue, 12 Apr 2022 02:46:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350627AbiDLGmj (ORCPT ); Tue, 12 Apr 2022 02:42:39 -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 81EB717A8A; Mon, 11 Apr 2022 23:36:31 -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 E354F61910; Tue, 12 Apr 2022 06:36:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C112CC385A6; Tue, 12 Apr 2022 06:36:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649745390; bh=l7KNRLuVOLNODXmB2fEIuwIo/lDJusxB1x0vOX/egec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=arCca/9FBx4+X560Yrc/eN1UgOXqzv3TrzcZIwNRIpfTtIJJJ5Le+mDHVAf5j4a6C J6gn1FkiV6mgEPcP3QnAza1cDFnitMNet4mcPaGzh2/PD1X7MnNwQc5ruF3NP4hzpk aRoCCN3c69IoZGbKbhEG+nAwQcdIvNwKr7uC39dI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Maxime Ripard , Stephen Boyd , Sasha Levin Subject: [PATCH 5.10 081/171] clk: Enforce that disjoints limits are invalid Date: Tue, 12 Apr 2022 08:29:32 +0200 Message-Id: <20220412062930.230828098@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062927.870347203@linuxfoundation.org> References: <20220412062927.870347203@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=-2.0 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=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 From: Maxime Ripard [ Upstream commit 10c46f2ea914202482d19cf80dcc9c321c9ff59b ] If we were to have two users of the same clock, doing something like: clk_set_rate_range(user1, 1000, 2000); clk_set_rate_range(user2, 3000, 4000); The second call would fail with -EINVAL, preventing from getting in a situation where we end up with impossible limits. However, this is never explicitly checked against and enforced, and works by relying on an undocumented behaviour of clk_set_rate(). Indeed, on the first clk_set_rate_range will make sure the current clock rate is within the new range, so it will be between 1000 and 2000Hz. On the second clk_set_rate_range(), it will consider (rightfully), that our current clock is outside of the 3000-4000Hz range, and will call clk_core_set_rate_nolock() to set it to 3000Hz. clk_core_set_rate_nolock() will then call clk_calc_new_rates() that will eventually check that our rate 3000Hz rate is outside the min 3000Hz max 2000Hz range, will bail out, the error will propagate and we'll eventually return -EINVAL. This solely relies on the fact that clk_calc_new_rates(), and in particular clk_core_determine_round_nolock(), won't modify the new rate allowing the error to be reported. That assumption won't be true for all drivers, and most importantly we'll break that assumption in a later patch. It can also be argued that we shouldn't even reach the point where we're calling clk_core_set_rate_nolock(). Let's make an explicit check for disjoints range before we're doing anything. Signed-off-by: Maxime Ripard Link: https://lore.kernel.org/r/20220225143534.405820-4-maxime@cerno.tech Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/clk.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 92fc084203b7..2e56cc0a3bce 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -631,6 +631,24 @@ static void clk_core_get_boundaries(struct clk_core *core, *max_rate = min(*max_rate, clk_user->max_rate); } +static bool clk_core_check_boundaries(struct clk_core *core, + unsigned long min_rate, + unsigned long max_rate) +{ + struct clk *user; + + lockdep_assert_held(&prepare_lock); + + if (min_rate > core->max_rate || max_rate < core->min_rate) + return false; + + hlist_for_each_entry(user, &core->clks, clks_node) + if (min_rate > user->max_rate || max_rate < user->min_rate) + return false; + + return true; +} + void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate, unsigned long max_rate) { @@ -2332,6 +2350,11 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) clk->min_rate = min; clk->max_rate = max; + if (!clk_core_check_boundaries(clk->core, min, max)) { + ret = -EINVAL; + goto out; + } + rate = clk_core_get_rate_nolock(clk->core); if (rate < min || rate > max) { /* @@ -2360,6 +2383,7 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) } } +out: if (clk->exclusive_count) clk_core_rate_protect(clk->core); -- 2.35.1