Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp4687462ybf; Wed, 4 Mar 2020 08:43:34 -0800 (PST) X-Google-Smtp-Source: ADFU+vsSjCR1itWun7Dz5CO3C08S/Ht+eRik1jSV2uuGijnKcICWy5pbunGDI7GZZdi2+9HJTAtQ X-Received: by 2002:aca:4a4e:: with SMTP id x75mr2414679oia.16.1583340214492; Wed, 04 Mar 2020 08:43:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583340214; cv=none; d=google.com; s=arc-20160816; b=PdnV9Ov2QxQfFsfeoIq8DXUJPsaoYa6pr+o5WohegqkdQbvyc4liTgi8nkLV2RkJFp jQN2S8G1q6ZvBPC8ndkxziuzsrwuQ+RNDFvfSs/x8v3ZVkKOjgNe8BZhiTUqzcEgSGMl XxbELDBIdRpyXIHnunzcSl3hBDMD8mrbLJy6ZtouOcDiFdiDPUGdLakElfQPucv2PWzG Eq/86JPJbAHhHeIS+H3NqPQuPVxErJd/4p/zDgB6RsvsRf/3wwmBOVQ0sJl452JLLYw+ JOPFrF8r+S+dKpf99PL8zCZyeCPR/OC0uRZeOixU1nzGarI1i/f5Ebxf3YlVa6d8KRxe MkZQ== 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=ySiSOnqcuA6nNk8c0jQCBZCPf0dSfXosbzJcm8d2dZY=; b=cWMzt6liEvln1C9pu4q+E2h4HmOaejzBVFv9Euz2BvX/30kP/2zq7ybW6psxBQOXEX 5OB0IbX3HI/hw6YNuyokDc5ZoNlGw3xkN+J9guLe9t8TKUYUSu8W6LCozEvXSiYh85fO Kqch81Xg+yG1f3t4fowE+kb0+kDazDOncNlBQfTIqgyUoIw+6fGYPkISKMVgpAkWIv7g TdNdxRh10KfRmoPT1c9Zs8TgPy4rbPGXJoeFN/ofeYrWPd7QyQ/XQoGm83uTqkYyrCOV DMjS1ZJKaTs8a2J8g0btwXvfpuWF5CWcphHcqOf9C9YElMGTB5jgo4XO6cJ5UmfWS8FE elMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@jlekstrand-net.20150623.gappssmtp.com header.s=20150623 header.b=CGhp6TNs; 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 t26si1516258otc.205.2020.03.04.08.43.21; Wed, 04 Mar 2020 08:43:34 -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=CGhp6TNs; 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 S1729865AbgCDQll (ORCPT + 99 others); Wed, 4 Mar 2020 11:41:41 -0500 Received: from mail-ed1-f65.google.com ([209.85.208.65]:40450 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729849AbgCDQll (ORCPT ); Wed, 4 Mar 2020 11:41:41 -0500 Received: by mail-ed1-f65.google.com with SMTP id a13so3066735edu.7 for ; Wed, 04 Mar 2020 08:41:40 -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=ySiSOnqcuA6nNk8c0jQCBZCPf0dSfXosbzJcm8d2dZY=; b=CGhp6TNsRBfRwAxiBCYBK+2j2pYnrWkcFtf+MnmaID1ngwxqdlKRJ6FfdxB35Sa+ID Og7cAaCnPcGGkMbb9DNd4/GpvNZ0RAupTytTiQZBUPb56W4J0gv+vo2Tp7BqLGM/Wa92 eNYypwTywpd6WelSB6qAzgraAMaAT725rtlK3vF1oavPqVDtvB5gSUb0EfMWRpOZfXb+ w86CuJRAP20QEzeMX0DYwE9Xonf5xONmLXLnGqGepmA5OWHvlRbe0HQGvlyYICbwVwdC 7+2JjcXKhUoM3R4bnp3PLB7ijlZe/D7JyaTO6/lTKOH8JikLCLkg/7X0szhBZCXyU3Mu IvQw== 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=ySiSOnqcuA6nNk8c0jQCBZCPf0dSfXosbzJcm8d2dZY=; b=nUUmFusbq7s18bRvktn59lU7gwxHcFIznIa6/DxVkFu8CnnRpuusxpiJ0tn4ggUKaO USl4Of1Xj9PginZgUUx/Id9QRU7Zl35VfOw3dmKaOzxXC0fUSaSnEMRs+hrxPIuDS2xu yT9zAj9jzhZo6tEObIu93FKFcAMt/KdPWQogjXHq27M8//oWcI3ZuvpG/jCY4cyNuRGm nlmQhLWWOTSTIucw+dyBvSX9ZbgIX5JuKW2TXN9cvuoIF2P7b6me1zYNN8m/hHjrJkYa I7MKKL2Jd9SLcRYV8iVOVZiPeUHOxkQ7wIHTr5PsuevagktmzBm2QwmyQ0bz4gK7Fydc zukQ== X-Gm-Message-State: ANhLgQ3nWeG/yyFuAlERIeVwrTgYQjyhNqlnBbC+szBTtPZizW8+1o9O Yx5Mp0rSLSA08hVY9jSHiNl1SMP9zTudaKQnaFohcw== X-Received: by 2002:aa7:c983:: with SMTP id c3mr3441099edt.98.1583340099330; Wed, 04 Mar 2020 08:41:39 -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> <21aeacc0-f3ae-c5dd-66df-4d2f3d73f73e@amd.com> In-Reply-To: From: Jason Ekstrand Date: Wed, 4 Mar 2020 10:41:28 -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 Wed, Mar 4, 2020 at 10:27 AM Jason Ekstrand wrote= : > > On Wed, Mar 4, 2020 at 2:34 AM Christian K=C3=B6nig wrote: > > > > Am 03.03.20 um 20:10 schrieb Jason Ekstrand: > > > On Thu, Feb 27, 2020 at 2:28 AM Christian K=C3=B6nig > > > wrote: > > >> [SNIP] > > >>> 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 containe= r I > > >> came up with for the sync timeline stuff has such a rather sophistic= ated > > >> 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 r= eused. > > > 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. > > > > That still won't work correctly in all cases. See the problem is not > > only optimization, but also avoiding situations where userspace can > > abuse the interface to do nasty things. > > > > For example if userspace just calls that function in a loop you can > > create a long chain of dma_fence_array objects. > > > > If that chain is then suddenly released the recursive dropping of > > references can overwrite the kernel stack. > > > > For reference see what dance is necessary in the dma_fence_chain_releas= e > > function to avoid that: > > > /* Manually unlink the chain as much as possible to avoid > > > recursion > > > * and potential stack overflow. > > > */ > > > while ((prev =3D rcu_dereference_protected(chain->prev, true)= )) { > > .... > > > > It took me quite a while to figure out how to do this without causing > > issues. But I don't see how this would be possible for dma_fence_array. > > Ah, I see the issue now! It hadn't even occurred to me that userspace > could use this to build up an infinite recursion chain. That's nasty! > I'll give this some more thought and see if can come up with > something clever. > > Here's one thought: We could make dma_fence_array automatically > collapse any arrays it references and instead directly reference their > fences. This way, no matter how much the client chains things, they > will never get more than one dma_fence_array. Of course, the > difficulty here (answering my own question) comes if they ping-pong > back-and-forth between something which constructs a dma_fence_array > and something which constructs a dma_fence_chain to get > array-of-chain-of-array-of-chain-of-... More thought needed. Answering my own questions again... I think the array-of-chain-of-array case is also solvable. For array-of-chain, we can simply add all unsignaled dma_fences in the chain to the array. The array won't signal until all of them have which is exactly the same behavior as if we'd added the chain itself. For chain-of-array, we can add all unsignaled dma_fences in the array to the same point in the chain. There may be some fiddling with the chain numbering required here but I think we can get it so the chain won't signal until everything in the array has signaled and we get the same behavior as if we'd added the dma_fence_array to the chain. In both cases, we end up with either a single array or a single and destruction doesn't require recursion. Thoughts? --Jason