From 7264f995c1f7ad77db613ed34e13645a8ddf5575 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 20 Jun 2011 13:29:36 +0100 Subject: [PATCH] drm/i915: Prefault all pages for pread and pwrite When using a GTT mapping as a source or destination for the pwrite or pread command respectively, unless the PTEs for the GTT vma had been prepopulated then get_user_pages() would fail with EFAULT. Usually, we only write small amounts of data with pwrite that happened to be conveniently prefaulted by the 2-page fault_in_pages_readable. By prefaulting all pages before we take the struct mutex, we avoid this issue, can stay in the fast path longer and reduce the likelihood of a recursive deadlock. "Fixes" gem_mmap_gtt. References: https://bugs.freedesktop.org/show_bug.cgi?id=38115 Signed-off-by: Chris Wilson --- drivers/gpu/drm/i915/i915_gem.c | 37 +++++++++++++++++++++++++++++++++---- 1 files changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c index a8579c5..a49db69 100644 --- a/drivers/gpu/drm/i915/i915_gem.c +++ b/drivers/gpu/drm/i915/i915_gem.c @@ -560,6 +560,20 @@ out: return ret; } +static int prefault_writeable(unsigned long uaddr, unsigned long len) +{ + /* overflow already checked */ + len += uaddr; + while (uaddr < len) { + if (__put_user(0, (char __user *)uaddr)) + return -EFAULT; + + uaddr += 4096; + } + + return 0; +} + /** * Reads data from the object referenced by handle. * @@ -581,8 +595,7 @@ i915_gem_pread_ioctl(struct drm_device *dev, void *data, args->size)) return -EFAULT; - ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr, - args->size); + ret = prefault_writeable(args->data_ptr, args->size); if (ret) return -EFAULT; @@ -986,6 +999,23 @@ out: return ret; } +static int prefault_readable(unsigned long uaddr, unsigned long len) +{ + volatile char c; + + /* overflow already checked */ + len += uaddr; + while (uaddr < len) { + if (__get_user(c, (const char __user *)uaddr)) + return -EFAULT; + + uaddr += 4096; + } + + return 0; + (void)c; +} + /** * Writes data to the object referenced by handle. * @@ -1007,8 +1037,7 @@ i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, args->size)) return -EFAULT; - ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr, - args->size); + ret = prefault_readable(args->data_ptr, args->size); if (ret) return -EFAULT; -- 1.7.5.4