Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2546791lqo; Tue, 14 May 2024 01:53:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVwUE1Voaq6TkP+uSvfL4rQ3KVvi/o7W2CLyJgkNbQxyO3HyapbS3nFEsMLAi4OJVLEOh1Kz2f9y36s+2hS4YB4V1d/4dYqUIdTD+Fmuw== X-Google-Smtp-Source: AGHT+IHh21zeJyojbGod8/RYaXjFIRD85H4RQ5FDbPc5TkVDjxeUAJ3KUxpE6ous/Uhe9p8xliG3 X-Received: by 2002:a05:6a00:1911:b0:6f4:1799:c70c with SMTP id d2e1a72fcca58-6f4e02aa518mr13593074b3a.9.1715676789825; Tue, 14 May 2024 01:53:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715676789; cv=pass; d=google.com; s=arc-20160816; b=06cbcP/iJwoTgdxS2HNFZeagUAtAg5IUGrse62Mt6/DWMXRlZ0evtwlDGRHtzOvtf7 88X7ZJZ3ZEvfeMpqMfAckUU0AVtNRh4XbIXRyHZPzJ/Zujvv871UGbisdst6Qhvb+Sce lm8BX4FLuOnPqaVyYCIDghtUL6hnlxbj+bpMJFxp+p+OzaEFEiakNXVZDI0foqO5pI0b 1y6t5Xspyvw0nuDlgkkNmudrN5WaBol4DaAGMz/9EB0oaZirWuhBhOz32QEpKcRwz5IX hE94Lf/vx77RHF86elqd8V9RP2Cui+TvzqPR+LmBshsOBFzGSPQs2fy502c/4PA0GZic 8zQg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:cc:to:dkim-signature :dkim-signature:from; bh=9VtHy4eod18lFlILaxkSyirZif/DH69UWjz2KgwQkT8=; fh=J1/9LvAES89+upWhBlpgk3I2BySwGplL094WyKc3cZ4=; b=gef1jRdT/eefCtNSJJkTFS1r8D4iz87SlIE5WbHTbVh5OHxzuvuJFb4pzFAYQjvvCZ ZjRmxBzpHM7hHbwxujFo/PS1oo/9rsPcNwZ3K+hUDp5WU3PhOjYG90VQ73R5/DmYW5mz zVPwHcziHvSRt+hugn4UJdCfMA5+3MPcYAmHLk+eBaC8fZBzIHxgYgEiLFELmeQPQekn j9aztK97uLn9VDfTYcib94WKxu1HVfzDBP7+qjBX+OvnaGSt5NnMqonP/eQWHIrozRxe 3UQk9ZnUvZnHx9ZiSu7/X8cG3GTzUKbl6fbsR3iPdBbhh5DjqP6gWgag17qBgbfFlI/I ja9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=xAl9MTv7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=4h2nBB2F; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-178469-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178469-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-634103f55fcsi10946680a12.262.2024.05.14.01.53.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 01:53:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178469-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=xAl9MTv7; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=4h2nBB2F; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-178469-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178469-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 68BBE2839A5 for ; Tue, 14 May 2024 08:53:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9E3121104; Tue, 14 May 2024 08:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xAl9MTv7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4h2nBB2F" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0D191D54D for ; Tue, 14 May 2024 08:51:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715676676; cv=none; b=cBCpFW9nFvqgnZyACHPR19HrLqtRhQPV1QTUdK6r7w0nnSE1yW82IwEaCkmkL/wWBlK/CW2FAscyWfHAu31zMAzdVVjYIcDnbRHKvivGtFAaCRNhgEG29yfxsUDzmupzjIAdxoRVk7D3yjLo40XCjYaCQNOhQ422iDtgNIPfRY0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715676676; c=relaxed/simple; bh=xuOfDGpcNy6f8HPvxiZX06FnPzvZC0xsPMZwFFxYgZA=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=Iszil2X6J06Ggg/zDxn2aUtP+BQyrEp6d+8Lvc4hF3DaAE1GljbE+8MBJcGQ3RAgEYNQJIAwo382bshszG8d23V8MtO3q2qwGy8B0pvFSvn99rdi+NiS0HcGStIdyPcKsApM70me1e0ltj+JvlGIjvjqccCx2808y5LopkK00m8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=xAl9MTv7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4h2nBB2F; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1715676673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9VtHy4eod18lFlILaxkSyirZif/DH69UWjz2KgwQkT8=; b=xAl9MTv7ncRejsH9tXbyQSRNZkw9ND9Y0IUX9GIVHba4A9bkNRpifUdKuulTFxXOByrOIy ugZroXa4ASjxtClkfWKJ2+M5sjk2xymnl/JFD0PDpaUZkdLuEoyrzMCkWnoZ+gtd4NfNlC aJU9UZVWnRZCWClg7jvquUw9ENpI6RgrA365syGGD/tN0yZZ8D5gDfcWFFH3s2SnZm/4S4 ANg0AbAd/eWhrqog0FPqntnZ4szL1iEqlSKhftfX8sgIzt9kvbEkBqWhzAzWGWYnv+dqd6 XXT48RYD4c0SMqT9gZFbgCF21gitmh9LwqRIvdqNpCSshjbugz3iS+j+nTgUAQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1715676673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9VtHy4eod18lFlILaxkSyirZif/DH69UWjz2KgwQkT8=; b=4h2nBB2F51Zo/bq+c20rHtxgqs+BynJtk2jxaaEoITt2z6fN1NPuGO0epxBrIh7tXQ/o/d cYW3EIf5UHVZ15DA== To: Justin Stitt , John Stultz Cc: Stephen Boyd , Nathan Chancellor , Bill Wendling , linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH] ntp: safeguard against time_constant overflow case In-Reply-To: References: <20240506-b4-sio-ntp-c-v1-1-a01281aa01ba@google.com> Date: Tue, 14 May 2024 10:51:12 +0200 Message-ID: <871q64n5dr.ffs@tglx> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain 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.