mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Zhang, Rui" <rui.zhang@intel.com>
To: "rafael@kernel.org" <rafael@kernel.org>
Cc: "linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>,
	"Wysocki, Rafael J" <rafael.j.wysocki@intel.com>,
	"daniel.lezcano@linaro.org" <daniel.lezcano@linaro.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] powercap/intel_rapl: Fix handling for large time window
Date: Sat, 11 Feb 2023 03:20:11 +0000	[thread overview]
Message-ID: <f11965a6908c4db51ee5b8b70404c12ecd28c3ea.camel@intel.com> (raw)
In-Reply-To: <CAJZ5v0gtCffDTVi-SoQ0KanPiSZUS3gONX7a-ZHAXQRr97H-EA@mail.gmail.com>

On Thu, 2023-02-09 at 18:06 +0100, Rafael J. Wysocki wrote:
> On Thu, Feb 2, 2023 at 2:25 AM Zhang Rui <rui.zhang@intel.com> wrote:
> > When setting the power limit time window, software updates the 'y'
> > bits
> > and 'f' bits in the power limit register, and the value hardware
> > takes
> > follows the formula below
> > 
> >         Time window = 2 ^ y * (1 + f / 4) * Time_Unit
> > 
> > When handling large time window input from userspace, using left
> > shifting breaks in two cases,
> > 1. when ilog2(value) is bigger than 31, in expression "1 << y",
> > left
> >    shifting by more than 31 bits has undefined behavior. This
> > breaks
> >    'y'. For example, on an Alderlake platform, "1 << 32" returns 1.
> > 2. when ilog2(value) equals 31, "1 << 31" returns negative value
> >    because '1' is recognized as signed int. And this breaks 'f'.
> > 
> > Given that 'y' has 5 bits and hardware can never take a value
> > larger
> > than 31, fix the first problem by clamp the time window to the
> > maximum
> > possible value that the hardware can take.
> > 
> > Fix the second problem by using unsigned bit left shift.
> > 
> > Note that hardware has its own maximum time window limitation,
> > which
> > may be lower than the time window value retrieved from the power
> > limit
> > register. When this happens, hardware clamps the input to its
> > maximum
> > time window limitation. That is why a software clamp is preferred
> > to
> > handle the problem on hand.
> > 
> > Signed-off-by: Zhang Rui <rui.zhang@intel.com>
> > ---
> >  drivers/powercap/intel_rapl_common.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/powercap/intel_rapl_common.c
> > b/drivers/powercap/intel_rapl_common.c
> > index 26d00b1853b4..8b30e5259d3b 100644
> > --- a/drivers/powercap/intel_rapl_common.c
> > +++ b/drivers/powercap/intel_rapl_common.c
> > @@ -999,7 +999,12 @@ static u64
> > rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
> > 
> >                 do_div(value, rp->time_unit);
> >                 y = ilog2(value);
> > -               f = div64_u64(4 * (value - (1 << y)), 1 << y);
> > +               if (y > 0x1f) {
> > +                       pr_warn("%s: time window too large,
> > clamped\n", rp->name);
> 
> IIUC this happens when user space provides a value that is too large.
> Why do you want to log a kernel warning in that case?
> 
Right.
I don't know any API for this purpose, so just removed the warning in
patch v2.

> > +                       return 0x7f;
> 
> Because the target hardware field has 7 bits, the function will
> return
> all ones if the exponent is too large.  It would be good to put a
> comment stating this here.
> 
sure.

thanks,
rui

> > +               }
> > +
> > +               f = div64_u64(4 * (value - (1ULL << y)), 1ULL <<
> > y);
> >                 value = (y & 0x1f) | ((f & 0x3) << 5);
> >         }
> >         return value;
> > --

      reply	other threads:[~2023-02-11  3:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-02  1:31 Zhang Rui
2023-02-09 17:06 ` Rafael J. Wysocki
2023-02-11  3:20   ` Zhang, Rui [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f11965a6908c4db51ee5b8b70404c12ecd28c3ea.camel@intel.com \
    --to=rui.zhang@intel.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael.j.wysocki@intel.com \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®