Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 AE7FA377EA2 for ; Fri, 24 Jul 2026 23:38:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784936320; cv=none; b=O2aRZCG2fBeSiXl9laxFonn201l38r/2mfUXQxZJ863qRcZiCLQA4lZJyW/kZ0/p0nOoqWH5Jq8pMpStIbQX0btvdwMq85AJsO+Xb2t/8V/8AIeagbYYEKohRKr7/5lOvIxDO8SJayytBv4zna6f45ZJ+IZwQ6nO25wAS7uEBjQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784936320; c=relaxed/simple; bh=R4aeED2qviRrSYHBk8tIzRI9ITwvLrUADpb7MhsLkTI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=lo/XpEMfZwDxT/coPC2OrAhSLzZ5Wd/G9Ck5vmldAbwHxFvoKeM6ouk6Kulc5fpfDy5x+eLFfP5lBAAzn3PCs0y41Z9jXorvXDz2k9VADiD3d0R3Vo3Ss+lxM7aYJiJRoPEIt+S1NY8u09yYnVE3ee7f8klXqwCahjW4YNdBeuA= 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=RNRLJxa8; arc=none smtp.client-ip=209.85.128.49 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="RNRLJxa8" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-49554ebb87dso8675765e9.3 for ; Fri, 24 Jul 2026 16:38:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784936317; x=1785541117; 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:content-type; bh=f39CLR4g0AB+NJDog9MggUnlDTuwvf2U9/I9mdNOEyg=; b=RNRLJxa87EOc75cIJkCOvjGgM5bWpiQaIeuukyjolgotWMTqgdrwfCS42s+WxSsRm/ WRaSr/e1GrSIPAFCdzk5VCo+tEHl5xYQqknlQ66j8B8hjvYmUF/V/yHoImKPJQBNah4b 5QV5hEzuGmIzUO7xruhgvxqQ2e4xNwM1FLtx7amifubp3QhFzkIk7asYrq0mDV4UrZ2Y /U2/NszVNMzyiyjX7ezi4l4Zwi6Gu4+1ys4cwSQ5G/XCP1SM3cWZ9JsMpkOf9oiLnK48 CIVeE6MRECuvjTflEDO57U0eWgP1a/BJkNGCpBPp4UWghEityK6fCdY8Yi74eyAOdIUB +t9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784936317; x=1785541117; 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:content-type; bh=f39CLR4g0AB+NJDog9MggUnlDTuwvf2U9/I9mdNOEyg=; b=esyN/oLbkd72RS0dNIlcvdkRn16Xz83Vzv6fPKuNxSNu/eSINoeLlBq1mrbAH2j+mx c+xlGHtGOSYHbY1L0OPPQI24KalXZhh83Vp0gPy1Xkx3nAmPkdmO26hQqLnezzc84G74 OnAEgWEHOjjAC+bSgI9nIzbfCBbDCBqcNEGA/Jpo0KH0DzBowl4AQY0qywMwBwQIHQm+ R51/yYk8UC5slMNDirhQyaXG7Dzu2x39cfCnd2aofqCSn5snnVwqX68LXTf5zHTgUsB1 TbwGwZN+ewMf3jqdVyoTkTY6cJjRN0AggxQdqpnZtIl3nd/q9ChcgEHJW4PAm/tfLT8U xdQA== X-Forwarded-Encrypted: i=1; AHgh+RqIUUWslCsPkAdqgzXJsGMjQjLyaEN0tEwfUcLcZwfKuBN7KUKjoVLw9t7pz5Qvp4K8M1xgczQASFGsyK8=@vger.kernel.org X-Gm-Message-State: AOJu0YxQEAhlteK5Dr6t3JG02nNqU56noz77lC2TkwNMUViE+688hMw9 BMxmpiKXC7/ZqiL7cWRpo2X2zLZtDSVA3+hjm6e2yQofN3c/b1Ez7GPU X-Gm-Gg: AR+sD120pr9yW248NLTdDUBaqRArPoaZsZ9qN6AAfA38G9sZkbvi3Bw2E7RVvTVjep/ wTbjwudHMYDLWEAQ/ex0v/566x4/wLVJi2gUZtoO8Vx1Dqr5mDwiXvyqFo1s2zOS6l6jQut6rRa k/CYIbpRsjNAFWvOkZxuUevWW3KotBCt9GEIvqJYRlgyAe94Mg61iiRxBQ+9PVVlJmwlFsDyChY CIsYkc2JE52+od7g4ActzDb45He3j+khIapbPEH7oRxTuFMo9qAk847MSYRtgtG6ykqD8HJ/BXI os0xrpKPeRbFGgZwlA8PIrxM9J0gDwjzmxD2bEELj/u9ofw4ql4Uf2+a3TPOvxFl91a/Llsi3qU fwW2JPWEb5pc1+8luRByJhNLe1EF8rEolwbWfz3Rbfr4Hx+KYXwS3ffao80ili7wToeUuNKbI+N SwVio1NI5XAzdeXCM8mM4r6TZ7kqyGFNx0DGSGn6gbsl5/ucdfNlwHSg7X X-Received: by 2002:a05:600c:4e0d:b0:495:5b4a:53b7 with SMTP id 5b1f17b1804b1-496b56f143dmr4023275e9.19.1784936316788; Fri, 24 Jul 2026 16:38:36 -0700 (PDT) Received: from Cian.taile2c8f4.ts.net ([2a02:8084:d03:fa00:819d:8e1d:c966:142d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4957b5f35a1sm72226775e9.0.2026.07.24.16.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jul 2026 16:38:36 -0700 (PDT) From: Cian McGuire To: Miguel Ojeda Cc: Boqun Feng , Gary Guo , Alice Ryhl , Lyude Paul , Daniel Almeida , =?UTF-8?q?Onur=20=C3=96zkan?= , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Alexandre Courbot , FUJITA Tomonori , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Cian McGuire Subject: [PATCH v2 1/4] rust: aref: document safety of ARef doctest example Date: Sat, 25 Jul 2026 00:38:22 +0100 Message-ID: <20260724233825.1321247-1-cianmcguire167@gmail.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The `into_raw` doctest contains two `unsafe` items whose SAFETY comments were left as "TODO.": the `AlwaysRefCounted` impl for the example's `Empty` type, and the subsequent `ARef::from_raw` call. Both are sound for the same reason: `Empty`'s `dec_ref` is a no-op and never deallocates anything. `AlwaysRefCounted`'s "keep alive until matching decrement" guarantee therefore holds vacuously here -- no decrement in this impl ever frees the object, so it cannot be freed while a live increment remains. The object's actual lifetime is governed entirely by ordinary Rust scoping instead: `data` is a stack value that outlives every use of `ptr`/`data_ref`/`raw_ptr`, none of which is ever dereferenced in the example. The same fact justifies the `from_raw` call. Elsewhere in this file, `Clone` and `From<&T>` justify their own calls to `from_raw` by pointing at a preceding `inc_ref()` call, but no such call happens in this example. That is fine here specifically because `Empty` does not track a real reference count at all, so there is no accounting for `from_raw` to violate, regardless of whether an increment "really" occurred. Suggested-by: Miguel Ojeda Link: https://github.com/Rust-for-Linux/linux/issues/351 Signed-off-by: Cian McGuire --- v2: - Reworked the `AlwaysRefCounted for Empty` comment: v1 only argued the `dec_ref` half of the trait's contract; now it also explains why the no-op `dec_ref` means nothing is ever freed through this impl, so the "keep alive until matching decrement" guarantee cannot be violated, and notes neither pointer is dereferenced in the example. - Reworded the `from_raw` comment to state concretely which invariant is vacuous, instead of just asserting "vacuously satisfied". rust/kernel/sync/aref.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs index b721b2e00b98..37dcd0a0f406 100644 --- a/rust/kernel/sync/aref.rs +++ b/rust/kernel/sync/aref.rs @@ -124,7 +124,11 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// struct Empty {} /// - /// # // SAFETY: TODO. + /// # // SAFETY: `dec_ref` is a no-op and never deallocates `data`, so the object's + /// # // lifetime is governed entirely by ordinary Rust scoping, not by this impl; the + /// # // trait's "keep alive until matching decrement" guarantee holds vacuously because + /// # // no decrement here ever frees anything, and neither `ptr` nor `raw_ptr` is ever + /// # // dereferenced in this example. /// unsafe impl AlwaysRefCounted for Empty { /// fn inc_ref(&self) {} /// unsafe fn dec_ref(_obj: NonNull) {} @@ -132,7 +136,9 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// let mut data = Empty {}; /// let ptr = NonNull::::new(&mut data).unwrap(); - /// # // SAFETY: TODO. + /// # // SAFETY: `from_raw`'s contract concerns a reference count that `Empty` doesn't + /// # // actually have; since `inc_ref`/`dec_ref` never touch any state, there is no + /// # // invariant here for `from_raw` to violate. /// let data_ref: ARef = unsafe { ARef::from_raw(ptr) }; /// let raw_ptr: NonNull = ARef::into_raw(data_ref); /// -- 2.55.0