From patchwork Wed Oct 23 07:52:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846618 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 4D74DD2E021 for ; Wed, 23 Oct 2024 07:53:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 130FF6B0083; Wed, 23 Oct 2024 03:53:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EE8136B0085; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC5546B0088; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 974F16B0085 for ; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C04FF407BB for ; Wed, 23 Oct 2024 07:52:57 +0000 (UTC) X-FDA: 82704100668.21.40CE140 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf24.hostedemail.com (Postfix) with ESMTP id C33AD18000C for ; Wed, 23 Oct 2024 07:53:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf24.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=1729669818; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EduvZG4ASAALJDJj71o7lEj/a56jFH1aiDrBl1tLHgs=; b=uwROtP+by662Oi4Zs+T0cqJEaIR2fkilXNNb7+HJ99rY4wz1lLEoWq5Zz/CuiJ9eh9+Mmi EUH8ED9X853d7UjYxzS58hg/Atfs33Og4S7mWABLE8K+KrGai5rL7RqwwdOI8YCHlVeUyp J7NMI4VDJvFU6wf4qkLmAtArdZce+4M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669818; a=rsa-sha256; cv=none; b=7VJbVLr4/6v4aMPCHir6qoTKzt1PRaiTMnkYvNViWG3qYnuKQyABmtPGOhAe5Pg4JAtrTF urK3Nl4rjFCoJZx1G28m5gNi1hDOl9PXPvr/rJZjzXDIg8Dm3X7w78mYx59lNVbqovQ8DR bwGnP3x/q263AhHOXuGBx46jCAXQx0Q= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf24.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 1/7] kernel/cgroup: Add "dev" memory accounting cgroup Date: Wed, 23 Oct 2024 09:52:54 +0200 Message-ID: <20241023075302.27194-2-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-Server: rspam07 X-Rspamd-Queue-Id: C33AD18000C X-Stat-Signature: zjad71mz8q46htfjgsphaf6aufpojpjc X-Rspam-User: X-HE-Tag: 1729669982-561508 X-HE-Meta: U2FsdGVkX1/UFMGHZMJlc5AUTf2WHL0OVeqCkSPqNKrNqr/W5jjw2xpf6DnCc4Tf8vy0mivxD6zcIp4vxtvJKKAiic2rlb9MLtkOLCO4/NZiBLzjvFFgMOlTgzZQ/Y/SbhnQduKeDnaPgOCNZ+guZoTCs6BJKkM44L9vMfOYRmmjfN3BqsZgEgacsmiQ38J080fBI/Ok3+JOAetG94q/pYLndSzRzUKVnUSRgdYRiKxnkLOWUPNI/B0SEgKNYv9l1mB7XP+ydPVsyJZMUIncSwhmx6PsQlKn2P3qPWoR2Puncc47w+uaHDvNJyaLC3FznS4dVE+B3bCuajqWzwnIbI0jLIIaVTQzjVr87mD4V4BpKlKZL4kfCWJ4S11zOVuN0DI6KYqe6bYOVi5b34fpsMBDvZg/chIGsGSTFx6cE5HkKLDjY0wCLWR9q4A+NDgFibVactWxBZotughGarx+pBbIeYEETL8DKwYFC32L8evfmueksxu42GveO32K9ytUHvNWfMH9lgwQEtIo0rK3HmIcUg7k+aMG2IcRkFuPUgRjK7pB+/iBwzQkH4nm+DOiygdnrNG8lqJSLiaNac+Ni03uTQGNIGnEOCzXC2qrgM4efFIbbOguV0koRErO38BGbAangpzFmtANgW7agZv7auidJ6LIAc1xI2BzqwNwbX8fussStFhgwlQtQXjOOxXXJGWcFbGt4FZkj8qxPnhWSZd9gCf6tHx10aoLEeQNv+VfeOwFAupgZebOo4ORCdY9BDD8SGSRsGyeAGorghNhkxht/mhE2p05jcKh3N3jBnyrAj9QWreLfjdxAT1kpRCoapJRdcJkwjOOuAUlKMBqe0gIV1MJ3lN9thmdC15uNp52UXUNr8Hy8B6ckETsr0ZvS3k9du9YTOUrUw2ieFAXPVxz6D/kL/2LZYRVCDULOIZQeoaUc63KEYBRZPdNBFWjsmhaZuudpzYGaeptNyy gaxKKU7S kSfMNhHyWniiVLZSfGpB88sdbZapfZ8xM2IemABwjdiVmF+ijObQrXHtRi7nHJgB7pak1zvpgUNqCkYHeWLgg22MAq55ZTSk6q2M2Gug4/rsymxEF0cbPtzNd0ONBG6VZJVF3XxOx/ms3bySOlo/IHtglW32Y2drPZm8NVQCOieRRnGp7fqndQowb/NHE86r9LHfs96E4YhEbK7Upmq+tfy47td+9+a/IbdD5Fstxj/f409RL/o0qfGDbxxFem6MSIN/+utgy/7H2NCzMlD2aUT4oqw2nvOEr+5gsDZNFjykNmBCVgKJJyyRegAMcbah17Exe 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: The initial version was based roughly on the rdma and misc cgroup controllers, with a lot of the accounting code borrowed from rdma. The current version is a complete rewrite with page counter; it uses the same min/low/max semantics as the memory cgroup as a result. There's a small mismatch as TTM uses u64, and page_counter long pages. In practice it's not a problem. 32-bits systems don't really come with >=4GB cards and as long as we're consistently wrong with units, it's fine. The device page size may not be in the same units as kernel page size, and each region might also have a different page size (VRAM vs GART for example). The interface is simple: - populate dev_cgroup_try_charge->regions[..] name and size for each active region, set num_regions accordingly. - Call (dev,drmm)_cgroup_register_device() - Use dev_cgroup_try_charge to check if you can allocate a chunk of memory, use dev_cgroup__uncharge when freeing it. This may return an error code, or -EAGAIN when the cgroup limit is reached. In that case a reference to the limiting pool is returned. - The limiting cs can be used as compare function for dev_cgroup_state_evict_valuable. - After having evicted enough, drop reference to limiting cs with dev_cgroup_pool_state_put. This API allows you to limit device resources with cgroups. You can see the supported cards in /sys/fs/cgroup/dev.region.capacity You need to echo +dev to cgroup.subtree_control, and then you can partition memory. Co-developed-by: Friedrich Vock Signed-off-by: Friedrich Vock Co-developed-by: Maxime Ripard Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- Documentation/admin-guide/cgroup-v2.rst | 51 ++ Documentation/core-api/cgroup.rst | 9 + Documentation/core-api/index.rst | 1 + Documentation/gpu/drm-compute.rst | 54 ++ include/linux/cgroup_dev.h | 91 +++ include/linux/cgroup_subsys.h | 4 + include/linux/page_counter.h | 2 +- init/Kconfig | 7 + kernel/cgroup/Makefile | 1 + kernel/cgroup/dev.c | 893 ++++++++++++++++++++++++ mm/page_counter.c | 4 +- 11 files changed, 1114 insertions(+), 3 deletions(-) create mode 100644 Documentation/core-api/cgroup.rst create mode 100644 Documentation/gpu/drm-compute.rst create mode 100644 include/linux/cgroup_dev.h create mode 100644 kernel/cgroup/dev.c diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 69af2173555fb..e8fe79244af9c 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -2612,6 +2612,57 @@ RDMA Interface Files mlx4_0 hca_handle=1 hca_object=20 ocrdma1 hca_handle=1 hca_object=23 +DEV +---- + +The "dev" controller regulates the distribution and accounting of +device resources, currently only memory regions. Because each memory +region may have its own page size, which does not have to be equal +to the system page size. the units are in bytes. + +DEV Interface Files +~~~~~~~~~~~~~~~~~~~~ + + dev.region.max, dev.region.min, dev.region.low + A readwrite nested-keyed file that exists for all the cgroups + except root that describes current configured resource limit + for a device. + + Lines are keyed by device name and are not ordered. + Each line contains space separated resource name and its configured + limit that can be distributed. + + The following nested keys are defined. + + ========== ======================================================= + * Maximum amount of bytes that allocatable in this region + ========== ======================================================= + + An example for xe follows:: + + drm/0000:03:00.0 vram0=1073741824 stolen=max + + The semantics are the same as for the memory cgroup controller, and are + calculated in the same way. + + dev.region.capacity + A read-only file that describes maximum region capacity. + It only exists on the root cgroup. Not all memory can be + allocated by cgroups, as the kernel reserves some for + internal use. + + An example for xe follows:: + + drm/0000:03:00.0 vram0=8514437120 stolen=67108864 + + dev.region.current + A read-only file that describes current resource usage. + It exists for all the cgroup except root. + + An example for xe follows:: + + drm/0000:03:00.0 vram0=12550144 stolen=8650752 + HugeTLB ------- diff --git a/Documentation/core-api/cgroup.rst b/Documentation/core-api/cgroup.rst new file mode 100644 index 0000000000000..475b32255bd68 --- /dev/null +++ b/Documentation/core-api/cgroup.rst @@ -0,0 +1,9 @@ +================== +Cgroup Kernel APIs +================== + +Device Cgroup API (devcg) +========================= +.. kernel-doc:: kernel/cgroup/dev.c + :export: + diff --git a/Documentation/core-api/index.rst b/Documentation/core-api/index.rst index 6a875743dd4b7..dbd6c4f9a6313 100644 --- a/Documentation/core-api/index.rst +++ b/Documentation/core-api/index.rst @@ -108,6 +108,7 @@ more memory-management documentation in Documentation/mm/index.rst. dma-isa-lpc swiotlb mm-api + cgroup genalloc pin_user_pages boot-time-mm diff --git a/Documentation/gpu/drm-compute.rst b/Documentation/gpu/drm-compute.rst new file mode 100644 index 0000000000000..116270976ef7a --- /dev/null +++ b/Documentation/gpu/drm-compute.rst @@ -0,0 +1,54 @@ +================================== +Long running workloads and compute +================================== + +Long running workloads (compute) are workloads that will not complete in 10 +seconds. (The time let the user wait before he reaches for the power button). +This means that other techniques need to be used to manage those workloads, +that cannot use fences. + +Some hardware may schedule compute jobs, and have no way to pre-empt them, or +have their memory swapped out from them. Or they simply want their workload +not to be preempted or swapped out at all. + +This means that it differs from what is described in driver-api/dma-buf.rst. + +As with normal compute jobs, dma-fence may not be used at all. In this case, +not even to force preemption. The driver with is simply forced to unmap a BO +from the long compute job's address space on unbind immediately, not even +waiting for the workload to complete. Effectively this terminates the workload +when there is no hardware support to recover. + +Since this is undesirable, there need to be mitigations to prevent a workload +from being terminated. There are several possible approach, all with their +advantages and drawbacks. + +The first approach you will likely try is to pin all buffers used by compute. +This guarantees that the job will run uninterrupted, but also allows a very +denial of service attack by pinning as much memory as possible, hogging the +all GPU memory, and possibly a huge chunk of CPU memory. + +A second approach that will work slightly better on its own is adding an option +not to evict when creating a new job (any kind). If all of userspace opts in +to this flag, it would prevent cooperating userspace from forced terminating +older compute jobs to start a new one. + +If job preemption and recoverable pagefaults are not available, those are the +only approaches possible. So even with those, you want a separate way of +controlling resources. The standard kernel way of doing so is cgroups. + +This creates a third option, using cgroups to prevent eviction. Both GPU and +driver-allocated CPU memory would be accounted to the correct cgroup, and +eviction would be made cgroup aware. This allows the GPU to be partitioned +into cgroups, that will allow jobs to run next to each other without +interference. + +The interface to the cgroup would be similar to the current CPU memory +interface, with similar semantics for min/low/high/max, if eviction can +be made cgroup aware. For now only max is implemented. + +What should be noted is that each memory region (tiled memory for example) +should have its own accounting, using $card key0 = value0 key1 = value1. + +The key is set to the regionid set by the driver, for example "tile0". +For the value of $card, we use drmGetUnique(). diff --git a/include/linux/cgroup_dev.h b/include/linux/cgroup_dev.h new file mode 100644 index 0000000000000..c6311d1d3ce48 --- /dev/null +++ b/include/linux/cgroup_dev.h @@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright © 2023 Intel Corporation + */ + +#ifndef _CGROUP_DEV_H +#define _CGROUP_DEV_H + +#include +#include + +struct dev_cgroup_pool_state; + +/* + * Use 8 as max, because of N^2 lookup when setting things, can be bumped if needed + * Identical to TTM_NUM_MEM_TYPES to allow simplifying that code. + */ +#define DEVICE_CGROUP_MAX_REGIONS 8 + +/* Public definition of cgroup device, should not be modified after _register() */ +struct dev_cgroup_device { + struct { + u64 size; + const char *name; + } regions[DEVICE_CGROUP_MAX_REGIONS]; + + int num_regions; + + /* used by cgroups, do not use */ + void *priv; +}; + +#if IS_ENABLED(CONFIG_CGROUP_DEV) +int dev_cgroup_register_device(struct dev_cgroup_device *cgdev, + const char *name); +void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev); +int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev, + u32 index, u64 size, + struct dev_cgroup_pool_state **ret_pool, + struct dev_cgroup_pool_state **ret_limit_pool); +void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool, + u32 index, u64 size); +bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index, + struct dev_cgroup_pool_state *limit_pool, + struct dev_cgroup_pool_state *test_pool, + bool ignore_low, bool *ret_hit_low); + +void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool); +#else +static inline int +dev_cgroup_register_device(struct dev_cgroup_device *cgdev, + const char *name) +{ + return 0; +} + +static inline void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev) +{ +} + +static int int dev_cgroup_try_charge(struct dev_cgroup_device *cgdev, + u32 index, u64 size, + struct dev_cgroup_pool_state **ret_pool, + struct dev_cgroup_pool_state **ret_limit_pool); +{ + *ret_pool = NULL; + + if (ret_limit_pool) + *ret_limit_pool = NULL; + + return 0; +} + +static inline void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool, + u32 index, u64 size) +{ } + +static inline +bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index, + struct dev_cgroup_pool_state *limit_pool, + struct dev_cgroup_pool_state *test_pool, + bool ignore_low, bool *ret_hit_low) +{ + return true; +} + +static inline void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool) +{ } + +#endif +#endif /* _CGROUP_DEV_H */ diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h index 4452354872307..898340cfe5843 100644 --- a/include/linux/cgroup_subsys.h +++ b/include/linux/cgroup_subsys.h @@ -65,6 +65,10 @@ SUBSYS(rdma) SUBSYS(misc) #endif +#if IS_ENABLED(CONFIG_CGROUP_DEV) +SUBSYS(dev) +#endif + /* * The following subsystems are not supported on the default hierarchy. */ diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h index 79dbd8bc35a72..d75376a1694ee 100644 --- a/include/linux/page_counter.h +++ b/include/linux/page_counter.h @@ -96,7 +96,7 @@ static inline void page_counter_reset_watermark(struct page_counter *counter) counter->watermark = usage; } -#ifdef CONFIG_MEMCG +#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE) void page_counter_calculate_protection(struct page_counter *root, struct page_counter *counter, bool recursive_protection); diff --git a/init/Kconfig b/init/Kconfig index 530a382ee0feb..2da595facd97f 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1123,6 +1123,13 @@ config CGROUP_RDMA Attaching processes with active RDMA resources to the cgroup hierarchy is allowed even if can cross the hierarchy's limit. +config CGROUP_DEV + bool "Device controller" + help + Provides the device subsystem controller. + + ... + config CGROUP_FREEZER bool "Freezer controller" help diff --git a/kernel/cgroup/Makefile b/kernel/cgroup/Makefile index a5c9359d516f8..441d346fdc51f 100644 --- a/kernel/cgroup/Makefile +++ b/kernel/cgroup/Makefile @@ -7,4 +7,5 @@ obj-$(CONFIG_CGROUP_RDMA) += rdma.o obj-$(CONFIG_CPUSETS) += cpuset.o obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o obj-$(CONFIG_CGROUP_MISC) += misc.o +obj-$(CONFIG_CGROUP_DEV) += dev.o obj-$(CONFIG_CGROUP_DEBUG) += debug.o diff --git a/kernel/cgroup/dev.c b/kernel/cgroup/dev.c new file mode 100644 index 0000000000000..e422ccbfbc444 --- /dev/null +++ b/kernel/cgroup/dev.c @@ -0,0 +1,893 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2023-2024 Intel Corporation (Maarten Lankhorst ) + * Copyright 2024 Red Hat (Maxime Ripard ) + * Partially based on the rdma and misc controllers, which bear the following copyrights: + * + * Copyright 2020 Google LLC + * Copyright (C) 2016 Parav Pandit + */ + +#include +#include +#include +#include +#include +#include +#include + +struct devcg_device { + /** + * @ref: References keeping the device alive. + * Keeps the device reference alive after a succesful RCU lookup. + */ + struct kref ref; + + /** @rcu: RCU head for freeing */ + struct rcu_head rcu; + + /** + * @dev_node: Linked into &devcg_devices list. + * Protected by RCU and global spinlock. + */ + struct list_head dev_node; + + /** + * @pools: List of pools linked to this device. + * Protected by global spinlock only + */ + struct list_head pools; + + /** + * @base: Copy of the struct passed on register. + * A copy is made to prevent lifetime issues. devcg_device may + * be kept alive when changing cgroups values concurrently through + * rcu lookups. + */ + struct dev_cgroup_device base; + + /** @name: Name describing the node, set by dev_cgroup_register_device */ + const char *name; + + /** + * @unregistered: Whether the device is unregistered by its caller. + * No new pools should be added to the device afterwards. + */ + bool unregistered; +}; + +struct devcg_state { + struct cgroup_subsys_state css; + + struct list_head pools; +}; + +struct dev_cgroup_pool_state { + struct devcg_device *device; + struct devcg_state *cs; + + /* css node, RCU protected against device teardown */ + struct list_head css_node; + + /* dev node, no RCU protection required */ + struct list_head dev_node; + + int num_res, inited; + struct rcu_head rcu; + + struct devcg_pool_res { + struct page_counter cnt; + } resources[]; +}; + +/* + * 3 operations require locking protection: + * - Registering and unregistering device to/from list, requires global lock. + * - Adding a dev_cgroup_pool_state to a CSS, removing when CSS is freed. + * - Adding a dev_cgroup_pool_state to a device list. + * + * Since for the most common operations RCU provides enough protection, I + * do not think more granular locking makes sense. Most protection is offered + * by RCU and the lockless operating page_counter. + */ +static DEFINE_SPINLOCK(devcg_lock); +static LIST_HEAD(devcg_devices); + +static inline struct devcg_state * +css_to_devcs(struct cgroup_subsys_state *css) +{ + return container_of(css, struct devcg_state, css); +} + +static inline struct devcg_state *get_current_devcs(void) +{ + return css_to_devcs(task_get_css(current, dev_cgrp_id)); +} + +static struct devcg_state *parent_devcs(struct devcg_state *cg) +{ + return cg->css.parent ? css_to_devcs(cg->css.parent) : NULL; +} + +static void free_cg_pool(struct dev_cgroup_pool_state *pool) +{ + list_del(&pool->dev_node); + kfree(pool); +} + +static void +set_resource_min(struct dev_cgroup_pool_state *pool, int i, u64 val) +{ + page_counter_set_min(&pool->resources[i].cnt, val); +} + +static void +set_resource_low(struct dev_cgroup_pool_state *pool, int i, u64 val) +{ + page_counter_set_low(&pool->resources[i].cnt, val); +} + +static void +set_resource_max(struct dev_cgroup_pool_state *pool, int i, u64 val) +{ + page_counter_set_max(&pool->resources[i].cnt, val); +} + +static u64 get_resource_low(struct dev_cgroup_pool_state *pool, int idx) +{ + return pool ? READ_ONCE(pool->resources[idx].cnt.low) : 0; +} + +static u64 get_resource_min(struct dev_cgroup_pool_state *pool, int idx) +{ + return pool ? READ_ONCE(pool->resources[idx].cnt.min) : 0; +} + +static u64 get_resource_max(struct dev_cgroup_pool_state *pool, int idx) +{ + return pool ? READ_ONCE(pool->resources[idx].cnt.max) : PAGE_COUNTER_MAX; +} + +static u64 get_resource_current(struct dev_cgroup_pool_state *pool, int idx) +{ + return pool ? page_counter_read(&pool->resources[idx].cnt) : 0; +} + +static void reset_all_resource_limits(struct dev_cgroup_pool_state *rpool) +{ + int i; + + for (i = 0; i < rpool->num_res; i++) { + set_resource_min(rpool, i, 0); + set_resource_low(rpool, i, 0); + set_resource_max(rpool, i, PAGE_COUNTER_MAX); + } +} + +static void devcs_offline(struct cgroup_subsys_state *css) +{ + struct devcg_state *devcs = css_to_devcs(css); + struct dev_cgroup_pool_state *pool; + + rcu_read_lock(); + list_for_each_entry_rcu(pool, &devcs->pools, css_node) + reset_all_resource_limits(pool); + rcu_read_unlock(); +} + +static void devcs_free(struct cgroup_subsys_state *css) +{ + struct devcg_state *devcs = css_to_devcs(css); + struct dev_cgroup_pool_state *pool, *next; + + spin_lock(&devcg_lock); + list_for_each_entry_safe(pool, next, &devcs->pools, css_node) { + /* + *The pool is dead and all references are 0, + * no need for RCU protection with list_del_rcu or freeing. + */ + list_del(&pool->css_node); + free_cg_pool(pool); + } + spin_unlock(&devcg_lock); + + kfree(devcs); +} + +static struct cgroup_subsys_state * +devcs_alloc(struct cgroup_subsys_state *parent_css) +{ + struct devcg_state *devcs = kzalloc(sizeof(*devcs), GFP_KERNEL); + if (!devcs) + return ERR_PTR(-ENOMEM); + + INIT_LIST_HEAD(&devcs->pools); + return &devcs->css; +} + +static struct dev_cgroup_pool_state * +find_cg_pool_locked(struct devcg_state *devcs, struct devcg_device *dev) +{ + struct dev_cgroup_pool_state *pool; + + list_for_each_entry_rcu(pool, &devcs->pools, css_node, spin_is_locked(&devcg_lock)) + if (pool->device == dev) + return pool; + + return NULL; +} + +static struct dev_cgroup_pool_state *pool_parent(struct dev_cgroup_pool_state *pool) +{ + if (!pool->resources[0].cnt.parent) + return NULL; + + return container_of(pool->resources[0].cnt.parent, typeof(*pool), resources[0].cnt); +} + +/** + * dev_cgroup_state_evict_valuable() - Check if we should evict from test_pool + * @dev: &dev_cgroup_device + * @index: The index number of the region being tested. + * @limit_pool: The pool for which we hit limits + * @test_pool: The pool for which to test + * @ignore_low: Whether we have to respect low watermarks. + * @ret_hit_low: Pointer to whether it makes sense to consider low watermark. + * + * This function returns true if we can evict from @test_pool, false if not. + * When returning false and @ignore_low is false, @ret_hit_low may + * be set to true to indicate this function can be retried with @ignore_low + * set to true. + * + * Return: bool + */ +bool dev_cgroup_state_evict_valuable(struct dev_cgroup_device *dev, int index, + struct dev_cgroup_pool_state *limit_pool, + struct dev_cgroup_pool_state *test_pool, + bool ignore_low, bool *ret_hit_low) +{ + struct dev_cgroup_pool_state *pool = test_pool; + struct page_counter *climit, *ctest; + u64 used, min, low; + + /* Can always evict from current pool, despite limits */ + if (limit_pool == test_pool) + return true; + + if (limit_pool) { + if (!parent_devcs(limit_pool->cs)) + return true; + + for (pool = test_pool; pool && limit_pool != pool; pool = pool_parent(pool)) + {} + + if (!pool) + return false; + } else { + /* + * If there is no cgroup limiting memory usage, use the root + * cgroup instead for limit calculations. + */ + for (limit_pool = test_pool; pool_parent(limit_pool); limit_pool = pool_parent(limit_pool)) + {} + } + + climit = &limit_pool->resources[index].cnt; + ctest = &test_pool->resources[index].cnt; + + page_counter_calculate_protection(climit, ctest, true); + + used = page_counter_read(ctest); + min = READ_ONCE(ctest->emin); + + if (used <= min) + return false; + + if (!ignore_low) { + low = READ_ONCE(ctest->elow); + if (used > low) + return true; + + *ret_hit_low = true; + return false; + } + return true; +} +EXPORT_SYMBOL_GPL(dev_cgroup_state_evict_valuable); + +static struct dev_cgroup_pool_state * +alloc_pool_single(struct devcg_state *devcs, struct devcg_device *dev, + struct dev_cgroup_pool_state **allocpool) +{ + struct devcg_state *parent = parent_devcs(devcs); + struct dev_cgroup_pool_state *pool, *ppool = NULL; + int i; + + if (!*allocpool) { + pool = kzalloc(offsetof(struct dev_cgroup_pool_state, resources[dev->base.num_regions]), GFP_NOWAIT); + if (!pool) + return ERR_PTR(-ENOMEM); + } else { + pool = *allocpool; + *allocpool = NULL; + } + + pool->device = dev; + pool->num_res = dev->base.num_regions; + pool->cs = devcs; + + if (parent) + ppool = find_cg_pool_locked(parent, dev); + + for (i = 0; i < pool->num_res; i++) + page_counter_init(&pool->resources[i].cnt, + ppool ? &ppool->resources[i].cnt : NULL, true); + reset_all_resource_limits(pool); + + list_add_tail_rcu(&pool->css_node, &devcs->pools); + list_add_tail(&pool->dev_node, &dev->pools); + + if (!parent) + pool->inited = true; + else + pool->inited = ppool ? ppool->inited : false; + return pool; +} + +static struct dev_cgroup_pool_state * +get_cg_pool_locked(struct devcg_state *devcs, struct devcg_device *dev, + struct dev_cgroup_pool_state **allocpool) +{ + struct dev_cgroup_pool_state *pool, *ppool, *retpool; + struct devcg_state *p, *pp; + int i; + + /* + * Recursively create pool, we may not initialize yet on + * recursion, this is done as a separate step. + */ + for (p = devcs; p; p = parent_devcs(p)) { + pool = find_cg_pool_locked(p, dev); + if (!pool) + pool = alloc_pool_single(p, dev, allocpool); + + if (IS_ERR(pool)) + return pool; + + if (p == devcs && pool->inited) + return pool; + + if (pool->inited) + break; + } + + retpool = pool = find_cg_pool_locked(devcs, dev); + for (p = devcs, pp = parent_devcs(devcs); pp; p = pp, pp = parent_devcs(p)) { + if (pool->inited) + break; + + /* ppool was created if it didn't exist by above loop. */ + ppool = find_cg_pool_locked(pp, dev); + + /* Fix up parent links, mark as inited. */ + for (i = 0; i < pool->num_res; i++) + pool->resources[i].cnt.parent = &ppool->resources[i].cnt; + pool->inited = true; + + pool = ppool; + } + + return retpool; +} + +static void devcg_free_rcu(struct rcu_head *rcu) +{ + struct devcg_device *dev = container_of(rcu, typeof(*dev), rcu); + struct dev_cgroup_pool_state *pool, *next; + + list_for_each_entry_safe(pool, next, &dev->pools, dev_node) + free_cg_pool(pool); + kfree(dev->name); + kfree(dev); +} + +static void devcg_free_device(struct kref *ref) +{ + struct devcg_device *cgdev = container_of(ref, typeof(*cgdev), ref); + + call_rcu(&cgdev->rcu, devcg_free_rcu); +} + +/** + * dev_cgroup_unregister_device() - Unregister a previously registered device. + * @cgdev: The device to unregister. + * + * This function undoes dev_cgroup_register_device. + */ +void dev_cgroup_unregister_device(struct dev_cgroup_device *cgdev) +{ + struct devcg_device *dev; + struct list_head *entry; + + if (!cgdev || !cgdev->priv) + return; + + dev = cgdev->priv; + cgdev->priv = NULL; + + spin_lock(&devcg_lock); + + /* Remove from global device list */ + list_del_rcu(&dev->dev_node); + + list_for_each_rcu(entry, &dev->pools) { + struct dev_cgroup_pool_state *pool = + container_of(entry, typeof(*pool), dev_node); + + list_del_rcu(&pool->css_node); + } + + /* + * Ensure any RCU based lookups fail. Additionally, + * no new pools should be added to the dead device + * by get_cg_pool_unlocked. + */ + dev->unregistered = true; + spin_unlock(&devcg_lock); + + kref_put(&dev->ref, devcg_free_device); +} +EXPORT_SYMBOL_GPL(dev_cgroup_unregister_device); + +/** + * dev_cgroup_register_device() - Register a devices for dev cgroup. + * @cgdev: A filled &dev_cgroup_device structure. + * @name: A name for the device. + * + * This function registers a node in the dev cgroup with the + * name given. After calling this function, the device can be + * used for allocations. + * + * Return: 0 on success, -ERRNO on failure. + */ +int dev_cgroup_register_device(struct dev_cgroup_device *cgdev, + const char *name) +{ + struct devcg_device *dev; + char *dev_name; + + cgdev->priv = NULL; + if (!cgdev->num_regions) + return 0; + + cgdev->priv = dev = kzalloc(sizeof (*dev), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + dev_name = kstrdup(name, GFP_KERNEL); + if (!dev_name) { + kfree(dev); + cgdev->priv = NULL; + return -ENOMEM; + } + + INIT_LIST_HEAD(&dev->pools); + dev->name = dev_name; + dev->base = *cgdev; + kref_init(&dev->ref); + + spin_lock(&devcg_lock); + list_add_tail_rcu(&dev->dev_node, &devcg_devices); + spin_unlock(&devcg_lock); + + return 0; +} +EXPORT_SYMBOL_GPL(dev_cgroup_register_device); + +static struct devcg_device *devcg_get_device_by_name(const char *name) +{ + struct devcg_device *dev; + + list_for_each_entry_rcu(dev, &devcg_devices, dev_node, spin_is_locked(&devcg_lock)) + if (!strcmp(name, dev->name) && + kref_get_unless_zero(&dev->ref)) + return dev; + + return NULL; +} + +/** + * dev_cgroup_pool_state_put() - Drop a reference to a dev_cgroup_pool_state + * @pool: &dev_cgroup_pool_state + * + * Called to drop a reference to the limiting pool returned by + * dev_cgroup_try_charge(). + */ +void dev_cgroup_pool_state_put(struct dev_cgroup_pool_state *pool) +{ + if (pool) + css_put(&pool->cs->css); +} +EXPORT_SYMBOL_GPL(dev_cgroup_pool_state_put); + +static struct dev_cgroup_pool_state * +get_cg_pool_unlocked(struct devcg_state *cg, struct devcg_device *dev) +{ + struct dev_cgroup_pool_state *pool, *allocpool = NULL; + + /* fastpath lookup? */ + rcu_read_lock(); + pool = find_cg_pool_locked(cg, dev); + if (pool && !READ_ONCE(pool->inited)) + pool = NULL; + rcu_read_unlock(); + + while (!pool) { + spin_lock(&devcg_lock); + if (!dev->unregistered) + pool = get_cg_pool_locked(cg, dev, &allocpool); + else + pool = ERR_PTR(-ENODEV); + spin_unlock(&devcg_lock); + + if (pool == ERR_PTR(-ENOMEM)) { + pool = NULL; + if (WARN_ON(allocpool)) + continue; + + allocpool = kzalloc(offsetof(struct dev_cgroup_pool_state, resources[dev->base.num_regions]), GFP_KERNEL); + if (allocpool) { + pool = NULL; + continue; + } + } + } + + kfree(allocpool); + return pool; +} + +/** + * dev_cgroup_uncharge() - Uncharge a pool. + * @pool: Pool to uncharge. + * @index: Index of region to uncharge. + * @size: Size to uncharge. + * + * Undoes the effects of dev_cgroup_try_charge. + * Must be called with the returned pool as argument, + * and same @index and @size. + */ +void dev_cgroup_uncharge(struct dev_cgroup_pool_state *pool, + u32 index, u64 size) +{ + struct dev_cgroup_device *cgdev; + + if (!pool) + return; + + cgdev = &pool->device->base; + if (index >= cgdev->num_regions) + return; + + page_counter_uncharge(&pool->resources[index].cnt, size); + css_put(&pool->cs->css); +} +EXPORT_SYMBOL_GPL(dev_cgroup_uncharge); + +/** + * dev_cgroup_try_charge() - Try charging a new allocation to a region. + * @dev: Device to charge + * @index: Index of the region to charge + * @size: Size (in bytes) to charge. + * @ret_pool: On succesfull allocation, the pool that is charged. + * @ret_limit_pool: On a failed allocation, the limiting pool. + * + * This function charges the current pool for @dev with region at @index for a + * size of @size bytes. + * + * If the function succeeds, @ret_pool is set, which must be passed to + * dev_cgroup_uncharge() when undoing the allocation. + * + * When this function fails with -EAGAIN and @ret_limit_pool is non-null, it + * will be set to the pool for which the limit is hit. This can be used for + * eviction as argument to dev_cgroup_evict_valuable(). This reference must be freed + * with @dev_cgroup_pool_state_put(). + * + * Return: 0 on success, -EAGAIN on hitting a limit, or a negative errno on failure. + */ +int dev_cgroup_try_charge(struct dev_cgroup_device *dev, + u32 index, u64 size, + struct dev_cgroup_pool_state **ret_pool, + struct dev_cgroup_pool_state **ret_limit_pool) +{ + struct devcg_device *cgdev = dev->priv; + struct devcg_state *cg; + struct dev_cgroup_pool_state *pool; + struct page_counter *fail; + int ret; + + *ret_pool = NULL; + if (ret_limit_pool) + *ret_limit_pool = NULL; + + /* Early init or device unregistered */ + if (!cgdev) + return 0; + + if (index >= cgdev->base.num_regions) + return -EINVAL; + + /* + * hold on to css, as cgroup can be removed but resource + * accounting happens on css. + */ + cg = get_current_devcs(); + + pool = get_cg_pool_unlocked(cg, cgdev); + if (IS_ERR(pool)) { + ret = PTR_ERR(pool); + goto err; + } + + if (!page_counter_try_charge(&pool->resources[index].cnt, size, &fail)) { + if (ret_limit_pool) { + *ret_limit_pool = container_of(fail, struct dev_cgroup_pool_state, resources[index].cnt); + css_get(&(*ret_limit_pool)->cs->css); + } + ret = -EAGAIN; + goto err; + } + + /* On success, reference is transferred to *ret_pool */ + *ret_pool = pool; + return 0; + +err: + css_put(&cg->css); + return ret; +} +EXPORT_SYMBOL_GPL(dev_cgroup_try_charge); + +static int devcg_region_capacity_show(struct seq_file *sf, void *v) +{ + struct devcg_device *dev; + int i; + + rcu_read_lock(); + list_for_each_entry_rcu(dev, &devcg_devices, dev_node) { + seq_puts(sf, dev->name); + for (i = 0; i < dev->base.num_regions; i++) + seq_printf(sf, " %s=%llu", + dev->base.regions[i].name, + dev->base.regions[i].size); + seq_putc(sf, '\n'); + } + rcu_read_unlock(); + return 0; +} + +static s64 parse_resource(char *c, char **retname) +{ + substring_t argstr; + char *name, *value = c; + size_t len; + int ret; + u64 retval; + + name = strsep(&value, "="); + if (!name || !value) + return -EINVAL; + + *retname = name; + len = strlen(value); + + argstr.from = value; + argstr.to = value + len; + + ret = match_u64(&argstr, &retval); + if (ret >= 0) { + if (retval > S64_MAX) + return -EINVAL; + if (retval > PAGE_COUNTER_MAX) + return PAGE_COUNTER_MAX; + return retval; + } + if (!strncmp(value, "max", len)) + return PAGE_COUNTER_MAX; + + /* Not u64 or max, error */ + return -EINVAL; +} + +static int devcg_parse_limits(char *options, struct devcg_device *dev, + u64 *new_limits, unsigned long *set_mask) +{ + char *c, *region; + + /* parse resource options */ + while ((c = strsep(&options, " \t")) != NULL) { + s64 limit; + int i; + + limit = parse_resource(c, ®ion); + if (limit < 0) + return limit; + + + for (i = 0; i < dev->base.num_regions; i++) + if (!strcmp(dev->base.regions[i].name, region)) + break; + + if (i == dev->base.num_regions) + return -EINVAL; + + new_limits[i] = limit; + *set_mask |= BIT(i); + } + return 0; +} + +static ssize_t devcg_limit_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off, + void (*apply)(struct dev_cgroup_pool_state *, int, u64)) +{ + struct devcg_state *devcs = css_to_devcs(of_css(of)); + int err = 0; + + while (buf && !err) { + struct dev_cgroup_pool_state *pool = NULL; + char *options, *dev_name; + unsigned long set_mask = 0; + struct devcg_device *dev; + u64 new_limits[DEVICE_CGROUP_MAX_REGIONS]; + int i; + + options = buf; + buf = strchr(buf, '\n'); + if (buf) + *buf++ = '\0'; + + options = strstrip(options); + + /* eat empty lines */ + if (!options[0]) + continue; + + dev_name = strsep(&options, " \t"); + if (!dev_name[0]) + continue; + + rcu_read_lock(); + dev = devcg_get_device_by_name(dev_name); + rcu_read_unlock(); + + if (!dev) + return -EINVAL; + + err = devcg_parse_limits(options, dev, new_limits, &set_mask); + if (err < 0) + goto out_put; + + pool = get_cg_pool_unlocked(devcs, dev); + if (IS_ERR(pool)) { + err = PTR_ERR(pool); + goto out_put; + } + + /* And commit */ + for_each_set_bit(i, &set_mask, DEVICE_CGROUP_MAX_REGIONS) + apply(pool, i, new_limits[i]); + +out_put: + kref_put(&dev->ref, devcg_free_device); + } + + + return err ?: nbytes; +} + +static int devcg_limit_show(struct seq_file *sf, void *v, + u64 (*fn)(struct dev_cgroup_pool_state *, int)) +{ + struct devcg_state *devcs = css_to_devcs(seq_css(sf)); + struct devcg_device *dev; + + rcu_read_lock(); + list_for_each_entry_rcu(dev, &devcg_devices, dev_node) { + struct dev_cgroup_pool_state *pool = find_cg_pool_locked(devcs, dev); + + seq_puts(sf, dev->name); + + for (int i = 0; i < dev->base.num_regions; i++) { + u64 val = fn(pool, i); + + if (val < PAGE_COUNTER_MAX) + seq_printf(sf, " %s=%lld", + dev->base.regions[i].name, val); + else + seq_printf(sf, " %s=max", dev->base.regions[i].name); + } + + seq_putc(sf, '\n'); + } + rcu_read_unlock(); + + return 0; +} + +static int devcg_region_current_show(struct seq_file *sf, void *v) +{ + return devcg_limit_show(sf, v, get_resource_current); +} + +static int devcg_region_min_show(struct seq_file *sf, void *v) +{ + return devcg_limit_show(sf, v, get_resource_min); +} + +static ssize_t devcg_region_min_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + return devcg_limit_write(of, buf, nbytes, off, set_resource_min); +} + +static int devcg_region_low_show(struct seq_file *sf, void *v) +{ + return devcg_limit_show(sf, v, get_resource_low); +} + +static ssize_t devcg_region_low_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + return devcg_limit_write(of, buf, nbytes, off, set_resource_low); +} + +static int devcg_region_max_show(struct seq_file *sf, void *v) +{ + return devcg_limit_show(sf, v, get_resource_max); +} + +static ssize_t devcg_region_max_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + return devcg_limit_write(of, buf, nbytes, off, set_resource_max); +} + +static struct cftype files[] = { + { + .name = "region.capacity", + .seq_show = devcg_region_capacity_show, + .flags = CFTYPE_ONLY_ON_ROOT, + }, + { + .name = "region.current", + .seq_show = devcg_region_current_show, + }, + { + .name = "region.min", + .write = devcg_region_min_write, + .seq_show = devcg_region_min_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "region.low", + .write = devcg_region_low_write, + .seq_show = devcg_region_low_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "region.max", + .write = devcg_region_max_write, + .seq_show = devcg_region_max_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { } /* Zero entry terminates. */ +}; + +struct cgroup_subsys dev_cgrp_subsys = { + .css_alloc = devcs_alloc, + .css_free = devcs_free, + .css_offline = devcs_offline, + .legacy_cftypes = files, + .dfl_cftypes = files, +}; diff --git a/mm/page_counter.c b/mm/page_counter.c index b249d15af9dd8..fbb0cb34415d7 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -288,7 +288,7 @@ int page_counter_memparse(const char *buf, const char *max, } -#ifdef CONFIG_MEMCG +#if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DEVICE) /* * This function calculates an individual page counter's effective * protection which is derived from its own memory.min/low, its @@ -460,4 +460,4 @@ void page_counter_calculate_protection(struct page_counter *root, atomic_long_read(&parent->children_low_usage), recursive_protection)); } -#endif /* CONFIG_MEMCG */ +#endif /* CONFIG_MEMCG || CONFIG_CGROUP_DEVICE */ From patchwork Wed Oct 23 07:52:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846615 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 1144DD2E01E for ; Wed, 23 Oct 2024 07:53:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 945606B0082; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CE8B6B0083; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 748036B0085; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 515D06B0082 for ; Wed, 23 Oct 2024 03:53:07 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9F827C05D8 for ; Wed, 23 Oct 2024 07:52:48 +0000 (UTC) X-FDA: 82704100794.05.D84EE2D Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 0F1C9160014 for ; Wed, 23 Oct 2024 07:52:53 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf08.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669862; a=rsa-sha256; cv=none; b=HCmiVzrsiSj+1REnjo1R3e7VdN2nSURwo1qi1u0jd2kpgBCDwYLTvn2tzgrOOF4vu/R5BP yDvfp1kfK1hsFaD6IrjZIsnUIR56ponNcn/46uhjrDnMs3n39GCxLFP+begoqKR69JRhqn AC2cV9GEnr67+kgHjNyGD/YJKX/Prl8= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf08.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=1729669862; 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=mMj4fisK6Cv90a37SqiKuP13+YHZVkmC64qpU/8DwVg=; b=8FDMJuX4p5wwCXfcM7eaRJXWn6vYZl/hW2jUh6U+q4T/9G9zZDbCNhCresyavlrERSoUFb 4ITKt1viCNXp19jf0dZMqnBwgXjQa07nEtoVVaWZVoEMiUZ1fPnYKQc96WVVmGCPgXtSan sZe04baw2hDkYZGcAH80vWn0QVFgM5M= 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 2/7] drm/drv: Add drmm cgroup registration for dev cgroups. Date: Wed, 23 Oct 2024 09:52:55 +0200 Message-ID: <20241023075302.27194-3-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-Server: rspam12 X-Rspamd-Queue-Id: 0F1C9160014 X-Stat-Signature: 56xq5eneb8wk3jjagtdpqrztnhq8mzhh X-Rspam-User: X-HE-Tag: 1729669973-80832 X-HE-Meta: U2FsdGVkX18ORxLocgzajqs1zDqTFJS0DwewC+wVMOw4I4FKQ2vszkRGWSAqlnBCIH/IkSyGmaEhjQNtHqVcI2YbERnuHf7HBw+DhdYD1/Bvas2Aj/h7RmMduI1pDUz5B3Gq/cyCMxIsz7BsiJ11HBQUV1+jBYKEaCU4hrhFVLWDh3OI3ypzzZlWqoepDOaVQXN/xj33tEdzgGE6E/nU9Z3MXw3ao+nOGpoQyUOil/bAhfxeuduwV0Ua05tO617QFw3I7ubIjQnBWXOC9dymzH7sdXe1vAKPCp4ZTn+tsVw6II3qvr1/6oY/1Yw9jJ137+0eQ8/v90TpeA94Ig44uVEyRIl1FwKVFjLycmR7N3LNA6SyIZ8E75i7V1zeeSDxwuK/3x/9HxuLy7hpbA76VK+BQ4JHa/FEtIm1jQd34OP6qm1KwXYWmMSD7YJNwv/32Ku6uVsvb1JWCOJFFORgPM22F6JgIy8rzrCO7Tu9sSMch4+51KAMxOJCsMMWa3qH+ehi+kf0xa4y6+6D8ZysT8hrjIAJB30P2khSvxQHfXpTDQllzSZcgwpB1muzqBWm4sGg/kj/+YI3QgSdX+440CXdpznj8WQb1/RzppOAc+KnqOHNLLc8ow2CwN6qZb+nyfJ+ErAEXTZ/bmTknbMhKz4ZWTaBMA0IJyyBDH2vDQkm9v8IoN7O1yi4GC4dPEBvrajE96VA4ibXs2bxEWIElLbTSKnW3XV8GtgygP2z/F2uYiivCaRvv6W7/hROk/TLqnct6SAacPGiNW+51NTxTczt46HdV+sYvJmpBDDZJAUmC3x4A9VS3jfxyjoZlx4QTxZzMxOdk/F19SL8nBwgZO4Wm12/GA7INIcuNJhY4RsO9XNXo8Qk7w+C3ssAsKnYqF+ECFvHtoh5+OhH0mMvvZNgQaK5tDQuMuzEL/B/3JcU40Y0yPa80wltGhD8X2kywc3DzCm6QYVqY/EuDsY IczOI6+s YyNPVF+YjQ0c8ku5h2BsslMcg2aBvgjBKfSsyVbTTyMqg3bAzQoA/DbEVTaBlhdDKTHkcz3tLWNQ/dRDj9I/M7oAbbkUIq8QmoHvPvm4kuVIHbJXAH1Fphm8/kb0lf54ZFLADWSHoxM2pCM0CpBUmjBetd+O/99ACCdlDp9jd48t1SGo6iQm6mTBpVO2SXzFTnSBl+m8qrwXS0mztaQjJFo3uU4+tSy2q8VqYidvY//LZ/BXvc7n0oVMoUpiqfwgv7Fht 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 Drivers will need to register a cgroup device at probe time, so let's give them a drm-managed helper. Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/drm_drv.c | 21 +++++++++++++++++++++ include/drm/drm_drv.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index c2c172eb25df7..251d1d69b09c5 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -26,6 +26,7 @@ * DEALINGS IN THE SOFTWARE. */ +#include #include #include #include @@ -820,6 +821,26 @@ void drm_dev_put(struct drm_device *dev) } EXPORT_SYMBOL(drm_dev_put); +static inline void drmm_cg_unregister_device(struct drm_device *dev, void *arg) +{ + dev_cgroup_unregister_device(arg); +} + +int drmm_cgroup_register_device(struct drm_device *dev, + struct dev_cgroup_device *cgdev) +{ + int ret; + char dev_name[32]; + + snprintf(dev_name, sizeof(dev_name), "drm/%s", dev->unique); + ret = dev_cgroup_register_device(cgdev, dev_name); + if (ret) + return ret; + + return drmm_add_action_or_reset(dev, drmm_cg_unregister_device, cgdev); +} +EXPORT_SYMBOL_GPL(drmm_cgroup_register_device); + static int create_compat_control_link(struct drm_device *dev) { struct drm_minor *minor; diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index 1bbbcb8e2d231..3e83c7ce7f2d0 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -34,6 +34,7 @@ #include +struct dev_cgroup_device; struct drm_fb_helper; struct drm_fb_helper_surface_size; struct drm_file; @@ -438,6 +439,9 @@ void *__devm_drm_dev_alloc(struct device *parent, const struct drm_driver *driver, size_t size, size_t offset); +int drmm_cgroup_register_device(struct drm_device *dev, + struct dev_cgroup_device *cgdev); + /** * devm_drm_dev_alloc - Resource managed allocation of a &drm_device instance * @parent: Parent device object From patchwork Wed Oct 23 07:52:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846619 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 1DD35D2E023 for ; Wed, 23 Oct 2024 07:53:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9E2336B0089; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F4B96B0092; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 572896B008A; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 23CD16B0089 for ; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 41BC3160782 for ; Wed, 23 Oct 2024 07:52:49 +0000 (UTC) X-FDA: 82704101088.14.0C6E1F0 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 5482F120016 for ; Wed, 23 Oct 2024 07:52:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; spf=none (imf29.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729669834; 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=4vqMuD3U9OkjPg5O6euHjufDvirie1Epb9x0OCJeatM=; b=kOnmQQCv9nkdosiNLWIJS2rrzevcr8GTSakNLPIgZrpcrOPqWMaMTILJog/mnPq7Zt2v/q D5lpHcDpc+U3FaSlAr5JLOUtiCkvi/PW7P7W7JRMHy0lqIYGPLBVCE9DOJo0yx4/Ox5Hav 6ky+axCpp04wO3DXIsPkzRpAxzaiQXc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669834; a=rsa-sha256; cv=none; b=H7h1FV8Kxd9yD3Pw/2cTvEXUPPYrjfFQJDn7I5ePeuiQ4lj/hDVa1gFZ/MHmcfao2hOA2l v9lDcKyTjFAFJqw6C5ZppNCifKeXO22knf/gjX8xVUnJ78zitJ6Yd9+uSkEnXyza2lYSD8 zbQDmIp80VfK5IlmBgn6d7/+o00tmi4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; spf=none (imf29.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) 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 3/7] drm/ttm: Handle cgroup based eviction in TTM Date: Wed, 23 Oct 2024 09:52:56 +0200 Message-ID: <20241023075302.27194-4-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-Stat-Signature: 93feac9ruk5s6tedk373bbiipkmpxsc5 X-Rspamd-Queue-Id: 5482F120016 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1729669964-22655 X-HE-Meta: U2FsdGVkX19m/14rWooZjB7mP0Qge0YWcGO/oYjalG97RHHdE6dHGp9ceecfhIw71Ju5vhWJqyqRSIany0sjYzpbhqLN4U41NcuRDhFlcSxpuf1/Cdm3vnO6BGNFcWiSlj7lbmE4O6DCOh5V8rUJ+UE3ReAnCF0PZ4t6AmcTwDBQMhlyxuNb4hHfoSb0AUoVeoElkN+TpOdyiDJgbjOEBGd0i+yurznh9U+8IUTS3iX/EZ/rP6e50GctIM1IIRqR+QIiby+1/HHHYcpfKWd/v9CrDRUesVHh9kDWrsYfYIGonnHfpFpo6fVcfKnGMYmZMfr8ZTXx8ZcAKLi87iE8eUFE1t7zFy2FN+HUuL5eoZDFjxbpcne0ig7ynd0iXr2OVPDyiijWq7niLZC828qLFfKet3vDPM8NP+AQh4j+ORf/x4pybOLygty/q2DUjerAJb5dnSEH9/mqKyTAblx3ioL0dDcBhpOePONtnpLVgmeXygNwl1czuwde47lZsdOMn97yG60CmWo3x0oOIgFAvI05zRMp/mxfwHco8qvI84Iaj+XpIH0TrdO7Y/zk1ytrmF13gxR4DnoA6KW7iNt6qqhriY8dgAm2YYTVGz5VrczTFqv0yDTPTy1PA7MKaPp5X9UG6nLAyeOgnyA2hSoB/5Ri/wZO3re0tw6AdGR5zK0Cfc1YfJ23Uc2rro4MGkEN2C/EYGUGK3kfz0l5ZPtAVHPvDqzYTpg4hpbRAGLYEYe+ZDddUklqttwCZBJbhlM2ZJWQelhpqKyuko1/+wWAkoTW0Cn4sogRHdsuA0sP6SCqTz2nfDud1eYBdISrPrHcK0vRzxG8FbUda08h2+9ik52k4Be2SXkuokXA1DDEFJ8AQ3huI3TvKGswK5LJqhGhc8RXOjwW1wYis8uCdDTJ0txgNRY9uJV4Ix8ixKhQd45P73tD6yx8zm5NSMvxQkvAm6vV7Z0LEH5Q/y8rbdV DRXXRqsZ y6iAHfy+Zzcoa+vUGG2/wc9rH9pX8HubVEhUfgToxIeorP0QHUjxifFZYOi3QyBh84Wv9MziYiJoS0psE2Tzt+9CXbXdipYg5hLNR/Z8UJl18GYvIBH9g7JiXrsnaOgYHh2D7SL5ggHrCZs0rXRD6FhjAYbkOwHZEHvmodtx44Q8/oQBGpEVE7phOMIdyzvOI4ZKmHi/zBHIOW6L+G+1P5QtuNM+X0AUsu2l7y05ooevc++PM5FhBjZRgbs9tSS2wmVEW 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: cgroup resource allocation has to be handled in TTM, so -EAGAIN from cgroups can be converted into -ENOSPC, and the limitcg can be properly evicted in ttm code. When hitting a resource limit through -EAGAIN, the cgroup for which the limit is hit is also returned. This allows eviction to delete only from cgroups which are a subgroup of the current cgroup. The returned CSS is used to determine if eviction is valuable for a given resource, and allows TTM to only target specific resources to lower memory usage. Co-developed-by: Friedrich Vock Signed-off-by: Friedrich Vock Co-developed-by: Maxime Ripard Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/ttm/tests/ttm_bo_test.c | 18 +++--- .../gpu/drm/ttm/tests/ttm_bo_validate_test.c | 4 +- drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 2 +- drivers/gpu/drm/ttm/ttm_bo.c | 57 ++++++++++++++++--- drivers/gpu/drm/ttm/ttm_resource.c | 24 +++++++- include/drm/ttm/ttm_resource.h | 16 +++++- 6 files changed, 98 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/ttm/tests/ttm_bo_test.c b/drivers/gpu/drm/ttm/tests/ttm_bo_test.c index 3139fd9128d84..f8f20d2f61740 100644 --- a/drivers/gpu/drm/ttm/tests/ttm_bo_test.c +++ b/drivers/gpu/drm/ttm/tests/ttm_bo_test.c @@ -258,13 +258,13 @@ static void ttm_bo_unreserve_basic(struct kunit *test) bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL); bo->priority = bo_prio; - err = ttm_resource_alloc(bo, place, &res1); + err = ttm_resource_alloc(bo, place, &res1, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo->resource = res1; /* Add a dummy resource to populate LRU */ - ttm_resource_alloc(bo, place, &res2); + ttm_resource_alloc(bo, place, &res2, NULL); dma_resv_lock(bo->base.resv, NULL); ttm_bo_unreserve(bo); @@ -300,12 +300,12 @@ static void ttm_bo_unreserve_pinned(struct kunit *test) dma_resv_lock(bo->base.resv, NULL); ttm_bo_pin(bo); - err = ttm_resource_alloc(bo, place, &res1); + err = ttm_resource_alloc(bo, place, &res1, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo->resource = res1; /* Add a dummy resource to the pinned list */ - err = ttm_resource_alloc(bo, place, &res2); + err = ttm_resource_alloc(bo, place, &res2, NULL); KUNIT_ASSERT_EQ(test, err, 0); KUNIT_ASSERT_EQ(test, list_is_last(&res2->lru.link, &priv->ttm_dev->unevictable), 1); @@ -355,7 +355,7 @@ static void ttm_bo_unreserve_bulk(struct kunit *test) ttm_bo_set_bulk_move(bo1, &lru_bulk_move); dma_resv_unlock(bo1->base.resv); - err = ttm_resource_alloc(bo1, place, &res1); + err = ttm_resource_alloc(bo1, place, &res1, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo1->resource = res1; @@ -363,7 +363,7 @@ static void ttm_bo_unreserve_bulk(struct kunit *test) ttm_bo_set_bulk_move(bo2, &lru_bulk_move); dma_resv_unlock(bo2->base.resv); - err = ttm_resource_alloc(bo2, place, &res2); + err = ttm_resource_alloc(bo2, place, &res2, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo2->resource = res2; @@ -401,7 +401,7 @@ static void ttm_bo_put_basic(struct kunit *test) bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL); bo->type = ttm_bo_type_device; - err = ttm_resource_alloc(bo, place, &res); + err = ttm_resource_alloc(bo, place, &res, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo->resource = res; @@ -518,7 +518,7 @@ static void ttm_bo_pin_unpin_resource(struct kunit *test) bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL); - err = ttm_resource_alloc(bo, place, &res); + err = ttm_resource_alloc(bo, place, &res, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo->resource = res; @@ -569,7 +569,7 @@ static void ttm_bo_multiple_pin_one_unpin(struct kunit *test) bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE, NULL); - err = ttm_resource_alloc(bo, place, &res); + err = ttm_resource_alloc(bo, place, &res, NULL); KUNIT_ASSERT_EQ(test, err, 0); bo->resource = res; diff --git a/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c b/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c index 1adf18481ea05..3148f5d3dbd66 100644 --- a/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +++ b/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c @@ -542,7 +542,7 @@ static void ttm_bo_validate_no_placement_signaled(struct kunit *test) bo->ttm = old_tt; } - err = ttm_resource_alloc(bo, place, &bo->resource); + err = ttm_resource_alloc(bo, place, &bo->resource, NULL); KUNIT_EXPECT_EQ(test, err, 0); KUNIT_ASSERT_EQ(test, man->usage, size); @@ -603,7 +603,7 @@ static void ttm_bo_validate_no_placement_not_signaled(struct kunit *test) bo = ttm_bo_kunit_init(test, test->priv, size, NULL); bo->type = params->bo_type; - err = ttm_resource_alloc(bo, place, &bo->resource); + err = ttm_resource_alloc(bo, place, &bo->resource, NULL); KUNIT_EXPECT_EQ(test, err, 0); placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL); diff --git a/drivers/gpu/drm/ttm/tests/ttm_resource_test.c b/drivers/gpu/drm/ttm/tests/ttm_resource_test.c index a9f4b81921c3c..e6ea2bd01f07a 100644 --- a/drivers/gpu/drm/ttm/tests/ttm_resource_test.c +++ b/drivers/gpu/drm/ttm/tests/ttm_resource_test.c @@ -302,7 +302,7 @@ static void ttm_sys_man_free_basic(struct kunit *test) res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, res); - ttm_resource_alloc(bo, place, &res); + ttm_resource_alloc(bo, place, &res, NULL); man = ttm_manager_type(priv->devs->ttm_dev, mem_type); man->func->free(man, res); diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index 48c5365efca1c..5bdebfdc95e92 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include "ttm_module.h" @@ -447,7 +448,7 @@ int ttm_bo_evict_first(struct ttm_device *bdev, struct ttm_resource_manager *man struct ttm_buffer_object *bo; struct ttm_resource *res; unsigned int mem_type; - int ret = 0; + int ret; spin_lock(&bdev->lru_lock); res = ttm_resource_manager_first(man, &cursor); @@ -499,14 +500,28 @@ struct ttm_bo_evict_walk { struct ttm_resource **res; /** @evicted: Number of successful evictions. */ unsigned long evicted; + + /** @limit_pool: Which pool limit we should test against */ + struct dev_cgroup_pool_state *limit_pool; + /** @try_low: Whether we should attempt to evict BO's with low watermark threshold */ + bool try_low; + /** @hit_low: If we cannot evict a bo when @try_low is false (first pass) */ + bool hit_low; }; static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) { struct ttm_bo_evict_walk *evict_walk = container_of(walk, typeof(*evict_walk), walk); + struct ttm_resource_manager *man = + ttm_manager_type(bo->bdev, bo->resource->mem_type); s64 lret; + if (!dev_cgroup_state_evict_valuable(man->cgdev, man->cgidx, + evict_walk->limit_pool, bo->resource->css, + evict_walk->try_low, &evict_walk->hit_low)) + return 0; + if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->place)) return 0; @@ -524,7 +539,7 @@ static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object * evict_walk->evicted++; if (evict_walk->res) lret = ttm_resource_alloc(evict_walk->evictor, evict_walk->place, - evict_walk->res); + evict_walk->res, NULL); if (lret == 0) return 1; out: @@ -545,7 +560,8 @@ static int ttm_bo_evict_alloc(struct ttm_device *bdev, struct ttm_buffer_object *evictor, struct ttm_operation_ctx *ctx, struct ww_acquire_ctx *ticket, - struct ttm_resource **res) + struct ttm_resource **res, + struct dev_cgroup_pool_state *limit_pool) { struct ttm_bo_evict_walk evict_walk = { .walk = { @@ -556,22 +572,39 @@ static int ttm_bo_evict_alloc(struct ttm_device *bdev, .place = place, .evictor = evictor, .res = res, + .limit_pool = limit_pool, }; s64 lret; evict_walk.walk.trylock_only = true; lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); + + /* One more attempt if we hit low limit? */ + if (!lret && evict_walk.hit_low) { + evict_walk.try_low = true; + lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); + } if (lret || !ticket) goto out; + /* Reset low limit */ + evict_walk.try_low = evict_walk.hit_low = false; /* If ticket-locking, repeat while making progress. */ evict_walk.walk.trylock_only = false; + +retry: do { /* The walk may clear the evict_walk.walk.ticket field */ evict_walk.walk.ticket = ticket; evict_walk.evicted = 0; lret = ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); } while (!lret && evict_walk.evicted); + + /* We hit the low limit? Try once more */ + if (!lret && evict_walk.hit_low && !evict_walk.try_low) { + evict_walk.try_low = true; + goto retry; + } out: if (lret < 0) return lret; @@ -689,6 +722,7 @@ static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo, for (i = 0; i < placement->num_placement; ++i) { const struct ttm_place *place = &placement->placement[i]; + struct dev_cgroup_pool_state *limit_pool = NULL; struct ttm_resource_manager *man; bool may_evict; @@ -701,15 +735,20 @@ static int ttm_bo_alloc_resource(struct ttm_buffer_object *bo, continue; may_evict = (force_space && place->mem_type != TTM_PL_SYSTEM); - ret = ttm_resource_alloc(bo, place, res); + ret = ttm_resource_alloc(bo, place, res, force_space ? &limit_pool : NULL); if (ret) { - if (ret != -ENOSPC) + if (ret != -ENOSPC && ret != -EAGAIN) { + dev_cgroup_pool_state_put(limit_pool); return ret; - if (!may_evict) + } + if (!may_evict) { + dev_cgroup_pool_state_put(limit_pool); continue; + } ret = ttm_bo_evict_alloc(bdev, man, place, bo, ctx, - ticket, res); + ticket, res, limit_pool); + dev_cgroup_pool_state_put(limit_pool); if (ret == -EBUSY) continue; if (ret) @@ -1056,6 +1095,8 @@ struct ttm_bo_swapout_walk { struct ttm_lru_walk walk; /** @gfp_flags: The gfp flags to use for ttm_tt_swapout() */ gfp_t gfp_flags; + + bool hit_low, evict_low; }; static s64 @@ -1106,7 +1147,7 @@ ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) memset(&hop, 0, sizeof(hop)); place.mem_type = TTM_PL_SYSTEM; - ret = ttm_resource_alloc(bo, &place, &evict_mem); + ret = ttm_resource_alloc(bo, &place, &evict_mem, NULL); if (ret) goto out; diff --git a/drivers/gpu/drm/ttm/ttm_resource.c b/drivers/gpu/drm/ttm/ttm_resource.c index a87665eb28a62..93190f7330b5c 100644 --- a/drivers/gpu/drm/ttm/ttm_resource.c +++ b/drivers/gpu/drm/ttm/ttm_resource.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -350,15 +351,29 @@ EXPORT_SYMBOL(ttm_resource_fini); int ttm_resource_alloc(struct ttm_buffer_object *bo, const struct ttm_place *place, - struct ttm_resource **res_ptr) + struct ttm_resource **res_ptr, + struct dev_cgroup_pool_state **ret_limit_pool) { struct ttm_resource_manager *man = ttm_manager_type(bo->bdev, place->mem_type); + struct dev_cgroup_pool_state *pool = NULL; int ret; + if (man->cgdev) { + ret = dev_cgroup_try_charge(man->cgdev, man->cgidx, + bo->base.size, &pool, ret_limit_pool); + if (ret) + return ret; + } + ret = man->func->alloc(man, bo, place, res_ptr); - if (ret) + if (ret) { + if (pool) + dev_cgroup_uncharge(pool, man->cgidx, bo->base.size); return ret; + } + + (*res_ptr)->css = pool; spin_lock(&bo->bdev->lru_lock); ttm_resource_add_bulk_move(*res_ptr, bo); @@ -370,6 +385,7 @@ EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc); void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res) { struct ttm_resource_manager *man; + struct dev_cgroup_pool_state *pool; if (!*res) return; @@ -377,9 +393,13 @@ void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res) spin_lock(&bo->bdev->lru_lock); ttm_resource_del_bulk_move(*res, bo); spin_unlock(&bo->bdev->lru_lock); + + pool = (*res)->css; man = ttm_manager_type(bo->bdev, (*res)->mem_type); man->func->free(man, *res); *res = NULL; + if (man->cgdev) + dev_cgroup_uncharge(pool, man->cgidx, bo->base.size); } EXPORT_SYMBOL(ttm_resource_free); diff --git a/include/drm/ttm/ttm_resource.h b/include/drm/ttm/ttm_resource.h index be034be56ba1b..12ff4b66c6f7d 100644 --- a/include/drm/ttm/ttm_resource.h +++ b/include/drm/ttm/ttm_resource.h @@ -38,6 +38,7 @@ #define TTM_MAX_BO_PRIORITY 4U #define TTM_NUM_MEM_TYPES 8 +struct dev_cgroup_device; struct ttm_device; struct ttm_resource_manager; struct ttm_resource; @@ -211,6 +212,15 @@ struct ttm_resource_manager { * bdev->lru_lock. */ uint64_t usage; + + /** + * @cgdev: dev_cgroup_device used for memory accounting, if not NULL. + */ + struct dev_cgroup_device *cgdev; + /** + * @cgidx: Resource index used by this resource manager for cgroup accounting + */ + u32 cgidx; }; /** @@ -239,6 +249,7 @@ struct ttm_bus_placement { * @placement: Placement flags. * @bus: Placement on io bus accessible to the CPU * @bo: weak reference to the BO, protected by ttm_device::lru_lock + * @css: cgroup state this resource is charged to * * Structure indicating the placement and space resources used by a * buffer object. @@ -251,6 +262,8 @@ struct ttm_resource { struct ttm_bus_placement bus; struct ttm_buffer_object *bo; + struct dev_cgroup_pool_state *css; + /** * @lru: Least recently used list, see &ttm_resource_manager.lru */ @@ -432,7 +445,8 @@ void ttm_resource_fini(struct ttm_resource_manager *man, int ttm_resource_alloc(struct ttm_buffer_object *bo, const struct ttm_place *place, - struct ttm_resource **res); + struct ttm_resource **res, + struct dev_cgroup_pool_state **ret_limit_pool); void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res); bool ttm_resource_intersects(struct ttm_device *bdev, struct ttm_resource *res, From patchwork Wed Oct 23 07:52:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846617 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 A3F0CD2E026 for ; Wed, 23 Oct 2024 07:53:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 721166B0088; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AA0C6B0089; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B1296B008C; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1D7B96B0088 for ; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 07FFE16077B for ; Wed, 23 Oct 2024 07:52:49 +0000 (UTC) X-FDA: 82704100752.30.37EC2D0 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 3E1E7C001E for ; Wed, 23 Oct 2024 07:52:46 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; spf=none (imf22.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729669910; 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=ejpx4io7xkTkt6wdywiSczKdYJy3tk7mjiwA8WRzGUs=; b=NW2iPZo1J+tszO47k6D0kVL7dTOnv2IrXK/7LsYwtGDbRuqftGGc2u5/8dagVJrCVxvAES u/MHMY/vmmaGltpx0h7C7U5NJtSSpt/GNsjGelnSRGJuQ0tRcbZDh+fJaSF+PzoJalLN55 wd2Isr/LyV7P146vRBLX+36y/z7fQ/w= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=none (imf22.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669910; a=rsa-sha256; cv=none; b=qSTGFE4KbwUwOguDuU7jfTzvlL5ojm96gQsgXh4MbTvd/KLpvh67hWDYkYLmcnxB78dtlx IMRLTJgZTzv0uUdZ+BglNLDz79BgTYX5EQCGBnfQURDu1EIJPn4CabiZq8+yU1NkYxHzwO 80tZcG+MaqWJbQNAj+cTtJRxkYqdsxk= 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 4/7] drm/xe: Implement cgroup for vram Date: Wed, 23 Oct 2024 09:52:57 +0200 Message-ID: <20241023075302.27194-5-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-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 3E1E7C001E X-Stat-Signature: thyq68a8hhyyw561bsm73gf1um6qktyo X-HE-Tag: 1729669966-91516 X-HE-Meta: U2FsdGVkX1/JUBA4N2w+MheJiY1gJ++pFU4CZzgJ72WMe2PiaNXX2loST16pFhhfgGzWtl0VprF9TyJ8Z/UjO4oS5Zjhmn4ye/4oWWplvTJMmofNdMVdzUE6rZJRZgUMOaz/FxUjIUr4LPU6zoTov90pnw2BFVRCDnbd8wyIanzfAi3qBQdjsqptC+FpvLTTRLS/+Ah6UaDIirqyDWBXg1B2YQ5F+v8j0G0F8U1+z32TriBFr1PMywDnTGqq1wq6e1iK+0QVks/JerrAr8ESEl1VTSurELyDSXYObm3ubAy7x57sCexXa3fSI/WAVXg2ZR8pubTHx64U9kwrnHB+tS1xtgvC/MMq06XUZ44HevHkE8nU3vlMdM8Cszj9Cb6wYUXQjZFyQlYbJJmaKtztR9aKeTFx+YsX3mN9neTblfjcPUb3hV/KdXE2FIoLCrGadHm0LBWTngs5/tG78Vsy0sBWt5s6ODNbJ/DI2WC2Lk9VmMij4SFPMy95dscX4T21sdWmRoiA9NWMRWsmHDzwcqQcrRElZejaQjkjYjk/dcpjvpTqoorv/Gjm4PwtwCHKP0Ehst00msvxoOEBbB3adOtpbfGxlUzlfp2s8oJgJJthVndnG80D3lCwqWaQgB67XPculRMQ6dqvO3ycIV9lXtoO9KlqrIek6JNcq8K+q7MUhYj+o236ylAa0+2POQhgcrXwXzCBTd6GvETYLWi7xIwm2Bwkfr2NGI5TLXQPj/dBSSSRkpAqfrr6iVJZW+mxtbXVsFswh10mxCnbA6hildeZMeVF/Rl6hCgLmJFiM62BikoyJdxV1uaqojUmsrV/XHwikYP0Rgzy6vk1u2GQK3S9voW0nfbNzqtwaiiD2LvD5Kbvr5OYwgQGea6Iuz+wo+Gc6p5buk3gKwdJVVf1+cIuZCJZIvLwB9Pz1BBcthgd8MvKpXJ/0+aSGBzyyu01R0DGUyhj225zSFNKyDg OZcRl67V 4lHoKvvyDU3kDfvwz8LdtsQy4F13l8C9WPG7+dV/VKOeK26EZ0Kt9Nysl8IZTXDf2Xphk8+VES++HwT0UP/Tk57OeOK92hgwnMuEUfiEpV/PtfsvAoV6h6JlYfiwWyk9JJt20hNUK9EILqmJB5FgROJDjX//61JZeK5+dh/1Ibwvd59G5J7+bTdDzy8xe4yLGOf+500PIaFqJ/Od6uQQWSCueDxv+CcYs5ATrQtZvr/ZrqtgPq4QlA3lPD7Nw1t63E9j/ 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: Add vram based cgroup eviction to Xe. Most hardware with VRAM uses TTM for its management, and can be similarly trivially enabled. Co-developed-by: Maxime Ripard Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/xe/xe_device.c | 4 ++++ drivers/gpu/drm/xe/xe_device_types.h | 4 ++++ drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c index 51bb9d875268f..cb4b2013eb808 100644 --- a/drivers/gpu/drm/xe/xe_device.c +++ b/drivers/gpu/drm/xe/xe_device.c @@ -726,6 +726,10 @@ int xe_device_probe(struct xe_device *xe) /* Allocate and map stolen after potential VRAM resize */ xe_ttm_stolen_mgr_init(xe); + err = drmm_cgroup_register_device(&xe->drm, &xe->cg); + if (err) + goto err; + /* * Now that GT is initialized (TTM in particular), * we can try to init display, and inherit the initial fb. diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h index 85bede4dd6461..0401b4fe6f645 100644 --- a/drivers/gpu/drm/xe/xe_device_types.h +++ b/drivers/gpu/drm/xe/xe_device_types.h @@ -7,6 +7,7 @@ #define _XE_DEVICE_TYPES_H_ #include +#include #include #include @@ -257,6 +258,9 @@ struct xe_device { /** @devcoredump: device coredump */ struct xe_devcoredump devcoredump; + /** @cg: device cgroup bookkeeping */ + struct dev_cgroup_device cg; + /** @info: device info */ struct intel_device_info { /** @info.platform_name: platform name */ diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c index 423b261ea7430..cb463be53f4bc 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c @@ -339,6 +339,16 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr, struct ttm_resource_manager *man = &mgr->manager; int err; + if (mem_type != XE_PL_STOLEN) { + int cgregion = xe->cg.num_regions++; + + xe->cg.regions[cgregion].size = size; + xe->cg.regions[cgregion].name = + mem_type == XE_PL_VRAM0 ? "vram0" : "vram1"; + man->cgdev = &xe->cg; + man->cgidx = cgregion; + } + man->func = &xe_ttm_vram_mgr_func; mgr->mem_type = mem_type; mutex_init(&mgr->lock); From patchwork Wed Oct 23 07:52:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846620 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 7104FD2E01E for ; Wed, 23 Oct 2024 07:53:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DBF26B008A; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A15D6B008C; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17D596B0092; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id D5F556B008A for ; Wed, 23 Oct 2024 03:53:09 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9C3FBAB6F9 for ; Wed, 23 Oct 2024 07:52:35 +0000 (UTC) X-FDA: 82704100626.27.7269F93 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf14.hostedemail.com (Postfix) with ESMTP id E02A710000F for ; Wed, 23 Oct 2024 07:52:48 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=none (imf14.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729669786; 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=IStRVWjuQ2ViCFyq4NjdNtG3RiDf7KbHOrMvbs+vNeA=; b=VTCltBbyfR7U2YaNWgQkuXwtvykH3quzRL7lI8Eko1WLBUfLaZ2ZdruYmQUK19jLejm8wI s9Y3yw34cmyVIOglOS8/PdrbVFi9wHS4Hrbl5qSfI1n7ky1yZgAedl1Qmib5rkLStuswkS x1YqvFvSK8hbJlj+47xno6Xo/o5897I= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=none (imf14.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669786; a=rsa-sha256; cv=none; b=UsOesaa4UKPw+ZeEG1Zl2FZBx14mS+7eh/n2JjKDLGnvG4oUfE71VWvZWa4Ls+5gnIrMRo kBQD9uAwJ+mYIq8dP4IqmqPT/OjUwFe70mFqSqJkG+E16/kZbL/rbhsRDSV0G4Yw7i4rOx sUtBPXb40sn78IkT9kqf2nYmxE15nP8= 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 5/7] drm/amdgpu: Add cgroups implementation Date: Wed, 23 Oct 2024 09:52:58 +0200 Message-ID: <20241023075302.27194-6-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-Server: rspam06 X-Rspamd-Queue-Id: E02A710000F X-Stat-Signature: iuosude8exeswhks7y7xw9qhfshufuuq X-Rspam-User: X-HE-Tag: 1729669968-294449 X-HE-Meta: U2FsdGVkX1+oPITVO3nWxf2xIOpbyVWUFFr2X+IezKUwI+ej67o0fhO0TSGeESUFvGPDpQ0kl36LuUQCZiKYxxr6ryifNKtk0tJ3dsTzFKtFQgFvJQHRaCV4OVB61+BaAALrmUdtXTPBy+P98FHKs9HANWxI8Kng2hReyC+9Q0qQBCVGitXkdPT9CmOb9FWECVkLt6YDQ+Y6CNx66wsUgl5rU0ZGq35ksmgv0xhR49QA577uerTVHA1cDYCQXgTNylXa5V2bky146gawF7q64UUbwmquhlIP8TwItptM/iY/ZCDsQyYzWkpCCoCD2K1d3r6rGcwHFFaIL3tVe8a82RutnASv8FgMqXhQyM7dD+RVzmLemErR13QyFKqhe1G6ysCxXvCmqt/qZ2zRwx1yZIVlhWEtnay3ttNVJlRwsNU4Xrk/+Ds4cXknBm562aw/MXjbZvcC/CfaVAmww81+JfTBNWTxRIwNegDOMUBkoiOEyv9F5RTPYKxqeElqtxSq94z8mG3koluiec5X3Ceq5tWdsd/u4sjiLL/o1XC8CsIZaOzS2rafbt0IuVa/KdO3thWWma1DWLIZZOZLaWzimNTl7gRzVsNCsv/7B3wzyElrkfIY7yFsALMH8SLVWbM6SKU200nvlBKOB0dQHR8v5JGZJK17mPeywTYN+CTIBg9T2rcPTzhHxFJrK09UfZqpiiRyKtqHHfZZMSqX8ilydcoG7muUhhrM/XYYaxOq9wPblpQdLI+qWSoY14bywlUoqT6PaW1JrVzN6RRQbz6CGqkFGfR934nPwSKmkAYCLNch1rLjcIFXCvmy2c0Lk97se2jXsNGREDS6T4CuypSE8H4Lo09l8UEYmWeMhWgsX02BwsYJvidVbw3aHERYCp46JkkegtchrwyuTQBAMabPUVVREloWXSkqKvjPFUmFEdn6gKclUW71jrXTzmscJcAAQEe7iQlWzhcCMJmw90Q c9egY4Rv 3/MCBeiw0h8thx9NEp21nEXCfoo+xJBYuTUuQU2OM55uJ/uCzb7p6aLLZ47yxOSPcu7G4X8dn36GLIN32sM1jaYwlGS3T6Iu++nMwXljYG5d/ju2UvNMeBfozNRK8MG2aXmrmcphKb3MJYSjRQkWBU0Eg4n6qdqN/7QwyDq5plfPzAqzDQUcUnkJp3mU/zmx5wClphPcsRQ94hszGjDBHZ63hrRs7j7WSwKKUY1B4HyKOpfhNe7jW6A447Q9+u6oJK07z 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: Similar to xe, enable some simple management of VRAM only. Co-developed-by: Maxime Ripard Signed-off-by: Maxime Ripard Signed-off-by: Maarten Lankhorst --- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 6 ++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h index 9b1e0ede05a45..27c11e43f8e9e 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h @@ -43,6 +43,7 @@ #include "amdgpu_ctx.h" #include +#include #include #include #include @@ -835,6 +836,7 @@ struct amdgpu_device { struct device *dev; struct pci_dev *pdev; struct drm_device ddev; + struct dev_cgroup_device cg; #ifdef CONFIG_DRM_AMD_ACP struct amdgpu_acp acp; diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index 74adb983ab03e..3f6554c7aac2f 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c @@ -1874,6 +1874,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev) return r; } + r = drmm_cgroup_register_device(adev_to_drm(adev), &adev->cg); + if (r) { + DRM_ERROR("Failed initializing cgroup allocator.\n"); + return r; + } + /* Change the size here instead of the init above so only lpfn is affected */ amdgpu_ttm_set_buffer_funcs_status(adev, false); #ifdef CONFIG_64BIT diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c index 7d26a962f811c..44d560bef5b7d 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c @@ -927,6 +927,12 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev) DRM_INFO("Setup dummy vram mgr\n"); } + adev->cg.regions[0].size = adev->gmc.real_vram_size; + adev->cg.regions[0].name = "vram"; + adev->cg.num_regions++; + man->cgdev = &adev->cg; + man->cgidx = 0; + ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager); ttm_resource_manager_set_used(man, true); return 0; From patchwork Wed Oct 23 07:52:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maarten Lankhorst X-Patchwork-Id: 13846621 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 81075D2E022 for ; Wed, 23 Oct 2024 07:53:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E757E6B008C; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFEF86B0095; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB57E6B0096; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 949216B008C for ; Wed, 23 Oct 2024 03:53:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id CA1ED16079D for ; Wed, 23 Oct 2024 07:52:50 +0000 (UTC) X-FDA: 82704101172.14.C1EC8F3 Received: from mblankhorst.nl (lankhorst.se [141.105.120.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 6E7C41C0006 for ; Wed, 23 Oct 2024 07:53:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf18.hostedemail.com: domain of mlankhorst@mblankhorst.nl has no SPF policy when checking 141.105.120.124) smtp.mailfrom=mlankhorst@mblankhorst.nl ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729669937; a=rsa-sha256; cv=none; b=3oGM+8mmXoXVIvq5GH9dDUT23xSKJGTYfpkD6HKBeIwz8Y2UNNaE0uC6VY4lN20LFkOGtO 0UyolGOQ7huszF5JHTrj2h4JsL/dCIc+x9Cau3Fv7a8A5ZGo1QFU3txoWbqZjwAT+3bZ8t O7Z7Oywv3RjwUmWLUXKDneiwJmhggqA= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none); spf=none (imf18.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=1729669937; 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=/6iKeHM5xEyYy3QUwUrb8TZP4rb+L03HlgF90ruQ3no=; b=k4egfHfAK0CrpdVlvSepudrTYf7wHAK65civnw+uqSPBldU+OAK1M9ZM29cNIMgybsPasU 29MlcfPRh5gonWHNU9JVY214NqVNx0aO2vrEvV3GbijhlKNXlVQgfXq+3qzVCGY5NDyj0L VUwaBXEkTkbhJnYeOFl7ZuL4H60PWWE= 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 6/7] [HACK] drm/xe: Hack to test with mapped pages instead of vram. Date: Wed, 23 Oct 2024 09:52:59 +0200 Message-ID: <20241023075302.27194-7-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-Rspam-User: X-Rspamd-Queue-Id: 6E7C41C0006 X-Rspamd-Server: rspam01 X-Stat-Signature: me3etmwm4wg66mp9qd6u8qgrgrjp87dj X-HE-Tag: 1729669981-879144 X-HE-Meta: U2FsdGVkX18zQ144i+8Jb0iHIY5RvzdpNUq0Bco1MlyPpwK+vmfz2TwHj6wgNYNuk9Yj30bsWZJuOnt1b0yrk1xukczlvkN0Wh3ZAQQfyjrhS9KhTJdWdlF2QOjMkQiztnTRl3d/R685k/Op7P7GA4tPumN0XpkjesfZGrw2ME+I6JzVYDomPQk8hpX1fwSbTMTUr2ryFpZfGs5NZGc+qSoiYZvAFWSpbH00vW4Hmjlg6U2paa6QfKkw+XMdm/jaqZ8bggxLRfA5/CwDzUU6z1ki5ZBthuBuIt0slVr8uUZjydF59V9HvyEFWBG8JDa0ntBnYCZDJHnJ57BCaa8Dni3RwRcg1lkrChDARvhzFCwl/NU7wdeDmVTypLVU6inAzvzpwyIz6X2SnDqrii9tgj9g3GvLk523AN1ToYxMcTFYsoQo2YQlFZ8hLhjUWfiqQk27DZfjczTAyr4LaPhqTOnPoRpHdCpj2FKIyr48mghWMKXjBvKD9cK7dxJLQ2dTdceI8FrJK4J7Tzt7xRodz7XN8l7oUlnrRQ1JYZnE5jSGEk3q48DhQ+u6wMZHykAoS0LLn6ly3LqKfRcxfWD/st4b+IzOWLV+ldV7qCYQM39edSGdda0ByEBmQubBGARJXLkWIM0J4Wbe9Jow05B8JPsYelkXp49/SwvJJjmgPJJfG2jmi44QP6bflF0VVpoYmNt+xiy3VSluhd3RVcVXo4qvty7ZvcxrBMwV8epsMaN17cVmNRP838y0gIaQRf5hyZmg3XZOuJgE6JeTHQlwOGyvdLc9906Mt7E4oij1f8pugRFYMAd5gsmXl+mSD7KFhgLpjPn/5GWSDD5x4GijnuzPUNg13mGgLiv1sbNIhLA1C2LLC8ADKCosknYo19nh0dC4l5MS4ye5DO8O5SCbwxmjv2a9AFMbXPX3IbpDqfxcWdCwNRR2HOvt+zq1arTQ61WfC0pJEufF/iAxaAM 1+IEmlMO vpn5BAfiJ5IhWBeOxXtWoZwDwMvW/PF/V+trtblTUdLeetNFnphg3nkxUVfvcHR+NsLP16ZLURX9FNQVPJ0Mr+t095nYqV+M4apRKhAB8CEHQi1y+fZfaWtYWG3Yn4FmUGtbRqXuBl/++qJzOB3UdCiYzWagG94iPv7x/dpWuXh+cayUQwGfGLJ1kFt81XCZIItruEp2oZhWpQa6wSPcykfSSzZNUg6t9nJB2fYY0x0etEzjVZY1VcPiMCD9BBKamVTHj1zCdUgjYT1GRfolVwQmZLWqSEa0iP+L/ 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: We will probably want to make this a proper region in TTM for everything, so that we can charge VRAM twice, once for mapped in sysmem, once for mapped in vram. That way we don't need to deal with evict failing from lack of available memory in mapped. Signed-off-by: Maarten Lankhorst Signed-off-by: Maxime Ripard --- drivers/gpu/drm/xe/xe_ttm_sys_mgr.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c index 9844a8edbfe19..20fa8ec8925ef 100644 --- a/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_sys_mgr.c @@ -101,6 +101,18 @@ static void ttm_sys_mgr_fini(struct drm_device *drm, void *arg) ttm_set_driver_manager(&xe->ttm, XE_PL_TT, NULL); } +static inline void apply_cg(struct xe_device *xe, + struct ttm_resource_manager *man, + u64 gtt_size) +{ + int cgregion = xe->cg.num_regions++; + + xe->cg.regions[cgregion].size = gtt_size; + xe->cg.regions[cgregion].name = "mapped"; + man->cgdev = &xe->cg; + man->cgidx = cgregion; + +} int xe_ttm_sys_mgr_init(struct xe_device *xe) { struct ttm_resource_manager *man = &xe->mem.sys_mgr; @@ -116,6 +128,8 @@ int xe_ttm_sys_mgr_init(struct xe_device *xe) man->func = &xe_ttm_sys_mgr_func; ttm_resource_manager_init(man, &xe->ttm, gtt_size >> PAGE_SHIFT); ttm_set_driver_manager(&xe->ttm, XE_PL_TT, man); + apply_cg(xe, man, gtt_size); + ttm_resource_manager_set_used(man, true); return drmm_add_action_or_reset(&xe->drm, ttm_sys_mgr_fini, xe); } 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; }; /**