Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp2211404rwp; Fri, 14 Jul 2023 02:36:00 -0700 (PDT) X-Google-Smtp-Source: APBJJlHW0+DOAa2Z17EakteQ1FLuux4cDpUjHemj6KsW6vJjxJUiEmOF8JQZbEG1oVU+RvCUJUMS X-Received: by 2002:a05:6808:1205:b0:3a2:1b3b:494b with SMTP id a5-20020a056808120500b003a21b3b494bmr4837202oil.45.1689327360181; Fri, 14 Jul 2023 02:36:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689327360; cv=none; d=google.com; s=arc-20160816; b=We++DnS8Hl0HSB9Dpwfd9QCTstToLJf+6hflFDXsr2ZewW5DbVKTu068ZJXdjQnjKE LQFWEWENX5wVbjEvxRjFvjyaOdBEMwcp+rvVHOOFlrKiBYd4OPK/DrSI0TSiE7Sm0Ve7 lfsUmGWMHiSHl9mQMTtyJ8nMkBbw+Wn+EvUBIcJysak9Pmk0HKpx/6+13ZbkuIZQQ1rj uix0BcvB9OBOoLf/+wDf9pSA1Y4mUK2bgloMRRUJ+PmVLSzHSV68GlKDOGXmnKsKvnI6 CPBmmVTfhU68JhCOZsjyPfaQsAJwQBwn3XAAuKms0lNYg234OeVjMYDyXW8zKsekugO+ WiqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=ZSJekJPPQlVwsSTGxYvFYZO9Ix3FwMjQGFqZsesziYg=; fh=P69duW5QZmRj9b1Pp5XKOSqIgFMUDbjoyFZWqOwQYzw=; b=HyHAkWKa5r2+/azWPNkNk98l99JWnfinI3mIohNISn1ecHhqpsNF6ibbkmIfbDRoSD OXM4J7iGSkNs7YXC3kYp9s6mBd1wJNi/GQgIo7DimbUVfVXkxodC9xG2MURlZ/NGsDGn F+gxQKNZcPbHAkZNUhi3xL5JrD141xvS7rFH/0KpMYus84XpR6xFvLpa8r+Xu5QsJ0Jn WG5QSZGSQKCL+8fiHK+I89jbkvd8bWHldJQgbWHaA0I8N9wGrm07P8k1K4afXtrko0pM MGDmJq0wAOvOxI3UuxOHaW6DokdNU9QaBaqpHmnQam6ZmFK/z2DIYKqww/UDELzdepdq d0+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=fGwsQoHq; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h191-20020a6383c8000000b0055acc7f805bsi1962386pge.334.2023.07.14.02.35.48; Fri, 14 Jul 2023 02:36: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=@asahilina.net header.s=default header.b=fGwsQoHq; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235857AbjGNJbp (ORCPT + 99 others); Fri, 14 Jul 2023 05:31:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235307AbjGNJbl (ORCPT ); Fri, 14 Jul 2023 05:31:41 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EA7435A3; Fri, 14 Jul 2023 02:31:14 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id F34395BC96; Fri, 14 Jul 2023 09:15:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1689326107; bh=iunu/MEktIfyC9aBXASL3Y+DNDi/HoqbZYswDE31qjY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=fGwsQoHqMFsrJDlFFDPR0k3MaE5WXRU8RB0KWzOUSDcHpsn22IQyGC3HN2lCwncyK wAuqBSwsU1wpop8HxHZZxntrScSS3ywoUW/fICNk8UmtKb6muutZNMoWZ+UcszLYHQ M+MSrjQttyZZqxSDaex5VDxabG/Ko11qdgQZOCoFPbKhO+Vjs9pYmCrlIlGj7ggUsr BvO2xfMlOB9+4V01CgzNjQgUzvvmG7Sld8Qn/tdtTxtZuPu/XThvbbVBE0MUoxrHM/ bX5ZWLPWUJJi1j1mSbgEirahVhQ5ap9KDWfGVt536mxdO/c9hzyPFZPJ2SUBaK+FOX g+Pc+qui1Bz3w== From: Asahi Lina Date: Fri, 14 Jul 2023 18:14:02 +0900 Subject: [PATCH RFC 10/11] rust: sync: Add LockdepMap abstraction MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230714-classless_lockdep-v1-10-229b9671ce31@asahilina.net> References: <20230714-classless_lockdep-v1-0-229b9671ce31@asahilina.net> In-Reply-To: <20230714-classless_lockdep-v1-0-229b9671ce31@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Masahiro Yamada , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , Tom Rix , Daniel Vetter Cc: Hector Martin , Sven Peter , Alyssa Rosenzweig , asahi@lists.linux.dev, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org, llvm@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1689326040; l=3708; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=iunu/MEktIfyC9aBXASL3Y+DNDi/HoqbZYswDE31qjY=; b=04lyCEBPyQa9PMDV9CRJ+d48EyVHc5I664n8RdVhvo5ioHbmWbeIlQUoXCgobM9ilIo2P40ht W2v4aU2ydo8CA+EolWeYDtyrdSPo3/F+5Rv8SBH/RL8TWwoGEWLzZtg X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 Add a simple abstraction for creating new lockdep maps. This allows Rust code to explicitly integrate types with lockdep. There's some voodoo compiler intrinsic magic to get the caller return address on the C side. I have no idea how to plumb that through in Rust if that's even possible, so let's just wrap it in a C helper for now. That gives us the callsite from the Rust abstraction instead of its user, but that's probably okay for now. Signed-off-by: Asahi Lina --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 16 ++++++++++++++++ rust/kernel/sync/lockdep.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 52f32e423b04..5c28de44e528 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 1ed71315d1eb..392f5359677a 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -143,6 +145,20 @@ u64 rust_helper_siphash(const void *data, size_t len, } EXPORT_SYMBOL_GPL(rust_helper_siphash); +void rust_helper_lock_acquire_ret(struct lockdep_map *lock, unsigned int subclass, + int trylock, int read, int check, + struct lockdep_map *nest_lock) +{ + lock_acquire(lock, subclass, trylock, read, check, nest_lock, _RET_IP_); +} +EXPORT_SYMBOL_GPL(rust_helper_lock_acquire_ret); + +void rust_helper_lock_release_ret(struct lockdep_map *lock) +{ + lock_release(lock, _RET_IP_); +} +EXPORT_SYMBOL_GPL(rust_helper_lock_release_ret); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust diff --git a/rust/kernel/sync/lockdep.rs b/rust/kernel/sync/lockdep.rs index fbf9f6ed403d..ca32aa833e10 100644 --- a/rust/kernel/sync/lockdep.rs +++ b/rust/kernel/sync/lockdep.rs @@ -187,3 +187,43 @@ pub(crate) fn caller_lock_class() -> (LockClassKey, &'static CStr) { } } } + +pub(crate) struct LockdepMap(Opaque); +pub(crate) struct LockdepGuard<'a>(&'a LockdepMap); + +#[allow(dead_code)] +impl LockdepMap { + #[track_caller] + pub(crate) fn new() -> Self { + let map = Opaque::uninit(); + let (key, name) = caller_lock_class(); + + unsafe { + bindings::lockdep_init_map_type( + map.get(), + name.as_char_ptr(), + key.as_ptr(), + 0, + bindings::lockdep_wait_type_LD_WAIT_INV as _, + bindings::lockdep_wait_type_LD_WAIT_INV as _, + bindings::lockdep_lock_type_LD_LOCK_NORMAL as _, + ) + }; + + LockdepMap(map) + } + + #[inline(always)] + pub(crate) fn lock(&self) -> LockdepGuard<'_> { + unsafe { bindings::lock_acquire_ret(self.0.get(), 0, 0, 1, 1, core::ptr::null_mut()) }; + + LockdepGuard(self) + } +} + +impl<'a> Drop for LockdepGuard<'a> { + #[inline(always)] + fn drop(&mut self) { + unsafe { bindings::lock_release_ret(self.0 .0.get()) }; + } +} -- 2.40.1