Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp149829lql; Fri, 12 Apr 2024 06:34:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVYyGFKuCv0xh86452l5sg6RlZVq6xFn1e4677FER68TaPR/OAopUdP4Ndm5IKjHMEMoNrSxDCMHs3HjHmjgkkIImQGE+vkNpETAPH9LQ== X-Google-Smtp-Source: AGHT+IEA6+EtNPPnjtKd67v3k5fgfnEdWM+7i3mfR+pkSfbWw/6k2jnzccPypVK5u4Zppz8n1Cl2 X-Received: by 2002:a05:6102:d93:b0:47b:6053:c204 with SMTP id d19-20020a0561020d9300b0047b6053c204mr817005vst.23.1712928863149; Fri, 12 Apr 2024 06:34:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712928863; cv=pass; d=google.com; s=arc-20160816; b=TaRT8b4ln+x7hR+U2p7MEm9vNyJUwQ9REzwE5a8EnIa5grByAQlr4kztL9PKiAdNiF E0SnqbMZwPSFO7lW3If5sRhCl6kDmiSDzMCwHFqymCYQGgXUwyWrco7wYNQWP3Ib6lSf Eol4hX2NWpAjEN/AZoBDXr/IFgApwONq0o0wSbFSZFWxVErql46rTEB4JpGZM9gqvgum l/jCQmrKASP5aIJ7/oUizhmQBO3moI59L9BI05qk92Dd2sepCWD5Dty6Hx54SmY9VXsj areJZOpPi82ypG17pp9es26GYVofZnpnmPheMHPVNB1F/j4KjA8Ntbg1/ThxyERcdosr j0Dg== 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=KvnQBzgnQ9uzdcnNmTyAu4+OT66V0srgD7VC/MHPQzE=; fh=OR9HfCgrhBAvJ4UzhLRCU/mLk90Frr2klIk5E8zONvQ=; b=LupiIm4gTnC3BG7OkoL9kqSHDS5yfzGRhxubRt0gWkW+FUhQlsn+WWKehqEF/NgykZ dV9nQJNbT7BJQOV/fEPOtqvxTvTQqsF1OCrmMieUPypSDnew4Ocr/1wlrAG6XztRtrNp HV1OeLbh7LzWdQYPJ87AGhIwkDq5EKgPb/xs5xbX+KElAeWpscPUQzZMVdnHrp6sAdYf cdkoPwbg1Qu+346/xmBTis5No+NusWpkGA9Jd17qHCDayjApjmiNWPItk2qw9DKZW4Uk ugLNIo18Qh7tbWmKYY9aDFPNKusRuzXE8zAVY83y27QqNDWa9NahIc8tvm3hbHx0zG+g s7Tg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jY/IlpJN"; 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-142738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142738-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h15-20020a056102104f00b0047a2ce4e7a1si535074vsq.10.2024.04.12.06.34.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 06:34:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="jY/IlpJN"; 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-142738-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142738-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 275341C21BDA for ; Fri, 12 Apr 2024 13:34:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D20B783CD6; Fri, 12 Apr 2024 13:34:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jY/IlpJN" Received: from mail-oa1-f50.google.com (mail-oa1-f50.google.com [209.85.160.50]) (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 74C1D83CBA; Fri, 12 Apr 2024 13:34:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712928855; cv=none; b=F9LGXfMXzoz10MwybRY+Nnfl9Ll9X7aOcih68XXcK1WJ0HzXe8s1kyZmOEjRcjTjrjLe4gQYq4UMduq+DrNxGx+6t07/c6ASeQMuevuonXpk2uNltY8P2KeQQ71SmRolew2eOe397H7eQoDRZ+gNQxR/sGzOeYeGh4nlBJgbpBc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712928855; c=relaxed/simple; bh=bXuJBsmh+9cSqT7Y0dmwOptaeFP94AwFcrF1Nc5BiuI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NUiuqKF1sfUS/0s0FM15N7ESTCNspY0Suw6upXM4qylEZputXdD0tJkfidDq3jtb6zxnku9QO0QHnDg+UmhVfZ91k93fnkO3GJNnhKzaEITtb2vS8W1bBr7zQXXq+hX2VPobPTNmo5ohT+4d/VrbrANo2HtkeNuwRioIlAtqJEg= 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=jY/IlpJN; arc=none smtp.client-ip=209.85.160.50 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-oa1-f50.google.com with SMTP id 586e51a60fabf-22e6b61d652so375897fac.0; Fri, 12 Apr 2024 06:34:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712928853; x=1713533653; 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=KvnQBzgnQ9uzdcnNmTyAu4+OT66V0srgD7VC/MHPQzE=; b=jY/IlpJN+sJu/dLKO2vpnbnmb6X1sT+W9+UBJqpfpUSwnAKl57tDb0HwFIf5PQNSsE StgaWZZ1L2W2IDIDU0I4XMEQWbT9bjDnteOuGu5N1IPZbEpZFT9t4xtF0+Bce/Vwd/EU vVmM6RCsFOSfGbZR4sy80OqVkl1XWfPZUL36H7d7Yw1XViNwrFLrjDKQucPzxodJ/x7U za3I+k7QO4vAzlWip91jEC+EbXMHK6Lo0L6CDcDDd4GXADQizHU9M/JKea2c3Cdh5czg FTp6KVOb+CFNyOSzPFTrxKvKiXJ4EMHWxM6qShnXfrCd9eICNVTVFraKbq6viJk21Dvl K3mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712928853; x=1713533653; 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=KvnQBzgnQ9uzdcnNmTyAu4+OT66V0srgD7VC/MHPQzE=; b=SVcdhzLP2Ha7kBrTiTv4rzQR3VbWtYSqWanVUqDAZMHtbs0DVM8G/Zo5kKrpIC4KPM uBzRELgRZcf+rFNDiFbD9U+RpU2j4bToErxKNVO8v5U/URly9o6Pdm4BD87cTze2ip+f 93BzaE0ET5n+iLZEp8aWBcXCFukKNP54h4QNKJOv9JOU0GryAR9MOuEaJwI9zLRXN05j bxqIxpiJq9gsnk/TT72cTWglaWZXeRKPuKuwo3InJQpG5MFFtfc8BtcSVEIFi9YPbHLU Lwnh6yjBGEhxCSpchDR277loBHeMoOaosHdbSTM8NiY0L+N0WY93bA9PVXVav36KDcqk jP1w== X-Forwarded-Encrypted: i=1; AJvYcCVKVqGH3Of0Mgo40jmK0LhbKoaJJdejUxQRDNTLVqxIc5B1cJ5GfSFRLsHalHCdSMTqNcUhdEZop3JfJI9qPfXx0ONz0TSIvezUx2zISiBsxFCoQXcECLR+hS+dkW9HTKulCg/5/qRojRcszK4= X-Gm-Message-State: AOJu0Yz5/JPsvoRwa5uODaQb/19KBIn13yvP1v3XoaRa8r8wwr2BoCuJ ownQN/0Jc/eeyBk/A4hjt7psS8DqJUtBTDyn/R+rwAxcCDXM+wDh X-Received: by 2002:a05:6870:4689:b0:22e:df68:ee7a with SMTP id a9-20020a056870468900b0022edf68ee7amr3006137oap.42.1712928853468; Fri, 12 Apr 2024 06:34:13 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id h13-20020ac8714d000000b00434ee466ea6sm2226084qtp.22.2024.04.12.06.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 06:34:12 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 7799C1200066; Fri, 12 Apr 2024 09:34:12 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Fri, 12 Apr 2024 09:34:12 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudeiuddgieeiucetufdoteggodetrfdotf 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 09:34:11 -0400 (EDT) Date: Fri, 12 Apr 2024 06:34:10 -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 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 09:14:03AM +0200, Miguel Ojeda wrote: > On Fri, Apr 12, 2024 at 1:08 AM Boqun Feng wrote: > > > > Currently since Rust code is compiled with "-Coverflow-checks=y", so a > > Nit: it is enabled by default, but configurable (`CONFIG_RUST_OVERFLOW_CHECKS`). > Ok, I will change it accordingly. > > although overflow detection is nice to have, however this makes > > `Ktime::sub()` behave differently than `ktime_sub()`, moreover it's not > > clear that the overflow checking is helpful, since for example, the > > current binder usage[1] doesn't have the checking. > > > > Therefore make `Ktime::sub()` have the same semantics as `ktime_sub()`: > > overflow behaves like 2s-complement wrapping sub. > > If `ktime_sub()`'s callers rely on wrapping in some cases, then an > alternative we should consider is having a method for explicitly > wrapping, like the integers. This would allow callers to decide and it That works for me, although I would prefer `Ktime::sub()` is wrapping sub and we have another function doing a safe version of sub. > would make the expected semantics clear since the beginning (which is > the easiest time to add this kind of thing) for Rust code. > > Otherwise, I agree we should at least document the preconditions clearly. > > Having said that, I see a `ktime_add_unsafe()` too, which was added > due to a UBSAN report for `ktime_add()` in commit 979515c56458 ("time: > Avoid undefined behaviour in ktime_add_safe()"). There is also a > private `ktime_add_safe()` too, which is a saturating one. > 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). > So, given that, can callers actually rely on wrapping for these > functions, or not? The documentation on the C side could perhaps be > clarified here (including the mention of UB in `ktime_add_unsafe()` -- > we use `-fno-strict-overflow`) and perhaps using the `wrapping_*()` C > functions too. > I must defer this to Thomas. > In addition, Binder calls `ktime_ms_delta()`, not `ktime_sub()`, > right? In that case the arguments are called `later` and `earlier`, > perhaps those have a different expectation even if `ktime_sub()` is > allowed to overflow and thus it would make sense to check in that > function only instead? (and document accordingly) > 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. Regards, Boqun > Thanks! > > Cheers, > Miguel