Message ID | 20230330043954.562237-1-wedsonaf@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp873101vqo; Wed, 29 Mar 2023 21:50:21 -0700 (PDT) X-Google-Smtp-Source: AKy350YBKqiXmlRgW2C1Fp3DGMY9lvkqcUy5oW/gl9ewFkAOK3lC/MSw89+kJpUZbFRJ4/6rQgw7 X-Received: by 2002:a17:90b:1104:b0:240:9b09:e9ce with SMTP id gi4-20020a17090b110400b002409b09e9cemr4721178pjb.16.1680151821503; Wed, 29 Mar 2023 21:50:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680151821; cv=none; d=google.com; s=arc-20160816; b=ppkYIzGq3ZNrN60CzjY8dfhMCuQwv55nAKS79EDspkA1fyHd5vwkM4KchaNAQ5UeUO bPHRuPOIihCTvFkuaMHF/EmJmkmwZKPX8tkk0AxuNkIcH6fRfmT3uurVfhTW1Yax2Ckp 77W/xDZeXUdiJWO7yCL6oxFuodJOQQ1puF+2iTKnUYTIwxiaZBDSrmSmhs1UcBXmIaeX q02jDjkFUM9uEGyYDrPedMvqH7EvfMU9nOs9EIUG/gasWRL48VwYPSBaiffJnylCuoON Hq8sDHuhO7cMExiuAFP8Z4Kc8Iki87BQRRs7qi12thlx5ZUHLF3fpdooTG3KyOGEvUUF aIdQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=rLQtYNZFpddodHi/ahJfQCsqx8fDKLCv/AuGB4H5UvE=; b=naFcXM3zltoN+ZvZuLi6oOcOeE9W+zNz/lzZiNq2yOiNfKW/1Ua1VpzD8FjsJpyy5o c8JjgMDYvHCm+94Hh2y51VapWvJkYml7CGUeV0ad7dpf0MrbDNZQo9ph+yXgHFqYtDH1 nJREX+0fpWNYxHx/ONP40cbniZMsg6xNhQmcvbLxS+BsdtFx1kAuf42WLob0ATYAqmeo IHg2rqzWdVQYL08gEzHjGjvRQ8XgmHyizyTRT7tCpJ5RZYtjutfBRN0bPBsbb3bUU2ZO GGdU3KYdlxbOzbbGRckY8eYD2paN+7p34X3zPM/jCVrhJ4Q8ED74TVT8MrDPMBm/bQMq VqNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IyJZWNVj; 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 nn14-20020a17090b38ce00b0023f0955f301si3266843pjb.166.2023.03.29.21.50.07; Wed, 29 Mar 2023 21:50:21 -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=IyJZWNVj; 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 S229678AbjC3ElC (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Thu, 30 Mar 2023 00:41:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjC3ElB (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 30 Mar 2023 00:41:01 -0400 Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96CA9618F; Wed, 29 Mar 2023 21:40:46 -0700 (PDT) Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-17ac5ee3f9cso18533073fac.12; Wed, 29 Mar 2023 21:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680151246; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=rLQtYNZFpddodHi/ahJfQCsqx8fDKLCv/AuGB4H5UvE=; b=IyJZWNVjjZ8/W8dHQs79bS3aAa+GUYXGhoabxs417dD1lH3ar/QQ9Pp9/L5d16GPAL tTH25vZr0ronU3Je4ybnvYKw7teys1hfsl87OSR4a0Wx5NHoCiyUQCfc+2WLWIm9iRT5 TDC6lE5mJermYKgsiBPBoV5Ov+1kBaiJDjY90oeIHB/iADj9voJMmcsgqTc9e6odc2oh 4c+UZMuVtDbekur4g/yLZX5RZ+zp0bjPmiCp0kK0JC2qum+TzDirISqDgMK92qXHUMIM P50YC0LPUoKglhRlUyWiNo7I8S4Xo/s55wISc9Bx0w7IMvc5ZTUrtqgWNJfm+tijzaRg VWSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151246; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=rLQtYNZFpddodHi/ahJfQCsqx8fDKLCv/AuGB4H5UvE=; b=snExaqrpvZxhGi5xwpvhBDzoZRjDqsDsnzWM0kJa7kjvWou87LOPYAmDaUHEhTJRtD ACClUkiFCy54VaZaY0xcpJWK/7TViC0uTs5e8TKu728dxQaQDYYtHpU2C3NPFgw5jQyO D8xjKXQN5iVX9gMSggYsuT1kqfbMY/MFU2xzbOhvAWHHxXMpAWPcP2g94RMtlJpTwpav JgRxMZKMacNEL0hWods6wrXHQYoLYxj3XZrQJ6ekIExoG5c0UunBUMhCDjlyAKgv5ZrB KVVqQwV7VZhDPiDNXEAZkgy0sv/erUHEKgXv2OgI09CJHCT2fuqMeekc4tssQgslDEop QPmg== X-Gm-Message-State: AAQBX9d17BhJG1kmuu1AUenNlO2V+IcOzmlUxN5dEbySOmEqwI/D3Iyx SjKkbAuBF/IZr5ar7tGion439WFg5en6lg== X-Received: by 2002:a05:6870:c150:b0:172:289b:93c5 with SMTP id g16-20020a056870c15000b00172289b93c5mr2508515oad.0.1680151245805; Wed, 29 Mar 2023 21:40:45 -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.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:40:45 -0700 (PDT) From: Wedson Almeida Filho <wedsonaf@gmail.com> To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, linux-kernel@vger.kernel.org, Wedson Almeida Filho <walmeida@microsoft.com>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>, Waiman Long <longman@redhat.com> Subject: [PATCH 01/13] rust: sync: introduce `LockClassKey` Date: Thu, 30 Mar 2023 01:39:42 -0300 Message-Id: <20230330043954.562237-1-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761766876235586081?= X-GMAIL-MSGID: =?utf-8?q?1761766876235586081?= |
Series |
[01/13] rust: sync: introduce `LockClassKey`
|
|
Commit Message
Wedson Almeida Filho
March 30, 2023, 4:39 a.m. UTC
From: Wedson Almeida Filho <walmeida@microsoft.com> It is a wrapper around C's `lock_class_key`, which is used by the synchronisation primitives that are checked with lockdep. This is in preparation for introducing Rust abstractions for these primitives. Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Will Deacon <will@kernel.org> Cc: Waiman Long <longman@redhat.com> Co-developed-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com> --- rust/kernel/sync.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
Comments
On Thu, 30 Mar 2023 01:39:42 -0300 Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > From: Wedson Almeida Filho <walmeida@microsoft.com> > > It is a wrapper around C's `lock_class_key`, which is used by the > synchronisation primitives that are checked with lockdep. This is in > preparation for introducing Rust abstractions for these primitives. > > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Will Deacon <will@kernel.org> > Cc: Waiman Long <longman@redhat.com> > Co-developed-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com> > --- > rust/kernel/sync.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 45 insertions(+) > > diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs > index 33da23e3076d..84a4b560828c 100644 > --- a/rust/kernel/sync.rs > +++ b/rust/kernel/sync.rs > @@ -5,6 +5,51 @@ > //! This module contains the kernel APIs related to synchronisation that have been ported or > //! wrapped for usage by Rust code in the kernel. > > +use crate::types::Opaque; > + > mod arc; > > pub use arc::{Arc, ArcBorrow, UniqueArc}; > + > +/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. > +#[repr(transparent)] > +pub struct LockClassKey(Opaque<bindings::lock_class_key>); > + > +// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and > +// provides its own synchronization. > +unsafe impl Sync for LockClassKey {} > + > +impl LockClassKey { > + /// Creates a new lock class key. > + pub const fn new() -> Self { > + Self(Opaque::uninit()) > + } > + > + #[allow(dead_code)] > + pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { > + self.0.get() > + } > +} > + > +/// Defines a new static lock class and returns a pointer to it. > +#[doc(hidden)] > +#[macro_export] > +macro_rules! static_lock_class { > + () => {{ > + static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new(); > + &CLASS > + }}; > +} > + > +/// Returns the given string, if one is provided, otherwise generateis one based on the source code Typo. > +/// location. > +#[doc(hidden)] > +#[macro_export] > +macro_rules! optional_name { > + () => { > + $crate::c_str!(core::concat!(core::file!(), ":", core::line!())) > + }; > + ($name:literal) => { > + $crate::c_str!($name) > + }; > +}
On 3/30/23 06:39, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho <walmeida@microsoft.com> > > It is a wrapper around C's `lock_class_key`, which is used by the > synchronisation primitives that are checked with lockdep. This is in > preparation for introducing Rust abstractions for these primitives. > > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Will Deacon <will@kernel.org> > Cc: Waiman Long <longman@redhat.com> > Co-developed-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com> > --- > +// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and > +// provides its own synchronization. > +unsafe impl Sync for LockClassKey {} No Send? > + > +impl LockClassKey { > + /// Creates a new lock class key. > + pub const fn new() -> Self { > + Self(Opaque::uninit()) > + } > + > + #[allow(dead_code)] > + pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { > + self.0.get() > + } I would just make this pub and drop the `#[allow(dead_code)]`. I think it is often useful to have methods like this available publicly.
On Fri, 31 Mar 2023 at 04:28, Alice Ryhl <alice@ryhl.io> wrote: > > On 3/30/23 06:39, Wedson Almeida Filho wrote: > > From: Wedson Almeida Filho <walmeida@microsoft.com> > > > > It is a wrapper around C's `lock_class_key`, which is used by the > > synchronisation primitives that are checked with lockdep. This is in > > preparation for introducing Rust abstractions for these primitives. > > > > Cc: Peter Zijlstra <peterz@infradead.org> > > Cc: Ingo Molnar <mingo@redhat.com> > > Cc: Will Deacon <will@kernel.org> > > Cc: Waiman Long <longman@redhat.com> > > Co-developed-by: Boqun Feng <boqun.feng@gmail.com> > > Signed-off-by: Boqun Feng <boqun.feng@gmail.com> > > Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com> > > --- > > +// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and > > +// provides its own synchronization. > > +unsafe impl Sync for LockClassKey {} > > No Send? We haven't needed it. We can add it when needed. > > + > > +impl LockClassKey { > > + /// Creates a new lock class key. > > + pub const fn new() -> Self { > > + Self(Opaque::uninit()) > > + } > > + > > + #[allow(dead_code)] > > + pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { > > + self.0.get() > > + } > > I would just make this pub and drop the `#[allow(dead_code)]`. I think > it is often useful to have methods like this available publicly. The `allow(dead_code)` is removed on the next patch, it's here just to make this patch compile when applied alone. This isn't public because the return type refers to a type from `bindings`, which we intend to eventually hide from drivers, making it public now would like our lives harder in the future.
diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 33da23e3076d..84a4b560828c 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -5,6 +5,51 @@ //! This module contains the kernel APIs related to synchronisation that have been ported or //! wrapped for usage by Rust code in the kernel. +use crate::types::Opaque; + mod arc; pub use arc::{Arc, ArcBorrow, UniqueArc}; + +/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. +#[repr(transparent)] +pub struct LockClassKey(Opaque<bindings::lock_class_key>); + +// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and +// provides its own synchronization. +unsafe impl Sync for LockClassKey {} + +impl LockClassKey { + /// Creates a new lock class key. + pub const fn new() -> Self { + Self(Opaque::uninit()) + } + + #[allow(dead_code)] + pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key { + self.0.get() + } +} + +/// Defines a new static lock class and returns a pointer to it. +#[doc(hidden)] +#[macro_export] +macro_rules! static_lock_class { + () => {{ + static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new(); + &CLASS + }}; +} + +/// Returns the given string, if one is provided, otherwise generateis one based on the source code +/// location. +#[doc(hidden)] +#[macro_export] +macro_rules! optional_name { + () => { + $crate::c_str!(core::concat!(core::file!(), ":", core::line!())) + }; + ($name:literal) => { + $crate::c_str!($name) + }; +}