From patchwork Fri Jul 14 09:14:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 120392 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp2389959vqm; Fri, 14 Jul 2023 03:00:34 -0700 (PDT) X-Google-Smtp-Source: APBJJlGDXv+g+RLBX7OpooQ94cBLkD7dnUOWzK2R1glurtaNwdIT2Lahp5RINl3GzT+6vI3sarKU X-Received: by 2002:aa7:ca52:0:b0:51d:d48b:9978 with SMTP id j18-20020aa7ca52000000b0051dd48b9978mr3803010edt.8.1689328834388; Fri, 14 Jul 2023 03:00:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689328834; cv=none; d=google.com; s=arc-20160816; b=ykoIF0b2jnAqtvQBlnepeY1Mum/mNcOhE3idElyIu1x3B68UnY7G9hOVSv9CLA2wBR OOFR5gBXc15SSsvieyw2c3ug7e2M7jUT3E6atc74SI2RhRvPpMi57cEsjEdBuPBozZuX aejv+DbyyK4pZLYEZV0PvnsnQFzP+4CPSvezYnPirOdSACA5li00Pl0dKYX1sb7AUbwE wy18jcyoCMu6Q/MIdj1EiVWvQsxuhTs+x0Up+UOsQlW7JjMWOt3nWjjXPgljc+ryEnrI vgyEjwJOdWaErFq2y/8gItExgdO8xJY9sgxFlR4iK/fJwREklvZzU4VwWkHmfAAcUOXQ Qg3w== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n17-20020aa7c451000000b0051da4b65e72si8407575edr.360.2023.07.14.03.00.07; Fri, 14 Jul 2023 03:00:33 -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 S235676AbjGNJbq (ORCPT + 99 others); Fri, 14 Jul 2023 05:31:46 -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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771389670581300698 X-GMAIL-MSGID: 1771389670581300698 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()) }; + } +}