Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3952735pxb; Fri, 11 Feb 2022 11:26:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9Kk+uBuOhFdtR5QfLcf6XxSF5Tgqhqv0FK8kRvk41srY+LpFVXjk15AHQ4tYqfi6xWEdw X-Received: by 2002:a17:902:b185:: with SMTP id s5mr3076256plr.123.1644607567235; Fri, 11 Feb 2022 11:26:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644607567; cv=none; d=google.com; s=arc-20160816; b=AK2saQCU5YH1SCkzmKnXKWtG9MsQz3a5aqZ0vWpiDDoer7Vp9M5T8GxqCt46UR5Q0X orKKs+9sXDhHnS6Q6w/wVqpJBiEHlQnbIYUN3Oak6l5v5+WzFql3eDgiM84CiekwVM77 QvMmcfC0T7nc1LuM54eBwchqnq+aZwzZxe7Helg/s6aZM9hxK7hh0NTfycKbKhsV1xLJ 6jQ+10Opgns0SSPVV2JYpADNH05A3lymKPQm4LxBVWBA939qwycqd/OCYJb8KebEYwOy jO1ak72gRWVj1eHluRWmtbsBDJwbvtACUq67pxKy+jrZs58Lssay96ihzBPGY3Dw8aIz X/Jg== 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:from:subject :references:mime-version:message-id:in-reply-to:date:dkim-signature; bh=yvZODKlks7uSqzZPx/QuM2FpvgBeZ2Sk/1s0RF7z1Xc=; b=X2aP0+YDUqMnff1/DtOduDd2SyhfegQtSIHS5CoUeOimRaT53hJ8eFvGmOT433shJs u3912DCSjwE4jfoiJyT/mnDTsjqD0K6xfEAdBs7Dc1zmMriDrIHR29VcRIjBPZDAwk6q uAI8VnPwqdzNKIcBiN8oIKvr8wtJqorea6BtGFVQeN+RAj+a64wbORjNJ358iDBCNFX3 63wSRf+9ufgW55mW50g5ZGFFYMYqLBdK6Csp0ezOoBQtYcT5Bn7MFedUgPQi5AlQBK0F twBpDzlTH30pyeUhKfOzpPRpifrEJlVh7MHlOaEUUoAWFYgnlohAslZy7gLuXa+TUTtt fpxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ec1Yy1HY; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f127si22090214pgc.335.2022.02.11.11.25.51; Fri, 11 Feb 2022 11:26:07 -0800 (PST) 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=@google.com header.s=20210112 header.b=ec1Yy1HY; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235699AbiBKQTS (ORCPT + 99 others); Fri, 11 Feb 2022 11:19:18 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:47370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351428AbiBKQTO (ORCPT ); Fri, 11 Feb 2022 11:19:14 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7E612F6 for ; Fri, 11 Feb 2022 08:19:07 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id v10-20020a05690204ca00b0061dd584eb83so19540761ybs.21 for ; Fri, 11 Feb 2022 08:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc:content-transfer-encoding; bh=yvZODKlks7uSqzZPx/QuM2FpvgBeZ2Sk/1s0RF7z1Xc=; b=ec1Yy1HYUYsk8sjBv+NO8SvcoXRcbLd5uiv9OPxDsNNur23ie7DmHnG9pRO3ilNRJc 1WvllX32ogWUY0ES0dpiEE0PkkZAqEtGHDkIOKsW2C7o8MPYg3yfWoNyM+WdEiwbD35T Wm/nnsqgsS4c257blD2h2q3WLvab8g25Dj83ORJVcsTjGYJyVICqcM/zkGknDnaHwtni S4ZDOVi8GlgnNuE1vqJ9LyP4yV9NB3a+ujEPEu7FgVA5ADIEY9Yho6UbQZlXhZiHM4fN WFpGmfjmGs9WnZVI4xN8jGbbfCl8+QIJaWkRzPaFr5hx6+gvkC/2/iIAsFPFyG5TXVMH vNVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=yvZODKlks7uSqzZPx/QuM2FpvgBeZ2Sk/1s0RF7z1Xc=; b=CB/AsVJsTn3nZCqMITVTgsAQjcGXmbV0NVwH5PAc2q0f12hoDNS7QNcmVSjGkKuRT/ rHwHEvgD8teQ5ohfKzQ5fKV4GMcCtdZRPxwczMPTctJx9EZ5lX4au2urIl/soVKsaoIw EediudJ4Q+atcx/L/8yZ2EqjwXNIwOw/pZ0stCa+b9nTxkUuqLnoQ7cNNlh1DjiuLozt Hvxw3U8EmCU2SXdjFfXglv3jl39TQJW8JvGh6vLAED11VNvtVSJ6Oc1MuiLDQt4Z2P+4 L4lAwdmoBB+0gYeMJsZN7MQDnuzS7FcNFcXY2LJPXjqxUviFwPwvLCXPyiM/PkFL0iBR gD5g== X-Gm-Message-State: AOAM533R8Wf0gTFNZj6v5bnMhNmhMnnFO5niupF/RAQbbpQZPA5haImT VjXi9wtFIi7zCNgqXK57Swf8RXnM2hYeprM= X-Received: from tj2.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:187]) (user=tjmercier job=sendgmr) by 2002:a81:2d03:: with SMTP id t3mr2496118ywt.215.1644596347024; Fri, 11 Feb 2022 08:19:07 -0800 (PST) Date: Fri, 11 Feb 2022 16:18:28 +0000 In-Reply-To: <20220211161831.3493782-1-tjmercier@google.com> Message-Id: <20220211161831.3493782-6-tjmercier@google.com> Mime-Version: 1.0 References: <20220211161831.3493782-1-tjmercier@google.com> X-Mailer: git-send-email 2.35.1.265.g69c8d7142f-goog Subject: [RFC v2 5/6] dmabuf: Add gpu cgroup charge transfer function From: "T.J. Mercier" To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Jonathan Corbet , Greg Kroah-Hartman , "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Christian Brauner , Hridya Valsaraju , Suren Baghdasaryan , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Benjamin Gaignard , Liam Mark , Laura Abbott , Brian Starkey , John Stultz , Tejun Heo , Zefan Li , Johannes Weiner Cc: kaleshsingh@google.com, Kenny.Ho@amd.com, "T.J. Mercier" , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 The dma_buf_charge_transfer function 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 memory. From: Hridya Valsaraju Signed-off-by: Hridya Valsaraju Co-developed-by: T.J. Mercier Signed-off-by: T.J. Mercier --- changes in v2 - Move dma-buf cgroup charge transfer from a dma_buf_op defined by every heap to a single dma-buf function for all heaps per Daniel Vetter and Christian K=C3=B6nig. drivers/dma-buf/dma-buf.c | 48 +++++++++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 2 ++ 2 files changed, 50 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 83d0d1b91547..55e1b982f840 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -1374,6 +1374,54 @@ void dma_buf_vunmap(struct dma_buf *dmabuf, struct d= ma_buf_map *map) } EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap, DMA_BUF); =20 +/** + * dma_buf_charge_transfer - Change the GPU cgroup to which the provided d= ma_buf + * is charged. + * @dmabuf: [in] buffer whose charge will be migrated to a different GPU + * cgroup + * @gpucg: [in] the destination GPU cgroup for dmabuf's charge + * + * Only tasks that belong to the same cgroup the buffer is currently charg= ed to + * may call this function, otherwise it will return -EPERM. + * + * Returns 0 on success, or a negative errno code otherwise. + */ +int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg) +{ +#ifdef CONFIG_CGROUP_GPU + struct gpucg *current_gpucg; + int ret =3D 0; + + /* + * Verify that the cgroup of the process requesting the transfer is the + * same as the one the buffer is currently charged to. + */ + current_gpucg =3D gpucg_get(current); + mutex_lock(&dmabuf->lock); + if (current_gpucg !=3D dmabuf->gpucg) { + ret =3D -EPERM; + goto err; + } + + ret =3D gpucg_try_charge(gpucg, dmabuf->gpucg_dev, dmabuf->size); + if (ret) + goto err; + + dmabuf->gpucg =3D gpucg; + + /* uncharge the buffer from the cgroup it's currently charged to. */ + gpucg_uncharge(current_gpucg, dmabuf->gpucg_dev, dmabuf->size); + +err: + mutex_unlock(&dmabuf->lock); + gpucg_put(current_gpucg); + return ret; +#else + return 0; +#endif /* CONFIG_CGROUP_GPU */ +} +EXPORT_SYMBOL_NS_GPL(dma_buf_charge_transfer, DMA_BUF); + #ifdef CONFIG_DEBUG_FS static int dma_buf_debug_show(struct seq_file *s, void *unused) { diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 742f29c3daaf..85c940c08867 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -646,4 +646,6 @@ int dma_buf_mmap(struct dma_buf *, struct vm_area_struc= t *, unsigned long); int dma_buf_vmap(struct dma_buf *dmabuf, struct dma_buf_map *map); void dma_buf_vunmap(struct dma_buf *dmabuf, struct dma_buf_map *map); + +int dma_buf_charge_transfer(struct dma_buf *dmabuf, struct gpucg *gpucg); #endif /* __DMA_BUF_H__ */ --=20 2.35.1.265.g69c8d7142f-goog