Skip to contentSkip to conversation

Increase the utilization of Stolen area on VLV

PATCH In review [email protected] 2 participants 13 messages Mar 7, 2014 – Mar 21, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Increase the utilization of Stolen area on VLV

From: Sourab Gupta <[email protected]>

These patches are mainly for increasing the utilization
of stolen memory area on VLV.
Although similar patches were submitted earlier, this time truncation is handled based on Chris and Daniel's input. Chris's patch handles the truncation of purgeable stolen objects.

The allocation from stolen area will be done based on user's input. We have exposed a new libdrm flag which user can use while creating an object to convey driver that direct cpu access to buffer is not required. Based on this flag, driver can decide to allocate the backing storage for this object from stolen mem area.

If we run out of stolen memory when trying to allocate an object, we can try to reap enough purgeable objects to free up enough contiguous free space for the allocation.

Chris Wilson (1):
drm/i915: Truncation logic for Stolen mem objects

Sourab Gupta (2):
drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create
ioctl.
drm/i915: Memory node free handling on truncation of stolen obj

 drivers/gpu/drm/i915/i915_gem.c        |   17 +++--
 drivers/gpu/drm/i915/i915_gem_stolen.c |  112 ++++++++++++++++++++++++++++++--
 include/uapi/drm/i915_drm.h            |    3 +-
 3 files changed, 119 insertions(+), 13 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/3 drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

From: Sourab Gupta <[email protected]>

Adding the flag 'I915_CPU_MAP_NOT_NEEDED' to gem_create ioctl. This is to indicate the driver that direct cpu access to this buffer object is not needed and hence Driver can opt to use Stolen area as a backing store for it. When this flag is set, try to allocate the objects from Stolen area. Fallback to shmem in case of allocation failure.

---
 drivers/gpu/drm/i915/i915_gem.c |   17 ++++++++++++-----
 include/uapi/drm/i915_drm.h     |    3 ++-
 2 files changed, 14 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +12 -5
