Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp61934rdb; Thu, 25 Jan 2024 08:15:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IFqJBlPvnzYB2mAX2VQymsV96F5MKaV97lmG14k0zzzTi+TZIM9a4yhm1Duiletyg0/ZSZh X-Received: by 2002:a17:902:a386:b0:1d6:8ca2:78a9 with SMTP id x6-20020a170902a38600b001d68ca278a9mr110409pla.69.1706199336755; Thu, 25 Jan 2024 08:15:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706199336; cv=pass; d=google.com; s=arc-20160816; b=KwWS+7lwa372r8dhdz8l+7s4E5ivP6nlz58CG31f5QJP7xsnV0AM4Hyf1sg8le43o/ gFlaR5+eK9bzcjyFe5G/OqArwuxH3m+z0JztnORaQycP3a2JCpmcl5Wnrh2yeAFHqXdC lgiI21h4uxDX2zSGJ3T8I0cBdMWubJuQamNwXSz3SlxaxpQ2aFOrFSSEMXWOlduFDTkM +OErpwcSbNSIO1y63//QZYMJoniJH/VwbjzkPV/mMBF37doqeHBARt+qRnQKHiWzsv2N XLaxYy/uKf/BX2PddjfZIOR/Eej4kBB/2d8YoHWj86IYZ5Sk1HHRjANvll8Sle4PGmKK O48g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=7lk63POKR0f1uaiQp+rL8Q768TeeIp8icZEI1dRCPmo=; fh=gjdK0mn+rEqakIwN7X+XsBynu1JSiFVvhRjmNblSFLc=; b=I1CbMle+wNxgbFytJ/Pzd+jfPcEjyBiRJYGuKr68Jt65BZSUIvl+FBSZVHAcbQrlPs HKGjKCPgGXqRvgT7DmeDlrtgSkXO/oNld5IxBEAmLdzUl3GOWLAmLaO2qz9Y6hdKN9GC TxwgtTCkFXFrYLL2ISFbojOYdMFfGGxglGCzTc1Se5npkfr+dTmhWodTVASGHzVjWa2P aIXQ9KUdo4bP2BfKvjPlukgRFo1LNhY3fnLK8tra/Hp+kKo+O7skjsgaf5TsnNu1T/sY wOtMkqzXIGwePY4KQrrn6E3PCk3UHnQqziu5ewTmyYrYsFI8qrP7C8bk48QmF97i96zU IaKg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sjxtG7oB; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-38877-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38877-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id b1-20020a170902d50100b001d748c17027si8278828plg.173.2024.01.25.08.15.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 08:15:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38877-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=@google.com header.s=20230601 header.b=sjxtG7oB; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-38877-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38877-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 650C92951D8 for ; Thu, 25 Jan 2024 16:15:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 98164129A72; Thu, 25 Jan 2024 16:15:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sjxtG7oB" Received: from mail-vk1-f176.google.com (mail-vk1-f176.google.com [209.85.221.176]) (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 DED1B6EB57 for ; Thu, 25 Jan 2024 16:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706199327; cv=none; b=qXsEfsnDjPJ4ktN/rz8o6zunVWP3UjNAsGHVWkwXTCQcUoKYpgKSyFIg61MdelDTBhpwm7uaFpuXFk9vztboozqVFUoT2dXuVPnKkS7/CAvV4txv8lzg9/nKWZX3Knm3WeDVn9gqDxxqHsO3AeKhYVv8uJ22wt6Gw0G0Tyw1lyw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706199327; c=relaxed/simple; bh=PMOq6b74OsAKYyIq+lnlaB8rWPhr1y7hF5VaJVdA2BY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=U3rAV9dIy1rYKWG8TzYj13LafWRLZmPNPXVDajYdouG/oidv66yVaITqMg4iKA7VFjFwEkqUbaukTXdYWiiLT4bhIZ+RFQNpa9C+v+KMYXcrbwgVU9D+/P3OB4PMylDJAk2lNcQRu5nQI+VAAnSytBn8+KhKIrwdv5YwGX1e4Rg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sjxtG7oB; arc=none smtp.client-ip=209.85.221.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-vk1-f176.google.com with SMTP id 71dfb90a1353d-4bd3dcee54eso511874e0c.0 for ; Thu, 25 Jan 2024 08:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706199325; x=1706804125; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=7lk63POKR0f1uaiQp+rL8Q768TeeIp8icZEI1dRCPmo=; b=sjxtG7oBriMA2BibP1+j7Tr3TQsGiu9IOM5dYxu3vF/6JRkqCIMZZhn6+aKRHbrucD DQDc1H3sqSscKKXyzAUWAxKNeHvQxW2SUFqeqtEWCHhwx982sH5MJa/GEQUmqqnyKjnF 3PwV/qIQriJVMl8Ftvrog4ATbNaP3U+5Jrzybz/6GAQJee390ycRRQiSBJDWWfx6RozN SGXW3N9QT+fnHUAjjt+3Ea8xg3uuDQ/zrS1loXiNvUziA+O1vlcr13OjUSzjDUN/6Jqx QVP9mCVb0ljaPkF2/8naiahBmjoXpR3eTqpyFCvJUq25vb0trG/s3h0Vqjfl+sf0fXdn 2k9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706199325; x=1706804125; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7lk63POKR0f1uaiQp+rL8Q768TeeIp8icZEI1dRCPmo=; b=AFDxhhkNYPu87guD4tr6z7a9QZvxC3bDyJylP43ao18432vm7Xi9v6OLUhzKCVsK8b PpykdzdrTdvdUI3MqwqLSM0yiykfZD64rR2g252AwyKRXX8+phTDvzZJSecHcYLWESbG 8QlLGjlXuUH1IypLTNIBXrxt0kQu24zMwqHCsRSi3lGRmiU7y+y37W6MHgvjZ1tHOLDm M2/jK66nSbrQPAKJpGhnpHZQtlzZSDRmZDLNQ1AMVASQ/xIuKPUxbPdWhZ3deA1wAH+B QeN5LNa+riCeY+sCyIE9SCoOo2CvMnr9RxyRzj2WVbcDr/K6GRwRFONLM+V64DFijC68 yf+w== X-Gm-Message-State: AOJu0Yxnk7gw0vayOCyDc3Tj1a8tcn1/beZ7yXfYCUDuRfRgS8bAJKcr BWwcUQ8ws4MFrHc2DGzOaKQDCzTzGJX6LYY0/rmSOmp7k1gd1NN4YhePzHAPizc1wOj4TrdLOhU YuGsfCotlIjHtDzpUqJyH+Vg/NUPWcTmHCtsA X-Received: by 2002:a05:6122:1807:b0:4bd:2957:2620 with SMTP id ay7-20020a056122180700b004bd29572620mr46434vkb.13.1706199324648; Thu, 25 Jan 2024 08:15:24 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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> In-Reply-To: From: Alice Ryhl Date: Thu, 25 Jan 2024 17:15:13 +0100 Message-ID: Subject: Re: [PATCH 2/3] rust: add typed accessors for userspace pointers To: Arnd Bergmann 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?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jan 25, 2024 at 5:00=E2=80=AFPM Arnd Bergmann wrote= : > > On Thu, Jan 25, 2024, at 13:37, Alice Ryhl wrote: > > On Thu, Jan 25, 2024 at 1:27=E2=80=AFPM Arnd Bergmann w= rote: > >> On Wed, Jan 24, 2024, at 12:20, Alice Ryhl wrote: > > >> > +EXPORT_SYMBOL_GPL(rust_helper_copy_to_user_unsafe_skip_check_object= _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, u= nsigned long n) > return raw_copy_to_user(to, from, n); > } > > -#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 > > -#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, un= signed long n) > } > return n; > } > -#else > extern __must_check unsigned long > _copy_to_user(void __user *, const void *, unsigned long); > -#endif > > 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 > } > > 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 > } > > #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 @@ > > /* out-of-line parts */ > > -#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, unsigne= d 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 > > -#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 Sure, if that's okay with you, then I'm happy to do it that way in v2. Thank you! Alice