public inbox for llvm@lists.linux.dev
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Justin Stitt <justinstitt@google.com>, John Stultz <jstultz@google.com>
Cc: Stephen Boyd <sboyd@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Bill Wendling <morbo@google.com>,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH] ntp: safeguard against time_constant overflow case
Date: Tue, 14 May 2024 10:51:12 +0200	[thread overview]
Message-ID: <871q64n5dr.ffs@tglx> (raw)
In-Reply-To: <zhb5dy6ysbdghj3vhnj2s3gd7fhqg3v7xmhonkgfxp33qtc6bw@pqyhntprkwjr>

On Tue, May 07 2024 at 22:03, Justin Stitt wrote:
> On Mon, May 06, 2024 at 11:02:17PM -0700, John Stultz wrote:
>> > @@ -734,10 +737,10 @@ static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
>> >
>> >         if (txc->modes & ADJ_TIMECONST) {
>> >                 time_constant = txc->constant;
>> > -               if (!(time_status & STA_NANO))
>> > -                       time_constant += 4;
>> > -               time_constant = min(time_constant, (long)MAXTC);
>> > -               time_constant = max(time_constant, 0l);
>> > +               if (!(time_status & STA_NANO) &&
>> > +                   unlikely(LONG_MAX - time_constant_inc >= time_constant))
>> > +                       time_constant += time_constant_inc;
>> > +               time_constant = clamp_t(long, time_constant, 0, MAXTC);
>> >         }
>> 
>> Overall, this looks fine. Though the time_status conditional is now a
>> little unwieldy.
>> 
>> I wonder if some sort of a helper like:
>>       time_constant = safe_add(time_constant, TIME_CONSTANT_INC, LONG_MAX);
>> 
>> Might make this a little easier to read?
>
> How about something like this:
>
> 	if (txc->modes & ADJ_TIMECONST) {
> 		if (!(time_status & STA_NANO))
> 			time_constant = clamp_t(long, txc->constant,
> 						-TIME_CONSTANT_INC,
> 						MAXTC - TIME_CONSTANT_INC) +
> 						TIME_CONSTANT_INC;
> 		else
> 			time_constant = clamp_t(long, txc->constant, 0, MAXTC);
> 	}
>
> We can remove the initial assignment and use some fancy clamps.

That's unreadable TBH.

  reply	other threads:[~2024-05-14  8:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06 22:01 [PATCH] ntp: safeguard against time_constant overflow case Justin Stitt
2024-05-07  6:02 ` John Stultz
2024-05-07 22:03   ` Justin Stitt
2024-05-14  8:51     ` Thomas Gleixner [this message]
2024-05-14  9:17 ` Thomas Gleixner

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=871q64n5dr.ffs@tglx \
    --to=tglx@linutronix.de \
    --cc=jstultz@google.com \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=sboyd@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