index 177c207..22b531a 100644
@@ -225,9 +225,11 @@ static int
 i915_gem_create(struct drm_file *file,
 		struct drm_device *dev,
 		uint64_t size,
-		uint32_t *handle_p)
+		uint32_t *handle_p,
+		uint32_t flags)
+
 {
-	struct drm_i915_gem_object *obj;
+	struct drm_i915_gem_object *obj=NULL;
 	int ret;
 	u32 handle;
 
@@ -236,7 +238,12 @@ i915_gem_create(struct drm_file *file,
 		return -EINVAL;
 
 	/* Allocate the new object */
-	obj = i915_gem_alloc_object(dev, size);
+	if (flags & I915_CPU_MAP_NOT_NEEDED)
+		obj = i915_gem_object_create_stolen(dev, size);
+
+	if (obj == NULL)
+		obj = i915_gem_alloc_object(dev, size);
+
 	if (obj == NULL)
 		return -ENOMEM;
 
@@ -259,7 +266,7 @@ i915_gem_dumb_create(struct drm_file *file,
 	args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
 	args->size = args->pitch * args->height;
 	return i915_gem_create(file, dev,
-			       args->size, &args->handle);
+			       args->size, &args->handle, args->flags);
 }
 
 /**
@@ -272,7 +279,7 @@ i915_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct drm_i915_gem_create *args = data;
 
 	return i915_gem_create(file, dev,
-			       args->size, &args->handle);
+			       args->size, &args->handle, args->flags);
 }
 
 static inline int
include/uapi/drm/i915_drm.h +2 -1
index 126bfaa..8625f54 100644
@@ -431,7 +431,8 @@ struct drm_i915_gem_create {
 	 * Object handles are nonzero.
 	 */
 	__u32 handle;
-	__u32 pad;
+	__u32 flags;
+#define I915_CPU_MAP_NOT_NEEDED 0x1
 };
 
 struct drm_i915_gem_pread {
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 8 lines · On Fri, Mar 07, 2014 at 03:29:25PM +0530, [email protected] wrote:

From: Sourab Gupta <[email protected]>

Adding the flag 'I915_CPU_MAP_NOT_NEEDED' to gem_create ioctl. This is to indicate the driver that direct cpu access to this buffer object is not needed and hence Driver can opt to use Stolen area as a backing store for it. When this flag is set, try to allocate the objects from Stolen area. Fallback to shmem in case of allocation failure.

Besides the issue of
http://blog.ffwll.ch/2013/11/botching-up-ioctls.html, this has an information leak that we are not clearing the stolen pages. Danny got extremely fussy over that when I sent this code almost 9 months ago. -Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#3 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Hi Chris,

For the issue of clearing of stolen pages, we have a patch which does memset of stolen objs after allocation, before handing the objs back over to userspace. We can apply this patch and send it in next version.

Regards,
Sourab

-----Original Message-----
From: Chris Wilson [mailto:[email protected]]
Sent: Friday, March 07, 2014 3:36 PM
To: Gupta, Sourab
Cc: [email protected]; Vetter, Daniel; Wilson, Chris; Goel, Akash
Subject: Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 8 lines · On Fri, Mar 07, 2014 at 03:29:25PM +0530, [email protected] wrote:

From: Sourab Gupta <[email protected]>

Adding the flag 'I915_CPU_MAP_NOT_NEEDED' to gem_create ioctl. This is to indicate the driver that direct cpu access to this buffer object is not needed and hence Driver can opt to use Stolen area as a backing store for it. When this flag is set, try to allocate the objects from Stolen area. Fallback to shmem in case of allocation failure.

Besides the issue of
http://blog.ffwll.ch/2013/11/botching-up-ioctls.html, this has an information leak that we are not clearing the stolen pages. Danny got extremely fussy over that when I sent this code almost 9 months ago. -Chris

--
Chris Wilson, Intel Open Source Technology Centre

#4 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Hi Chris,

For the issue mentioned by you ( regarding botching up ioctls), we understand that this is related to the compatibility between the older/newer versions of driver/userspace.
In our old implementation, the 'pad' field was replaced with 'flags' in the ioctl structure. This would have led to the erroneous behavior when the new userspace is communicating with old driver.

So, we are planning to mitigate it using following approach:
1) Extend the ioctl structure with 'flags' field at the end of existing fields
2) Add a driver feature flag, so that newer userspace can figure out whether kernel supports the new
version of ioctl. Add checks in libdrm corresponding to the same.
3) In the driver, check the passed in size for the ioctl structure and zero-extend any mismatch( which
happens in case of older userspace calling newer kernel).
4) In case of newer userspace calling older driver (wherein feature is not supported), we have a few choices:
a) The userspace will fallback to using the old ioctl structure. Driver will have the new version of the ioctl structure. Since the libdrm shared header files will also now have new ioctl structure definition, we'll have to keep an old definition of the ioctl structure privately in order to send it in the ioctl.
b) Or, we may keep the old ioctl structure as it is in the driver; and define a new structure in driver which will be used now. Then, libdrm will also use this new structure (if feature is supported) or old structure (feature not supported)
c) Or, we may fail the call altogether, if the newer userspace is trying to communicate with older driver. We don't need to keep the two different version of ioctl structures in this case. But this may not be the desired behaviour

Can you provide your opinion regarding the above approaches.

Regards,
Sourab

-----Original Message-----
From: Gupta, Sourab
Sent: Friday, March 07, 2014 4:26 PM
To: Chris Wilson
Cc: [email protected]; Wilson, Chris; Goel, Akash; [email protected]
Subject: RE: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Hi Chris,

For the issue of clearing of stolen pages, we have a patch which does memset of stolen objs after allocation, before handing the objs back over to userspace. We can apply this patch and send it in next version.

Regards,
Sourab

-----Original Message-----
From: Chris Wilson [mailto:[email protected]]
Sent: Friday, March 07, 2014 3:36 PM
To: Gupta, Sourab
Cc: [email protected]; Vetter, Daniel; Wilson, Chris; Goel, Akash
Subject: Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 8 lines · On Fri, Mar 07, 2014 at 03:29:25PM +0530, [email protected] wrote:

From: Sourab Gupta <[email protected]>

