Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1192095pxb; Fri, 21 Jan 2022 11:55:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwAM8usJocs0yEVcpv7WIsXBR+0HfFuBLI4abY8Rr0nXAqLfTgCS00dGehWpvuhYT6MdUq X-Received: by 2002:aa7:91c3:0:b0:4b0:eebe:49c0 with SMTP id z3-20020aa791c3000000b004b0eebe49c0mr5045175pfa.6.1642794928222; Fri, 21 Jan 2022 11:55:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642794928; cv=none; d=google.com; s=arc-20160816; b=QtJoXaJsjAbEwhJkP74UlLdadkfqkO2eMC5POi7KXOB5qRUIyAR8emRl8WgdLFN3vI 4PNqfb6KcQKogQAkDLfvM698HvNb/2kEZSB3L1v5rkLt8UEshHKoS5YbUkVWO62LdWuf PtqQjNd3RDeczuU0PzqJ9STovLGPZZNRtde6Q4bnDbjLyMxWSAQgICtJu3E/7zO4D8Kd V7gczCKBwOBRI1DFDmbaSoAQNYIcpSgBpyYnqoQ+byxNB5WRgkY200MUZiiQN2jF7Z8C qQn9i1+jne56lYTz8fTh20Ncfd6gWTh9LtXHelgyO2LylaJUvm/YNLboX30rRsCL5jvZ UQ1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ow/r3jR/x5GfOuGnuaEDOpHXZ2D5zk6XSR55QH2bw2k=; b=hiOGyKyLIkvnV1wKbPZtDkZncSAcUn30gpePsczZmFH5tlC5n51NddGdilgssPAR4t drDra70bSz/l3oDgUV+BEa9LCwcq6HM9JAKcw/hu4bX+7Wa1RX6kcyr7bqb2TO+aLDdH fFHoIa0hbZ3c97Pv+pDKNklrJEnLmNXs065FL4348YwdL1+D5Pfi1wYyP2lLgj3//SIO Tp2ojIz6mjBixxv9xS34pvsEYZNB2uKF0fhZHIXgRuvSKvRS2gfIk+GxdR78Y2euq8IQ WSHCWcAjrBdoPwKgvb19vcRUTY02LLaJA9gyNQEX599QkEP2dlw3R1FEV4DwfKNdwaSk lAHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FLI8i7I5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 127si6878032pgc.257.2022.01.21.11.55.16; Fri, 21 Jan 2022 11:55:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=FLI8i7I5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356698AbiASSWi (ORCPT + 99 others); Wed, 19 Jan 2022 13:22:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235941AbiASSWd (ORCPT ); Wed, 19 Jan 2022 13:22:33 -0500 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09A0BC061574 for ; Wed, 19 Jan 2022 10:22:33 -0800 (PST) Received: by mail-yb1-xb33.google.com with SMTP id h14so9985046ybe.12 for ; Wed, 19 Jan 2022 10:22:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ow/r3jR/x5GfOuGnuaEDOpHXZ2D5zk6XSR55QH2bw2k=; b=FLI8i7I5yY8vnry+xbaYi6fdQCzv/or9KmsXB8RqqToh1UZrTutJ8AvfQ35vIwRpaZ ZVIDvaMC9WrXJlmNwtpWjHaWlh2hOUrkwFG7C3i2Gjctt+KKhjxyftX7F345XteoowUn tmj3k+QHGMBCEsEd1hH7U0I28J5nEx8e5PpVhVuGXuhCte6lCImn19BiZqTDEqOw/Fdg fvcYZ8Aq05r+5q6HwLuIb9fePc6b1rxye0TZ4rjMY47us1/36Bhlc7LrAw524c1oJWJz AMiicIHHXoNcakeFhzP4hNVTgux4URUiw4uTdMc92CXEM0fnjkPofkv52VCekSlDs/yT fqqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ow/r3jR/x5GfOuGnuaEDOpHXZ2D5zk6XSR55QH2bw2k=; b=CM2JOIDSlQk7F/yuFFHieCf+wjIb5VrVxgD9gk48Sxz0uGCl6UizEZYp5dNQEhHf22 LCjvRgA+OuIP+xav1QEssUheN/3MnRuoWgOT5Q9NXwDfyN/i7bYU6uGOtJQJ95/5vhz2 +C1DrLLkEtMnMSGIg/xbA/PsbujJz2H+R0hWLtkxChE6+OIPIjesSitWf8H6lbtKcZgJ J7JFv/vSHhBU93nWhxiqsgpP8ZWqbM8Gf1n1+cu3C76WmCaoGT00V1mcqBVqG817v6kT QX4WCDVGmBXfZm3Wqj6E4KgJG/c8UYPvNnWdMPsHbQD4SK2QX4WVh9QQ/db808xfLJVf 8SAg== X-Gm-Message-State: AOAM530mU757UvYFU65Nrm2Gpk72Uwk268d1DpMG29F5s89zTnf9hL7k YjPPkRjhMss9w98GbfB3b7YdKrgCH4rmA7BVm/X5sw== X-Received: by 2002:a5b:550:: with SMTP id r16mr38968597ybp.403.1642616552050; Wed, 19 Jan 2022 10:22:32 -0800 (PST) MIME-Version: 1.0 References: <20220115010622.3185921-1-hridya@google.com> <20220115010622.3185921-5-hridya@google.com> <5cc27a05-8131-ce9b-dea1-5c75e994216d@amd.com> In-Reply-To: <5cc27a05-8131-ce9b-dea1-5c75e994216d@amd.com> From: Hridya Valsaraju Date: Wed, 19 Jan 2022 10:21:56 -0800 Message-ID: Subject: Re: [RFC 4/6] dma-buf: Add DMA-BUF exporter op to charge a DMA-BUF to a cgroup. To: =?UTF-8?Q?Christian_K=C3=B6nig?= Cc: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Jonathan Corbet , Greg Kroah-Hartman , =?UTF-8?B?QXJ2ZSBIasO4bm5ldsOlZw==?= , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Suren Baghdasaryan , Sumit Semwal , Benjamin Gaignard , Liam Mark , Laura Abbott , Brian Starkey , John Stultz , Tejun Heo , Zefan Li , Johannes Weiner , Dave Airlie , Jason Ekstrand , Matthew Auld , Matthew Brost , Li Li , Marco Ballesio , Miguel Ojeda , Hang Lu , Wedson Almeida Filho , Masahiro Yamada , Andrew Morton , Nathan Chancellor , Kees Cook , Nick Desaulniers , Chris Down , Vipin Sharma , Daniel Borkmann , Vlastimil Babka , Arnd Bergmann , dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, cgroups@vger.kernel.org, Kenny.Ho@amd.com, daniels@collabora.com, kaleshsingh@google.com, tjmercier@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 7:58 AM Christian K=C3=B6nig wrote: > > Am 19.01.22 um 16:54 schrieb Daniel Vetter: > > On Tue, Jan 18, 2022 at 10:54:16AM -0800, Hridya Valsaraju wrote: > >> On Sun, Jan 16, 2022 at 11:46 PM Christian K=C3=B6nig > >> wrote: > >>> Am 15.01.22 um 02:06 schrieb Hridya Valsaraju: > >>>> The optional exporter op provides a way for processes to transfer > >>>> charge of a buffer to a different process. This is essential for the > >>>> cases where a central allocator process does allocations for various > >>>> subsystems, hands over the fd to the client who > >>>> requested the memory and drops all references to the allocated memor= y. > >>>> > >>>> Signed-off-by: Hridya Valsaraju > >>>> --- > >>>> include/linux/dma-buf.h | 18 ++++++++++++++++++ > >>>> 1 file changed, 18 insertions(+) > >>>> > >>>> diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h > >>>> index 7ab50076e7a6..d5e52f81cc6f 100644 > >>>> --- a/include/linux/dma-buf.h > >>>> +++ b/include/linux/dma-buf.h > >>>> @@ -13,6 +13,7 @@ > >>>> #ifndef __DMA_BUF_H__ > >>>> #define __DMA_BUF_H__ > >>>> > >>>> +#include > >>>> #include > >>>> #include > >>>> #include > >>>> @@ -285,6 +286,23 @@ struct dma_buf_ops { > >>>> > >>>> int (*vmap)(struct dma_buf *dmabuf, struct dma_buf_map *map); > >>>> void (*vunmap)(struct dma_buf *dmabuf, struct dma_buf_map *ma= p); > >>>> + > >>>> + /** > >>>> + * @charge_to_cgroup: > >>>> + * > >>>> + * This is called by an exporter to charge a buffer to the spe= cified > >>>> + * cgroup. > >>> Well that sentence makes absolutely no sense at all. > >>> > >>> The dma_buf_ops are supposed to be called by the DMA-buf subsystem on > >>> behalves of the importer and never by the exporter itself. > >>> > >>> I hope that this is just a documentation mixup. > >> Thank you for taking a look Christian! > >> > >> Yes, that was poor wording, sorry about that. It should instead say > >> that the op would be called by the process the buffer is currently > >> charged to in order to transfer the buffer's charge to a different > >> cgroup. This is helpful in the case where a process acts as an > >> allocator for multiple client processes and we would like the > >> allocated buffers to be charged to the clients who requested their > >> allocation(instead of the allocating process as is the default > >> behavior). In Android, the graphics allocator HAL process[1] does > >> most of the graphics allocations on behalf of various clients. After > >> allocation, the HAL process passes the fd to the client over binder > >> IPC and the binder driver invokes the charge_to_cgroup() DMA-BUF op to > >> uncharge the buffer from the HAL process and charge it to the client > >> process instead. > >> > >> [1]: https://nam11.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F= %2Fsource.android.com%2Fdevices%2Fgraphics%2Farch-bq-gralloc&data=3D04%= 7C01%7Cchristian.koenig%40amd.com%7C838d25da974d4ea4257508d9db63eb70%7C3dd8= 961fe4884e608e11a82d994e183d%7C0%7C0%7C637782044488604857%7CUnknown%7CTWFpb= GZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7= C3000&sdata=3DQn7JeyF5Rq9tnrGw1KgNuQkpu5RbcrvPhDOa1OBJ6TU%3D&reserv= ed=3D0 > > For that use-case, do we really need to have the vfunc abstraction and > > force all exporters to do something reasonable with it? > > I was about to write up a similar answer, but more from the technical sid= e. > > Why in the world should that be done on the DMA-buf object as a > communication function between importer and exporter? > > That design makes absolutely no sense at all to me. > > Regards, > Christian. > > > > > I think just storing the cgrpus gpu memory bucket this is charged again= st > > and doing this in a generic way would be a lot better. > > > > That way we can also easily add other neat features in the future, like > > e.g. ttm could take care of charge-assignement automatically maybe, or = we > > could print the current cgroups charge relationship in the sysfs info > > file. Or anything else really. Thank you for the comments Daniel and Christian! I made the charge/uncharge/transfer part of the exporter implementation since it provided exporters a choice on whether they wanted to enable cgroup memory accounting for the buffers they were exporting. I also see the benefits of making the charge/uncharge/transfer generic by moving it to the DMA-BUF framework like you are suggesting though. We will move to a more generic design in the next version of the RFC. Regards, Hridya > > > > I do feel that in general for gpu memory cgroups to be useful, we shoul= d > > really have memory pools as a fairly strong concept. Otherwise every > > driver/allocator/thing is going to come up with their own, and very lik= ely > > incompatible interpretation. And we end up with a supposed generic cgro= ups > > interface which cannot actually be used in a driver/vendor agnostic way= at > > all. > > -Daniel > > > >> Regards, > >> Hridya > >> > >> > >>> Regards, > >>> Christian. > >>> > >>>> The caller must hold a reference to @gpucg obtained via > >>>> + * gpucg_get(). The DMA-BUF will be uncharged from the cgroup = it is > >>>> + * currently charged to before being charged to @gpucg. The ca= ller must > >>>> + * belong to the cgroup the buffer is currently charged to. > >>>> + * > >>>> + * This callback is optional. > >>>> + * > >>>> + * Returns: > >>>> + * > >>>> + * 0 on success or negative error code on failure. > >>>> + */ > >>>> + int (*charge_to_cgroup)(struct dma_buf *dmabuf, struct gpucg *= gpucg); > >>>> }; > >>>> > >>>> /** >