public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v8 0/4] rust: Add safe pointer formatting support
@ 2026-01-01  8:16 Ke Sun
  2026-01-01  8:16 ` [PATCH v8 1/4] lib/vsprintf: Export ptr_to_hashval() for Rust kernel crate use Ke Sun
                   ` (3 more replies)
  0 siblings, 4 replies; 20+ messages in thread
From: Ke Sun @ 2026-01-01  8:16 UTC (permalink / raw)
  To: Dirk Behme, Boqun Feng, Miguel Ojeda, Petr Mladek, Steven Rostedt,
	Timur Tabi, Danilo Krummrich, Benno Lossin, Alice Ryhl
  Cc: John Ogness, Andy Shevchenko, Rasmus Villemoes, Andrew Morton,
	Gary Guo, Björn Roy Baron, Andreas Hindborg, Trevor Gross,
	Tamir Duberstein, Ke Sun, rust-for-linux, Ke Sun

This patch series adds safe pointer formatting support for Rust kernel code,
providing two pointer wrapper types (HashedPtr, RawPtr) that correspond to
C kernel's printk format specifiers %p and %px.

The implementation ensures that raw pointers are automatically hashed when
formatted with {:p}, providing safe default behavior that prevents information
leaks about kernel memory layout. Users can also explicitly use wrapper types
when they need specific formatting behavior.

---
v8:
- Remove RestrictedPtr (%pK) support: only export ptr_to_hashval() with
  EXPORT_SYMBOL_NS_GPL using "RUST_INTERNAL" namespace, provide only two
  pointer wrapper types (HashedPtr, RawPtr) for %p and %px
- Change API from HashedPtr::from(ptr) to HashedPtr(ptr) for direct construction

v7: https://lore.kernel.org/rust-for-linux/20251229072157.3857053-1-sunke@kylinos.cn/
- Refactor kptr_restrict handling: extract kptr_restrict_value() from
  restricted_pointer() in lib/vsprintf.c and export it for Rust use, and
  improve RestrictedPtr::fmt() implementation to directly handle
  kptr_restrict_value() return values (0, 1, 2, -1) for better code clarity
- Remove Debug derive from pointer wrapper types (HashedPtr, RestrictedPtr, RawPtr)

v6: https://lore.kernel.org/rust-for-linux/20251227033958.3713232-1-sunke@kylinos.cn/
- Fix placeholder formatting to use `f.pad()` instead of `f.write_str()` in
  format_hashed_ptr(), ensuring width, alignment, and padding options are
  correctly applied to PTR_PLACEHOLDER

v5: https://lore.kernel.org/rust-for-linux/20251226140751.2215563-1-sunke@kylinos.cn/
- Format use statements in rust/kernel/ptr.rs and rust/kernel/fmt.rs using kernel
  vertical style with alphabetical ordering
- Remove unnecessary SAFETY comment in rust/kernel/ptr.rs (addressed Clippy warning)
- Update type ordering to alphabetical (HashedPtr, RawPtr, RestrictedPtr) in
  fmt.rs macro invocation

v4: https://lore.kernel.org/rust-for-linux/20251225225709.3944255-1-sunke@kylinos.cn/
- Use Pointer::fmt() instead of write!(f, "{:p}", ...) to preserve formatting
  options (width, alignment, padding characters)
- Improve code structure: reduce unsafe block scope, use early return pattern
- Add doctests with formatting option tests for all pointer wrapper types
- Enhance documentation with detailed formatting options section, including
  examples for width, alignment, and padding
- Fix RestrictedPtr example to use pr_info! instead of seq_print! in docs

v3: https://lore.kernel.org/rust-for-linux/20251224081315.729684-1-sunke@kylinos.cn/
- Export ptr_to_hashval() from lib/vsprintf.c for Rust pointer hashing
- Add three pointer wrapper types (HashedPtr, RestrictedPtr, RawPtr) in
  rust/kernel/ptr.rs corresponding to %p, %pK, and %px
- Make raw pointers automatically use HashedPtr when formatted with {:p}
- Add documentation for pointer wrapper types

v2: https://lore.kernel.org/rust-for-linux/20251223033018.2814732-1-sunke@kylinos.cn/
- Disabled {:p} raw pointer printing by default to prevent accidental
  information leaks

v1: https://lore.kernel.org/rust-for-linux/20251218032709.2184890-1-sunke@kylinos.cn/
- Initial implementation that directly printed raw pointers
---

Ke Sun (4):
  lib/vsprintf: Export ptr_to_hashval() for Rust kernel crate use
  rust: kernel: Add pointer wrapper types for safe pointer formatting
  rust: fmt: Default raw pointer formatting to HashedPtr
  docs: rust: Add pointer formatting documentation

 Documentation/rust/index.rst              |   1 +
 Documentation/rust/pointer-formatting.rst |  90 +++++++++++++++
 lib/vsprintf.c                            |   1 +
 rust/kernel/fmt.rs                        |  64 ++++++++++-
 rust/kernel/ptr.rs                        | 133 +++++++++++++++++++++-
 5 files changed, 285 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/rust/pointer-formatting.rst

base-commit: 7839932417dd53bb09eb5a585a7a92781dfd7cb2
-- 
2.43.0


^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2026-01-06 21:06 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-01  8:16 [PATCH v8 0/4] rust: Add safe pointer formatting support Ke Sun
2026-01-01  8:16 ` [PATCH v8 1/4] lib/vsprintf: Export ptr_to_hashval() for Rust kernel crate use Ke Sun
2026-01-02 12:15   ` Andy Shevchenko
2026-01-01  8:16 ` [PATCH v8 2/4] rust: kernel: Add pointer wrapper types for safe pointer formatting Ke Sun
2026-01-02  7:57   ` Dirk Behme
2026-01-02 11:06     ` Gary Guo
2026-01-02 11:13   ` Gary Guo
2026-01-02 12:17   ` Andy Shevchenko
2026-01-02 12:33     ` Danilo Krummrich
2026-01-02 12:43       ` Ke Sun
2026-01-01  8:16 ` [PATCH v8 3/4] rust: fmt: Default raw pointer formatting to HashedPtr Ke Sun
2026-01-02 11:17   ` Gary Guo
2026-01-02 17:39   ` Petr Mladek
2026-01-05  8:19     ` Alice Ryhl
2026-01-06  3:06       ` Ke Sun
2026-01-06 16:45       ` Petr Mladek
2026-01-06 17:18         ` Alice Ryhl
2026-01-06 21:00           ` Andy Shevchenko
2026-01-06 21:06             ` Alice Ryhl
2026-01-01  8:16 ` [PATCH v8 4/4] docs: rust: Add pointer formatting documentation Ke Sun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox