Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2175958rwl; Thu, 6 Apr 2023 06:56:16 -0700 (PDT) X-Google-Smtp-Source: AKy350Zap3wS/s6NnLq9aK19QiK40uf2j2sqA3gBbTvaIxy3YDNyCeKkLP8N3cUKxXN5WYE9+77Z X-Received: by 2002:a17:903:124c:b0:1a2:4921:f9a1 with SMTP id u12-20020a170903124c00b001a24921f9a1mr12314042plh.44.1680789376672; Thu, 06 Apr 2023 06:56:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680789376; cv=none; d=google.com; s=arc-20160816; b=mdcCaUFuT6T13NP8Ho52SsO6CBRMnVmuefYA8dh1dS3Ihw9zNoB032ruX4ndDnIurC qOMi+G+QoI9f3MpNDs8FT5OMEmweL0Sbd6lRAY2t5eOPaM6xqHOzpicmlsWeaQHY8O56 C+W6/kV/Vdqj4WTJuobUTUBIQv2cMDcKVKwn1lxJA7OPkvVqhPX+HyPJ39AEn9eLaMEI 2kFyxoDCYmY3v4a/9fuCnePLLBPwKsocKNOqwDM9W8ipegOoITvyDQyjx2EsV2ALwKVi 2oeqT+9Ic2+6X/X7NfccaixKWEDiTTxXV7Q/l5vo/vDCWqsIWcQk0wSnJ1Sewuh6sCfw ZD2w== 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=6IAw+w9FZQ7jDjRJq27qAY9c4XuwhViWVLB/Lio4RVQ=; b=nC8Pl+huUQkRdxdAXwwyjr2h3vf1L6cHkcXEHsqZkbOotym1UaFITHkVkb8YE3vGPf r/DzvLvKClM2OytSiaDbUqOuOLxgKkTAwZuJQ0yPZB0h8hQv3/NmOAFltsewF+f2zelw E9jdccgVhTinlqREbkXfaRI58RR2tP77M9kx7chGvNJ4FwESY0FhXytaH90fs+DvQ8uB UBZS2cDU63VDj7su3K450yHTfBdyNuNauaCz2MkIfrsZ7r0wAPFafpWIN6dvRcQeOBlS kqi2l39FgjH77Yd729ZuXCbEkFrm19NaNhZIYz4QhBlcxtCGXI3ZCf8Ly/tDCOICajwp XqjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=KUt1ik1n; 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 p13-20020a170902e74d00b001a045fcd743si1841539plf.142.2023.04.06.06.56.04; Thu, 06 Apr 2023 06:56:16 -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=KUt1ik1n; 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 S238500AbjDFNy3 (ORCPT + 99 others); Thu, 6 Apr 2023 09:54:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237846AbjDFNy0 (ORCPT ); Thu, 6 Apr 2023 09:54:26 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E31E9EF5 for ; Thu, 6 Apr 2023 06:54:23 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-502aa0f24daso150193a12.1 for ; Thu, 06 Apr 2023 06:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1680789261; 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=6IAw+w9FZQ7jDjRJq27qAY9c4XuwhViWVLB/Lio4RVQ=; b=KUt1ik1nEQ7opDVbPc0+qo1l68PyDK102Q81UoQaowFU8oZQnUfLRTEOuJ9PSGKkF9 1A++BO2vwnWo6oGsrOMP/rydGG7O9rF5mope4P/Qj/zFDIW2ytENDapXtkf6DwjeImGW MHY6SgpblxvywBYjXMGqP2wvATzKf0VPUDBZ8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680789261; 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=6IAw+w9FZQ7jDjRJq27qAY9c4XuwhViWVLB/Lio4RVQ=; b=O6itBjKB2eUVvimJSjZCyun13wL06EoQdCBy38vgtVMEfFIQNIQz/1p2klJo6BFbcj 38uaMbPhsnGCWVYgrYgtm5mAmF2zU0ELzm/e0+MeNZQdVR6nOZNBUY6E+5m1YpT+0XGp sQq8neEm5qgtPibAA297xMEAQawNBHrWIhWUcQwePsP1rXOtKGr99+cPPfyKCv+xizHi 7FIiJ5oLuFWlGQCI9A0vbpkgNoLfBf4yud9KGPlFa8f7F5YKPqkLPTysBmZZ5IzmW7Hs paO2Fo8RHkf2y7T1+UnJkvLVk+MpGFGJ7pHsHNqPax/eqOhnVTpTdZ+hmIJnITxoZ3ix Se9g== X-Gm-Message-State: AAQBX9fIn0VgNEq/t5lfwDPoJJ1ZtO+1BZlbolWPl8rHOkMNkFrPUyu+ RNG6xTtOzfRE3yWwD7c/ynvJBw== X-Received: by 2002:a17:906:3f12:b0:947:8734:a058 with SMTP id c18-20020a1709063f1200b009478734a058mr4982175ejj.4.1680789261344; Thu, 06 Apr 2023 06:54:21 -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 e4-20020a17090681c400b00925ce7c7705sm839581ejx.162.2023.04.06.06.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 06:54:20 -0700 (PDT) Date: Thu, 6 Apr 2023 15:54:18 +0200 From: Daniel Vetter To: Asahi Lina Cc: Daniel Vetter , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , 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: [Linaro-mm-sig] Re: [PATCH RFC 18/18] drm/asahi: Add the Asahi driver for Apple AGX GPUs Message-ID: Mail-Followup-To: Asahi Lina , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , 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-18-917ff5bc80a8@asahilina.net> <6200f93d-6d95-5d03-cc1c-22d7924d66eb@asahilina.net> <5330cde5-2d04-200f-d606-5467f20a5f7f@asahilina.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5330cde5-2d04-200f-d606-5467f20a5f7f@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 Thu, Apr 06, 2023 at 10:32:29PM +0900, Asahi Lina wrote: > On 06/04/2023 20.25, Daniel Vetter wrote: > > On Thu, Apr 06, 2023 at 02:02:55PM +0900, Asahi Lina wrote: > > > On 05/04/2023 23.44, Daniel Vetter wrote: > > > > On Tue, Mar 07, 2023 at 11:25:43PM +0900, Asahi Lina wrote: > > > > > +/// Look up a GEM object handle for a `File` and return an `ObjectRef` for it. > > > > > +pub(crate) fn lookup_handle(file: &DrmFile, handle: u32) -> Result { > > > > > + Ok(ObjectRef::new(shmem::Object::lookup_handle(file, handle)?)) > > > > > +} > > > > > > > > So maybe my expectations for rust typing is a bit too much, but I kinda > > > > expected this to be fully generic: > > > > > > > > - trait Driver (drm_driver) knows the driver's object type > > > > - a generic create_handle function could ensure that for drm_file (which > > > > is always for a specific drm_device and hence Driver) can ensure at the > > > > type level that you only put the right objects into the drm_file > > > > - a generic lookup_handle function on the drm_file knows the Driver trait > > > > and so can give you back the right type right away. > > > > > > > > Why the wrapping, what do I miss? > > > > > > Sigh, so this is one of the many ways I'm trying to work around the "Rust > > > doesn't do subclasses" problem (so we can figure out what the best one is > > > ^^). > > > > > > The generic shmem::Object::lookup_handle() call *is* fully generic and will > > > get you back a driver-specific object. But since Rust doesn't do > > > subclassing, what you get back isn't a driver-specific type T, but rather a > > > (reference to a) shmem::Object. T represents the inner driver-specific > > > data/functionality (only), and the outer shmem::Object includes the > > > actual drm_gem_shmem_object plus a T. This is backwards from C, where you > > > expect the opposite situation where T contains a shmem object, but that just > > > doesn't work with Rust because there's no way to build a safe API around > > > that model as far as I know. > > > > Ah I think I just got confused. I did untangle (I think at least) the > > Object trick, I guess the only thing that confused me here is why this > > is in the shmem module? Or is that the rust problem again? > > > > I'd kinda have expected that we'd have a gem::Object here that the > > lookup_handle function returns. So for the shmem case I guess that would > > then be gem::Object> for the driver type T with driver > > specific stuff? I guess not very pretty ... > > Ahh, uh... Yeah, so shmem objects are allocated their own way (the shmem > core expects to kfree them in drm_gem_shmem_free) and > bindings::drm_gem_shmem_object already contains a bindings::drm_gem_object. > Since the composition is already done in the C side, we can't just do it > again in Rust cleanly. That's why I have this weird setup with both a common > trait for common GEM functionality and separate actual types that both > implement it. Hm this is annoying. For a single driver it doesn't matter, but I do expect that once we have more, and especially once we have more libraries wrapped (ttm, gpuva, execbuf submit helpers, ...) then the common glue really becomes the gem_bo for many of these things. Could we have a GemObject trait which covers this? sole function is an unsafe one that gives you the raw C pointer :-) It still means that every gem memory manager library needs to impl that trait, but all the manager-agnostic bits in the wrappers would be generic? trait would then also have the right dependent type to ensure type safety in all this. Maybe something to discuss in the next meeting with the rust folks. > Honestly the whole GEM codepath is untested other than the bits inherited by > shmem. I'm not sure I'll be able to verify that this all makes sense until > another Rust driver comes along that needs something other than shmem. I > just felt I had to do *something* for GEM since the hierarchy is there and I > needed shmem... > > This whole gem stuff is IMO the messiest part of the abstractions though, so > I'm happy to turn it on its head if it makes it better and someone has an > idea of how to do that ^^ Yeah I still haven't worked up enough courage to type up my gem abstraction review :-/ > > > Now the problem is from the higher layers I want object operations that > > > interact with the shmem::Object (that is, they call generic GEM functions > > > on the object). Options so far: > > > > > > 1. Add an outer wrapper and put that functionality there. > > > 2. Just have the functions on T as helpers, so you need to call T::foo(obj) > > > instead of obj.foo(). > > > 3. Use the undocumented method receiver trait thing to make shmem::Object > > > a valid `self` type, plus add auto-Deref to shmem::Object. Then obj.foo() > > > works. > > > > > > #1 is what I use here. #2 is how the driver-specific File ioctl callbacks > > > are implemented, and also sched::Job. #3 is used for fence callbacks > > > (FenceObject). None of them are great, and I'd love to hear what people > > > think of the various options... > > > > > > There are other unexplored options, like in this GEM case it could be > > > covered with a driver-internal auxiliary trait impl'd on shmem::Object > > > buuut that doesn't work when you actually need callbacks on T itself to > > > circle back to shmem::Object, as is the case with File/Job/FenceObject. > > > > Ok I think I'm completely lost here. But I also havent' looked at how this > > is all really used in the driver, it's really just the shmem:: module in > > the lookup_handle function which looked strange to me. > > Ah, sorry, I misunderstood what you were talking about in my previous email > then. That's just a default trait function. It comes from common > functionality in the gem module, but shmem::Object implements the trait so > it ends up offering it too (lookup_handle() is not duplicated, it only lives > in gem, shmem only has to implement going to/from the drm_gem_object pointer > so the rest of the methods can use it). That's part of why the type/trait > hierarchy is kind of messy here, it's so I can share functionality between > both types even though they are pre-composed on the C side. Ok, so it's all already what I expect and I'm just confused with rust syntax. > In the end the object types are specialized for any given driver, so you're > always getting your own unique kind of object anyway. It's just that drivers > based on shmem will go through it to reach the common code and work with a > shmem::Object, and drivers using raw gem will use gem::Object instead. Ok, sounds all good. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch