From patchwork Sat Apr 8 07:53:28 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: 81132 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp740848vqo; Sat, 8 Apr 2023 01:07:12 -0700 (PDT) X-Google-Smtp-Source: AKy350aX6ZuzvoucXmo+GjYwLJiFp09XqwBbyQH0y+XcZ0oha3KR92w251xLsH8Ggw6fRtR8+MZa X-Received: by 2002:a17:906:b197:b0:92b:846d:8928 with SMTP id w23-20020a170906b19700b0092b846d8928mr897282ejy.65.1680941232139; Sat, 08 Apr 2023 01:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680941232; cv=none; d=google.com; s=arc-20160816; b=km9H4T2kmCmD3iEZxD+Xkwg3uBlwQfOzedGCtYZEpzdA5cR0C4bVIMbNs1CGzZYr12 JEI32WP5nEYd71c02lapcb5bdkseVwS9rByHgqmgzplMuu7mIzyneN5/rk+r9KqJYQcw XEGGTMgOLYxMI3hT/gkp7Psx7VrYmhgrYzHIshxXBuaGpnwqKQMzzSqvypfQfzJzBoEf tvpVueJ5wVu5MBErzqiKJqYnYoAQ+OUfQz6RfGVnhVL3PRjGYafH9ghBLtBcOV9iPFtH IZHVb4f/K3YwZJi4GSurqSMPgbzTkSo3O/z2Tii1t/0xU/aFjSJCycQGOBjtAZczQcik ApNQ== 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=8DXY5enKnxnJyRV7r86vq2fiEM2iNKd+iCSjske2KHI=; b=jPF3QRMEQKjnGPbZO2qwkiUP2XjtlGvZAd5i/OLHlaosddrnfU4Q51zB9oqIzfNdnW NPWX4t7cW+CuGZ/3c3HNiL2SR+PMX2HfCzZPxUZtITDqI+FR8ys4D1/GTGGQ66NaoNyn OpluP5+Z+lHLfHQwgpHZa31lU5oPuJntHyRb8HM17K4mN6QT6rif6Y7S4O7rwLaHwAZS Y4Bn7dhii3iYFnGcCtC4GXZGo0gD3oCHwAn9h24vnxryXGxQ0KXTKDWtyU0evkUbbxp4 CnKEkfwenuz03WPXlRL6aZvPhDNa67MUzeuTw+lFAO9VAwa4AaI5M0jsqP9lM47OmdNn K/1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QpArfm+D; 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 qw32-20020a1709066a2000b00938de018211si5255500ejc.831.2023.04.08.01.06.47; Sat, 08 Apr 2023 01:07:12 -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=QpArfm+D; 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 S229802AbjDHHyA (ORCPT + 99 others); Sat, 8 Apr 2023 03:54:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbjDHHx5 (ORCPT ); Sat, 8 Apr 2023 03:53:57 -0400 Received: from mail-oi1-x22d.google.com (mail-oi1-x22d.google.com [IPv6:2607:f8b0:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4712B26A4; Sat, 8 Apr 2023 00:53:55 -0700 (PDT) Received: by mail-oi1-x22d.google.com with SMTP id q27so31655633oiw.0; Sat, 08 Apr 2023 00:53:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680940434; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8DXY5enKnxnJyRV7r86vq2fiEM2iNKd+iCSjske2KHI=; b=QpArfm+D2AtseAfEe0p39Qc9A+hlCKSMdgaYUrWWmhe6qILHfP6feTEBkFhEDZKW1j ZOwdqgemZ4V/YA6c5VbWjVEzyzVuCyx2P5XKeFe232Y+vRNQADzY7RfNu5WsrOJdFRnQ BdyomMOB0g4OaNpeeqPs3Ix4bsVepv+Bi+W3l/PQB5/JhuWrd9CzYK3rq+LJUNFbOVGt MHY4TITVWIsPSD+ZC4YC71w3/zDdN92b2PZoVgci40DC8lr6FMP7D5SpaU1/b5EaaKuh SIWj4PyM04rL/OWEjl/o0RZ9QP+L64oL84oBqMeEexYYzsFulBXmoPggImafbw9mgGxm /GTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680940434; 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=8DXY5enKnxnJyRV7r86vq2fiEM2iNKd+iCSjske2KHI=; b=v9bq2rD27TbhoXbS/rWPs0BwozYDnSOIQ3wjwXK4aQNRmI3e9X2VL05E6+GHp5EQY4 Asw8yZNlCe6lKqlqo8G4dJ3fAxuh8v2mrfRDKMoJhIH4RC/SFJhgvA8meGE80r+5J+C1 Zya3gajxbhcqlDQh0KRW0b/qGwbFSrH8kaJk/y48/ucUuK7Ega6Q9jqZYBdyXDv3eqs9 Jj258YNPQvyh0pQdgCWGosv3e63rqxnoOeWBYt/iUrC05PFfbSwbBMrKzgRQRG9N2x9P IBKjLIRZxZmJq/ER7LE8BSMAlj1CEVq0VYZvwO9uieE/Uwf1dyPIRZxKdKuOcfWbjeOW oXqg== X-Gm-Message-State: AAQBX9cPvk/Gya7jTTt4Y3+zdfuh/ZyoEaYorMdkMQmY+jQbOHKTmPDB iHYHoWbEnD1AYUblj75aQlJQQV3+yu21OQ== X-Received: by 2002:aca:1905:0:b0:389:4a80:336f with SMTP id l5-20020aca1905000000b003894a80336fmr2042313oii.23.1680940434442; Sat, 08 Apr 2023 00:53:54 -0700 (PDT) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id o6-20020a4a9586000000b0051ffe0fe11bsm2435175ooi.6.2023.04.08.00.53.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Apr 2023 00:53:54 -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 , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long Subject: [PATCH v3 01/13] rust: sync: introduce `LockClassKey` Date: Sat, 8 Apr 2023 04:53:28 -0300 Message-Id: <20230408075340.25237-1-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 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?1762594632989641159?= X-GMAIL-MSGID: =?utf-8?q?1762594632989641159?= 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 Reviewed-by: Martin Rodriguez Reboredo --- v1 -> v2: Fixed a typo in comment v2 -> v3: Replaced `core` with `::core` in macro rust/kernel/sync.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 33da23e3076d..541d235ffbeb 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 generates 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) + }; +}