Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp613927rwl; Wed, 5 Apr 2023 05:34:32 -0700 (PDT) X-Google-Smtp-Source: AKy350bGVI5IngM6Okc/ATbrVPtQTCQ3hrKtmnSdEcPYH6wF2RWDrtuOiczx2uBbuAmvOUvytKRR X-Received: by 2002:a17:902:ec87:b0:19e:72c5:34df with SMTP id x7-20020a170902ec8700b0019e72c534dfmr7541238plg.52.1680698072696; Wed, 05 Apr 2023 05:34:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680698072; cv=none; d=google.com; s=arc-20160816; b=WX3YSNQdwmf+WGArEXMaVr5/nqIZy/j9+TlFUUQ3ePlIGHS4RVQcU4eKgKXo41HfPa vNAn1j9x8Hv2wUtTujQ2Tg3BJClVaChQynvMWN1OHE9NnFucCS3RU9zsN2WImVatt24I CkuR9+bAVHMywkwL/G/ajAkDZC4tHGfEsfD33e+OqCZ1dkGF9uLiF0AEZsJZgS4L2Y47 5rRw6xa/HxeGvrT84vcI+82lh5vNFIJbyCOH0WrBj290sDsdsGMkYoueJ1KIt6MHnaQ/ qqtuu7IuYniOeFsI/CWeSlFfaMsdbFxtWA1s2sJLHMEJpuEUxh9ISHApgbT2MyMLRkvC iAuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=mLn0ogcl0BF5IqyMl3PAUGbH0jpqq1AyfGbugR/InEw=; b=tIjyyCleFTkJoAhC0XNUIrZz7hEJG7uZ6yXb0oREqYWuoBiT0Luan/VtB5l5J0ME/2 /xUi9Oi9MmPq80oGdsm7sBJ+I/X3SwF4fUWJfteGznEl9sSUQobA7fZdeEjW17S9d3eZ vGauxlxjOZPbMSvj4qDUPwVdCDxffnflYA/YDUAf14RaRkQ31V5tkEtgqoV9YmV2Vx1W QzFmo9qyaR1kB2R5I+0TT1lvieFBYon/bxxDGE24TFiuU0xxB/1z07BgBEBm5mICKsVq 4+wSdX5Sd/6rns3lWHyTIqHDpT8WTJBXFHaxLqD+INaCLYQVU1oF3lPr9lmXoRbo3xr1 +0Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=Kj+iccol; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g12-20020a1709026b4c00b0019b521f4b07si11907505plt.52.2023.04.05.05.34.20; Wed, 05 Apr 2023 05:34:32 -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=@ffwll.ch header.s=google header.b=Kj+iccol; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238025AbjDEMdp (ORCPT + 99 others); Wed, 5 Apr 2023 08:33:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237879AbjDEMdn (ORCPT ); Wed, 5 Apr 2023 08:33:43 -0400 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED0931BC5 for ; Wed, 5 Apr 2023 05:33:39 -0700 (PDT) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-5002f12ccfeso43023a12.0 for ; Wed, 05 Apr 2023 05:33:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1680698018; x=1683290018; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date:from:to:cc :subject:date:message-id:reply-to; bh=mLn0ogcl0BF5IqyMl3PAUGbH0jpqq1AyfGbugR/InEw=; b=Kj+iccolbVQfKM6jZP2rJr6vvKgZMe4H0SxVgfuZYTm3PnXPF0cVqAqxe80FPLqr+F L2MfX8A/pL0ohBy6Oshpt/8+orS19AKvmmkHGmmxImCIAvDHKY0BgnTDUZGzcVxDepO9 EUSOhBxht69OnVyx6WqSCLMw6BX1kJjJ9TuYE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680698018; x=1683290018; h=in-reply-to:content-disposition:mime-version:references :mail-followup-to:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mLn0ogcl0BF5IqyMl3PAUGbH0jpqq1AyfGbugR/InEw=; b=b8FiH+PRLMADj8NJoCjaWiqxznv+xjnjtOA/UP7l+eTTF19FWmA+uouGSJ6yEAtN7n gwgZBSL6TBQM5hp1b+l2bBigL3Riej0R+hwH9LQ53ZzE0Pd2jjbdbGOjPDcgtimW2Bu7 qn0XA78W2OQJ1IOc/j7tyWPMao1My4Djnhgd+LZkhA8FwbHrWZpq+w2O4xRCAgIHDdKM hf6BhMJfQSb/ss5FLziFRcVbu7Uz8DeFOCUrRaykY+qXAQqKS02gjwbBB+xDRNzxfJ3f YEUSKYD7jKdSJ+5CwnhYW8UV5aJtcZ6pNBqIxxwVwmTn7zfBso2VB3RxQOYjyhE+trao qEgQ== X-Gm-Message-State: AAQBX9dUrOrgir0zGr+8CrvEKjw3h2NH/83WNfavF37lDPwhPQSlgQXI RA1vLIoEFbkft+HmNkopRm/AJQ== X-Received: by 2002:a05:6402:34c8:b0:502:367:d5b8 with SMTP id w8-20020a05640234c800b005020367d5b8mr1906388edc.4.1680698018440; Wed, 05 Apr 2023 05:33:38 -0700 (PDT) Received: from phenom.ffwll.local (212-51-149-33.fiber7.init7.net. [212.51.149.33]) by smtp.gmail.com with ESMTPSA id l2-20020a170906938200b00948a57aac08sm3761007ejx.204.2023.04.05.05.33.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 05:33:37 -0700 (PDT) Date: Wed, 5 Apr 2023 14:33:35 +0200 From: Daniel Vetter To: Asahi Lina Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Luben Tuikov , Jarkko Sakkinen , Dave Hansen , Alyssa Rosenzweig , Karol Herbst , Ella Stanforth , Faith Ekstrand , Mary , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-sgx@vger.kernel.org, asahi@lists.linux.dev Subject: Re: [PATCH RFC 09/18] rust: drm: syncobj: Add DRM Sync Object abstraction Message-ID: Mail-Followup-To: Asahi Lina , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Sumit Semwal , Christian =?iso-8859-1?Q?K=F6nig?= , Luben Tuikov , Jarkko Sakkinen , Dave Hansen , Alyssa Rosenzweig , Karol Herbst , Ella Stanforth , Faith Ekstrand , Mary , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-sgx@vger.kernel.org, asahi@lists.linux.dev References: <20230307-rust-drm-v1-0-917ff5bc80a8@asahilina.net> <20230307-rust-drm-v1-9-917ff5bc80a8@asahilina.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230307-rust-drm-v1-9-917ff5bc80a8@asahilina.net> X-Operating-System: Linux phenom 6.1.0-7-amd64 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE 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 On Tue, Mar 07, 2023 at 11:25:34PM +0900, Asahi Lina wrote: > DRM Sync Objects are a container for a DMA fence, and can be waited on > signaled, exported, and imported from userspace. Add a Rust abstraction > so Rust DRM drivers can support this functionality. > > Signed-off-by: Asahi Lina > --- > rust/bindings/bindings_helper.h | 1 + > rust/helpers.c | 19 ++++++++++ > rust/kernel/drm/mod.rs | 1 + > rust/kernel/drm/syncobj.rs | 77 +++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 98 insertions(+) > > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h > index 705af292a5b4..b6696011f3a4 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > #include > #include > #include > diff --git a/rust/helpers.c b/rust/helpers.c > index 8e906a7a7d8a..11965b1e2f4e 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -20,6 +20,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -461,6 +462,24 @@ __u64 rust_helper_drm_vma_node_offset_addr(struct drm_vma_offset_node *node) > } > EXPORT_SYMBOL_GPL(rust_helper_drm_vma_node_offset_addr); > > +void rust_helper_drm_syncobj_get(struct drm_syncobj *obj) > +{ > + drm_syncobj_get(obj); > +} > +EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_get); > + > +void rust_helper_drm_syncobj_put(struct drm_syncobj *obj) > +{ > + drm_syncobj_put(obj); > +} > +EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_put); > + > +struct dma_fence *rust_helper_drm_syncobj_fence_get(struct drm_syncobj *syncobj) > +{ > + return drm_syncobj_fence_get(syncobj); > +} > +EXPORT_SYMBOL_GPL(rust_helper_drm_syncobj_fence_get); > + > #ifdef CONFIG_DRM_GEM_SHMEM_HELPER > > void rust_helper_drm_gem_shmem_object_free(struct drm_gem_object *obj) > diff --git a/rust/kernel/drm/mod.rs b/rust/kernel/drm/mod.rs > index 73fab2dee3af..dae98826edfd 100644 > --- a/rust/kernel/drm/mod.rs > +++ b/rust/kernel/drm/mod.rs > @@ -8,3 +8,4 @@ pub mod file; > pub mod gem; > pub mod ioctl; > pub mod mm; > +pub mod syncobj; > diff --git a/rust/kernel/drm/syncobj.rs b/rust/kernel/drm/syncobj.rs > new file mode 100644 > index 000000000000..10eed05eb27a > --- /dev/null > +++ b/rust/kernel/drm/syncobj.rs > @@ -0,0 +1,77 @@ > +// SPDX-License-Identifier: GPL-2.0 OR MIT > + > +//! DRM Sync Objects > +//! > +//! C header: [`include/linux/drm/drm_syncobj.h`](../../../../include/linux/drm/drm_syncobj.h) > + > +use crate::{bindings, dma_fence::*, drm, error::Result, prelude::*}; > + > +/// A DRM Sync Object > +/// > +/// # Invariants > +/// ptr is a valid pointer to a drm_syncobj and we own a reference to it. > +pub struct SyncObj { > + ptr: *mut bindings::drm_syncobj, > +} > + > +impl SyncObj { > + /// Looks up a sync object by its handle for a given `File`. > + pub fn lookup_handle(file: &impl drm::file::GenericFile, handle: u32) -> Result { > + // SAFETY: The arguments are all valid per the type invariants. > + let ptr = unsafe { bindings::drm_syncobj_find(file.raw() as *mut _, handle) }; Just an aside, but the semantics of this are nasty: You're not allowed to hold any locks while calling this. We have runtime checks for that (if you enable lockdep), but I don't see any way to encode that on the rust side and check it at compile time :-/ > + > + if ptr.is_null() { > + Err(ENOENT) > + } else { > + Ok(SyncObj { ptr }) > + } > + } > + > + /// Returns the DMA fence associated with this sync object, if any. > + pub fn fence_get(&self) -> Option { > + let fence = unsafe { bindings::drm_syncobj_fence_get(self.ptr) }; > + if fence.is_null() { > + None > + } else { > + // SAFETY: The pointer is non-NULL and drm_syncobj_fence_get acquired an > + // additional reference. > + Some(unsafe { Fence::from_raw(fence) }) > + } > + } > + > + /// Replaces the DMA fence with a new one, or removes it if fence is None. > + pub fn replace_fence(&self, fence: Option<&Fence>) { > + unsafe { > + bindings::drm_syncobj_replace_fence( > + self.ptr, > + fence.map_or(core::ptr::null_mut(), |a| a.raw()), > + ) > + }; > + } > + > + /// Adds a new timeline point to the syncobj. > + pub fn add_point(&self, chain: FenceChain, fence: &Fence, point: u64) { > + // SAFETY: All arguments should be valid per the respective type invariants. > + // This takes over the FenceChain ownership. > + unsafe { bindings::drm_syncobj_add_point(self.ptr, chain.into_raw(), fence.raw(), point) }; > + } > +} > + > +impl Drop for SyncObj { > + fn drop(&mut self) { > + // SAFETY: We own a reference to this syncobj. > + unsafe { bindings::drm_syncobj_put(self.ptr) }; > + } > +} > + > +impl Clone for SyncObj { > + fn clone(&self) -> Self { > + // SAFETY: `ptr` is valid per the type invariant and we own a reference to it. > + unsafe { bindings::drm_syncobj_get(self.ptr) }; So yeah syncobj are refcounted because they're shareable uapi objects (you can pass them around as fd), but that really should be entirely the subsystems business, not for drivers. This is kinda like drm_file, which is also refcounted (by virtue of hanging of struct file), but the refcounting is entirely handled by the vfs and all drivers get is a borrowed reference, which nicely bounds the lifetime to the callback (which is usually an ioctl handler). I think we want the same semantics for syncobj, because if a driver is hanging onto a syncobj for longer than the ioctl. If my rust understanding is right we'd get that by dropping Clone here and relying on lookup_handle only being able to return stuff that's bound by the drm_file? People are talking about drivers holding onto syncobj for longer, but I'm still not sold on the idea that this is any good and doesn't just bend the dma_fence and syncobj rules a bit too much over the breaking point. For kernel drivers it really should be just a different way to lookup and return dma_fence from the ioctl, pretty much matching what you could also do with sync_file (but since syncobj provides generic compat ioctl to convert to/from sync_file drivders only need to handle syncobj). -Daniel > + SyncObj { ptr: self.ptr } > + } > +} > + > +// SAFETY: drm_syncobj operations are internally locked. > +unsafe impl Sync for SyncObj {} > +unsafe impl Send for SyncObj {} > > -- > 2.35.1 > -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch