From nobody Sat Apr 4 00:23:42 2026 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.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 9977128689B for ; Sat, 21 Mar 2026 14:34:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774103686; cv=none; b=RnYR8Awo1q+YaRnZi709HqI/oLTAByBt0xlAlyPus/JpU1wttbwSXxpFRX6iLnqb0ZpsLXY5bES78sDFdhI2fhM75gcTxtC8kImnvB07E2hpM4/Il2yDtjDhPiuWYGssNREHX2fWyAEAgzlDrcEPO7+CQYsQKTla814kdWvIjCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774103686; c=relaxed/simple; bh=jg3J/QbnOeA8e8Lkzp57SJ3jUfyIegtFeaJ8WstOylU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=PQ8qeWViWK+QsR5fP7liauYSRboPjqVd7ATPSJ/zfoRzgnaifT59/cRllhLR8EIaR1uG+oLoKjR3Fc/1lfZRJxKs6nMssTtugr6+C8NrysRsk7vB0nTXc64zeIrjvfCVx7FOkQ8x42kiWex5rBYzWuo983RbxCKM6jWvMzCvRQQ= 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=eSMO/BUM; arc=none smtp.client-ip=209.85.210.176 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="eSMO/BUM" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-82418b0178cso1469369b3a.1 for ; Sat, 21 Mar 2026 07:34:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1774103685; x=1774708485; 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=ZjK3Ei6VCai7bQqO8E9VEEw85prRc4SetFe1e4pxmFU=; b=eSMO/BUMsHaLXgSMVp528C6NA+jqhUg5HEdUjD1F7HM1wvoRprdoZJL8wy0YsIehBv ylfzaNbyxdqM6Iy1ysU7N1l7VKJ0PqQthaJgLu4D3u/cYsD4wk82Ln06rhfvfKVVTyWq L0jhMT3m7+sDld1jtDnPfTI/7rOXnAVgmVF0FaPg6r1jS2ImcqSBilcsLsnWaPOrat9p DH9qgHjuOKbTmNucSkbPiHWfKS4H1cylald0MCzPTB7z4W66UBqrba+22F1Z/R5LrOOm OKMhIuY+4pycqn4v4s32lm46gGaVeOFLjaYas9mIe674XNzsQWP0+1/U3mo0yqEi4Zoo 20kQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774103685; x=1774708485; 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=ZjK3Ei6VCai7bQqO8E9VEEw85prRc4SetFe1e4pxmFU=; b=jAOTSCvQPQKY9XvUr6Fs2NK+xsv4DgpyEiGCTDeUCY4fOkZ4e4xZL4mhxeD8LAM4jE CL92CNyRgFVeW39XS3uJ1e/xUcFJQf6cXFvwMb+4lwPh0CNgk4gEsOzkneEBbg2SSWCV Uo2bU+O7V2eOKFq+21jsglL/AGFUngm49lxMNgAWnM+BcB6CFbsK9XKmlizjhk+49SCm E1Z+FjjciB6QU0rVGTHx/F8LBfFPGKGxNhm3W+Cwg7VBZ1RqAYQU+GkXGdY0qyQmiLt0 V6bliTSkVE9lQWjP4mzrbHxaoagZ8Vf7bpisd+E8QwC/Z3/xSI1oIZCvyuoiUfWHIldy qqnA== X-Forwarded-Encrypted: i=1; AJvYcCXb+jR5bxHOFJgSgC7MOVBJOSJe/Yw3eew20HSKcUAHoNNuVVuKlwCWglYoduqq/cHdvdUh5cx3ng8Z20E=@vger.kernel.org X-Gm-Message-State: AOJu0Yzmbs/fY0SGDPosK8yohoi4VGNTdahqUWRFeDEMu3i50cu33EVm zBeldYqLuQE1mEaBx3SaF79rA6jLSZQR+9SAY/DxANdaeL66qJGklXpu X-Gm-Gg: ATEYQzxOT/mw6+4oj8Jk4OdVMQYxZD3RKK/YZQeD94LMcKuxaY8fW6tWmmFeJy3enM+ zzfHJlSI6xpbtZtpU0cOFpp8O/5Go0q2bECn9ffch1CIdy9hZ6+89EPI2rVZPPc1R/meBL2mKpN dHkAw/88mJvz6+l8Z9gL+Lb4cH5du4IV4HJ17pAjI1byo0bh7uppZJP486d/9gG0Pdl+INQ48J2 iRu2G29va3IfjGfzoRmshA3wO5BVrnA++iMypz0YY5zuEVOkOwanPMBEzoznX8L7TcCIvhrsO5H FFXPJ1VV/9ACtXGTsQOWbh5JxB5tZeMUREKHmTVjEFuwN24xJaGlK1Vd+WYnI5QvmPf3Edkj6e7 JcQLgihf3If2hOSODC+0bFKsraTUrV7B+hCJel/Sy5kZMGQqpUBUkHwkq+jdQ/RlfyUIqixnCCG UAZgoRafafthP9k4tA90A18fSKVC5jM7thL5rKS+fw/EZTuNU5BTZhO/QYYbql70Xbp/OTRQ== X-Received: by 2002:a05:6a00:27a5:b0:820:2f9b:fe31 with SMTP id d2e1a72fcca58-82a8c346e32mr5536160b3a.30.1774103684919; Sat, 21 Mar 2026 07:34:44 -0700 (PDT) Received: from albab.. ([114.130.187.48]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82b0354baefsm4805972b3a.0.2026.03.21.07.34.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Mar 2026 07:34:44 -0700 (PDT) From: Albab Hasan To: Miguel Ojeda Cc: Albab Hasan , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rust: print: add SAFETY comments to unsafe blocks Date: Sat, 21 Mar 2026 20:33:57 +0600 Message-ID: <20260321143406.67066-1-albabhasan276@gmail.com> X-Mailer: git-send-email 2.43.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" Replace the placeholder // SAFETY: TODO. comments with proper safety descriptions for the two unsafe blocks in rust_fmt_argument and call_printk. For rust_fmt_argument the ptr parameter is provided by the %pA format specifier handler in vsprintf that guarantees it points to a valid properly aligned fmt::Arguments<'_> value. since fmt::Arguments implements Copy the dereference is a bitwise copy with no side effects. For call_printk the safety depends on the functions documented preconditions the format string is one of the fixed compile time constants from format_strings and the module name is null terminated. the arguments match the format specifiers (%s for module name %pA for the fmt::Arguments pointer). Signed-off-by: Albab Hasan --- rust/kernel/print.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 6fd84389a858..8e1029b0a0b4 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -29,7 +29,9 @@ use fmt::Write; // SAFETY: The C contract guarantees that `buf` is valid if it's less = than `end`. let mut w =3D unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast())= }; - // SAFETY: TODO. + // SAFETY: `ptr` is provided by the `%pA` format specifier in `vsprint= f` which guarantees + // that it points to a valid, properly aligned `fmt::Arguments<'_>` va= lue for the lifetime + // of this function call. let _ =3D w.write_fmt(unsafe { *ptr.cast::>() }); w.pos().cast() } @@ -109,7 +111,10 @@ pub unsafe fn call_printk( ) { // `_printk` does not seem to fail in any path. #[cfg(CONFIG_PRINTK)] - // SAFETY: TODO. + // SAFETY: `format_string` is one of the fixed `format_strings::*` con= stants, which are + // valid null-terminated C format strings. `module_name` is guaranteed= by the caller to be + // null-terminated. `&args` points to a valid `fmt::Arguments` on the = stack, passed as + // `%pA` which `_printk` will forward to `rust_fmt_argument` for rende= ring. unsafe { bindings::_printk( format_string.as_ptr(), --=20 2.43.0