Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2842933rwd; Mon, 15 May 2023 18:23:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5E+Bw90z33nGfToR16HeuCGBezl7ETTopFd5zddieR4IljAxd7AOqRPHI/jZ7pOl8mL1K1 X-Received: by 2002:a05:6a00:2ea0:b0:648:ebb2:3d6 with SMTP id fd32-20020a056a002ea000b00648ebb203d6mr19958998pfb.26.1684200220527; Mon, 15 May 2023 18:23:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684200220; cv=none; d=google.com; s=arc-20160816; b=0mmwync4d+v3de4anuKQrI770H+0GrMsLEW2LUfS9qUemWsWvN8jmoZBxQ+XMQeWB+ 5ihjTszeMxrsplLEK0L60Jmkj8rISSQRLZ4AVDBG1x+mXB898Ury3BMo/eLVpsFa5YDV Pt47w8ZnYu5XZLIEYYKmC1zssUQLoFxDDqAybLq7IvUHDrPkxmiu7Di5ucjMsQ6+XDxA uXe9rFP0+tKnTGqflsh0ghCQnuwuKMjxN8WaogNnD8tGEAtAYkVhFfOGTprxhNpr1ArF sSaNnHLEGpS2ti18gNdbVQXJt5sptb6NFHyUvJUEixL3fDMbP620bZXaFHXngy1WHtVf XC2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ua1H7BSlxOVgCXi1cqjAb+iXMvrfwZl+3k3+nHoxTf0=; b=YvGGd18HoYZKXRXZDogpen+TjtCbXCGMnpBC6CEr0Jgu7V3TNBWe5O1+CNBZKgxerv BipnL6fdHgZbD5lXigugftzRw7cWjOU0d1yammnSKYH82CLR1Jg04VTVDCY+/AGXjNlG xfeHRUYlolihwP5uP1gxFfX2aFMVCD8vl7s9S7fLmYRE3NUZrW0HW5hUM30x2alLvV8w uDOYw+3NkVDoK69WUmaTWyMEXlwFqde862SJ+gSus585KB99NvGQm3KDt2BCXlYYa2l8 rHaWQEcoj89o244I1/p9nZBikguZrUmX7sALpEStTSsFppECNv10U4Tej61PSeh4T+qp 0lsQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QBbDKsdx; 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=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r27-20020aa7989b000000b00643b6fa7d34si18589973pfl.214.2023.05.15.18.23.27; Mon, 15 May 2023 18:23:40 -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=@linaro.org header.s=google header.b=QBbDKsdx; 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=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245678AbjEPBKH (ORCPT + 99 others); Mon, 15 May 2023 21:10:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242991AbjEPBKG (ORCPT ); Mon, 15 May 2023 21:10:06 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4951455BD for ; Mon, 15 May 2023 18:10:05 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6439bbc93b6so9524196b3a.1 for ; Mon, 15 May 2023 18:10:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1684199405; x=1686791405; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ua1H7BSlxOVgCXi1cqjAb+iXMvrfwZl+3k3+nHoxTf0=; b=QBbDKsdxw/tBLj5Hk9EFNid5818/CeBXuZiv6gmeMwGg7aR4G2fPUjtrWG66Pkzr6R BFPLNNAMEVZ4w94vJWFyJBVOFAW1ywnmCeTIuyAQBGAZnZu8BDy7j2GAMJXkQIpydR0r BkvXHTYhFxDU69IPtD28tbT3bMe40Mi3HVaYl8qmTwbma+N1twu1ku4EKFeiBmzRNC6l mITDtao5b2/HJnIiivZQhxf0oBa9CNI5TrCn6nFxcrJVawy6Nmb6eLCwTT2G8TtzB7we jJcLvcnt//872bs9mpC44wRU2do8MJWeyCXfj2+3O91v3KGmeomPZBP7Fo7F8KdqIv/b ce2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684199405; x=1686791405; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ua1H7BSlxOVgCXi1cqjAb+iXMvrfwZl+3k3+nHoxTf0=; b=TuDujVI+QAjLKxwKFOEh4MhoJcmB7V9Xhv1AZZqRVKSBZyPnXU47awkCSbaaDAyKo4 o06ogrdcz7diX0QAFxO6K978G9+gydyotPXdE7w+rqxNhXdsolmbkO8cgsRN/TJXC22l TXoZq3GY12H8qCfsmQH3o0Z0nJg1gL1CTy9IUj8iD/b8jJ5bVO61l15ng7pAgbMC4Kde NfU+91zIRIwROY8OCzsTE/TFP+AvMGS8iMSucj8HyhTHQwigwMyAdFfTFXf8Rtdk9Smh YScnq8ewlbyLUkFURro8GpkzebBbx0JXiuYvVIhOUYgi5VCWdSYJh7sWUbuEf/vLGlY/ 3hEQ== X-Gm-Message-State: AC+VfDwDxY8Hrx5nySAwyCEDFCiw1tgQ81rqvXnaER++PN+Rbz57Opgs wdWH9bQXluMxaz/kpER3Dv3uww== X-Received: by 2002:a05:6a20:8e24:b0:100:214c:d76e with SMTP id y36-20020a056a208e2400b00100214cd76emr37821002pzj.53.1684199404643; Mon, 15 May 2023 18:10:04 -0700 (PDT) Received: from localhost ([122.172.82.60]) by smtp.gmail.com with ESMTPSA id k4-20020a632404000000b00502e7115cbdsm12313129pgk.51.2023.05.15.18.10.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 18:10:03 -0700 (PDT) Date: Tue, 16 May 2023 06:40:01 +0530 From: Viresh Kumar To: Wyes Karny Cc: ray.huang@amd.com, rafael@kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, gautham.shenoy@amd.com Subject: Re: [PATCH v3 2/2] cpufreq: Warn if fast_switch is not set Message-ID: <20230516011001.epa4xlvbiimu6ai3@vireshk-i7> References: <20230515113404.4259-1-wyes.karny@amd.com> <20230515113404.4259-3-wyes.karny@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230515113404.4259-3-wyes.karny@amd.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 On 15-05-23, 11:34, Wyes Karny wrote: > If fast_switch_possible flag is set by the scaling driver, the governor > is free to select fast_switch function even if adjust_perf is set. When > the frequency invariance is disabled due to some reason governor > fallbacks to fast_switch if fast_switch_possible is set. This could > crash the kernel if the driver didn't set the fast_switch function > pointer. > > This issue becomes apparent when aperf/mperf overflow occurs with > amd_pstate (passive) + schedutil. When this happens, kernel disables > frequency invariance calculation which causes schedutil to fallback to > sugov_update_single_freq which currently relies on the fast_switch > callback. > > Normal flow: > sugov_update_single_perf > cpufreq_driver_adjust_perf > cpufreq_driver->adjust_perf > > Error case flow: > sugov_update_single_perf > sugov_update_single_freq <-- This is chosen because the freq invariant is disabled due to aperf/mperf overflow > cpufreq_driver_fast_switch > cpufreq_driver->fast_switch <-- Here NULL pointer dereference is happening, because fast_switch is not set Not sure if all these details are required for this patch or not. It is logically incorrect to set fast_switch_possible, while fast_switch isn't set. That's a reason enough. > Put up a warning message if the driver sets fast_switch_possible flag > but not fast_switch. > > Signed-off-by: Wyes Karny > --- > drivers/cpufreq/cpufreq.c | 18 ++++++++++++++++++ > include/linux/cpufreq.h | 5 ++++- > 2 files changed, 22 insertions(+), 1 deletion(-) > > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c > index 6b52ebe5a890..180be9235b08 100644 > --- a/drivers/cpufreq/cpufreq.c > +++ b/drivers/cpufreq/cpufreq.c > @@ -501,6 +501,13 @@ void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) > if (!policy->fast_switch_possible) > return; > > + /** Doc style comments aren't required here I guess. > + * It's not expected driver's fast_switch callback is not set > + * even fast_switch_possible is true. > + */ > + if (!cpufreq_driver_has_fast_switch()) > + pr_alert_once("fast_switch callback is not set\n"); > + > mutex_lock(&cpufreq_fast_switch_lock); > if (cpufreq_fast_switch_count >= 0) { > cpufreq_fast_switch_count++; > @@ -2143,6 +2150,17 @@ unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, > } > EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); > > +/** > + * cpufreq_driver_has_fast_switch - Check "fast switch" callback. > + * > + * Return 'true' if the ->fast_switch callback is present for the > + * current driver or 'false' otherwise. > + */ > +bool cpufreq_driver_has_fast_switch(void) Why create a routine for this, when no one else is going to use it ? > +{ > + return !!cpufreq_driver->fast_switch; > +} I think you should add the required check in cpufreq_online(), after cpufreq_driver->init() is called, and return failure if fast_switch isn't set and fast_switch_possible is. -- viresh