Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp68164rwl; Thu, 6 Apr 2023 14:58:52 -0700 (PDT) X-Google-Smtp-Source: AKy350Y2eif4FRKFqYyYY7CVWw9nbJk21GA9bFFKmtqROHzW7Huxvev6HW9dhGARxYEhT31JtIt5 X-Received: by 2002:a17:902:d141:b0:1a3:d5af:9b6f with SMTP id t1-20020a170902d14100b001a3d5af9b6fmr560854plt.19.1680818331854; Thu, 06 Apr 2023 14:58:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680818331; cv=none; d=google.com; s=arc-20160816; b=jqWq4Ip9yji9DB0g9k/KVa67ruNXvM0Q3HXgi7sJ3+lpeJtA8I9IPrS/ZTGHF4v/Dk EWwN+r6RNB0GoknOV3zCPMyXUqU8iCS1e8hlC21npSpTAwBBVddVPvkiaNsiY1Uhtp0Y llcvcHkyh7EhhZ5f0lq6PwXeD2JcaO1xNhmI5cXdAhfTeroXSxar3AQ7T+M/KjD4rm9B BYQGgYpczTB+AO5IXqQaXA33GY1Phz1yfsFzAhnZzSeAyTElYjWFwTao+FPRoc7Q4BRo enr7fYGSJ5SBX3EeJbxATlxmE9DFYnwvGLwWsSuXGAJ8IFZYXq5siaZQwH/LUph+EQqN z2zQ== 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=IYV0p/6EHUgDk3fB2/Zl63fi2qgxj7b14pfAHYz6f6g=; b=zXAQVIHDqc/X83woceoKya6Pcc+EbW3eOswUlThYpERoQnisO6AmSf8oPJLewl5u5k pkIzuoEUbVlilgEe49ceZs8Y9/uSvamJ3FR2tSO4B2K9TZK/wTQ5JRS2Gs687HaNFp+g 0yizicYT2u0jFBbIPqMzlrHZmmOjz89NgRHQqmBoh7w8jvGiBEZcB4amBYZ1jqaelhgY Dh06i1qSukYRk2P+8NVYkcP57LyphRGKXDRd0PJB80bO9p4AspIUG3esH4/oxmSrVt7u yuAuOyEMfa4FGAl+FEwElNdQUcxO1BUqEP7vBx7lflfiJDUPNFiJFbmVoO7TABfgA+jU SWlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=QAQepvs3; 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=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a6-20020a170902ecc600b001a04212a4b1si2615328plh.439.2023.04.06.14.58.40; Thu, 06 Apr 2023 14:58:51 -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=@collabora.com header.s=mail header.b=QAQepvs3; 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=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238455AbjDFV5m (ORCPT + 99 others); Thu, 6 Apr 2023 17:57:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238783AbjDFV5N (ORCPT ); Thu, 6 Apr 2023 17:57:13 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62F12B453; Thu, 6 Apr 2023 14:56:37 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2804:14d:72b4:8284:32a8:8167:f815:2895]) (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: dwlsalmeida) by madras.collabora.co.uk (Postfix) with ESMTPSA id B759766031D9; Thu, 6 Apr 2023 22:56:33 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1680818196; bh=Z1mWh6lO7+hdoozjzHbSbRzBrAE3llpM//4UJUMybns=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QAQepvs3Zbpe0+7Y9i4iiyVuHrSwrwPxURN0SnPqlGdXKKV2xwAcxDJo/g7rthf6F tKTIaCLdCWpceHIn/aD/e2JtpmxEJNGtLOdAsZiA7JxNK0cBxGsFj5+LVQq6+t7cLP cO2vM1NDzL5Cf93rZ/tHahn8YVzTJgGYBf1QcjiaqVgFi+5lqYNu7VHmeLfEZIZ5UD OJEvHEtuYBWTyx/nslLupNS5l+EEkWXRvBy/id14oXNfTExAsgV2Fqf+KFxzPD+1Ow wpTVW+O5X4Kzaykh/HAxMQt+Cp8AlxWHTJb8b0KY3Qzc3RB1D+2aCHESAzurWihORA Olef4SVXq8PYQ== From: Daniel Almeida To: wedsonaf@gmail.com, ojeda@kernel.org, mchehab@kernel.org, hverkuil@xs4all.nl Cc: Daniel Almeida , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, kernel@collabora.com Subject: [PATCH 3/6] rust: sync: introduce FfiMutex Date: Thu, 6 Apr 2023 18:56:12 -0300 Message-Id: <20230406215615.122099-4-daniel.almeida@collabora.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230406215615.122099-1-daniel.almeida@collabora.com> References: <20230406215615.122099-1-daniel.almeida@collabora.com> 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,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 Introduce an owned mutex that can be passed as bindings::mutex when initializing C data structures. The kernel will be locking it without user intervention so there is no data to protect and no guard type. Signed-off-by: Daniel Almeida --- rust/kernel/sync.rs | 1 + rust/kernel/sync/ffi_mutex.rs | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 rust/kernel/sync/ffi_mutex.rs diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index b07db83972ac..1415062be34d 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -26,6 +26,7 @@ use core::{cell::UnsafeCell, mem::MaybeUninit, pin::Pin}; mod arc; mod condvar; +pub mod ffi_mutex; mod guard; mod locked_by; mod mutex; diff --git a/rust/kernel/sync/ffi_mutex.rs b/rust/kernel/sync/ffi_mutex.rs new file mode 100644 index 000000000000..e615ec4059f6 --- /dev/null +++ b/rust/kernel/sync/ffi_mutex.rs @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! An owned mutex that can be passed as bindings::mutex when initializing C +//! data structures. The kernel will be locking it without user intervention so +//! there is no data to protect and no guard type. + +#![allow(dead_code)] + +use crate::prelude::*; +use crate::sync::{LockClassKey, LockIniter}; +use core::marker::PhantomPinned; +use core::pin::Pin; + +use crate::Opaque; + +/// An owned mutex that can be passed as bindings::mutex when initializing C +/// data structures. The kernel will be locking it without user intervention so +/// there is no data to protect and no guard type. +pub struct FfiMutex { + mutex: Opaque, + _pin: PhantomPinned, +} + +impl FfiMutex { + /// Constructs a new Mutex for FFI purposes. + /// + /// # Safety + /// + /// The caller must call [`FfiMutex::init_lock`] before using the raw Mutex. + pub const unsafe fn new() -> Self { + Self { + mutex: Opaque::uninit(), + _pin: PhantomPinned, + } + } + + /// Returns the inner bindings::mutex + /// + /// # Safety + /// + /// The caller must call [`FfiMutex::init_lock`] before using the raw Mutex. + pub(crate) unsafe fn raw(self: &mut Pin<&mut Self>) -> *mut bindings::mutex { + let this = self.as_mut(); + // SAFETY: mutex is pinned when Lock is. The argument to the function is not moved. + let this = unsafe { this.map_unchecked_mut(|x| &mut x.mutex) }; + // This does not move from the field. + this.get() + } +} + +impl LockIniter for FfiMutex { + fn init_lock(self: Pin<&mut Self>, name: &'static CStr, key: &'static LockClassKey) { + unsafe { bindings::__mutex_init(self.mutex.get(), name.as_char_ptr(), key.get()) }; + } +} + +// SAFETY: the underlying bindings::mutex can be used from any thread. +unsafe impl Send for FfiMutex {} +// SAFETY: two threads can try locking the underlying bindings::mutex at the +// same thread without issues. +unsafe impl Sync for FfiMutex {} + +/// Safely initialises a [`FfiMutex`] with the given name, generating a new lock +/// class. +#[macro_export] +macro_rules! ffi_mutex_init { + ($mutex:expr, $name:literal) => { + $crate::init_with_lockdep!($mutex, $name); + }; +} -- 2.40.0