From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 806F332C8B for ; Sun, 12 Jul 2026 15:49:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783871365; cv=none; b=LrSQwFejDNrTHcXUJ4kdLIAYLssRhj9q1twSGDxk7VLNhHbZrXb0eBo8qUnbdQZkuMlNQ269r8hB+BQMDmMmsiL4sqlT6ZOz//g/cWg05/y/Yl8bC3qwEGt4SB0bbzMeuV0l2Pcc5muwYXQMhGCDa9TKr76LAgJYmG0Tw9416EQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783871365; c=relaxed/simple; bh=hWbRhZH2/ExHhSbV++KSjAbuk1/gQEjL0oR7v0HZxyw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jpD+yMFNgMJnkq4sv/igpjab13C23dg40CG5dJsLKvrEb/cbO7e86sMb++QylROytFteRReHJgLk6B65sOvz9fzyuUFlXp4geo8uetKRKo2qSgbKqA7/yTaHbEsizUpPABVrzOdU/oy1lNXQahMY/cr/rAPGlwGVN18PFucUDAs= 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=j1cKlw8B; arc=none smtp.client-ip=209.85.216.45 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="j1cKlw8B" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-38511175ad3so1940503a91.2 for ; Sun, 12 Jul 2026 08:49:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783871364; x=1784476164; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vuV8hhyXMBSw35UwBV3Vw5b5QvmJJCKQBJkLO6Tuw2U=; b=j1cKlw8BohNouHDFfo7IPnccWx9q3Noo3QtU4nR7sSs7wuLMB/tcjoLjT/y9m+KqwP BLSQMWfeh+sfguzSZuhy3fu9b76xoIdYbn8w+F6fovxmLKpeCExerKPXrGr2xmcZA8oD +xIQkJgR38Wa0NIlfdO573kptO+vebUwZLx+MYtdSf/W/parSSkHKM3yYcr+YxHjJi1C PqckjsFw0lvVnHAMH/ah5KTQdF/0eNZeq2Y3AV3HP8/4K8Z2t7kx2ede5G1osWhUvfQ3 Xp+8+0cKjFEsfMRU0vR81ZYNrOsHZrFjUirkTWVIyCisREnleACAi8CjIrlRGXwcERpF SOhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783871364; x=1784476164; h=content-transfer-encoding:mime-version:references:in-reply-to :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=vuV8hhyXMBSw35UwBV3Vw5b5QvmJJCKQBJkLO6Tuw2U=; b=E/WpRf4goC+K9RZspaOnty/wLD5vUjXAkZbEHXTq0Tnl/9iw/PTbPc5M8cIm/CphlB 3ia3RBEMb2MeFK2quz8U6s9SLglRJffkopkOMcr4fBPq+xyA+CD9+cEul2HL0FvHH5VY x1a50j9DgswwVJukJzQtEsTkd/MoJKuqZj/eLs/iTWEwPejpi9f8AmNT6U54nUtGoihW RLxUrDpoeF8LxJOu41jtUrBEdhh+J2T6Jp2rM/h0kJYbqu9NLaJIm+NnmcyT//fAQgIh Zaj+XhCvHS/ZJ5dJA0DpE2GWQ4pUGbvEP9e9QQPxozNkkIeCe0+MxAEtm0g10sOxhs+k fmyA== X-Forwarded-Encrypted: i=1; AHgh+RoS+5eoNqMpHcmHzbMbtQY+AFK7zLRKAmbr97daxypiKoqen6t8szQS2UEn+VDSalf8KRxDxYnGsXIbEIM=@vger.kernel.org X-Gm-Message-State: AOJu0YyLkhjngjsqQdjNtdGeYnd1PUwrYiJWIO+aoPencBTQnJSjclqq f8wcuBN91DEqEd3W2FjrsQWPl99QSmwYgeexDZ40hLb+/P+vuQdDpgFG X-Gm-Gg: AfdE7clZmVwWdYcHttE/CkWEjTBuKbtko4Kfg01sXcyHejYtrGmBryaTTJ4wrvzBUu8 bC8KewG9+W4JJXiVZxJACK804IXKwZ/S+I8PhDDVyTAqaXLu0ZWFPfyoZB4/df20xKmyGs4Re9m ov+U2VOMOM0+BejxIpehKjuQwsO4BLd3Y5Iw1PVD27cSwdWYzahvY0oM8hY9SQZ0CIvzPd+Od5O x7Pwnp29B/7VvlmBPC3Kc+3FR4co3PCLg34BkkxNzL0UTTCsOYwQu/D0sGqUNwhGo2ucq/ivIKq oZTBCXZP4CN6+Gr4FT4pxTfXE4XRxT0rmOH2WHSit7oPgWItxz1OBDANlLg9u8uoJLYLy0bkc/B 8I2xlv7iAFiMAyM4dVAgX4tyL8C+oEDi3tLIHqFNlnGzXz9GoqdPkvdy6//CTN9YwOPHt/OskW4 hlpAUO82WCYNCKcMdTIf9vC/qmnkKT9XPb5jj7Eg== X-Received: by 2002:a17:90b:57c5:b0:383:5a16:bd70 with SMTP id 98e67ed59e1d1-38dc75dc0fcmr5785697a91.16.1783871363754; Sun, 12 Jul 2026 08:49:23 -0700 (PDT) Received: from Excalibur ([157.51.238.219]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-31198cb2b99sm39268481eec.26.2026.07.12.08.49.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jul 2026 08:49:23 -0700 (PDT) From: christopherjoshy To: ojeda@kernel.org Cc: boqun@kernel.org, gary@garyguo.net, aliceryhl@google.com, lyude@redhat.com, daniel.almeida@collabora.com, work@onurozkan.dev, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org, tamird@nvidia.com, acourbot@nvidia.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Christopher Joshy Subject: [PATCH v3] rust: kernel: add SAFETY comments in aref doc example Date: Sun, 12 Jul 2026 21:19:13 +0530 Message-ID: <20260712154913.1779-1-christopherjoshy4@gmail.com> X-Mailer: git-send-email 2.55.0.windows.2 In-Reply-To: <20260712153028.896-1-christopherjoshy4@gmail.com> References: <20260712153028.896-1-christopherjoshy4@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Christopher Joshy Add the missing `// SAFETY` comments in the `ARef::into_raw` doc example, replacing the `TODO` placeholders from issue #351. For the `unsafe impl AlwaysRefCounted`, explain that `Empty` owns no data and has no destructor, so the no-op `inc_ref`/`dec_ref` cannot free or leak anything, and that this doc example keeps the backing object alive for the `ARef`'s lifetime. For the `ARef::from_raw` call, explain that `ptr` is derived from a valid reference (so it is valid) and that the required refcount increment is trivially owned because `Empty`'s `AlwaysRefCounted` manages no actual refcount. Link: https://github.com/Rust-for-Linux/linux/issues/351 Suggested-by: Miguel Ojeda Signed-off-by: Christopher Joshy --- 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 b721b2e00..3cf7bc27f 100644 --- a/rust/kernel/sync/aref.rs +++ b/rust/kernel/sync/aref.rs @@ -124,7 +124,10 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// struct Empty {} /// - /// # // SAFETY: TODO. + /// // SAFETY: `Empty` owns no data and has no destructor, so its `inc_ref`/`dec_ref` + /// // are no-ops that cannot free or leak anything. This doc example keeps the backing + /// // `Empty` (`data`) alive for the whole lifetime of `data_ref`, so the `ARef` never + /// // dereferences freed memory. /// unsafe impl AlwaysRefCounted for Empty { /// fn inc_ref(&self) {} /// unsafe fn dec_ref(_obj: NonNull) {} @@ -132,7 +135,10 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// /// let mut data = Empty {}; /// let ptr = NonNull::::new(&mut data).unwrap(); - /// # // SAFETY: TODO. + /// // SAFETY: `ptr` comes from a valid `&mut data` reference, hence it is non-null, + /// // aligned and points to a valid `Empty`. `ARef::from_raw` also requires owning a + /// // refcount increment; this is trivially met because `Empty`'s `AlwaysRefCounted` uses + /// // no-op `inc_ref`/`dec_ref` and so manages no actual refcount. /// let data_ref: ARef = unsafe { ARef::from_raw(ptr) }; /// let raw_ptr: NonNull = ARef::into_raw(data_ref); /// -- 2.43.0