linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Maxime Ripard <mripard@kernel.org>
To: "Andrew Morton" <akpm@linux-foundation.org>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Benjamin Gaignard" <benjamin.gaignard@collabora.com>,
	"Brian Starkey" <Brian.Starkey@arm.com>,
	"John Stultz" <jstultz@google.com>,
	"T.J. Mercier" <tjmercier@google.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Tomasz Figa" <tfiga@chromium.org>,
	"Mauro Carvalho Chehab" <mchehab@kernel.org>
Cc: Hans Verkuil <hverkuil@xs4all.nl>,
	 Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>,
	 linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	iommu@lists.linux.dev,  linux-media@vger.kernel.org,
	dri-devel@lists.freedesktop.org,  linaro-mm-sig@lists.linaro.org,
	Maxime Ripard <mripard@kernel.org>
Subject: [PATCH RFC 12/12] media: videobuf2: Track buffer allocations through the dmem cgroup
Date: Mon, 10 Mar 2025 13:06:18 +0100	[thread overview]
Message-ID: <20250310-dmem-cgroups-v1-12-2984c1bc9312@kernel.org> (raw)
In-Reply-To: <20250310-dmem-cgroups-v1-0-2984c1bc9312@kernel.org>

The dmem cgroup allows to track any DMA memory allocation made by the
userspace. Let's charge our allocations in videobuf2 to enable proper
memory tracking.

Signed-off-by: Maxime Ripard <mripard@kernel.org>
---
 drivers/media/common/videobuf2/videobuf2-dma-contig.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/drivers/media/common/videobuf2/videobuf2-dma-contig.c b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
index a13ec569c82f6da2d977222b94af32e74c6c6c82..48384e18030812f4f89f1c225c38def2ac6aa3ca 100644
--- a/drivers/media/common/videobuf2/videobuf2-dma-contig.c
+++ b/drivers/media/common/videobuf2/videobuf2-dma-contig.c
@@ -8,10 +8,11 @@
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation.
  */
 
+#include <linux/cgroup_dmem.h>
 #include <linux/dma-buf.h>
 #include <linux/module.h>
 #include <linux/refcount.h>
 #include <linux/scatterlist.h>
 #include <linux/sched.h>
@@ -40,10 +41,14 @@ struct vb2_dc_buf {
 	struct sg_table			*sgt_base;
 
 	/* DMABUF related */
 	struct dma_buf_attachment	*db_attach;
 
+#ifdef CONFIG_CGROUP_DMEM
+	struct dmem_cgroup_pool_state	*cgroup_pool_state;
+#endif
+
 	struct vb2_buffer		*vb;
 	bool				non_coherent_mem;
 };
 
 /*********************************************/
@@ -169,10 +174,14 @@ static void vb2_dc_put(void *buf_priv)
 	struct vb2_dc_buf *buf = buf_priv;
 
 	if (!refcount_dec_and_test(&buf->refcount))
 		return;
 
+#ifdef CONFIG_CGROUP_DMEM
+	dmem_cgroup_uncharge(buf->cgroup_pool_state, buf->size);
+#endif
+
 	if (buf->non_coherent_mem) {
 		if (buf->vaddr)
 			dma_vunmap_noncontiguous(buf->dev, buf->vaddr);
 		dma_free_noncontiguous(buf->dev, buf->size,
 				       buf->dma_sgt, buf->dma_dir);
@@ -230,10 +239,11 @@ static int vb2_dc_alloc_non_coherent(struct vb2_dc_buf *buf)
 
 static void *vb2_dc_alloc(struct vb2_buffer *vb,
 			  struct device *dev,
 			  unsigned long size)
 {
+	struct dmem_cgroup_pool_state *pool;
 	struct vb2_dc_buf *buf;
 	int ret;
 
 	if (WARN_ON(!dev))
 		return ERR_PTR(-EINVAL);
@@ -249,25 +259,34 @@ static void *vb2_dc_alloc(struct vb2_buffer *vb,
 
 	buf->size = size;
 	/* Prevent the device from being released while the buffer is used */
 	buf->dev = get_device(dev);
 
+	ret = dmem_cgroup_try_charge(dma_get_dmem_cgroup_region(dev), size, &pool, NULL);
+	if (ret)
+		return ret;
+
 	if (buf->non_coherent_mem)
 		ret = vb2_dc_alloc_non_coherent(buf);
 	else
 		ret = vb2_dc_alloc_coherent(buf);
 
 	if (ret) {
 		dev_err(dev, "dma alloc of size %lu failed\n", size);
+		dmem_cgroup_uncharge(pool, size);
 		kfree(buf);
 		return ERR_PTR(-ENOMEM);
 	}
 
 	buf->handler.refcount = &buf->refcount;
 	buf->handler.put = vb2_dc_put;
 	buf->handler.arg = buf;
 
+#ifdef CONFIG_CGROUP_DMEM
+	buf->cgroup_pool_state = pool;
+#endif
+
 	refcount_set(&buf->refcount, 1);
 
 	return buf;
 }
 

-- 
2.48.1



  parent reply	other threads:[~2025-03-10 12:06 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-10 12:06 [PATCH RFC 00/12] dma: Enable dmem cgroup tracking Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 01/12] cma: Register dmem region for each cma region Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 02/12] cma: Provide accessor to cma dmem region Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 03/12] dma: coherent: Register dmem region for each coherent region Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 04/12] dma: coherent: Provide accessor to dmem region Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 05/12] dma: contiguous: " Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 06/12] dma: direct: " Maxime Ripard
2025-03-10 14:56   ` Robin Murphy
2025-03-10 16:28     ` Maxime Ripard
2025-03-10 18:44       ` Robin Murphy
2025-03-13 18:16         ` Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 07/12] dma: Create default dmem region for DMA allocations Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 08/12] dma: Provide accessor to dmem region Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 09/12] dma-buf: Clear cgroup accounting on release Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 10/12] dma-buf: cma: Account for allocations in dmem cgroup Maxime Ripard
2025-03-10 12:06 ` [PATCH RFC 11/12] drm/gem: Add cgroup memory accounting Maxime Ripard
2025-03-10 15:06   ` Robin Murphy
2025-03-10 12:06 ` Maxime Ripard [this message]
2025-03-10 12:15 ` [PATCH RFC 00/12] dma: Enable dmem cgroup tracking Maxime Ripard
2025-03-10 14:16 ` Christian König
2025-03-10 14:26   ` Maxime Ripard
2025-03-31 20:43     ` Dave Airlie
2025-04-01 11:03       ` Christian König
2025-04-03  6:07         ` Dave Airlie
2025-04-03  7:39           ` Christian König
2025-04-03 15:47             ` Maxime Ripard
2025-04-04  8:47               ` Christian König
2025-04-05  1:57                 ` T.J. Mercier
2025-04-07 11:46                   ` Christian König
2025-04-08  1:03                     ` T.J. Mercier
2025-04-03  8:27 ` Simona Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250310-dmem-cgroups-v1-12-2984c1bc9312@kernel.org \
    --to=mripard@kernel.org \
    --cc=Brian.Starkey@arm.com \
    --cc=airlied@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=benjamin.gaignard@collabora.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hverkuil@xs4all.nl \
    --cc=iommu@lists.linux.dev \
    --cc=jstultz@google.com \
    --cc=laurent.pinchart+renesas@ideasonboard.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=m.szyprowski@samsung.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mchehab@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=simona@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=tfiga@chromium.org \
    --cc=tjmercier@google.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox