Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp275168lqp; Fri, 12 Apr 2024 18:31:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWzwpesYAFCifqooMBbn33DlHsvSWUudEoqSeHhLjKL/IegrhIU4h1gRms/reI4ya7SJEbAtNBDsXH5vAtXXjT0c4RXpDmbFmah/znCAA== X-Google-Smtp-Source: AGHT+IEBmPhQlkQ5pyV1vqXTl26XGpIU0M15nlppMkFQh+4gv1OLb7EfmawHJ8H/vBRrbREzDWk9 X-Received: by 2002:a50:9ec2:0:b0:56d:f9f8:3e68 with SMTP id a60-20020a509ec2000000b0056df9f83e68mr3291021edf.12.1712971869897; Fri, 12 Apr 2024 18:31:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712971869; cv=pass; d=google.com; s=arc-20160816; b=xneCF39pDvVpHViEvl1gxAaHtgGamWnOOR6S3AuBBjNjMGNIiaVA8bZvQ/WE0KVgPD AezoUPiZXu8GzDA5WgdIoBll/lIrfTLwdo+3QXNITwxJghaY6MM1ewduGnWm1aKDljp1 fmXucrz49gTQooIKFcx7SvTdw1a8g/AmlyzRFdwE1ngDSkREgyo2BrOLh/LgSaKosLU7 jtJ18PLWGG3Z0fnpATlkSxYZZo3eLKxQuB8YLgcq7SXA5E7BsqjUmX3zfrFz9ZzF9Jre N9EkX+0qDm2aBPcedftDpHfTWySQVI3UoZrqSjePgvotk0iROHKmTGqG5F994UXz8OU+ gMDg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=WGZV+9Baru/G/BJ7iBWX/ai40r2yV5HMJ8W7pjdUee8=; fh=yEaNqplgOWrvPnaUcjJ5LuuLjbOwepMBKrfNsBu2qO4=; b=le7th+kCxKSmSUj/F2TIU+Lk+jZzCFAfTS4uS/yJ0RWcI4cwBJbJu/z1dp5OXYIGPm 4ju1jyGX8wP80ZzaMz4Ee0Sd4xA93bOkyiE4guKM59zEjo36AmPof4kUQk9LvknP1Jtr alH5abnlii4+Hr9IKZ2iY+cD2F7HKT20fz5ZLOK2oEVb/GsOeF05j4Dfy5mac/tU3x3p LzM4hXnnHNInHAHXLmpWa03Ncz7YYKh9pJv3yKJQpWQDUx+pwoHCcopC90xNk9Kp2CQK pq/X2R0w0Qe/2dr7Vumhh/4z2oSY0tr0FGaCYecflTicmr+ueQTlQ+5ArKl0tTMrv9yk 2GJw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QSJzKSm6; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-143491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143491-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z18-20020a05640240d200b0056e32d9857esi2259084edb.523.2024.04.12.18.31.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 18:31:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QSJzKSm6; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-143491-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143491-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 39F9C1F22B97 for ; Sat, 13 Apr 2024 01:31:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 36DBABE5E; Sat, 13 Apr 2024 01:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QSJzKSm6" Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com [209.85.217.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A008663C8; Sat, 13 Apr 2024 01:30:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712971841; cv=none; b=O9KnBJWriym+gWDZoZIc6MWvr4MkznOWvB4DfP3uUXNEUS+PwbnE2kb+SrsuT88pCuNy86kDhaBqo8WHMupez+blh/G0oEj4D/vHJQatqaJKU31Pw1SBNVwTQLGVJUS/wEHVsaNyiWJecPFKy/6EJgM36zR8NZyBy8+N5bmpX/8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712971841; c=relaxed/simple; bh=RlBJ6FJhdBQgcIf20qlpXCjBXGhLET1XFZuTUNZ6Yaw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PlTXTVGljOLmpmtOs1LxxE9DkPI24dm0SHmGG3WmkWSk78HE1xTZIbDTJAhPt9wmcNpn0JZMF/V+VPYhEiSi0UHuUvAHjQTeki+oqdIeOYQOp+gMWihpjvgS641Fy1no8XHqIU+fdZlWQCD4ctqO9FIlFXbnzKbER+grLuLFuaU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QSJzKSm6; arc=none smtp.client-ip=209.85.217.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-vs1-f48.google.com with SMTP id ada2fe7eead31-479c0e8b1c5so413327137.1; Fri, 12 Apr 2024 18:30:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712971838; x=1713576638; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=WGZV+9Baru/G/BJ7iBWX/ai40r2yV5HMJ8W7pjdUee8=; b=QSJzKSm6XfIKekPqtsNuLo5Wb3MR1lTlXXZg3T4hZBCnJo1dkAGZcAOP2GzeP/faeH oP771Pe0z0nVSXOBLrZe58SSQPGDtag4hwgnPKNynlmoIKN3vzj76V/1gRalpt7UdloT pkHeu632xx7lfI2j+EYHJWkWqI26689mcCtZ41cACl6gcLLU1dmpjXvSJfKUUNdyZack ksdg7dCzX1ze7CRCR68Tp7dfSP+4maxRVxHYv+TX37Urz+s2//DSB7PybXTHmx8965di a1drbW1ciPVcSayNHC2rIMhq7i0NeRs7g/5+BLUw38XUkidsn2DQG6FUIKXfMXQux5pP 0jhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712971838; x=1713576638; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :feedback-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WGZV+9Baru/G/BJ7iBWX/ai40r2yV5HMJ8W7pjdUee8=; b=HHUmq/aX9yJkEXz1OjPEY4kdx+KwmI7MFmo26ZOf2BzMXHGdyTQAdwGA7tqoTKAy1I 03xB+DSHh6DztB76Kzpz4VKmgWPG+IoqMK2wOv2QB8/MKqp3TH7fCn9cUwKJeJG5dSoT /4nkoaaVKEvZdPx+AGmIq6fwr2zNHrJZupZhcflRTpr/Spdn3mLqd41bCDx83G+YBjvh O1Z/5X7GBQBUwWreZKAjCV9c9U4eK0BkmDUVFwJ7harmevBRavBz8CrChjWzKBrhXA1N 0a7kLuWrDKSPcmEH1S5bQCp4/JbuaqnET4CKkpkSsE2REX5bAc99fCy/VdHmL+XwDz7l DsSw== X-Forwarded-Encrypted: i=1; AJvYcCWifcDAJQMri2BMoMupIZw6JYkIUrA79GbLYY1ooPsE/XxQ5dZ3l1QocKTBYb5Cc0qvMgZG9jwona/OCaoFfQ2COI+YC2FJ1VNg3pVNg2XFCQV5JBZgo8X3i2YSob5vlqUgMWcji0O6NX4UDI8= X-Gm-Message-State: AOJu0Yy05vAnAja5AshHQmZazDL2x55YD0MjMFnOjj64Kkkd/SLYZcNj UFDz64liMY4N2cKBsL8QSjd9P59D6bgXfinQpHbrslys4U3LwUm1 X-Received: by 2002:a05:6102:f94:b0:47a:42c4:4cde with SMTP id e20-20020a0561020f9400b0047a42c44cdemr5751224vsv.18.1712971838479; Fri, 12 Apr 2024 18:30:38 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id d24-20020ac85358000000b004364d940d3dsm2878244qto.96.2024.04.12.18.30.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 18:30:37 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfauth.nyi.internal (Postfix) with ESMTP id 11B261200066; Fri, 12 Apr 2024 21:30:37 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 12 Apr 2024 21:30:37 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeihedgtdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggugfgjsehtkeertddttdejnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepvefghfeuveekudetgfevudeuudejfeeltdfhgfehgeekkeeigfdukefh gfegleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 12 Apr 2024 21:30:36 -0400 (EDT) Date: Fri, 12 Apr 2024 18:30:34 -0700 From: Boqun Feng To: Miguel Ojeda Cc: Thomas Gleixner , Miguel Ojeda , John Stultz , Stephen Boyd , Alex Gaynor , Wedson Almeida Filho , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Vegard Nossum Subject: Re: [PATCH 2/2] rust: time: Use wrapping_sub() for Ktime::sub() Message-ID: References: <20240411230801.1504496-1-boqun.feng@gmail.com> <20240411230801.1504496-3-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Fri, Apr 12, 2024 at 04:41:26PM +0200, Miguel Ojeda wrote: > On Fri, Apr 12, 2024 at 3:34 PM Boqun Feng wrote: > > > > That works for me, although I would prefer `Ktime::sub()` is wrapping > > sub and we have another function doing a safe version of sub. > > Why? It goes against the "normal" case in integers. It is also not > what `ktime_sub()` does, which is the "normal" case here, vs. Seems we have a different reading of `ktime_sub()` ;-) Based on your reply to Philipp, I take it that CONFIG_RUST_CHECK_OVERFLOWS can be enabled in a production kernel, right? IOW, it's not a debug-only feature like UBSAN (or maybe I'm way wrong, that UBSAN is also a feature that production kernel can or already use?). If so, then the current `Ktime::sub()` has a different behavior compared to `ktime_sub()`: it will perform overflow checks and panic (which is BUG()) in production kernels. Now I wasn't trying to say substraction overflows shouldn't be checked (by default), the thing is that `Ktime` is just a `ktime_t` wrapper, so it's natural that it provides as least difference as possible. If it was a standalone abstraction, then by all means let's add different APIs for different purpose. If you look at ktime API, ktime_sub() is the only one doing substraction between two ktime_t, there is no raw or unsafe or safe API, So as a minimal abstraction, it's natural for a user to expect `Ktime::sub()` behaves like `ktime_sub()`. That's my reasoning, but it depends one a few "if"s and what time subsystem wants to do. > `_unsafe()` and `_safe()` ones. > > > Exactly, ktime_add_safe() doesn't panic if overflow happens, right? > > I think that's pretty clear on how time subsystem wants to handle > > overflow (saturating it, or zeroing it instead of panicing). > > There are three variants in C (for addition) that I can see: > > - No suffix: not supposed to wrap. > - `_unsafe()`: wraps. > - `_safe()`: saturates. > > The first one, in normal C, would be UB. In kernel C, it wraps but may > be detected by UBSAN (this is what Kees is re-introducing very > recently with 557f8c582a9b ("ubsan: Reintroduce signed overflow > sanitizer")). > > So, in Rust terms, the three options above would map to: > > - Raw operators. > - `wrapping_`. > - `saturating_`. > > Because the raw operators are what we use for arithmetic that is "not > supposed to wrap" too. That is, they wrap, but may be checked by the > Kconfig option. Of course, it may be worth having an intermediate > option that does not actually go for a full-blown Rust-panic for that, > but the point is that the current "not supposed to wrap" methods are > the raw operators. > > All three, in fact, are "safe" in Rust terms, since none can actually > trigger UB (in kernel C at least -- it would be different in normal C: > the first one would map to an unsafe Rust method, i.e. `unchecked_`). > > Instead, in the C side, `_unsafe()` seems to be used to mean instead > "you should be checking for overflow if needed, because it will never > be reported by UBSAN unlike the raw one". Again, this is based on my > reading of that commit and the docs on `_unsafe()`. It may be wrong, > or maybe the subtraction is supposed to be different. It should > probably be clarified in the C side anyway. > > And, relatedly, I see that when the `union` was removed in commit > 2456e8553544 ("ktime: Get rid of the union"), `ktime_add_unsafe()` > stopped returning a `ktime_t` even when both inputs are `ktime_t`s > themselves: > > static_assert(_Generic(ktime_add(a, b), ktime_t: true, default: > false)); // OK > static_assert(_Generic(ktime_add_unsafe(a, b), ktime_t: true, > default: false)); // Bad > > It returns an `u64` now, which could surprise users, and probably > should be fixed. The only user just puts the result into a `ktime_t`, > so there is no actual issue today. > > > I must defer this to Thomas. > > Yeah, the question on the C API was meant for Thomas et al. > Maybe it's wise to just wait for them to reply, I don't think you and I have much disagree other than ktime_t API semantics ;-) Regards, Boqun > > Maybe, however neither of this function probably shouldn't have the > > panic-on-overflow behavior. So I agree that overflow checking is not a > > bad thing, but when to check and how to handle overflow should be > > controlled by the users, and making the default behavior > > panic-on-overflow doesn't look reasonable to me. > > Yes, it should be controlled by callers, but the point above is that, > from the looks of it, these interfaces are not meant to overflow to > begin with. > > Cheers, > Miguel