From patchwork Wed Oct 23 07:53:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846622 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E5A2D2E01E for ; Wed, 23 Oct 2024 07:53:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DE656B0095; Wed, 23 Oct 2024 03:53:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 23E456B0096; Wed, 23 Oct 2024 03:53:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3AFE6B0098; Wed, 23 Oct 2024 03:53:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B06076B0095 for ; Wed, 23 Oct 2024 03:53:11 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2BC001407B9 for ; Wed, 23 Oct 2024 07:52:53 +0000 (UTC) X-FDA: 82704100542.01.083B5A3 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 5F039A002A for ; Wed, 23 Oct 2024 07:52:52 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf15.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729669839; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T57t+6asUgxWWAkNrJBBxSlO1uc6P6NPZPVRIUe5yGA=; b=bE4s8EuVkGCupVpPZhT8/0rXCIyAThiQV3oEKmgCbmjTTXhx2gpHpWQ34LoQtbfa3d6EYJ 3C7Ag+8jnoZE6cIaua2FrKOXDWM0gQezBXKCHukSvhJoqHuKCPCiNdd6736NKKtTfgIt/5 EUqoes9J/dnstHVyjYv192EgPUA2BRA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669839; a=rsa-sha256; cv=none; b=sOWjPTBs32VoLbSZ/++gHNVfuD6diENAzjKiHFHSJBV8KO9NKsyYay5M0xoetfPH7tsUIl iRF+whGZVj/Y9sTu1zN0lDpTDZn8Xp6nxYlzQ2fVO1pmhAE0gm675ZlV8neTnFz+sttWes 6lgno6ttgO2uwq/2m3SZq8b8PnyDW5I= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf15.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl From: Maarten Lankhorst To: intel-xe@lists.freedesktop.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, Tejun Heo , Zefan Li , Johannes Weiner , Andrew Morton Cc: Friedrich Vock , cgroups@vger.kernel.org, linux-mm@kvack.org, Maxime Ripard , Maarten Lankhorst Subject: [PATCH 7/7] [DISCUSSION] drm/gem: Add cgroup memory accounting Date: Wed, 23 Oct 2024 09:53:00 +0200 Message-ID: <20241023075302.27194-8-maarten.lankhorst@linux.intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241023075302.27194-1-maarten.lankhorst@linux.intel.com> References: <20241023075302.27194-1-maarten.lankhorst@linux.intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5F039A002A X-Stat-Signature: 3cri7a1tcqp86ii41netkpnih6zm5ykc X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1729669972-931046 X-HE-Meta: U2FsdGVkX19RB4fOKbINgEqQYnHKs1UCPsbncC9gPEgQjejN4rbb3l7wj2d8sFR/UrfQUaUBHCym4MCsOCsKDk38eE7tOIn4ByXiovvh/7CoDDRRlKGGV+irQb9cjiH3OmEJwifHHL9Dqvc5u3Rd3VBWk1jpuRXnuFZddsxRWZ/vcXPiHREOrEgkPw79RB6dz2unWcBgDSp4lFIwnlkFzo7FTOn0MD+IyR+kh2EyIUUotkff2gagiQFZ6/5grGptJRj5t+8u1cYG0/ketH12BleHgCRQLbxh8q+JW6C7di7WGyL/PSasYygNc+fL/UKpv/wd3Yj1CfVcB657hKWhUvbSUeI9Hcx9htp2/D4vYOEI2XJh71q9lN1e/iAXN8jRHOUjpZS0QbWT6xlQNH6FE6LtVFzE/0zQL1Zj67pdNCR/Cc+2spVxYMzH2ei2GPMElUd1Wsh4W7pMy/DHr+LY7yvZgK9t1bH4uWutlkYet1eX1IUJs9xSz+FNuMaEKBJGVL/kEbUPkpe9suJDPirKvPOPCWDY6j+Yz+JUR4hsmRdsim3guwcJ0Nsnzf2bQ3QeBb+a2o+uFGtkjHB/T/zEf6opHx5n53IoDEGFOVZhyBkvg6SEC+NbrGRW7/KBRiJSr2TdRnXlakBcrRJwr0eqxMH/lW+bgHKXG855xWVzu7FflfJQAijqGlSY4MZJ4+Zukt6Ecwg9uOC9jMF4aS0SO1GwUPLvBoOlJ6U9Ck8jDwMN85PRs0XqPPy7Gt97ZxLSb0I/6Aky2kiarDLzPf9iaTITDXqidF7F/l/sUHBa4ztPf1d8Zi5GPoSmP3vzWtn7WLp4pnJVNei9rKnnkkhOrXOzmYYuveVFtOcCU0NeypM8ob/TK5/ypJR0qmAhcO9UCzTtc4M+FuSn1InMkapAffiPf7QSE5eGu7A0P7r6U3rBUyULse+yMBfKD86M8bEVg4X+DEmFnF99ApeJFqN UW0LnRc1 zZnlHzf0sAhg2PaXQWA+rL/KnKE8VEWFh0ubbOpXWfQB6FyrxlxSAHcD11KZ9LkHbhVw7xP/B4bZlKsuWiD/D300qLYVCBp1gnrMVBaVMb30NNraAV/gnXmVEnB7eog0SZg1krHE65+9ZhjVeTopZDlnPqf6QfxQdzAzzMM0/BfqsNos2xkDmpttLInfSdg5kClPhMe77/oWjYIKjt52GopvgO3ezu+ThNgqKfd+dkH4EiD4GXnggTSSo2HtVsiV99yY2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Maxime Ripard In order to support any device using the GEM support, let's register a dev cgroup device in the drm_dev_register path, and account for allocated buffers in the buffer allocation path. Marked discussion by Maarten Lankhorst: This is only implemented for drm_gem_dma_helper.c, and breaks the other drivers. It's still here for discussion, as we need to figure out how to make something like this work for both TTM and GEM drivers. Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/drm_drv.c | 11 ++++++++++- drivers/gpu/drm/drm_gem.c | 4 ++++ drivers/gpu/drm/drm_gem_dma_helper.c | 4 ++++ include/drm/drm_device.h | 4 ++++ include/drm/drm_gem.h | 2 ++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 251d1d69b09c5..6b1cffd1f52c6 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -930,6 +930,12 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags) if (drm_dev_needs_global_mutex(dev)) mutex_lock(&drm_global_mutex); + ret = drmm_cgroup_register_device(dev, &dev->cg); + if (ret) { + DRM_ERROR("Cannot create cgroup device.\n"); + goto out_unlock; + } + if (drm_core_check_feature(dev, DRIVER_COMPUTE_ACCEL)) accel_debugfs_register(dev); else @@ -937,7 +943,7 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags) ret = drm_minor_register(dev, DRM_MINOR_RENDER); if (ret) - goto err_minors; + goto out_unregister_cgroup; ret = drm_minor_register(dev, DRM_MINOR_PRIMARY); if (ret) @@ -982,6 +988,8 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags) drm_minor_unregister(dev, DRM_MINOR_ACCEL); drm_minor_unregister(dev, DRM_MINOR_PRIMARY); drm_minor_unregister(dev, DRM_MINOR_RENDER); +out_unregister_cgroup: + dev_cgroup_unregister_device(&dev->cg); out_unlock: if (drm_dev_needs_global_mutex(dev)) mutex_unlock(&drm_global_mutex); @@ -1024,6 +1032,7 @@ void drm_dev_unregister(struct drm_device *dev) drm_minor_unregister(dev, DRM_MINOR_PRIMARY); drm_minor_unregister(dev, DRM_MINOR_RENDER); drm_debugfs_dev_fini(dev); + dev_cgroup_unregister_device(&dev->cg); } EXPORT_SYMBOL(drm_dev_unregister); diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c index ee811764c3df4..95af268d9dd9b 100644 --- a/drivers/gpu/drm/drm_gem.c +++ b/drivers/gpu/drm/drm_gem.c @@ -110,6 +110,10 @@ drm_gem_init(struct drm_device *dev) DRM_FILE_PAGE_OFFSET_START, DRM_FILE_PAGE_OFFSET_SIZE); + dev->cg.regions[0].size = U64_MAX; + dev->cg.regions[0].name = "gem"; + dev->cg.num_regions++; + return drmm_add_action(dev, drm_gem_init_release, NULL); } diff --git a/drivers/gpu/drm/drm_gem_dma_helper.c b/drivers/gpu/drm/drm_gem_dma_helper.c index 870b90b78bc4e..32eafbf4aa814 100644 --- a/drivers/gpu/drm/drm_gem_dma_helper.c +++ b/drivers/gpu/drm/drm_gem_dma_helper.c @@ -106,6 +106,10 @@ __drm_gem_dma_create(struct drm_device *drm, size_t size, bool private) goto error; } + ret = dev_cgroup_try_charge(&drm->cg, 0, size, &dma_obj->base.cgroup_pool_state, NULL); + if (ret) + goto error; + return dma_obj; error: diff --git a/include/drm/drm_device.h b/include/drm/drm_device.h index c91f87b5242d7..bdb7656e7db67 100644 --- a/include/drm/drm_device.h +++ b/include/drm/drm_device.h @@ -1,6 +1,7 @@ #ifndef _DRM_DEVICE_H_ #define _DRM_DEVICE_H_ +#include #include #include #include @@ -317,6 +318,9 @@ struct drm_device { * Root directory for debugfs files. */ struct dentry *debugfs_root; + + /** @cg: device cgroup bookkeeping */ + struct dev_cgroup_device cg; }; #endif diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 5b8b1b059d32c..cc998abea7924 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -430,6 +430,8 @@ struct drm_gem_object { * The current LRU list that the GEM object is on. */ struct drm_gem_lru *lru; + + struct dev_cgroup_pool_state *cgroup_pool_state; }; /**