From patchwork Thu Mar 30 04:39:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wedson Almeida Filho X-Patchwork-Id: 76897 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp883798vqo; Wed, 29 Mar 2023 22:17:24 -0700 (PDT) X-Google-Smtp-Source: AKy350Y7nOFd8UvP000l5y/NJsAxemSwLZsE2Kqh0X4BfK3SNiqJs3wlpqbvjkoS2/WYbNByyiQV X-Received: by 2002:a17:906:1685:b0:930:660d:8f92 with SMTP id s5-20020a170906168500b00930660d8f92mr23697544ejd.52.1680153444058; Wed, 29 Mar 2023 22:17:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680153444; cv=none; d=google.com; s=arc-20160816; b=Nly6A8HMeM8IxkWBVx8AamasLATVsXe5lxdGnbwLJp6KZQYdYqvhoVnBww04mBK6LT /lKZLLuatdHfBIepgsv6BbtDAlmf2X5tHCnlrH/VjkT3w9btLUehZbAImYHlCZUXAnnV dtWHXuVinqQZi2vwLdf7wXAUAly6NIhmyV7wsMa8z01EEiZKslTK5NpU8uVy35Xpt6P7 qFIlkcPWLcfQJNlBg0qU6NG+LvaJJsW6hHBP6S5oFBDwBJggwzJ3vdo5yRXYLLj/1h8e wQzz0HHLV+TVx0URGkv0O4dGwb9eg7BA9NHUa3m/68qSgS04jpVHLbxFg04O3FcK65sE Nc1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZtdTMwagWvTyqFDLE8n/H2MEvpwLqJxi8owZihcxfPE=; b=M4fS37ZjwX87EB5Kao038kwKN4vxDHt336xENttHmdV/jQyxdPOaP8eJkg3hWNRV3D 3vZhL0JQOOiAidwP0w5lGUQAOBQ4A8z2key9xy8C85Rua85hjqsa1FZeNV9+EZFJG2jv oNvusbgvBUKI/U7pRr7lUKnHuOOd/xAc3SX0NGS4kGRdCouP16mMke8aoZv56A790p8w bGkIQl/DgsdP6MjHAE9Y5U7J+CSlLzSqA48phwYiLfbBsT093SXAz71ZwTTbrMqGqgEF LXo5Sb6J/ieSZJfR/Lq+KolQM83RAn0wM47KMO0fju5Yk8xdKbdU7mJpaRrOwtefni3D 4/aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HAFHUZpW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e6-20020a50ec86000000b00501dd471e58si24055813edr.37.2023.03.29.22.16.59; Wed, 29 Mar 2023 22:17:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=HAFHUZpW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230146AbjC3Emx (ORCPT + 99 others); Thu, 30 Mar 2023 00:42:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbjC3EmQ (ORCPT ); Thu, 30 Mar 2023 00:42:16 -0400 Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 201F37690; Wed, 29 Mar 2023 21:41:39 -0700 (PDT) Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-17fcc07d6c4so4496091fac.8; Wed, 29 Mar 2023 21:41:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680151298; 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; bh=ZtdTMwagWvTyqFDLE8n/H2MEvpwLqJxi8owZihcxfPE=; b=HAFHUZpWxT3qSD2wPnoPLCaZ5UDuTIvrCv1uNN1BeOYbL4/IS6Rlz485uErRKtu69b Sz7poGHEm212a83mkkSRMnMFB2CAa+zgu3IBD1ftjFiaUSG6DrP3aXAx8WonZFQeM6+k ElIpLFxVGHzLjNgNkSWHg1RrXofiOwzWF9OY3zIVS8oyZodOUlgK/WJXd8xBX/yDRqEk /W8I39ws/8jWZy4/eYe+URo+XmOisZ/FU/Wg1ZJxYWlTfbbr/Kif2mLtXxkvZTHyL0er 3/Jv0xvE6BGQRqXplmDWW2bjTEOodfySEnj0VAJ8fRdXvsBtThzK+HNQx1bG8dZ2WKQG OgDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZtdTMwagWvTyqFDLE8n/H2MEvpwLqJxi8owZihcxfPE=; b=58zN0d5UBf9olSr+vPicz+4B3enqGIevWuxF84nfp5agmgTCA4kkeaBYXn6t4DdG8T Qy0ESHC/2hljOlD2O0Tmjn6ZXTejqSjQH7cLN4XDjR2rIw06STVba3ZbH3cBoUl5EdEt Rs38AwpuPm45uC+AYq0bLvSgNuoztKdS/XPFtrRvndxgHwjGlpdMl7SlNguBkmFL6tNd EgHl170FkJCZ3p2eQDk02OHde5Fyhv5Cben1UgQ+imQirg6fOM6u8foXm9vYKNbDx/vA /++3d4H0bQfWMX6W5to/g0ahWtZUIC3ulguMZWfNenf3fx35bo4qbP3weR0zpK/nlbxg 8MlQ== X-Gm-Message-State: AAQBX9eCZpfduexPBl8NfrN04bW71q3oq11WP4NESGBR62nJAgJ6yNMf peFH/xtxzd3/cze/zAJnLI/wn3X/1TTVvA== X-Received: by 2002:a05:6870:968b:b0:177:caaa:4cc8 with SMTP id o11-20020a056870968b00b00177caaa4cc8mr15409780oaq.20.1680151297693; Wed, 29 Mar 2023 21:41:37 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id ea43-20020a056870072b00b0017e0c13b29asm7518599oab.36.2023.03.29.21.41.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:41:37 -0700 (PDT) From: Wedson Almeida Filho To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: [PATCH 13/13] rust: sync: introduce `LockedBy` Date: Thu, 30 Mar 2023 01:39:54 -0300 Message-Id: <20230330043954.562237-13-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330043954.562237-1-wedsonaf@gmail.com> References: <20230330043954.562237-1-wedsonaf@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761768577979979805?= X-GMAIL-MSGID: =?utf-8?q?1761768577979979805?= From: Wedson Almeida Filho This allows us to have data protected by a lock despite not being wrapped by it. Access is granted by providing evidence that the lock is held by the caller. Signed-off-by: Wedson Almeida Filho --- rust/kernel/sync.rs | 2 + rust/kernel/sync/lock.rs | 2 +- rust/kernel/sync/locked_by.rs | 126 ++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 rust/kernel/sync/locked_by.rs diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index d6dd0e2c1678..f8edb6d0d794 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -10,10 +10,12 @@ use crate::types::Opaque; mod arc; mod condvar; pub mod lock; +mod locked_by; pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use condvar::CondVar; pub use lock::{mutex::Mutex, spinlock::SpinLock}; +pub use locked_by::LockedBy; /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. #[repr(transparent)] diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f52ba9ab1b70..51c996ca2109 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -111,7 +111,7 @@ pub struct Lock { _pin: PhantomPinned, /// The data protected by the lock. - data: UnsafeCell, + pub(crate) data: UnsafeCell, } // SAFETY: `Lock` can be transferred across thread boundaries iff the data it protects can. diff --git a/rust/kernel/sync/locked_by.rs b/rust/kernel/sync/locked_by.rs new file mode 100644 index 000000000000..cbfd4e84b770 --- /dev/null +++ b/rust/kernel/sync/locked_by.rs @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! A wrapper for data protected by a lock that does not wrap it. + +use super::{lock::Backend, lock::Lock}; +use core::{cell::UnsafeCell, ptr}; + +/// Allows access to some data to be serialised by a lock that does not wrap it. +/// +/// In most cases, data protected by a lock is wrapped by the appropriate lock type, e.g., +/// [`super::Mutex`] or [`super::SpinLock`]. [`LockedBy`] is meant for cases when this is not +/// possible. For example, if a container has a lock and some data in the contained elements needs +/// to be protected by the same lock. +/// +/// [`LockedBy`] wraps the data in lieu of another locking primitive, and only allows access to it +/// when the caller shows evidence that the 'external' lock is locked. +/// +/// # Examples +/// +/// The following is an example for illustrative purposes: `InnerDirectory::bytes_used` is an +/// aggregate of all `InnerFile::bytes_used` and must be kept consistent; so we wrap `InnerFile` in +/// a `LockedBy` so that it shares a lock with `InnerDirectory`. This allows us to enforce at +/// compile-time that access to `InnerFile` is only granted when an `InnerDirectory` is also +/// locked; we enforce at run time that the right `InnerDirectory` is locked. +/// +/// ``` +/// use kernel::sync::{LockedBy, Mutex}; +/// +/// struct InnerFile { +/// bytes_used: u64, +/// } +/// +/// struct File { +/// _ino: u32, +/// inner: LockedBy, +/// } +/// +/// struct InnerDirectory { +/// /// The sum of the bytes used by all files. +/// bytes_used: u64, +/// _files: Vec, +/// } +/// +/// struct Directory { +/// _ino: u32, +/// inner: Mutex, +/// } +/// +/// /// Prints `bytes_used` from both the directory and file. +/// fn print_bytes_used(dir: &Directory, file: &File) { +/// let guard = dir.inner.lock(); +/// let inner_file = file.inner.access(&guard); +/// pr_info!("{} {}", guard.bytes_used, inner_file.bytes_used); +/// } +/// +/// /// Increments `bytes_used` for both the directory and file. +/// fn inc_bytes_used(dir: &Directory, file: &File) { +/// let mut guard = dir.inner.lock(); +/// guard.bytes_used += 10; +/// +/// let file_inner = file.inner.access_mut(&mut guard); +/// file_inner.bytes_used += 10; +/// } +/// +/// /// Creates a new file. +/// fn new_file(ino: u32, dir: &Directory) -> File { +/// File { +/// _ino: ino, +/// inner: LockedBy::new(&dir.inner, InnerFile { bytes_used: 0 }), +/// } +/// } +/// ``` +pub struct LockedBy { + owner: *const U, + data: UnsafeCell, +} + +// SAFETY: `LockedBy` can be transferred across thread boundaries iff the data it protects can. +unsafe impl Send for LockedBy {} + +// SAFETY: `LockedBy` serialises the interior mutability it provides, so it is `Sync` as long as the +// data it protects is `Send`. +unsafe impl Sync for LockedBy {} + +impl LockedBy { + /// Constructs a new instance of [`LockedBy`]. + /// + /// It stores a raw pointer to the owner that is never dereferenced. It is only used to ensure + /// that the right owner is being used to access the protected data. If the owner is freed, the + /// data becomes inaccessible; if another instance of the owner is allocated *on the same + /// memory location*, the data becomes accessible again: none of this affects memory safety + /// because in any case at most one thread (or CPU) can access the protected data at a time. + pub fn new(owner: &Lock, data: T) -> Self { + Self { + owner: owner.data.get(), + data: UnsafeCell::new(data), + } + } +} + +impl LockedBy { + /// Returns a reference to the protected data when the caller provides evidence (via a + /// reference) that the owner is locked. + pub fn access<'a>(&'a self, owner: &'a U) -> &'a T { + if !ptr::eq(owner, self.owner) { + panic!("mismatched owners"); + } + + // SAFETY: `owner` is evidence that the owner is locked. + unsafe { &*self.data.get() } + } + + /// Returns a mutable reference to the protected data when the caller provides evidence (via a + /// mutable owner) that the owner is locked mutably. + /// + /// Showing a mutable reference to the owner is sufficient because we know no other references + /// can exist to it. + pub fn access_mut<'a>(&'a self, owner: &'a mut U) -> &'a mut T { + if !ptr::eq(owner, self.owner) { + panic!("mismatched owners"); + } + + // SAFETY: `owner` is evidence that there is only one reference to the owner. + unsafe { &mut *self.data.get() } + } +}