Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp538630lqc; Fri, 8 Mar 2024 05:17:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWf0IKJeyXMpDxc+USu8DDFgPidNcLGsYzxAWtkgCtrEL/3hXUlBQWvHN7FTUklhynZDRRVpVxs/k1qMHkn4sGXprr7VbmKmBnJCLuWZA== X-Google-Smtp-Source: AGHT+IHtznnn1cvhLE7q/FjLQd0GzPGwPvg3Em1ujm9ncbxo6mzpGkV41HkMyschJT1O6coMUoRR X-Received: by 2002:a17:90a:2ec8:b0:29b:2eab:6bda with SMTP id h8-20020a17090a2ec800b0029b2eab6bdamr17336094pjs.35.1709903854826; Fri, 08 Mar 2024 05:17:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709903854; cv=pass; d=google.com; s=arc-20160816; b=sr1EdgmX1MFofPVRlGFiWnxcn/n96G9nhzrmfRHT/li8i0zHaaGxQtfqZ5NKRT8Kd4 W31qPyIf/rqK6iAgCrBlMzRLm/wm7K5F0L1A5FlAbw3NvDVn1Qp/seKJ5+thmWDmWufm HBYPSbIhP2XEoUp6V65l4uLW7DwQ8iEI5wUn+6WVOHBA3WFiCj5ZzZiFALhZdzRYVlRD KZdp7121LRuVqaJJZ/2t0fnmZf3ZSokgOAOzdUHvf65xbhduvcRbLF6NCiSOdlQBFeSy qXJfgLD7601Rda45rsQEHQmOB555Bp4BvtKU3FYFanIKpUX/jkdyEWveT0e8yXYjdL3+ u6dg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:organization:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=2qHAiXKICf3XnrS//OXM1tGkuqQDB1YYpAgYq0s6+6U=; fh=wgq3SFFcpYoLsYNr1Fy+HYH2+WGNJgTNu+AnKLa6P2U=; b=tZDsf9xgp/YSUBKnmAtHdRqwkAzNbfFgP/dIJiGRb8jzVOWI0OqX5NcRXN/f3/Cpmz YwFUSlbeIgsEjqHOrqQlrJ8nPKlMax9r22Y0huWovXL08sRKtohsnEV+CA2Azq2qVqvE Dpv+Ef2TZzMqO8WBf/myNSeXpkQX+1tSKd+vHs67BKGD0emhMr0MonvdJ1KRvbS8HTF5 IFl8olN/1VboyNkQJFp8I2V3dEmB3iO6lmrV4TjcWbtbAc8dzK5wmFKT0jr3LOV0Ghs2 DPDFI/X9A+RjFvvCFkp91NSf2/V/c+nln2PiEXffR7/6C9apntQVgK9IAz1AUZdbYwUn OK0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=diYBaxQV; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-97050-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97050-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l13-20020a17090a72cd00b0029b7e8a506asi3770626pjk.134.2024.03.08.05.17.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 05:17:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97050-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=diYBaxQV; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-97050-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97050-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com 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 1359F281946 for ; Fri, 8 Mar 2024 13:17:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EA9683BBF9; Fri, 8 Mar 2024 13:16:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="diYBaxQV" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 797FC3BBE0; Fri, 8 Mar 2024 13:16:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709903778; cv=none; b=VUSEHVgXS4Np+paUL73vwoFt8/EDbAOp2BvqOPvxRis3ZPf/8aWUR7xj4avQ3KykPTAFA8ODIHvie72tAu1Vok9DdpAU2ljWuOZC0In25yTy3Hv6DmVCbnMTRzTLcY/Rj5zzsstuCpQcorgJYBCS38U/+SRrrXX1y4shGUKGPqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709903778; c=relaxed/simple; bh=Zjh6IUndlImHk90vhCoelXOI1d7rMagiowI5tzl77NQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eUaoqblTdJwU92vTP02KtVICQflYuaraO4IcXoJszNAmogrve51GNccufOE+rK9xUZelpCZDNE/37qbu0GZJPDXYi++9dtxmb1F+J/6Q9z25Mn3xXJ73IW+SgFsb+Cwo0fzXZSSjpYbdor/DSxTCjIUPesb2vux5QdY4raJPUa4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=diYBaxQV; arc=none smtp.client-ip=192.198.163.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709903777; x=1741439777; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zjh6IUndlImHk90vhCoelXOI1d7rMagiowI5tzl77NQ=; b=diYBaxQV8nYBFa2FFhkrINNiKgQELbj4e+fe0P+H0Cs9EG0kLwXn6hEJ X7h/vL0ufYtVdvrfxAteRW3u8LIzHAiCLyrboyRD5kz7OsU/8xycBimXs HPovD1cht86BtfK3WtiRZDx7HlW7Q9YMF5IG7CQ95pH77lFxfxmKpXzJe KL+3EnrywT9BjEZ8JMNHlw2xugwdQ4GVz8hFKFx5KTts1k67UpxZQ5Tv4 CuZU2vXiCFmNbSJyzOxJdGcBumA/yxbNWCmFsb/xFE3w4wO51rYHCnEGd JR8r7prvCZME+vCPH7z5UmD4y01mOS1H1COKC8UrwS9v05kcH5RDPZyP2 w==; X-IronPort-AV: E=McAfee;i="6600,9927,11006"; a="15342569" X-IronPort-AV: E=Sophos;i="6.07,109,1708416000"; d="scan'208";a="15342569" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 05:16:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,109,1708416000"; d="scan'208";a="15161445" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.46.63]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2024 05:16:09 -0800 From: Adrian Hunter To: Thomas Gleixner Cc: Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Vincenzo Frascino , John Stultz , Stephen Boyd , Peter Zijlstra , Randy Dunlap , Bjorn Helgaas , Arnd Bergmann , Anna-Maria Behnsen , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH 07/19] vdso: Make delta calculation overflow safe Date: Fri, 8 Mar 2024 15:15:00 +0200 Message-Id: <20240308131512.44324-8-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240308131512.44324-1-adrian.hunter@intel.com> References: <20240308131512.44324-1-adrian.hunter@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: 8bit Kernel timekeeping is designed to keep the change in cycles (since the last timer interrupt) below max_cycles, which prevents multiplication overflow when converting cycles to nanoseconds. However, if timer interrupts stop, the calculation will eventually overflow. Add protection against that, enabled by config option CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT. Check against max_cycles, falling back to a slower higher precision calculation. Suggested-by: Thomas Gleixner Signed-off-by: Adrian Hunter --- lib/vdso/gettimeofday.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/vdso/gettimeofday.c b/lib/vdso/gettimeofday.c index 9fa90e0794c9..9c3a8d2440c9 100644 --- a/lib/vdso/gettimeofday.c +++ b/lib/vdso/gettimeofday.c @@ -13,6 +13,18 @@ # define VDSO_DELTA_MASK(vd) (vd->mask) #endif +#ifdef CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT +static __always_inline bool vdso_delta_ok(const struct vdso_data *vd, u64 delta) +{ + return delta < vd->max_cycles; +} +#else +static __always_inline bool vdso_delta_ok(const struct vdso_data *vd, u64 delta) +{ + return true; +} +#endif + #ifndef vdso_shift_ns static __always_inline u64 vdso_shift_ns(u64 ns, u32 shift) { @@ -28,7 +40,10 @@ static __always_inline u64 vdso_calc_ns(const struct vdso_data *vd, u64 cycles, { u64 delta = (cycles - vd->cycle_last) & VDSO_DELTA_MASK(vd); - return vdso_shift_ns((delta * vd->mult) + base, vd->shift); + if (likely(vdso_delta_ok(vd, delta))) + return vdso_shift_ns((delta * vd->mult) + base, vd->shift); + + return mul_u64_u32_add_u64_shr(delta, vd->mult, base, vd->shift); } #endif /* vdso_calc_ns */ -- 2.34.1