From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D9B9C47258 for ; Thu, 25 Jan 2024 16:00:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 106B76B0085; Thu, 25 Jan 2024 11:00:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B7D46B0087; Thu, 25 Jan 2024 11:00:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E72BD6B0089; Thu, 25 Jan 2024 11:00:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D4CD96B0085 for ; Thu, 25 Jan 2024 11:00:09 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6941C40E0D for ; Thu, 25 Jan 2024 16:00:09 +0000 (UTC) X-FDA: 81718294938.18.05D04AC Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by imf12.hostedemail.com (Postfix) with ESMTP id B77874000F for ; Thu, 25 Jan 2024 16:00:06 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=arndb.de header.s=fm2 header.b=hZUco6hT; dkim=pass header.d=messagingengine.com header.s=fm3 header.b="k LFp17Y"; dmarc=pass (policy=none) header.from=arndb.de; spf=pass (imf12.hostedemail.com: domain of arnd@arndb.de designates 66.111.4.28 as permitted sender) smtp.mailfrom=arnd@arndb.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706198407; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SqRtybdjact+f0QeQR9azdnQV/iHBEyjbNT4deqFxZo=; b=7RfVe35B2c9BBwWpW8DoyOULpxJUJ/KTk3m/KzmwmIfdTp21P2ROXhhmHTQYNQeqY/1bOd 2EZvAcvgHdUTzVvLLrAx9PUzqbx1KGW/LGM7TUo4jp0iATX+zFSFECaCFu6KziD9ZAn2Gg VtO6KLsrVJoTGPUmY/zmI3BrbvfvjEk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=arndb.de header.s=fm2 header.b=hZUco6hT; dkim=pass header.d=messagingengine.com header.s=fm3 header.b="k LFp17Y"; dmarc=pass (policy=none) header.from=arndb.de; spf=pass (imf12.hostedemail.com: domain of arnd@arndb.de designates 66.111.4.28 as permitted sender) smtp.mailfrom=arnd@arndb.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706198407; a=rsa-sha256; cv=none; b=B2EDbVmf56dOV67jkB/69Zti/K2Wnt2c8pJk/vzzPn+XaMzSZ1r6o8HUij3dbl12QE/v/S b7/z0+ixbiE2iNsnBXQUcZIjJpSj108OFJyKwi9LTGOD3nw0EZwzEH2Y2v/Mj7uL20UVhw OfY8gLiMrTHOfP8U78biupHEwX5REmw= Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 005105C00DB; Thu, 25 Jan 2024 11:00:06 -0500 (EST) Received: from imap51 ([10.202.2.101]) by compute5.internal (MEProxy); Thu, 25 Jan 2024 11:00:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arndb.de; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1706198405; x=1706284805; bh=SqRtybdjact+f0QeQR9azdnQV/iHBEyjbNT4deqFxZo=; b= hZUco6hTk7rOkCqcYBakCuKg7hmrOp10Kl7rKZgdg3AtevpRTSE1FNx35WvDHTR3 1C7FoNO4AoNwonAd2Chx/+UWKiyhmFU2eCeCnBslCiMYNOh/xTT/6M8hCTDPxsfz 89i84ehFOG8mwCbtzrBv12r4eSH1QZg8tTBBkeJHz1RPqzkzk1xjsOxfOl3oRnTU sPr6vb5q46615EIDB1gJQAN8YA0J5/aOiEJZdQQaOAce8GGilRlQZyztp0ZwZTsc pFM6+FXy3riyiKtPd0DiquSWJ6oRpJyFvfcZeM5uaBpdXOMGhik9K+cRR86Lxl9j yiK2yA9KyAwtsZpPiGf+Nw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1706198405; x= 1706284805; bh=SqRtybdjact+f0QeQR9azdnQV/iHBEyjbNT4deqFxZo=; b=k LFp17YtrpRi6UPiYMjCy1sSkRCu+4ovdGWJglYg38qH4EC9qgTEkBYuLaLEYguEu e1XEM9HH3ivgr7ZJK9gNu7vf6g4WOJwlqkMdyt+wf9Wm1zSfCn8WTSbbV537JAtg Zd6WuoyJTAhl94CQKpuZNFTyNESGF0Y8bolk8bSWVO3604SjANDQMOdxikiPhGCt GG4VfwNqhjNrQam6m5b+aW7zQsOT6MnsQlRnal4Y9WN0hHW0ZVvRNE6SrI13KGaD 8azmTSTIlo/kQew6erQirnv72LdKvsDMb841+XjVkiHjaAmwa9dQKfuzjCHi/6tV nFAlkE9/wgYcr80jMtazw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdelgedgheeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtgfesthhqredtreerjeenucfhrhhomhepfdet rhhnugcuuegvrhhgmhgrnhhnfdcuoegrrhhnugesrghrnhgusgdruggvqeenucggtffrrg htthgvrhhnpeegfeejhedvledvffeijeeijeeivddvhfeliedvleevheejleetgedukedt gfejveenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grrhhnugesrghrnhgusgdruggv X-ME-Proxy: Feedback-ID: i56a14606:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 4DCDEB6008D; Thu, 25 Jan 2024 11:00:05 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-119-ga8b98d1bd8-fm-20240108.001-ga8b98d1b MIME-Version: 1.0 Message-Id: In-Reply-To: References: <20240124-alice-mm-v1-0-d1abcec83c44@google.com> <20240124-alice-mm-v1-2-d1abcec83c44@google.com> <070574cb-8d7f-4fe7-9826-cec6110168ff@app.fastmail.com> Date: Thu, 25 Jan 2024 16:59:42 +0100 From: "Arnd Bergmann" To: "Alice Ryhl" Cc: "Miguel Ojeda" , "Alex Gaynor" , "Wedson Almeida Filho" , "Boqun Feng" , "Gary Guo" , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Kees Cook" , "Alexander Viro" , "Andrew Morton" , "Greg Kroah-Hartman" , =?UTF-8?Q?Arve_Hj=C3=B8nnev=C3=A5g?= , "Todd Kjos" , "Martijn Coenen" , "Joel Fernandes" , "Carlos Llamas" , "Suren Baghdasaryan" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, "Christian Brauner" Subject: Re: [PATCH 2/3] rust: add typed accessors for userspace pointers Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat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nsjyt65 EZ7Nj+Iu 09zts61vqipQAxrhcR3/jxTaYlCmP1jsL983lrpMsO5rdKS3nxdrogYhb3Fce6+0R09sF2TabdOB4IJa8+tf/9kobE5KK4pjrGQcxkYGJUmtfS0S63KWIg51fjwqTQUfuS1QPyyEz9vn9Nv/rHgcxppHgxc6KKs8qUb8c13QUetgM8B6SYKjhaEOgtJhDkssN0WUvRr1aJkecuBBLrZfaPB0sJ3nWfG1NhVc1QiEH58S//vzYutCqvFVG5ThfU+x4jBgxUNJR9gnQhSongpJI32AiUlB2NGrkCykJ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Jan 25, 2024, at 13:37, Alice Ryhl wrote: > On Thu, Jan 25, 2024 at 1:27=E2=80=AFPM Arnd Bergmann = wrote: >> On Wed, Jan 24, 2024, at 12:20, Alice Ryhl wrote: >> > +EXPORT_SYMBOL_GPL(rust_helper_copy_to_user_unsafe_skip_check_objec= t_size); >> >> These functions are almost identical to the ones in >> lib/usercopy.c for !defined(INLINE_COPY_TO_USER). >> >> That version has an extra memset() after a partial >> copy_from_user(), and you probably want to have the >> same thing here for consistency. >> >> I think ideally we should only have one out-of-line copy >> of these two functions and have that one shared between >> rust and architectures that want the C version out of line >> as well. > > I had a bit of trouble figuring out all of the copy_[to/from]_user > methods that are available. I was hoping that a better solution would > be available, and it sounds like one is. Is _copy_from_user always > available as an exported symbol? If it's always available and skips > the check, then I can just use that. I don't think the memset matters > for my case. At the moment, it appears that it's available on the few architectures that don't #define INLINE_COPY_FROM_USER: alpha, csky, powerpc, riscv and x86. On the other architectures it is always an inline function. > Otherwise, I can add a helper in rust/helpers.c that wraps > _copy_from_user only when INLINE_COPY_FROM_USER is defined, and call > the helper in those cases, and otherwise call the exported symbol > directly. (I need an exported symbol to call into C from Rust.) > > Would that make sense? I don't think we can have a perfect abstraction here, but rather than putting knowledge of INLINE_COPY_FROM_USER into the rust wrapper, I would suggest putting a bit of information about rust into lib/usercopy.c. I've tried to come up with an idea below, see if that works for you. Signed-off-by: Arnd Bergmann diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index 3064314f4832..835aa175d0ee 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -138,13 +138,18 @@ __copy_to_user(void __user *to, const void *from, = unsigned long n) return raw_copy_to_user(to, from, n); } =20 -#ifdef INLINE_COPY_FROM_USER static inline __must_check unsigned long -_copy_from_user(void *to, const void __user *from, unsigned long n) +_inline_copy_from_user(void *to, const void __user *from, unsigned long= n) { unsigned long res =3D n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { + /* + * Ensure that bad access_ok() speculation will not + * lead to nasty side effects *after* the copy is + * finished: + */ + barrier_nospec(); instrument_copy_from_user_before(to, from, n); res =3D raw_copy_from_user(to, from, n); instrument_copy_from_user_after(to, from, n, res); @@ -153,14 +158,11 @@ _copy_from_user(void *to, const void __user *from,= unsigned long n) memset(to + (n - res), 0, res); return res; } -#else extern __must_check unsigned long _copy_from_user(void *, const void __user *, unsigned long); -#endif =20 -#ifdef INLINE_COPY_TO_USER static inline __must_check unsigned long -_copy_to_user(void __user *to, const void *from, unsigned long n) +_inline_copy_to_user(void __user *to, const void *from, unsigned long n) { might_fault(); if (should_fail_usercopy()) @@ -171,25 +173,32 @@ _copy_to_user(void __user *to, const void *from, u= nsigned long n) } return n; } -#else extern __must_check unsigned long _copy_to_user(void __user *, const void *, unsigned long); -#endif =20 static __always_inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n) { - if (check_copy_size(to, n, false)) - n =3D _copy_from_user(to, from, n); - return n; + if (!check_copy_size(to, n, false)) + return n; +#ifdef INLINE_COPY_FROM_USER + return _inline_copy_from_user(to, from, n); +#else + return _copy_from_user(to, from, n); +#endif } =20 static __always_inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n) { - if (check_copy_size(from, n, true)) - n =3D _copy_to_user(to, from, n); - return n; + if (!check_copy_size(from, n, true)) + return n; + +#ifdef INLINE_COPY_TO_USER + return _inline_copy_to_user(to, from, n); +#else + return _copy_to_user(to, from, n); +#endif } =20 #ifndef copy_mc_to_kernel diff --git a/lib/usercopy.c b/lib/usercopy.c index d29fe29c6849..503a064d79e2 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -7,40 +7,18 @@ =20 /* out-of-line parts */ =20 -#ifndef INLINE_COPY_FROM_USER +#if !defined(INLINE_COPY_FROM_USER) || defined(CONFIG_RUST) unsigned long _copy_from_user(void *to, const void __user *from, unsign= ed long n) { - unsigned long res =3D n; - might_fault(); - if (!should_fail_usercopy() && likely(access_ok(from, n))) { - /* - * Ensure that bad access_ok() speculation will not - * lead to nasty side effects *after* the copy is - * finished: - */ - barrier_nospec(); - instrument_copy_from_user_before(to, from, n); - res =3D raw_copy_from_user(to, from, n); - instrument_copy_from_user_after(to, from, n, res); - } - if (unlikely(res)) - memset(to + (n - res), 0, res); - return res; + return _inline_copy_from_user(to, from, n); } EXPORT_SYMBOL(_copy_from_user); #endif =20 -#ifndef INLINE_COPY_TO_USER +#if !defined(INLINE_COPY_TO_USER) || defined(CONFIG_RUST) unsigned long _copy_to_user(void __user *to, const void *from, unsigned= long n) { - might_fault(); - if (should_fail_usercopy()) - return n; - if (likely(access_ok(to, n))) { - instrument_copy_to_user(to, from, n); - n =3D raw_copy_to_user(to, from, n); - } - return n; + return _inline_copy_to_user(to, from, n); } EXPORT_SYMBOL(_copy_to_user); #endif