From patchwork Thu Mar 30 04:39:47 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: 76880 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp872407vqo; Wed, 29 Mar 2023 21:48:25 -0700 (PDT) X-Google-Smtp-Source: AKy350ZdjA9ksve1sq4vV1LG1UqFd13dqYW7GnJ3DJwzjrjfrICEk/BTc05zg5mcSBpbmd0lkhSL X-Received: by 2002:a17:90a:6805:b0:233:e305:f617 with SMTP id p5-20020a17090a680500b00233e305f617mr23207531pjj.32.1680151704731; Wed, 29 Mar 2023 21:48:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680151704; cv=none; d=google.com; s=arc-20160816; b=R4FPUzf7bqP/AUWHElDMXWe2XkGk3Qy5pG8HlGN+32nDhyNFJCyPbJ0pFs+Rmhd/AT x03C2pLadt5zSJeBX2/ewJdXoyOsV5xakVsqt+UxC9bn2hTBp1FXfkF3Q3V2XY/EsG7j eDWSdWzdk8l++pE0vpJhie1AzpustW42LIA12eaFCuWfZwlafvNkWDWhFq1t1Z6mA0g5 sKmJONaGH++ULL2TLborMohYszSsBiRU9PiwCnUOP2I72YifW4U4h3SUNJF51NkJ2gBu 6RzjKGMzBxzGzO1uwnP/STXDBcmK40Eajjer4A4U2ouE3mmS4KZ98TYp9Ien2JmeQ1Q/ 7SHw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u/B9dFAeaUqrqqXcKIk+Y/eQ9yVG9iQNTR7il5e9jLs=; b=lR/0qZAnd7MeWhA9EYW3qeFFEJpl7zFQGcIiTi8xBdRgNrz9QUPabbem8C21/LIgzY oBc8tItuNWydMrfYs61W1tavzq4C5y29AXnQHZT2PHkCA7rfN/nz6M6U2tkFjsRCQQS9 UydKafjNvM6998fZBq2+pR5uNCW7LGbr3B7vfdSbLnAC6OtlNJMkiAbu+DjJgSN1TLZP K4QhQN3tOo+FV2Toq1veFf2ttecttU+cJw9i176Kv9jvHrWBZMEcY/88VGlI/rwrA1vl FhXd7vWxan+yqhrwgRVdFsGczVrtEuU4318ln2iI7iGpyfOI6aesr/6ATq4zUazUQwxZ 0Pew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=FBldUjeW; 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 ne17-20020a17090b375100b00233c73769dfsi3078142pjb.120.2023.03.29.21.48.09; Wed, 29 Mar 2023 21:48:24 -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=FBldUjeW; 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 S229948AbjC3Elg (ORCPT + 99 others); Thu, 30 Mar 2023 00:41:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229929AbjC3ElW (ORCPT ); Thu, 30 Mar 2023 00:41:22 -0400 Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AD2255B5; Wed, 29 Mar 2023 21:41:08 -0700 (PDT) Received: by mail-ot1-x331.google.com with SMTP id d22-20020a9d5e16000000b0069b5252ced7so9390956oti.13; Wed, 29 Mar 2023 21:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680151268; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=u/B9dFAeaUqrqqXcKIk+Y/eQ9yVG9iQNTR7il5e9jLs=; b=FBldUjeWmTnMcGDYtqPGJOtkr5qqAbx5XiPvKUIklDArae9Xj9CeCHqo5AgHV6DmJ8 7VYNkFwXjHt4PiCnF+qGoy4piOQ79h/06J07fzF9g5FmSl5EQ8XijZ7ko5HcBgGDj9VN /4QFZCOftuHqdU3uFgL27j225r4bIsAFt1dSBzTyi08LjsDzas/byE1wbN1j+qk/reYm notFF2U2pairKLHR4xcS/GOOvf5ZRL5zq8oTEYoeXLobavX7T7GvXuk79j2KjtecFyVK OhnnTSq5hYySGzqeWDgy78MFf4lbW63A37vRGZWAKb+MzYToslbH527fdxj4M/YLnlNI QUJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151268; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u/B9dFAeaUqrqqXcKIk+Y/eQ9yVG9iQNTR7il5e9jLs=; b=ITvSaEUzvQD8Kt+lA8J1CruVuHlwy3d8+9NLWQGviMt91kGCYuyJJZuDiYaq8jqc8O lEaGEKKv8T6aMXqmOg1AyOEWp5VYYfyuotEyJrGdnAwFs6vLWgXWxcc1aogEZP0iR/j6 SKE8oiR8PbXMqBa/2RF5CnMI6jpvHXxEQi2VR9Ado/Gtj+C6y4yFtOMirOaclD6d5sSZ dNC0LyW7pIRzWVWQWXA9Bo/eLE/icN2oqbJRWSXwM6V4ni7lH0HluNfnrx5vC5tOIuh/ dB45BDyXDoCahXFKwYvHryZf8fqkoDrxHRew0dt7Dg0dZHZpGSKFjr8a7E6D8696wgk5 Br9Q== X-Gm-Message-State: AO0yUKWxTYVW7LUCqfo8xKwas6Q0qu2UM+i0vP4gRkfMZkOSC3vZxpG+ IPiu6KB675tq7iP8dPymiYTWEd44G+NswA== X-Received: by 2002:a9d:6854:0:b0:693:d998:f50c with SMTP id c20-20020a9d6854000000b00693d998f50cmr10533454oto.5.1680151267883; Wed, 29 Mar 2023 21:41:07 -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.41.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:41:07 -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 Subject: [PATCH 06/13] rust: lock: add support for `Lock::lock_irqsave` Date: Thu, 30 Mar 2023 01:39:47 -0300 Message-Id: <20230330043954.562237-6-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330043954.562237-1-wedsonaf@gmail.com> References: <20230330043954.562237-1-wedsonaf@gmail.com> 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?1761766754173428026?= X-GMAIL-MSGID: =?utf-8?q?1761766754173428026?= From: Wedson Almeida Filho This allows locks like spinlocks and raw spinlocks to expose a `lock_irqsave` variant in Rust that corresponds to the C version. Signed-off-by: Wedson Almeida Filho --- rust/kernel/sync/lock.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index bca9af2a9a5a..491446c3a074 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -57,6 +57,29 @@ pub unsafe trait Backend { unsafe fn unlock(ptr: *mut Self::State, guard_state: &Self::GuardState); } +/// The "backend" of a lock that supports the irq-save variant. +/// +/// # Safety +/// +/// The same requirements wrt mutual exclusion in [`Backend`] apply for acquiring the lock via +/// [`IrqSaveBackend::lock_irqsave`]. +/// +/// Additionally, when [`IrqSaveBackend::lock_irqsave`] is used to acquire the lock, implementers +/// must disable interrupts on lock, and restore interrupt state on unlock. Implementers may use +/// [`Backend::GuardState`] to store state needed to keep track of the interrupt state. +pub unsafe trait IrqSaveBackend: Backend { + /// Acquires the lock, making the caller its owner. + /// + /// Before acquiring the lock, it disables interrupts, and returns the previous interrupt state + /// as its guard state so that the guard can restore it when it is dropped. + /// + /// # Safety + /// + /// Callers must ensure that [`Backend::init`] has been previously called. + #[must_use] + unsafe fn lock_irqsave(ptr: *mut Self::State) -> Self::GuardState; +} + /// A mutual exclusion primitive. /// /// Exposes one of the kernel locking primitives. Which one is exposed depends on the lock banckend @@ -109,6 +132,21 @@ impl Lock { } } +impl Lock { + /// Acquires the lock and gives the caller access to the data protected by it. + /// + /// Before acquiring the lock, it disables interrupts. When the guard is dropped, the interrupt + /// state (either enabled or disabled) is restored to its state before + /// [`lock_irqsave`](Self::lock_irqsave) was called. + pub fn lock_irqsave(&self) -> Guard<'_, T, B> { + // SAFETY: The constructor of the type calls `init`, so the existence of the object proves + // that `init` was called. + let state = unsafe { B::lock_irqsave(self.state.get()) }; + // SAFETY: The lock was just acquired. + unsafe { Guard::new(self, state) } + } +} + /// A lock guard. /// /// Allows mutual exclusion primitives that implement the `Backend` trait to automatically unlock