Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1599591rwl; Wed, 29 Mar 2023 21:42:01 -0700 (PDT) X-Google-Smtp-Source: AKy350YRkwYq05JIyvzQzEXvpENW4Mbmec3/ybE5dxC3+8DhZeZP3VcykB283I3LwrvB/qFQ8NCg X-Received: by 2002:a05:6a20:9384:b0:e3:e236:bbd5 with SMTP id x4-20020a056a20938400b000e3e236bbd5mr787872pzh.2.1680151320920; Wed, 29 Mar 2023 21:42:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680151320; cv=none; d=google.com; s=arc-20160816; b=zmgCVrh8FzEG6p+2Pxp6AvS3gjSx4Yrp8tFZpxzFvGAjvDVtBqz+6c1tChD8CnB5gi 9Px1uk0E2yvRFOw8a6GldoZJD6llOG+EXfqh88QIInZTS78QsHhbNW1zh/rCKJpFgQX0 6O7c04wppTyYPMFkHnHqM6FPaLkZTkUXKk5wuSwhxbGunslM9hYyvBtj6tJZ+XN1MgNg K1LaQ7F1iZgzMxgDrV/hTfsZyGoNseAtPqGtRc3G4Kh4AXDGhq4PBLpdKMeY1VL4Yqgy S7NFHAo5pV1h9/kzyXo0q26+oARa02y+SonjEGAZFdrnLR4XXBE0tY5RDPkO5NUDasUi UaLQ== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bc22-20020a656d96000000b0050bedf49bb3si32876898pgb.821.2023.03.29.21.41.48; Wed, 29 Mar 2023 21:42:00 -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 S229760AbjC3ElC (ORCPT + 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 ); 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 To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long 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: X-Mailing-List: linux-kernel@vger.kernel.org From: Wedson Almeida Filho 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 Cc: Ingo Molnar Cc: Will Deacon Cc: Waiman Long Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: Wedson Almeida Filho --- 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); + +// 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) + }; +} -- 2.34.1