dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Vetter <daniel-/w4YWyX8dFk@public.gmane.org>
To: Kenny Ho <Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
Cc: jsparks-WVYJKLFxKCc@public.gmane.org,
	amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
	lkaplan-WVYJKLFxKCc@public.gmane.org,
	alexander.deucher-5C7GfCeVMHo@public.gmane.org,
	y2kenny-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org,
	dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
	joseph.greathouse-5C7GfCeVMHo@public.gmane.org,
	tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	christian.koenig-5C7GfCeVMHo@public.gmane.org
Subject: Re: [RFC PATCH v3 11/11] drm, cgroup: Allow more aggressive memory reclaim
Date: Wed, 26 Jun 2019 18:44:44 +0200	[thread overview]
Message-ID: <20190626164444.GX12905@phenom.ffwll.local> (raw)
In-Reply-To: <20190626150522.11618-12-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>

On Wed, Jun 26, 2019 at 11:05:22AM -0400, Kenny Ho wrote:
> Allow DRM TTM memory manager to register a work_struct, such that, when
> a drmcgrp is under memory pressure, memory reclaiming can be triggered
> immediately.
> 
> Change-Id: I25ac04e2db9c19ff12652b88ebff18b44b2706d8
> Signed-off-by: Kenny Ho <Kenny.Ho@amd.com>
> ---
>  drivers/gpu/drm/ttm/ttm_bo.c    | 47 +++++++++++++++++++++++++++++++++
>  include/drm/drm_cgroup.h        | 14 ++++++++++
>  include/drm/ttm/ttm_bo_driver.h |  2 ++
>  kernel/cgroup/drm.c             | 33 +++++++++++++++++++++++
>  4 files changed, 96 insertions(+)
> 
> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
> index 79c530f4a198..5fc3bc5bd4c5 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> @@ -1509,6 +1509,44 @@ int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type)
>  }
>  EXPORT_SYMBOL(ttm_bo_evict_mm);
>  
> +static void ttm_bo_reclaim_wq(struct work_struct *work)
> +{

I think a design a bit more inspired by memcg aware core shrinkers would
be nice, i.e. explicitly passing:
- which drm_cgroup needs to be shrunk
- which ttm_mem_reg (well the fancy new abstracted out stuff for tracking
  special gpu memory resources like tt or vram or whatever)
- how much it needs to be shrunk

I think with that a lot more the book-keeping could be pushed into the
drm_cgroup code, and the callback just needs to actually shrink enough as
requested.
-Daniel

> +	struct ttm_operation_ctx ctx = {
> +		.interruptible = false,
> +		.no_wait_gpu = false,
> +		.flags = TTM_OPT_FLAG_FORCE_ALLOC
> +	};
> +	struct ttm_mem_type_manager *man =
> +	    container_of(work, struct ttm_mem_type_manager, reclaim_wq);
> +	struct ttm_bo_device *bdev = man->bdev;
> +	struct dma_fence *fence;
> +	int mem_type;
> +	int ret;
> +
> +	for (mem_type = 0; mem_type < TTM_NUM_MEM_TYPES; mem_type++)
> +		if (&bdev->man[mem_type] == man)
> +			break;
> +
> +	BUG_ON(mem_type >= TTM_NUM_MEM_TYPES);
> +
> +	if (!drmcgrp_mem_pressure_scan(bdev, mem_type))
> +		return;
> +
> +	ret = ttm_mem_evict_first(bdev, mem_type, NULL, &ctx);
> +	if (ret)
> +		return;
> +
> +	spin_lock(&man->move_lock);
> +	fence = dma_fence_get(man->move);
> +	spin_unlock(&man->move_lock);
> +
> +	if (fence) {
> +		ret = dma_fence_wait(fence, false);
> +		dma_fence_put(fence);
> +	}
> +
> +}
> +
>  int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
>  			unsigned long p_size)
>  {
> @@ -1543,6 +1581,13 @@ int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
>  		INIT_LIST_HEAD(&man->lru[i]);
>  	man->move = NULL;
>  
> +	pr_err("drmcgrp %p type %d\n", bdev->ddev, type);
> +
> +	if (type <= TTM_PL_VRAM) {
> +		INIT_WORK(&man->reclaim_wq, ttm_bo_reclaim_wq);
> +		drmcgrp_register_device_mm(bdev->ddev, type, &man->reclaim_wq);
> +	}
> +
>  	return 0;
>  }
>  EXPORT_SYMBOL(ttm_bo_init_mm);
> @@ -1620,6 +1665,8 @@ int ttm_bo_device_release(struct ttm_bo_device *bdev)
>  		man = &bdev->man[i];
>  		if (man->has_type) {
>  			man->use_type = false;
> +			drmcgrp_unregister_device_mm(bdev->ddev, i);
> +			cancel_work_sync(&man->reclaim_wq);
>  			if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) {
>  				ret = -EBUSY;
>  				pr_err("DRM memory manager type %d is not clean\n",
> diff --git a/include/drm/drm_cgroup.h b/include/drm/drm_cgroup.h
> index 360c1e6c809f..134d6e5475f3 100644
> --- a/include/drm/drm_cgroup.h
> +++ b/include/drm/drm_cgroup.h
> @@ -5,6 +5,7 @@
>  #define __DRM_CGROUP_H__
>  
>  #include <linux/cgroup_drm.h>
> +#include <linux/workqueue.h>
>  #include <drm/ttm/ttm_bo_api.h>
>  #include <drm/ttm/ttm_bo_driver.h>
>  
> @@ -12,6 +13,9 @@
>  
>  int drmcgrp_register_device(struct drm_device *device);
>  int drmcgrp_unregister_device(struct drm_device *device);
> +void drmcgrp_register_device_mm(struct drm_device *dev, unsigned type,
> +		struct work_struct *wq);
> +void drmcgrp_unregister_device_mm(struct drm_device *dev, unsigned type);
>  bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self,
>  		struct drmcgrp *relative);
>  void drmcgrp_chg_bo_alloc(struct drmcgrp *drmcgrp, struct drm_device *dev,
> @@ -40,6 +44,16 @@ static inline int drmcgrp_unregister_device(struct drm_device *device)
>  	return 0;
>  }
>  
> +static inline void drmcgrp_register_device_mm(struct drm_device *dev,
> +		unsigned type, struct work_struct *wq)
> +{
> +}
> +
> +static inline void drmcgrp_unregister_device_mm(struct drm_device *dev,
> +		unsigned type)
> +{
> +}
> +
>  static inline bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self,
>  		struct drmcgrp *relative)
>  {
> diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
> index 4cbcb41e5aa9..0956ca7888fc 100644
> --- a/include/drm/ttm/ttm_bo_driver.h
> +++ b/include/drm/ttm/ttm_bo_driver.h
> @@ -205,6 +205,8 @@ struct ttm_mem_type_manager {
>  	 * Protected by @move_lock.
>  	 */
>  	struct dma_fence *move;
> +
> +	struct work_struct reclaim_wq;
>  };
>  
>  /**
> diff --git a/kernel/cgroup/drm.c b/kernel/cgroup/drm.c
> index 1ce13db36ce9..985a89e849d3 100644
> --- a/kernel/cgroup/drm.c
> +++ b/kernel/cgroup/drm.c
> @@ -31,6 +31,8 @@ struct drmcgrp_device {
>  	s64			mem_bw_avg_bytes_per_us_default;
>  
>  	s64			mem_highs_default[TTM_PL_PRIV+1];
> +
> +	struct work_struct	*mem_reclaim_wq[TTM_PL_PRIV];
>  };
>  
>  #define DRMCG_CTF_PRIV_SIZE 3
> @@ -793,6 +795,31 @@ int drmcgrp_unregister_device(struct drm_device *dev)
>  }
>  EXPORT_SYMBOL(drmcgrp_unregister_device);
>  
> +void drmcgrp_register_device_mm(struct drm_device *dev, unsigned type,
> +		struct work_struct *wq)
> +{
> +	if (dev == NULL || dev->primary->index > max_minor
> +			|| type >= TTM_PL_PRIV)
> +		return;
> +
> +	mutex_lock(&drmcgrp_mutex);
> +	known_drmcgrp_devs[dev->primary->index]->mem_reclaim_wq[type] = wq;
> +	mutex_unlock(&drmcgrp_mutex);
> +}
> +EXPORT_SYMBOL(drmcgrp_register_device_mm);
> +
> +void drmcgrp_unregister_device_mm(struct drm_device *dev, unsigned type)
> +{
> +	if (dev == NULL || dev->primary->index > max_minor
> +			|| type >= TTM_PL_PRIV)
> +		return;
> +
> +	mutex_lock(&drmcgrp_mutex);
> +	known_drmcgrp_devs[dev->primary->index]->mem_reclaim_wq[type] = NULL;
> +	mutex_unlock(&drmcgrp_mutex);
> +}
> +EXPORT_SYMBOL(drmcgrp_unregister_device_mm);
> +
>  bool drmcgrp_is_self_or_ancestor(struct drmcgrp *self, struct drmcgrp *relative)
>  {
>  	for (; self != NULL; self = parent_drmcgrp(self))
> @@ -1004,6 +1031,12 @@ void drmcgrp_mem_track_move(struct ttm_buffer_object *old_bo, bool evict,
>  
>  		ddr->mem_bw_stats[DRMCGRP_MEM_BW_ATTR_BYTE_CREDIT]
>  			-= move_in_bytes;
> +
> +		if (known_dev->mem_reclaim_wq[new_mem_type] != NULL &&
> +                        ddr->mem_stats[new_mem_type] >
> +				ddr->mem_highs[new_mem_type])
> +			schedule_work(
> +				known_dev->mem_reclaim_wq[new_mem_type]);
>  	}
>  	mutex_unlock(&known_dev->mutex);
>  }
> -- 
> 2.21.0
> 
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

  parent reply	other threads:[~2019-06-26 16:44 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-26 15:05 [RFC PATCH v3 00/11] new cgroup controller for gpu/drm subsystem Kenny Ho
2019-06-26 15:05 ` [RFC PATCH v3 02/11] cgroup: Add mechanism to register DRM devices Kenny Ho
     [not found]   ` <20190626150522.11618-3-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 15:56     ` Daniel Vetter
2019-06-26 20:37       ` Kenny Ho
2019-06-26 21:03         ` Daniel Vetter
     [not found]           ` <CAKMK7uERvn7Ed2trGQShM94Ozp6+x8bsULFyGj9CYWstuzb56A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-26 21:58             ` Kenny Ho
2019-06-26 15:05 ` [RFC PATCH v3 03/11] drm/amdgpu: Register AMD devices for DRM cgroup Kenny Ho
2019-06-26 15:05 ` [RFC PATCH v3 04/11] drm, cgroup: Add total GEM buffer allocation limit Kenny Ho
     [not found]   ` <20190626150522.11618-5-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 16:05     ` Daniel Vetter
     [not found]       ` <20190626160553.GR12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2019-06-26 21:27         ` Kenny Ho
     [not found]           ` <CAOWid-eurCMx1F7ciUwx0e+p=s=NP8=UxQUhhF-hdK-iAna+fA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-26 21:41             ` Daniel Vetter
     [not found]               ` <20190626214113.GA12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2019-06-26 22:41                 ` Kenny Ho
     [not found]                   ` <CAOWid-egYGijS0a6uuG4mPUmOWaPwF-EKokR=LFNJ=5M+akVZw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-27  5:43                     ` Daniel Vetter
2019-06-27 18:42                       ` Kenny Ho
     [not found]                         ` <CAOWid-cT4TQ7HGzcSWjmLGjAW_D1hRrkNguEiV8N+baNiKQm_A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-27 21:24                           ` Daniel Vetter
2019-06-28 18:43                             ` Kenny Ho
     [not found]                               ` <CAOWid-dZQhpKHxYEFn+X+WSep+B66M_LtN6v0=4-uO3ecZ0pcg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-07-02 13:16                                 ` Daniel Vetter
     [not found] ` <20190626150522.11618-1-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 15:05   ` [RFC PATCH v3 01/11] cgroup: Introduce cgroup for drm subsystem Kenny Ho
     [not found]     ` <20190626150522.11618-2-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 15:49       ` Daniel Vetter
2019-06-26 19:35         ` Kenny Ho
     [not found]           ` <CAOWid-dyGwf=e0ikBEQ=bnVM_bC8-FeTOD8fJVMJKUgPv6vtyw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-26 20:12             ` Daniel Vetter
2019-06-26 15:05   ` [RFC PATCH v3 05/11] drm, cgroup: Add peak GEM buffer allocation limit Kenny Ho
2019-06-26 15:05   ` [RFC PATCH v3 06/11] drm, cgroup: Add GEM buffer allocation count stats Kenny Ho
2019-06-26 15:05   ` [RFC PATCH v3 09/11] drm, cgroup: Add per cgroup bw measure and control Kenny Ho
     [not found]     ` <20190626150522.11618-10-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 16:25       ` Daniel Vetter
     [not found]         ` <20190626162554.GU12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2019-06-27  4:34           ` Kenny Ho
     [not found]             ` <CAOWid-dO5QH4wLyN_ztMaoZtLM9yzw-FEMgk3ufbh1ahHJ2vVg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-27  6:11               ` Daniel Vetter
     [not found]                 ` <20190627061153.GD12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2019-06-28 19:49                   ` Kenny Ho
     [not found]                     ` <CAOWid-dCkevUiN27pkwfPketdqS8O+ZGYu8vRMPY2GhXGaVARA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-07-02 13:20                       ` Daniel Vetter
2019-06-26 15:05   ` [RFC PATCH v3 11/11] drm, cgroup: Allow more aggressive memory reclaim Kenny Ho
     [not found]     ` <20190626150522.11618-12-Kenny.Ho-5C7GfCeVMHo@public.gmane.org>
2019-06-26 16:44       ` Daniel Vetter [this message]
2019-06-26 22:52         ` Kenny Ho
2019-06-27  6:15           ` Daniel Vetter
2019-06-26 15:05 ` [RFC PATCH v3 07/11] drm, cgroup: Add TTM buffer allocation stats Kenny Ho
2019-06-26 16:12   ` Daniel Vetter
     [not found]     ` <20190626161254.GS12905-dv86pmgwkMBes7Z6vYuT8azUEOm+Xw19@public.gmane.org>
2019-06-27  4:06       ` Kenny Ho
     [not found]         ` <CAOWid-f3kKnM=4oC5Bba5WW5WNV2MH5PvVamrhO6LBr5ydPJQg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-06-27  6:01           ` Daniel Vetter
2019-06-27 20:17             ` Kenny Ho
2019-06-27 21:33               ` Daniel Vetter
2019-06-28  1:16             ` Welty, Brian
     [not found]               ` <01a6efa8-802c-b8b1-931e-4f0c1c63beca-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2019-06-28  6:53                 ` Daniel Vetter
2019-06-26 15:05 ` [RFC PATCH v3 08/11] drm, cgroup: Add TTM buffer peak usage stats Kenny Ho
2019-06-26 16:16   ` Daniel Vetter
2019-06-26 15:05 ` [RFC PATCH v3 10/11] drm, cgroup: Add soft VRAM limit Kenny Ho
2019-06-27  7:24 ` [RFC PATCH v3 00/11] new cgroup controller for gpu/drm subsystem Daniel Vetter
2019-06-30  5:10   ` Kenny Ho
2019-07-02 13:21     ` Daniel Vetter

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20190626164444.GX12905@phenom.ffwll.local \
    --to=daniel-/w4ywyx8dfk@public.gmane.org \
    --cc=Kenny.Ho-5C7GfCeVMHo@public.gmane.org \
    --cc=alexander.deucher-5C7GfCeVMHo@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    --cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=christian.koenig-5C7GfCeVMHo@public.gmane.org \
    --cc=dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    --cc=joseph.greathouse-5C7GfCeVMHo@public.gmane.org \
    --cc=jsparks-WVYJKLFxKCc@public.gmane.org \
    --cc=lkaplan-WVYJKLFxKCc@public.gmane.org \
    --cc=tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=y2kenny-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    /path/to/YOUR_REPLY

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

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