Adding the flag 'I915_CPU_MAP_NOT_NEEDED' to gem_create ioctl. This is to indicate the driver that direct cpu access to this buffer object is not needed and hence Driver can opt to use Stolen area as a backing store for it. When this flag is set, try to allocate the objects from Stolen area. Fallback to shmem in case of allocation failure.

Besides the issue of
http://blog.ffwll.ch/2013/11/botching-up-ioctls.html, this has an information leak that we are not clearing the stolen pages. Danny got extremely fussy over that when I sent this code almost 9 months ago. -Chris

--
Chris Wilson, Intel Open Source Technology Centre

#5 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 7 lines · Sourab

Hi Chris,

For the issue mentioned by you ( regarding botching up ioctls), we understand that this is related to the compatibility between the older/newer versions of driver/userspace.
In our old implementation, the 'pad' field was replaced with 'flags' in the ioctl structure. This would have led to the erroneous behavior when the new userspace is communicating with old driver.

You missed the important point that there is no guarrantee that current userspace is not stuffing garbage into the pad field. It is.

You have to go with a new ioctl. So you may as well design one that tries to fulfil today's varied needs for a constructor. For example, here's one I prepared earlier,
http://cgit.freedesktop.org/~ickle/linux-2.6/commit/?h=create2&id=401fa740adcaf252d0149cdd63d5fdf5e3969907
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#6 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 9 lines · Chris Wilson
Quoted 7 lines · Sourab

Hi Chris,

For the issue mentioned by you ( regarding botching up ioctls), we understand that this is related to the compatibility between the older/newer versions of driver/userspace.
In our old implementation, the 'pad' field was replaced with 'flags' in the ioctl structure. This would have led to the erroneous behavior when the new userspace is communicating with old driver.

Quoted 9 lines

You missed the important point that there is no guarrantee that current userspace is not stuffing garbage into the pad field. It is.

You have to go with a new ioctl. So you may as well design one that tries to fulfil today's varied needs for a constructor. For example, here's one I prepared earlier,
http://cgit.freedesktop.org/~ickle/linux-2.6/commit/?h=create2&id=401fa740adcaf252d0149cdd63d5fdf5e3969907
-Chris

Hi Chris,

We went through this patch and this fulfills the requirements from the stolen mem perspective, and this looks like a replacement of the first patch we have submitted.
This patch seems to be in your private branch. When do you plan to merge this patch with drm-intel branch?
Have you also prepared the corresponding libdrm side patches? If so, can you share those also. Actually, the igt patch which we submitted earlier would also need to be modified according to the libdrm changes.

Also,
When do you plan to merge the second and third patch in the series (truncation of stolen mem objs) as they seem to be fine to us.

Regards,
Sourab

#7 lore raw

Re: [PATCH 1/3] drm/i915: Added a new 'I915_CPU_MAP_NOT_NEEDED' flag to gem_create ioctl.

Quoted 31 lines · sourab gupta
Quoted 9 lines · Chris Wilson
Quoted 7 lines · Sourab

Hi Chris,

For the issue mentioned by you ( regarding botching up ioctls), we understand that this is related to the compatibility between the older/newer versions of driver/userspace.
In our old implementation, the 'pad' field was replaced with 'flags' in the ioctl structure. This would have led to the erroneous behavior when the new userspace is communicating with old driver.

Quoted 9 lines

You missed the important point that there is no guarrantee that current userspace is not stuffing garbage into the pad field. It is.

You have to go with a new ioctl. So you may as well design one that tries to fulfil today's varied needs for a constructor. For example, here's one I prepared earlier,
http://cgit.freedesktop.org/~ickle/linux-2.6/commit/?h=create2&id=401fa740adcaf252d0149cdd63d5fdf5e3969907
-Chris

Hi Chris,

We went through this patch and this fulfills the requirements from the stolen mem perspective, and this looks like a replacement of the first patch we have submitted.
This patch seems to be in your private branch. When do you plan to merge this patch with drm-intel branch?
Have you also prepared the corresponding libdrm side patches? If so, can you share those also. Actually, the igt patch which we submitted earlier would also need to be modified according to the libdrm changes.

Quoted 7 lines

Also,
When do you plan to merge the second and third patch in the series (truncation of stolen mem objs) as they seem to be fine to us.

Regards,
Sourab

Hi Chris,
We came across an earlier patch submitted by you regarding the ioctl for user specified placement.
(http://lists.freedesktop.org/archives/intel-gfx/2013-July/029843.html)

As we understand, there were issues regarding the dma-buf support regarding this patch.
Can you please tell us, whether it is planned to be taken up at a later date (with/without dma-buf support).

Also, if the zeroing out of memory is required, we can provide an additional patch on top of our earlier patches in order to clear the memory gotten from stolen mem this way.

Regards,
Sourab

#8 lore raw

PATCH 2/3 drm/i915: Truncation logic for Stolen mem objects

In reply to [email protected]

From: Chris Wilson <[email protected]>

If we run out of stolen memory when trying to allocate an object, see if we can reap enough purgeable objects to free up enough contiguous free space for the allocation. This is in principle very much like evicting objects to free up enough contiguous space in the vma when binding a new object - and you will be forgiven for thinking that the code looks very similar.

At the moment, we do not allow userspace to allocate objects in stolen, so there is neither the memory pressure to trigger stolen eviction nor any purgeable objects inside the stolen arena. However, this will change in the near future, and so better management and defragmentation of stolen memory will become a real issue.

---
 drivers/gpu/drm/i915/i915_gem_stolen.c |  114 ++++++++++++++++++++++++++++++--
 1 file changed, 107 insertions(+), 7 deletions(-)
drivers/gpu/drm/i915/i915_gem_stolen.c +107 -7
index d58b4e2..d092bf3 100644
@@ -322,12 +322,24 @@ cleanup:
 	return NULL;
 }
 
-struct drm_i915_gem_object *
-i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
+static bool mark_free(struct drm_i915_gem_object *obj, struct list_head
+*unwind) {
+	if (obj->stolen == NULL)
+		return false;
+
+	if (obj->madv != I915_MADV_DONTNEED)
+		return false;
+
+	list_add(&obj->obj_exec_link, unwind);
+	return drm_mm_scan_add_block(obj->stolen);
+}
+
+static struct drm_mm_node *stolen_alloc(struct drm_i915_private
+*dev_priv, u32 size)
 {
-	struct drm_i915_private *dev_priv = dev->dev_private;
-	struct drm_i915_gem_object *obj;
 	struct drm_mm_node *stolen;
+	struct drm_i915_gem_object *obj;
+	struct list_head unwind, evict;
 	int ret;
 
 	if (!drm_mm_initialized(&dev_priv->mm.stolen))
@@ -343,11 +355,99 @@ i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
 
 	ret = drm_mm_insert_node(&dev_priv->mm.stolen, stolen, size,
 				 4096, DRM_MM_SEARCH_DEFAULT);
-	if (ret) {
-		kfree(stolen);
-		return NULL;
+	if (ret == 0)
+		return stolen;
+
+	/* No more stolen memory available, or too fragmented.
+	 * Try evicting purgeable objects and search again.
+	 */
+
+	drm_mm_init_scan(&dev_priv->mm.stolen, size, 4096, 0);
+	INIT_LIST_HEAD(&unwind);
+
+	list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
+		if (mark_free(obj, &unwind))
+			goto found;
+
+	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
+		if (mark_free(obj, &unwind))
+			goto found;
+
+found:
+	INIT_LIST_HEAD(&evict);
+	while (!list_empty(&unwind)) {
+		obj = list_first_entry(&unwind,
+				       struct drm_i915_gem_object,
+				       obj_exec_link);
+		list_del_init(&obj->obj_exec_link);
+
+		if (drm_mm_scan_remove_block(obj->stolen)) {
+			list_add(&obj->obj_exec_link, &evict);
+			drm_gem_object_reference(&obj->base);
+		}
 	}
 
+	ret = 0;
+	while (!list_empty(&evict)) {
+		obj = list_first_entry(&evict,
+				       struct drm_i915_gem_object,
+				       obj_exec_link);
+		list_del_init(&obj->obj_exec_link);
+
+		if (ret == 0) {
+			struct i915_vma *vma, *vma_next;
+
+			list_for_each_entry_safe(vma, vma_next,
+						 &obj->vma_list,
+						 vma_link)
+				if (i915_vma_unbind(vma))
+					break;
+
+			/* Stolen pins its pages to prevent the
+			 * normal shrinker from processing stolen
+			 * objects.
+			 */
+			i915_gem_object_unpin_pages(obj);
+
+			ret = i915_gem_object_put_pages(obj);
+			if (ret == 0) {
+				obj->madv = __I915_MADV_PURGED;
+
+				kfree(obj->stolen);
+				obj->stolen = NULL;
+			} else
+				i915_gem_object_pin_pages(obj);
+		}
+
+		drm_gem_object_unreference(&obj->base);
+	}
+
+	if (ret == 0)
+		ret = drm_mm_insert_node(&dev_priv->mm.stolen, stolen, size,
+					 4096, DRM_MM_SEARCH_DEFAULT);
+	if (ret == 0)
+		return stolen;
+
+	kfree(stolen);
+	return NULL;
+}
+
+struct drm_i915_gem_object *
+i915_gem_object_create_stolen(struct drm_device *dev, u32 size)
+{
+	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_i915_gem_object *obj;
+	struct drm_mm_node *stolen;
+
+	if (!drm_mm_initialized(&dev_priv->mm.stolen))
+		return NULL;
+
+	DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
+
+	stolen = stolen_alloc(dev_priv, size);
+	if (stolen == NULL)
+		return NULL;
+
 	obj = _i915_gem_object_create_stolen(dev, stolen);
 	if (obj)
 		return obj;
Signature · 1 line
1.7.9.5
#9 lore raw

PATCH 3/3 drm/i915: Memory node free handling on truncation of stolen obj

In reply to [email protected]

From: Sourab Gupta <[email protected]>

While we are freeing the purgeable stolen object, the memory node has to be freed so as to make space for the new object.

---
 drivers/gpu/drm/i915/i915_gem_stolen.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/i915/i915_gem_stolen.c +1 -3
index d092bf3..ae12074 100644
@@ -411,10 +411,8 @@ found:
 
 			ret = i915_gem_object_put_pages(obj);
 			if (ret == 0) {
+				i915_gem_object_release_stolen(obj);
 				obj->madv = __I915_MADV_PURGED;
-
-				kfree(obj->stolen);
-				obj->stolen = NULL;
 			} else
 				i915_gem_object_pin_pages(obj);
 		}
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [PATCH 3/3] drm/i915: Memory node free handling on truncation of stolen obj

Quoted 5 lines · On Fri, Mar 07, 2014 at 03:29:27PM +0530, [email protected] wrote:

From: Sourab Gupta <[email protected]>

While we are freeing the purgeable stolen object, the memory node has to be freed so as to make space for the new object.

The node has already been removed from the stolen mm, so this should be triggering a WARN.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#11 lore raw

Re: [PATCH 3/3] drm/i915: Memory node free handling on truncation of stolen obj

Hi Chris,

Not able to locate where the node is removed. I looked primarily around the put_pages function but the node doesn't seem to be removed there. Can you please let us know where the node is removed

Regards,
Sourab

-----Original Message-----
From: Chris Wilson [mailto:[email protected]]
Sent: Friday, March 07, 2014 3:32 PM
To: Gupta, Sourab
Cc: [email protected]; Vetter, Daniel; Wilson, Chris
Subject: Re: [Intel-gfx] [PATCH 3/3] drm/i915: Memory node free handling on truncation of stolen obj

Quoted 5 lines · On Fri, Mar 07, 2014 at 03:29:27PM +0530, [email protected] wrote:

From: Sourab Gupta <[email protected]>

While we are freeing the purgeable stolen object, the memory node has to be freed so as to make space for the new object.

The node has already been removed from the stolen mm, so this should be triggering a WARN. -Chris

--
Chris Wilson, Intel Open Source Technology Centre

#12 lore raw

Re: [PATCH 3/3] drm/i915: Memory node free handling on truncation of stolen obj

Quoted 4 lines · Sourab

Hi Chris,

Not able to locate where the node is removed. I looked primarily around the put_pages function but the node doesn't seem to be removed there. Can you please let us know where the node is removed

My mistake, I thought it would be removed by drm_mm_scan_remove_block(). -Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre