[Public] > From: Koenig, Christian <Christian.Koenig@xxxxxxx> > Sent: Wednesday, December 11, 2024 3:16 > Am 10.12.24 um 18:59 schrieb Yunxiang Li: > > Tracking the state of a GEM object for shared stats is quite difficult > > since the handle_count is managed behind driver's back. So instead > > considers GEM object shared the moment it is exported with flink ioctl. > > This makes it work the same to the dma_buf case. Add a callback for > > drivers to get notified when GEM object is being shared. > > First of all GEM flink is pretty much deprecated, we only have it for compatibility > reasons. So please don't change anything here. > > Then flink is not the only way to create multiple handles for a GEM object. So this > here won't handle all cases. > > And finally we already have the .open and .close callbacks, which are called > whenever a handle for a GEM object is created/destroyed. So it shouldn't be > necessary in the first place. For the importing VM the shared stats is automatically correct by open and close, but for the exporting VM we need to update the shared stat when the buffer gets shared, since it is already counted as private there. As far as I could find, seems like flink ioctl is the only place where the global name is assigned? The importing side have multiple places to get the global name, but the exporter always needs to first call flink to allocate the number right? So hooking into flink and dma-buf should cover the bases? I could probably make handle_count work somehow, but it looks like it's read in a lot of places without locks so I'm not sure if there will be some race conditions. > Regards, > Christian. > > > > > Signed-off-by: Yunxiang Li <Yunxiang.Li@xxxxxxx> > > > > CC: dri-devel@xxxxxxxxxxxxxxxxxxxxx > > --- > > drivers/gpu/drm/drm_gem.c | 3 +++ > > drivers/gpu/drm/drm_prime.c | 3 +++ > > include/drm/drm_gem.h | 12 +++++++++++- > > 3 files changed, 17 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c > > index d4bbc5d109c8b..1ead11de31f6b 100644 > > --- a/drivers/gpu/drm/drm_gem.c > > +++ b/drivers/gpu/drm/drm_gem.c > > @@ -854,6 +854,9 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data, > > goto err; > > > > obj->name = ret; > > + > > + if (obj->funcs->shared) > > + obj->funcs->shared(obj); > > } > > > > args->name = (uint64_t) obj->name; > > diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c > > index 0e3f8adf162f6..336d982d69807 100644 > > --- a/drivers/gpu/drm/drm_prime.c > > +++ b/drivers/gpu/drm/drm_prime.c > > @@ -406,6 +406,9 @@ static struct dma_buf *export_and_register_object(struct > drm_device *dev, > > obj->dma_buf = dmabuf; > > get_dma_buf(obj->dma_buf); > > > > + if (obj->funcs->shared) > > + obj->funcs->shared(obj); > > + > > return dmabuf; > > } > > > > diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index > > da11c16e212aa..8c5ffcd485752 100644 > > --- a/include/drm/drm_gem.h > > +++ b/include/drm/drm_gem.h > > @@ -122,6 +122,16 @@ struct drm_gem_object_funcs { > > */ > > struct dma_buf *(*export)(struct drm_gem_object *obj, int flags); > > > > + /** > > + * @shared: > > + * > > + * Callback when GEM object becomes shared, see also > > + * drm_gem_object_is_shared_for_memory_stats > > + * > > + * This callback is optional. > > + */ > > + void (*shared)(struct drm_gem_object *obj); > > + > > /** > > * @pin: > > * > > @@ -568,7 +578,7 @@ int drm_gem_evict(struct drm_gem_object *obj); > > */ > > static inline bool drm_gem_object_is_shared_for_memory_stats(struct > drm_gem_object *obj) > > { > > - return (obj->handle_count > 1) || obj->dma_buf; > > + return obj->name || obj->dma_buf; > > } > > > > #ifdef CONFIG_LOCKDEP