Revert "drm/i915/gem: Drop relocation slowpath"
Subsystems (5)
PATCH 1/22 Revert "drm/i915/gem: Drop relocation slowpath"
This reverts commit 7dc8f1143778 ("drm/i915/gem: Drop relocation slowpath"). We need the slowpath relocation for taking ww-mutex inside the page fault handler, and we will take this mutex when pinning all objects.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 239 +++++++++++++++++- 1 file changed, 235 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +235 -4
@@ -1480,7 +1480,9 @@ static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev)* we would try to acquire the struct mutex again. Obviously * this is bad and so lockdep complains vehemently. */ - copied =__copy_from_user (r, urelocs, count *sizeof (r[0 ])); +pagefault_disable (); + copied =__copy_from_user_inatomic (r, urelocs, count *sizeof (r[0 ])); +pagefault_enable ();if (unlikely (copied)) { remain = -EFAULT ; goto out;
@@ -1530,6 +1532,236 @@ static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev)return remain; } +static int +eb_relocate_vma_slow (struct i915_execbuffer *eb,struct eb_vma *ev) +{ +const struct drm_i915_gem_exec_object2 *entry = ev->exec; +struct drm_i915_gem_relocation_entry *relocs = +u64_to_ptr (typeof (*relocs), entry->relocs_ptr); +unsigned int i; +int err; + +for (i =0 ; i < entry->relocation_count; i++) { +u64 offset =eb_relocate_entry (eb, ev, &relocs[i]); + +if ((s64 )offset <0 ) { + err = (int )offset; + goto err; + } + } + err =0 ; +err: +reloc_cache_reset (&eb->reloc_cache); +return err; +} + +static int check_relocations (const struct drm_i915_gem_exec_object2 *entry) +{ +const char __user *addr, *end; +unsigned long size; +char __maybe_unused c; + + size = entry->relocation_count; +if (size ==0 ) +return 0 ; + +if (size >N_RELOC (ULONG_MAX )) +return -EINVAL ; + + addr =u64_to_user_ptr (entry->relocs_ptr); + size *=sizeof (struct drm_i915_gem_relocation_entry ); +if (!access_ok (addr, size)) +return -EFAULT ; + + end = addr + size; +for (; addr < end; addr +=PAGE_SIZE ) { +int err =__get_user (c, addr); +if (err) +return err; + } +return __get_user (c, end -1 ); +} + +static int eb_copy_relocations (const struct i915_execbuffer *eb) +{ +struct drm_i915_gem_relocation_entry *relocs; +const unsigned int count = eb->buffer_count; +unsigned int i; +int err; + +for (i =0 ; i < count; i++) { +const unsigned int nreloc = eb->exec[i].relocation_count; +struct drm_i915_gem_relocation_entry __user *urelocs; +unsigned long size; +unsigned long copied; + +if (nreloc ==0 ) +continue ; + + err =check_relocations (&eb->exec[i]); +if (err) + goto err; + + urelocs =u64_to_user_ptr (eb->exec[i].relocs_ptr); + size = nreloc *sizeof (*relocs); + + relocs =kvmalloc_array (size,1 ,GFP_KERNEL ); +if (!relocs) { + err = -ENOMEM ; + goto err; + } + +/* copy_from_user is limited to < 4GiB */ + copied =0 ; +do { +unsigned int len = +min_t (u64,BIT_ULL (31 ), size - copied); + +if (__copy_from_user ((char *)relocs + copied, + (char __user *)urelocs + copied, + len)) + goto end; + + copied += len; + }while (copied < size); + +/* +* As we do not update the known relocation offsets after +* relocating (due to the complexities in lock handling), +* we need to mark them as invalid now so that we force the +* relocation processing next time. Just in case the target +* object is evicted and then rebound into its old +* presumed_offset before the next execbuffer - if that +* happened we would make the mistake of assuming that the +* relocations were valid. +*/ +if (!user_access_begin (urelocs, size)) + goto end; + +for (copied =0 ; copied < nreloc; copied++) +unsafe_put_user (-1 , + &urelocs[copied].presumed_offset, + end_user); +user_access_end (); + + eb->exec[i].relocs_ptr = (uintptr_t )relocs; + } + +return 0 ; + +end_user: +user_access_end (); +end: +kvfree (relocs); + err = -EFAULT ; +err: +while (i--) { + relocs =u64_to_ptr (typeof (*relocs), eb->exec[i].relocs_ptr); +if (eb->exec[i].relocation_count) +kvfree (relocs); + } +return err; +} + +static int eb_prefault_relocations (const struct i915_execbuffer *eb) +{ +const unsigned int count = eb->buffer_count; +unsigned int i; + +for (i =0 ; i < count; i++) { +int err; + + err =check_relocations (&eb->exec[i]); +if (err) +return err; + } + +return 0 ; +} + +static noinline int eb_relocate_slow(struct i915_execbuffer *eb) +{ +bool have_copy = false; +struct eb_vma *ev; +int err =0 ; + +repeat: +if (signal_pending (current)) { + err = -ERESTARTSYS ; + goto out; + } + +/* +* We take 3 passes through the slowpatch. +* +* 1 - we try to just prefault all the user relocation entries and +* then attempt to reuse the atomic pagefault disabled fast path again. +* +* 2 - we copy the user entries to a local buffer here outside of the +* local and allow ourselves to wait upon any rendering before +* relocations +* +* 3 - we already have a local copy of the relocation entries, but +* were interrupted (EAGAIN) whilst waiting for the objects, try again. +*/ +if (!err) { + err =eb_prefault_relocations (eb); + }else if (!have_copy) { + err =eb_copy_relocations (eb); + have_copy = err ==0 ; + }else { +cond_resched (); + err =0 ; + } +if (err) + goto out; + +list_for_each_entry (ev, &eb->relocs, reloc_link) { +if (!have_copy) { +pagefault_disable (); + err =eb_relocate_vma (eb, ev); +pagefault_enable (); +if (err) + goto repeat; + }else { + err =eb_relocate_vma_slow (eb, ev); +if (err) + goto err; + } + } + +/* +* Leave the user relocations as are, this is the painfully slow path, +* and we want to avoid the complication of dropping the lock whilst +* having buffers reserved in the aperture and so causing spurious +* ENOSPC for random operations. +*/ + +err: +if (err == -EAGAIN ) + goto repeat; + +out: +if (have_copy) { +const unsigned int count = eb->buffer_count; +unsigned int i; + +for (i =0 ; i < count; i++) { +const struct drm_i915_gem_exec_object2 *entry = + &eb->exec[i]; +struct drm_i915_gem_relocation_entry *relocs; + +if (!entry->relocation_count) +continue ; + + relocs =u64_to_ptr (typeof (*relocs), entry->relocs_ptr); +kvfree (relocs); + } + } + +return err; +} +static int eb_relocate (struct i915_execbuffer *eb) {int err;
@@ -1549,9 +1781,8 @@ static int eb_relocate(struct i915_execbuffer *eb)struct eb_vma *ev;list_for_each_entry (ev, &eb->relocs, reloc_link) { - err =eb_relocate_vma (eb, ev); -if (err) -return err; +if (eb_relocate_vma (eb, ev)) +return eb_relocate_slow (eb); } }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 2/22 perf/core: Only copy-to-user after completely unlocking all locks. (CI test)
We inadvertently create a dependency on mmap_sem with a whole chain.
This breaks any user who wants to take a lock and call rcu_barrier(), while also taking that lock inside mmap_sem:
<4> [604.892532] ======================================================
<4> [604.892534] WARNING: possible circular locking dependency detected
<4> [604.892536] 5.6.0-rc7-CI-Patchwork_17096+ #1 Tainted: G U
<4> [604.892537] ------------------------------------------------------
<4> [604.892538] kms_frontbuffer/2595 is trying to acquire lock:
<4> [604.892540] ffffffff8264a558 (rcu_state.barrier_mutex){+.+.}, at: rcu_barrier+0x23/0x190
<4> [604.892547]
but task is already holding lock:
<4> [604.892547] ffff888484716050 (reservation_ww_class_mutex){+.+.}, at: i915_gem_object_pin_to_display_plane+0x89/0x270 [i915] <4> [604.892592]
which lock already depends on the new lock.
<4> [604.892593]
the existing dependency chain (in reverse order) is:
<4> [604.892594]
-> #6 (reservation_ww_class_mutex){+.+.}:
<4> [604.892597] __ww_mutex_lock.constprop.15+0xc3/0x1090
<4> [604.892598] ww_mutex_lock+0x39/0x70
<4> [604.892600] dma_resv_lockdep+0x10e/0x1f5
<4> [604.892602] do_one_initcall+0x58/0x300
<4> [604.892604] kernel_init_freeable+0x17b/0x1dc
<4> [604.892605] kernel_init+0x5/0x100
<4> [604.892606] ret_from_fork+0x24/0x50
<4> [604.892607]
-> #5 (reservation_ww_class_acquire){+.+.}:
<4> [604.892609] dma_resv_lockdep+0xec/0x1f5
<4> [604.892610] do_one_initcall+0x58/0x300
<4> [604.892610] kernel_init_freeable+0x17b/0x1dc
<4> [604.892611] kernel_init+0x5/0x100
<4> [604.892612] ret_from_fork+0x24/0x50
<4> [604.892613]
-> #4 (&mm->mmap_sem#2){++++}:
<4> [604.892615] __might_fault+0x63/0x90
<4> [604.892617] _copy_to_user+0x1e/0x80
<4> [604.892619] perf_read+0x200/0x2b0
<4> [604.892621] vfs_read+0x96/0x160
<4> [604.892622] ksys_read+0x9f/0xe0
<4> [604.892623] do_syscall_64+0x4f/0x220
<4> [604.892624] entry_SYSCALL_64_after_hwframe+0x49/0xbe
<4> [604.892625]
-> #3 (&cpuctx_mutex){+.+.}:
<4> [604.892626] __mutex_lock+0x9a/0x9c0
<4> [604.892627] perf_event_init_cpu+0xa4/0x140
<4> [604.892629] perf_event_init+0x19d/0x1cd
<4> [604.892630] start_kernel+0x362/0x4e4
<4> [604.892631] secondary_startup_64+0xa4/0xb0
<4> [604.892631]
-> #2 (pmus_lock){+.+.}:
<4> [604.892633] __mutex_lock+0x9a/0x9c0
<4> [604.892633] perf_event_init_cpu+0x6b/0x140
<4> [604.892635] cpuhp_invoke_callback+0x9b/0x9d0
<4> [604.892636] _cpu_up+0xa2/0x140
<4> [604.892637] do_cpu_up+0x61/0xa0
<4> [604.892639] smp_init+0x57/0x96
<4> [604.892639] kernel_init_freeable+0x87/0x1dc
<4> [604.892640] kernel_init+0x5/0x100
<4> [604.892642] ret_from_fork+0x24/0x50
<4> [604.892642]
-> #1 (cpu_hotplug_lock.rw_sem){++++}:
<4> [604.892643] cpus_read_lock+0x34/0xd0
<4> [604.892644] rcu_barrier+0xaa/0x190
<4> [604.892645] kernel_init+0x21/0x100
<4> [604.892647] ret_from_fork+0x24/0x50
<4> [604.892647]
-> #0 (rcu_state.barrier_mutex){+.+.}:
<4> [604.892649] __lock_acquire+0x1328/0x15d0
<4> [604.892650] lock_acquire+0xa7/0x1c0
<4> [604.892651] __mutex_lock+0x9a/0x9c0
<4> [604.892652] rcu_barrier+0x23/0x190
<4> [604.892680] i915_gem_object_unbind+0x29d/0x3f0 [i915]
<4> [604.892707] i915_gem_object_pin_to_display_plane+0x141/0x270 [i915]
<4> [604.892737] intel_pin_and_fence_fb_obj+0xec/0x1f0 [i915]
<4> [604.892767] intel_plane_pin_fb+0x3f/0xd0 [i915]
<4> [604.892797] intel_prepare_plane_fb+0x13b/0x5c0 [i915]
<4> [604.892798] drm_atomic_helper_prepare_planes+0x85/0x110
<4> [604.892827] intel_atomic_commit+0xda/0x390 [i915]
<4> [604.892828] drm_atomic_helper_set_config+0x57/0xa0
<4> [604.892830] drm_mode_setcrtc+0x1c4/0x720
<4> [604.892830] drm_ioctl_kernel+0xb0/0xf0
<4> [604.892831] drm_ioctl+0x2e1/0x390
<4> [604.892833] ksys_ioctl+0x7b/0x90
<4> [604.892835] __x64_sys_ioctl+0x11/0x20
<4> [604.892835] do_syscall_64+0x4f/0x220
<4> [604.892836] entry_SYSCALL_64_after_hwframe+0x49/0xbe<4> [604.892837]
--- kernel/events/core.c | 59 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-)
kernel/events/core.c +31 -28
@@ -4926,20 +4926,20 @@ static int __perf_read_group_add(struct perf_event *leader, }static int perf_read_group (struct perf_event *event, -u64 read_format,char __user *buf) +u64 read_format,char __user *buf, +u64 **values) {struct perf_event *leader = event->group_leader, *child;struct perf_event_context *ctx = leader->ctx;int ret; -u64 *values;lockdep_assert_held (&ctx->mutex); - values =kzalloc (event->read_size,GFP_KERNEL ); -if (!values) + *values =kzalloc (event->read_size,GFP_KERNEL ); +if (!*values)return -ENOMEM ; - values[0 ] =1 + leader->nr_siblings; + *values[0 ] =1 + leader->nr_siblings;/* * By locking the child_mutex of the leader we effectively
@@ -4947,47 +4947,42 @@ static int perf_read_group(struct perf_event *event,*/ mutex_lock (&leader->child_mutex); - ret =__perf_read_group_add (leader, read_format, values); + ret =__perf_read_group_add (leader, read_format, *values);if (ret) goto unlock;list_for_each_entry (child, &leader->child_list, child_list) { - ret =__perf_read_group_add (child, read_format, values); + ret =__perf_read_group_add (child, read_format, *values);if (ret) goto unlock; } -mutex_unlock (&leader->child_mutex); - ret = event->read_size; -if (copy_to_user (buf, values, event->read_size)) - ret = -EFAULT ; - goto out; - unlock:mutex_unlock (&leader->child_mutex); -out: -kfree (values); +if (ret <0 ) +kfree (*values);return ret; }static int perf_read_one (struct perf_event *event, -u64 read_format,char __user *buf) +u64 read_format,char __user *buf, +u64 **values) {u64 enabled, running; -u64 values[4 ];int n =0 ; - values[n++] =__perf_event_read_value (event, &enabled, &running); + *values =kzalloc (sizeof (**values) *4 ,GFP_KERNEL ); +if (!*values) +return -ENOMEM ; + + *values[n++] =__perf_event_read_value (event, &enabled, &running);if (read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) - values[n++] = enabled; + *values[n++] = enabled;if (read_format &PERF_FORMAT_TOTAL_TIME_RUNNING ) - values[n++] = running; + *values[n++] = running;if (read_format &PERF_FORMAT_ID ) - values[n++] =primary_event_id (event); - -if (copy_to_user (buf, values, n *sizeof (u64))) -return -EFAULT ; + *values[n++] =primary_event_id (event);return n *sizeof (u64); }
@@ -5009,7 +5004,8 @@ static bool is_event_hup(struct perf_event *event)* Read the performance event - simple non blocking version for now */ static ssize_t -__perf_read (struct perf_event *event,char __user *buf,size_t count) +__perf_read (struct perf_event *event,char __user *buf, +size_t count,u64 **values) {u64 read_format = event->attr.read_format;int ret;
@@ -5027,9 +5023,9 @@ __perf_read(struct perf_event *event, char __user *buf, size_t count)WARN_ON_ONCE (event->ctx->parent_ctx);if (read_format &PERF_FORMAT_GROUP ) - ret =perf_read_group (event, read_format, buf); + ret =perf_read_group (event, read_format, buf, values);else - ret =perf_read_one (event, read_format, buf); + ret =perf_read_one (event, read_format, buf, values);return ret; }
@@ -5039,6 +5035,7 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {struct perf_event *event = file->private_data;struct perf_event_context *ctx; +u64 *values;int ret; ret =security_perf_event_read (event);
@@ -5046,9 +5043,15 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)return ret; ctx =perf_event_ctx_lock (event); - ret =__perf_read (event, buf, count); + ret =__perf_read (event, buf, count, &values);perf_event_ctx_unlock (event, ctx); +if (ret >0 ) { +if (copy_to_user (buf, values, ret)) + ret = -EFAULT ; +kfree (values); + } +return ret; }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 3/22 drm/i915: Add an implementation for i915_gem_ww_ctx locking, v2.
In reply to Maarten Lankhorsti915_gem_ww_ctx is used to lock all gem bo's for pinning and memory eviction. We don't use it yet, but lets start adding the definition first.
To use it, we have to pass a non-NULL ww to gem_object_lock, and don't unlock directly. It is done in i915_gem_ww_ctx_fini.
Changes since v1:
- Change ww_ctx and obj order in locking functions (Jonas Lahtinen)
--- drivers/gpu/drm/i915/display/intel_display.c | 4 +- .../gpu/drm/i915/gem/i915_gem_client_blt.c | 2 +- drivers/gpu/drm/i915/gem/i915_gem_context.c | 2 +- drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c | 4 +- drivers/gpu/drm/i915/gem/i915_gem_domain.c | 10 ++-- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 4 +- drivers/gpu/drm/i915/gem/i915_gem_object.c | 2 +- drivers/gpu/drm/i915/gem/i915_gem_object.h | 38 +++++++++++--- .../gpu/drm/i915/gem/i915_gem_object_blt.c | 2 +- .../gpu/drm/i915/gem/i915_gem_object_types.h | 9 ++++ drivers/gpu/drm/i915/gem/i915_gem_pm.c | 2 +- drivers/gpu/drm/i915/gem/i915_gem_tiling.c | 2 +- .../gpu/drm/i915/gem/selftests/huge_pages.c | 2 +- .../i915/gem/selftests/i915_gem_client_blt.c | 2 +- .../i915/gem/selftests/i915_gem_coherency.c | 10 ++-- .../drm/i915/gem/selftests/i915_gem_context.c | 4 +- .../drm/i915/gem/selftests/i915_gem_mman.c | 4 +- .../drm/i915/gem/selftests/i915_gem_phys.c | 2 +- drivers/gpu/drm/i915/gt/intel_gt.c | 2 +- .../gpu/drm/i915/gt/selftest_workarounds.c | 2 +- drivers/gpu/drm/i915/gvt/cmd_parser.c | 2 +- drivers/gpu/drm/i915/i915_gem.c | 52 +++++++++++++++++-- drivers/gpu/drm/i915/i915_gem.h | 11 ++++ drivers/gpu/drm/i915/selftests/i915_gem.c | 41 +++++++++++++++ drivers/gpu/drm/i915/selftests/i915_vma.c | 2 +- .../drm/i915/selftests/intel_memory_region.c | 2 +- 26 files changed, 175 insertions(+), 44 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +2 -2
@@ -2303,7 +2303,7 @@ intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,void intel_unpin_fb_vma (struct i915_vma *vma,unsigned long flags) { -i915_gem_object_lock (vma->obj); +i915_gem_object_lock (vma->obj,NULL );if (flags &PLANE_HAS_FENCE )i915_vma_unpin_fence (vma);i915_gem_object_unpin_from_display_plane (vma);
@@ -17047,7 +17047,7 @@ static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,if (!intel_fb->frontbuffer)return -ENOMEM ; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); tiling =i915_gem_object_get_tiling (obj); stride =i915_gem_object_get_stride (obj);i915_gem_object_unlock (obj);
drivers/gpu/drm/i915/gem/i915_gem_client_blt.c +1 -1
@@ -287,7 +287,7 @@ int i915_gem_schedule_fill_pages_blt(struct drm_i915_gem_object *obj,dma_fence_init (&work->dma, &clear_pages_work_ops, &fence_lock,0 ,0 );i915_sw_fence_init (&work->wait, clear_pages_work_notify); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err = i915_sw_fence_await_reservation(&work->wait, obj->base.resv, NULL, true, I915_FENCE_TIMEOUT,
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -1
@@ -113,7 +113,7 @@ static void lut_close(struct i915_gem_context *ctx)continue ;rcu_read_unlock (); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );list_for_each_entry (lut, &obj->lut_list, obj_link) {if (lut->ctx != ctx)continue ;
drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c +2 -2
@@ -128,7 +128,7 @@ static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direif (err)return err; - err =i915_gem_object_lock_interruptible (obj); + err =i915_gem_object_lock_interruptible (obj,NULL );if (err) goto out;
@@ -149,7 +149,7 @@ static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_directif (err)return err; - err =i915_gem_object_lock_interruptible (obj); + err =i915_gem_object_lock_interruptible (obj,NULL );if (err) goto out;
drivers/gpu/drm/i915/gem/i915_gem_domain.c +5 -5
@@ -32,7 +32,7 @@ void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)if (!i915_gem_object_is_framebuffer (obj))return ; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );__i915_gem_object_flush_for_display (obj);i915_gem_object_unlock (obj); }
@@ -197,7 +197,7 @@ int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,if (ret)return ret; - ret =i915_gem_object_lock_interruptible (obj); + ret =i915_gem_object_lock_interruptible (obj,NULL );if (ret)return ret;
@@ -541,7 +541,7 @@ i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,if (err) goto out; - err =i915_gem_object_lock_interruptible (obj); + err =i915_gem_object_lock_interruptible (obj,NULL );if (err) goto out_unpin;
@@ -581,7 +581,7 @@ int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,if (!i915_gem_object_has_struct_page (obj))return -ENODEV ; - ret =i915_gem_object_lock_interruptible (obj); + ret =i915_gem_object_lock_interruptible (obj,NULL );if (ret)return ret;
@@ -635,7 +635,7 @@ int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,if (!i915_gem_object_has_struct_page (obj))return -ENODEV ; - ret =i915_gem_object_lock_interruptible (obj); + ret =i915_gem_object_lock_interruptible (obj,NULL );if (ret)return ret;
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +2 -2
@@ -754,7 +754,7 @@ static int __eb_add_lut(struct i915_execbuffer *eb,if (err ==0 ) {/* And nor has this handle */ struct drm_i915_gem_object *obj = vma->obj; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );if (idr_find (&eb->file->object_idr, handle) == obj) {list_add (&lut->obj_link, &obj->lut_list); }else {
@@ -1040,7 +1040,7 @@ static void *reloc_iomap(struct drm_i915_gem_object *obj,if (use_cpu_reloc (cache, obj))return NULL ; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true);i915_gem_object_unlock (obj);if (err)
drivers/gpu/drm/i915/gem/i915_gem_object.c +1 -1
@@ -104,7 +104,7 @@ void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)struct i915_lut_handle *lut, *ln;LIST_HEAD (close); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );list_for_each_entry_safe (lut, ln, &obj->lut_list, obj_link) {struct i915_gem_context *ctx = lut->ctx;
drivers/gpu/drm/i915/gem/i915_gem_object.h +31 -7
@@ -110,20 +110,44 @@ i915_gem_object_put(struct drm_i915_gem_object *obj)#define assert_object_held (obj) dma_resv_assert_held((obj)->base.resv) -static inline void i915_gem_object_lock (struct drm_i915_gem_object *obj) +static inline int __i915_gem_object_lock (struct drm_i915_gem_object *obj, +struct i915_gem_ww_ctx *ww, +bool intr) { -dma_resv_lock (obj->base.resv,NULL ); +int ret; + +if (intr) + ret =dma_resv_lock_interruptible (obj->base.resv, ww ? &ww->ctx :NULL ); +else + ret =dma_resv_lock (obj->base.resv, ww ? &ww->ctx :NULL ); + +if (!ret && ww) +list_add_tail (&obj->obj_link, &ww->obj_list); +if (ret == -EALREADY ) + ret =0 ; + +if (ret == -EDEADLK ) + ww->contended = obj; + +return ret; } -static inline bool i915_gem_object_trylock (struct drm_i915_gem_object *obj) +static inline int i915_gem_object_lock (struct drm_i915_gem_object *obj, +struct i915_gem_ww_ctx *ww) { -return dma_resv_trylock (obj->base.resv); +return __i915_gem_object_lock (obj, ww, ww && ww->intr); } -static inline int -i915_gem_object_lock_interruptible (struct drm_i915_gem_object *obj) +static inline int i915_gem_object_lock_interruptible (struct drm_i915_gem_object *obj, +struct i915_gem_ww_ctx *ww) { -return dma_resv_lock_interruptible (obj->base.resv,NULL ); +WARN_ON (ww && !ww->intr); +return __i915_gem_object_lock (obj, ww, true); +} + +static inline bool i915_gem_object_trylock (struct drm_i915_gem_object *obj) +{ +return dma_resv_trylock (obj->base.resv); }static inline void i915_gem_object_unlock (struct drm_i915_gem_object *obj)
drivers/gpu/drm/i915/gem/i915_gem_object_blt.c +1 -1
@@ -142,7 +142,7 @@ int i915_gem_object_fill_blt(struct drm_i915_gem_object *obj,return err;if (obj->cache_dirty & ~obj->cache_coherent) { -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );i915_gem_clflush_object (obj,0 );i915_gem_object_unlock (obj); }
drivers/gpu/drm/i915/gem/i915_gem_object_types.h +9 -0
@@ -120,6 +120,15 @@ struct drm_i915_gem_object {*/ struct list_head lut_list; +/** +* @obj_link: Link into @i915_gem_ww_ctx.obj_list +* +* When we lock this object through i915_gem_object_lock() with a +* context, we add it to the list to ensure we can unlock everything +* when i915_gem_ww_ctx_backoff() or i915_gem_ww_ctx_fini() are called. +*/ +struct list_head obj_link; +/** Stolen memory for this object, instead of being backed by shmem. */ struct drm_mm_node *stolen;union {
drivers/gpu/drm/i915/gem/i915_gem_pm.c +1 -1
@@ -84,7 +84,7 @@ void i915_gem_suspend_late(struct drm_i915_private *i915)spin_unlock_irqrestore (&i915->mm.obj_lock, flags); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );drm_WARN_ON (&i915->drm,i915_gem_object_set_to_gtt_domain (obj, false));i915_gem_object_unlock (obj);
drivers/gpu/drm/i915/gem/i915_gem_tiling.c +1 -1
@@ -235,7 +235,7 @@ i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,* whilst executing a fenced command for an untiled object. */ -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL );if (i915_gem_object_is_framebuffer (obj)) {i915_gem_object_unlock (obj);return -EBUSY ;
drivers/gpu/drm/i915/gem/selftests/huge_pages.c +1 -1
@@ -974,7 +974,7 @@ static int gpu_write(struct intel_context *ce, {int err; -i915_gem_object_lock (vma->obj); +i915_gem_object_lock (vma->obj,NULL ); err =i915_gem_object_set_to_gtt_domain (vma->obj, true);i915_gem_object_unlock (vma->obj);if (err)
drivers/gpu/drm/i915/gem/selftests/i915_gem_client_blt.c +1 -1
@@ -72,7 +72,7 @@ static int __igt_client_fill(struct intel_engine_cs *engine)if (err) goto err_unpin; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_cpu_domain (obj, false);i915_gem_object_unlock (obj);if (err)
drivers/gpu/drm/i915/gem/selftests/i915_gem_coherency.c +5 -5
@@ -82,7 +82,7 @@ static int gtt_set(struct context *ctx, unsigned long offset, u32 v)u32 __iomem *map;int err =0 ; -i915_gem_object_lock (ctx->obj); +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_gtt_domain (ctx->obj, true);i915_gem_object_unlock (ctx->obj);if (err)
@@ -115,7 +115,7 @@ static int gtt_get(struct context *ctx, unsigned long offset, u32 *v)u32 __iomem *map;int err =0 ; -i915_gem_object_lock (ctx->obj); +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_gtt_domain (ctx->obj, false);i915_gem_object_unlock (ctx->obj);if (err)
@@ -147,7 +147,7 @@ static int wc_set(struct context *ctx, unsigned long offset, u32 v)u32 *map;int err; -i915_gem_object_lock (ctx->obj); +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_wc_domain (ctx->obj, true);i915_gem_object_unlock (ctx->obj);if (err)
@@ -168,7 +168,7 @@ static int wc_get(struct context *ctx, unsigned long offset, u32 *v)u32 *map;int err; -i915_gem_object_lock (ctx->obj); +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_wc_domain (ctx->obj, false);i915_gem_object_unlock (ctx->obj);if (err)
@@ -191,7 +191,7 @@ static int gpu_set(struct context *ctx, unsigned long offset, u32 v)u32 *cs;int err; -i915_gem_object_lock (ctx->obj); +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_gtt_domain (ctx->obj, true);i915_gem_object_unlock (ctx->obj);if (err)
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +2 -2
@@ -950,7 +950,7 @@ emit_rpcs_query(struct drm_i915_gem_object *obj,if (IS_ERR (vma))return PTR_ERR (vma); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, false);i915_gem_object_unlock (obj);if (err)
@@ -1691,7 +1691,7 @@ static int read_from_scratch(struct i915_gem_context *ctx,i915_request_add (rq); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_cpu_domain (obj, false);i915_gem_object_unlock (obj);if (err)
drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c +2 -2
@@ -103,7 +103,7 @@ static int check_partial_mapping(struct drm_i915_gem_object *obj,GEM_BUG_ON (i915_gem_object_get_tiling (obj) != tile->tiling);GEM_BUG_ON (i915_gem_object_get_stride (obj) != tile->stride); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true);i915_gem_object_unlock (obj);if (err) {
@@ -188,7 +188,7 @@ static int check_partial_mappings(struct drm_i915_gem_object *obj,GEM_BUG_ON (i915_gem_object_get_tiling (obj) != tile->tiling);GEM_BUG_ON (i915_gem_object_get_stride (obj) != tile->stride); -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true);i915_gem_object_unlock (obj);if (err) {
drivers/gpu/drm/i915/gem/selftests/i915_gem_phys.c +1 -1
@@ -44,7 +44,7 @@ static int mock_phys_object(void *arg) }/* Make the object dirty so that put_pages must do copy back the data */ -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true);i915_gem_object_unlock (obj);if (err) {
drivers/gpu/drm/i915/gt/intel_gt.c +1 -1
@@ -485,7 +485,7 @@ static int __engines_record_defaults(struct intel_gt *gt)if (err) goto out; -i915_gem_object_lock (state->obj); +i915_gem_object_lock (state->obj,NULL ); err =i915_gem_object_set_to_cpu_domain (state->obj, false);i915_gem_object_unlock (state->obj);if (err)
drivers/gpu/drm/i915/gt/selftest_workarounds.c +1 -1
@@ -214,7 +214,7 @@ static int check_whitelist(struct i915_gem_context *ctx,return PTR_ERR (results); err =0 ; -i915_gem_object_lock (results); +i915_gem_object_lock (results,NULL );intel_wedge_on_timeout (&wedge, engine->gt,HZ /5 )/* safety net! */ err = i915_gem_object_set_to_cpu_domain(results, false);i915_gem_object_unlock (results);
drivers/gpu/drm/i915/gvt/cmd_parser.c +1 -1
@@ -2957,7 +2957,7 @@ static int shadow_indirect_ctx(struct intel_shadow_wa_ctx *wa_ctx) goto put_obj; } -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); ret =i915_gem_object_set_to_cpu_domain (obj, false);i915_gem_object_unlock (obj);if (ret) {
drivers/gpu/drm/i915/i915_gem.c +49 -3
76 lines not on this page · raw message
drivers/gpu/drm/i915/i915_gem.h +11 -0
@@ -116,4 +116,15 @@ static inline bool __tasklet_is_scheduled(struct tasklet_struct *t) return test_bit(TASKLET_STATE_SCHED, &t->state); } +struct i915_gem_ww_ctx { +struct ww_acquire_ctx ctx; +struct list_head obj_list; +bool intr; +struct drm_i915_gem_object *contended; +}; + +void i915_gem_ww_ctx_init (struct i915_gem_ww_ctx *ctx,bool intr); +void i915_gem_ww_ctx_fini (struct i915_gem_ww_ctx *ctx); +int __must_check i915_gem_ww_ctx_backoff (struct i915_gem_ww_ctx *ctx); +#endif /* __I915_GEM_H__ */
drivers/gpu/drm/i915/selftests/i915_gem.c +41 -0
52 lines not on this page · raw message
drivers/gpu/drm/i915/selftests/i915_vma.c +1 -1
@@ -892,7 +892,7 @@ static int igt_vma_remapped_gtt(void *arg)unsigned int x, y;int err; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true);i915_gem_object_unlock (obj);if (err)
drivers/gpu/drm/i915/selftests/intel_memory_region.c +1 -1
@@ -509,7 +509,7 @@ static int igt_lmem_write_cpu(void *arg)if (err) goto out_unpin; -i915_gem_object_lock (obj); +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_wc_domain (obj, true);i915_gem_object_unlock (obj);if (err)
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 4/22 drm/i915: Remove locking from i915_gem_object_prepare_read/write
In reply to Maarten LankhorstExecbuffer submission will perform its own WW locking, and we cannot rely on the implicit lock there.
This also makes it clear that the GVT code will get a lockdep splat when multiple batchbuffer shadows need to be performed in the same instance, fix that up.
--- drivers/gpu/drm/i915/gem/i915_gem_domain.c | 20 ++++++------------- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 13 ++++++++++-- drivers/gpu/drm/i915/gem/i915_gem_object.h | 1 - .../gpu/drm/i915/gem/selftests/huge_pages.c | 5 ++++- .../i915/gem/selftests/i915_gem_coherency.c | 14 +++++++++---- .../drm/i915/gem/selftests/i915_gem_context.c | 12 ++++++++--- drivers/gpu/drm/i915/gt/intel_renderstate.c | 5 ++++- drivers/gpu/drm/i915/gvt/cmd_parser.c | 9 ++++++++- drivers/gpu/drm/i915/i915_gem.c | 20 +++++++++++++++++-- 9 files changed, 70 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_domain.c +6 -14
@@ -581,19 +581,17 @@ int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,if (!i915_gem_object_has_struct_page (obj))return -ENODEV ; - ret =i915_gem_object_lock_interruptible (obj,NULL ); -if (ret) -return ret; +assert_object_held (obj); ret =i915_gem_object_wait (obj,I915_WAIT_INTERRUPTIBLE ,MAX_SCHEDULE_TIMEOUT );if (ret) - goto err_unlock; +return ret; ret =i915_gem_object_pin_pages (obj);if (ret) - goto err_unlock; +return ret;if (obj->cache_coherent &I915_BO_CACHE_COHERENT_FOR_READ || !static_cpu_has (X86_FEATURE_CLFLUSH )) {
@@ -621,8 +619,6 @@ int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj, err_unpin:i915_gem_object_unpin_pages (obj); -err_unlock: -i915_gem_object_unlock (obj);return ret; }
@@ -635,20 +631,18 @@ int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,if (!i915_gem_object_has_struct_page (obj))return -ENODEV ; - ret =i915_gem_object_lock_interruptible (obj,NULL ); -if (ret) -return ret; +assert_object_held (obj); ret =i915_gem_object_wait (obj,I915_WAIT_INTERRUPTIBLE |I915_WAIT_ALL ,MAX_SCHEDULE_TIMEOUT );if (ret) - goto err_unlock; +return ret; ret =i915_gem_object_pin_pages (obj);if (ret) - goto err_unlock; +return ret;if (obj->cache_coherent &I915_BO_CACHE_COHERENT_FOR_WRITE || !static_cpu_has (X86_FEATURE_CLFLUSH )) {
@@ -685,7 +679,5 @@ int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj, err_unpin:i915_gem_object_unpin_pages (obj); -err_unlock: -i915_gem_object_unlock (obj);return ret; }
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +11 -2
@@ -960,11 +960,14 @@ static void reloc_cache_reset(struct reloc_cache *cache) vaddr =unmask_page (cache->vaddr);if (cache->vaddr &KMAP ) { +struct drm_i915_gem_object *obj = + (struct drm_i915_gem_object *)cache->node.mm;if (cache->vaddr &CLFLUSH_AFTER )mb ();kunmap_atomic (vaddr); -i915_gem_object_finish_access ((struct drm_i915_gem_object *)cache->node.mm); +i915_gem_object_finish_access (obj); +i915_gem_object_unlock (obj); }else {struct i915_ggtt *ggtt =cache_to_ggtt (cache);
@@ -999,10 +1002,16 @@ static void *reloc_kmap(struct drm_i915_gem_object *obj,unsigned int flushes;int err; - err =i915_gem_object_prepare_write (obj, &flushes); + err =i915_gem_object_lock_interruptible (obj,NULL );if (err)return ERR_PTR (err); + err =i915_gem_object_prepare_write (obj, &flushes); +if (err) { +i915_gem_object_unlock (obj); +return ERR_PTR (err); + } +BUILD_BUG_ON (KMAP &CLFLUSH_FLAGS );BUILD_BUG_ON ((KMAP |CLFLUSH_FLAGS ) &PAGE_MASK );
drivers/gpu/drm/i915/gem/i915_gem_object.h +0 -1
@@ -434,7 +434,6 @@ static inline void i915_gem_object_finish_access(struct drm_i915_gem_object *obj) {i915_gem_object_unpin_pages (obj); -i915_gem_object_unlock (obj); } static inline struct intel_engine_cs *
drivers/gpu/drm/i915/gem/selftests/huge_pages.c +4 -1
@@ -991,9 +991,10 @@ __cpu_check_shmem(struct drm_i915_gem_object *obj, u32 dword, u32 val)unsigned long n;int err; +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_prepare_read (obj, &needs_flush);if (err) -return err; + goto err_unlock;for (n =0 ; n < obj->base.size >>PAGE_SHIFT ; ++n) {u32 *ptr =kmap_atomic (i915_gem_object_get_page (obj, n));
@@ -1013,6 +1014,8 @@ __cpu_check_shmem(struct drm_i915_gem_object *obj, u32 dword, u32 val) }i915_gem_object_finish_access (obj); +err_unlock: +i915_gem_object_unlock (obj);return err; }
drivers/gpu/drm/i915/gem/selftests/i915_gem_coherency.c +10 -4
@@ -27,9 +27,10 @@ static int cpu_set(struct context *ctx, unsigned long offset, u32 v)u32 *cpu;int err; +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_prepare_write (ctx->obj, &needs_clflush);if (err) -return err; + goto out; page =i915_gem_object_get_page (ctx->obj, offset >>PAGE_SHIFT ); map =kmap_atomic (page);
@@ -46,7 +47,9 @@ static int cpu_set(struct context *ctx, unsigned long offset, u32 v)kunmap_atomic (map);i915_gem_object_finish_access (ctx->obj); -return 0 ; +out: +i915_gem_object_unlock (ctx->obj); +return err; }static int cpu_get (struct context *ctx,unsigned long offset,u32 *v)
@@ -57,9 +60,10 @@ static int cpu_get(struct context *ctx, unsigned long offset, u32 *v)u32 *cpu;int err; +i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_prepare_read (ctx->obj, &needs_clflush);if (err) -return err; + goto out; page =i915_gem_object_get_page (ctx->obj, offset >>PAGE_SHIFT ); map =kmap_atomic (page);
@@ -73,7 +77,9 @@ static int cpu_get(struct context *ctx, unsigned long offset, u32 *v)kunmap_atomic (map);i915_gem_object_finish_access (ctx->obj); -return 0 ; +out: +i915_gem_object_unlock (ctx->obj); +return err; }static int gtt_set (struct context *ctx,unsigned long offset,u32 v)
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +9 -3
@@ -461,9 +461,10 @@ static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)unsigned int n, m, need_flush;int err; +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_prepare_write (obj, &need_flush);if (err) -return err; + goto out;for (n =0 ; n <real_page_count (obj); n++) {u32 *map;
@@ -479,7 +480,9 @@ static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)i915_gem_object_finish_access (obj); obj->read_domains =I915_GEM_DOMAIN_GTT |I915_GEM_DOMAIN_CPU ; obj->write_domain =0 ; -return 0 ; +out: +i915_gem_object_unlock (obj); +return err; } static noinline int cpu_check(struct drm_i915_gem_object *obj,
@@ -488,9 +491,10 @@ static noinline int cpu_check(struct drm_i915_gem_object *obj,unsigned int n, m, needs_flush;int err; +i915_gem_object_lock (obj,NULL ); err =i915_gem_object_prepare_read (obj, &needs_flush);if (err) -return err; + goto out_unlock;for (n =0 ; n <real_page_count (obj); n++) {u32 *map;
@@ -527,6 +531,8 @@ static noinline int cpu_check(struct drm_i915_gem_object *obj, }i915_gem_object_finish_access (obj); +out_unlock: +i915_gem_object_unlock (obj);return err; }
drivers/gpu/drm/i915/gt/intel_renderstate.c +4 -1
@@ -74,9 +74,10 @@ static int render_state_setup(struct intel_renderstate *so,u32 *d;int ret; +i915_gem_object_lock (so->vma->obj,NULL ); ret =i915_gem_object_prepare_write (so->vma->obj, &needs_clflush);if (ret) -return ret; + goto out_unlock; d =kmap_atomic (i915_gem_object_get_dirty_page (so->vma->obj,0 ));
@@ -157,6 +158,8 @@ static int render_state_setup(struct intel_renderstate *so, ret =0 ; out:i915_gem_object_finish_access (so->vma->obj); +out_unlock: +i915_gem_object_unlock (so->vma->obj);return ret; err:
drivers/gpu/drm/i915/gvt/cmd_parser.c +8 -1
@@ -1870,10 +1870,14 @@ static int perform_bb_shadow(struct parser_exec_state *s) goto err_free_bb; } - ret =i915_gem_object_prepare_write (bb->obj, &bb->clflush); + ret =i915_gem_object_lock_interruptible (bb->obj,NULL );if (ret) goto err_free_obj; + ret =i915_gem_object_prepare_write (bb->obj, &bb->clflush); +if (ret) + goto err_unlock; + bb->va =i915_gem_object_pin_map (bb->obj,I915_MAP_WB );if (IS_ERR (bb->va)) { ret =PTR_ERR (bb->va);
@@ -1898,6 +1902,7 @@ static int perform_bb_shadow(struct parser_exec_state *s)if (ret) goto err_unmap; +i915_gem_object_unlock (bb->obj);INIT_LIST_HEAD (&bb->list);list_add (&bb->list, &s->workload->shadow_bb);
@@ -1924,6 +1929,8 @@ static int perform_bb_shadow(struct parser_exec_state *s)i915_gem_object_unpin_map (bb->obj); err_finish_shmem_access:i915_gem_object_finish_access (bb->obj); +err_unlock: +i915_gem_object_unlock (bb->obj); err_free_obj:i915_gem_object_put (bb->obj); err_free_bb:
drivers/gpu/drm/i915/i915_gem.c +18 -2
@@ -330,12 +330,20 @@ i915_gem_shmem_pread(struct drm_i915_gem_object *obj,u64 remain;int ret; - ret =i915_gem_object_prepare_read (obj, &needs_clflush); + ret =i915_gem_object_lock_interruptible (obj,NULL );if (ret)return ret; + ret =i915_gem_object_prepare_read (obj, &needs_clflush); +if (ret) { +i915_gem_object_unlock (obj); +return ret; + } + fence =i915_gem_object_lock_fence (obj);i915_gem_object_finish_access (obj); +i915_gem_object_unlock (obj); +if (!fence)return -ENOMEM ;
@@ -729,12 +737,20 @@ i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,u64 remain;int ret; - ret =i915_gem_object_prepare_write (obj, &needs_clflush); + ret =i915_gem_object_lock_interruptible (obj,NULL );if (ret)return ret; + ret =i915_gem_object_prepare_write (obj, &needs_clflush); +if (ret) { +i915_gem_object_unlock (obj); +return ret; + } + fence =i915_gem_object_lock_fence (obj);i915_gem_object_finish_access (obj); +i915_gem_object_unlock (obj); +if (!fence)return -ENOMEM ;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 5/22 drm/i915: Parse command buffer earlier in eb_relocate(slow)
In reply to Maarten LankhorstWe want to introduce backoff logic, but we need to lock the pool object as well for command parsing. Because of this, we will need backoff logic for the engine pool obj, move the batch validation up slightly to eb_lookup_vmas, and the actual command parsing in a separate function which can get called from execbuf relocation fast and slowpath.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +37 -31
@@ -285,6 +285,8 @@ struct i915_execbuffer {struct hlist_head *buckets;/** ht for relocation handles */ }; +static int eb_parse (struct i915_execbuffer *eb); +static inline bool eb_use_cmdparser (const struct i915_execbuffer *eb) { return intel_engine_requires_cmd_parser(eb->engine) ||
@@ -814,6 +816,7 @@ static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle)static int eb_lookup_vmas (struct i915_execbuffer *eb) { +struct drm_i915_private *i915 = eb->i915;unsigned int batch =eb_batch_index (eb);unsigned int i;int err =0 ;
@@ -827,18 +830,37 @@ static int eb_lookup_vmas(struct i915_execbuffer *eb) vma =eb_lookup_vma (eb, eb->exec[i].handle);if (IS_ERR (vma)) { err =PTR_ERR (vma); -break ; + goto err; } err =eb_validate_vma (eb, &eb->exec[i], vma);if (unlikely (err)) {i915_vma_put (vma); -break ; + goto err; }eb_add_vma (eb, i, batch, vma); } +if (unlikely (eb->batch->flags &EXEC_OBJECT_WRITE )) { +drm_dbg (&i915->drm, +"Attempting to use self-modifying batch buffer\n" ); +return -EINVAL ; + } + +if (range_overflows_t (u64, + eb->batch_start_offset, eb->batch_len, + eb->batch->vma->size)) { +drm_dbg (&i915->drm,"Attempting to use out-of-bounds batch\n" ); +return -EINVAL ; + } + +if (eb->batch_len ==0 ) + eb->batch_len = eb->batch->vma->size - eb->batch_start_offset; + +return 0 ; + +err: eb->vma[i].vma =NULL ;return err; }
@@ -1688,7 +1710,7 @@ static int eb_prefault_relocations(const struct i915_execbuffer *eb)return 0 ; } -static noinline int eb_relocate_slow(struct i915_execbuffer *eb) +static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) {bool have_copy = false;struct eb_vma *ev;
@@ -1739,6 +1761,11 @@ static noinline int eb_relocate_slow(struct i915_execbuffer *eb) } } +/* as last step, parse the command buffer */ + err =eb_parse (eb); +if (err) + goto err; +/* * Leave the user relocations as are, this is the painfully slow path, * and we want to avoid the complication of dropping the lock whilst
@@ -1771,7 +1798,7 @@ static noinline int eb_relocate_slow(struct i915_execbuffer *eb)return err; } -static int eb_relocate (struct i915_execbuffer *eb) +static int eb_relocate_parse (struct i915_execbuffer *eb) {int err;
@@ -1791,11 +1818,11 @@ static int eb_relocate(struct i915_execbuffer *eb)list_for_each_entry (ev, &eb->relocs, reloc_link) {if (eb_relocate_vma (eb, ev)) -return eb_relocate_slow (eb); +return eb_relocate_parse_slow (eb); } } -return 0 ; +return eb_parse (eb); }static int eb_move_to_gpu (struct i915_execbuffer *eb)
@@ -2731,7 +2758,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (unlikely (err)) goto err_context; - err =eb_relocate (&eb); + err =eb_relocate_parse (&eb);if (err) {/* * If the user expects the execobject.offset and
@@ -2744,33 +2771,10 @@ i915_gem_do_execbuffer(struct drm_device *dev, goto err_vma; } -if (unlikely (eb.batch->flags &EXEC_OBJECT_WRITE )) { -drm_dbg (&i915->drm, -"Attempting to use self-modifying batch buffer\n" ); - err = -EINVAL ; - goto err_vma; - } - -if (range_overflows_t (u64, - eb.batch_start_offset, eb.batch_len, - eb.batch->vma->size)) { -drm_dbg (&i915->drm,"Attempting to use out-of-bounds batch\n" ); - err = -EINVAL ; - goto err_vma; - } - -if (eb.batch_len ==0 ) - eb.batch_len = eb.batch->vma->size - eb.batch_start_offset; - - err =eb_parse (&eb); -if (err) - goto err_vma; -/* * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure * batch" bit. Hence we need to pin secure batches into the global gtt. * hsw should have this fixed, but bdw mucks it up again. */ - batch = eb.batch->vma;if (eb.batch_flags &I915_DISPATCH_SECURE ) {struct i915_vma *vma;
@@ -2784,13 +2788,15 @@ i915_gem_do_execbuffer(struct drm_device *dev,* fitting due to fragmentation. * So this is actually safe. */ - vma =i915_gem_object_ggtt_pin (batch->obj,NULL ,0 ,0 ,0 ); + vma =i915_gem_object_ggtt_pin (eb.batch->vma->obj,NULL ,0 ,0 ,0 );if (IS_ERR (vma)) { err =PTR_ERR (vma); goto err_parse; } batch = vma; + }else { + batch = eb.batch->vma; }/* All GPU relocation batches must be submitted prior to the user rq */
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 6/22 drm/i915: Use per object locking in execbuf, v7.
In reply to Maarten LankhorstNow that we changed execbuf submission slightly to allow us to do all pinning in one place, we can now simply add ww versions on top of struct_mutex. All we have to do is a separate path for -EDEADLK handling, which needs to unpin all gem bo's before dropping the lock, then starting over.
This finally allows us to do parallel submission, but because not all of the pinning code uses the ww ctx yet, we cannot completely drop struct_mutex yet.
Changes since v1:
- Keep struct_mutex for now. :(
Changes since v2:
- Make sure we always lock the ww context in slowpath. Changes since v3:
- Don't call __eb_unreserve_vma in eb_move_to_gpu now; this can be done on normal unlock path.
- Unconditionally release vmas and context.
Changes since v4:
- Rebased on top of struct_mutex reduction.
Changes since v5:
- Remove training wheels.
Changes since v6:
- Fix accidentally broken -ENOSPC handling.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 273 ++++++++++-------- 1 file changed, 148 insertions(+), 125 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +148 -125
@@ -249,6 +249,8 @@ struct i915_execbuffer {/** list of vma that have execobj.relocation_count */ struct list_head relocs; +struct i915_gem_ww_ctx ww; +/** * Track the most recently used object for relocations, as we * frequently have to perform multiple relocations within the same
@@ -404,24 +406,18 @@ eb_pin_vma(struct i915_execbuffer *eb,return !eb_vma_misplaced (entry, vma, ev->flags); } -static inline void __eb_unreserve_vma (struct i915_vma *vma,unsigned int flags) -{ -GEM_BUG_ON (!(flags & __EXEC_OBJECT_HAS_PIN)); - -if (unlikely (flags & __EXEC_OBJECT_HAS_FENCE)) -__i915_vma_unpin_fence (vma); - -__i915_vma_unpin (vma); -} -static inline void eb_unreserve_vma (struct eb_vma *ev) {if (!(ev->flags & __EXEC_OBJECT_HAS_PIN))return ; -__eb_unreserve_vma (ev->vma, ev->flags); ev->flags &= ~__EXEC_OBJECT_RESERVED; + +if (unlikely (ev->flags & __EXEC_OBJECT_HAS_FENCE)) +__i915_vma_unpin_fence (ev->vma); + +__i915_vma_unpin (ev->vma); } static int
@@ -515,16 +511,6 @@ eb_add_vma(struct i915_execbuffer *eb, eb->batch = ev; } - -if (eb_pin_vma (eb, entry, ev)) { -if (entry->offset != vma->node.start) { - entry->offset = vma->node.start |UPDATE ; - eb->args->flags |= __EXEC_HAS_RELOC; - } - }else { -eb_unreserve_vma (ev); -list_add_tail (&ev->bind_link, &eb->unbound); - } } static inline int use_cpu_reloc(const struct reloc_cache *cache,
@@ -628,10 +614,6 @@ static int eb_reserve(struct i915_execbuffer *eb)* This avoid unnecessary unbinding of later objects in order to make * room for the earlier objects *unless* we need to defragment. */ - -if (mutex_lock_interruptible (&eb->i915->drm.struct_mutex)) -return -EINTR ; - pass =0 ; do { list_for_each_entry(ev, &eb->unbound, bind_link) {
@@ -639,8 +621,8 @@ static int eb_reserve(struct i915_execbuffer *eb)if (err)break ; } -if (!(err == -ENOSPC || err == -EAGAIN )) -break ; +if (err != -ENOSPC ) +return err;/* Resort *all* the objects into priority order */ INIT_LIST_HEAD (&eb->unbound);
@@ -670,13 +652,6 @@ static int eb_reserve(struct i915_execbuffer *eb) }list_splice_tail (&last, &eb->unbound); -if (err == -EAGAIN ) { -mutex_unlock (&eb->i915->drm.struct_mutex); -flush_workqueue (eb->i915->mm.userptr_wq); -mutex_lock (&eb->i915->drm.struct_mutex); -continue ; - } -switch (pass++) {case 0 :break ;
@@ -687,20 +662,15 @@ static int eb_reserve(struct i915_execbuffer *eb) err =i915_gem_evict_vm (eb->context->vm);mutex_unlock (&eb->context->vm->mutex);if (err) - goto unlock; +return err;break ;default : - err = -ENOSPC ; - goto unlock; +return -ENOSPC ; } pin_flags =PIN_USER ; }while (1 ); - -unlock: -mutex_unlock (&eb->i915->drm.struct_mutex); -return err; }static unsigned int eb_batch_index (const struct i915_execbuffer *eb)
@@ -822,7 +792,6 @@ static int eb_lookup_vmas(struct i915_execbuffer *eb)int err =0 ;INIT_LIST_HEAD (&eb->relocs); -INIT_LIST_HEAD (&eb->unbound);for (i =0 ; i < eb->buffer_count; i++) {struct i915_vma *vma;
@@ -865,6 +834,48 @@ static int eb_lookup_vmas(struct i915_execbuffer *eb)return err; } +static int eb_validate_vmas (struct i915_execbuffer *eb) +{ +unsigned int i; +int err; + +INIT_LIST_HEAD (&eb->unbound); + +for (i =0 ; i < eb->buffer_count; i++) { +struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; +struct eb_vma *ev = &eb->vma[i]; +struct i915_vma *vma = ev->vma; + + err =i915_gem_object_lock (vma->obj, &eb->ww); +if (err) +return err; + +if (eb_pin_vma (eb, entry, ev)) { +if (entry->offset != vma->node.start) { + entry->offset = vma->node.start |UPDATE ; + eb->args->flags |= __EXEC_HAS_RELOC; + } + }else { +eb_unreserve_vma (ev); + +list_add_tail (&ev->bind_link, &eb->unbound); +if (drm_mm_node_allocated (&vma->node)) { + err =i915_vma_unbind (vma); +if (err) +return err; + } + } + +GEM_BUG_ON (drm_mm_node_allocated (&vma->node) && +eb_vma_misplaced (&eb->exec[i], vma, ev->flags)); + } + +if (!list_empty (&eb->unbound)) +return eb_reserve (eb); + +return 0 ; +} +static struct eb_vma *eb_get_vma (const struct i915_execbuffer *eb,unsigned long handle) {
@@ -885,7 +896,7 @@ eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) } } -static void eb_release_vmas (const struct i915_execbuffer *eb) +static void eb_release_vmas (const struct i915_execbuffer *eb,bool final) {const unsigned int count = eb->buffer_count;unsigned int i;
@@ -897,12 +908,10 @@ static void eb_release_vmas(const struct i915_execbuffer *eb)if (!vma)break ; - eb->vma[i].vma =NULL ; - -if (ev->flags & __EXEC_OBJECT_HAS_PIN) -__eb_unreserve_vma (vma, ev->flags); +eb_unreserve_vma (ev); -i915_vma_put (vma); +if (final) +i915_vma_put (vma); } }
@@ -989,7 +998,6 @@ static void reloc_cache_reset(struct reloc_cache *cache)kunmap_atomic (vaddr);i915_gem_object_finish_access (obj); -i915_gem_object_unlock (obj); }else {struct i915_ggtt *ggtt =cache_to_ggtt (cache);
@@ -1024,15 +1032,9 @@ static void *reloc_kmap(struct drm_i915_gem_object *obj,unsigned int flushes;int err; - err =i915_gem_object_lock_interruptible (obj,NULL ); -if (err) -return ERR_PTR (err); - err =i915_gem_object_prepare_write (obj, &flushes); -if (err) { -i915_gem_object_unlock (obj); +if (err)return ERR_PTR (err); - }BUILD_BUG_ON (KMAP &CLFLUSH_FLAGS );BUILD_BUG_ON ((KMAP |CLFLUSH_FLAGS ) &PAGE_MASK );
@@ -1071,9 +1073,7 @@ static void *reloc_iomap(struct drm_i915_gem_object *obj,if (use_cpu_reloc (cache, obj))return NULL ; -i915_gem_object_lock (obj,NULL ); err =i915_gem_object_set_to_gtt_domain (obj, true); -i915_gem_object_unlock (obj);if (err)return ERR_PTR (err);
@@ -1162,7 +1162,7 @@ static int reloc_move_to_gpu(struct i915_request *rq, struct i915_vma *vma)struct drm_i915_gem_object *obj = vma->obj;int err; -i915_vma_lock (vma); +assert_vma_held (vma);if (obj->cache_dirty & ~obj->cache_coherent)i915_gem_clflush_object (obj,0 );
@@ -1172,8 +1172,6 @@ static int reloc_move_to_gpu(struct i915_request *rq, struct i915_vma *vma)if (err ==0 ) err =i915_vma_move_to_active (vma, rq,EXEC_OBJECT_WRITE ); -i915_vma_unlock (vma); -return err; }
@@ -1192,6 +1190,10 @@ static int __reloc_gpu_alloc(struct i915_execbuffer *eb,if (IS_ERR (pool))return PTR_ERR (pool); + err =i915_gem_object_lock (pool->obj, &eb->ww); +if (err) + goto out_pool; + cmd = i915_gem_object_pin_map(pool->obj, cache->has_llc ? I915_MAP_FORCE_WB :
@@ -1231,11 +1233,10 @@ static int __reloc_gpu_alloc(struct i915_execbuffer *eb,if (err) goto skip_request; -i915_vma_lock (batch); +assert_vma_held (batch); err =i915_request_await_object (rq, batch->obj, false);if (err ==0 ) err =i915_vma_move_to_active (batch, rq,0 ); -i915_vma_unlock (batch);if (err) goto skip_request;
@@ -1316,7 +1317,9 @@ relocate_entry(struct i915_vma *vma, len =3 ; batch =reloc_gpu (eb, vma, len); -if (IS_ERR (batch)) +if (batch ==ERR_PTR (-EDEADLK )) +return (s64)-EDEADLK ; +else if (IS_ERR (batch)) goto repeat; addr =gen8_canonical_addr (vma->node.start + offset);
@@ -1722,6 +1725,10 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) goto out; } +/* We may process another execbuffer during the unlock... */ +eb_release_vmas (eb, false); +i915_gem_ww_ctx_fini (&eb->ww); +/* * We take 3 passes through the slowpatch. *
@@ -1744,15 +1751,29 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb)cond_resched (); err =0 ; } + +flush_workqueue (eb->i915->mm.userptr_wq); + +i915_gem_ww_ctx_init (&eb->ww, true);if (err) goto out; +/* reacquire the objects */ +repeat_validate: + err =eb_validate_vmas (eb); +if (err) + goto err; + +GEM_BUG_ON (!eb->batch); +list_for_each_entry (ev, &eb->relocs, reloc_link) {if (!have_copy) {pagefault_disable (); err =eb_relocate_vma (eb, ev);pagefault_enable (); -if (err) +if (err == -EDEADLK ) + goto err; +else if (err) goto repeat; }else { err =eb_relocate_vma_slow (eb, ev);
@@ -1774,6 +1795,13 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb)*/ err: +if (err == -EDEADLK ) { +eb_release_vmas (eb, false); + err =i915_gem_ww_ctx_backoff (&eb->ww); +if (!err) + goto repeat_validate; + } +if (err == -EAGAIN ) goto repeat;
@@ -1802,61 +1830,59 @@ static int eb_relocate_parse(struct i915_execbuffer *eb) {int err; - err =eb_lookup_vmas (eb); -if (err) -return err; - -if (!list_empty (&eb->unbound)) { - err =eb_reserve (eb); -if (err) -return err; - } +retry: + err =eb_validate_vmas (eb); +if (err == -EAGAIN ) + goto slow; +else if (err) + goto err;/* The objects are in their final locations, apply the relocations. */ if (eb->args->flags & __EXEC_HAS_RELOC) {struct eb_vma *ev;list_for_each_entry (ev, &eb->relocs, reloc_link) { -if (eb_relocate_vma (eb, ev)) -return eb_relocate_parse_slow (eb); + err =eb_relocate_vma (eb, ev); +if (err == -EDEADLK ) + goto err; +else if (err) + goto slow; } } -return eb_parse (eb); + err =eb_parse (eb); + +err: +if (err == -EDEADLK ) { +eb_release_vmas (eb, false); + err =i915_gem_ww_ctx_backoff (&eb->ww); +if (!err) + goto retry; + } + +return err; + +slow: + err =eb_relocate_parse_slow (eb); +if (err) +/* +* If the user expects the execobject.offset and +* reloc.presumed_offset to be an exact match, +* as for using NO_RELOC, then we cannot update +* the execobject.offset until we have completed +* relocation. +*/ + eb->args->flags &= ~__EXEC_HAS_RELOC; + +return err; }static int eb_move_to_gpu (struct i915_execbuffer *eb) {const unsigned int count = eb->buffer_count; -struct ww_acquire_ctx acquire; -unsigned int i; +unsigned int i = count;int err =0 ; -ww_acquire_init (&acquire, &reservation_ww_class); - -for (i =0 ; i < count; i++) { -struct eb_vma *ev = &eb->vma[i]; -struct i915_vma *vma = ev->vma; - - err =ww_mutex_lock_interruptible (&vma->resv->lock, &acquire); -if (err == -EDEADLK ) { -GEM_BUG_ON (i ==0 ); -do { -int j = i -1 ; - -ww_mutex_unlock (&eb->vma[j].vma->resv->lock); - -swap (eb->vma[i], eb->vma[j]); - }while (--i); - - err =ww_mutex_lock_slow_interruptible (&vma->resv->lock, - &acquire); - } -if (err) -break ; - } -ww_acquire_done (&acquire); -while (i--) {struct eb_vma *ev = &eb->vma[i];struct i915_vma *vma = ev->vma;
@@ -1900,21 +1926,11 @@ static int eb_move_to_gpu(struct i915_execbuffer *eb)if (err ==0 ) err =i915_vma_move_to_active (vma, eb->request, flags); - -i915_vma_unlock (vma); - -__eb_unreserve_vma (vma, flags); -i915_vma_put (vma); - - ev->vma =NULL ; } -ww_acquire_fini (&acquire);if (unlikely (err)) goto err_skip; - eb->exec =NULL ; -/* Unconditionally flush any chipset caches (for streaming writes). */ intel_gt_chipset_flush (eb->engine->gt);return 0 ;
@@ -2064,10 +2080,6 @@ static int eb_parse_pipeline(struct i915_execbuffer *eb, pw->shadow = shadow; pw->trampoline = trampoline; - err =dma_resv_lock_interruptible (pw->batch->resv,NULL ); -if (err) - goto err_trampoline; - err =dma_resv_reserve_shared (pw->batch->resv,1 );if (err) goto err_batch_unlock;
@@ -2082,19 +2094,14 @@ static int eb_parse_pipeline(struct i915_execbuffer *eb,/* Keep the batch alive and unwritten as we parse */ dma_resv_add_shared_fence (pw->batch->resv, &pw->base.dma); -dma_resv_unlock (pw->batch->resv); -/* Force execution to wait for completion of the parser */ -dma_resv_lock (shadow->resv,NULL );dma_resv_add_excl_fence (shadow->resv, &pw->base.dma); -dma_resv_unlock (shadow->resv);dma_fence_work_commit_imm (&pw->base);return 0 ; err_batch_unlock:dma_resv_unlock (pw->batch->resv); -err_trampoline:if (trampoline)i915_active_release (&trampoline->active); err_shadow:
@@ -2136,6 +2143,10 @@ static int eb_parse(struct i915_execbuffer *eb)if (IS_ERR (pool))return PTR_ERR (pool); + err =i915_gem_object_lock (pool->obj, &eb->ww); +if (err) + goto err; + shadow =shadow_batch_pin (pool->obj, eb->context->vm,PIN_USER );if (IS_ERR (shadow)) { err =PTR_ERR (shadow);
@@ -2758,6 +2769,14 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (unlikely (err)) goto err_context; + err =eb_lookup_vmas (&eb); +if (err) { +eb_release_vmas (&eb, true); + goto err_engine; + } + +i915_gem_ww_ctx_init (&eb.ww, true); + err =eb_relocate_parse (&eb);if (err) {/*
@@ -2771,6 +2790,8 @@ i915_gem_do_execbuffer(struct drm_device *dev, goto err_vma; } +ww_acquire_done (&eb.ww.ctx); +/* * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure * batch" bit. Hence we need to pin secure batches into the global gtt.
@@ -2876,10 +2897,12 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (batch->private)intel_engine_pool_put (batch->private); err_vma: -if (eb.exec) -eb_release_vmas (&eb); +eb_release_vmas (&eb, true);if (eb.trampoline)i915_vma_unpin (eb.trampoline); +WARN_ON (err == -EDEADLK ); +i915_gem_ww_ctx_fini (&eb.ww); +err_engine:eb_unpin_engine (&eb); err_context:i915_gem_context_put (eb.gem_context);
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 7/22 drm/i915: Use ww locking in intel_renderstate.
In reply to Maarten LankhorstWe want to start using ww locking in intel_context_pin, for this we need to lock multiple objects, and the single i915_gem_object_lock is not enough.
Convert to using ww-waiting, and make sure we always pin intel_context_state, even if we don't have a renderstate object.
--- drivers/gpu/drm/i915/gt/intel_gt.c | 21 +++--- drivers/gpu/drm/i915/gt/intel_renderstate.c | 71 ++++++++++++++------- drivers/gpu/drm/i915/gt/intel_renderstate.h | 9 ++- 3 files changed, 65 insertions(+), 36 deletions(-)
drivers/gpu/drm/i915/gt/intel_gt.c +12 -9
@@ -406,21 +406,20 @@ static int __engines_record_defaults(struct intel_gt *gt)/* We must be able to switch to something! */ GEM_BUG_ON (!engine->kernel_context); - err =intel_renderstate_init (&so, engine); -if (err) - goto out; - ce =intel_context_create (engine);if (IS_ERR (ce)) { err =PTR_ERR (ce); goto out; } - rq =intel_context_create_request (ce); + err =intel_renderstate_init (&so, ce); +if (err) + goto err; + + rq =i915_request_create (ce);if (IS_ERR (rq)) { err =PTR_ERR (rq); -intel_context_put (ce); - goto out; + goto err_fini; } err =intel_engine_emit_ctx_wa (rq);
@@ -434,9 +433,13 @@ static int __engines_record_defaults(struct intel_gt *gt) err_rq: requests[id] =i915_request_get (rq);i915_request_add (rq); -intel_renderstate_fini (&so); -if (err) +err_fini: +intel_renderstate_fini (&so, ce); +err: +if (err) { +intel_context_put (ce); goto out; + } }/* Flush the default context image to memory, and enable powersaving. */
drivers/gpu/drm/i915/gt/intel_renderstate.c +47 -24
130 lines not on this page · raw message
drivers/gpu/drm/i915/gt/intel_renderstate.h +6 -3
@@ -25,9 +25,10 @@#define _INTEL_RENDERSTATE_H_#include <linux/types.h> +#include "i915_gem.h" struct i915_request ; -struct intel_engine_cs ; +struct intel_context ;struct i915_vma ;struct intel_renderstate_rodata {
@@ -49,6 +50,7 @@ extern const struct intel_renderstate_rodata gen8_null_state;extern const struct intel_renderstate_rodata gen9_null_state;struct intel_renderstate { +struct i915_gem_ww_ctx ww;const struct intel_renderstate_rodata *rodata;struct i915_vma *vma;u32 batch_offset;
@@ -58,9 +60,10 @@ struct intel_renderstate { };int intel_renderstate_init (struct intel_renderstate *so, -struct intel_engine_cs *engine); +struct intel_context *ce);int intel_renderstate_emit (struct intel_renderstate *so,struct i915_request *rq); -void intel_renderstate_fini (struct intel_renderstate *so); +void intel_renderstate_fini (struct intel_renderstate *so, +struct intel_context *ce);#endif /* _INTEL_RENDERSTATE_H_ */
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 8/22 drm/i915: Add ww context handling to context_barrier_task
In reply to Maarten LankhorstThis is required if we want to pass a ww context in intel_context_pin and gen6_ppgtt_pin().
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 55 ++++++++++++++----- .../drm/i915/gem/selftests/i915_gem_context.c | 22 +++----- 2 files changed, 48 insertions(+), 29 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +40 -15
@@ -1097,6 +1097,7 @@ I915_SELFTEST_DECLARE(static intel_engine_mask_t context_barrier_inject_fault);static int context_barrier_task (struct i915_gem_context *ctx,intel_engine_mask_t engines,bool (*skip)(struct intel_context *ce,void *data), +int (*pin)(struct intel_context *ce,struct i915_gem_ww_ctx *ww,void *data),int (*emit)(struct i915_request *rq,void *data),void (*task)(void *data),void *data)
@@ -1104,6 +1105,7 @@ static int context_barrier_task(struct i915_gem_context *ctx,struct context_barrier_task *cb;struct i915_gem_engines_iter it;struct i915_gem_engines *e; +struct i915_gem_ww_ctx ww;struct intel_context *ce;int err =0 ;
@@ -1141,10 +1143,21 @@ static int context_barrier_task(struct i915_gem_context *ctx,if (skip &&skip (ce, data))continue ; - rq =intel_context_create_request (ce); +i915_gem_ww_ctx_init (&ww, true); +retry: + err =intel_context_pin (ce); +if (err) + goto err; + +if (pin) + err =pin (ce, &ww, data); +if (err) + goto err_unpin; + + rq =i915_request_create (ce);if (IS_ERR (rq)) { err =PTR_ERR (rq); -break ; + goto err_unpin; } err =0 ;
@@ -1154,6 +1167,16 @@ static int context_barrier_task(struct i915_gem_context *ctx, err =i915_active_add_request (&cb->base, rq);i915_request_add (rq); +err_unpin: +intel_context_unpin (ce); +err: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); +if (err)break ; }
@@ -1209,6 +1232,17 @@ static void set_ppgtt_barrier(void *data)i915_vm_close (old); } +static int pin_ppgtt_update (struct intel_context *ce,struct i915_gem_ww_ctx *ww,void *data) +{ +struct i915_address_space *vm = ce->vm; + +if (!HAS_LOGICAL_RING_CONTEXTS (vm->i915)) +/* ppGTT is not part of the legacy context image */ +return gen6_ppgtt_pin (i915_vm_to_ppgtt (vm)); + +return 0 ; +} +static int emit_ppgtt_update (struct i915_request *rq,void *data) {struct i915_address_space *vm = rq->context->vm;
@@ -1265,20 +1299,10 @@ static int emit_ppgtt_update(struct i915_request *rq, void *data)static bool skip_ppgtt_update (struct intel_context *ce,void *data) { -if (!test_bit (CONTEXT_ALLOC_BIT , &ce->flags)) -return true; -if (HAS_LOGICAL_RING_CONTEXTS (ce->engine->i915)) -return false; - -if (!atomic_read (&ce->pin_count)) -return true; - -/* ppGTT is not part of the legacy context image */ -if (gen6_ppgtt_pin (i915_vm_to_ppgtt (ce->vm))) -return true; - -return false; +return !ce->state; +else +return !atomic_read (&ce->pin_count); } static int set_ppgtt(struct drm_i915_file_private *file_priv,
@@ -1329,6 +1353,7 @@ static int set_ppgtt(struct drm_i915_file_private *file_priv,*/ err =context_barrier_task (ctx,ALL_ENGINES , skip_ppgtt_update, + pin_ppgtt_update, emit_ppgtt_update, set_ppgtt_barrier, old);
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +8 -14
@@ -1903,8 +1903,8 @@ static int mock_context_barrier(void *arg)return -ENOMEM ; counter =0 ; - err =context_barrier_task (ctx,0 , -NULL ,NULL , mock_barrier_task, &counter); + err =context_barrier_task (ctx,0 ,NULL ,NULL ,NULL , + mock_barrier_task, &counter);if (err) {pr_err ("Failed at line %d, err=%d\n" , __LINE__, err); goto out;
@@ -1916,11 +1916,8 @@ static int mock_context_barrier(void *arg) } counter =0 ; - err =context_barrier_task (ctx,ALL_ENGINES , - skip_unused_engines, -NULL , - mock_barrier_task, - &counter); + err =context_barrier_task (ctx,ALL_ENGINES , skip_unused_engines, +NULL ,NULL , mock_barrier_task, &counter);if (err) {pr_err ("Failed at line %d, err=%d\n" , __LINE__, err); goto out;
@@ -1940,8 +1937,8 @@ static int mock_context_barrier(void *arg) counter =0 ; context_barrier_inject_fault =BIT (RCS0 ); - err =context_barrier_task (ctx,ALL_ENGINES , -NULL ,NULL , mock_barrier_task, &counter); + err =context_barrier_task (ctx,ALL_ENGINES ,NULL ,NULL ,NULL , + mock_barrier_task, &counter); context_barrier_inject_fault =0 ;if (err == -ENXIO ) err =0 ;
@@ -1955,11 +1952,8 @@ static int mock_context_barrier(void *arg) goto out; counter =0 ; - err =context_barrier_task (ctx,ALL_ENGINES , - skip_unused_engines, -NULL , - mock_barrier_task, - &counter); + err =context_barrier_task (ctx,ALL_ENGINES , skip_unused_engines, +NULL ,NULL , mock_barrier_task, &counter);if (err) {pr_err ("Failed at line %d, err=%d\n" , __LINE__, err); goto out;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 9/22 drm/i915: Nuke arguments to eb_pin_engine
In reply to Maarten LankhorstThose arguments are already set as eb.file and eb.args, so kill off the extra arguments. This will allow us to move eb_pin_engine() to after we reserved all BO's.
--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +7 -10
@@ -2412,11 +2412,10 @@ static void eb_unpin_engine(struct i915_execbuffer *eb) }static unsigned int -eb_select_legacy_ring (struct i915_execbuffer *eb, -struct drm_file *file, -struct drm_i915_gem_execbuffer2 *args) +eb_select_legacy_ring (struct i915_execbuffer *eb) {struct drm_i915_private *i915 = eb->i915; +struct drm_i915_gem_execbuffer2 *args = eb->args;unsigned int user_ring_id = args->flags &I915_EXEC_RING_MASK ; if (user_ring_id != I915_EXEC_BSD &&
@@ -2431,7 +2430,7 @@ eb_select_legacy_ring(struct i915_execbuffer *eb,unsigned int bsd_idx = args->flags &I915_EXEC_BSD_MASK ;if (bsd_idx ==I915_EXEC_BSD_DEFAULT ) { - bsd_idx =gen8_dispatch_bsd_engine (i915, file); + bsd_idx =gen8_dispatch_bsd_engine (i915, eb->file); }else if (bsd_idx >=I915_EXEC_BSD_RING1 && bsd_idx <=I915_EXEC_BSD_RING2 ) { bsd_idx >>=I915_EXEC_BSD_SHIFT ;
@@ -2456,18 +2455,16 @@ eb_select_legacy_ring(struct i915_execbuffer *eb, }static int -eb_pin_engine (struct i915_execbuffer *eb, -struct drm_file *file, -struct drm_i915_gem_execbuffer2 *args) +eb_pin_engine (struct i915_execbuffer *eb) {struct intel_context *ce;unsigned int idx;int err;if (i915_gem_context_user_engines (eb->gem_context)) - idx = args->flags &I915_EXEC_RING_MASK ; + idx = eb->args->flags &I915_EXEC_RING_MASK ;else - idx =eb_select_legacy_ring (eb, file, args); + idx =eb_select_legacy_ring (eb); ce =i915_gem_context_get_engine (eb->gem_context, idx);if (IS_ERR (ce))
@@ -2765,7 +2762,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (unlikely (err)) goto err_destroy; - err =eb_pin_engine (&eb, file, args); + err =eb_pin_engine (&eb);if (unlikely (err)) goto err_context;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 10/22 drm/i915: Pin engine before pinning all objects, v3.
In reply to Maarten LankhorstWe want to lock all gem objects, including the engine context objects, rework the throttling to ensure that we can do this. Now we only throttle once, but can take eb_pin_engine while acquiring objects. This means we will have to drop the lock to wait. If we don't have to throttle we can still take the fastpath, if not we will take the slowpath and wait for the throttle request while unlocked.
The engine has to be pinned as first step, otherwise gpu relocations won't work.
Changes since v1:
- Only need to get a throttled request in the fastpath, no need for a global flag any more.
- Always free the waited request correctly.
Changes since v2:
- Use intel_engine_pm_get()/put() to keeep engine pool alive during EDEADLK handling.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 174 ++++++++++++------ 1 file changed, 118 insertions(+), 56 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +118 -56
@@ -16,6 +16,7 @@#include "gem/i915_gem_ioctls.h" #include "gt/intel_context.h" #include "gt/intel_engine_pool.h" +#include "gt/intel_engine_pm.h" #include "gt/intel_gt.h" #include "gt/intel_gt_pm.h" #include "gt/intel_ring.h"
@@ -55,7 +56,8 @@ enum {#define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE)#define __EXEC_HAS_RELOC BIT(31) -#define __EXEC_INTERNAL_FLAGS (~0u << 31) +#define __EXEC_ENGINE_PINNED BIT(30) +#define __EXEC_INTERNAL_FLAGS (~0u << 30)#define UPDATE PIN_OFFSET_FIXED#define BATCH_OFFSET_BIAS (256*1024)
@@ -288,6 +290,9 @@ struct i915_execbuffer { };static int eb_parse (struct i915_execbuffer *eb); +static struct i915_request *eb_pin_engine (struct i915_execbuffer *eb, +bool throttle); +static void eb_unpin_engine (struct i915_execbuffer *eb);static inline bool eb_use_cmdparser (const struct i915_execbuffer *eb) {
@@ -896,7 +901,7 @@ eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) } } -static void eb_release_vmas (const struct i915_execbuffer *eb,bool final) +static void eb_release_vmas (struct i915_execbuffer *eb,bool final) {const unsigned int count = eb->buffer_count;unsigned int i;
@@ -913,6 +918,8 @@ static void eb_release_vmas(const struct i915_execbuffer *eb, bool final)if (final)i915_vma_put (vma); } + +eb_unpin_engine (eb); }static void eb_destroy (const struct i915_execbuffer *eb)
@@ -1713,7 +1720,8 @@ static int eb_prefault_relocations(const struct i915_execbuffer *eb)return 0 ; } -static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) +static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb, +struct i915_request *rq) {bool have_copy = false;struct eb_vma *ev;
@@ -1729,6 +1737,21 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb)eb_release_vmas (eb, false);i915_gem_ww_ctx_fini (&eb->ww); +if (rq) { +/* nonblocking is always false */ +if (i915_request_wait (rq,I915_WAIT_INTERRUPTIBLE , +MAX_SCHEDULE_TIMEOUT ) <0 ) { +i915_request_put (rq); + rq =NULL ; + + err = -EINTR ; + goto err_relock; + } + +i915_request_put (rq); + rq =NULL ; + } +/* * We take 3 passes through the slowpatch. *
@@ -1752,14 +1775,25 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) err =0 ; } -flush_workqueue (eb->i915->mm.userptr_wq); +if (!err) +flush_workqueue (eb->i915->mm.userptr_wq); +err_relock:i915_gem_ww_ctx_init (&eb->ww, true);if (err) goto out;/* reacquire the objects */ repeat_validate: + rq =eb_pin_engine (eb, false); +if (IS_ERR (rq)) { + err =PTR_ERR (rq); + goto err; + } + +/* We didn't throttle, should be NULL */ +GEM_WARN_ON (rq); + err =eb_validate_vmas (eb);if (err) goto err;
@@ -1823,14 +1857,47 @@ static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb) } } +if (rq) +i915_request_put (rq); +return err; }static int eb_relocate_parse (struct i915_execbuffer *eb) {int err; +struct i915_request *rq =NULL ; +bool throttle = true; retry: + rq =eb_pin_engine (eb, throttle); +if (IS_ERR (rq)) { + err =PTR_ERR (rq); + rq =NULL ; +if (err != -EDEADLK ) +return err; + + goto err; + } + +if (rq) { +bool nonblock = eb->file->filp->f_flags &O_NONBLOCK ; + +/* Need to drop all locks now for throttling, take slowpath */ + err =i915_request_wait (rq,I915_WAIT_INTERRUPTIBLE ,0 ); +if (err == -ETIME ) { +if (nonblock) { + err = -EWOULDBLOCK ; +i915_request_put (rq); + goto err; + } + goto slow; + } + } + +/* only throttle once, even if we didn't need to throttle */ + throttle = false; + err =eb_validate_vmas (eb);if (err == -EAGAIN ) goto slow;
@@ -1863,7 +1930,7 @@ static int eb_relocate_parse(struct i915_execbuffer *eb)return err; slow: - err =eb_relocate_parse_slow (eb); + err =eb_relocate_parse_slow (eb, rq);if (err)/* * If the user expects the execobject.offset and
@@ -2288,7 +2355,7 @@ static const enum intel_engine_id user_ring_map[] = { [I915_EXEC_VEBOX] = VECS0 }; -static struct i915_request *eb_throttle (struct intel_context *ce) +static struct i915_request *eb_throttle (struct i915_execbuffer *eb,struct intel_context *ce) {struct intel_ring *ring = ce->ring;struct intel_timeline *tl = ce->timeline;
@@ -2322,22 +2389,17 @@ static struct i915_request *eb_throttle(struct intel_context *ce)return i915_request_get (rq); } -static int __eb_pin_engine (struct i915_execbuffer *eb,struct intel_context *ce) +static struct i915_request *eb_pin_engine (struct i915_execbuffer *eb,bool throttle) { +struct intel_context *ce = eb->context;struct intel_timeline *tl; -struct i915_request *rq; +struct i915_request *rq =NULL ;int err; -/* -* ABI: Before userspace accesses the GPU (e.g. execbuffer), report -* EIO if the GPU is already wedged. -*/ - err =intel_gt_terminally_wedged (ce->engine->gt); -if (err) -return err; +GEM_BUG_ON (eb->args->flags & __EXEC_ENGINE_PINNED);if (unlikely (intel_context_is_banned (ce))) -return -EIO ; +return ERR_PTR (-EIO );/* * Pinning the contexts may generate requests in order to acquire
@@ -2346,7 +2408,7 @@ static int __eb_pin_engine(struct i915_execbuffer *eb, struct intel_context *ce)*/ err =intel_context_pin (ce);if (err) -return err; +return ERR_PTR (err);/* * Take a local wakeref for preparing to dispatch the execbuf as
@@ -2358,45 +2420,17 @@ static int __eb_pin_engine(struct i915_execbuffer *eb, struct intel_context *ce)*/ tl =intel_context_timeline_lock (ce);if (IS_ERR (tl)) { - err =PTR_ERR (tl); - goto err_unpin; +intel_context_unpin (ce); +return ERR_CAST (tl); }intel_context_enter (ce); - rq =eb_throttle (ce); - +if (throttle) + rq =eb_throttle (eb, ce);intel_context_timeline_unlock (tl); -if (rq) { -bool nonblock = eb->file->filp->f_flags &O_NONBLOCK ; -long timeout; - - timeout =MAX_SCHEDULE_TIMEOUT ; -if (nonblock) - timeout =0 ; - - timeout =i915_request_wait (rq, -I915_WAIT_INTERRUPTIBLE , - timeout); -i915_request_put (rq); - -if (timeout <0 ) { - err = nonblock ? -EWOULDBLOCK : timeout; - goto err_exit; - } - } - - eb->engine = ce->engine; - eb->context = ce; -return 0 ; - -err_exit: -mutex_lock (&tl->mutex); -intel_context_exit (ce); -intel_context_timeline_unlock (tl); -err_unpin: -intel_context_unpin (ce); -return err; + eb->args->flags |= __EXEC_ENGINE_PINNED; +return rq; }static void eb_unpin_engine (struct i915_execbuffer *eb)
@@ -2404,6 +2438,11 @@ static void eb_unpin_engine(struct i915_execbuffer *eb)struct intel_context *ce = eb->context;struct intel_timeline *tl = ce->timeline; +if (!(eb->args->flags & __EXEC_ENGINE_PINNED)) +return ; + + eb->args->flags &= ~__EXEC_ENGINE_PINNED; +mutex_lock (&tl->mutex);intel_context_exit (ce);mutex_unlock (&tl->mutex);
@@ -2455,7 +2494,7 @@ eb_select_legacy_ring(struct i915_execbuffer *eb) }static int -eb_pin_engine (struct i915_execbuffer *eb) +eb_select_engine (struct i915_execbuffer *eb) {struct intel_context *ce;unsigned int idx;
@@ -2470,12 +2509,35 @@ eb_pin_engine(struct i915_execbuffer *eb)if (IS_ERR (ce))return PTR_ERR (ce); - err =__eb_pin_engine (eb, ce); -intel_context_put (ce); +/* +* ABI: Before userspace accesses the GPU (e.g. execbuffer), report +* EIO if the GPU is already wedged. +*/ + err =intel_gt_terminally_wedged (ce->engine->gt); +if (err) { +intel_context_put (ce); +return err; + } + + eb->context = ce; + eb->engine = ce->engine; +/* +* Make sure engine pool stays alive even if we call intel_context_put +* during ww handling. The pool is destroyed when last pm reference +* is dropped, which breaks our -EDEADLK handling. +*/ +intel_engine_pm_get (eb->engine);return err; } +static void +eb_put_engine (struct i915_execbuffer *eb) +{ +intel_context_put (eb->context); +intel_engine_pm_put (eb->engine); +} +static void __free_fence_array (struct drm_syncobj **fences,unsigned int n) {
@@ -2762,7 +2824,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (unlikely (err)) goto err_destroy; - err =eb_pin_engine (&eb); + err =eb_select_engine (&eb);if (unlikely (err)) goto err_context;
@@ -2900,7 +2962,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,WARN_ON (err == -EDEADLK );i915_gem_ww_ctx_fini (&eb.ww); err_engine: -eb_unpin_engine (&eb); +eb_put_engine (&eb); err_context:i915_gem_context_put (eb.gem_context); err_destroy:
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 11/22 drm/i915: Rework intel_context pinning to do everything outside of pin_mutex
In reply to Maarten LankhorstInstead of doing everything inside of pin_mutex, we move all pinning outside. Because i915_active has its own reference counting and pinning is also having the same issues vs mutexes, we make sure everything is pinned first, so the pinning in i915_active only needs to bump refcounts. This allows us to take pin refcounts correctly all the time.
--- drivers/gpu/drm/i915/gt/intel_context.c | 233 +++++++++++------- drivers/gpu/drm/i915/gt/intel_context_types.h | 4 +- drivers/gpu/drm/i915/gt/intel_lrc.c | 34 ++- drivers/gpu/drm/i915/gt/intel_renderstate.c | 1 - .../gpu/drm/i915/gt/intel_ring_submission.c | 13 +- drivers/gpu/drm/i915/gt/mock_engine.c | 13 +- 6 files changed, 191 insertions(+), 107 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +142 -91
@@ -93,79 +93,6 @@ static void intel_context_active_release(struct intel_context *ce)i915_active_release (&ce->active); } -int __intel_context_do_pin (struct intel_context *ce) -{ -int err; - -if (unlikely (!test_bit (CONTEXT_ALLOC_BIT , &ce->flags))) { - err =intel_context_alloc_state (ce); -if (err) -return err; - } - - err =i915_active_acquire (&ce->active); -if (err) -return err; - -if (mutex_lock_interruptible (&ce->pin_mutex)) { - err = -EINTR ; - goto out_release; - } - -if (unlikely (intel_context_is_closed (ce))) { - err = -ENOENT ; - goto out_unlock; - } - -if (likely (!atomic_add_unless (&ce->pin_count,1 ,0 ))) { - err =intel_context_active_acquire (ce); -if (unlikely (err)) - goto out_unlock; - - err = ce->ops->pin (ce); -if (unlikely (err)) - goto err_active; - -CE_TRACE (ce,"pin ring:{start:%08x, head:%04x, tail:%04x}\n" , -i915_ggtt_offset (ce->ring->vma), - ce->ring->head, ce->ring->tail); - -smp_mb__before_atomic ();/* flush pin before it is visible */ -atomic_inc (&ce->pin_count); - } - -GEM_BUG_ON (!intel_context_is_pinned (ce));/* no overflow! */ -GEM_BUG_ON (i915_active_is_idle (&ce->active)); - goto out_unlock; - -err_active: -intel_context_active_release (ce); -out_unlock: -mutex_unlock (&ce->pin_mutex); -out_release: -i915_active_release (&ce->active); -return err; -} - -void intel_context_unpin (struct intel_context *ce) -{ -if (!atomic_dec_and_test (&ce->pin_count)) -return ; - -CE_TRACE (ce,"unpin\n" ); - ce->ops->unpin (ce); - -/* -* Once released, we may asynchronously drop the active reference. -* As that may be the only reference keeping the context alive, -* take an extra now so that it is not freed before we finish -* dereferencing it. -*/ -intel_context_get (ce); -intel_context_active_release (ce); -intel_context_put (ce); -} -static int __context_pin_state (struct i915_vma *vma) {unsigned int bias =i915_ggtt_pin_bias (vma) |PIN_OFFSET_BIAS ;
@@ -225,6 +152,138 @@ static void __ring_retire(struct intel_ring *ring)i915_active_release (&ring->vma->active); } +static int intel_context_pre_pin (struct intel_context *ce) +{ +int err; + +CE_TRACE (ce,"active\n" ); + + err =__ring_active (ce->ring); +if (err) +return err; + + err =intel_timeline_pin (ce->timeline); +if (err) + goto err_ring; + +if (!ce->state) +return 0 ; + + err =__context_pin_state (ce->state); +if (err) + goto err_timeline; + + +return 0 ; + +err_timeline: +intel_timeline_unpin (ce->timeline); +err_ring: +__ring_retire (ce->ring); +return err; +} + +static void intel_context_post_unpin (struct intel_context *ce) +{ +if (ce->state) +__context_unpin_state (ce->state); + +intel_timeline_unpin (ce->timeline); +__ring_retire (ce->ring); +} + +int __intel_context_do_pin (struct intel_context *ce) +{ +bool handoff = false; +void *vaddr; +int err =0 ; + +if (unlikely (!test_bit (CONTEXT_ALLOC_BIT , &ce->flags))) { + err =intel_context_alloc_state (ce); +if (err) +return err; + } + +/* +* We always pin the context/ring/timeline here, to ensure a pin +* refcount for __intel_context_active(), which prevent a lock +* inversion of ce->pin_mutex vs dma_resv_lock(). +*/ + err =intel_context_pre_pin (ce); +if (err) +return err; + + err =i915_active_acquire (&ce->active); +if (err) + goto err_ctx_unpin; + + err = ce->ops->pre_pin (ce, &vaddr); +if (err) + goto err_release; + + err =mutex_lock_interruptible (&ce->pin_mutex); +if (err) + goto err_post_unpin; + +if (unlikely (intel_context_is_closed (ce))) { + err = -ENOENT ; + goto err_unlock; + } + +if (likely (!atomic_add_unless (&ce->pin_count,1 ,0 ))) { + err =intel_context_active_acquire (ce); +if (unlikely (err)) + goto err_unlock; + + err = ce->ops->pin (ce, vaddr); +if (err) { +intel_context_active_release (ce); + goto err_unlock; + } + +CE_TRACE (ce,"pin ring:{start:%08x, head:%04x, tail:%04x}\n" , +i915_ggtt_offset (ce->ring->vma), + ce->ring->head, ce->ring->tail); + + handoff = true; +smp_mb__before_atomic ();/* flush pin before it is visible */ +atomic_inc (&ce->pin_count); + } + +GEM_BUG_ON (!intel_context_is_pinned (ce));/* no overflow! */ + +err_unlock: +mutex_unlock (&ce->pin_mutex); +err_post_unpin: +if (!handoff) + ce->ops->post_unpin (ce); +err_release: +i915_active_release (&ce->active); +err_ctx_unpin: +intel_context_post_unpin (ce); +return err; +} + +void intel_context_unpin (struct intel_context *ce) +{ +if (!atomic_dec_and_test (&ce->pin_count)) +return ; + +CE_TRACE (ce,"unpin\n" ); + ce->ops->unpin (ce); + ce->ops->post_unpin (ce); + +/* +* Once released, we may asynchronously drop the active reference. +* As that may be the only reference keeping the context alive, +* take an extra now so that it is not freed before we finish +* dereferencing it. +*/ +intel_context_get (ce); +intel_context_active_release (ce); +intel_context_put (ce); +} +__i915_active_call static void __intel_context_retire(struct i915_active *active) {
@@ -235,43 +294,35 @@ static void __intel_context_retire(struct i915_active *active) intel_context_get_avg_runtime_ns(ce));set_bit (CONTEXT_VALID_BIT , &ce->flags); -if (ce->state) -__context_unpin_state (ce->state); - -intel_timeline_unpin (ce->timeline); -__ring_retire (ce->ring); - +intel_context_post_unpin (ce);intel_context_put (ce); } +__i915_active_call static int __intel_context_active(struct i915_active *active) {struct intel_context *ce =container_of (active,typeof (*ce), active);int err; -CE_TRACE (ce,"active\n" ); -intel_context_get (ce); +/* everything should already be activated by intel_context_pre_pin() */ err =__ring_active (ce->ring); -if (err) +if (GEM_WARN_ON (err)) goto err_put; err =intel_timeline_pin (ce->timeline); -if (err) +if (GEM_WARN_ON (err)) goto err_ring; -if (!ce->state) -return 0 ; - - err =__context_pin_state (ce->state); -if (err) - goto err_timeline; +if (ce->state) { +GEM_WARN_ON (!i915_active_acquire_if_busy (&ce->state->active)); +__i915_vma_pin (ce->state); +i915_vma_make_unshrinkable (ce->state); + }return 0 ; -err_timeline: -intel_timeline_unpin (ce->timeline); err_ring:__ring_retire (ce->ring); err_put:
drivers/gpu/drm/i915/gt/intel_context_types.h +3 -1
@@ -30,8 +30,10 @@ struct intel_ring;struct intel_context_ops {int (*alloc)(struct intel_context *ce); -int (*pin)(struct intel_context *ce); +int (*pre_pin)(struct intel_context *ce,void **vaddr); +int (*pin)(struct intel_context *ce,void *vaddr);void (*unpin)(struct intel_context *ce); +void (*post_unpin)(struct intel_context *ce);void (*enter)(struct intel_context *ce);void (*exit)(struct intel_context *ce);
drivers/gpu/drm/i915/gt/intel_lrc.c +22 -12
@@ -3023,7 +3023,10 @@ static void execlists_context_unpin(struct intel_context *ce) {check_redzone ((void *)ce->lrc_reg_state -LRC_STATE_PN *PAGE_SIZE , ce->engine); +} +static void execlists_context_post_unpin (struct intel_context *ce) +{i915_gem_object_unpin_map (ce->state->obj); }
@@ -3053,20 +3056,23 @@ __execlists_update_reg_state(const struct intel_context *ce, }static int -__execlists_context_pin (struct intel_context *ce, -struct intel_engine_cs *engine) +execlists_context_pre_pin (struct intel_context *ce,void **vaddr) { -void *vaddr; -GEM_BUG_ON (!ce->state);GEM_BUG_ON (!i915_vma_is_pinned (ce->state)); - vaddr =i915_gem_object_pin_map (ce->state->obj, -i915_coherent_map_type (engine->i915) | + *vaddr =i915_gem_object_pin_map (ce->state->obj, +i915_coherent_map_type (ce->engine->i915) |I915_MAP_OVERRIDE ); -if (IS_ERR (vaddr)) -return PTR_ERR (vaddr); +return PTR_ERR_OR_ZERO (*vaddr); +} + +static int +__execlists_context_pin (struct intel_context *ce, +struct intel_engine_cs *engine, +void *vaddr) +{ ce->lrc_desc =lrc_descriptor (ce, engine) |CTX_DESC_FORCE_RESTORE ; ce->lrc_reg_state = vaddr +LRC_STATE_PN *PAGE_SIZE ;__execlists_update_reg_state (ce, engine, ce->ring->tail);
@@ -3074,9 +3080,9 @@ __execlists_context_pin(struct intel_context *ce,return 0 ; } -static int execlists_context_pin (struct intel_context *ce) +static int execlists_context_pin (struct intel_context *ce,void *vaddr) { -return __execlists_context_pin (ce, ce->engine); +return __execlists_context_pin (ce, ce->engine, vaddr); }static int execlists_context_alloc (struct intel_context *ce)
@@ -3102,8 +3108,10 @@ static void execlists_context_reset(struct intel_context *ce)static const struct intel_context_ops execlists_context_ops = { .alloc = execlists_context_alloc, + .pre_pin = execlists_context_pre_pin, .pin = execlists_context_pin, .unpin = execlists_context_unpin, + .post_unpin = execlists_context_post_unpin, .enter = intel_context_enter_engine, .exit = intel_context_exit_engine,
@@ -4880,13 +4888,13 @@ static int virtual_context_alloc(struct intel_context *ce)return __execlists_context_alloc (ce, ve->siblings[0 ]); } -static int virtual_context_pin (struct intel_context *ce) +static int virtual_context_pin (struct intel_context *ce,void *vaddr) {struct virtual_engine *ve =container_of (ce,typeof (*ve), context);int err;/* Note: we must use a real engine class for setting up reg state */ - err =__execlists_context_pin (ce, ve->siblings[0 ]); + err =__execlists_context_pin (ce, ve->siblings[0 ], vaddr);if (err)return err;
@@ -4919,8 +4927,10 @@ static void virtual_context_exit(struct intel_context *ce)static const struct intel_context_ops virtual_context_ops = { .alloc = virtual_context_alloc, + .pre_pin = execlists_context_pre_pin, .pin = virtual_context_pin, .unpin = execlists_context_unpin, + .post_unpin = execlists_context_post_unpin, .enter = virtual_context_enter, .exit = virtual_context_exit,
drivers/gpu/drm/i915/gt/intel_renderstate.c +0 -1
@@ -270,7 +270,6 @@ void intel_renderstate_fini(struct intel_renderstate *so, struct intel_context *ce) {i915_vma_unpin_and_release (&so->vma,0 ); -intel_context_unpin (ce);i915_gem_ww_ctx_fini (&so->ww); }
drivers/gpu/drm/i915/gt/intel_ring_submission.c +12 -1
@@ -1206,6 +1206,10 @@ static void __context_unpin_ppgtt(struct intel_context *ce) }static void ring_context_unpin (struct intel_context *ce) +{ +} + +static void ring_context_post_unpin (struct intel_context *ce) {__context_unpin_ppgtt (ce); }
@@ -1303,11 +1307,16 @@ static int ring_context_alloc(struct intel_context *ce)return 0 ; } -static int ring_context_pin (struct intel_context *ce) +static int ring_context_pre_pin (struct intel_context *ce,void **unused) {return __context_pin_ppgtt (ce); } +static int ring_context_pin (struct intel_context *ce,void *unused) +{ +return 0 ; +} +static void ring_context_reset (struct intel_context *ce) {intel_ring_reset (ce->ring, ce->ring->emit);
@@ -1316,8 +1325,10 @@ static void ring_context_reset(struct intel_context *ce)static const struct intel_context_ops ring_context_ops = { .alloc = ring_context_alloc, + .pre_pin = ring_context_pre_pin, .pin = ring_context_pin, .unpin = ring_context_unpin, + .post_unpin = ring_context_post_unpin, .enter = intel_context_enter_engine, .exit = intel_context_exit_engine,
drivers/gpu/drm/i915/gt/mock_engine.c +12 -1
@@ -132,6 +132,10 @@ static void mock_context_unpin(struct intel_context *ce) { } +static void mock_context_post_unpin (struct intel_context *ce) +{ +} +static void mock_context_destroy (struct kref *ref) {struct intel_context *ce =container_of (ref,typeof (*ce), ref);
@@ -165,7 +169,12 @@ static int mock_context_alloc(struct intel_context *ce)return 0 ; } -static int mock_context_pin (struct intel_context *ce) +static int mock_context_pre_pin (struct intel_context *ce,void **unused) +{ +return 0 ; +} + +static int mock_context_pin (struct intel_context *ce,void *unused) {return 0 ; }
@@ -177,8 +186,10 @@ static void mock_context_reset(struct intel_context *ce)static const struct intel_context_ops mock_context_ops = { .alloc = mock_context_alloc, + .pre_pin = mock_context_pre_pin, .pin = mock_context_pin, .unpin = mock_context_unpin, + .post_unpin = mock_context_post_unpin, .enter = intel_context_enter_engine, .exit = intel_context_exit_engine,
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 12/22 drm/i915: Make sure execbuffer always passes ww state to i915_vma_pin.
In reply to Maarten LankhorstAs a preparation step for full object locking and wait/wound handling during pin and object mapping, ensure that we always pass the ww context in i915_gem_execbuffer.c to i915_vma_pin, use lockdep to ensure this happens.
This also requires changing the order of eb_parse slightly, to ensure we pass ww at a point where we could still handle -EDEADLK safely.
--- drivers/gpu/drm/i915/display/intel_display.c | 2 +- drivers/gpu/drm/i915/gem/i915_gem_context.c | 4 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 125 ++++++++++-------- drivers/gpu/drm/i915/gt/gen6_ppgtt.c | 4 +- drivers/gpu/drm/i915/gt/gen6_ppgtt.h | 4 +- drivers/gpu/drm/i915/gt/intel_context.c | 65 +++++---- drivers/gpu/drm/i915/gt/intel_context.h | 13 ++ drivers/gpu/drm/i915/gt/intel_context_types.h | 3 +- drivers/gpu/drm/i915/gt/intel_engine_cs.c | 2 +- drivers/gpu/drm/i915/gt/intel_gt.c | 2 +- drivers/gpu/drm/i915/gt/intel_lrc.c | 5 +- drivers/gpu/drm/i915/gt/intel_renderstate.c | 2 +- drivers/gpu/drm/i915/gt/intel_ring.c | 10 +- drivers/gpu/drm/i915/gt/intel_ring.h | 3 +- .../gpu/drm/i915/gt/intel_ring_submission.c | 15 +-- drivers/gpu/drm/i915/gt/intel_timeline.c | 12 +- drivers/gpu/drm/i915/gt/intel_timeline.h | 3 +- drivers/gpu/drm/i915/gt/mock_engine.c | 3 +- drivers/gpu/drm/i915/gt/selftest_timeline.c | 4 +- drivers/gpu/drm/i915/gt/uc/intel_guc.c | 2 +- drivers/gpu/drm/i915/i915_drv.h | 13 +- drivers/gpu/drm/i915/i915_gem.c | 11 +- drivers/gpu/drm/i915/i915_vma.c | 13 +- drivers/gpu/drm/i915/i915_vma.h | 13 +- 24 files changed, 207 insertions(+), 126 deletions(-)
drivers/gpu/drm/i915/display/intel_display.c +1 -1
@@ -3441,7 +3441,7 @@ initial_plane_vma(struct drm_i915_private *i915,if (IS_ERR (vma)) goto err_obj; -if (i915_ggtt_pin (vma,0 ,PIN_MAPPABLE |PIN_OFFSET_FIXED | base)) +if (i915_ggtt_pin (vma,NULL ,0 ,PIN_MAPPABLE |PIN_OFFSET_FIXED | base)) goto err_obj; if (i915_gem_object_is_tiled(obj) &&
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -2
@@ -1145,7 +1145,7 @@ static int context_barrier_task(struct i915_gem_context *ctx,i915_gem_ww_ctx_init (&ww, true); retry: - err =intel_context_pin (ce); + err =intel_context_pin_ww (ce, &ww);if (err) goto err;
@@ -1238,7 +1238,7 @@ static int pin_ppgtt_update(struct intel_context *ce, struct i915_gem_ww_ctx *wwif (!HAS_LOGICAL_RING_CONTEXTS (vm->i915))/* ppGTT is not part of the legacy context image */ -return gen6_ppgtt_pin (i915_vm_to_ppgtt (vm)); +return gen6_ppgtt_pin (i915_vm_to_ppgtt (vm), ww);return 0 ; }
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +69 -56
273 lines not on this page · raw message
drivers/gpu/drm/i915/gt/gen6_ppgtt.c +2 -2
@@ -376,7 +376,7 @@ static struct i915_vma *pd_vma_create(struct gen6_ppgtt *ppgtt, int size)return vma; } -int gen6_ppgtt_pin (struct i915_ppgtt *base) +int gen6_ppgtt_pin (struct i915_ppgtt *base,struct i915_gem_ww_ctx *ww) {struct gen6_ppgtt *ppgtt =to_gen6_ppgtt (base);int err;
@@ -402,7 +402,7 @@ int gen6_ppgtt_pin(struct i915_ppgtt *base)*/ err =0 ;if (!atomic_read (&ppgtt->pin_count)) - err =i915_ggtt_pin (ppgtt->vma,GEN6_PD_ALIGN ,PIN_HIGH ); + err =i915_ggtt_pin (ppgtt->vma, ww,GEN6_PD_ALIGN ,PIN_HIGH );if (!err)atomic_inc (&ppgtt->pin_count);mutex_unlock (&ppgtt->pin_mutex);
drivers/gpu/drm/i915/gt/gen6_ppgtt.h +3 -1
@@ -8,6 +8,8 @@#include "intel_gtt.h" +struct i915_gem_ww_ctx ; +struct gen6_ppgtt {struct i915_ppgtt base;
@@ -66,7 +68,7 @@ static inline struct gen6_ppgtt *to_gen6_ppgtt(struct i915_ppgtt *base) (pt =i915_pt_entry (pd, iter), true); \ ++iter) -int gen6_ppgtt_pin (struct i915_ppgtt *base); +int gen6_ppgtt_pin (struct i915_ppgtt *base,struct i915_gem_ww_ctx *ww);void gen6_ppgtt_unpin (struct i915_ppgtt *base);void gen6_ppgtt_unpin_all (struct i915_ppgtt *base);void gen6_ppgtt_enable (struct intel_gt *gt);
drivers/gpu/drm/i915/gt/intel_context.c +41 -24
148 lines not on this page · raw message
drivers/gpu/drm/i915/gt/intel_context.h +13 -0
@@ -25,6 +25,8 @@ ##__VA_ARGS__); \ }while (0 ) +struct i915_gem_ww_ctx; +void intel_context_init (struct intel_context *ce,struct intel_engine_cs *engine);void intel_context_fini (struct intel_context *ce);
@@ -81,6 +83,8 @@ static inline void intel_context_unlock_pinned(struct intel_context *ce) }int __intel_context_do_pin (struct intel_context *ce); +int __intel_context_do_pin_ww (struct intel_context *ce, +struct i915_gem_ww_ctx *ww);static inline bool intel_context_pin_if_active (struct intel_context *ce) {
@@ -95,6 +99,15 @@ static inline int intel_context_pin(struct intel_context *ce)return __intel_context_do_pin (ce); } +static inline int intel_context_pin_ww (struct intel_context *ce, +struct i915_gem_ww_ctx *ww) +{ +if (likely (intel_context_pin_if_active (ce))) +return 0 ; + +return __intel_context_do_pin_ww (ce, ww); +} +static inline void __intel_context_pin (struct intel_context *ce) {GEM_BUG_ON (!intel_context_is_pinned (ce));
drivers/gpu/drm/i915/gt/intel_context_types.h +2 -1
@@ -23,6 +23,7 @@DECLARE_EWMA (runtime,3 ,8 );struct i915_gem_context ; +struct i915_gem_ww_ctx ;struct i915_vma ;struct intel_context ;struct intel_ring ;
@@ -30,7 +31,7 @@ struct intel_ring;struct intel_context_ops {int (*alloc)(struct intel_context *ce); -int (*pre_pin)(struct intel_context *ce,void **vaddr); +int (*pre_pin)(struct intel_context *ce,struct i915_gem_ww_ctx *ww,void **vaddr);int (*pin)(struct intel_context *ce,void *vaddr);void (*unpin)(struct intel_context *ce);void (*post_unpin)(struct intel_context *ce);
drivers/gpu/drm/i915/gt/intel_engine_cs.c +1 -1
@@ -546,7 +546,7 @@ static int pin_ggtt_status_page(struct intel_engine_cs *engine,else flags =PIN_HIGH ; -return i915_ggtt_pin (vma,0 , flags); +return i915_ggtt_pin (vma,NULL ,0 , flags); }static int init_status_page (struct intel_engine_cs *engine)
drivers/gpu/drm/i915/gt/intel_gt.c +1 -1
@@ -344,7 +344,7 @@ static int intel_gt_init_scratch(struct intel_gt *gt, unsigned int size) goto err_unref; } - ret =i915_ggtt_pin (vma,0 ,PIN_HIGH ); + ret =i915_ggtt_pin (vma,NULL ,0 ,PIN_HIGH );if (ret) goto err_unref;
drivers/gpu/drm/i915/gt/intel_lrc.c +3 -2
@@ -3056,7 +3056,8 @@ __execlists_update_reg_state(const struct intel_context *ce, }static int -execlists_context_pre_pin (struct intel_context *ce,void **vaddr) +execlists_context_pre_pin (struct intel_context *ce, +struct i915_gem_ww_ctx *ww,void **vaddr) {GEM_BUG_ON (!ce->state);GEM_BUG_ON (!i915_vma_is_pinned (ce->state));
@@ -3418,7 +3419,7 @@ static int lrc_setup_wa_ctx(struct intel_engine_cs *engine) goto err; } - err =i915_ggtt_pin (vma,0 ,PIN_HIGH ); + err =i915_ggtt_pin (vma,NULL ,0 ,PIN_HIGH );if (err) goto err;
drivers/gpu/drm/i915/gt/intel_renderstate.c +1 -1
@@ -195,7 +195,7 @@ int intel_renderstate_init(struct intel_renderstate *so,i915_gem_ww_ctx_init (&so->ww, true); retry: - err =intel_context_pin (ce); + err =intel_context_pin_ww (ce, &so->ww);if (err) goto err_fini;
drivers/gpu/drm/i915/gt/intel_ring.c +8 -2
@@ -21,7 +21,13 @@ unsigned int intel_ring_update_space(struct intel_ring *ring)return space; } -int intel_ring_pin (struct intel_ring *ring) +void __intel_ring_pin (struct intel_ring *ring) +{ +GEM_BUG_ON (!atomic_read (&ring->pin_count)); +atomic_inc (&ring->pin_count); +} + +int intel_ring_pin (struct intel_ring *ring,struct i915_gem_ww_ctx *ww) {struct i915_vma *vma = ring->vma;unsigned int flags;
@@ -39,7 +45,7 @@ int intel_ring_pin(struct intel_ring *ring)else flags |=PIN_HIGH ; - ret =i915_ggtt_pin (vma,0 , flags); + ret =i915_ggtt_pin (vma, ww,0 , flags);if (unlikely (ret)) goto err_unpin;
drivers/gpu/drm/i915/gt/intel_ring.h +2 -1
@@ -21,7 +21,8 @@ int intel_ring_cacheline_align(struct i915_request *rq);unsigned int intel_ring_update_space (struct intel_ring *ring); -int intel_ring_pin (struct intel_ring *ring); +void __intel_ring_pin (struct intel_ring *ring); +int intel_ring_pin (struct intel_ring *ring,struct i915_gem_ww_ctx *ww);void intel_ring_unpin (struct intel_ring *ring);void intel_ring_reset (struct intel_ring *ring,u32 tail);
drivers/gpu/drm/i915/gt/intel_ring_submission.c +6 -9
@@ -1184,14 +1184,16 @@ static void ring_context_destroy(struct kref *ref)intel_context_free (ce); } -static int __context_pin_ppgtt (struct intel_context *ce) +static int ring_context_pre_pin (struct intel_context *ce, +struct i915_gem_ww_ctx *ww, +void **unused) {struct i915_address_space *vm;int err =0 ; vm =vm_alias (ce->vm);if (vm) - err =gen6_ppgtt_pin (i915_vm_to_ppgtt ((vm))); + err =gen6_ppgtt_pin (i915_vm_to_ppgtt ((vm)), ww);return err; }
@@ -1307,11 +1309,6 @@ static int ring_context_alloc(struct intel_context *ce)return 0 ; } -static int ring_context_pre_pin (struct intel_context *ce,void **unused) -{ -return __context_pin_ppgtt (ce); -} -static int ring_context_pin (struct intel_context *ce,void *unused) {return 0 ;
@@ -2082,7 +2079,7 @@ int intel_ring_submission_setup(struct intel_engine_cs *engine) }GEM_BUG_ON (timeline->has_initial_breadcrumb); - err =intel_timeline_pin (timeline); + err =intel_timeline_pin (timeline,NULL );if (err) goto err_timeline;
@@ -2092,7 +2089,7 @@ int intel_ring_submission_setup(struct intel_engine_cs *engine) goto err_timeline_unpin; } - err =intel_ring_pin (ring); + err =intel_ring_pin (ring,NULL );if (err) goto err_ring;
drivers/gpu/drm/i915/gt/intel_timeline.c +9 -3
@@ -313,14 +313,20 @@ intel_timeline_create(struct intel_gt *gt, struct i915_vma *global_hwsp)return timeline; } -int intel_timeline_pin (struct intel_timeline *tl) +void __intel_timeline_pin (struct intel_timeline *tl) +{ +GEM_BUG_ON (!atomic_read (&tl->pin_count)); +atomic_inc (&tl->pin_count); +} + +int intel_timeline_pin (struct intel_timeline *tl,struct i915_gem_ww_ctx *ww) {int err;if (atomic_add_unless (&tl->pin_count,1 ,0 ))return 0 ; - err =i915_ggtt_pin (tl->hwsp_ggtt,0 ,PIN_HIGH ); + err =i915_ggtt_pin (tl->hwsp_ggtt, ww,0 ,PIN_HIGH );if (err)return err;
@@ -445,7 +451,7 @@ __intel_timeline_get_seqno(struct intel_timeline *tl, goto err_rollback; } - err =i915_ggtt_pin (vma,0 ,PIN_HIGH ); + err =i915_ggtt_pin (vma,NULL ,0 ,PIN_HIGH );if (err) {__idle_hwsp_free (vma->private, cacheline); goto err_rollback;
drivers/gpu/drm/i915/gt/intel_timeline.h +2 -1
@@ -76,7 +76,8 @@ static inline bool intel_timeline_sync_is_later(struct intel_timeline *tl,return __intel_timeline_sync_is_later (tl, fence->context, fence->seqno); } -int intel_timeline_pin (struct intel_timeline *tl); +void __intel_timeline_pin (struct intel_timeline *tl); +int intel_timeline_pin (struct intel_timeline *tl,struct i915_gem_ww_ctx *ww);void intel_timeline_enter (struct intel_timeline *tl); int intel_timeline_get_seqno(struct intel_timeline *tl, struct i915_request *rq,
drivers/gpu/drm/i915/gt/mock_engine.c +2 -1
@@ -169,7 +169,8 @@ static int mock_context_alloc(struct intel_context *ce)return 0 ; } -static int mock_context_pre_pin (struct intel_context *ce,void **unused) +static int mock_context_pre_pin (struct intel_context *ce, +struct i915_gem_ww_ctx *ww,void **unused) {return 0 ; }
drivers/gpu/drm/i915/gt/selftest_timeline.c +2 -2
@@ -454,7 +454,7 @@ tl_write(struct intel_timeline *tl, struct intel_engine_cs *engine, u32 value)struct i915_request *rq;int err; - err =intel_timeline_pin (tl); + err =intel_timeline_pin (tl,NULL );if (err) { rq =ERR_PTR (err); goto out;
@@ -664,7 +664,7 @@ static int live_hwsp_wrap(void *arg)if (!tl->has_initial_breadcrumb || !tl->hwsp_cacheline) goto out_free; - err =intel_timeline_pin (tl); + err =intel_timeline_pin (tl,NULL );if (err) goto out_free;
drivers/gpu/drm/i915/gt/uc/intel_guc.c +1 -1
@@ -677,7 +677,7 @@ struct i915_vma *intel_guc_allocate_vma(struct intel_guc *guc, u32 size) goto err; flags =PIN_OFFSET_BIAS |i915_ggtt_pin_bias (vma); - ret =i915_ggtt_pin (vma,0 , flags); + ret =i915_ggtt_pin (vma,NULL ,0 , flags);if (ret) { vma =ERR_PTR (ret); goto err;
drivers/gpu/drm/i915/i915_drv.h +10 -3
@@ -1729,11 +1729,18 @@ static inline void i915_gem_drain_workqueue(struct drm_i915_private *i915) }struct i915_vma *__must_check +i915_gem_object_ggtt_pin_ww (struct drm_i915_gem_object *obj, +struct i915_gem_ww_ctx *ww, +const struct i915_ggtt_view *view, +u64 size,u64 alignment,u64 flags); + +static inline struct i915_vma *__must_check i915_gem_object_ggtt_pin (struct drm_i915_gem_object *obj,const struct i915_ggtt_view *view, -u64 size, -u64 alignment, -u64 flags); +u64 size,u64 alignment,u64 flags) +{ +return i915_gem_object_ggtt_pin_ww (obj,NULL , view, size, alignment, flags); +}int i915_gem_object_unbind (struct drm_i915_gem_object *obj,unsigned long flags);
drivers/gpu/drm/i915/i915_gem.c +5 -6
@@ -945,11 +945,10 @@ void i915_gem_runtime_suspend(struct drm_i915_private *i915) }struct i915_vma * -i915_gem_object_ggtt_pin (struct drm_i915_gem_object *obj, -const struct i915_ggtt_view *view, -u64 size, -u64 alignment, -u64 flags) +i915_gem_object_ggtt_pin_ww (struct drm_i915_gem_object *obj, +struct i915_gem_ww_ctx *ww, +const struct i915_ggtt_view *view, +u64 size,u64 alignment,u64 flags) {struct drm_i915_private *i915 =to_i915 (obj->base.dev);struct i915_ggtt *ggtt = &i915->ggtt;
@@ -1017,7 +1016,7 @@ i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,return ERR_PTR (ret); } - ret =i915_vma_pin (vma, size, alignment, flags |PIN_GLOBAL ); + ret =i915_vma_pin_ww (vma, ww, size, alignment, flags |PIN_GLOBAL );if (ret)return ERR_PTR (ret);
drivers/gpu/drm/i915/i915_vma.c +10 -3
@@ -881,13 +881,19 @@ static void vma_unbind_pages(struct i915_vma *vma)__vma_put_pages (vma, count | count <<I915_VMA_PAGES_BIAS ); } -int i915_vma_pin (struct i915_vma *vma,u64 size,u64 alignment,u64 flags) +int i915_vma_pin_ww (struct i915_vma *vma,struct i915_gem_ww_ctx *ww, +u64 size,u64 alignment,u64 flags) {struct i915_vma_work *work =NULL ;intel_wakeref_t wakeref =0 ;unsigned int bound;int err; +#ifdef CONFIG_PROVE_LOCKING +if (debug_locks &&lockdep_is_held (&vma->vm->i915->drm.struct_mutex)) +WARN_ON (!ww); +#endif +BUILD_BUG_ON (PIN_GLOBAL !=I915_VMA_GLOBAL_BIND );BUILD_BUG_ON (PIN_USER !=I915_VMA_LOCAL_BIND );
@@ -1018,7 +1024,8 @@ static void flush_idle_contexts(struct intel_gt *gt)intel_gt_wait_for_idle (gt,MAX_SCHEDULE_TIMEOUT ); } -int i915_ggtt_pin (struct i915_vma *vma,u32 align,unsigned int flags) +int i915_ggtt_pin (struct i915_vma *vma,struct i915_gem_ww_ctx *ww, +u32 align,unsigned int flags) {struct i915_address_space *vm = vma->vm;int err;
@@ -1026,7 +1033,7 @@ int i915_ggtt_pin(struct i915_vma *vma, u32 align, unsigned int flags)GEM_BUG_ON (!i915_vma_is_ggtt (vma)); do { - err = i915_vma_pin(vma, 0, align, flags | PIN_GLOBAL); + err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL); if (err != -ENOSPC) { if (!err) { err = i915_vma_wait_for_bind(vma);
drivers/gpu/drm/i915/i915_vma.h +11 -2
@@ -236,8 +236,17 @@ static inline void i915_vma_unlock(struct i915_vma *vma) }int __must_check -i915_vma_pin (struct i915_vma *vma,u64 size,u64 alignment,u64 flags); -int i915_ggtt_pin (struct i915_vma *vma,u32 align,unsigned int flags); +i915_vma_pin_ww (struct i915_vma *vma,struct i915_gem_ww_ctx *ww, +u64 size,u64 alignment,u64 flags); + +static inline int __must_check +i915_vma_pin (struct i915_vma *vma,u64 size,u64 alignment,u64 flags) +{ +return i915_vma_pin_ww (vma,NULL , size, alignment, flags); +} + +int i915_ggtt_pin (struct i915_vma *vma,struct i915_gem_ww_ctx *ww, +u32 align,unsigned int flags);static inline int i915_vma_pin_count (const struct i915_vma *vma) {
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 13/22 drm/i915: Convert i915_gem_object/client_blt.c to use ww locking as well, v2.
In reply to Maarten LankhorstThis is the last part outside of selftests that still don't use the correct lock ordering of timeline->mutex vs resv_lock.
With gem fixed, there are a few places that still get locking wrong:
- gvt/scheduler.c
- i915_perf.c
- Most if not all selftests.
Changes since v1:
- Add intel_engine_pm_get/put() calls to fix use-after-free when using intel_engine_get_pool().
--- .../gpu/drm/i915/gem/i915_gem_client_blt.c | 80 +++++++-- .../gpu/drm/i915/gem/i915_gem_object_blt.c | 156 +++++++++++------- .../gpu/drm/i915/gem/i915_gem_object_blt.h | 3 + 3 files changed, 165 insertions(+), 74 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_client_blt.c +62 -18
@@ -157,6 +157,7 @@ static void clear_pages_worker(struct work_struct *work)struct clear_pages_work *w =container_of (work,typeof (*w), work);struct drm_i915_gem_object *obj = w->sleeve->vma->obj;struct i915_vma *vma = w->sleeve->vma; +struct i915_gem_ww_ctx ww;struct i915_request *rq;struct i915_vma *batch;int err = w->dma.error;
@@ -172,17 +173,20 @@ static void clear_pages_worker(struct work_struct *work) obj->read_domains =I915_GEM_GPU_DOMAINS ; obj->write_domain =0 ; - err =i915_vma_pin (vma,0 ,0 ,PIN_USER ); -if (unlikely (err)) +i915_gem_ww_ctx_init (&ww, false); +intel_engine_pm_get (w->ce->engine); +retry: + err =intel_context_pin_ww (w->ce, &ww); +if (err) goto out_signal; - batch =intel_emit_vma_fill_blt (w->ce, vma, w->value); + batch =intel_emit_vma_fill_blt (w->ce, vma, &ww, w->value);if (IS_ERR (batch)) { err =PTR_ERR (batch); - goto out_unpin; + goto out_ctx; } - rq =intel_context_create_request (w->ce); + rq =i915_request_create (w->ce);if (IS_ERR (rq)) { err =PTR_ERR (rq); goto out_batch;
@@ -224,9 +228,19 @@ static void clear_pages_worker(struct work_struct *work)i915_request_add (rq); out_batch:intel_emit_vma_release (w->ce, batch); -out_unpin: -i915_vma_unpin (vma); +out_ctx: +intel_context_unpin (w->ce); out_signal: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); + +i915_vma_unpin (w->sleeve->vma); +intel_engine_pm_put (w->ce->engine); +if (unlikely (err)) {dma_fence_set_error (&w->dma, err);dma_fence_signal (&w->dma);
@@ -234,6 +248,45 @@ static void clear_pages_worker(struct work_struct *work) } } +static int pin_wait_clear_pages_work (struct clear_pages_work *w, +struct intel_context *ce) +{ +struct i915_vma *vma = w->sleeve->vma; +struct i915_gem_ww_ctx ww; +int err; + +i915_gem_ww_ctx_init (&ww, false); +retry: + err =i915_gem_object_lock (vma->obj, &ww); +if (err) + goto out; + + err =i915_vma_pin_ww (vma, &ww,0 ,0 ,PIN_USER ); +if (unlikely (err)) + goto out; + + err =i915_sw_fence_await_reservation (&w->wait, + vma->obj->base.resv,NULL , + true,I915_FENCE_TIMEOUT , +I915_FENCE_GFP ); +if (err) + goto err_unpin_vma; + +dma_resv_add_excl_fence (vma->obj->base.resv, &w->dma); + +err_unpin_vma: +if (err) +i915_vma_unpin (vma); +out: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); +return err; +} + static int __i915_sw_fence_callclear_pages_work_notify (struct i915_sw_fence *fence,enum i915_sw_fence_notify state)
@@ -287,18 +340,9 @@ int i915_gem_schedule_fill_pages_blt(struct drm_i915_gem_object *obj,dma_fence_init (&work->dma, &clear_pages_work_ops, &fence_lock,0 ,0 );i915_sw_fence_init (&work->wait, clear_pages_work_notify); -i915_gem_object_lock (obj,NULL ); - err =i915_sw_fence_await_reservation (&work->wait, - obj->base.resv,NULL , - true,I915_FENCE_TIMEOUT , -I915_FENCE_GFP ); -if (err <0 ) { + err =pin_wait_clear_pages_work (work, ce); +if (err <0 )dma_fence_set_error (&work->dma, err); - }else { -dma_resv_add_excl_fence (obj->base.resv, &work->dma); - err =0 ; - } -i915_gem_object_unlock (obj);dma_fence_get (&work->dma);i915_sw_fence_commit (&work->wait);
drivers/gpu/drm/i915/gem/i915_gem_object_blt.c +100 -56
304 lines not on this page · raw message
drivers/gpu/drm/i915/gem/i915_gem_object_blt.h +3 -0
@@ -14,12 +14,15 @@#include "i915_vma.h" struct drm_i915_gem_object ; +struct i915_gem_ww_ctx ;struct i915_vma *intel_emit_vma_fill_blt (struct intel_context *ce,struct i915_vma *vma, +struct i915_gem_ww_ctx *ww,u32 value);struct i915_vma *intel_emit_vma_copy_blt (struct intel_context *ce, +struct i915_gem_ww_ctx *ww,struct i915_vma *src,struct i915_vma *dst);
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 14/22 drm/i915: Kill last user of intel_context_create_request outside of selftests
In reply to Maarten LankhorstInstead of using intel_context_create_request(), use intel_context_pin() and i915_create_request directly.
Now all those calls are gone outside of selftests. :)
--- drivers/gpu/drm/i915/gt/intel_workarounds.c | 43 ++++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-)
drivers/gpu/drm/i915/gt/intel_workarounds.c +29 -14
@@ -1744,6 +1744,7 @@ static int engine_wa_list_verify(struct intel_context *ce,const struct i915_wa *wa;struct i915_request *rq;struct i915_vma *vma; +struct i915_gem_ww_ctx ww;unsigned int i;u32 *results;int err;
@@ -1756,29 +1757,34 @@ static int engine_wa_list_verify(struct intel_context *ce,return PTR_ERR (vma);intel_engine_pm_get (ce->engine); - rq =intel_context_create_request (ce); -intel_engine_pm_put (ce->engine); +i915_gem_ww_ctx_init (&ww, false); +retry: + err =i915_gem_object_lock (vma->obj, &ww); +if (err ==0 ) + err =intel_context_pin_ww (ce, &ww); +if (err) + goto err_pm; + + rq =i915_request_create (ce);if (IS_ERR (rq)) { err =PTR_ERR (rq); - goto err_vma; + goto err_unpin; } -i915_vma_lock (vma); err =i915_request_await_object (rq, vma->obj, true);if (err ==0 ) err =i915_vma_move_to_active (vma, rq,EXEC_OBJECT_WRITE ); -i915_vma_unlock (vma); -if (err) { -i915_request_add (rq); - goto err_vma; - } - - err =wa_list_srm (rq, wal, vma); -if (err) - goto err_vma; +if (err ==0 ) + err =wa_list_srm (rq, wal, vma);i915_request_get (rq); +if (err) +i915_request_set_error_once (rq, err);i915_request_add (rq); + +if (err) + goto err_rq; +if (i915_request_wait (rq,0 ,HZ /5 ) <0 ) { err = -ETIME ; goto err_rq;
@@ -1803,7 +1809,16 @@ static int engine_wa_list_verify(struct intel_context *ce, err_rq:i915_request_put (rq); -err_vma: +err_unpin: +intel_context_unpin (ce); +err_pm: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); +intel_engine_pm_put (ce->engine);i915_vma_unpin (vma);i915_vma_put (vma);return err;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 15/22 drm/i915: Convert i915_perf to ww locking as well
In reply to Maarten LankhorstWe have the ordering of timeline->mutex vs resv_lock wrong, convert the i915_pin_vma and intel_context_pin as well to future-proof this.
We may need to do future changes to do this more transaction-like, and only get down to a single i915_gem_ww_ctx, but for now this should work.
--- drivers/gpu/drm/i915/i915_perf.c | 57 +++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/i915_perf.c +42 -15
@@ -1192,24 +1192,39 @@ static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)struct i915_gem_engines_iter it;struct i915_gem_context *ctx = stream->ctx;struct intel_context *ce; -int err; +struct i915_gem_ww_ctx ww; +int err = -ENODEV ;for_each_gem_engine (ce,i915_gem_context_lock_engines (ctx), it) {if (ce->engine != stream->engine)/* first match! */ continue ; -/* -* As the ID is the gtt offset of the context's vma we -* pin the vma to ensure the ID remains fixed. -*/ - err =intel_context_pin (ce); -if (err ==0 ) { - stream->pinned_ctx = ce; -break ; - } + err =0 ; +break ; }i915_gem_context_unlock_engines (ctx); +if (err) +return ERR_PTR (err); + +i915_gem_ww_ctx_init (&ww, true); +retry: +/* +* As the ID is the gtt offset of the context's vma we +* pin the vma to ensure the ID remains fixed. +*/ + err =intel_context_pin_ww (ce, &ww); +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); + +if (err) +return ERR_PTR (err); + + stream->pinned_ctx = ce;return stream->pinned_ctx; }
@@ -1923,15 +1938,22 @@ emit_oa_config(struct i915_perf_stream *stream, {struct i915_request *rq;struct i915_vma *vma; +struct i915_gem_ww_ctx ww;int err; vma =get_oa_vma (stream, oa_config);if (IS_ERR (vma))return ERR_CAST (vma); - err =i915_vma_pin (vma,0 ,0 ,PIN_GLOBAL |PIN_HIGH ); +i915_gem_ww_ctx_init (&ww, true); +retry: + err =i915_gem_object_lock (vma->obj, &ww);if (err) - goto err_vma_put; + goto err; + + err =i915_vma_pin_ww (vma, &ww,0 ,0 ,PIN_GLOBAL |PIN_HIGH ); +if (err) + goto err;intel_engine_pm_get (ce->engine); rq =i915_request_create (ce);
@@ -1941,11 +1963,9 @@ emit_oa_config(struct i915_perf_stream *stream, goto err_vma_unpin; } -i915_vma_lock (vma); err =i915_request_await_object (rq, vma->obj,0 );if (!err) err =i915_vma_move_to_active (vma, rq,0 ); -i915_vma_unlock (vma);if (err) goto err_add_request;
@@ -1960,7 +1980,14 @@ emit_oa_config(struct i915_perf_stream *stream,i915_request_add (rq); err_vma_unpin:i915_vma_unpin (vma); -err_vma_put: +err: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } + +i915_gem_ww_ctx_fini (&ww);i915_vma_put (vma);return err ?ERR_PTR (err) : rq; }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 16/22 drm/i915: Dirty hack to fix selftests locking inversion
In reply to Maarten LankhorstSome i915 selftests still use i915_vma_lock() as inner lock, and intel_context_create_request() intel_timeline->mutex as outer lock. Fortunately for selftests this is not an issue, they should be fixed but we can move ahead and cleanify lockdep now.
--- drivers/gpu/drm/i915/gt/intel_context.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/gpu/drm/i915/gt/intel_context.c +12 -0
@@ -460,6 +460,18 @@ struct i915_request *intel_context_create_request(struct intel_context *ce) rq =i915_request_create (ce);intel_context_unpin (ce); +if (IS_ERR (rq)) +return rq; + +/* +* timeline->mutex should be the inner lock, but is used as outer lock. +* Hack around this to shut up lockdep in selftests.. +*/ +lockdep_unpin_lock (&ce->timeline->mutex, rq->cookie); +mutex_release (&ce->timeline->mutex.dep_map, _RET_IP_); +mutex_acquire (&ce->timeline->mutex.dep_map,SINGLE_DEPTH_NESTING ,0 , _RET_IP_); + rq->cookie =lockdep_pin_lock (&ce->timeline->mutex); +return rq; }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 17/22 drm/i915/selftests: Fix locking inversion in lrc selftest.
In reply to Maarten LankhorstThis function does not use intel_context_create_request, so it has to use the same locking order as normal code. This is required to shut up lockdep in selftests.
--- drivers/gpu/drm/i915/gt/selftest_lrc.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gt/selftest_lrc.c +12 -3
@@ -4283,6 +4283,7 @@ static int __live_lrc_state(struct intel_engine_cs *engine, {struct intel_context *ce;struct i915_request *rq; +struct i915_gem_ww_ctx ww;enum {RING_START_IDX =0 ,RING_TAIL_IDX ,
@@ -4297,7 +4298,11 @@ static int __live_lrc_state(struct intel_engine_cs *engine,if (IS_ERR (ce))return PTR_ERR (ce); - err =intel_context_pin (ce); +i915_gem_ww_ctx_init (&ww, false); +retry: + err =i915_gem_object_lock (scratch->obj, &ww); +if (!err) + err =intel_context_pin_ww (ce, &ww);if (err) goto err_put;
@@ -4326,11 +4331,9 @@ static int __live_lrc_state(struct intel_engine_cs *engine, *cs++ =i915_ggtt_offset (scratch) +RING_TAIL_IDX *sizeof (u32); *cs++ =0 ; -i915_vma_lock (scratch); err =i915_request_await_object (rq, scratch->obj, true);if (!err) err =i915_vma_move_to_active (scratch, rq,EXEC_OBJECT_WRITE ); -i915_vma_unlock (scratch);i915_request_get (rq);i915_request_add (rq);
@@ -4367,6 +4370,12 @@ static int __live_lrc_state(struct intel_engine_cs *engine, err_unpin:intel_context_unpin (ce); err_put: +if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + } +i915_gem_ww_ctx_fini (&ww);intel_context_put (ce);return err; }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 18/22 drm/i915: Use ww pinning for intel_context_create_request()
In reply to Maarten LankhorstWe want to get rid of intel_context_pin(), convert intel_context_create_request() first. :)
--- drivers/gpu/drm/i915/gt/intel_context.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/gt/intel_context.c +15 -5
@@ -450,15 +450,25 @@ int intel_context_prepare_remote_request(struct intel_context *ce,struct i915_request *intel_context_create_request (struct intel_context *ce) { +struct i915_gem_ww_ctx ww;struct i915_request *rq;int err; - err =intel_context_pin (ce); -if (unlikely (err)) -return ERR_PTR (err); +i915_gem_ww_ctx_init (&ww, true); +retry: + err =intel_context_pin_ww (ce, &ww); +if (!err) { + rq =i915_request_create (ce); +intel_context_unpin (ce); + }else if (err == -EDEADLK ) { + err =i915_gem_ww_ctx_backoff (&ww); +if (!err) + goto retry; + }else { + rq =ERR_PTR (err); + } - rq =i915_request_create (ce); -intel_context_unpin (ce); +i915_gem_ww_ctx_fini (&ww);if (IS_ERR (rq))return rq;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 19/22 drm/i915: Move i915_vma_lock in the selftests to avoid lock inversion, v2.
In reply to Maarten LankhorstMake sure vma_lock is not used as inner lock when kernel context is used, and add ww handling where appropriate.
--- .../i915/gem/selftests/i915_gem_coherency.c | 26 ++++++------ .../drm/i915/gem/selftests/i915_gem_mman.c | 41 ++++++++++++++----- drivers/gpu/drm/i915/selftests/i915_request.c | 18 +++++--- 3 files changed, 57 insertions(+), 28 deletions(-)
drivers/gpu/drm/i915/gem/selftests/i915_gem_coherency.c +14 -12
@@ -199,25 +199,25 @@ static int gpu_set(struct context *ctx, unsigned long offset, u32 v)i915_gem_object_lock (ctx->obj,NULL ); err =i915_gem_object_set_to_gtt_domain (ctx->obj, true); -i915_gem_object_unlock (ctx->obj);if (err) -return err; + goto out_unlock; vma =i915_gem_object_ggtt_pin (ctx->obj,NULL ,0 ,0 ,0 ); -if (IS_ERR (vma)) -return PTR_ERR (vma); +if (IS_ERR (vma)) { + err =PTR_ERR (vma); + goto out_unlock; + } rq =intel_engine_create_kernel_request (ctx->engine);if (IS_ERR (rq)) { -i915_vma_unpin (vma); -return PTR_ERR (rq); + err =PTR_ERR (rq); + goto out_unpin; } cs =intel_ring_begin (rq,4 );if (IS_ERR (cs)) { -i915_request_add (rq); -i915_vma_unpin (vma); -return PTR_ERR (cs); + err =PTR_ERR (cs); + goto out_rq; }if (INTEL_GEN (ctx->engine->i915) >=8 ) {
@@ -238,14 +238,16 @@ static int gpu_set(struct context *ctx, unsigned long offset, u32 v) }intel_ring_advance (rq, cs); -i915_vma_lock (vma); err =i915_request_await_object (rq, vma->obj, true);if (err ==0 ) err =i915_vma_move_to_active (vma, rq,EXEC_OBJECT_WRITE ); -i915_vma_unlock (vma); -i915_vma_unpin (vma); +out_rq:i915_request_add (rq); +out_unpin: +i915_vma_unpin (vma); +out_unlock: +i915_gem_object_unlock (ctx->obj);return err; }
drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c +31 -10
94 lines not on this page · raw message
drivers/gpu/drm/i915/selftests/i915_request.c +12 -6
@@ -854,6 +854,8 @@ static int live_all_engines(void *arg) goto out_free; } +i915_vma_lock (batch); + idx =0 ;for_each_uabi_engine (engine, i915) { request[idx] =intel_engine_create_kernel_request (engine);
@@ -871,11 +873,9 @@ static int live_all_engines(void *arg)GEM_BUG_ON (err); request[idx]->batch = batch; -i915_vma_lock (batch); err =i915_request_await_object (request[idx], batch->obj,0 );if (err ==0 ) err =i915_vma_move_to_active (batch, request[idx],0 ); -i915_vma_unlock (batch);GEM_BUG_ON (err);i915_request_get (request[idx]);
@@ -883,6 +883,8 @@ static int live_all_engines(void *arg) idx++; } +i915_vma_unlock (batch); + idx =0 ;for_each_uabi_engine (engine, i915) {if (i915_request_completed (request[idx])) {
@@ -973,12 +975,13 @@ static int live_sequential_engines(void *arg) goto out_free; } +i915_vma_lock (batch); request[idx] =intel_engine_create_kernel_request (engine);if (IS_ERR (request[idx])) { err =PTR_ERR (request[idx]);pr_err ("%s: Request allocation failed for %s with err=%d\n" , __func__, engine->name, err); - goto out_request; + goto out_unlock; }if (prev) {
@@ -988,7 +991,7 @@ static int live_sequential_engines(void *arg)i915_request_add (request[idx]);pr_err ("%s: Request await failed for %s with err=%d\n" , __func__, engine->name, err); - goto out_request; + goto out_unlock; } }
@@ -999,12 +1002,10 @@ static int live_sequential_engines(void *arg)GEM_BUG_ON (err); request[idx]->batch = batch; -i915_vma_lock (batch); err =i915_request_await_object (request[idx], batch->obj, false);if (err ==0 ) err =i915_vma_move_to_active (batch, request[idx],0 ); -i915_vma_unlock (batch);GEM_BUG_ON (err);i915_request_get (request[idx]);
@@ -1012,6 +1013,11 @@ static int live_sequential_engines(void *arg) prev = request[idx]; idx++; + +out_unlock: +i915_vma_unlock (batch); +if (err) + goto out_request; } idx =0 ;
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 20/22 drm/i915: Add ww locking to vm_fault_gtt
In reply to Maarten Lankhorst--- drivers/gpu/drm/i915/gem/i915_gem_mman.c | 51 +++++++++++++++--------- 1 file changed, 33 insertions(+), 18 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_mman.c +33 -18
@@ -283,37 +283,46 @@ static vm_fault_t vm_fault_gtt(struct vm_fault *vmf)struct intel_runtime_pm *rpm = &i915->runtime_pm;struct i915_ggtt *ggtt = &i915->ggtt;bool write = area->vm_flags &VM_WRITE ; +struct i915_gem_ww_ctx ww;intel_wakeref_t wakeref;struct i915_vma *vma;pgoff_t page_offset;int srcu;int ret; -/* Sanity check that we allow writing into this object */ -if (i915_gem_object_is_readonly (obj) && write) -return VM_FAULT_SIGBUS ; -/* We don't use vmf->pgoff since that has the fake offset */ page_offset = (vmf->address - area->vm_start) >>PAGE_SHIFT ;trace_i915_gem_object_fault (obj, page_offset, true, write); - ret =i915_gem_object_pin_pages (obj); + wakeref =intel_runtime_pm_get (rpm); + +i915_gem_ww_ctx_init (&ww, true); +retry: + ret =i915_gem_object_lock (obj, &ww);if (ret) - goto err; + goto err_rpm; - wakeref =intel_runtime_pm_get (rpm); +/* Sanity check that we allow writing into this object */ +if (i915_gem_object_is_readonly (obj) && write) { + ret = -EFAULT ; + goto err_rpm; + } - ret =intel_gt_reset_trylock (ggtt->vm.gt, &srcu); + ret =i915_gem_object_pin_pages (obj);if (ret) goto err_rpm; + ret =intel_gt_reset_trylock (ggtt->vm.gt, &srcu); +if (ret) + goto err_pages; +/* Now pin it into the GTT as needed */ - vma =i915_gem_object_ggtt_pin (obj,NULL ,0 ,0 , -PIN_MAPPABLE | -PIN_NONBLOCK /* NOWARN */ | -PIN_NOEVICT ); -if (IS_ERR (vma)) { + vma =i915_gem_object_ggtt_pin_ww (obj, &ww,NULL ,0 ,0 , +PIN_MAPPABLE | +PIN_NONBLOCK /* NOWARN */ | +PIN_NOEVICT ); +if (IS_ERR (vma) && vma !=ERR_PTR (-EDEADLK )) {/* Use a partial view if it is bigger than available space */ struct i915_ggtt_view view =compute_partial_view (obj, page_offset,MIN_CHUNK_PAGES );
@@ -328,11 +337,11 @@ static vm_fault_t vm_fault_gtt(struct vm_fault *vmf)* all hope that the hardware is able to track future writes. */ - vma =i915_gem_object_ggtt_pin (obj, &view,0 ,0 , flags); -if (IS_ERR (vma)) { + vma =i915_gem_object_ggtt_pin_ww (obj, &ww, &view,0 ,0 , flags); +if (IS_ERR (vma) && vma !=ERR_PTR (-EDEADLK )) { flags =PIN_MAPPABLE ; view.type =I915_GGTT_VIEW_PARTIAL ; - vma =i915_gem_object_ggtt_pin (obj, &view,0 ,0 , flags); + vma =i915_gem_object_ggtt_pin_ww (obj, &ww, &view,0 ,0 , flags); }/* The entire mappable GGTT is pinned? Unexpected! */
@@ -389,10 +398,16 @@ static vm_fault_t vm_fault_gtt(struct vm_fault *vmf)__i915_vma_unpin (vma); err_reset:intel_gt_reset_unlock (ggtt->vm.gt, srcu); +err_pages: +i915_gem_object_unpin_pages (obj); err_rpm: +if (ret == -EDEADLK ) { + ret =i915_gem_ww_ctx_backoff (&ww); +if (!ret) + goto retry; + } +i915_gem_ww_ctx_fini (&ww);intel_runtime_pm_put (rpm, wakeref); -i915_gem_object_unpin_pages (obj); -err:return i915_error_to_vmf_fault (ret); }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 21/22 drm/i915: Add ww locking to pin_to_display_plane
In reply to Maarten Lankhorst--- drivers/gpu/drm/i915/gem/i915_gem_domain.c | 65 ++++++++++++++++------ drivers/gpu/drm/i915/gem/i915_gem_object.h | 1 + 2 files changed, 49 insertions(+), 17 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_domain.c +48 -17
@@ -37,6 +37,12 @@ void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)i915_gem_object_unlock (obj); } +void i915_gem_object_flush_if_display_locked (struct drm_i915_gem_object *obj) +{ +if (i915_gem_object_is_framebuffer (obj)) +__i915_gem_object_flush_for_display (obj); +} +/** * Moves a single object to the WC read, and possibly write domain. * @obj: object to act on
@@ -197,18 +203,12 @@ int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,if (ret)return ret; - ret =i915_gem_object_lock_interruptible (obj,NULL ); -if (ret) -return ret; -/* Always invalidate stale cachelines */ if (obj->cache_level != cache_level) {i915_gem_object_set_cache_coherency (obj, cache_level); obj->cache_dirty = true; } -i915_gem_object_unlock (obj); -/* The cache-level will be applied when each vma is rebound. */ return i915_gem_object_unbind(obj, I915_GEM_OBJECT_UNBIND_ACTIVE |
@@ -255,6 +255,7 @@ int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,struct drm_i915_gem_caching *args = data;struct drm_i915_gem_object *obj;enum i915_cache_level level; +struct i915_gem_ww_ctx ww;int ret =0 ;switch (args->caching) {
@@ -293,7 +294,18 @@ int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, goto out; } - ret =i915_gem_object_set_cache_level (obj, level); +i915_gem_ww_ctx_init (&ww, true); +retry: + ret =i915_gem_object_lock (obj, &ww); +if (!ret) + ret =i915_gem_object_set_cache_level (obj, level); + +if (ret == -EDEADLK ) { + ret =i915_gem_ww_ctx_backoff (&ww); +if (!ret) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); out:i915_gem_object_put (obj);
@@ -313,6 +325,7 @@ i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, unsigned int flags) {struct drm_i915_private *i915 =to_i915 (obj->base.dev); +struct i915_gem_ww_ctx ww;struct i915_vma *vma;int ret;
@@ -320,6 +333,11 @@ i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,if (HAS_LMEM (i915) && !i915_gem_object_is_lmem (obj))return ERR_PTR (-EINVAL ); +i915_gem_ww_ctx_init (&ww, true); +retry: + ret =i915_gem_object_lock (obj, &ww); +if (ret) + goto err;/* * The display engine is not coherent with the LLC cache on gen6. As * a result, we make sure that the pinning that is about to occur is
@@ -334,7 +352,7 @@ i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,HAS_WT (i915) ? I915_CACHE_WT : I915_CACHE_NONE);if (ret) -return ERR_PTR (ret); + goto err;/* * As the user may map the buffer once pinned in the display plane
@@ -347,18 +365,31 @@ i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, vma =ERR_PTR (-ENOSPC );if ((flags &PIN_MAPPABLE ) ==0 && (!view || view->type ==I915_GGTT_VIEW_NORMAL )) - vma =i915_gem_object_ggtt_pin (obj, view,0 , alignment, - flags | -PIN_MAPPABLE | -PIN_NONBLOCK ); -if (IS_ERR (vma)) - vma =i915_gem_object_ggtt_pin (obj, view,0 , alignment, flags); -if (IS_ERR (vma)) -return vma; + vma =i915_gem_object_ggtt_pin_ww (obj, &ww, view,0 , alignment, + flags |PIN_MAPPABLE | +PIN_NONBLOCK ); +if (IS_ERR (vma) && vma !=ERR_PTR (-EDEADLK )) + vma =i915_gem_object_ggtt_pin_ww (obj, &ww, view,0 , + alignment, flags); +if (IS_ERR (vma)) { + ret =PTR_ERR (vma); + goto err; + } vma->display_alignment =max_t (u64, vma->display_alignment, alignment); -i915_gem_object_flush_if_display (obj); +i915_gem_object_flush_if_display_locked (obj); + +err: +if (ret == -EDEADLK ) { + ret =i915_gem_ww_ctx_backoff (&ww); +if (!ret) + goto retry; + } +i915_gem_ww_ctx_fini (&ww); + +if (ret) +return ERR_PTR (ret);return vma; }
drivers/gpu/drm/i915/gem/i915_gem_object.h +1 -0
@@ -456,6 +456,7 @@ i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)void i915_gem_object_set_cache_coherency (struct drm_i915_gem_object *obj,unsigned int cache_level);void i915_gem_object_flush_if_display (struct drm_i915_gem_object *obj); +void i915_gem_object_flush_if_display_locked (struct drm_i915_gem_object *obj);int __must_check i915_gem_object_set_to_wc_domain (struct drm_i915_gem_object *obj,bool write);
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 22/22 drm/i915: Ensure we hold the pin mutex
In reply to Maarten Lankhorst--- drivers/gpu/drm/i915/gt/intel_renderstate.c | 2 +- drivers/gpu/drm/i915/i915_vma.c | 9 ++++++++- drivers/gpu/drm/i915/i915_vma.h | 1 + 3 files changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gt/intel_renderstate.c +1 -1
@@ -207,7 +207,7 @@ int intel_renderstate_init(struct intel_renderstate *so,if (err) goto err_context; - err =i915_vma_pin (so->vma,0 ,0 ,PIN_GLOBAL |PIN_HIGH ); + err =i915_vma_pin_ww (so->vma, &so->ww,0 ,0 ,PIN_GLOBAL |PIN_HIGH );if (err) goto err_context;
drivers/gpu/drm/i915/i915_vma.c +8 -1
@@ -892,6 +892,8 @@ int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,#ifdef CONFIG_PROVE_LOCKING if (debug_locks &&lockdep_is_held (&vma->vm->i915->drm.struct_mutex))WARN_ON (!ww); +if (debug_locks && ww && vma->resv) +assert_vma_held (vma);#endif BUILD_BUG_ON (PIN_GLOBAL !=I915_VMA_GLOBAL_BIND );
@@ -1032,8 +1034,13 @@ int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,GEM_BUG_ON (!i915_vma_is_ggtt (vma)); +WARN_ON (!ww && vma->resv &&dma_resv_held (vma->resv)); + do { - err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL); + if (ww) + err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL); + else + err = i915_vma_pin(vma, 0, align, flags | PIN_GLOBAL); if (err != -ENOSPC) { if (!err) { err = i915_vma_wait_for_bind(vma);
drivers/gpu/drm/i915/i915_vma.h +1 -0
@@ -242,6 +242,7 @@ i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,static inline int __must_check i915_vma_pin (struct i915_vma *vma,u64 size,u64 alignment,u64 flags) { +WARN_ON_ONCE (vma->resv &&dma_resv_held (vma->resv));return i915_vma_pin_ww (vma,NULL , size, alignment, flags); }
Signature · 6 lines
2.25.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Patchwork
CI[Intel-gfx] ✗ Fi.CI.BUILD: failure for series starting with [01/22] Revert "drm/i915/gem: Drop relocation slowpath"
In reply to Maarten Lankhorst== Series Details ==
== Series Details ==
Series: series starting with [01/22] Revert "drm/i915/gem: Drop relocation slowpath"
URL : https://patchwork.freedesktop.org/series/75251/
State : failure
== Summary ==
Applying: Revert "drm/i915/gem: Drop relocation slowpath"
Applying: perf/core: Only copy-to-user after completely unlocking all locks. (CI test)
Applying: drm/i915: Add an implementation for i915_gem_ww_ctx locking, v2.
Applying: drm/i915: Remove locking from i915_gem_object_prepare_read/write
Applying: drm/i915: Parse command buffer earlier in eb_relocate(slow)
Applying: drm/i915: Use per object locking in execbuf, v7.
Applying: drm/i915: Use ww locking in intel_renderstate.
Applying: drm/i915: Add ww context handling to context_barrier_task
Applying: drm/i915: Nuke arguments to eb_pin_engine
Applying: drm/i915: Pin engine before pinning all objects, v3.
Applying: drm/i915: Rework intel_context pinning to do everything outside of pin_mutex
Applying: drm/i915: Make sure execbuffer always passes ww state to i915_vma_pin.
Applying: drm/i915: Convert i915_gem_object/client_blt.c to use ww locking as well, v2.
Applying: drm/i915: Kill last user of intel_context_create_request outside of selftests
Applying: drm/i915: Convert i915_perf to ww locking as well
Using index info to reconstruct a base tree...
M drivers/gpu/drm/i915/i915_perf.c
Falling back to patching base and 3-way merge...
Auto-merging drivers/gpu/drm/i915/i915_perf.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/i915/i915_perf.c
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0015 drm/i915: Convert i915_perf to ww locking as well When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx