From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 7912033CE87 for ; Fri, 17 Oct 2025 11:11:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760699474; cv=none; b=hbBiTvgqddMd9pVt7wVFkYWM9uiOro4wJqdIynyYSqGjfjaWUJZkrZke6YWj0IavAYTF/Dc+7ZDtvY/RwSqV0NO0UVqN99yY/C/bAP5R9Cia7aZ4vvyiYpluWaGzAY7Z9nccUmxZJYYFsc1VURx5QTJrcJiylSylsbqHan52jks= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760699474; c=relaxed/simple; bh=MhZSD4MoKQuIdmvJLptvhaWaCOk0x2r/AtANTi0X07M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n7eK2iRc5vjCZi7ThOB2LUhjD3lyESVsNtoPIC5F9TFk4TET9Qhm+omlmyvgTXLSC/8r+EOHSBofHXGJJKEAN0o8GTuBsxc1vCFJ5GR+LxemUKXcRmQwXUA6Sr20Qeu3aU+KSW0vhAk+Emf+vwkcGq72XOKWYNepda33QV4N9UU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lmDcMGPJ; arc=none smtp.client-ip=209.85.221.74 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=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lmDcMGPJ" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-427027a2095so1464630f8f.2 for ; Fri, 17 Oct 2025 04:11:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1760699471; x=1761304271; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i2wzK6MiosI9/ERAVMVfGUCr8MWHBxLmt48MiuCJv8g=; b=lmDcMGPJEblBIjiFQ4SqKsYaWNOqQUYi1CXR8L/6uElRcQR+9C2NOEHJv6BC8D0cqK aXx7tZb6X+bMZ09gqnfhx7ZAntKRK/QLvsT/N8/kjxFAHFdZKkZ910/edYyrggU9RdIb PfMUApfOYzPI6Yc/46E4H3+fvJAv4k8jpb92VVr6ioRMzVnPw4ALPzLxL1cnO3dD1v1v UxAG3wBSsght6vQYY66pE+NADy0duvEQSAxsuJ9PVPf1I1ggnAvT/VLWMnpu5JcydSoo +hsab8Ik7ZM0zZfqwiRbD52O+J0GFQU67s5tFnLCbCaWnIC9WhomWnmACR1NJ88YDqRW 7oug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760699471; x=1761304271; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i2wzK6MiosI9/ERAVMVfGUCr8MWHBxLmt48MiuCJv8g=; b=YJCEUI5GIXlCCL0GuCofKscpFV4McP2sB6yHKaWBdzwOeXyJKUnpE2SuW8iBfRccCZ VXC6HoIfcwppWs6PHyjjjW603NJDG8TwXoan04WAGsTyaQRneMxs8hdqgxlW2DFevcsp npQ+IIYrfLPqOsH/YKtD46YqS70+rZCpp9dWKKPIyR/Gatp/N5LLTcZPwUm798Ernj6f 786N/gbDxWGbidEFvVz8RttiDw0uY8Rm5Ff40LvWuK/L/pDtdOoipa10/V8LQLXrHa17 mGPdFxgw0QEsZXHWx+KwULcxBC6V/YL0dCIwFOerHflR2v3gBM8P5aAMIr/YnywOvl0l Db8g== X-Forwarded-Encrypted: i=1; AJvYcCXvKk01eBOGdTKT4ATBumTWGj9RAVXCzzJGKQyLwE6jzKt03OH+yi4EI8U6FNWJ2VNbdIow9nWqIehJaZ0atQ==@vger.kernel.org X-Gm-Message-State: AOJu0Yx8EijT/orWKX/l/oM0TTYWHxslle0uZDzbaNxZal1wCbz9sgnf 1uJUWOYVG+iAOZmZ4yKbo/aluIojJ+a+GjiPZGG2r8IYAJ+70PUr5g7DurmNJEh33PVnQSlXADA Z3zjKTWzaxqs+TcuymA== X-Google-Smtp-Source: AGHT+IHYFyQyj1kLDpdNGuhEdH6AEjt4MzESRGh/PCle0M85/6aN3reSFKPj5aDv/9IoZZ+HFF5idXBzSlPyeVo= X-Received: from wmbg27.prod.google.com ([2002:a05:600c:a41b:b0:46c:db55:f87]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2911:b0:426:ffaa:8e9c with SMTP id ffacd0b85a97d-42704d83d30mr2587924f8f.13.1760699470868; Fri, 17 Oct 2025 04:11:10 -0700 (PDT) Date: Fri, 17 Oct 2025 11:11:10 +0000 In-Reply-To: <20251003222729.322059-2-dakr@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251003222729.322059-1-dakr@kernel.org> <20251003222729.322059-2-dakr@kernel.org> Message-ID: Subject: Re: [PATCH 1/7] rust: uaccess: add UserSliceReader::read_slice_partial() From: Alice Ryhl To: Danilo Krummrich Cc: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu, mmaurer@google.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Sat, Oct 04, 2025 at 12:26:38AM +0200, Danilo Krummrich wrote: > The existing read_slice() method is a wrapper around copy_from_user() > and expects the user buffer to be larger than the destination buffer. > > However, userspace may split up writes in multiple partial operations > providing an offset into the destination buffer and a smaller user > buffer. > > In order to support this common case, provide a helper for partial > reads. > > Signed-off-by: Danilo Krummrich > --- > rust/kernel/uaccess.rs | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs > index a8fb4764185a..1b0b57e855c9 100644 > --- a/rust/kernel/uaccess.rs > +++ b/rust/kernel/uaccess.rs > @@ -287,6 +287,19 @@ pub fn read_slice(&mut self, out: &mut [u8]) -> Result { > self.read_raw(out) > } > > + /// Reads raw data from the user slice into a kernel buffer partially. > + /// > + /// This is the same as [`Self::read_slice`] but considers the given `offset` into `out` and > + /// truncates the read to the boundaries of `self` and `out`. > + /// > + /// On success, returns the number of bytes read. > + pub fn read_slice_partial(&mut self, out: &mut [u8], offset: usize) -> Result { > + let end = offset.checked_add(self.len()).ok_or(EINVAL)?.min(out.len()); Should this be? let end = offset.checked_add(self.len()).unwrap_or(out.len()).min(out.len()); > + out.get_mut(offset..end) > + .map_or(Ok(0), |dst| self.read_slice(dst).map(|()| dst.len())) So if out.len() < offset, then we return Ok(0)? Alice