Message ID | 20230329133600.908723-1-yajun.deng@linux.dev |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp437231vqo; Wed, 29 Mar 2023 07:03:56 -0700 (PDT) X-Google-Smtp-Source: AKy350aNoDpF3vnRjzEn2xPuh1r2hQBfgeJZfBOuhxNieghve7RFZUCMh7f2Y6Phu05r/st7kDey X-Received: by 2002:a17:90a:4618:b0:23b:28a7:7dc2 with SMTP id w24-20020a17090a461800b0023b28a77dc2mr22379068pjg.5.1680098635917; Wed, 29 Mar 2023 07:03:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680098635; cv=none; d=google.com; s=arc-20160816; b=zPOD4+apZ/ADrT3cOj6545MJ5SWU1A10risReqg/Ra+2omSjNteNh3I9RXEYIRTCo4 DSQLy4OqmBvL0abeyNPoQ1q3pnsahKwUt7zCcmDIoYF4D++6/dgZXw1Hl7xzgtz556x/ DLq3kn3hxoyF+QzavBGt0N7QIlxNY8tQW6HuoKLA9Pw9ykkQGaAmnLoI/jOsLIz5/Sr4 X+YLPL2HERqf7JT+EYJpqxo6kO4VYFqx1bsljB4I9ythwxlctHjepeygIPAbUmYUCdb8 vaBAwtOCPVN6bbIzFj6psCCBAJWGBix4uJBBRaVLI6QBhiKbjQvf6ChW8V8Sk5ixxOXa C8Sw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=UiPBpxfgqtblG7e87/gO/rTDIyusVpWOK3LN9PbGo4M=; b=rZZNDN5kfWM6DNGFsVz1MQJKoC2w17lO24H6PrQlgJGhAoRqFGyBQRYNeVAU2f/gjH BCBj5sVvdzaJUL9/fR1faNxMaDgWEPht9c3l+v4XVhPNDrCXixr7VPLB4TInOy+CsLPP EoVzxwXdHrX9hqGMCzZgkIvOLiTe+etGSPbCwwQM9bf5ucx0K6GXnnj0gg+vvQp4zFXQ NOCVED9f8m0ePY0FF3N9NQb/mwVLskGplDRSUX4VSjCuzhdjjoHEKO8i0jPnRYYfcUCz bPhtMZYZCdHi0yHej8+c3zHxf8+AWlRLSehIgDYp/rZW6txRFVA4STPXFUB0jmcO9oJH U8OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=eOOy+X26; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t17-20020a17090ad15100b0023f37564a38si1454015pjw.177.2023.03.29.07.03.41; Wed, 29 Mar 2023 07:03:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=eOOy+X26; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230354AbjC2Ngh (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Wed, 29 Mar 2023 09:36:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbjC2Ngg (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Mar 2023 09:36:36 -0400 Received: from out-46.mta0.migadu.com (out-46.mta0.migadu.com [IPv6:2001:41d0:1004:224b::2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0B2549DF for <linux-kernel@vger.kernel.org>; Wed, 29 Mar 2023 06:36:32 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1680096990; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=UiPBpxfgqtblG7e87/gO/rTDIyusVpWOK3LN9PbGo4M=; b=eOOy+X26B7Q/Qcfu70uyttKgSCXxeLseZzn7JN7s8AO4yC9+Uo7gov+4GrGwUSCBsibb0A XWMtcwpFIykKphDfIpGJGTlk09NJHo6tok2qWscH1ZBlQq7z+ou4VeqPnGMdwUXQVUS5Oe FOWa45iFSfbigpVe3u5fYpwqWJ+THzo= From: Yajun Deng <yajun.deng@linux.dev> To: rafael@kernel.org, viresh.kumar@linaro.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Yajun Deng <yajun.deng@linux.dev> Subject: [PATCH] cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target() Date: Wed, 29 Mar 2023 21:36:00 +0800 Message-Id: <20230329133600.908723-1-yajun.deng@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761711107562166705?= X-GMAIL-MSGID: =?utf-8?q?1761711107562166705?= |
Series |
cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()
|
|
Commit Message
Yajun Deng
March 29, 2023, 1:36 p.m. UTC
__resolve_freq() may be return target_freq if policy->freq_table is
NULL. In this case, it should return -EINVAL before __target_index().
Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
drivers/cpufreq/cpufreq.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote: > > __resolve_freq() may be return target_freq if policy->freq_table is > NULL. In this case, it should return -EINVAL before __target_index(). Even so, __target_index() itself doesn't dereference freq_table AFAICS, so arguably the driver should be prepared to deal with a NULL freq_table which comes from it after all. Or, if you want to argue that drivers providing ->target_index() must also provide freq_table that is not NULL, a check to that effect needs to be done at the driver registration time IMO. > Signed-off-by: Yajun Deng <yajun.deng@linux.dev> > --- > drivers/cpufreq/cpufreq.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index c0e5be0fe2d6..308a3df1a940 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, > return cpufreq_driver->target(policy, target_freq, relation); > } > > - if (!cpufreq_driver->target_index) > + if (!cpufreq_driver->target_index || !policy->freq_table) > return -EINVAL; > > return __target_index(policy, policy->cached_resolved_idx); > --
March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote: > On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote: > >> __resolve_freq() may be return target_freq if policy->freq_table is >> NULL. In this case, it should return -EINVAL before __target_index(). > > Even so, __target_index() itself doesn't dereference freq_table > AFAICS, so arguably the driver should be prepared to deal with a NULL > freq_table which comes from it after all. > But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' in __target_index(), if driver doesn't provide freq_table, __target_index() will fault before the driver itself. > Or, if you want to argue that drivers providing ->target_index() must > also provide freq_table that is not NULL, a check to that effect needs > to be done at the driver registration time IMO. > >> Signed-off-by: Yajun Deng <yajun.deng@linux.dev> >> --- >> drivers/cpufreq/cpufreq.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c >> index c0e5be0fe2d6..308a3df1a940 100644 >> --- a/drivers/cpufreq/cpufreq.c >> +++ b/drivers/cpufreq/cpufreq.c >> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, >> return cpufreq_driver->target(policy, target_freq, relation); >> } >> >> - if (!cpufreq_driver->target_index) >> + if (!cpufreq_driver->target_index || !policy->freq_table) >> return -EINVAL; >> >> return __target_index(policy, policy->cached_resolved_idx); >> --
On 30-03-23, 01:39, Yajun Deng wrote: > March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote: > > > On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote: > > > >> __resolve_freq() may be return target_freq if policy->freq_table is > >> NULL. In this case, it should return -EINVAL before __target_index(). > > > > Even so, __target_index() itself doesn't dereference freq_table > > AFAICS, so arguably the driver should be prepared to deal with a NULL > > freq_table which comes from it after all. > > > > But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' > in __target_index(), if driver doesn't provide freq_table, __target_index() > will fault before the driver itself. Driver must provide a freq table here.
On Thu, Mar 30, 2023 at 5:57 AM Viresh Kumar <viresh.kumar@linaro.org> wrote: > > On 30-03-23, 01:39, Yajun Deng wrote: > > March 29, 2023 10:21 PM, "Rafael J. Wysocki" <rafael@kernel.org> wrote: > > > > > On Wed, Mar 29, 2023 at 3:36 PM Yajun Deng <yajun.deng@linux.dev> wrote: > > > > > >> __resolve_freq() may be return target_freq if policy->freq_table is > > >> NULL. In this case, it should return -EINVAL before __target_index(). > > > > > > Even so, __target_index() itself doesn't dereference freq_table > > > AFAICS, so arguably the driver should be prepared to deal with a NULL > > > freq_table which comes from it after all. > > > > > > > But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' > > in __target_index(), if driver doesn't provide freq_table, __target_index() > > will fault before the driver itself. > > Driver must provide a freq table here. OK, so let's do the check when the driver gets registered.
On 29-03-23, 21:36, Yajun Deng wrote: > __resolve_freq() may be return target_freq if policy->freq_table is > NULL. In this case, it should return -EINVAL before __target_index(). > > Signed-off-by: Yajun Deng <yajun.deng@linux.dev> > --- > drivers/cpufreq/cpufreq.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index c0e5be0fe2d6..308a3df1a940 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, > return cpufreq_driver->target(policy, target_freq, relation); > } > > - if (!cpufreq_driver->target_index) > + if (!cpufreq_driver->target_index || !policy->freq_table) > return -EINVAL; Hi, I have sent an alternate patch [1] for this, please try it.
April 3, 2023 12:11 PM, "Viresh Kumar" <viresh.kumar@linaro.org> wrote: > On 29-03-23, 21:36, Yajun Deng wrote: > >> __resolve_freq() may be return target_freq if policy->freq_table is >> NULL. In this case, it should return -EINVAL before __target_index(). >> >> Signed-off-by: Yajun Deng <yajun.deng@linux.dev> >> --- >> drivers/cpufreq/cpufreq.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c >> index c0e5be0fe2d6..308a3df1a940 100644 >> --- a/drivers/cpufreq/cpufreq.c >> +++ b/drivers/cpufreq/cpufreq.c >> @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, >> return cpufreq_driver->target(policy, target_freq, relation); >> } >> >> - if (!cpufreq_driver->target_index) >> + if (!cpufreq_driver->target_index || !policy->freq_table) >> return -EINVAL; > > Hi, > > I have sent an alternate patch [1] for this, please try it. > Thanks, v2 is fine. > -- > viresh > > [1] > https://lore.kernel.org/all/53d4ed4e5b18a59a48790434f8146fb207e11c49.1680494945.git.viresh.kumar@lin > ro.org
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c index c0e5be0fe2d6..308a3df1a940 100644 --- a/drivers/cpufreq/cpufreq.c +++ b/drivers/cpufreq/cpufreq.c @@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy, return cpufreq_driver->target(policy, target_freq, relation); } - if (!cpufreq_driver->target_index) + if (!cpufreq_driver->target_index || !policy->freq_table) return -EINVAL; return __target_index(policy, policy->cached_resolved_idx);