Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp786121lqt; Fri, 19 Apr 2024 10:17:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXS+ToC55etrevMc4OxMBjl/l9cyJDzMfn8gBlEUM79xYFjelMBE30LGmtlTXmCFkj99tKQ0Y9qPhJE4biepJzc66Mh1aMZFmnZK9rUtw== X-Google-Smtp-Source: AGHT+IHLX0GST8H/P4MPBrjvYhAdFuXa3Qr1tFaPYErwc3Xd6opDNq2GzfdEpTRTCR/uHytD2gd6 X-Received: by 2002:a05:6870:46aa:b0:22e:df68:ebcf with SMTP id a42-20020a05687046aa00b0022edf68ebcfmr3150110oap.56.1713547055977; Fri, 19 Apr 2024 10:17:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713547055; cv=pass; d=google.com; s=arc-20160816; b=cIHL6ULmMYUNqqCmSgh5WRoBZ0SMiBhpwTCjDeValhZwWa3D4bpVG62qOotWdgLycK KiS917dHKEdCZFPIPo4R/GsGf9blv7p9XY+N2UH4OxUsHlz/3dNmFQaOafTpgtJrH9zD hsQGHYRFDRw4SkAw0N7J4xykE/PR0nbO2OppnOPP624BDrg0fmNjFW4B9z8o1KjC5gwt BmkdyL6ihJKZ0Lossp/ZBQui6zqjuZXYx+2MhtHos3iy6Q4huOevChc6iOhQD0JiVr51 jpViyqhqPhEeEs4lc+hWoAqSYJv7NrZfMjiJuTqByNuw1Oj+k3nWwzErbNjzHUwsP5Uz 8XuA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=etFE82tcfTZVhWATTB9AHQDcgyv5F3QBoyzAyqb3uco=; fh=IiJfRY54JdTG4naMWcglGoqfDDtMn1F53Z5PvrW2n+8=; b=xgNGqCB7cMVZDp5YsgKpRq+QQha7vCvC9AHNvRdOazl6KmdA7+KkMwPIJDDynKSNcr bFLhBifkYn1Mg5Q/2KgNfyIi37/f2ziEd+u0oWRHAfkndFZEd+b/LQsBmTwM9FgBK6Lq iBamPV4fe9tsw2itCxFoGOk4IC4DkFgDUIxZJorrUT+UMOkqyu8B498rXd5VOE18usJa CqWg9qWdKOp1/CZJ8ZMlymQXlq9+kNTH9EExVrL7nsmk1Y3LaYWrCisD28ApOyM0m7wJ UHE4wnc4ibZA9+yNL7LXDaY2QJH45nKBz5jDdFP1IAUDnf6rdYP/UCjjpzodblXZtfyQ mcbA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Z8NOizub; 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-151809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151809-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 n20-20020a638f14000000b005f7a3af8ed5si2183168pgd.780.2024.04.19.10.17.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 10:17:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151809-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=@gmail.com header.s=20230601 header.b=Z8NOizub; 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-151809-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151809-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9868B286F3C for ; Fri, 19 Apr 2024 17:11:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 36ADA139D05; Fri, 19 Apr 2024 17:11:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Z8NOizub" Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) (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 9EE8E85284; Fri, 19 Apr 2024 17:11:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713546710; cv=none; b=rBvtRihujWwIAY7eamAcVdFlmqkqpXEpzVNLbjxq287dgV+nZiWuOBaNg1ksYmLW3zmnZED6XW9ACcsc6f7j/sC1JsPZM+30dlkcvlcNf4j/4Pqv+G3rikn4/c6jCw/Ha0aICUxIbPG9Q1D39nXc80ScMhcIzmgWOUwWAdvRMzM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713546710; c=relaxed/simple; bh=8tkp2OzXq/LQyBsBo9UUSKU1a3auTWKdaRyv5yhqPDQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=RMYGTLRb6Y8nO+nPPCUUBPGA/HLtlZ7UDzfsGb2kXKtExMX5SMVG+uzliv2qHV4ZysaqdXAgmposToJogWMnV8pi09XKL8mWOiijX2ckGb/cMZ4kM61wjK2ko0WoGYNeO3b/3rzQG0BSVShBS1HAPV3aMXuZ+QSqG07LH8MLwBg= 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=Z8NOizub; arc=none smtp.client-ip=209.85.222.175 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-qk1-f175.google.com with SMTP id af79cd13be357-78edc3e80e6so153405785a.2; Fri, 19 Apr 2024 10:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713546707; x=1714151507; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=etFE82tcfTZVhWATTB9AHQDcgyv5F3QBoyzAyqb3uco=; b=Z8NOizubSZM+EYSZbV7w0Y4nNw610IfAi4SQ6tgaisRs1mddMR693mkcuhwxKapJLE NNkKcszX4eJzVgoZ675HTgvTHed53DR9VRrXVcuQDc839iY3zQiFgc2lFxlZjh5+19fD SeqK798RQspdM7Xti5zgXQLYOIDSF2M4J00Mvce6EUbdayJA16uJLc/HbdB6GXhUZ+n+ v+0ARaxba2o9+H8grueUKKTdJOX/cTupH5eYjbLju6gONMVbtVGC5zpoOjo/13+HL0QQ UVk+lxjanx3+i1ExMSJM6Rs8hXksIso+292ZQ6G8j6UFZ/WUvVpDNouvM/JMChnfBt7+ Og7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713546707; x=1714151507; h=in-reply-to: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=etFE82tcfTZVhWATTB9AHQDcgyv5F3QBoyzAyqb3uco=; b=rw/0FdpbI50z1f0cjPrBz9k/4FNY5EP9t4bY43stD91I80y1M1QrYEVipHwe1MrSzC cOzeh4KhGVXCyyuTrsvkciM+txQJPJlcUIb0+fXYjKnptDzIxDaYXX3OhQTrFnae/uEO 6r7yI+LUdpyqAOepWlU+IpqOFXDQGZvYdxBgw4VtOKPpyEMJAutF06lM95fffGVNBv5L dShILVJIa76JdMF4Pvbbm+g4pLvszFv5O+z178YnjmskI/TXjkYYdmoH1mSqoS4BFtHe m96bRXyhvZ8Uw+YIaQKBL5IjqZRgtMEH0o3KJJevK0H/IEOPBG5hfJxWu4cyEDzyqzd1 pkdA== X-Forwarded-Encrypted: i=1; AJvYcCU+VsWuZkaIXC+VAevgPyxi2mUL8PCsASrL8tIOsh/pyKJi0/bfLhqNI+2SyhCUqmhQeVY5tH9vK3iVLsKPCdrnqRkc1xbSYryTh7XCBEFHRqPgAKmpMaCDppYJghB5yFlI/tthqNh2iQRMuYI= X-Gm-Message-State: AOJu0Yx+nWwU1b74S7ww8IbMQIs9y/68f36YTLSdq8JhMm5MITsiV5G2 0CUDF1TLApkZF1iS6OAczNBZ0IaQwCVUyXkG0LIB01XzAjUPlnH/ X-Received: by 2002:a05:620a:5650:b0:78b:ac85:3b79 with SMTP id vw16-20020a05620a565000b0078bac853b79mr2904061qkn.40.1713546707344; Fri, 19 Apr 2024 10:11:47 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id a4-20020a05620a066400b0078d73685803sm1760929qkh.99.2024.04.19.10.11.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 10:11:46 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id D72DA1200043; Fri, 19 Apr 2024 13:11:45 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 19 Apr 2024 13:11:45 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudekvddgudduvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdortddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepiedtfeevhfetkeelgfethfegleekfeffledvvefhheeukedtvefhtedt vdetvedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 19 Apr 2024 13:11:45 -0400 (EDT) Date: Fri, 19 Apr 2024 10:11:18 -0700 From: Boqun Feng To: Alice Ryhl Cc: Miguel Ojeda , Matthew Wilcox , Al Viro , Andrew Morton , Kees Cook , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Greg Kroah-Hartman , Arve =?iso-8859-1?B?SGr4bm5lduVn?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Arnd Bergmann , Trevor Gross , linux-mm@kvack.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Christian Brauner Subject: Re: [PATCH v6 1/4] rust: uaccess: add userspace pointers Message-ID: References: <20240418-alice-mm-v6-0-cb8f3e5d688f@google.com> <20240418-alice-mm-v6-1-cb8f3e5d688f@google.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=us-ascii Content-Disposition: inline In-Reply-To: <20240418-alice-mm-v6-1-cb8f3e5d688f@google.com> On Thu, Apr 18, 2024 at 08:59:17AM +0000, Alice Ryhl wrote: [...] > + /// Reads the entirety of the user slice, appending it to the end of the provided buffer. > + /// > + /// Fails with `EFAULT` if the read happens on a bad address. > + pub fn read_all(mut self, buf: &mut Vec, flags: Flags) -> Result { > + let len = self.length; > + buf.reserve(len, flags)?; (Reportedy by Miguel) When compile with `make rusttest`, kernel crate is compiled as userspace program, so we need to explicitly pick where the `reserve` comes from (Vec or VecExt), the current version will hit the following error: error[E0061]: this method takes 1 argument but 2 arguments were supplied --> rust/kernel/uaccess.rs:296:13 | 296 | buf.reserve(len, flags)?; | ^^^^^^^ ------- | | | | | unexpected argument of type `Flags` | help: remove the extra argument | note: method defined here --> /home/boqun/linux-rust/rust/test/sysroot/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:910:12 | 910 | pub fn reserve(&mut self, additional: usize) { | ^^^^^^^ error[E0277]: the `?` operator can only be applied to values that implement `Try` --> rust/kernel/uaccess.rs:296:9 | 296 | buf.reserve(len, flags)?; | ^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `()` | = help: the trait `Try` is not implemented for `()` error: aborting due to 2 previous errors Some errors have detailed explanations: E0061, E0277. For more information about an error, try `rustc --explain E0061`. and we need to the following fix diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs index 39481e374c40..80f7e7ca2f5e 100644 --- a/rust/kernel/uaccess.rs +++ b/rust/kernel/uaccess.rs @@ -293,7 +293,7 @@ pub fn read(&mut self) -> Result { /// Fails with `EFAULT` if the read happens on a bad address. pub fn read_all(mut self, buf: &mut Vec, flags: Flags) -> Result { let len = self.length; - buf.reserve(len, flags)?; + VecExt::::reserve(buf, len, flags)?; // The call to `try_reserve` was successful, so the spare capacity is at least `len` bytes // long. Regards, Boqun > + > + // The call to `try_reserve` was successful, so the spare capacity is at least `len` bytes > + // long. > + self.read_raw(&mut buf.spare_capacity_mut()[..len])?; > + > + // SAFETY: Since the call to `read_raw` was successful, so the next `len` bytes of the > + // vector have been initialized. > + unsafe { buf.set_len(buf.len() + len) }; > + Ok(()) > + } > +} > + > +/// A writer for [`UserSlice`]. > +/// > +/// Used to incrementally write into the user slice. > +pub struct UserSliceWriter { > + ptr: UserPtr, > + length: usize, > +} > + > +impl UserSliceWriter { > + /// Returns the amount of space remaining in this buffer. > + /// > + /// Note that even writing less than this number of bytes may fail. > + pub fn len(&self) -> usize { > + self.length > + } > + > + /// Returns `true` if no more data can be written to this buffer. > + pub fn is_empty(&self) -> bool { > + self.length == 0 > + } > + > + /// Writes raw data to this user pointer from a kernel buffer. > + /// > + /// Fails with `EFAULT` if the write happens on a bad address, or if the write goes out of bounds > + /// of this [`UserSliceWriter`]. This call may modify the associated userspace slice even if it > + /// returns an error. > + pub fn write_slice(&mut self, data: &[u8]) -> Result { > + let len = data.len(); > + let data_ptr = data.as_ptr().cast::(); > + if len > self.length { > + return Err(EFAULT); > + } > + let Ok(len_ulong) = c_ulong::try_from(len) else { > + return Err(EFAULT); > + }; > + // SAFETY: `data_ptr` points into an immutable slice of length `len_ulong`, so we may read > + // that many bytes from it. > + let res = unsafe { bindings::copy_to_user(self.ptr as *mut c_void, data_ptr, len_ulong) }; > + if res != 0 { > + return Err(EFAULT); > + } > + self.ptr = self.ptr.wrapping_add(len); > + self.length -= len; > + Ok(()) > + } > +} > > -- > 2.44.0.683.g7961c838ac-goog >