Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp3829366ybf; Tue, 3 Mar 2020 13:42:29 -0800 (PST) X-Google-Smtp-Source: ADFU+vv7hzibXD25M4i6kiXM+9G0RjVK0fmZuu4o79qs/3tGq5q5CD343Qrw4CdxX9Qxr0zC8Gyy X-Received: by 2002:a9d:2264:: with SMTP id o91mr4981384ota.328.1583271748973; Tue, 03 Mar 2020 13:42:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583271748; cv=none; d=google.com; s=arc-20160816; b=0xslUjuCCB8wmP8c5an/l1G1Hs33SQoDz/p2CfiBVfq9VZOO0dDb/xzlhh9HsXsZ4U D3U0e4wHOPrSpu/87Az7/W7gTdw//P/fwNRRnoBLaDetK0e4txh+rWNe/u2bNFrfUC67 KVbHCwIP1ovUhdwmuii3nelqRtHvfcrQ0+iAbJG1YZh0HyAdi0bSv/CB0W2vWFKGDT8y Ns7s+kMrBek6IP1h4AtzEuAkmWy75XSEgN+HSEwuCjkMgfPpIfG+i5eMKNR2N//6T25w ZdiBbEgUOjgt/B/DmUZgIg/0qgaA95XpJviSmor5e6FBKUn/M5EYRc/QbafAtdTRAvXl bIcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=jv7Ih1NyrqeNZpG1dR8gyRFpz0epRhdNQwzv72VuIfM=; b=m3LggKPwTImwCOR9DOGGiZZIlCjkzlm8XbHsaQY8n442TpTIsOfRq3e1nMkkG5VRyj QKIoiu6gh2UQXMvr6ygIbCxEXAtfjBACWtvFn3ZTrQW8zar86nko1xbRL1hzjFym3blz HvZi6ZtPVxufff4CfI/KdpkHS4NeAZ0b98razG7VtAj1vXi8Dh6IG3Bbxx2bANZ4WUPO wVJD1UuVAbYNc8YALw+Jk+X66cQWOhxRy1GqW4f5tuuQPH3SK8QUOhgc6DBTJHrms0hj soIoqiqycTy1Olz6RDog+kpf13W1WvxlVgaU0031XkonLMdy8Mm7lNW44lpAfwEeaAwa REQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@jlekstrand-net.20150623.gappssmtp.com header.s=20150623 header.b=O3uRmfbH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r13si3694413otq.279.2020.03.03.13.42.16; Tue, 03 Mar 2020 13:42:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@jlekstrand-net.20150623.gappssmtp.com header.s=20150623 header.b=O3uRmfbH; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730566AbgCCTKd (ORCPT + 99 others); Tue, 3 Mar 2020 14:10:33 -0500 Received: from mail-ed1-f67.google.com ([209.85.208.67]:34396 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728467AbgCCTKd (ORCPT ); Tue, 3 Mar 2020 14:10:33 -0500 Received: by mail-ed1-f67.google.com with SMTP id dm3so5853358edb.1 for ; Tue, 03 Mar 2020 11:10:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jlekstrand-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=jv7Ih1NyrqeNZpG1dR8gyRFpz0epRhdNQwzv72VuIfM=; b=O3uRmfbHh64VdAmQ/qm/TdRLtr3VzWsMlk35GCQ0l1JuNPFgo5hbwSh/l/b/kJ6zIp p2ekHiW/aBjc+H3XbL9cCmchVqOF4PcbUr8Uj7XwIEM2jM7I61+HidBb2dndcg3ycAzt RbnojAZvLK7rxv3G7Q+5s9dNqt7AkMefzVZqzibZzjsw/ARxzZYcS0wya2W5FVtJFnkk Ncojxn4je1V6yqKoYsuPmEpl+tJBm4VFaaxcy+zTxZzmXbkQk5qd2lMu9E9J8E1tzDg1 GEFBgYYSdvqAN+keSv5aTnTIC947P46GTp407QMVAxFgTn1zqXyEc98AeBmhfjcNZT3b XN+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=jv7Ih1NyrqeNZpG1dR8gyRFpz0epRhdNQwzv72VuIfM=; b=Mvt2jYe/zj1DclgtWQs2l+WavmkMq63kBYCKGtwoPpyjqzRCxpkNgzXj+KWIQ4rAgm oftd/TkVRF2S31QP0jg9Ff73t7G7N6pnOyj/Vtf4juVLMKd/Akb+2pyNI0jmmRXltBC4 6a1KkYRMv+j0tXA3pSgyA2KemOVEKC9yGhhKcj7xrKdK8Ferbc/+49wl0xWaMAK5bs7b zKklB9l/pfxyrH8/dvTY5SPbg0YbK4NfqCU6CIauy0Avmiso0/kKW+Usy3Fo+e3F6nj6 YHdu9DK1DFxix4/LgJz8tBTAulfR0UfKW+f5yY8G9jKzcjFLJshlNlClPUJOVnQnU43i ca8A== X-Gm-Message-State: ANhLgQ0E0KMAOOYQxMPYm/xShvDrPZy5/7fPMruLyfc+4FNA2qtlyO69 F1oseFMoaf9ON8riuCpBxF2CarooPxTspqjAJ/e/KQ== X-Received: by 2002:a50:f38e:: with SMTP id g14mr5691650edm.168.1583262630144; Tue, 03 Mar 2020 11:10:30 -0800 (PST) MIME-Version: 1.0 References: <20200225235856.975366-1-jason@jlekstrand.net> <8066d8b2-dd6a-10ef-a7bb-2c18a0661912@amd.com> <20200226100523.GQ2363188@phenom.ffwll.local> <810a26e7-4294-a615-b7ee-18148ac70641@amd.com> In-Reply-To: <810a26e7-4294-a615-b7ee-18148ac70641@amd.com> From: Jason Ekstrand Date: Tue, 3 Mar 2020 13:10:18 -0600 Message-ID: Subject: Re: [PATCH] RFC: dma-buf: Add an API for importing and exporting sync files To: =?UTF-8?Q?Christian_K=C3=B6nig?= Cc: Bas Nieuwenhuizen , Dave Airlie , Jesse Hall , James Jones , Daniel Stone , =?UTF-8?Q?Kristian_H=C3=B8gsberg?= , Sumit Semwal , Chenbo Feng , Greg Hackmann , linux-media@vger.kernel.org, Maling list - DRI developers , linaro-mm-sig@lists.linaro.org, LKML , Daniel Vetter Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 27, 2020 at 2:28 AM Christian K=C3=B6nig wrote: > > Am 26.02.20 um 17:46 schrieb Bas Nieuwenhuizen: > > On Wed, Feb 26, 2020 at 4:29 PM Jason Ekstrand w= rote: > >> On Wed, Feb 26, 2020 at 4:05 AM Daniel Vetter wrote: > >>> On Wed, Feb 26, 2020 at 10:16:05AM +0100, Christian K=C3=B6nig wrote: > >>> [SNIP] > >>>> Just imagine that you access some DMA-buf with a shader and that ope= ration > >>>> is presented as a fence on the DMA-bufs reservation object. And now = you can > >>>> go ahead and replace that fence and free up the memory. > >>>> > >>>> Tricking the Linux kernel into allocating page tables in that freed = memory > >>>> is trivial and that's basically it you can overwrite page tables wit= h your > >>>> shader and gain access to all of system memory :) > >>>> > >>>> What we could do is to always make sure that the added fences will c= omplete > >>>> later than the already existing ones, but that is also rather tricky= to get > >>>> right. I wouldn't do that if we don't have a rather big use case for= this. > >> Right. I thought about that but I'm still learning how dma_resv > >> works. It'd be easy enough to make a fence array that contains both > >> the old fence and the new fence and replace the old fence with that. > >> What I don't know is the proper way to replace the exclusive fence > >> safely. Some sort of atomic_cpxchg loop, perhaps? I presume there's > >> some way of doing it properly because DRM drivers are doing it all the > >> time. > > First of all you need to grab the lock of the dma_resv object or you > can't replace the exclusive nor the shared ones. > > This way you don't need to do a atomic_cmpxchg or anything else and > still guarantee correct ordering. Fixed in v3. > > I think for an exclusive fence you may need to create a fence array > > that includes the existing exclusive and shared fences in the dma_resv > > combined with the added fence. > > Yes, that at least gives us the correct synchronization. Fixed in v2 > > However, I'm not sure what the best way is to do garbage collection on > > that so that we don't get an impossibly list of fence arrays. > > Exactly yes. That's also the reason why the dma_fence_chain container I > came up with for the sync timeline stuff has such a rather sophisticated > garbage collection. > > When some of the included fences signal you need to free up the > array/chain and make sure that the memory for the container can be reused= . Currently (as of v2), I'm using dma_fence_array and being careful to not bother constructing one if there's only one fence in play. Is this insufficient? If so, maybe we should consider improving dma_fence_array. > > (Note > > the dma_resv has a lock that needs to be taken before adding an > > exclusive fence, might be useful). Some code that does a thing like > > this is __dma_resv_make_exclusive in > > drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c > > Wanted to move that into dma_resv.c for quite a while since there are > quite a few other cases where we need this. I've roughly done that. The primary difference is that my version takes an optional additional fence to add to the array. This makes it a bit more complicated but I think I got it mostly right. I've also written userspace code which exercises this and it seems to work. Hopefully, that will give a better idea of what I'm trying to accomplish. https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4037 --Jason