From nobody Sat Feb 7 16:00:07 2026 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 B552C326D45 for ; Wed, 28 Jan 2026 20:21:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769631705; cv=none; b=G6/H1EtlH45VdpFivGbKwBmxtkzZi6G5tIVo4IUdjvVOk77zFVxb7maJtNtp7cDQH3BXg/uSSCVehe12x2cs/lVqz1ztfeVbUAzzCjBKTeMo1e658jqI1Y+7G0ugr83IMz7opa9OqDgGLMKrSeVt+TQCCo3w1TgNSD07DoC43rI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769631705; c=relaxed/simple; bh=fxSRMnS+DTdOe/9PQuED4usCl8i1K+SEzjMJ8MhoJtc=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=buMOoQesbAbk1mCz84LwSiDBDIigdlDCE9BkTHSCCKMfR9f4LB6xFj4doPKfXzrj47lB4TubqIOzqQA03IrN1ry05yIXKejIV6sHZgVGzNZCdqAbPGJ2je41rpCperA/Nu2eOd9OAGzTV1N906ABgMBz9AGYe4q9SDuWfvvLqQg= 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=kTpaboKw; arc=none smtp.client-ip=209.85.214.195 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kTpaboKw" Received: by mail-pl1-f195.google.com with SMTP id d9443c01a7336-2a12ebe4b74so2180475ad.0 for ; Wed, 28 Jan 2026 12:21:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769631704; x=1770236504; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=gHGLmxFDguwmEL7MTwAtIpFUj1gQFdHnrXFkxEwQ3gA=; b=kTpaboKwK5CXaVp53TOAGFvBhypJpCfr+nEK4TPDuHVk2HKhqoF+V5H2eRf7w+ylbU ctXV5bttJuqj5rszDr6l2xOsujEdw+H8whZ4j1BG4x2AcWb+mGFBD0UjjUf61CRCL2WH lsFMilhkarXr+uvImfcisJAb5HlgSwL0Nh20My3AnsqcjqIXrpmIaD/8jWKmUQ8nzg2n FHy3zaYOtdzYY0nFcNUSAPemWrJNX19RHd7W2TBMEWVXOxtVOFRTUk7Kim2ccHP8zNjX Ffpvfn4SKFifJahwHVS8RJkq/5Oi8qtnlOLkeZ7WkP9b8KzRpta5zuq7RiOQO9VlXm5z kcxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769631704; x=1770236504; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gHGLmxFDguwmEL7MTwAtIpFUj1gQFdHnrXFkxEwQ3gA=; b=HpgXnm5VTq6ZJQkKWc9Xn4IvsBI+RE1YHP+wnNR4HSK5fzTGgjOA4nxrAvMlwyGNVA iBwJz3h46cNt0CMGohGTMI46bJ2ChzKKAKEhW5T1YHvQscUOREKETIuTvtmFpJnT8yPV J/Dy605nsabrpX/DPa6jykfQDTLdpRPQ0n7g6VZTam/M8Kv903bL4T9BK03DR+q9WWIt 3PjY7Vi6mGPjO/hBY2tBzTXMnlVgQh7X//n/qeyB9J9KDqjdXK3dIFxWx0OpaMMcHs19 5Afy9coKW5DCcrB2JTrTCGQ8UY2r7ZI7pZHBfGt12hHWszCkpnze8D5a3pCvE4KDONWQ OI5w== X-Forwarded-Encrypted: i=1; AJvYcCWRvs++uYwu5U5x8Fj5I8ZKGJaAEz5+LD6MF3o89vV8Uv4srfEH7yz2SYAm9Oy9RNDZtE3n59tDXrM23Cg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3tp5Kx5qVr9KKOkEZdxaR1IJXA/rx4FgAZPNQBN5Zs4pqY4sF tSO4kExJMB4UuS3z5sUadhFlZTvVg06i57hpP0crMAFi7hSrFmmuNZDs X-Gm-Gg: AZuq6aJyFCEdyqG8UDzwA2M5Zk/ckGMGc7HtrvFPIviFdceYvcfW8B33eu3onM8tt+h /E0lJSnWfQ21HzBnFinU1MoSTUob6dtHmSuDY7EhgIQlorgW6kWH5wfwDbZLk8obGysFlHTmosn ywFjYNJ1/B5cPTZ2tapdy2joHdj4ywAJzWnHk9vWBaZZGg15RW53oDr40RCGuWrSouJmmtRm3yF NHpBrFo8ZsIwolSHwdTePy+K5lLS7KgVT0at8gRDVXX1KoRUdicHkIyBOjsvvH9JO1gXxXgRTpx XqgXAulQMVU2thu8o3lPiVFT5LCHEa0kp54/gMAw4tUgPEKFc9A2ppdDYBSBLWh/2dwi5GrfB88 RwSf9JDuCto6MXED/qhX7iZ066J1I80Piyu/fevCRobj2EmuAkh04a+SrbGU1TAjKO0/d81Pt3Y 0MpIXU9PJCNwgjsdvjdooNR7sF X-Received: by 2002:a17:902:ce83:b0:2a1:e19:ff5 with SMTP id d9443c01a7336-2a870e19191mr61737335ad.38.1769631703952; Wed, 28 Jan 2026 12:21:43 -0800 (PST) Received: from xozo.localdomain ([117.250.76.240]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a88b4c3deasm30691385ad.57.2026.01.28.12.21.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 12:21:43 -0800 (PST) From: Shivendra Sharma To: ojeda@kernel.org, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, shivendra02467@gmail.com Subject: [PATCH] rust: kernel: document safety for rust_fmt_argument Date: Thu, 29 Jan 2026 01:51:30 +0530 Message-ID: <20260128202130.196419-1-shivendra02467@gmail.com> X-Mailer: git-send-email 2.52.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, `rust_fmt_argument` uses a TODO for its safety rationale and suppresses the `missing_safety_doc` clippy warning. Following feedback from a previous attempt [1], replace the suppression with a formal "# Safety" section and update the internal "// SAFETY" comments to reference these requirements. Additionally, add a comment to the C header file pointing to the Rust documentation for safety preconditions. [1] https://lore.kernel.org/rust-for-linux/CANiq72=3Db=3DZ+6dk4XWK6QWcKDJx7= SYXL0obAFFfWp8xpE1CFkkw@mail.gmail.com/ Signed-off-by: Shivendra Sharma --- include/linux/sprintf.h | 7 ++++++- rust/kernel/print.rs | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/linux/sprintf.h b/include/linux/sprintf.h index f06f7b785091..f915829cbba5 100644 --- a/include/linux/sprintf.h +++ b/include/linux/sprintf.h @@ -25,7 +25,12 @@ __scanf(2, 0) int vsscanf(const char *, const char *, va= _list); extern bool no_hash_pointers; void hash_pointers_finalize(bool slub_debug); =20 -/* Used for Rust formatting ('%pA') */ +/** + * Used for Rust formatting ('%pA'). + * + * Safety preconditions are documented in the Rust implementation + * (rust/kernel/print.rs). + */ char *rust_fmt_argument(char *buf, char *end, const void *ptr); =20 #endif /* _LINUX_KERNEL_SPRINTF_H */ diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 2d743d78d220..30da8b974398 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -14,7 +14,12 @@ }; =20 // Called from `vsprintf` with format specifier `%pA`. -#[expect(clippy::missing_safety_doc)] +/// +/// # Safety +/// +/// - `buf` must be valid for writes until `end`. +/// - `ptr` must point to a valid `fmt::Arguments` instance. +/// - The `fmt::Arguments` must remain valid for the duration of the call. #[export] unsafe extern "C" fn rust_fmt_argument( buf: *mut c_char, @@ -22,9 +27,12 @@ ptr: *const c_void, ) -> *mut c_char { use fmt::Write; - // SAFETY: The C contract guarantees that `buf` is valid if it's less = than `end`. + // SAFETY: The safety requirements of this function (see above) + // guarantee that `buf` and `end` define a valid range. let mut w =3D unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast())= }; - // SAFETY: TODO. + + // SAFETY: The safety requirements of this function guarantee that `pt= r` + // points to a valid `fmt::Arguments`. let _ =3D w.write_fmt(unsafe { *ptr.cast::>() }); w.pos().cast() } --=20 2.52.0