drm/i915/gem: ioctl clean-ups (v9)
Subsystems (5)
cover drm/i915/gem: ioctl clean-ups (v9)
Overview:
---------
This patch series attempts to clean up some of the IOCTL mess we've created over the last few years. The most egregious bit being context mutability. In summary, this series:
1. Drops two never-used context params: RINGSIZE and NO_ZEROMAP
2. Drops the entire CONTEXT_CLONE API
3. Implements SINGLE_TIMELINE with a syncobj instead of actually sharing intel_timeline between engines.
4. Adds a few sanity restrictions to the balancing/bonding API.
5. Implements a proto-ctx mechanism so that the engine set and VM can only be set early on in the lifetime of a context, before anything ever executes on it. This effectively makes the VM and engine set immutable.
This series has been tested with IGT as well as the Iris, ANV, and the Intel media driver doing an 8K decode (this uses bonding/balancing). I've also done quite a bit of git archeology to ensure that nothing in here will break anything that's already shipped at some point in history. It's possible I've missed something, but I've dug quite a bit.
Details and motivation:
-----------------------
In very broad strokes, there's an effort going on right now within Intel to try and clean up and simplify i915 anywhere we can. We obviously don't want to break any shipping userspace but, as can be seen by this series, there's a lot i915 theoretically supports which userspace doesn't actually need. Some of this, like the two context params used here, were simply oversights where we went through the usual API review process and merged the i915 bits but the userspace bits never landed for some reason.
Not all are so innocent, however. For instance, there's an entire context cloning API which allows one to create a context with certain parameters "cloned" from some other context. This entire API has never been used by any userspace except IGT and there were never patches to any other userspace to use it. It never should have landed. Also, when we added support for setting explicit engine sets and sharing VMs across contexts, people decided to do so via SET_CONTEXT_PARAM. While this allowed them to re-use existing API, it did so at the cost of making those states mutable which leads to a plethora of potential race conditions. There were even IGT tests merged to cover some of theses:
- gem_vm_create@async-destroy and gem_vm_create@destroy-race which test swapping out the VM on a running context.
- gem_ctx_persistence@replace* which test whether a client can escape a non-persistent context by submitting a hanging batch and then swapping out the engine set before the hang is detected.
- api_intel_bb@bb-with-vm which tests the that intel_bb_assign_vm works properly. This API is never used by any other IGT test.
There is also an entire deferred flush and set state framework in i915_gem_cotnext.c which exists for safely swapping out the VM while there is work in-flight on a context.
So, clearly people knew that this API was inherently racy and difficult to implement but they landed it anyway. Why? The best explanation I've been given is because it makes the API more "unified" or "symmetric" for this stuff to go through SET_CONTEXT_PARAM. It's not because any userspace actually wants to be able to swap out the VM or the set of engines on a running context. That would be utterly insane.
This patch series cleans up this particular mess by introducing the concept of a i915_gem_proto_context data structure which contains context creation information. When you initially call GEM_CONTEXT_CREATE, a proto-context in created instead of an actual context. Then, the first time something is done on the context besides SET_CONTEXT_PARAM, an actual context is created. This allows us to keep the old drivers which use SET_CONTEXT_PARAM to set up the engine set (see also media) while ensuring that, once you have an i915_gem_context, the VM and the engine set are immutable state.
Eventually, there are more clean-ups I'd like to do on top of this which should make working with contexts inside i915 simpler and safer:
1. Move the GEM handle -> vma LUT from i915_gem_context into either i915_ppgtt or drm_i915_file_private depending on whether or not the hardware has a full PPGTT.
2. Move the delayed context destruction code into intel_context or a per-engine wrapper struct rather than i915_gem_context.
3. Get rid of the separation between context close and context destroy
4. Get rid of the RCU on i915_gem_context
However, these should probably be done as a separate patch series as this one is already starting to get longish, especially if you consider the 89 IGT patches that go along with it.
Test-with: [email protected]
Jason Ekstrand (30):
drm/i915: Drop I915_CONTEXT_PARAM_RINGSIZE
drm/i915: Stop storing the ring size in the ring pointer (v3) drm/i915: Drop I915_CONTEXT_PARAM_NO_ZEROMAP
drm/i915/gem: Set the watchdog timeout directly in
intel_context_set_gem (v2)
drm/i915/gem: Return void from context_apply_all
drm/i915: Drop the CONTEXT_CLONE API (v2)
drm/i915: Implement SINGLE_TIMELINE with a syncobj (v4) drm/i915: Drop getparam support for I915_CONTEXT_PARAM_ENGINES drm/i915/gem: Disallow bonding of virtual engines (v3) drm/i915/gem: Remove engine auto-magic with FENCE_SUBMIT (v2) drm/i915/request: Remove the hook from await_execution drm/i915/gem: Disallow creating contexts with too many engines drm/i915: Stop manually RCU banging in reset_stats_ioctl (v2) drm/i915/gem: Add a separate validate_priority helper drm/i915: Add gem/i915_gem_context.h to the docs drm/i915/gem: Add an intermediate proto_context struct (v5) drm/i915/gem: Rework error handling in default_engines drm/i915/gem: Optionally set SSEU in intel_context_set_gem drm/i915: Add an i915_gem_vm_lookup helper
drm/i915/gem: Make an alignment check more sensible drm/i915/gem: Use the proto-context to handle create parameters (v5) drm/i915/gem: Return an error ptr from context_lookup drm/i915/gt: Drop i915_address_space::file (v2) drm/i915/gem: Delay context creation (v3)
drm/i915/gem: Don't allow changing the VM on running contexts (v4) drm/i915/gem: Don't allow changing the engine set on running contexts
(v3)
drm/i915/selftests: Take a VM in kernel_context() i915/gem/selftests: Assign the VM at context creation in
igt_shared_ctx_exec
drm/i915/gem: Roll all of context creation together drm/i915: Finalize contexts in GEM_CONTEXT_CREATE on version 13+
Documentation/gpu/i915.rst | 2 + drivers/gpu/drm/i915/Makefile | 1 - drivers/gpu/drm/i915/gem/i915_gem_context.c | 2926 ++++++++--------- drivers/gpu/drm/i915/gem/i915_gem_context.h | 3 + .../gpu/drm/i915/gem/i915_gem_context_types.h | 196 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 31 +- .../drm/i915/gem/selftests/i915_gem_context.c | 127 +- .../gpu/drm/i915/gem/selftests/mock_context.c | 67 +- .../gpu/drm/i915/gem/selftests/mock_context.h | 4 +- drivers/gpu/drm/i915/gt/intel_context.c | 3 +- drivers/gpu/drm/i915/gt/intel_context.h | 5 - drivers/gpu/drm/i915/gt/intel_context_param.c | 63 - drivers/gpu/drm/i915/gt/intel_context_param.h | 6 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 1 + drivers/gpu/drm/i915/gt/intel_engine_cs.c | 3 +- drivers/gpu/drm/i915/gt/intel_engine_types.h | 7 - .../drm/i915/gt/intel_execlists_submission.c | 114 - .../drm/i915/gt/intel_execlists_submission.h | 8 +- drivers/gpu/drm/i915/gt/intel_gtt.h | 11 - drivers/gpu/drm/i915/gt/intel_lrc.c | 2 +- drivers/gpu/drm/i915/gt/intel_migrate.c | 3 +- drivers/gpu/drm/i915/gt/selftest_execlists.c | 251 +- drivers/gpu/drm/i915/gt/selftest_hangcheck.c | 2 +- drivers/gpu/drm/i915/gt/selftest_mocs.c | 2 +- drivers/gpu/drm/i915/gt/selftest_timeline.c | 2 +- drivers/gpu/drm/i915/gvt/scheduler.c | 7 +- drivers/gpu/drm/i915/i915_drv.h | 82 +- drivers/gpu/drm/i915/i915_perf.c | 4 +- drivers/gpu/drm/i915/i915_request.c | 42 +- drivers/gpu/drm/i915/i915_request.h | 4 +- .../drm/i915/selftests/i915_mock_selftests.h | 1 - drivers/gpu/drm/i915/selftests/mock_gtt.c | 1 - include/uapi/drm/i915_drm.h | 40 +- 33 files changed, 1681 insertions(+), 2340 deletions(-) delete mode 100644 drivers/gpu/drm/i915/gt/intel_context_param.c
Signature · 1 line
2.31.1
PATCH 1/30 drm/i915: Drop I915_CONTEXT_PARAM_RINGSIZE
This reverts commit 88be76cdafc7 ("drm/i915: Allow userspace to specify ringsize on construction"). This API was originally added for OpenCL but the compute-runtime PR has sat open for a year without action so we can still pull it out if we want. I argue we should drop it for three reasons:
1. If the compute-runtime PR has sat open for a year, this clearly isn't that important.
2. It's a very leaky API. Ring size is an implementation detail of the current execlist scheduler and really only makes sense there. It can't apply to the older ring-buffer scheduler on pre-execlist hardware because that's shared across all contexts and it won't apply to the GuC scheduler that's in the pipeline.
3. Having userspace set a ring size in bytes is a bad solution to the problem of having too small a ring. There is no way that userspace has the information to know how to properly set the ring size so it's just going to detect the feature and always set it to the maximum of 512K. This is what the compute-runtime PR does. The scheduler in i915, on the other hand, does have the information to make an informed choice. It could detect if the ring size is a problem and grow it itself. Or, if that's too hard, we could just increase the default size from 16K to 32K or even 64K instead of relying on userspace to do it.
Let's drop this API for now and, if someone decides they really care about solving this problem, they can do it properly.
--- drivers/gpu/drm/i915/Makefile | 1 - drivers/gpu/drm/i915/gem/i915_gem_context.c | 85 +------------------ drivers/gpu/drm/i915/gt/intel_context_param.c | 63 -------------- drivers/gpu/drm/i915/gt/intel_context_param.h | 3 - include/uapi/drm/i915_drm.h | 20 +---- 5 files changed, 4 insertions(+), 168 deletions(-) delete mode 100644 drivers/gpu/drm/i915/gt/intel_context_param.c
drivers/gpu/drm/i915/Makefile +0 -1
@@ -89,7 +89,6 @@ gt-y += \ gt/gen8_ppgtt.o \ gt/intel_breadcrumbs.o \ gt/intel_context.o \ - gt/intel_context_param.o \ gt/intel_context_sseu.o \ gt/intel_engine_cs.o \ gt/intel_engine_heartbeat.o \
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -83
117 lines not on this page · raw message
drivers/gpu/drm/i915/gt/intel_context_param.c deleted +0 -63
63 lines not on this page · raw message
drivers/gpu/drm/i915/gt/intel_context_param.h +0 -3
@@ -10,9 +10,6 @@#include "intel_context.h" -int intel_context_set_ring_size (struct intel_context *ce,long sz); -long intel_context_get_ring_size (struct intel_context *ce); -static inline int intel_context_set_watchdog_us (struct intel_context *ce,u64 timeout_us) {
include/uapi/drm/i915_drm.h +2 -18
@@ -1722,24 +1722,8 @@ struct drm_i915_gem_context_param {*/ #define I915_CONTEXT_PARAM_PERSISTENCE 0xb -/* -* I915_CONTEXT_PARAM_RINGSIZE: -* -* Sets the size of the CS ringbuffer to use for logical ring contexts. This -* applies a limit of how many batches can be queued to HW before the caller -* is blocked due to lack of space for more commands. -* -* Only reliably possible to be set prior to first use, i.e. during -* construction. At any later point, the current execution must be flushed as -* the ring can only be changed while the context is idle. Note, the ringsize -* can be specified as a constructor property, see -* I915_CONTEXT_CREATE_EXT_SETPARAM, but can also be set later if required. -* -* Only applies to the current set of engine and lost when those engines -* are replaced by a new mapping (see I915_CONTEXT_PARAM_ENGINES). -* -* Must be between 4 - 512 KiB, in intervals of page size [4 KiB]. -* Default is 16 KiB. +/* This API has been removed. On the off chance someone somewhere has +* attempted to use it, never re-use this context param number. */ #define I915_CONTEXT_PARAM_RINGSIZE 0xc/* Must be kept compact -- no holes and well documented */
Signature · 1 line
2.31.1
PATCH 2/30 drm/i915: Stop storing the ring size in the ring pointer (v3)
In reply to Jason EkstrandPreviously, we were storing the ring size in the ring pointer before it was actually allocated. We would then guard setting the ring size on checking for CONTEXT_ALLOC_BIT. This is error-prone at best and really only saves us a few bytes on something that already burns at least 4K. Instead, this patch adds a new ring_size field and makes everything use that.
v2 (Daniel Vetter):
- Replace 512 * SZ_4K with SZ_2M
v2 (Jason Ekstrand):
- Rebase on top of page migration code
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 3 +-- drivers/gpu/drm/i915/gt/intel_context.c | 3 ++- drivers/gpu/drm/i915/gt/intel_context.h | 5 ----- drivers/gpu/drm/i915/gt/intel_context_types.h | 1 + drivers/gpu/drm/i915/gt/intel_engine_cs.c | 3 ++- drivers/gpu/drm/i915/gt/intel_lrc.c | 2 +- drivers/gpu/drm/i915/gt/intel_migrate.c | 3 ++- drivers/gpu/drm/i915/gt/selftest_execlists.c | 2 +- drivers/gpu/drm/i915/gt/selftest_mocs.c | 2 +- drivers/gpu/drm/i915/gt/selftest_timeline.c | 2 +- drivers/gpu/drm/i915/gvt/scheduler.c | 7 ++----- 11 files changed, 14 insertions(+), 19 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -2
@@ -211,8 +211,7 @@ static void intel_context_set_gem(struct intel_context *ce,GEM_BUG_ON (rcu_access_pointer (ce->gem_context));RCU_INIT_POINTER (ce->gem_context, ctx); -if (!test_bit (CONTEXT_ALLOC_BIT , &ce->flags)) - ce->ring =__intel_context_ring_size (SZ_16K ); + ce->ring_size =SZ_16K ;if (rcu_access_pointer (ctx->vm)) {struct i915_address_space *vm;
drivers/gpu/drm/i915/gt/intel_context.c +2 -1
@@ -371,7 +371,8 @@ intel_context_init(struct intel_context *ce, struct intel_engine_cs *engine) ce->engine = engine; ce->ops = engine->cops; ce->sseu = engine->sseu; - ce->ring =__intel_context_ring_size (SZ_4K ); + ce->ring =NULL ; + ce->ring_size =SZ_4K ;ewma_runtime_init (&ce->runtime.avg);
drivers/gpu/drm/i915/gt/intel_context.h +0 -5
@@ -175,11 +175,6 @@ int intel_context_prepare_remote_request(struct intel_context *ce,struct i915_request *intel_context_create_request (struct intel_context *ce); -static inline struct intel_ring *__intel_context_ring_size (u64 sz) -{ - return u64_to_ptr(struct intel_ring, sz); -} -static inline bool intel_context_is_barrier (const struct intel_context *ce) {return test_bit (CONTEXT_BARRIER_BIT , &ce->flags);
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
@@ -82,6 +82,7 @@ struct intel_context {spinlock_t signal_lock;/* protects signals, the list of requests */ struct i915_vma *state; +u32 ring_size;struct intel_ring *ring;struct intel_timeline *timeline;
drivers/gpu/drm/i915/gt/intel_engine_cs.c +2 -1
@@ -807,7 +807,8 @@ intel_engine_create_pinned_context(struct intel_engine_cs *engine,__set_bit (CONTEXT_BARRIER_BIT , &ce->flags); ce->timeline =page_pack_bits (NULL , hwsp); - ce->ring =__intel_context_ring_size (ring_size); + ce->ring =NULL ; + ce->ring_size = ring_size;i915_vm_put (ce->vm); ce->vm =i915_vm_get (vm);
drivers/gpu/drm/i915/gt/intel_lrc.c +1 -1
@@ -845,7 +845,7 @@ int lrc_alloc(struct intel_context *ce, struct intel_engine_cs *engine)if (IS_ERR (vma))return PTR_ERR (vma); - ring =intel_engine_create_ring (engine, (unsigned long )ce->ring); + ring =intel_engine_create_ring (engine, ce->ring_size);if (IS_ERR (ring)) { err =PTR_ERR (ring); goto err_vma;
drivers/gpu/drm/i915/gt/intel_migrate.c +2 -1
@@ -232,7 +232,8 @@ struct intel_context *intel_migrate_create_context(struct intel_migrate *m)if (IS_ERR (ce))return ce; - ce->ring =__intel_context_ring_size (SZ_256K ); + ce->ring =NULL ; + ce->ring_size =SZ_256K ;i915_vm_put (ce->vm); ce->vm =i915_vm_get (m->context->vm);
drivers/gpu/drm/i915/gt/selftest_execlists.c +1 -1
@@ -2810,7 +2810,7 @@ static int __live_preempt_ring(struct intel_engine_cs *engine, goto err_ce; } - tmp->ring =__intel_context_ring_size (ring_sz); + tmp->ring_size = ring_sz; err =intel_context_pin (tmp);if (err) {
drivers/gpu/drm/i915/gt/selftest_mocs.c +1 -1
@@ -28,7 +28,7 @@ static struct intel_context *mocs_context_create(struct intel_engine_cs *engine)return ce;/* We build large requests to read the registers from the ring */ - ce->ring =__intel_context_ring_size (SZ_16K ); + ce->ring_size =SZ_16K ;return ce; }
drivers/gpu/drm/i915/gt/selftest_timeline.c +1 -1
@@ -874,7 +874,7 @@ static int create_watcher(struct hwsp_watcher *w,if (IS_ERR (ce))return PTR_ERR (ce); - ce->ring =__intel_context_ring_size (ringsz); + ce->ring_size = ringsz; w->rq =intel_context_create_request (ce);intel_context_put (ce);if (IS_ERR (w->rq))
drivers/gpu/drm/i915/gvt/scheduler.c +2 -5
@@ -1409,11 +1409,8 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)intel_context_set_single_submission (ce);/* Max ring buffer size */ -if (!intel_uc_wants_guc_submission (&engine->gt->uc)) { -const unsigned int ring_size =512 *SZ_4K ; - - ce->ring =__intel_context_ring_size (ring_size); - } +if (!intel_uc_wants_guc_submission (&engine->gt->uc)) + ce->ring_size =SZ_2M ; s->shadow[i] = ce; }
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 3/30 drm/i915: Drop I915_CONTEXT_PARAM_NO_ZEROMAP
In reply to Jason EkstrandThe idea behind this param is to support OpenCL drivers with relocations because OpenCL reserves 0x0 for NULL and, if we placed memory there, it would confuse CL kernels. It was originally sent out as part of a patch series including libdrm [1] and Beignet [2] support. However, the libdrm and Beignet patches never landed in their respective upstream projects so this API has never been used. It's never been used in Mesa or any other driver, either.
Dropping this API allows us to delete a small bit of code.
[1]: https://lists.freedesktop.org/archives/intel-gfx/2015-May/067030.html
[2]: https://lists.freedesktop.org/archives/intel-gfx/2015-May/067031.html
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 16 ++-------------- .../gpu/drm/i915/gem/i915_gem_context_types.h | 1 - drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 8 -------- include/uapi/drm/i915_drm.h | 4 ++++ 4 files changed, 6 insertions(+), 23 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -14
@@ -1920,15 +1920,6 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,int ret =0 ;switch (args->param) { -case I915_CONTEXT_PARAM_NO_ZEROMAP : -if (args->size) - ret = -EINVAL ; -else if (args->value) -set_bit (UCONTEXT_NO_ZEROMAP , &ctx->user_flags); -else -clear_bit (UCONTEXT_NO_ZEROMAP , &ctx->user_flags); -break ; -case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE :if (args->size) ret = -EINVAL ;
@@ -1978,6 +1969,7 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv, ret =set_persistence (ctx, args);break ; +case I915_CONTEXT_PARAM_NO_ZEROMAP :case I915_CONTEXT_PARAM_BAN_PERIOD :case I915_CONTEXT_PARAM_RINGSIZE :default :
@@ -2358,11 +2350,6 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,return -ENOENT ;switch (args->param) { -case I915_CONTEXT_PARAM_NO_ZEROMAP : - args->size =0 ; - args->value =test_bit (UCONTEXT_NO_ZEROMAP , &ctx->user_flags); -break ; -case I915_CONTEXT_PARAM_GTT_SIZE : args->size =0 ;rcu_read_lock ();
@@ -2410,6 +2397,7 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data, args->value =i915_gem_context_is_persistent (ctx);break ; +case I915_CONTEXT_PARAM_NO_ZEROMAP :case I915_CONTEXT_PARAM_BAN_PERIOD :case I915_CONTEXT_PARAM_RINGSIZE :default :
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +0 -1
@@ -129,7 +129,6 @@ struct i915_gem_context {* @user_flags: small set of booleans controlled by the user */ unsigned long user_flags; -#define UCONTEXT_NO_ZEROMAP 0#define UCONTEXT_NO_ERROR_CAPTURE 1#define UCONTEXT_BANNABLE 2#define UCONTEXT_RECOVERABLE 3
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +0 -8
@@ -290,7 +290,6 @@ struct i915_execbuffer {struct intel_context *reloc_context;u64 invalid_flags;/** Set of execobj.flags that are invalid */ -u32 context_flags;/** Set of execobj.flags to insert from the ctx */ u64 batch_len;/** Length of batch within object */ u32 batch_start_offset;/** Location within object of batch */
@@ -552,9 +551,6 @@ eb_validate_vma(struct i915_execbuffer *eb, entry->flags |=EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP; } -if (!(entry->flags &EXEC_OBJECT_PINNED )) - entry->flags |= eb->context_flags; -return 0 ; }
@@ -761,10 +757,6 @@ static int eb_select_context(struct i915_execbuffer *eb)if (rcu_access_pointer (ctx->vm)) eb->invalid_flags |=EXEC_OBJECT_NEEDS_GTT ; - eb->context_flags =0 ; -if (test_bit (UCONTEXT_NO_ZEROMAP , &ctx->user_flags)) - eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS; -return 0 ; }
include/uapi/drm/i915_drm.h +4 -0
@@ -1638,6 +1638,10 @@ struct drm_i915_gem_context_param {__u32 size;__u64 param;#define I915_CONTEXT_PARAM_BAN_PERIOD 0x1 +/* I915_CONTEXT_PARAM_NO_ZEROMAP has been removed. On the off chance +* someone somewhere has attempted to use it, never re-use this context +* param number. +*/ #define I915_CONTEXT_PARAM_NO_ZEROMAP 0x2#define I915_CONTEXT_PARAM_GTT_SIZE 0x3#define I915_CONTEXT_PARAM_NO_ERROR_CAPTURE 0x4
Signature · 1 line
2.31.1
PATCH 4/30 drm/i915/gem: Set the watchdog timeout directly in intel_context_set_gem (v2)
In reply to Jason EkstrandInstead of handling it like a context param, unconditionally set it when intel_contexts are created. For years we've had the idea of a watchdog uAPI floating about. The aim was for media, so that they could set very tight deadlines for their transcodes jobs, so that if you have a corrupt bitstream (especially for decoding) you don't hang your desktop too hard. But it's been stuck in limbo since forever, and this simplifies things a bit in preparation for the proto-context work. If we decide to actually make said uAPI a reality, we can do it through the proto- context easily enough.
This does mean that we move from reading the request_timeout_ms param once per engine when engines are created instead of once at context creation. If someone changes request_timeout_ms between creating a context and setting engines, it will mean that they get the new timeout. If someone races setting request_timeout_ms and context creation, they can theoretically end up with different timeouts. However, since both of these are fairly harmless and require changing kernel params, we don't care.
v2 (Tvrtko Ursulin):
- Add a comment about races with request_timeout_ms
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 44 +++---------------- .../gpu/drm/i915/gem/i915_gem_context_types.h | 4 -- drivers/gpu/drm/i915/gt/intel_context_param.h | 3 +- 3 files changed, 7 insertions(+), 44 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +6 -38
@@ -232,7 +232,12 @@ static void intel_context_set_gem(struct intel_context *ce, intel_engine_has_timeslices(ce->engine))__set_bit (CONTEXT_USE_SEMAPHORES , &ce->flags); -intel_context_set_watchdog_us (ce, ctx->watchdog.timeout_us); +if (IS_ACTIVE (CONFIG_DRM_I915_REQUEST_TIMEOUT ) && + ctx->i915->params.request_timeout_ms) { +unsigned int timeout_ms = ctx->i915->params.request_timeout_ms; + +intel_context_set_watchdog_us (ce, (u64 )timeout_ms *1000 ); + } }static void __free_engines (struct i915_gem_engines *e,unsigned int count)
@@ -791,41 +796,6 @@ static void __assign_timeline(struct i915_gem_context *ctx,context_apply_all (ctx, __apply_timeline, timeline); } -static int __apply_watchdog (struct intel_context *ce,void *timeout_us) -{ -return intel_context_set_watchdog_us (ce, (uintptr_t )timeout_us); -} - -static int -__set_watchdog (struct i915_gem_context *ctx,unsigned long timeout_us) -{ -int ret; - - ret =context_apply_all (ctx, __apply_watchdog, - (void *)(uintptr_t )timeout_us); -if (!ret) - ctx->watchdog.timeout_us = timeout_us; - -return ret; -} - -static void __set_default_fence_expiry (struct i915_gem_context *ctx) -{ -struct drm_i915_private *i915 = ctx->i915; -int ret; - -if (!IS_ACTIVE (CONFIG_DRM_I915_REQUEST_TIMEOUT ) || - !i915->params.request_timeout_ms) -return ; - -/* Default expiry for user fences. */ - ret =__set_watchdog (ctx, i915->params.request_timeout_ms *1000 ); -if (ret) -drm_notice (&i915->drm, -"Failed to configure default fence expiry! (%d)" , - ret); -} -static struct i915_gem_context *i915_gem_create_context (struct drm_i915_private *i915,unsigned int flags) {
@@ -870,8 +840,6 @@ i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags)intel_timeline_put (timeline); } -__set_default_fence_expiry (ctx); -trace_i915_context_create (ctx);return ctx;
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +0 -4
@@ -153,10 +153,6 @@ struct i915_gem_context {*/ atomic_t active_count; -struct { -u64 timeout_us; - } watchdog; -/** * @hang_timestamp: The last time(s) this context caused a GPU hang */
drivers/gpu/drm/i915/gt/intel_context_param.h +1 -2
@@ -10,11 +10,10 @@#include "intel_context.h" -static inline int +static inline void intel_context_set_watchdog_us (struct intel_context *ce,u64 timeout_us) { ce->watchdog.timeout_us = timeout_us; -return 0 ; }#endif /* INTEL_CONTEXT_PARAM_H */
Signature · 1 line
2.31.1
PATCH 5/30 drm/i915/gem: Return void from context_apply_all
In reply to Jason EkstrandNone of the callbacks we use with it return an error code anymore; they all return 0 unconditionally.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 26 +++++++-------------- 1 file changed, 8 insertions(+), 18 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +8 -18
@@ -718,32 +718,25 @@ __context_engines_await(const struct i915_gem_context *ctx,return engines; } -static int +static void context_apply_all (struct i915_gem_context *ctx, -int (*fn)(struct intel_context *ce,void *data), +void (*fn)(struct intel_context *ce,void *data),void *data) {struct i915_gem_engines_iter it;struct i915_gem_engines *e;struct intel_context *ce; -int err =0 ; e =__context_engines_await (ctx,NULL ); -for_each_gem_engine (ce, e, it) { - err =fn (ce, data); -if (err) -break ; - } +for_each_gem_engine (ce, e, it) +fn (ce, data);i915_sw_fence_complete (&e->fence); - -return err; } -static int __apply_ppgtt (struct intel_context *ce,void *vm) +static void __apply_ppgtt (struct intel_context *ce,void *vm) {i915_vm_put (ce->vm); ce->vm =i915_vm_get (vm); -return 0 ; } static struct i915_address_space *
@@ -783,10 +776,9 @@ static void __set_timeline(struct intel_timeline **dst,intel_timeline_put (old); } -static int __apply_timeline (struct intel_context *ce,void *timeline) +static void __apply_timeline (struct intel_context *ce,void *timeline) {__set_timeline (&ce->timeline, timeline); -return 0 ; } static void __assign_timeline(struct i915_gem_context *ctx,
@@ -1841,19 +1833,17 @@ set_persistence(struct i915_gem_context *ctx,return __context_set_persistence (ctx, args->value); } -static int __apply_priority (struct intel_context *ce,void *arg) +static void __apply_priority (struct intel_context *ce,void *arg) {struct i915_gem_context *ctx = arg;if (!intel_engine_has_timeslices (ce->engine)) -return 0 ; +return ;if (ctx->sched.priority >=I915_PRIORITY_NORMAL )intel_context_set_use_semaphores (ce);else intel_context_clear_use_semaphores (ce); - -return 0 ; } static int set_priority(struct i915_gem_context *ctx,
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 6/30 drm/i915: Drop the CONTEXT_CLONE API (v2)
In reply to Jason EkstrandThis API allows one context to grab bits out of another context upon creation. It can be used as a short-cut for setparam(getparam()) for things like I915_CONTEXT_PARAM_VM. However, it's never been used by any real userspace. It's used by a few IGT tests and that's it. Since it doesn't add any real value (most of the stuff you can CLONE you can copy in other ways), drop it.
There is one thing that this API allows you to clone which you cannot clone via getparam/setparam: timelines. However, timelines are an implementation detail of i915 and not really something that needs to be exposed to userspace. Also, sharing timelines between contexts isn't obviously useful and supporting it has the potential to complicate i915 internally. It also doesn't add any functionality that the client can't get in other ways. If a client really wants a shared timeline, they can use a syncobj and set it as an in and out fence on every submit.
v2 (Jason Ekstrand):
- More detailed commit message
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 199 +----------------- .../drm/i915/gt/intel_execlists_submission.c | 28 --- .../drm/i915/gt/intel_execlists_submission.h | 3 - include/uapi/drm/i915_drm.h | 16 +- 4 files changed, 6 insertions(+), 240 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +3 -196
@@ -1957,207 +1957,14 @@ static int create_setparam(struct i915_user_extension __user *ext, void *data)return ctx_setparam (arg->fpriv, arg->ctx, &local.param); } -static int clone_engines (struct i915_gem_context *dst, -struct i915_gem_context *src) +static int invalid_ext (struct i915_user_extension __user *ext,void *data) { -struct i915_gem_engines *clone, *e; -bool user_engines; -unsigned long n; - - e =__context_engines_await (src, &user_engines); -if (!e) -return -ENOENT ; - - clone =alloc_engines (e->num_engines); -if (!clone) - goto err_unlock; - -for (n =0 ; n < e->num_engines; n++) { -struct intel_engine_cs *engine; - -if (!e->engines[n]) { - clone->engines[n] =NULL ; -continue ; - } - engine = e->engines[n]->engine; - -/* -* Virtual engines are singletons; they can only exist -* inside a single context, because they embed their -* HW context... As each virtual context implies a single -* timeline (each engine can only dequeue a single request -* at any time), it would be surprising for two contexts -* to use the same engine. So let's create a copy of -* the virtual engine instead. -*/ -if (intel_engine_is_virtual (engine)) - clone->engines[n] = -intel_execlists_clone_virtual (engine); -else - clone->engines[n] =intel_context_create (engine); -if (IS_ERR_OR_NULL (clone->engines[n])) { -__free_engines (clone, n); - goto err_unlock; - } - -intel_context_set_gem (clone->engines[n], dst); - } - clone->num_engines = n; -i915_sw_fence_complete (&e->fence); - -/* Serialised by constructor */ -engines_idle_release (dst,rcu_replace_pointer (dst->engines, clone,1 )); -if (user_engines) -i915_gem_context_set_user_engines (dst); -else -i915_gem_context_clear_user_engines (dst); -return 0 ; - -err_unlock: -i915_sw_fence_complete (&e->fence); -return -ENOMEM ; -} - -static int clone_flags (struct i915_gem_context *dst, -struct i915_gem_context *src) -{ - dst->user_flags = src->user_flags; -return 0 ; -} - -static int clone_schedattr (struct i915_gem_context *dst, -struct i915_gem_context *src) -{ - dst->sched = src->sched; -return 0 ; -} - -static int clone_sseu (struct i915_gem_context *dst, -struct i915_gem_context *src) -{ -struct i915_gem_engines *e =i915_gem_context_lock_engines (src); -struct i915_gem_engines *clone; -unsigned long n; -int err; - -/* no locking required; sole access under constructor*/ - clone =__context_engines_static (dst); -if (e->num_engines != clone->num_engines) { - err = -EINVAL ; - goto unlock; - } - -for (n =0 ; n < e->num_engines; n++) { -struct intel_context *ce = e->engines[n]; - -if (clone->engines[n]->engine->class != ce->engine->class) { -/* Must have compatible engine maps! */ - err = -EINVAL ; - goto unlock; - } - -/* serialises with set_sseu */ - err =intel_context_lock_pinned (ce); -if (err) - goto unlock; - - clone->engines[n]->sseu = ce->sseu; -intel_context_unlock_pinned (ce); - } - - err =0 ; -unlock: -i915_gem_context_unlock_engines (src); -return err; -} - -static int clone_timeline (struct i915_gem_context *dst, -struct i915_gem_context *src) -{ -if (src->timeline) -__assign_timeline (dst, src->timeline); - -return 0 ; -} - -static int clone_vm (struct i915_gem_context *dst, -struct i915_gem_context *src) -{ -struct i915_address_space *vm; -int err =0 ; - -if (!rcu_access_pointer (src->vm)) -return 0 ; - -rcu_read_lock (); - vm =context_get_vm_rcu (src); -rcu_read_unlock (); - -if (!mutex_lock_interruptible (&dst->mutex)) { -__assign_ppgtt (dst, vm); -mutex_unlock (&dst->mutex); - }else { - err = -EINTR ; - } - -i915_vm_put (vm); -return err; -} - -static int create_clone (struct i915_user_extension __user *ext,void *data) -{ -static int (*const fn[])(struct i915_gem_context *dst, -struct i915_gem_context *src) = { -#define MAP(x, y) [ilog2(I915_CONTEXT_CLONE_##x)] = y -MAP (ENGINES , clone_engines), -MAP (FLAGS , clone_flags), -MAP (SCHEDATTR , clone_schedattr), -MAP (SSEU , clone_sseu), -MAP (TIMELINE , clone_timeline), -MAP (VM , clone_vm), -#undef MAP - }; -struct drm_i915_gem_context_create_ext_clone local; -const struct create_ext *arg = data; -struct i915_gem_context *dst = arg->ctx; -struct i915_gem_context *src; -int err, bit; - -if (copy_from_user (&local, ext,sizeof (local))) -return -EFAULT ; - -BUILD_BUG_ON (GENMASK (BITS_PER_TYPE (local.flags) -1 ,ARRAY_SIZE (fn)) != -I915_CONTEXT_CLONE_UNKNOWN ); - -if (local.flags &I915_CONTEXT_CLONE_UNKNOWN ) -return -EINVAL ; - -if (local.rsvd) -return -EINVAL ; - -rcu_read_lock (); - src =__i915_gem_context_lookup_rcu (arg->fpriv, local.clone_id); -rcu_read_unlock (); -if (!src) -return -ENOENT ; - -GEM_BUG_ON (src == dst); - -for (bit =0 ; bit <ARRAY_SIZE (fn); bit++) { -if (!(local.flags &BIT (bit))) -continue ; - - err = fn[bit](dst, src); -if (err) -return err; - } - -return 0 ; +return -EINVAL ; }static const i915_user_extension_fn create_extensions[] = { [I915_CONTEXT_CREATE_EXT_SETPARAM ] = create_setparam, - [I915_CONTEXT_CREATE_EXT_CLONE ] = create_clone, + [I915_CONTEXT_CREATE_EXT_CLONE ] = invalid_ext, };static bool client_is_banned (struct drm_i915_file_private *file_priv)
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +0 -28
@@ -3850,34 +3850,6 @@ intel_execlists_create_virtual(struct intel_engine_cs **siblings,return ERR_PTR (err); } -struct intel_context * -intel_execlists_clone_virtual (struct intel_engine_cs *src) -{ -struct virtual_engine *se =to_virtual_engine (src); -struct intel_context *dst; - - dst =intel_execlists_create_virtual (se->siblings, - se->num_siblings); -if (IS_ERR (dst)) -return dst; - -if (se->num_bonds) { -struct virtual_engine *de =to_virtual_engine (dst->engine); - - de->bonds =kmemdup (se->bonds, -sizeof (*se->bonds) * se->num_bonds, -GFP_KERNEL ); -if (!de->bonds) { -intel_context_put (dst); -return ERR_PTR (-ENOMEM ); - } - - de->num_bonds = se->num_bonds; - } - -return dst; -} -int intel_virtual_engine_attach_bond (struct intel_engine_cs *engine,const struct intel_engine_cs *master,const struct intel_engine_cs *sibling)
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +0 -3
@@ -36,9 +36,6 @@ struct intel_context * intel_execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count); -struct intel_context * -intel_execlists_clone_virtual (struct intel_engine_cs *src); -int intel_virtual_engine_attach_bond (struct intel_engine_cs *engine,const struct intel_engine_cs *master,const struct intel_engine_cs *sibling);
include/uapi/drm/i915_drm.h +3 -13
@@ -2006,20 +2006,10 @@ struct drm_i915_gem_context_create_ext_setparam {struct drm_i915_gem_context_param param; }; -struct drm_i915_gem_context_create_ext_clone { +/* This API has been removed. On the off chance someone somewhere has +* attempted to use it, never re-use this extension number. +*/ #define I915_CONTEXT_CREATE_EXT_CLONE 1 -struct i915_user_extension base; -__u32 clone_id; -__u32 flags; -#define I915_CONTEXT_CLONE_ENGINES (1u << 0) -#define I915_CONTEXT_CLONE_FLAGS (1u << 1) -#define I915_CONTEXT_CLONE_SCHEDATTR (1u << 2) -#define I915_CONTEXT_CLONE_SSEU (1u << 3) -#define I915_CONTEXT_CLONE_TIMELINE (1u << 4) -#define I915_CONTEXT_CLONE_VM (1u << 5) -#define I915_CONTEXT_CLONE_UNKNOWN -(I915_CONTEXT_CLONE_VM << 1) -__u64 rsvd; -};struct drm_i915_gem_context_destroy {__u32 ctx_id;
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 7/30 drm/i915: Implement SINGLE_TIMELINE with a syncobj (v4)
In reply to Jason EkstrandThis API is entirely unnecessary and I'd love to get rid of it. If userspace wants a single timeline across multiple contexts, they can either use implicit synchronization or a syncobj, both of which existed at the time this feature landed. The justification given at the time was that it would help GL drivers which are inherently single-timeline. However, neither of our GL drivers actually wanted the feature. i965 was already in maintenance mode at the time and iris uses syncobj for everything.
Unfortunately, as much as I'd love to get rid of it, it is used by the media driver so we can't do that. We can, however, do the next-best thing which is to embed a syncobj in the context and do exactly what we'd expect from userspace internally. This isn't an entirely identical implementation because it's no longer atomic if userspace races with itself by calling execbuffer2 twice simultaneously from different threads. It won't crash in that case; it just doesn't guarantee any ordering between those two submits. It also means that sync files exported from different engines on a SINGLE_TIMELINE context will have different fence contexts. This is visible to userspace if it looks at the obj_name field of sync_fence_info.
Moving SINGLE_TIMELINE to a syncobj emulation has a couple of technical advantages beyond mere annoyance. One is that intel_timeline is no longer an api-visible object and can remain entirely an implementation detail. This may be advantageous as we make scheduler changes going forward. Second is that, together with deleting the CLONE_CONTEXT API, we should now have a 1:1 mapping between intel_context and intel_timeline which may help us reduce locking.
v2 (Tvrtko Ursulin):
- Update the comment on i915_gem_context::syncobj to mention that it's an emulation and the possible race if userspace calls execbuffer2 twice on the same context concurrently.
v2 (Jason Ekstrand):
- Wrap the checks for eb.gem_context->syncobj in unlikely()
- Drop the dma_fence reference
- Improved commit message
v3 (Jason Ekstrand):
- Move the dma_fence_put() to before the error exit
v4 (Tvrtko Ursulin):
- Add a comment about fence contexts to the commit message
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 49 +++++-------------- .../gpu/drm/i915/gem/i915_gem_context_types.h | 14 +++++- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 16 ++++++ 3 files changed, 40 insertions(+), 39 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +11 -38
@@ -67,6 +67,8 @@#include <linux/log2.h> #include <linux/nospec.h> +#include <drm/drm_syncobj.h> +#include "gt/gen6_ppgtt.h" #include "gt/intel_context.h" #include "gt/intel_context_param.h"
@@ -224,10 +226,6 @@ static void intel_context_set_gem(struct intel_context *ce, ce->vm = vm; } -GEM_BUG_ON (ce->timeline); -if (ctx->timeline) - ce->timeline =intel_timeline_get (ctx->timeline); -if (ctx->sched.priority >=I915_PRIORITY_NORMAL &&intel_engine_has_timeslices (ce->engine))__set_bit (CONTEXT_USE_SEMAPHORES , &ce->flags);
@@ -351,9 +349,6 @@ void i915_gem_context_release(struct kref *ref)mutex_destroy (&ctx->engines_mutex);mutex_destroy (&ctx->lut_mutex); -if (ctx->timeline) -intel_timeline_put (ctx->timeline); -put_pid (ctx->pid);mutex_destroy (&ctx->mutex);
@@ -570,6 +565,9 @@ static void context_close(struct i915_gem_context *ctx)if (vm)i915_vm_close (vm); +if (ctx->syncobj) +drm_syncobj_put (ctx->syncobj); + ctx->file_priv =ERR_PTR (-EBADF );/*
@@ -765,33 +763,11 @@ static void __assign_ppgtt(struct i915_gem_context *ctx,i915_vm_close (vm); } -static void __set_timeline (struct intel_timeline **dst, -struct intel_timeline *src) -{ -struct intel_timeline *old = *dst; - - *dst = src ?intel_timeline_get (src) :NULL ; - -if (old) -intel_timeline_put (old); -} - -static void __apply_timeline (struct intel_context *ce,void *timeline) -{ -__set_timeline (&ce->timeline, timeline); -} - -static void __assign_timeline (struct i915_gem_context *ctx, -struct intel_timeline *timeline) -{ -__set_timeline (&ctx->timeline, timeline); -context_apply_all (ctx, __apply_timeline, timeline); -} -static struct i915_gem_context *i915_gem_create_context (struct drm_i915_private *i915,unsigned int flags) {struct i915_gem_context *ctx; +int ret;if (flags &I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE && !HAS_EXECLISTS (i915))
@@ -820,16 +796,13 @@ i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags) }if (flags &I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE ) { -struct intel_timeline *timeline; - - timeline =intel_timeline_create (&i915->gt); -if (IS_ERR (timeline)) { + ret =drm_syncobj_create (&ctx->syncobj, +DRM_SYNCOBJ_CREATE_SIGNALED , +NULL ); +if (ret) {context_close (ctx); -return ERR_CAST (timeline); +return ERR_PTR (ret); } - -__assign_timeline (ctx, timeline); -intel_timeline_put (timeline); }trace_i915_context_create (ctx);
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +13 -1
@@ -83,7 +83,19 @@ struct i915_gem_context {struct i915_gem_engines __rcu *engines;struct mutex engines_mutex;/* guards writes to engines */ -struct intel_timeline *timeline; +/** +* @syncobj: Shared timeline syncobj +* +* When the SHARED_TIMELINE flag is set on context creation, we +* emulate a single timeline across all engines using this syncobj. +* For every execbuffer2 call, this syncobj is used as both an in- +* and out-fence. Unlike the real intel_timeline, this doesn't +* provide perfect atomic in-order guarantees if the client races +* with itself by calling execbuffer2 twice concurrently. However, +* if userspace races with itself, that's not likely to yield well- +* defined results anyway so we choose to not care. +*/ +struct drm_syncobj *syncobj;/** * @vm: unique address space (GTT)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +16 -0
@@ -3479,6 +3479,16 @@ i915_gem_do_execbuffer(struct drm_device *dev, goto err_vma; } +if (unlikely (eb.gem_context->syncobj)) { +struct dma_fence *fence; + + fence =drm_syncobj_fence_get (eb.gem_context->syncobj); + err =i915_request_await_dma_fence (eb.request, fence); +dma_fence_put (fence); +if (err) + goto err_ext; + } +if (in_fence) {if (args->flags &I915_EXEC_FENCE_SUBMIT ) err = i915_request_await_execution(eb.request,
@@ -3536,6 +3546,12 @@ i915_gem_do_execbuffer(struct drm_device *dev,fput (out_fence->file); } } + +if (unlikely (eb.gem_context->syncobj)) { +drm_syncobj_replace_fence (eb.gem_context->syncobj, + &eb.request->fence); + } +i915_request_put (eb.request); err_vma:
Signature · 1 line
2.31.1
PATCH 8/30 drm/i915: Drop getparam support for I915_CONTEXT_PARAM_ENGINES
In reply to Jason EkstrandThis has never been used by any userspace except IGT and provides no real functionality beyond parroting back parameters userspace passed in as part of context creation or via setparam. If the context is in legacy mode (where you use I915_EXEC_RENDER and friends), it returns success with zero data so it's not useful for discovering what engines are in the context. It's also not a replacement for the recently removed I915_CONTEXT_CLONE_ENGINES because it doesn't return any of the balancing or bonding information.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 77 +-------------------- 1 file changed, 1 insertion(+), 76 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -76
@@ -1724,78 +1724,6 @@ set_engines(struct i915_gem_context *ctx,return 0 ; } -static int -get_engines (struct i915_gem_context *ctx, -struct drm_i915_gem_context_param *args) -{ -struct i915_context_param_engines __user *user; -struct i915_gem_engines *e; -size_t n, count, size; -bool user_engines; -int err =0 ; - - e =__context_engines_await (ctx, &user_engines); -if (!e) -return -ENOENT ; - -if (!user_engines) { -i915_sw_fence_complete (&e->fence); - args->size =0 ; -return 0 ; - } - - count = e->num_engines; - -/* Be paranoid in case we have an impedance mismatch */ -if (!check_struct_size (user, engines, count, &size)) { - err = -EINVAL ; - goto err_free; - } -if (overflows_type (size, args->size)) { - err = -EINVAL ; - goto err_free; - } - -if (!args->size) { - args->size = size; - goto err_free; - } - -if (args->size < size) { - err = -EINVAL ; - goto err_free; - } - - user =u64_to_user_ptr (args->value); -if (put_user (0 , &user->extensions)) { - err = -EFAULT ; - goto err_free; - } - -for (n =0 ; n < count; n++) { -struct i915_engine_class_instance ci = { - .engine_class =I915_ENGINE_CLASS_INVALID , - .engine_instance =I915_ENGINE_CLASS_INVALID_NONE , - }; - -if (e->engines[n]) { - ci.engine_class = e->engines[n]->engine->uabi_class; - ci.engine_instance = e->engines[n]->engine->uabi_instance; - } - -if (copy_to_user (&user->engines[n], &ci,sizeof (ci))) { - err = -EFAULT ; - goto err_free; - } - } - - args->size = size; - -err_free: -i915_sw_fence_complete (&e->fence); -return err; -} -static int set_persistence (struct i915_gem_context *ctx,const struct drm_i915_gem_context_param *args)
@@ -2126,10 +2054,6 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data, ret =get_ppgtt (file_priv, ctx, args);break ; -case I915_CONTEXT_PARAM_ENGINES : - ret =get_engines (ctx, args); -break ; -case I915_CONTEXT_PARAM_PERSISTENCE : args->size =0 ; args->value =i915_gem_context_is_persistent (ctx);
@@ -2137,6 +2061,7 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,case I915_CONTEXT_PARAM_NO_ZEROMAP :case I915_CONTEXT_PARAM_BAN_PERIOD : +case I915_CONTEXT_PARAM_ENGINES :case I915_CONTEXT_PARAM_RINGSIZE :default : ret = -EINVAL ;
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 9/30 drm/i915/gem: Disallow bonding of virtual engines (v3)
In reply to Jason EkstrandThis adds a bunch of complexity which the media driver has never actually used. The media driver does technically bond a balanced engine to another engine but the balanced engine only has one engine in the sibling set. This doesn't actually result in a virtual engine.
This functionality was originally added to handle cases where we may have more than two video engines and media might want to load-balance their bonded submits by, for instance, submitting to a balanced vcs0-1 as the primary and then vcs2-3 as the secondary. However, no such hardware has shipped thus far and, if we ever want to enable such use-cases in the future, we'll use the up-and-coming parallel submit API which targets GuC submission.
This makes I915_CONTEXT_ENGINES_EXT_BOND a total no-op. We leave the validation code in place in case we ever decide we want to do something interesting with the bonding information.
v2 (Jason Ekstrand):
- Don't delete quite as much code.
v3 (Tvrtko Ursulin):
- Add some history to the commit message
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 18 +- .../drm/i915/gt/intel_execlists_submission.c | 69 ------ .../drm/i915/gt/intel_execlists_submission.h | 5 +- drivers/gpu/drm/i915/gt/selftest_execlists.c | 229 ------------------ 4 files changed, 8 insertions(+), 313 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +6 -12
@@ -1552,6 +1552,12 @@ set_engines__bond(struct i915_user_extension __user *base, void *data) } virtual = set->engines->engines[idx]->engine; +if (intel_engine_is_virtual (virtual)) { +drm_dbg (&i915->drm, +"Bonding with virtual engines not allowed\n" ); +return -EINVAL ; + } + err =check_user_mbz (&ext->flags);if (err)return err;
@@ -1592,18 +1598,6 @@ set_engines__bond(struct i915_user_extension __user *base, void *data) n, ci.engine_class, ci.engine_instance);return -EINVAL ; } - -/* -* A non-virtual engine has no siblings to choose between; and -* a submit fence will always be directed to the one engine. -*/ -if (intel_engine_is_virtual (virtual)) { - err =intel_virtual_engine_attach_bond (virtual, - master, - bond); -if (err) -return err; - } }return 0 ;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +0 -69
100 lines not on this page · raw message
drivers/gpu/drm/i915/gt/intel_execlists_submission.h +2 -3
@@ -36,8 +36,7 @@ struct intel_context * intel_execlists_create_virtual(struct intel_engine_cs **siblings, unsigned int count); -int intel_virtual_engine_attach_bond (struct intel_engine_cs *engine, -const struct intel_engine_cs *master, -const struct intel_engine_cs *sibling); +bool +intel_engine_in_execlists_submission_mode (const struct intel_engine_cs *engine);#endif /* __INTEL_EXECLISTS_SUBMISSION_H__ */
drivers/gpu/drm/i915/gt/selftest_execlists.c +0 -229
241 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH 10/30 drm/i915/gem: Remove engine auto-magic with FENCE_SUBMIT (v2)
In reply to Jason EkstrandEven though FENCE_SUBMIT is only documented to wait until the request in the in-fence starts instead of waiting until it completes, it has a bit more magic than that. If FENCE_SUBMIT is used to submit something to a balanced engine, we would wait to assign engines until the primary request was ready to start and then attempt to assign it to a different engine than the primary. There is an IGT test (the bonded-slice subtest of gem_exec_balancer) which exercises this by submitting a primary batch to a specific VCS and then using FENCE_SUBMIT to submit a secondary which can run on any VCS and have i915 figure out which VCS to run it on such that they can run in parallel.
However, this functionality has never been used in the real world. The media driver (the only user of FENCE_SUBMIT) always picks exactly two physical engines to bond and never asks us to pick which to use.
v2 (Daniel Vetter):
- Mention the exact IGT test this breaks
--- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 +- drivers/gpu/drm/i915/gt/intel_engine_types.h | 7 ------- .../drm/i915/gt/intel_execlists_submission.c | 17 ----------------- 3 files changed, 1 insertion(+), 25 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +1 -1
@@ -3493,7 +3493,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (args->flags &I915_EXEC_FENCE_SUBMIT ) err =i915_request_await_execution (eb.request, in_fence, - eb.engine->bond_execute); +NULL );else err =i915_request_await_dma_fence (eb.request, in_fence);
drivers/gpu/drm/i915/gt/intel_engine_types.h +0 -7
@@ -416,13 +416,6 @@ struct intel_engine_cs {*/ void (*submit_request)(struct i915_request *rq); -/* -* Called on signaling of a SUBMIT_FENCE, passing along the signaling -* request down to the bonded pairs. -*/ -void (*bond_execute)(struct i915_request *rq, -struct dma_fence *signal); -void (*release)(struct intel_engine_cs *engine);struct intel_engine_execlists execlists;
drivers/gpu/drm/i915/gt/intel_execlists_submission.c +0 -17
@@ -3655,22 +3655,6 @@ static void virtual_submit_request(struct i915_request *rq)spin_unlock_irqrestore (&ve->base.sched_engine->lock, flags); } -static void -virtual_bond_execute (struct i915_request *rq,struct dma_fence *signal) -{ -intel_engine_mask_t allowed, exec; - - allowed = ~to_request (signal)->engine->mask; - -/* Restrict the bonded request to run on only the available engines */ - exec =READ_ONCE (rq->execution_mask); -while (!try_cmpxchg (&rq->execution_mask, &exec, exec & allowed)) - ; - -/* Prevent the master from being re-run on the bonded engines */ -to_request (signal)->execution_mask &= ~allowed; -} -struct intel_context *intel_execlists_create_virtual (struct intel_engine_cs **siblings,unsigned int count)
@@ -3731,7 +3715,6 @@ intel_execlists_create_virtual(struct intel_engine_cs **siblings, ve->base.sched_engine->schedule = i915_schedule; ve->base.sched_engine->kick_backend = kick_execlists; ve->base.submit_request = virtual_submit_request; - ve->base.bond_execute = virtual_bond_execute;INIT_LIST_HEAD (virtual_queue (ve));tasklet_setup (&ve->base.sched_engine->tasklet, virtual_submission_tasklet);
Signature · 1 line
2.31.1
PATCH 11/30 drm/i915/request: Remove the hook from await_execution
In reply to Jason EkstrandThis was only ever used for FENCE_SUBMIT automatic engine selection which was removed in the previous commit.
--- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 3 +- drivers/gpu/drm/i915/i915_request.c | 42 ++++--------------- drivers/gpu/drm/i915/i915_request.h | 4 +- 3 files changed, 9 insertions(+), 40 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +1 -2
@@ -3492,8 +3492,7 @@ i915_gem_do_execbuffer(struct drm_device *dev,if (in_fence) {if (args->flags &I915_EXEC_FENCE_SUBMIT ) err =i915_request_await_execution (eb.request, - in_fence, -NULL ); + in_fence);else err =i915_request_await_dma_fence (eb.request, in_fence);
drivers/gpu/drm/i915/i915_request.c +7 -35
107 lines not on this page · raw message
drivers/gpu/drm/i915/i915_request.h +1 -3
@@ -352,9 +352,7 @@ int i915_request_await_object(struct i915_request *to,int i915_request_await_dma_fence (struct i915_request *rq,struct dma_fence *fence);int i915_request_await_execution (struct i915_request *rq, -struct dma_fence *fence, -void (*hook)(struct i915_request *rq, -struct dma_fence *signal)); +struct dma_fence *fence);void i915_request_add (struct i915_request *rq);
Signature · 1 line
2.31.1
PATCH 12/30 drm/i915/gem: Disallow creating contexts with too many engines
In reply to Jason EkstrandThere's no sense in allowing userspace to create more engines than it can possibly access via execbuf.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +4 -4
@@ -1639,11 +1639,11 @@ set_engines(struct i915_gem_context *ctx,return -EINVAL ; } -/* -* Note that I915_EXEC_RING_MASK limits execbuf to only using the -* first 64 engines defined here. -*/ num_engines = (args->size -sizeof (*user)) /sizeof (*user->engines); +/* RING_MASK has no shift so we can use it directly here */ +if (num_engines >I915_EXEC_RING_MASK +1 ) +return -EINVAL ; + set.engines =alloc_engines (num_engines);if (!set.engines)return -ENOMEM ;
Signature · 1 line
2.31.1
PATCH 13/30 drm/i915: Stop manually RCU banging in reset_stats_ioctl (v2)
In reply to Jason EkstrandAs far as I can tell, the only real reason for this is to avoid taking a reference to the i915_gem_context. The cost of those two atomics probably pales in comparison to the cost of the ioctl itself so we're really not buying ourselves anything here. We're about to make context lookup a tiny bit more complicated, so let's get rid of the one hand- rolled case.
Some usermode drivers such as our Vulkan driver call GET_RESET_STATS on every execbuf so the perf here could theoretically be an issue. If this ever does become a performance issue for any such userspace drivers, they can use set CONTEXT_PARAM_RECOVERABLE to false and look for -EIO coming from execbuf to check for hangs instead.
v2 (Daniel Vetter):
- Add a comment in the commit message about recoverable contexts
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 13 ++++--------- drivers/gpu/drm/i915/i915_drv.h | 8 +------- 2 files changed, 5 insertions(+), 16 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +4 -9
@@ -2090,16 +2090,13 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,struct drm_i915_private *i915 =to_i915 (dev);struct drm_i915_reset_stats *args = data;struct i915_gem_context *ctx; -int ret;if (args->flags || args->pad)return -EINVAL ; - ret = -ENOENT ; -rcu_read_lock (); - ctx =__i915_gem_context_lookup_rcu (file->driver_priv, args->ctx_id); + ctx =i915_gem_context_lookup (file->driver_priv, args->ctx_id);if (!ctx) - goto out; +return -ENOENT ;/* * We opt for unserialised reads here. This may result in tearing
@@ -2116,10 +2113,8 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev, args->batch_active =atomic_read (&ctx->guilty_count); args->batch_pending =atomic_read (&ctx->active_count); - ret =0 ; -out: -rcu_read_unlock (); -return ret; +i915_gem_context_put (ctx); +return 0 ; }/* GEM context-engines iterator: for_each_gem_engine() */
drivers/gpu/drm/i915/i915_drv.h +1 -7
@@ -1853,19 +1853,13 @@ struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,struct dma_buf *i915_gem_prime_export (struct drm_gem_object *gem_obj,int flags); -static inline struct i915_gem_context * -__i915_gem_context_lookup_rcu (struct drm_i915_file_private *file_priv,u32 id) -{ -return xa_load (&file_priv->context_xa, id); -} -static inline struct i915_gem_context *i915_gem_context_lookup (struct drm_i915_file_private *file_priv,u32 id) {struct i915_gem_context *ctx;rcu_read_lock (); - ctx =__i915_gem_context_lookup_rcu (file_priv, id); + ctx =xa_load (&file_priv->context_xa, id);if (ctx && !kref_get_unless_zero (&ctx->ref)) ctx =NULL ;rcu_read_unlock ();
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 14/30 drm/i915/gem: Add a separate validate_priority helper
In reply to Jason EkstrandWith the proto-context stuff added later in this series, we end up having to duplicate set_priority. This lets us avoid duplicating the validation logic.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 42 +++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +27 -15
@@ -169,6 +169,28 @@ lookup_user_engine(struct i915_gem_context *ctx,return i915_gem_context_get_engine (ctx, idx); } +static int validate_priority (struct drm_i915_private *i915, +const struct drm_i915_gem_context_param *args) +{ +s64 priority = args->value; + +if (args->size) +return -EINVAL ; + +if (!(i915->caps.scheduler &I915_SCHEDULER_CAP_PRIORITY )) +return -ENODEV ; + +if (priority >I915_CONTEXT_MAX_USER_PRIORITY || + priority <I915_CONTEXT_MIN_USER_PRIORITY ) +return -EINVAL ; + +if (priority >I915_CONTEXT_DEFAULT_PRIORITY && + !capable (CAP_SYS_NICE )) +return -EPERM ; + +return 0 ; +} +static struct i915_address_space *context_get_vm_rcu (struct i915_gem_context *ctx) {
@@ -1744,23 +1766,13 @@ static void __apply_priority(struct intel_context *ce, void *arg)static int set_priority (struct i915_gem_context *ctx,const struct drm_i915_gem_context_param *args) { -s64 priority = args->value; - -if (args->size) -return -EINVAL ; - -if (!(ctx->i915->caps.scheduler &I915_SCHEDULER_CAP_PRIORITY )) -return -ENODEV ; - -if (priority >I915_CONTEXT_MAX_USER_PRIORITY || - priority <I915_CONTEXT_MIN_USER_PRIORITY ) -return -EINVAL ; +int err; -if (priority >I915_CONTEXT_DEFAULT_PRIORITY && - !capable (CAP_SYS_NICE )) -return -EPERM ; + err =validate_priority (ctx->i915, args); +if (err) +return err; - ctx->sched.priority = priority; + ctx->sched.priority = args->value;context_apply_all (ctx, __apply_priority, ctx);return 0 ;
Signature · 1 line
2.31.1
PATCH 15/30 drm/i915: Add gem/i915_gem_context.h to the docs
In reply to Jason EkstrandIn order to prevent kernel doc warnings, also fill out docs for any missing fields and fix those that forgot the "@".
--- Documentation/gpu/i915.rst | 2 + .../gpu/drm/i915/gem/i915_gem_context_types.h | 43 ++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-)
Documentation/gpu/i915.rst +2 -0
@@ -422,6 +422,8 @@ Batchbuffer Parsing User Batchbuffer Execution -------------------------- +.. kernel-doc:: drivers/gpu/drm/i915/gem/i915_gem_context_types.h + .. kernel-doc:: drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c :doc: User command execution
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +36 -7
113 lines not on this page · raw message
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 16/30 drm/i915/gem: Add an intermediate proto_context struct (v5)
In reply to Jason EkstrandThe current context uAPI allows for two methods of setting context
parameters: SET_CONTEXT_PARAM and CONTEXT_CREATE_EXT_SETPARAM. The former is allowed to be called at any time while the later happens as part of GEM_CONTEXT_CREATE. Currently, everything settable via one is settable via the other. While some params are fairly simple and setting them on a live context is harmless such the context priority, others are far trickier such as the VM or the set of engines. In order to swap out the VM, for instance, we have to delay until all current in-flight work is complete, swap in the new VM, and then continue. This leads to a plethora of potential race conditions we'd really rather avoid.
Unfortunately, both methods of setting the VM and the engine set are in active use today so we can't simply disallow setting the VM or engine set vial SET_CONTEXT_PARAM. In order to work around this wart, this commit adds a proto-context struct which contains all the context create parameters.
v2 (Daniel Vetter):
- Better commit message
- Use __set/clear_bit instead of set/clear_bit because there's no race and we don't need the atomics
v3 (Daniel Vetter):
- Use manual bitops and BIT() instead of __set_bit
v4 (Daniel Vetter):
- Add a changelog to the commit message
- Better hyperlinking in docs
- Create the default PPGTT in i915_gem_create_context
v5 (Daniel Vetter):
- Hand-roll the initialization of UCONTEXT_PERSISTENCE
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 84 +++++++++++++++---- .../gpu/drm/i915/gem/i915_gem_context_types.h | 22 +++++ .../gpu/drm/i915/gem/selftests/mock_context.c | 16 +++- 3 files changed, 105 insertions(+), 17 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +69 -15
@@ -191,6 +191,43 @@ static int validate_priority(struct drm_i915_private *i915,return 0 ; } +static void proto_context_close (struct i915_gem_proto_context *pc) +{ +if (pc->vm) +i915_vm_put (pc->vm); +kfree (pc); +} + +static struct i915_gem_proto_context * +proto_context_create (struct drm_i915_private *i915,unsigned int flags) +{ +struct i915_gem_proto_context *pc, *err; + + pc =kzalloc (sizeof (*pc),GFP_KERNEL ); +if (!pc) +return ERR_PTR (-ENOMEM ); + + pc->user_flags =BIT (UCONTEXT_BANNABLE ) | +BIT (UCONTEXT_RECOVERABLE ); +if (i915->params.enable_hangcheck) + pc->user_flags |=BIT (UCONTEXT_PERSISTENCE ); + pc->sched.priority =I915_PRIORITY_NORMAL ; + +if (flags &I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE ) { +if (!HAS_EXECLISTS (i915)) { + err =ERR_PTR (-EINVAL ); + goto proto_close; + } + pc->single_timeline = true; + } + +return pc; + +proto_close: +proto_context_close (pc); +return err; +} +static struct i915_address_space *context_get_vm_rcu (struct i915_gem_context *ctx) {
@@ -660,7 +697,8 @@ static int __context_set_persistence(struct i915_gem_context *ctx, bool state) }static struct i915_gem_context * -__create_context (struct drm_i915_private *i915) +__create_context (struct drm_i915_private *i915, +const struct i915_gem_proto_context *pc) {struct i915_gem_context *ctx;struct i915_gem_engines *e;
@@ -673,7 +711,7 @@ __create_context(struct drm_i915_private *i915)kref_init (&ctx->ref); ctx->i915 = i915; - ctx->sched.priority =I915_PRIORITY_NORMAL ; + ctx->sched = pc->sched;mutex_init (&ctx->mutex);INIT_LIST_HEAD (&ctx->link);
@@ -696,9 +734,7 @@ __create_context(struct drm_i915_private *i915)* is no remap info, it will be a NOP. */ ctx->remap_slice =ALL_L3_SLICES (i915); -i915_gem_context_set_bannable (ctx); -i915_gem_context_set_recoverable (ctx); -__context_set_persistence (ctx, true/* cgroup hook? */ ); + ctx->user_flags = pc->user_flags;for (i =0 ; i <ARRAY_SIZE (ctx->hang_timestamp); i++) ctx->hang_timestamp[i] = jiffies -CONTEXT_FAST_HANG_JIFFIES ;
@@ -786,20 +822,22 @@ static void __assign_ppgtt(struct i915_gem_context *ctx, }static struct i915_gem_context * -i915_gem_create_context (struct drm_i915_private *i915,unsigned int flags) +i915_gem_create_context (struct drm_i915_private *i915, +const struct i915_gem_proto_context *pc) {struct i915_gem_context *ctx;int ret; -if (flags &I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE && - !HAS_EXECLISTS (i915)) -return ERR_PTR (-EINVAL ); - - ctx =__create_context (i915); + ctx =__create_context (i915, pc);if (IS_ERR (ctx))return ctx; -if (HAS_FULL_PPGTT (i915)) { +if (pc->vm) { +/* __assign_ppgtt() requires this mutex to be held */ +mutex_lock (&ctx->mutex); +__assign_ppgtt (ctx, pc->vm); +mutex_unlock (&ctx->mutex); + }else if (HAS_FULL_PPGTT (i915)) {struct i915_ppgtt *ppgtt; ppgtt =i915_ppgtt_create (&i915->gt);
@@ -810,14 +848,16 @@ i915_gem_create_context(struct drm_i915_private *i915, unsigned int flags)return ERR_CAST (ppgtt); } +/* __assign_ppgtt() requires this mutex to be held */ mutex_lock (&ctx->mutex);__assign_ppgtt (ctx, &ppgtt->vm);mutex_unlock (&ctx->mutex); +/* __assign_ppgtt() takes another reference for us */ i915_vm_put (&ppgtt->vm); } -if (flags &I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE ) { +if (pc->single_timeline) { ret =drm_syncobj_create (&ctx->syncobj,DRM_SYNCOBJ_CREATE_SIGNALED ,NULL );
@@ -883,6 +923,7 @@ int i915_gem_context_open(struct drm_i915_private *i915, struct drm_file *file) {struct drm_i915_file_private *file_priv = file->driver_priv; +struct i915_gem_proto_context *pc;struct i915_gem_context *ctx;int err;u32 id;
@@ -892,7 +933,14 @@ int i915_gem_context_open(struct drm_i915_private *i915,/* 0 reserved for invalid/unassigned ppgtt */ xa_init_flags (&file_priv->vm_xa,XA_FLAGS_ALLOC1 ); - ctx =i915_gem_create_context (i915,0 ); + pc =proto_context_create (i915,0 ); +if (IS_ERR (pc)) { + err =PTR_ERR (pc); + goto err; + } + + ctx =i915_gem_create_context (i915, pc); +proto_context_close (pc);if (IS_ERR (ctx)) { err =PTR_ERR (ctx); goto err;
@@ -1884,6 +1932,7 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, {struct drm_i915_private *i915 =to_i915 (dev);struct drm_i915_gem_context_create_ext *args = data; +struct i915_gem_proto_context *pc;struct create_ext ext_data;int ret;u32 id;
@@ -1906,7 +1955,12 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,return -EIO ; } - ext_data.ctx =i915_gem_create_context (i915, args->flags); + pc =proto_context_create (i915, args->flags); +if (IS_ERR (pc)) +return PTR_ERR (pc); + + ext_data.ctx =i915_gem_create_context (i915, pc); +proto_context_close (pc);if (IS_ERR (ext_data.ctx))return PTR_ERR (ext_data.ctx);
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +22 -0
@@ -66,6 +66,28 @@ struct i915_gem_engines_iter {const struct i915_gem_engines *engines; }; +/** +* struct i915_gem_proto_context - prototype context +* +* The struct i915_gem_proto_context represents the creation parameters for +* a struct i915_gem_context. This is used to gather parameters provided +* either through creation flags or via SET_CONTEXT_PARAM so that, when we +* create the final i915_gem_context, those parameters can be immutable. +*/ +struct i915_gem_proto_context { +/** @vm: See &i915_gem_context.vm */ +struct i915_address_space *vm; + +/** @user_flags: See &i915_gem_context.user_flags */ +unsigned long user_flags; + +/** @sched: See &i915_gem_context.sched */ +struct i915_sched_attr sched; + +/** @single_timeline: See See &i915_gem_context.syncobj */ +bool single_timeline; +}; +/** * struct i915_gem_context - client state *
drivers/gpu/drm/i915/gem/selftests/mock_context.c +14 -2
@@ -80,11 +80,17 @@ void mock_init_contexts(struct drm_i915_private *i915)struct i915_gem_context *live_context (struct drm_i915_private *i915,struct file *file) { +struct i915_gem_proto_context *pc;struct i915_gem_context *ctx;int err;u32 id; - ctx =i915_gem_create_context (i915,0 ); + pc =proto_context_create (i915,0 ); +if (IS_ERR (pc)) +return ERR_CAST (pc); + + ctx =i915_gem_create_context (i915, pc); +proto_context_close (pc);if (IS_ERR (ctx))return ctx;
@@ -142,8 +148,14 @@ struct i915_gem_context * kernel_context(struct drm_i915_private *i915) {struct i915_gem_context *ctx; +struct i915_gem_proto_context *pc; + + pc =proto_context_create (i915,0 ); +if (IS_ERR (pc)) +return ERR_CAST (pc); - ctx =i915_gem_create_context (i915,0 ); + ctx =i915_gem_create_context (i915, pc); +proto_context_close (pc);if (IS_ERR (ctx))return ctx;
Signature · 1 line
2.31.1
PATCH 17/30 drm/i915/gem: Rework error handling in default_engines
In reply to Jason EkstrandSince free_engines works for partially constructed engine sets, we can use the usual goto pattern.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +8 -5
@@ -366,7 +366,7 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx) {const struct intel_gt *gt = &ctx->i915->gt;struct intel_engine_cs *engine; -struct i915_gem_engines *e; +struct i915_gem_engines *e, *err;enum intel_engine_id id; e =alloc_engines (I915_NUM_ENGINES );
@@ -384,18 +384,21 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx) ce =intel_context_create (engine);if (IS_ERR (ce)) { -__free_engines (e, e->num_engines +1 ); -return ERR_CAST (ce); + err =ERR_CAST (ce); + goto free_engines; }intel_context_set_gem (ce, ctx); e->engines[engine->legacy_idx] = ce; - e->num_engines =max (e->num_engines, engine->legacy_idx); + e->num_engines =max (e->num_engines, engine->legacy_idx +1 ); } - e->num_engines++;return e; + +free_engines: +free_engines (e); +return err; }void i915_gem_context_release (struct kref *ref)
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 18/30 drm/i915/gem: Optionally set SSEU in intel_context_set_gem
In reply to Jason EkstrandFor now this is a no-op because everyone passes in a null SSEU but it lets us get some of the error handling and selftest refactoring plumbed through.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 41 +++++++++++++++---- .../gpu/drm/i915/gem/selftests/mock_context.c | 6 ++- 2 files changed, 36 insertions(+), 11 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +32 -9
@@ -266,9 +266,12 @@ context_get_vm_rcu(struct i915_gem_context *ctx) }while (1 ); } -static void intel_context_set_gem (struct intel_context *ce, -struct i915_gem_context *ctx) +static int intel_context_set_gem (struct intel_context *ce, +struct i915_gem_context *ctx, +struct intel_sseu sseu) { +int ret =0 ; +GEM_BUG_ON (rcu_access_pointer (ce->gem_context));RCU_INIT_POINTER (ce->gem_context, ctx);
@@ -295,6 +298,12 @@ static void intel_context_set_gem(struct intel_context *ce,intel_context_set_watchdog_us (ce, (u64 )timeout_ms *1000 ); } + +/* A valid SSEU has no zero fields */ +if (sseu.slice_mask && !WARN_ON (ce->engine->class !=RENDER_CLASS )) + ret =intel_context_reconfigure_sseu (ce, sseu); + +return ret; }static void __free_engines (struct i915_gem_engines *e,unsigned int count)
@@ -362,7 +371,8 @@ static struct i915_gem_engines *alloc_engines(unsigned int count)return e; } -static struct i915_gem_engines *default_engines (struct i915_gem_context *ctx) +static struct i915_gem_engines *default_engines (struct i915_gem_context *ctx, +struct intel_sseu rcs_sseu) {const struct intel_gt *gt = &ctx->i915->gt;struct intel_engine_cs *engine;
@@ -375,6 +385,8 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)for_each_engine (engine, gt, id) {struct intel_context *ce; +struct intel_sseu sseu = {}; +int ret;if (engine->legacy_idx ==INVALID_ENGINE )continue ;
@@ -388,10 +400,18 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx) goto free_engines; } -intel_context_set_gem (ce, ctx); - e->engines[engine->legacy_idx] = ce; e->num_engines =max (e->num_engines, engine->legacy_idx +1 ); + +if (engine->class ==RENDER_CLASS ) + sseu = rcs_sseu; + + ret =intel_context_set_gem (ce, ctx, sseu); +if (ret) { + err =ERR_PTR (ret); + goto free_engines; + } + }return e;
@@ -705,6 +725,7 @@ __create_context(struct drm_i915_private *i915, {struct i915_gem_context *ctx;struct i915_gem_engines *e; +struct intel_sseu null_sseu = {};int err;int i;
@@ -722,7 +743,7 @@ __create_context(struct drm_i915_private *i915,INIT_LIST_HEAD (&ctx->stale.engines);mutex_init (&ctx->engines_mutex); - e =default_engines (ctx); + e =default_engines (ctx, null_sseu);if (IS_ERR (e)) { err =PTR_ERR (e); goto err_free;
@@ -1508,6 +1529,7 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data)struct intel_engine_cs *stack[16 ];struct intel_engine_cs **siblings;struct intel_context *ce; +struct intel_sseu null_sseu = {};u16 num_siblings, idx;unsigned int n;int err;
@@ -1580,7 +1602,7 @@ set_engines__load_balance(struct i915_user_extension __user *base, void *data) goto out_siblings; } -intel_context_set_gem (ce, set->ctx); +intel_context_set_gem (ce, set->ctx, null_sseu);if (cmpxchg (&set->engines->engines[idx],NULL , ce)) {intel_context_put (ce);
@@ -1688,6 +1710,7 @@ set_engines(struct i915_gem_context *ctx,struct drm_i915_private *i915 = ctx->i915;struct i915_context_param_engines __user *user =u64_to_user_ptr (args->value); +struct intel_sseu null_sseu = {};struct set_engines set = { .ctx = ctx };unsigned int num_engines, n;u64 extensions;
@@ -1697,7 +1720,7 @@ set_engines(struct i915_gem_context *ctx,if (!i915_gem_context_user_engines (ctx))return 0 ; - set.engines =default_engines (ctx); + set.engines =default_engines (ctx, null_sseu);if (IS_ERR (set.engines))return PTR_ERR (set.engines);
@@ -1754,7 +1777,7 @@ set_engines(struct i915_gem_context *ctx,return PTR_ERR (ce); } -intel_context_set_gem (ce, ctx); +intel_context_set_gem (ce, ctx, null_sseu); set.engines->engines[n] = ce; }
drivers/gpu/drm/i915/gem/selftests/mock_context.c +4 -2
@@ -14,6 +14,7 @@ mock_context(struct drm_i915_private *i915, {struct i915_gem_context *ctx;struct i915_gem_engines *e; +struct intel_sseu null_sseu = {}; ctx =kzalloc (sizeof (*ctx),GFP_KERNEL );if (!ctx)
@@ -31,7 +32,7 @@ mock_context(struct drm_i915_private *i915,i915_gem_context_set_persistence (ctx);mutex_init (&ctx->engines_mutex); - e =default_engines (ctx); + e =default_engines (ctx, null_sseu);if (IS_ERR (e)) goto err_free;RCU_INIT_POINTER (ctx->engines, e);
@@ -112,6 +113,7 @@ live_context_for_engine(struct intel_engine_cs *engine, struct file *file) {struct i915_gem_engines *engines;struct i915_gem_context *ctx; +struct intel_sseu null_sseu = {};struct intel_context *ce; engines =alloc_engines (1 );
@@ -130,7 +132,7 @@ live_context_for_engine(struct intel_engine_cs *engine, struct file *file)return ERR_CAST (ce); } -intel_context_set_gem (ce, ctx); +intel_context_set_gem (ce, ctx, null_sseu); engines->engines[0 ] = ce; engines->num_engines =1 ;
Signature · 1 line
2.31.1
PATCH 19/30 drm/i915: Add an i915_gem_vm_lookup helper
In reply to Jason EkstrandThis is the VM equivalent of i915_gem_context_lookup. It's only used once in this patch but future patches will need to duplicate this lookup code so it's better to have it in a helper.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 6 +----- drivers/gpu/drm/i915/i915_drv.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -5
@@ -1311,11 +1311,7 @@ static int set_ppgtt(struct drm_i915_file_private *file_priv,if (upper_32_bits (args->value))return -ENOENT ; -rcu_read_lock (); - vm =xa_load (&file_priv->vm_xa, args->value); -if (vm && !kref_get_unless_zero (&vm->ref)) - vm =NULL ; -rcu_read_unlock (); + vm =i915_gem_vm_lookup (file_priv, args->value);if (!vm)return -ENOENT ;
drivers/gpu/drm/i915/i915_drv.h +14 -0
@@ -1867,6 +1867,20 @@ i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id)return ctx; } +static inline struct i915_address_space * +i915_gem_vm_lookup (struct drm_i915_file_private *file_priv,u32 id) +{ +struct i915_address_space *vm; + +rcu_read_lock (); + vm =xa_load (&file_priv->vm_xa, id); +if (vm && !kref_get_unless_zero (&vm->ref)) + vm =NULL ; +rcu_read_unlock (); + +return vm; +} +/* i915_gem_evict.c */ int __must_check i915_gem_evict_something(struct i915_address_space *vm, u64 min_size, u64 alignment,
Signature · 1 line
2.31.1
Jani Nikula
maintainerRe: [PATCH 19/30] drm/i915: Add an i915_gem_vm_lookup helper
Quoted 6 lines · Jason Ekstrand
This is the VM equivalent of
i915_gem_context_lookup. It's only used once in this patch but future patches will need to duplicate this lookup code so it's better to have it in a helper.Signed-off-by Jason Ekstrand <[email protected]>Reviewed-by Daniel Vetter <[email protected]>
I'm looking at i915_drv.h.... and really, i915_drv.h is not your dumping ground. None of this belongs in i915_drv.h.
BR,
Jani.
Quoted 47 lines
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 6 +----- drivers/gpu/drm/i915/i915_drv.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-)drivers/gpu/drm/i915/gem/i915_gem_context.c +1
-5@@ -1311,11 +1311,7 @@ static int set_ppgtt(struct drm_i915_file_private *file_priv,if (upper_32_bits (args->value))return -ENOENT ; -rcu_read_lock (); - vm =xa_load (&file_priv->vm_xa, args->value); -if (vm && !kref_get_unless_zero (&vm->ref)) - vm =NULL ; -rcu_read_unlock (); + vm =i915_gem_vm_lookup (file_priv, args->value);if (!vm)return -ENOENT ;drivers/gpu/drm/i915/i915_drv.h +14
-0@@ -1867,6 +1867,20 @@ i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id)return ctx; } +static inline struct i915_address_space * +i915_gem_vm_lookup (struct drm_i915_file_private *file_priv,u32 id) +{ +struct i915_address_space *vm; + +rcu_read_lock (); + vm =xa_load (&file_priv->vm_xa, id); +if (vm && !kref_get_unless_zero (&vm->ref)) + vm =NULL ; +rcu_read_unlock (); + +return vm; +} +/* i915_gem_evict.c */ int __must_check i915_gem_evict_something(struct i915_address_space *vm, u64 min_size, u64 alignment,
Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
PATCH 20/30 drm/i915/gem: Make an alignment check more sensible
In reply to Jason EkstrandWhat we really want to check is that size of the engines array, i.e. args->size - sizeof(*user) is divisible by the element size, i.e. sizeof(*user->engines) because that's what's required for computing the array length right below the check. However, we're currently not doing this and instead doing a compile-time check that sizeof(*user) is divisible by sizeof(*user->engines) and avoiding the subtraction. As far as I can tell, the only reason for the more confusing pair of checks is to avoid a single subtraction of a constant.
The other thing the BUILD_BUG_ON might be trying to implicitly check is that offsetof(user->engines) == sizeof(*user) and we don't have any weird padding throwing us off. However, that's not the check it's doing and it's not even a reliable way to do that check.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -2
@@ -1723,9 +1723,8 @@ set_engines(struct i915_gem_context *ctx, goto replace; } -BUILD_BUG_ON (!IS_ALIGNED (sizeof (*user),sizeof (*user->engines)));if (args->size <sizeof (*user) || - !IS_ALIGNED (args->size,sizeof (*user->engines))) { + !IS_ALIGNED (args->size -sizeof (*user),sizeof (*user->engines))) {drm_dbg (&i915->drm,"Invalid size for engine array: %d\n" , args->size);return -EINVAL ;
Signature · 1 line
2.31.1
PATCH 21/30 drm/i915/gem: Use the proto-context to handle create parameters (v5)
In reply to Jason EkstrandThis means that the proto-context needs to grow support for engine configuration information as well as setparam logic. Fortunately, we'll be deleting a lot of setparam logic on the primary context shortly so it will hopefully balance out.
There's an extra bit of fun here when it comes to setting SSEU and the way it interacts with PARAM_ENGINES. Unfortunately, thanks to SET_CONTEXT_PARAM and not being allowed to pick the order in which we handle certain parameters, we have think about those interactions.
v2 (Daniel Vetter):
- Add a proto_context_free_user_engines helper
- Comment on SSEU in the commit message
- Use proto_context_set_persistence in set_proto_ctx_param
v3 (Daniel Vetter):
- Fix a doc comment
- Do an explicit HAS_FULL_PPGTT check in set_proto_ctx_vm instead of relying on pc->vm != NULL.
- Handle errors for CONTEXT_PARAM_PERSISTENCE
- Don't allow more resetting user engines
- Rework initialization of UCONTEXT_PERSISTENCE
v4 (Jason Ekstrand):
- Move hand-rolled initialization of UCONTEXT_PERSISTENCE to an earlier patch
v5 (Jason Ekstrand):
- Move proto_context_set_persistence to this patch
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 577 +++++++++++++++++- .../gpu/drm/i915/gem/i915_gem_context_types.h | 58 ++ 2 files changed, 618 insertions(+), 17 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +560 -17
@@ -193,11 +193,59 @@ static int validate_priority(struct drm_i915_private *i915,static void proto_context_close (struct i915_gem_proto_context *pc) { +int i; +if (pc->vm)i915_vm_put (pc->vm); +if (pc->user_engines) { +for (i =0 ; i < pc->num_user_engines; i++) +kfree (pc->user_engines[i].siblings); +kfree (pc->user_engines); + }kfree (pc); } +static int proto_context_set_persistence (struct drm_i915_private *i915, +struct i915_gem_proto_context *pc, +bool persist) +{ +if (persist) { +/* +* Only contexts that are short-lived [that will expire or be +* reset] are allowed to survive past termination. We require +* hangcheck to ensure that the persistent requests are healthy. +*/ +if (!i915->params.enable_hangcheck) +return -EINVAL ; + + pc->user_flags |=BIT (UCONTEXT_PERSISTENCE ); + }else { +/* To cancel a context we use "preempt-to-idle" */ +if (!(i915->caps.scheduler &I915_SCHEDULER_CAP_PREEMPTION )) +return -ENODEV ; + +/* +* If the cancel fails, we then need to reset, cleanly! +* +* If the per-engine reset fails, all hope is lost! We resort +* to a full GPU reset in that unlikely case, but realistically +* if the engine could not reset, the full reset does not fare +* much better. The damage has been done. +* +* However, if we cannot reset an engine by itself, we cannot +* cleanup a hanging persistent context without causing +* colateral damage, and we should not pretend we can by +* exposing the interface. +*/ +if (!intel_has_reset_engine (&i915->gt)) +return -ENODEV ; + + pc->user_flags &= ~BIT (UCONTEXT_PERSISTENCE ); + } + +return 0 ; +} +static struct i915_gem_proto_context *proto_context_create (struct drm_i915_private *i915,unsigned int flags) {
@@ -207,6 +255,8 @@ proto_context_create(struct drm_i915_private *i915, unsigned int flags)if (!pc)return ERR_PTR (-ENOMEM ); + pc->num_user_engines =-1 ; + pc->user_engines =NULL ; pc->user_flags =BIT (UCONTEXT_BANNABLE ) |BIT (UCONTEXT_RECOVERABLE );if (i915->params.enable_hangcheck)
@@ -228,6 +278,430 @@ proto_context_create(struct drm_i915_private *i915, unsigned int flags)return err; } +static int set_proto_ctx_vm (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +const struct drm_i915_gem_context_param *args) +{ +struct drm_i915_private *i915 = fpriv->dev_priv; +struct i915_address_space *vm; + +if (args->size) +return -EINVAL ; + +if (!HAS_FULL_PPGTT (i915)) +return -ENODEV ; + +if (upper_32_bits (args->value)) +return -ENOENT ; + + vm =i915_gem_vm_lookup (fpriv, args->value); +if (!vm) +return -ENOENT ; + +if (pc->vm) +i915_vm_put (pc->vm); + pc->vm = vm; + +return 0 ; +} + +struct set_proto_ctx_engines { +struct drm_i915_private *i915; +unsigned num_engines; +struct i915_gem_proto_engine *engines; +}; + +static int +set_proto_ctx_engines_balance (struct i915_user_extension __user *base, +void *data) +{ +struct i915_context_engines_load_balance __user *ext = +container_of_user (base,typeof (*ext), base); +const struct set_proto_ctx_engines *set = data; +struct drm_i915_private *i915 = set->i915; +struct intel_engine_cs **siblings; +u16 num_siblings, idx; +unsigned int n; +int err; + +if (!HAS_EXECLISTS (i915)) +return -ENODEV ; + +if (intel_uc_uses_guc_submission (&i915->gt.uc)) +return -ENODEV ;/* not implement yet */ + +if (get_user (idx, &ext->engine_index)) +return -EFAULT ; + +if (idx >= set->num_engines) { +drm_dbg (&i915->drm,"Invalid placement value, %d >= %d\n" , + idx, set->num_engines); +return -EINVAL ; + } + + idx =array_index_nospec (idx, set->num_engines); +if (set->engines[idx].type !=I915_GEM_ENGINE_TYPE_INVALID ) { +drm_dbg (&i915->drm, +"Invalid placement[%d], already occupied\n" , idx); +return -EEXIST ; + } + +if (get_user (num_siblings, &ext->num_siblings)) +return -EFAULT ; + + err =check_user_mbz (&ext->flags); +if (err) +return err; + + err =check_user_mbz (&ext->mbz64); +if (err) +return err; + +if (num_siblings ==0 ) +return 0 ; + + siblings =kmalloc_array (num_siblings,sizeof (*siblings),GFP_KERNEL ); +if (!siblings) +return -ENOMEM ; + +for (n =0 ; n < num_siblings; n++) { +struct i915_engine_class_instance ci; + +if (copy_from_user (&ci, &ext->engines[n],sizeof (ci))) { + err = -EFAULT ; + goto err_siblings; + } + + siblings[n] =intel_engine_lookup_user (i915, + ci.engine_class, + ci.engine_instance); +if (!siblings[n]) { +drm_dbg (&i915->drm, +"Invalid sibling[%d]: { class:%d, inst:%d }\n" , + n, ci.engine_class, ci.engine_instance); + err = -EINVAL ; + goto err_siblings; + } + } + +if (num_siblings ==1 ) { + set->engines[idx].type =I915_GEM_ENGINE_TYPE_PHYSICAL ; + set->engines[idx].engine = siblings[0 ]; +kfree (siblings); + }else { + set->engines[idx].type =I915_GEM_ENGINE_TYPE_BALANCED ; + set->engines[idx].num_siblings = num_siblings; + set->engines[idx].siblings = siblings; + } + +return 0 ; + +err_siblings: +kfree (siblings); + +return err; +} + +static int +set_proto_ctx_engines_bond (struct i915_user_extension __user *base,void *data) +{ +struct i915_context_engines_bond __user *ext = +container_of_user (base,typeof (*ext), base); +const struct set_proto_ctx_engines *set = data; +struct drm_i915_private *i915 = set->i915; +struct i915_engine_class_instance ci; +struct intel_engine_cs *master; +u16 idx, num_bonds; +int err, n; + +if (get_user (idx, &ext->virtual_index)) +return -EFAULT ; + +if (idx >= set->num_engines) { +drm_dbg (&i915->drm, +"Invalid index for virtual engine: %d >= %d\n" , + idx, set->num_engines); +return -EINVAL ; + } + + idx =array_index_nospec (idx, set->num_engines); +if (set->engines[idx].type ==I915_GEM_ENGINE_TYPE_INVALID ) { +drm_dbg (&i915->drm,"Invalid engine at %d\n" , idx); +return -EINVAL ; + } + +if (set->engines[idx].type !=I915_GEM_ENGINE_TYPE_PHYSICAL ) { +drm_dbg (&i915->drm, +"Bonding with virtual engines not allowed\n" ); +return -EINVAL ; + } + + err =check_user_mbz (&ext->flags); +if (err) +return err; + +for (n =0 ; n <ARRAY_SIZE (ext->mbz64); n++) { + err =check_user_mbz (&ext->mbz64[n]); +if (err) +return err; + } + +if (copy_from_user (&ci, &ext->master,sizeof (ci))) +return -EFAULT ; + + master =intel_engine_lookup_user (i915, + ci.engine_class, + ci.engine_instance); +if (!master) { +drm_dbg (&i915->drm, +"Unrecognised master engine: { class:%u, instance:%u }\n" , + ci.engine_class, ci.engine_instance); +return -EINVAL ; + } + +if (get_user (num_bonds, &ext->num_bonds)) +return -EFAULT ; + +for (n =0 ; n < num_bonds; n++) { +struct intel_engine_cs *bond; + +if (copy_from_user (&ci, &ext->engines[n],sizeof (ci))) +return -EFAULT ; + + bond =intel_engine_lookup_user (i915, + ci.engine_class, + ci.engine_instance); +if (!bond) { +drm_dbg (&i915->drm, +"Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n" , + n, ci.engine_class, ci.engine_instance); +return -EINVAL ; + } + } + +return 0 ; +} + +static const i915_user_extension_fn set_proto_ctx_engines_extensions[] = { + [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE ] = set_proto_ctx_engines_balance, + [I915_CONTEXT_ENGINES_EXT_BOND ] = set_proto_ctx_engines_bond, +}; + +static int set_proto_ctx_engines (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +const struct drm_i915_gem_context_param *args) +{ +struct drm_i915_private *i915 = fpriv->dev_priv; +struct set_proto_ctx_engines set = { .i915 = i915 }; +struct i915_context_param_engines __user *user = +u64_to_user_ptr (args->value); +unsigned int n; +u64 extensions; +int err; + +if (pc->num_user_engines >=0 ) { +drm_dbg (&i915->drm,"Cannot set engines twice" ); +return -EINVAL ; + } + +if (args->size <sizeof (*user) || + !IS_ALIGNED (args->size -sizeof (*user),sizeof (*user->engines))) { +drm_dbg (&i915->drm,"Invalid size for engine array: %d\n" , + args->size); +return -EINVAL ; + } + + set.num_engines = (args->size -sizeof (*user)) /sizeof (*user->engines); +/* RING_MASK has no shift so we can use it directly here */ +if (set.num_engines >I915_EXEC_RING_MASK +1 ) +return -EINVAL ; + + set.engines =kmalloc_array (set.num_engines,sizeof (*set.engines),GFP_KERNEL ); +if (!set.engines) +return -ENOMEM ; + +for (n =0 ; n < set.num_engines; n++) { +struct i915_engine_class_instance ci; +struct intel_engine_cs *engine; + +if (copy_from_user (&ci, &user->engines[n],sizeof (ci))) { +kfree (set.engines); +return -EFAULT ; + } + +memset (&set.engines[n],0 ,sizeof (set.engines[n])); + +if (ci.engine_class == (u16 )I915_ENGINE_CLASS_INVALID && + ci.engine_instance == (u16 )I915_ENGINE_CLASS_INVALID_NONE ) +continue ; + + engine =intel_engine_lookup_user (i915, + ci.engine_class, + ci.engine_instance); +if (!engine) { +drm_dbg (&i915->drm, +"Invalid engine[%d]: { class:%d, instance:%d }\n" , + n, ci.engine_class, ci.engine_instance); +kfree (set.engines); +return -ENOENT ; + } + + set.engines[n].type =I915_GEM_ENGINE_TYPE_PHYSICAL ; + set.engines[n].engine = engine; + } + + err = -EFAULT ; +if (!get_user (extensions, &user->extensions)) + err =i915_user_extensions (u64_to_user_ptr (extensions), + set_proto_ctx_engines_extensions, +ARRAY_SIZE (set_proto_ctx_engines_extensions), + &set); +if (err) { +kfree (set.engines); +return err; + } + + pc->num_user_engines = set.num_engines; + pc->user_engines = set.engines; + +return 0 ; +} + +static int set_proto_ctx_sseu (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +struct drm_i915_gem_context_param *args) +{ +struct drm_i915_private *i915 = fpriv->dev_priv; +struct drm_i915_gem_context_param_sseu user_sseu; +struct intel_sseu *sseu; +int ret; + +if (args->size <sizeof (user_sseu)) +return -EINVAL ; + +if (GRAPHICS_VER (i915) !=11 ) +return -ENODEV ; + +if (copy_from_user (&user_sseu,u64_to_user_ptr (args->value), +sizeof (user_sseu))) +return -EFAULT ; + +if (user_sseu.rsvd) +return -EINVAL ; + +if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX )) +return -EINVAL ; + +if (!!(user_sseu.flags &I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX ) != (pc->num_user_engines >=0 )) +return -EINVAL ; + +if (pc->num_user_engines >=0 ) { +int idx = user_sseu.engine.engine_instance; +struct i915_gem_proto_engine *pe; + +if (idx >= pc->num_user_engines) +return -EINVAL ; + + pe = &pc->user_engines[idx]; + +/* Only render engine supports RPCS configuration. */ +if (pe->engine->class !=RENDER_CLASS ) +return -EINVAL ; + + sseu = &pe->sseu; + }else { +/* Only render engine supports RPCS configuration. */ +if (user_sseu.engine.engine_class !=I915_ENGINE_CLASS_RENDER ) +return -EINVAL ; + +/* There is only one render engine */ +if (user_sseu.engine.engine_instance !=0 ) +return -EINVAL ; + + sseu = &pc->legacy_rcs_sseu; + } + + ret =i915_gem_user_to_context_sseu (&i915->gt, &user_sseu, sseu); +if (ret) +return ret; + + args->size =sizeof (user_sseu); + +return 0 ; +} + +static int set_proto_ctx_param (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +struct drm_i915_gem_context_param *args) +{ +int ret =0 ; + +switch (args->param) { +case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE : +if (args->size) + ret = -EINVAL ; +else if (args->value) + pc->user_flags |=BIT (UCONTEXT_NO_ERROR_CAPTURE ); +else + pc->user_flags &= ~BIT (UCONTEXT_NO_ERROR_CAPTURE ); +break ; + +case I915_CONTEXT_PARAM_BANNABLE : +if (args->size) + ret = -EINVAL ; +else if (!capable (CAP_SYS_ADMIN ) && !args->value) + ret = -EPERM ; +else if (args->value) + pc->user_flags |=BIT (UCONTEXT_BANNABLE ); +else + pc->user_flags &= ~BIT (UCONTEXT_BANNABLE ); +break ; + +case I915_CONTEXT_PARAM_RECOVERABLE : +if (args->size) + ret = -EINVAL ; +else if (args->value) + pc->user_flags |=BIT (UCONTEXT_RECOVERABLE ); +else + pc->user_flags &= ~BIT (UCONTEXT_RECOVERABLE ); +break ; + +case I915_CONTEXT_PARAM_PRIORITY : + ret =validate_priority (fpriv->dev_priv, args); +if (!ret) + pc->sched.priority = args->value; +break ; + +case I915_CONTEXT_PARAM_SSEU : + ret =set_proto_ctx_sseu (fpriv, pc, args); +break ; + +case I915_CONTEXT_PARAM_VM : + ret =set_proto_ctx_vm (fpriv, pc, args); +break ; + +case I915_CONTEXT_PARAM_ENGINES : + ret =set_proto_ctx_engines (fpriv, pc, args); +break ; + +case I915_CONTEXT_PARAM_PERSISTENCE : +if (args->size) + ret = -EINVAL ; + ret =proto_context_set_persistence (fpriv->dev_priv, pc, + args->value); +break ; + +case I915_CONTEXT_PARAM_NO_ZEROMAP : +case I915_CONTEXT_PARAM_BAN_PERIOD : +case I915_CONTEXT_PARAM_RINGSIZE : +default : + ret = -EINVAL ; +break ; + } + +return ret; +} +static struct i915_address_space *context_get_vm_rcu (struct i915_gem_context *ctx) {
@@ -421,6 +895,56 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx,return err; } +static struct i915_gem_engines *user_engines (struct i915_gem_context *ctx, +unsigned int num_engines, +struct i915_gem_proto_engine *pe) +{ +struct i915_gem_engines *e, *err; +unsigned int n; + + e =alloc_engines (num_engines); +for (n =0 ; n < num_engines; n++) { +struct intel_context *ce; +int ret; + +switch (pe[n].type) { +case I915_GEM_ENGINE_TYPE_PHYSICAL : + ce =intel_context_create (pe[n].engine); +break ; + +case I915_GEM_ENGINE_TYPE_BALANCED : + ce =intel_execlists_create_virtual (pe[n].siblings, + pe[n].num_siblings); +break ; + +case I915_GEM_ENGINE_TYPE_INVALID : +default : +GEM_WARN_ON (pe[n].type !=I915_GEM_ENGINE_TYPE_INVALID ); +continue ; + } + +if (IS_ERR (ce)) { + err =ERR_CAST (ce); + goto free_engines; + } + + e->engines[n] = ce; + + ret =intel_context_set_gem (ce, ctx, pe->sseu); +if (ret) { + err =ERR_PTR (ret); + goto free_engines; + } + } + e->num_engines = num_engines; + +return e; + +free_engines: +free_engines (e); +return err; +} +void i915_gem_context_release (struct kref *ref) {struct i915_gem_context *ctx =container_of (ref,typeof (*ctx), ref);
@@ -725,7 +1249,6 @@ __create_context(struct drm_i915_private *i915, {struct i915_gem_context *ctx;struct i915_gem_engines *e; -struct intel_sseu null_sseu = {};int err;int i;
@@ -743,7 +1266,7 @@ __create_context(struct drm_i915_private *i915,INIT_LIST_HEAD (&ctx->stale.engines);mutex_init (&ctx->engines_mutex); - e =default_engines (ctx, null_sseu); + e =default_engines (ctx, pc->legacy_rcs_sseu);if (IS_ERR (e)) { err =PTR_ERR (e); goto err_free;
@@ -881,6 +1404,24 @@ i915_gem_create_context(struct drm_i915_private *i915,i915_vm_put (&ppgtt->vm); } +if (pc->num_user_engines >=0 ) { +struct i915_gem_engines *engines; + + engines =user_engines (ctx, pc->num_user_engines, + pc->user_engines); +if (IS_ERR (engines)) { +context_close (ctx); +return ERR_CAST (engines); + } + +mutex_lock (&ctx->engines_mutex); +i915_gem_context_set_user_engines (ctx); + engines =rcu_replace_pointer (ctx->engines, engines,1 ); +mutex_unlock (&ctx->engines_mutex); + +free_engines (engines); + } +if (pc->single_timeline) { ret = drm_syncobj_create(&ctx->syncobj, DRM_SYNCOBJ_CREATE_SIGNALED,
@@ -1915,7 +2456,7 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv, }struct create_ext { -struct i915_gem_context *ctx; +struct i915_gem_proto_context *pc;struct drm_i915_file_private *fpriv; };
@@ -1930,7 +2471,7 @@ static int create_setparam(struct i915_user_extension __user *ext, void *data)if (local.param.ctx_id)return -EINVAL ; -return ctx_setparam (arg->fpriv, arg->ctx, &local.param); +return set_proto_ctx_param (arg->fpriv, arg->pc, &local.param); }static int invalid_ext (struct i915_user_extension __user *ext,void *data)
@@ -1953,7 +2494,7 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, {struct drm_i915_private *i915 =to_i915 (dev);struct drm_i915_gem_context_create_ext *args = data; -struct i915_gem_proto_context *pc; +struct i915_gem_context *ctx;struct create_ext ext_data;int ret;u32 id;
@@ -1976,25 +2517,27 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,return -EIO ; } - pc =proto_context_create (i915, args->flags); -if (IS_ERR (pc)) -return PTR_ERR (pc); - - ext_data.ctx =i915_gem_create_context (i915, pc); -proto_context_close (pc); -if (IS_ERR (ext_data.ctx)) -return PTR_ERR (ext_data.ctx); + ext_data.pc =proto_context_create (i915, args->flags); +if (IS_ERR (ext_data.pc)) +return PTR_ERR (ext_data.pc);if (args->flags &I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS ) { ret =i915_user_extensions (u64_to_user_ptr (args->extensions), create_extensions,ARRAY_SIZE (create_extensions), &ext_data); -if (ret) - goto err_ctx; +if (ret) { +proto_context_close (ext_data.pc); +return ret; + } } - ret =gem_context_register (ext_data.ctx, ext_data.fpriv, &id); + ctx =i915_gem_create_context (i915, ext_data.pc); +proto_context_close (ext_data.pc); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); + + ret =gem_context_register (ctx, ext_data.fpriv, &id);if (ret <0 ) goto err_ctx;
@@ -2004,7 +2547,7 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,return 0 ; err_ctx: -context_close (ext_data.ctx); +context_close (ctx);return ret; }
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +58 -0
70 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH 22/30 drm/i915/gem: Return an error ptr from context_lookup
In reply to Jason EkstrandWe're about to start doing lazy context creation which means contexts get created in i915_gem_context_lookup and we may start having more errors than -ENOENT.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 12 ++++++------ drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 4 ++-- drivers/gpu/drm/i915/i915_drv.h | 2 +- drivers/gpu/drm/i915/i915_perf.c | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +6 -6
@@ -2636,8 +2636,8 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,int ret =0 ; ctx =i915_gem_context_lookup (file_priv, args->ctx_id); -if (!ctx) -return -ENOENT ; +if (IS_ERR (ctx)) +return PTR_ERR (ctx);switch (args->param) {case I915_CONTEXT_PARAM_GTT_SIZE :
@@ -2705,8 +2705,8 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,int ret; ctx =i915_gem_context_lookup (file_priv, args->ctx_id); -if (!ctx) -return -ENOENT ; +if (IS_ERR (ctx)) +return PTR_ERR (ctx); ret =ctx_setparam (file_priv, ctx, args);
@@ -2725,8 +2725,8 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,return -EINVAL ; ctx =i915_gem_context_lookup (file->driver_priv, args->ctx_id); -if (!ctx) -return -ENOENT ; +if (IS_ERR (ctx)) +return PTR_ERR (ctx);/* * We opt for unserialised reads here. This may result in tearing
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +2 -2
@@ -750,8 +750,8 @@ static int eb_select_context(struct i915_execbuffer *eb)struct i915_gem_context *ctx; ctx =i915_gem_context_lookup (eb->file->driver_priv, eb->args->rsvd1); -if (unlikely (!ctx)) -return -ENOENT ; +if (unlikely (IS_ERR (ctx))) +return PTR_ERR (ctx); eb->gem_context = ctx;if (rcu_access_pointer (ctx->vm))
drivers/gpu/drm/i915/i915_drv.h +1 -1
@@ -1864,7 +1864,7 @@ i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id) ctx =NULL ;rcu_read_unlock (); -return ctx; +return ctx ? ctx :ERR_PTR (-ENOENT ); } static inline struct i915_address_space *
drivers/gpu/drm/i915/i915_perf.c +2 -2
@@ -3414,10 +3414,10 @@ i915_perf_open_ioctl_locked(struct i915_perf *perf,struct drm_i915_file_private *file_priv = file->driver_priv; specific_ctx =i915_gem_context_lookup (file_priv, ctx_handle); -if (!specific_ctx) { +if (IS_ERR (specific_ctx)) {DRM_DEBUG ("Failed to look up context with ID %u for opening perf stream\n" , ctx_handle); - ret = -ENOENT ; + ret =PTR_ERR (specific_ctx); goto err; } }
Signature · 1 line
2.31.1
PATCH 23/30 drm/i915/gt: Drop i915_address_space::file (v2)
In reply to Jason EkstrandThere's a big comment saying how useful it is but no one is using this for anything anymore.
It was added in 2bfa996e031b ("drm/i915: Store owning file on the i915_address_space") and used for debugfs at the time as well as telling the difference between the global GTT and a PPGTT. In f6e8aa387171 ("drm/i915: Report the number of closed vma held by each context in debugfs") we removed one use of it by switching to a context walk and comparing with the VM in the context. Finally, VM stats for debugfs were entirely nuked in db80a1294c23 ("drm/i915/gem: Remove per-client stats from debugfs/i915_gem_objects")
v2 (Daniel Vetter):
- Delete a struct drm_i915_file_private pre-declaration
- Add a comment to the commit message about history
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 9 --------- drivers/gpu/drm/i915/gt/intel_gtt.h | 11 ----------- drivers/gpu/drm/i915/selftests/mock_gtt.c | 1 - 3 files changed, 21 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +0 -9
@@ -1453,17 +1453,10 @@ static int gem_context_register(struct i915_gem_context *ctx, u32 *id) {struct drm_i915_private *i915 = ctx->i915; -struct i915_address_space *vm;int ret; ctx->file_priv = fpriv; -mutex_lock (&ctx->mutex); - vm =i915_gem_context_vm (ctx); -if (vm) -WRITE_ONCE (vm->file, fpriv);/* XXX */ -mutex_unlock (&ctx->mutex); - ctx->pid =get_task_pid (current,PIDTYPE_PID );snprintf (ctx->name,sizeof (ctx->name),"%s[%d]" , current->comm,pid_nr (ctx->pid));
@@ -1562,8 +1555,6 @@ int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,if (IS_ERR (ppgtt))return PTR_ERR (ppgtt); - ppgtt->vm.file = file_priv; -if (args->extensions) { err = i915_user_extensions(u64_to_user_ptr(args->extensions), NULL, 0,
drivers/gpu/drm/i915/gt/intel_gtt.h +0 -11
@@ -140,7 +140,6 @@ typedef u64 gen8_pte_t;enum i915_cache_level ; -struct drm_i915_file_private ;struct drm_i915_gem_object ;struct i915_fence_reg ;struct i915_vma ;
@@ -220,16 +219,6 @@ struct i915_address_space {struct intel_gt *gt;struct drm_i915_private *i915;struct device *dma; -/* -* Every address space belongs to a struct file - except for the global -* GTT that is owned by the driver (and so @file is set to NULL). In -* principle, no information should leak from one context to another -* (or between files/processes etc) unless explicitly shared by the -* owner. Tracking the owner is important in order to free up per-file -* objects along with the file, to aide resource tracking, and to -* assign blame. -*/ -struct drm_i915_file_private *file;u64 total;/* size addr space maps (ex. 2GB for ggtt) */ u64 reserved;/* size addr space reserved */
drivers/gpu/drm/i915/selftests/mock_gtt.c +0 -1
@@ -73,7 +73,6 @@ struct i915_ppgtt *mock_ppgtt(struct drm_i915_private *i915, const char *name) ppgtt->vm.gt = &i915->gt; ppgtt->vm.i915 = i915; ppgtt->vm.total =round_down (U64_MAX ,PAGE_SIZE ); - ppgtt->vm.file =ERR_PTR (-ENODEV ); ppgtt->vm.dma = i915->drm.dev;i915_address_space_init (&ppgtt->vm,VM_CLASS_PPGTT );
Signature · 1 line
2.31.1
PATCH 24/30 drm/i915/gem: Delay context creation (v3)
In reply to Jason EkstrandThe current context uAPI allows for two methods of setting context
parameters: SET_CONTEXT_PARAM and CONTEXT_CREATE_EXT_SETPARAM. The former is allowed to be called at any time while the later happens as part of GEM_CONTEXT_CREATE. Currently, everything settable via one is settable via the other. While some params are fairly simple and setting them on a live context is harmless such as the context priority, others are far trickier such as the VM or the set of engines. In order to swap out the VM, for instance, we have to delay until all current in-flight work is complete, swap in the new VM, and then continue. This leads to a plethora of potential race conditions we'd really rather avoid.
In previous patches, we added a i915_gem_proto_context struct which is capable of storing and tracking all such create parameters. This commit delays the creation of the actual context until after the client is done configuring it with SET_CONTEXT_PARAM. From the perspective of the client, it has the same u32 context ID the whole time. From the perspective of i915, however, it's an i915_gem_proto_context right up until the point where we attempt to do something which the proto-context can't handle. Then the real context gets created.
This is accomplished via a little xarray dance. When GEM_CONTEXT_CREATE is called, we create a proto-context, reserve a slot in context_xa but leave it NULL, the proto-context in the corresponding slot in proto_context_xa. Then, whenever we go to look up a context, we first check context_xa. If it's there, we return the i915_gem_context and we're done. If it's not, we look in proto_context_xa and, if we find it there, we create the actual context and kill the proto-context.
In order for this dance to work properly, everything which ever touches a proto-context is guarded by drm_i915_file_private::proto_context_lock, including context creation. Yes, this means context creation now takes a giant global lock but it can't really be helped and that should never be on any driver's fast-path anyway.
v2 (Daniel Vetter):
- Commit message grammatical fixes.
- Use WARN_ON instead of GEM_BUG_ON
- Rename lazy_create_context_locked to finalize_create_context_locked
- Rework the control-flow logic in the setparam ioctl
- Better documentation all around
v3 (kernel test robot):
- Make finalize_create_context_locked static
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 203 ++++++++++++++---- drivers/gpu/drm/i915/gem/i915_gem_context.h | 3 + .../gpu/drm/i915/gem/i915_gem_context_types.h | 54 +++++ .../gpu/drm/i915/gem/selftests/mock_context.c | 5 +- drivers/gpu/drm/i915/i915_drv.h | 76 +++++-- 5 files changed, 283 insertions(+), 58 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +160 -43
@@ -278,6 +278,42 @@ proto_context_create(struct drm_i915_private *i915, unsigned int flags)return err; } +static int proto_context_register_locked (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +u32 *id) +{ +int ret; +void *old; + +lockdep_assert_held (&fpriv->proto_context_lock); + + ret =xa_alloc (&fpriv->context_xa, id,NULL , xa_limit_32b,GFP_KERNEL ); +if (ret) +return ret; + + old =xa_store (&fpriv->proto_context_xa, *id, pc,GFP_KERNEL ); +if (xa_is_err (old)) { +xa_erase (&fpriv->context_xa, *id); +return xa_err (old); + } +WARN_ON (old); + +return 0 ; +} + +static int proto_context_register (struct drm_i915_file_private *fpriv, +struct i915_gem_proto_context *pc, +u32 *id) +{ +int ret; + +mutex_lock (&fpriv->proto_context_lock); + ret =proto_context_register_locked (fpriv, pc, id); +mutex_unlock (&fpriv->proto_context_lock); + +return ret; +} +static int set_proto_ctx_vm (struct drm_i915_file_private *fpriv,struct i915_gem_proto_context *pc,const struct drm_i915_gem_context_param *args)
@@ -1448,12 +1484,12 @@ void i915_gem_init__contexts(struct drm_i915_private *i915)init_contexts (&i915->gem.contexts); } -static int gem_context_register (struct i915_gem_context *ctx, -struct drm_i915_file_private *fpriv, -u32 *id) +static void gem_context_register (struct i915_gem_context *ctx, +struct drm_i915_file_private *fpriv, +u32 id) {struct drm_i915_private *i915 = ctx->i915; -int ret; +void *old; ctx->file_priv = fpriv;
@@ -1462,19 +1498,12 @@ static int gem_context_register(struct i915_gem_context *ctx, current->comm, pid_nr(ctx->pid));/* And finally expose ourselves to userspace via the idr */ - ret =xa_alloc (&fpriv->context_xa, id, ctx, xa_limit_32b,GFP_KERNEL ); -if (ret) - goto err_pid; + old =xa_store (&fpriv->context_xa, id, ctx,GFP_KERNEL ); +WARN_ON (old);spin_lock (&i915->gem.contexts.lock);list_add_tail (&ctx->link, &i915->gem.contexts.list);spin_unlock (&i915->gem.contexts.lock); - -return 0 ; - -err_pid: -put_pid (fetch_and_zero (&ctx->pid)); -return ret; } int i915_gem_context_open(struct drm_i915_private *i915,
@@ -1484,9 +1513,12 @@ int i915_gem_context_open(struct drm_i915_private *i915,struct i915_gem_proto_context *pc;struct i915_gem_context *ctx;int err; -u32 id; -xa_init_flags (&file_priv->context_xa,XA_FLAGS_ALLOC ); +mutex_init (&file_priv->proto_context_lock); +xa_init_flags (&file_priv->proto_context_xa,XA_FLAGS_ALLOC ); + +/* 0 reserved for the default context */ +xa_init_flags (&file_priv->context_xa,XA_FLAGS_ALLOC1 );/* 0 reserved for invalid/unassigned ppgtt */ xa_init_flags (&file_priv->vm_xa,XA_FLAGS_ALLOC1 );
@@ -1504,28 +1536,31 @@ int i915_gem_context_open(struct drm_i915_private *i915, goto err; } - err =gem_context_register (ctx, file_priv, &id); -if (err <0 ) - goto err_ctx; +gem_context_register (ctx, file_priv,0 ); -GEM_BUG_ON (id);return 0 ; -err_ctx: -context_close (ctx); err:xa_destroy (&file_priv->vm_xa);xa_destroy (&file_priv->context_xa); +xa_destroy (&file_priv->proto_context_xa); +mutex_destroy (&file_priv->proto_context_lock);return err; }void i915_gem_context_close (struct drm_file *file) {struct drm_i915_file_private *file_priv = file->driver_priv; +struct i915_gem_proto_context *pc;struct i915_address_space *vm;struct i915_gem_context *ctx;unsigned long idx; +xa_for_each (&file_priv->proto_context_xa, idx, pc) +proto_context_close (pc); +xa_destroy (&file_priv->proto_context_xa); +mutex_destroy (&file_priv->proto_context_lock); +xa_for_each (&file_priv->context_xa, idx, ctx)context_close (ctx);xa_destroy (&file_priv->context_xa);
@@ -2480,12 +2515,73 @@ static bool client_is_banned(struct drm_i915_file_private *file_priv)return atomic_read (&file_priv->ban_score) >=I915_CLIENT_SCORE_BANNED ; } +static inline struct i915_gem_context * +__context_lookup (struct drm_i915_file_private *file_priv,u32 id) +{ +struct i915_gem_context *ctx; + +rcu_read_lock (); + ctx =xa_load (&file_priv->context_xa, id); +if (ctx && !kref_get_unless_zero (&ctx->ref)) + ctx =NULL ; +rcu_read_unlock (); + +return ctx; +} + +static struct i915_gem_context * +finalize_create_context_locked (struct drm_i915_file_private *file_priv, +struct i915_gem_proto_context *pc,u32 id) +{ +struct i915_gem_context *ctx; +void *old; + +lockdep_assert_held (&file_priv->proto_context_lock); + + ctx =i915_gem_create_context (file_priv->dev_priv, pc); +if (IS_ERR (ctx)) +return ctx; + +gem_context_register (ctx, file_priv, id); + + old =xa_erase (&file_priv->proto_context_xa, id); +GEM_BUG_ON (old != pc); +proto_context_close (pc); + +/* One for the xarray and one for the caller */ +return i915_gem_context_get (ctx); +} + +struct i915_gem_context * +i915_gem_context_lookup (struct drm_i915_file_private *file_priv,u32 id) +{ +struct i915_gem_proto_context *pc; +struct i915_gem_context *ctx; + + ctx =__context_lookup (file_priv, id); +if (ctx) +return ctx; + +mutex_lock (&file_priv->proto_context_lock); +/* Try one more time under the lock */ + ctx =__context_lookup (file_priv, id); +if (!ctx) { + pc =xa_load (&file_priv->proto_context_xa, id); +if (!pc) + ctx =ERR_PTR (-ENOENT ); +else + ctx =finalize_create_context_locked (file_priv, pc, id); + } +mutex_unlock (&file_priv->proto_context_lock); + +return ctx; +} +int i915_gem_context_create_ioctl (struct drm_device *dev,void *data,struct drm_file *file) {struct drm_i915_private *i915 =to_i915 (dev);struct drm_i915_gem_context_create_ext *args = data; -struct i915_gem_context *ctx;struct create_ext ext_data;int ret;u32 id;
@@ -2517,28 +2613,21 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, create_extensions,ARRAY_SIZE (create_extensions), &ext_data); -if (ret) { -proto_context_close (ext_data.pc); -return ret; - } +if (ret) + goto err_pc; } - ctx =i915_gem_create_context (i915, ext_data.pc); -proto_context_close (ext_data.pc); -if (IS_ERR (ctx)) -return PTR_ERR (ctx); - - ret =gem_context_register (ctx, ext_data.fpriv, &id); + ret =proto_context_register (ext_data.fpriv, ext_data.pc, &id);if (ret <0 ) - goto err_ctx; + goto err_pc; args->ctx_id = id;drm_dbg (&i915->drm,"HW context %d created\n" , args->ctx_id);return 0 ; -err_ctx: -context_close (ctx); +err_pc: +proto_context_close (ext_data.pc);return ret; }
@@ -2547,6 +2636,7 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data, {struct drm_i915_gem_context_destroy *args = data;struct drm_i915_file_private *file_priv = file->driver_priv; +struct i915_gem_proto_context *pc;struct i915_gem_context *ctx;if (args->pad !=0 )
@@ -2555,11 +2645,24 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,if (!args->ctx_id)return -ENOENT ; +/* We need to hold the proto-context lock here to prevent races +* with finalize_create_context_locked(). +*/ +mutex_lock (&file_priv->proto_context_lock); ctx =xa_erase (&file_priv->context_xa, args->ctx_id); -if (!ctx) + pc =xa_erase (&file_priv->proto_context_xa, args->ctx_id); +mutex_unlock (&file_priv->proto_context_lock); + +if (!ctx && !pc)return -ENOENT ; +GEM_WARN_ON (ctx && pc); + +if (pc) +proto_context_close (pc); + +if (ctx) +context_close (ctx); -context_close (ctx);return 0 ; }
@@ -2692,16 +2795,30 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data, {struct drm_i915_file_private *file_priv = file->driver_priv;struct drm_i915_gem_context_param *args = data; +struct i915_gem_proto_context *pc;struct i915_gem_context *ctx; -int ret; +int ret =0 ; - ctx =i915_gem_context_lookup (file_priv, args->ctx_id); -if (IS_ERR (ctx)) -return PTR_ERR (ctx); +mutex_lock (&file_priv->proto_context_lock); + ctx =__context_lookup (file_priv, args->ctx_id); +if (!ctx) { +/* FIXME: We should consider disallowing SET_CONTEXT_PARAM +* for most things on future platforms. Clients should be +* using CONTEXT_CREATE_EXT_PARAM instead. +*/ + pc =xa_load (&file_priv->proto_context_xa, args->ctx_id); +if (pc) + ret =set_proto_ctx_param (file_priv, pc, args); +else + ret = -ENOENT ; + } +mutex_unlock (&file_priv->proto_context_lock); - ret =ctx_setparam (file_priv, ctx, args); +if (ctx) { + ret =ctx_setparam (file_priv, ctx, args); +i915_gem_context_put (ctx); + } -i915_gem_context_put (ctx);return ret; }
drivers/gpu/drm/i915/gem/i915_gem_context.h +3 -0
@@ -133,6 +133,9 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,int i915_gem_context_reset_stats_ioctl (struct drm_device *dev,void *data,struct drm_file *file); +struct i915_gem_context * +i915_gem_context_lookup (struct drm_i915_file_private *file_priv,u32 id); +static inline struct i915_gem_context *i915_gem_context_get (struct i915_gem_context *ctx) {
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +54 -0
60 lines not on this page · raw message
drivers/gpu/drm/i915/gem/selftests/mock_context.c +4 -1
@@ -81,6 +81,7 @@ void mock_init_contexts(struct drm_i915_private *i915)struct i915_gem_context *live_context (struct drm_i915_private *i915,struct file *file) { +struct drm_i915_file_private *fpriv =to_drm_file (file)->driver_priv;struct i915_gem_proto_context *pc;struct i915_gem_context *ctx;int err;
@@ -97,10 +98,12 @@ live_context(struct drm_i915_private *i915, struct file *file)i915_gem_context_set_no_error_capture (ctx); - err =gem_context_register (ctx,to_drm_file (file)->driver_priv, &id); + err =xa_alloc (&fpriv->context_xa, &id,NULL , xa_limit_32b,GFP_KERNEL );if (err <0 ) goto err_ctx; +gem_context_register (ctx, fpriv, id); +return ctx; err_ctx:
drivers/gpu/drm/i915/i915_drv.h +62 -14
88 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH 25/30 drm/i915/gem: Don't allow changing the VM on running contexts (v4)
In reply to Jason EkstrandWhen the APIs were added to manage VMs more directly from userspace, the questionable choice was made to allow changing out the VM on a context at any time. This is horribly racy and there's absolutely no reason why any userspace would want to do this outside of testing that exact race. By removing support for CONTEXT_PARAM_VM from ctx_setparam, we make it impossible to change out the VM after the context has been fully created. This lets us delete a bunch of deferred task code as well as a duplicated (and slightly different) copy of the code which programs the PPGTT registers.
v2 (Jason Ekstrand):
- Expand the commit message
v3 (Daniel Vetter):
- Don't drop the __rcu on the vm pointer
v4 (Jason Ekstrand):
- Make it more obvious that I915_CONTEXT_PARAM_VM returns -EINVAL
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 263 +----------------- .../drm/i915/gem/selftests/i915_gem_context.c | 119 -------- .../drm/i915/selftests/i915_mock_selftests.h | 1 - 3 files changed, 1 insertion(+), 382 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -262
@@ -1633,120 +1633,6 @@ int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,return 0 ; } -struct context_barrier_task { -struct i915_active base; -void (*task)(void *data); -void *data; -}; - -static void cb_retire (struct i915_active *base) -{ -struct context_barrier_task *cb =container_of (base,typeof (*cb), base); - -if (cb->task) - cb->task (cb->data); - -i915_active_fini (&cb->base); -kfree (cb); -} - -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) -{ -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 ; - -GEM_BUG_ON (!task); - - cb =kmalloc (sizeof (*cb),GFP_KERNEL ); -if (!cb) -return -ENOMEM ; - -i915_active_init (&cb->base,NULL , cb_retire,0 ); - err =i915_active_acquire (&cb->base); -if (err) { -kfree (cb); -return err; - } - - e =__context_engines_await (ctx,NULL ); -if (!e) { -i915_active_release (&cb->base); -return -ENOENT ; - } - -for_each_gem_engine (ce, e, it) { -struct i915_request *rq; - -if (I915_SELFTEST_ONLY (context_barrier_inject_fault & - ce->engine->mask)) { - err = -ENXIO ; -break ; - } - -if (!(ce->engine->mask & engines)) -continue ; - -if (skip &&skip (ce, data)) -continue ; - -i915_gem_ww_ctx_init (&ww, true); -retry: - err =intel_context_pin_ww (ce, &ww); -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); - goto err_unpin; - } - - err =0 ; -if (emit) - err =emit (rq, data); -if (err ==0 ) - 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 ; - } -i915_sw_fence_complete (&e->fence); - - cb->task = err ?NULL : task;/* caller needs to unwind instead */ - cb->data = data; - -i915_active_release (&cb->base); - -return err; -} -static int get_ppgtt (struct drm_i915_file_private *file_priv,struct i915_gem_context *ctx,struct drm_i915_gem_context_param *args)
@@ -1779,150 +1665,6 @@ static int get_ppgtt(struct drm_i915_file_private *file_priv,return err; } -static void set_ppgtt_barrier (void *data) -{ -struct i915_address_space *old = data; - -if (GRAPHICS_VER (old->i915) <8 ) -gen6_ppgtt_unpin_all (i915_vm_to_ppgtt (old)); - -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), ww); - -return 0 ; -} - -static int emit_ppgtt_update (struct i915_request *rq,void *data) -{ -struct i915_address_space *vm = rq->context->vm; -struct intel_engine_cs *engine = rq->engine; -u32 base = engine->mmio_base; -u32 *cs; -int i; - -if (i915_vm_is_4lvl (vm)) { -struct i915_ppgtt *ppgtt =i915_vm_to_ppgtt (vm); -const dma_addr_t pd_daddr =px_dma (ppgtt->pd); - - cs =intel_ring_begin (rq,6 ); -if (IS_ERR (cs)) -return PTR_ERR (cs); - - *cs++ =MI_LOAD_REGISTER_IMM (2 ); - - *cs++ =i915_mmio_reg_offset (GEN8_RING_PDP_UDW (base,0 )); - *cs++ =upper_32_bits (pd_daddr); - *cs++ =i915_mmio_reg_offset (GEN8_RING_PDP_LDW (base,0 )); - *cs++ =lower_32_bits (pd_daddr); - - *cs++ =MI_NOOP ; -intel_ring_advance (rq, cs); - }else if (HAS_LOGICAL_RING_CONTEXTS (engine->i915)) { -struct i915_ppgtt *ppgtt =i915_vm_to_ppgtt (vm); -int err; - -/* Magic required to prevent forcewake errors! */ - err = engine->emit_flush (rq,EMIT_INVALIDATE ); -if (err) -return err; - - cs =intel_ring_begin (rq,4 *GEN8_3LVL_PDPES +2 ); -if (IS_ERR (cs)) -return PTR_ERR (cs); - - *cs++ =MI_LOAD_REGISTER_IMM (2 *GEN8_3LVL_PDPES ) |MI_LRI_FORCE_POSTED ; -for (i =GEN8_3LVL_PDPES ; i--; ) { -const dma_addr_t pd_daddr =i915_page_dir_dma_addr (ppgtt, i); - - *cs++ =i915_mmio_reg_offset (GEN8_RING_PDP_UDW (base, i)); - *cs++ =upper_32_bits (pd_daddr); - *cs++ =i915_mmio_reg_offset (GEN8_RING_PDP_LDW (base, i)); - *cs++ =lower_32_bits (pd_daddr); - } - *cs++ =MI_NOOP ; -intel_ring_advance (rq, cs); - } - -return 0 ; -} - -static bool skip_ppgtt_update (struct intel_context *ce,void *data) -{ -if (HAS_LOGICAL_RING_CONTEXTS (ce->engine->i915)) -return !ce->state; -else -return !atomic_read (&ce->pin_count); -} - -static int set_ppgtt (struct drm_i915_file_private *file_priv, -struct i915_gem_context *ctx, -struct drm_i915_gem_context_param *args) -{ -struct i915_address_space *vm, *old; -int err; - -if (args->size) -return -EINVAL ; - -if (!rcu_access_pointer (ctx->vm)) -return -ENODEV ; - -if (upper_32_bits (args->value)) -return -ENOENT ; - - vm =i915_gem_vm_lookup (file_priv, args->value); -if (!vm) -return -ENOENT ; - - err =mutex_lock_interruptible (&ctx->mutex); -if (err) - goto out; - -if (i915_gem_context_is_closed (ctx)) { - err = -ENOENT ; - goto unlock; - } - -if (vm ==rcu_access_pointer (ctx->vm)) - goto unlock; - - old =__set_ppgtt (ctx, vm); - -/* Teardown the existing obj:vma cache, it will have to be rebuilt. */ -lut_close (ctx); - -/* -* We need to flush any requests using the current ppgtt before -* we release it as the requests do not hold a reference themselves, -* only indirectly through the context. -*/ - err =context_barrier_task (ctx,ALL_ENGINES , - skip_ppgtt_update, - pin_ppgtt_update, - emit_ppgtt_update, - set_ppgtt_barrier, - old); -if (err) { -i915_vm_close (__set_ppgtt (ctx, old)); -i915_vm_close (old); -lut_close (ctx);/* force a rebuild of the old obj:vma cache */ - } - -unlock: -mutex_unlock (&ctx->mutex); -out: -i915_vm_put (vm); -return err; -} - int i915_gem_user_to_context_sseu(struct intel_gt *gt, const struct drm_i915_gem_context_param_sseu *user,
@@ -2458,10 +2200,6 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv, ret =set_sseu (ctx, args);break ; -case I915_CONTEXT_PARAM_VM : - ret =set_ppgtt (fpriv, ctx, args); -break ; -case I915_CONTEXT_PARAM_ENGINES : ret =set_engines (ctx, args);break ;
@@ -2473,6 +2211,7 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,case I915_CONTEXT_PARAM_NO_ZEROMAP :case I915_CONTEXT_PARAM_BAN_PERIOD :case I915_CONTEXT_PARAM_RINGSIZE : +case I915_CONTEXT_PARAM_VM :default : ret = -EINVAL ;break ;
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +0 -119
125 lines not on this page · raw message
drivers/gpu/drm/i915/selftests/i915_mock_selftests.h +0 -1
@@ -32,6 +32,5 @@ selftest(vma, i915_vma_mock_selftests) selftest(evict, i915_gem_evict_mock_selftests) selftest(gtt, i915_gem_gtt_mock_selftests) selftest(hugepages, i915_gem_huge_page_mock_selftests) -selftest(contexts, i915_gem_context_mock_selftests) selftest(memory_region, intel_memory_region_mock_selftests)selftest (buddy, i915_buddy_mock_selftests)
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 26/30 drm/i915/gem: Don't allow changing the engine set on running contexts (v3)
In reply to Jason EkstrandWhen the APIs were added to manage the engine set on a GEM context directly from userspace, the questionable choice was made to allow changing the engine set on a context at any time. This is horribly racy and there's absolutely no reason why any userspace would want to do this outside of trying to exercise interesting race conditions. By removing support for CONTEXT_PARAM_ENGINES from ctx_setparam, we make it impossible to change the engine set after the context has been fully created.
This doesn't yet let us delete all the deferred engine clean-up code as that's still used for handling the case where the client dies or calls GEM_CONTEXT_DESTROY while work is in flight. However, moving to an API where the engine set is effectively immutable gives us more options to potentially clean that code up a bit going forward. It also removes a whole class of ways in which a client can hurt itself or try to get around kernel context banning.
v2 (Jason Ekstrand):
- Expand the commit mesage
v3 (Jason Ekstrand):
- Make it more obvious that I915_CONTEXT_PARAM_ENGINES returns -EINVAL
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 304 +------------------- 1 file changed, 1 insertion(+), 303 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -303
@@ -1819,305 +1819,6 @@ static int set_sseu(struct i915_gem_context *ctx,return ret; } -struct set_engines { -struct i915_gem_context *ctx; -struct i915_gem_engines *engines; -}; - -static int -set_engines__load_balance (struct i915_user_extension __user *base,void *data) -{ -struct i915_context_engines_load_balance __user *ext = -container_of_user (base,typeof (*ext), base); -const struct set_engines *set = data; -struct drm_i915_private *i915 = set->ctx->i915; -struct intel_engine_cs *stack[16 ]; -struct intel_engine_cs **siblings; -struct intel_context *ce; -struct intel_sseu null_sseu = {}; -u16 num_siblings, idx; -unsigned int n; -int err; - -if (!HAS_EXECLISTS (i915)) -return -ENODEV ; - -if (intel_uc_uses_guc_submission (&i915->gt.uc)) -return -ENODEV ;/* not implement yet */ - -if (get_user (idx, &ext->engine_index)) -return -EFAULT ; - -if (idx >= set->engines->num_engines) { -drm_dbg (&i915->drm,"Invalid placement value, %d >= %d\n" , - idx, set->engines->num_engines); -return -EINVAL ; - } - - idx =array_index_nospec (idx, set->engines->num_engines); -if (set->engines->engines[idx]) { -drm_dbg (&i915->drm, -"Invalid placement[%d], already occupied\n" , idx); -return -EEXIST ; - } - -if (get_user (num_siblings, &ext->num_siblings)) -return -EFAULT ; - - err =check_user_mbz (&ext->flags); -if (err) -return err; - - err =check_user_mbz (&ext->mbz64); -if (err) -return err; - - siblings = stack; -if (num_siblings >ARRAY_SIZE (stack)) { - siblings =kmalloc_array (num_siblings, -sizeof (*siblings), -GFP_KERNEL ); -if (!siblings) -return -ENOMEM ; - } - -for (n =0 ; n < num_siblings; n++) { -struct i915_engine_class_instance ci; - -if (copy_from_user (&ci, &ext->engines[n],sizeof (ci))) { - err = -EFAULT ; - goto out_siblings; - } - - siblings[n] =intel_engine_lookup_user (i915, - ci.engine_class, - ci.engine_instance); -if (!siblings[n]) { -drm_dbg (&i915->drm, -"Invalid sibling[%d]: { class:%d, inst:%d }\n" , - n, ci.engine_class, ci.engine_instance); - err = -EINVAL ; - goto out_siblings; - } - } - - ce =intel_execlists_create_virtual (siblings, n); -if (IS_ERR (ce)) { - err =PTR_ERR (ce); - goto out_siblings; - } - -intel_context_set_gem (ce, set->ctx, null_sseu); - -if (cmpxchg (&set->engines->engines[idx],NULL , ce)) { -intel_context_put (ce); - err = -EEXIST ; - goto out_siblings; - } - -out_siblings: -if (siblings != stack) -kfree (siblings); - -return err; -} - -static int -set_engines__bond (struct i915_user_extension __user *base,void *data) -{ -struct i915_context_engines_bond __user *ext = -container_of_user (base,typeof (*ext), base); -const struct set_engines *set = data; -struct drm_i915_private *i915 = set->ctx->i915; -struct i915_engine_class_instance ci; -struct intel_engine_cs *virtual; -struct intel_engine_cs *master; -u16 idx, num_bonds; -int err, n; - -if (get_user (idx, &ext->virtual_index)) -return -EFAULT ; - -if (idx >= set->engines->num_engines) { -drm_dbg (&i915->drm, -"Invalid index for virtual engine: %d >= %d\n" , - idx, set->engines->num_engines); -return -EINVAL ; - } - - idx =array_index_nospec (idx, set->engines->num_engines); -if (!set->engines->engines[idx]) { -drm_dbg (&i915->drm,"Invalid engine at %d\n" , idx); -return -EINVAL ; - } - virtual = set->engines->engines[idx]->engine; - -if (intel_engine_is_virtual (virtual)) { -drm_dbg (&i915->drm, -"Bonding with virtual engines not allowed\n" ); -return -EINVAL ; - } - - err =check_user_mbz (&ext->flags); -if (err) -return err; - -for (n =0 ; n <ARRAY_SIZE (ext->mbz64); n++) { - err =check_user_mbz (&ext->mbz64[n]); -if (err) -return err; - } - -if (copy_from_user (&ci, &ext->master,sizeof (ci))) -return -EFAULT ; - - master =intel_engine_lookup_user (i915, - ci.engine_class, ci.engine_instance); -if (!master) { -drm_dbg (&i915->drm, -"Unrecognised master engine: { class:%u, instance:%u }\n" , - ci.engine_class, ci.engine_instance); -return -EINVAL ; - } - -if (get_user (num_bonds, &ext->num_bonds)) -return -EFAULT ; - -for (n =0 ; n < num_bonds; n++) { -struct intel_engine_cs *bond; - -if (copy_from_user (&ci, &ext->engines[n],sizeof (ci))) -return -EFAULT ; - - bond =intel_engine_lookup_user (i915, - ci.engine_class, - ci.engine_instance); -if (!bond) { -drm_dbg (&i915->drm, -"Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n" , - n, ci.engine_class, ci.engine_instance); -return -EINVAL ; - } - } - -return 0 ; -} - -static const i915_user_extension_fn set_engines__extensions[] = { - [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE ] = set_engines__load_balance, - [I915_CONTEXT_ENGINES_EXT_BOND ] = set_engines__bond, -}; - -static int -set_engines (struct i915_gem_context *ctx, -const struct drm_i915_gem_context_param *args) -{ -struct drm_i915_private *i915 = ctx->i915; -struct i915_context_param_engines __user *user = -u64_to_user_ptr (args->value); -struct intel_sseu null_sseu = {}; -struct set_engines set = { .ctx = ctx }; -unsigned int num_engines, n; -u64 extensions; -int err; - -if (!args->size) {/* switch back to legacy user_ring_map */ -if (!i915_gem_context_user_engines (ctx)) -return 0 ; - - set.engines =default_engines (ctx, null_sseu); -if (IS_ERR (set.engines)) -return PTR_ERR (set.engines); - - goto replace; - } - -if (args->size <sizeof (*user) || - !IS_ALIGNED (args->size -sizeof (*user),sizeof (*user->engines))) { -drm_dbg (&i915->drm,"Invalid size for engine array: %d\n" , - args->size); -return -EINVAL ; - } - - num_engines = (args->size -sizeof (*user)) /sizeof (*user->engines); -/* RING_MASK has no shift so we can use it directly here */ -if (num_engines >I915_EXEC_RING_MASK +1 ) -return -EINVAL ; - - set.engines =alloc_engines (num_engines); -if (!set.engines) -return -ENOMEM ; - -for (n =0 ; n < num_engines; n++) { -struct i915_engine_class_instance ci; -struct intel_engine_cs *engine; -struct intel_context *ce; - -if (copy_from_user (&ci, &user->engines[n],sizeof (ci))) { -__free_engines (set.engines, n); -return -EFAULT ; - } - -if (ci.engine_class == (u16 )I915_ENGINE_CLASS_INVALID && - ci.engine_instance == (u16 )I915_ENGINE_CLASS_INVALID_NONE ) { - set.engines->engines[n] =NULL ; -continue ; - } - - engine =intel_engine_lookup_user (ctx->i915, - ci.engine_class, - ci.engine_instance); -if (!engine) { -drm_dbg (&i915->drm, -"Invalid engine[%d]: { class:%d, instance:%d }\n" , - n, ci.engine_class, ci.engine_instance); -__free_engines (set.engines, n); -return -ENOENT ; - } - - ce =intel_context_create (engine); -if (IS_ERR (ce)) { -__free_engines (set.engines, n); -return PTR_ERR (ce); - } - -intel_context_set_gem (ce, ctx, null_sseu); - - set.engines->engines[n] = ce; - } - set.engines->num_engines = num_engines; - - err = -EFAULT ; -if (!get_user (extensions, &user->extensions)) - err =i915_user_extensions (u64_to_user_ptr (extensions), - set_engines__extensions, -ARRAY_SIZE (set_engines__extensions), - &set); -if (err) { -free_engines (set.engines); -return err; - } - -replace: -mutex_lock (&ctx->engines_mutex); -if (i915_gem_context_is_closed (ctx)) { -mutex_unlock (&ctx->engines_mutex); -free_engines (set.engines); -return -ENOENT ; - } -if (args->size) -i915_gem_context_set_user_engines (ctx); -else -i915_gem_context_clear_user_engines (ctx); - set.engines =rcu_replace_pointer (ctx->engines, set.engines,1 ); -mutex_unlock (&ctx->engines_mutex); - -/* Keep track of old engine sets for kill_context() */ -engines_idle_release (ctx, set.engines); - -return 0 ; -} -static int set_persistence (struct i915_gem_context *ctx,const struct drm_i915_gem_context_param *args)
@@ -2200,10 +1901,6 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv, ret =set_sseu (ctx, args);break ; -case I915_CONTEXT_PARAM_ENGINES : - ret =set_engines (ctx, args); -break ; -case I915_CONTEXT_PARAM_PERSISTENCE : ret =set_persistence (ctx, args);break ;
@@ -2212,6 +1909,7 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,case I915_CONTEXT_PARAM_BAN_PERIOD :case I915_CONTEXT_PARAM_RINGSIZE :case I915_CONTEXT_PARAM_VM : +case I915_CONTEXT_PARAM_ENGINES :default : ret = -EINVAL ;break ;
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 27/30 drm/i915/selftests: Take a VM in kernel_context()
In reply to Jason EkstrandThis better models where we want to go with contexts in general where things like the VM and engine set are create parameters instead of being set after the fact.
--- .../drm/i915/gem/selftests/i915_gem_context.c | 4 ++-- .../gpu/drm/i915/gem/selftests/mock_context.c | 9 ++++++++- .../gpu/drm/i915/gem/selftests/mock_context.h | 4 +++- drivers/gpu/drm/i915/gt/selftest_execlists.c | 20 +++++++++---------- drivers/gpu/drm/i915/gt/selftest_hangcheck.c | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +2 -2
@@ -680,7 +680,7 @@ static int igt_ctx_exec(void *arg)struct i915_gem_context *ctx;struct intel_context *ce; - ctx =kernel_context (i915); + ctx =kernel_context (i915,NULL );if (IS_ERR (ctx)) { err =PTR_ERR (ctx); goto out_file;
@@ -813,7 +813,7 @@ static int igt_shared_ctx_exec(void *arg)struct i915_gem_context *ctx;struct intel_context *ce; - ctx =kernel_context (i915); + ctx =kernel_context (i915,NULL );if (IS_ERR (ctx)) { err =PTR_ERR (ctx); goto out_test;
drivers/gpu/drm/i915/gem/selftests/mock_context.c +8 -1
@@ -150,7 +150,8 @@ live_context_for_engine(struct intel_engine_cs *engine, struct file *file) }struct i915_gem_context * -kernel_context (struct drm_i915_private *i915) +kernel_context (struct drm_i915_private *i915, +struct i915_address_space *vm) {struct i915_gem_context *ctx;struct i915_gem_proto_context *pc;
@@ -159,6 +160,12 @@ kernel_context(struct drm_i915_private *i915)if (IS_ERR (pc))return ERR_CAST (pc); +if (vm) { +if (pc->vm) +i915_vm_put (pc->vm); + pc->vm =i915_vm_get (vm); + } + ctx =i915_gem_create_context (i915, pc);proto_context_close (pc);if (IS_ERR (ctx))
drivers/gpu/drm/i915/gem/selftests/mock_context.h +3 -1
@@ -10,6 +10,7 @@struct file ;struct drm_i915_private ;struct intel_engine_cs ; +struct i915_address_space ;void mock_init_contexts (struct drm_i915_private *i915);
@@ -25,7 +26,8 @@ live_context(struct drm_i915_private *i915, struct file *file);struct i915_gem_context *live_context_for_engine (struct intel_engine_cs *engine,struct file *file); -struct i915_gem_context *kernel_context (struct drm_i915_private *i915); +struct i915_gem_context *kernel_context (struct drm_i915_private *i915, +struct i915_address_space *vm);void kernel_context_close (struct i915_gem_context *ctx);#endif /* !__MOCK_CONTEXT_H */
drivers/gpu/drm/i915/gt/selftest_execlists.c +10 -10
@@ -1539,12 +1539,12 @@ static int live_busywait_preempt(void *arg)* preempt the busywaits used to synchronise between rings. */ - ctx_hi =kernel_context (gt->i915); + ctx_hi =kernel_context (gt->i915,NULL );if (!ctx_hi)return -ENOMEM ; ctx_hi->sched.priority =I915_CONTEXT_MAX_USER_PRIORITY ; - ctx_lo =kernel_context (gt->i915); + ctx_lo =kernel_context (gt->i915,NULL );if (!ctx_lo) goto err_ctx_hi; ctx_lo->sched.priority =I915_CONTEXT_MIN_USER_PRIORITY ;
@@ -1741,12 +1741,12 @@ static int live_preempt(void *arg)if (igt_spinner_init (&spin_lo, gt)) goto err_spin_hi; - ctx_hi =kernel_context (gt->i915); + ctx_hi =kernel_context (gt->i915,NULL );if (!ctx_hi) goto err_spin_lo; ctx_hi->sched.priority =I915_CONTEXT_MAX_USER_PRIORITY ; - ctx_lo =kernel_context (gt->i915); + ctx_lo =kernel_context (gt->i915,NULL );if (!ctx_lo) goto err_ctx_hi; ctx_lo->sched.priority =I915_CONTEXT_MIN_USER_PRIORITY ;
@@ -1833,11 +1833,11 @@ static int live_late_preempt(void *arg)if (igt_spinner_init (&spin_lo, gt)) goto err_spin_hi; - ctx_hi =kernel_context (gt->i915); + ctx_hi =kernel_context (gt->i915,NULL );if (!ctx_hi) goto err_spin_lo; - ctx_lo =kernel_context (gt->i915); + ctx_lo =kernel_context (gt->i915,NULL );if (!ctx_lo) goto err_ctx_hi;
@@ -1927,7 +1927,7 @@ struct preempt_client {static int preempt_client_init (struct intel_gt *gt,struct preempt_client *c) { - c->ctx =kernel_context (gt->i915); + c->ctx =kernel_context (gt->i915,NULL );if (!c->ctx)return -ENOMEM ;
@@ -3384,12 +3384,12 @@ static int live_preempt_timeout(void *arg)if (igt_spinner_init (&spin_lo, gt))return -ENOMEM ; - ctx_hi =kernel_context (gt->i915); + ctx_hi =kernel_context (gt->i915,NULL );if (!ctx_hi) goto err_spin_lo; ctx_hi->sched.priority =I915_CONTEXT_MAX_USER_PRIORITY ; - ctx_lo =kernel_context (gt->i915); + ctx_lo =kernel_context (gt->i915,NULL );if (!ctx_lo) goto err_ctx_hi; ctx_lo->sched.priority =I915_CONTEXT_MIN_USER_PRIORITY ;
@@ -3676,7 +3676,7 @@ static int live_preempt_smoke(void *arg) }for (n =0 ; n < smoke.ncontext; n++) { - smoke.contexts[n] =kernel_context (smoke.gt->i915); + smoke.contexts[n] =kernel_context (smoke.gt->i915,NULL );if (!smoke.contexts[n]) goto err_ctx; }
drivers/gpu/drm/i915/gt/selftest_hangcheck.c +1 -1
@@ -42,7 +42,7 @@ static int hang_init(struct hang *h, struct intel_gt *gt)memset (h,0 ,sizeof (*h)); h->gt = gt; - h->ctx =kernel_context (gt->i915); + h->ctx =kernel_context (gt->i915,NULL );if (IS_ERR (h->ctx))return PTR_ERR (h->ctx);
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 28/30 i915/gem/selftests: Assign the VM at context creation in igt_shared_ctx_exec
In reply to Jason EkstrandWe want to delete __assign_ppgtt and, generally, stop setting the VM after context creation. This is the one place I could find in the selftests where we set a VM after the fact.
--- drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpu/drm/i915/gem/selftests/i915_gem_context.c +1 -5
@@ -813,16 +813,12 @@ static int igt_shared_ctx_exec(void *arg)struct i915_gem_context *ctx;struct intel_context *ce; - ctx =kernel_context (i915,NULL ); + ctx =kernel_context (i915,ctx_vm (parent));if (IS_ERR (ctx)) { err =PTR_ERR (ctx); goto out_test; } -mutex_lock (&ctx->mutex); -__assign_ppgtt (ctx,ctx_vm (parent)); -mutex_unlock (&ctx->mutex); - ce =i915_gem_context_get_engine (ctx, engine->legacy_idx);GEM_BUG_ON (IS_ERR (ce));
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 29/30 drm/i915/gem: Roll all of context creation together
In reply to Jason EkstrandNow that we have the whole engine set and VM at context creation time, we can just assign those fields instead of creating first and handling the VM and engines later. This lets us avoid creating useless VMs and engine sets and lets us get rid of the complex VM setting code.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 176 ++++++------------ .../gpu/drm/i915/gem/selftests/mock_context.c | 33 ++-- 2 files changed, 73 insertions(+), 136 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +59 -117
@@ -1279,56 +1279,6 @@ static int __context_set_persistence(struct i915_gem_context *ctx, bool state)return 0 ; } -static struct i915_gem_context * -__create_context (struct drm_i915_private *i915, -const struct i915_gem_proto_context *pc) -{ -struct i915_gem_context *ctx; -struct i915_gem_engines *e; -int err; -int i; - - ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); -if (!ctx) -return ERR_PTR (-ENOMEM ); - -kref_init (&ctx->ref); - ctx->i915 = i915; - ctx->sched = pc->sched; -mutex_init (&ctx->mutex); -INIT_LIST_HEAD (&ctx->link); - -spin_lock_init (&ctx->stale.lock); -INIT_LIST_HEAD (&ctx->stale.engines); - -mutex_init (&ctx->engines_mutex); - e =default_engines (ctx, pc->legacy_rcs_sseu); -if (IS_ERR (e)) { - err =PTR_ERR (e); - goto err_free; - } -RCU_INIT_POINTER (ctx->engines, e); - -INIT_RADIX_TREE (&ctx->handles_vma,GFP_KERNEL ); -mutex_init (&ctx->lut_mutex); - -/* NB: Mark all slices as needing a remap so that when the context first -* loads it will restore whatever remap state already exists. If there -* is no remap info, it will be a NOP. */ - ctx->remap_slice =ALL_L3_SLICES (i915); - - ctx->user_flags = pc->user_flags; - -for (i =0 ; i <ARRAY_SIZE (ctx->hang_timestamp); i++) - ctx->hang_timestamp[i] = jiffies -CONTEXT_FAST_HANG_JIFFIES ; - -return ctx; - -err_free: -kfree (ctx); -return ERR_PTR (err); -} -static inline struct i915_gem_engines *__context_engines_await (const struct i915_gem_context *ctx,bool *user_engines)
@@ -1372,54 +1322,31 @@ context_apply_all(struct i915_gem_context *ctx,i915_sw_fence_complete (&e->fence); } -static void __apply_ppgtt (struct intel_context *ce,void *vm) -{ -i915_vm_put (ce->vm); - ce->vm =i915_vm_get (vm); -} - -static struct i915_address_space * -__set_ppgtt (struct i915_gem_context *ctx,struct i915_address_space *vm) -{ -struct i915_address_space *old; - - old =rcu_replace_pointer (ctx->vm, -i915_vm_open (vm), -lockdep_is_held (&ctx->mutex)); -GEM_BUG_ON (old &&i915_vm_is_4lvl (vm) !=i915_vm_is_4lvl (old)); - -context_apply_all (ctx, __apply_ppgtt, vm); - -return old; -} - -static void __assign_ppgtt (struct i915_gem_context *ctx, -struct i915_address_space *vm) -{ -if (vm ==rcu_access_pointer (ctx->vm)) -return ; - - vm =__set_ppgtt (ctx, vm); -if (vm) -i915_vm_close (vm); -} -static struct i915_gem_context *i915_gem_create_context (struct drm_i915_private *i915,const struct i915_gem_proto_context *pc) {struct i915_gem_context *ctx; -int ret; +struct i915_address_space *vm =NULL ; +struct i915_gem_engines *e; +int err; +int i; - ctx =__create_context (i915, pc); -if (IS_ERR (ctx)) -return ctx; + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); +if (!ctx) +return ERR_PTR (-ENOMEM ); + +kref_init (&ctx->ref); + ctx->i915 = i915; + ctx->sched = pc->sched; +mutex_init (&ctx->mutex); +INIT_LIST_HEAD (&ctx->link); + +spin_lock_init (&ctx->stale.lock); +INIT_LIST_HEAD (&ctx->stale.engines);if (pc->vm) { -/* __assign_ppgtt() requires this mutex to be held */ -mutex_lock (&ctx->mutex); -__assign_ppgtt (ctx, pc->vm); -mutex_unlock (&ctx->mutex); + vm =i915_vm_get (pc->vm); }else if (HAS_FULL_PPGTT (i915)) {struct i915_ppgtt *ppgtt;
@@ -1427,50 +1354,65 @@ i915_gem_create_context(struct drm_i915_private *i915,if (IS_ERR (ppgtt)) {drm_dbg (&i915->drm,"PPGTT setup failed (%ld)\n" ,PTR_ERR (ppgtt)); -context_close (ctx); -return ERR_CAST (ppgtt); + err =PTR_ERR (ppgtt); + goto err_ctx; } + vm = &ppgtt->vm; + } +if (vm) { +RCU_INIT_POINTER (ctx->vm,i915_vm_open (vm)); -/* __assign_ppgtt() requires this mutex to be held */ -mutex_lock (&ctx->mutex); -__assign_ppgtt (ctx, &ppgtt->vm); -mutex_unlock (&ctx->mutex); - -/* __assign_ppgtt() takes another reference for us */ -i915_vm_put (&ppgtt->vm); +/* i915_vm_open() takes a reference */ +i915_vm_put (vm); } +mutex_init (&ctx->engines_mutex);if (pc->num_user_engines >=0 ) { -struct i915_gem_engines *engines; +i915_gem_context_set_user_engines (ctx); + e =user_engines (ctx, pc->num_user_engines, pc->user_engines); + }else { +i915_gem_context_clear_user_engines (ctx); + e =default_engines (ctx, pc->legacy_rcs_sseu); + } +if (IS_ERR (e)) { + err =PTR_ERR (e); + goto err_vm; + } +RCU_INIT_POINTER (ctx->engines, e); - engines =user_engines (ctx, pc->num_user_engines, - pc->user_engines); -if (IS_ERR (engines)) { -context_close (ctx); -return ERR_CAST (engines); - } +INIT_RADIX_TREE (&ctx->handles_vma,GFP_KERNEL ); +mutex_init (&ctx->lut_mutex); -mutex_lock (&ctx->engines_mutex); -i915_gem_context_set_user_engines (ctx); - engines =rcu_replace_pointer (ctx->engines, engines,1 ); -mutex_unlock (&ctx->engines_mutex); +/* NB: Mark all slices as needing a remap so that when the context first +* loads it will restore whatever remap state already exists. If there +* is no remap info, it will be a NOP. */ + ctx->remap_slice =ALL_L3_SLICES (i915); -free_engines (engines); - } + ctx->user_flags = pc->user_flags; + +for (i =0 ; i <ARRAY_SIZE (ctx->hang_timestamp); i++) + ctx->hang_timestamp[i] = jiffies -CONTEXT_FAST_HANG_JIFFIES ;if (pc->single_timeline) { - ret =drm_syncobj_create (&ctx->syncobj, + err =drm_syncobj_create (&ctx->syncobj,DRM_SYNCOBJ_CREATE_SIGNALED ,NULL ); -if (ret) { -context_close (ctx); -return ERR_PTR (ret); - } +if (err) + goto err_engines; }trace_i915_context_create (ctx);return ctx; + +err_engines: +free_engines (e); +err_vm: +if (ctx->vm) +i915_vm_close (ctx->vm); +err_ctx: +kfree (ctx); +return ERR_PTR (err); }static void init_contexts (struct i915_gem_contexts *gc)
drivers/gpu/drm/i915/gem/selftests/mock_context.c +14 -19
@@ -31,15 +31,6 @@ mock_context(struct drm_i915_private *i915,i915_gem_context_set_persistence (ctx); -mutex_init (&ctx->engines_mutex); - e =default_engines (ctx, null_sseu); -if (IS_ERR (e)) - goto err_free; -RCU_INIT_POINTER (ctx->engines, e); - -INIT_RADIX_TREE (&ctx->handles_vma,GFP_KERNEL ); -mutex_init (&ctx->lut_mutex); -if (name) {struct i915_ppgtt *ppgtt;
@@ -47,25 +38,29 @@ mock_context(struct drm_i915_private *i915, ppgtt =mock_ppgtt (i915, name);if (!ppgtt) - goto err_put; - -mutex_lock (&ctx->mutex); -__set_ppgtt (ctx, &ppgtt->vm); -mutex_unlock (&ctx->mutex); + goto err_free; + ctx->vm =i915_vm_open (&ppgtt->vm);i915_vm_put (&ppgtt->vm); } +mutex_init (&ctx->engines_mutex); + e =default_engines (ctx, null_sseu); +if (IS_ERR (e)) + goto err_vm; +RCU_INIT_POINTER (ctx->engines, e); + +INIT_RADIX_TREE (&ctx->handles_vma,GFP_KERNEL ); +mutex_init (&ctx->lut_mutex); +return ctx; +err_vm: +if (ctx->vm) +i915_vm_close (ctx->vm); err_free:kfree (ctx);return NULL ; - -err_put: -i915_gem_context_set_closed (ctx); -i915_gem_context_put (ctx); -return NULL ; }void mock_context_close (struct i915_gem_context *ctx)
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 30/30 drm/i915: Finalize contexts in GEM_CONTEXT_CREATE on version 13+
In reply to Jason EkstrandAll the proto-context stuff for context creation exists to allow older userspace drivers to set VMs and engine sets via SET_CONTEXT_PARAM. Drivers need to update to use CONTEXT_CREATE_EXT_* for this going forward. Force the issue by blocking the old mechanism on any future hardware generations.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 39 ++++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +30 -9
@@ -1996,9 +1996,28 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data, goto err_pc; } - ret =proto_context_register (ext_data.fpriv, ext_data.pc, &id); -if (ret <0 ) - goto err_pc; +if (GRAPHICS_VER (i915) >12 ) { +struct i915_gem_context *ctx; + +/* Get ourselves a context ID */ + ret =xa_alloc (&ext_data.fpriv->context_xa, &id,NULL , + xa_limit_32b,GFP_KERNEL ); +if (ret) + goto err_pc; + + ctx =i915_gem_create_context (i915, ext_data.pc); +if (IS_ERR (ctx)) { + ret =PTR_ERR (ctx); + goto err_pc; + } + +proto_context_close (ext_data.pc); +gem_context_register (ctx, ext_data.fpriv, id); + }else { + ret =proto_context_register (ext_data.fpriv, ext_data.pc, &id); +if (ret <0 ) + goto err_pc; + } args->ctx_id = id;drm_dbg (&i915->drm,"HW context %d created\n" , args->ctx_id);
@@ -2181,15 +2200,17 @@ int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,mutex_lock (&file_priv->proto_context_lock); ctx =__context_lookup (file_priv, args->ctx_id);if (!ctx) { -/* FIXME: We should consider disallowing SET_CONTEXT_PARAM -* for most things on future platforms. Clients should be -* using CONTEXT_CREATE_EXT_PARAM instead. -*/ pc =xa_load (&file_priv->proto_context_xa, args->ctx_id); -if (pc) +if (pc) { +/* Contexts should be finalized inside +* GEM_CONTEXT_CREATE starting with graphics +* version 13. +*/ +WARN_ON (GRAPHICS_VER (file_priv->dev_priv) >12 ); ret =set_proto_ctx_param (file_priv, pc, args); -else + }else { ret = -ENOENT ; + } }mutex_unlock (&file_priv->proto_context_lock);
Signature · 6 lines
2.31.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Quoted 125 lines · Jason Ekstrand
Overview:
---------This patch series attempts to clean up some of the IOCTL mess we've created over the last few years. The most egregious bit being context mutability. In summary, this series:
1. Drops two never-used context params: RINGSIZE and
NO_ZEROMAP
2. Drops the entireCONTEXT_CLONEAPI
3. ImplementsSINGLE_TIMELINEwith a syncobj instead of actually sharingintel_timelinebetween engines.
4. Adds a few sanity restrictions to the balancing/bonding API.
5. Implements a proto-ctx mechanism so that the engine set and VM can only be set early on in the lifetime of a context, before anything ever executes on it. This effectively makes the VM and engine set immutable.This series has been tested with IGT as well as the Iris, ANV, and the Intel media driver doing an 8K decode (this uses bonding/balancing). I've also done quite a bit of git archeology to ensure that nothing in here will break anything that's already shipped at some point in history. It's possible I've missed something, but I've dug quite a bit.
Details and motivation:
-----------------------In very broad strokes, there's an effort going on right now within Intel to try and clean up and simplify i915 anywhere we can. We obviously don't want to break any shipping userspace but, as can be seen by this series, there's a lot i915 theoretically supports which userspace doesn't actually need. Some of this, like the two context params used here, were simply oversights where we went through the usual API review process and merged the i915 bits but the userspace bits never landed for some reason.
Not all are so innocent, however. For instance, there's an entire context cloning API which allows one to create a context with certain parameters "cloned" from some other context. This entire API has never been used by any userspace except IGT and there were never patches to any other userspace to use it. It never should have landed. Also, when we added support for setting explicit engine sets and sharing VMs across contexts, people decided to do so via
SET_CONTEXT_PARAM. While this allowed them to re-use existing API, it did so at the cost of making those states mutable which leads to a plethora of potential race conditions. There were even IGT tests merged to cover some of theses:- gem_vm_create@async-destroy and gem_vm_create@destroy-race which test swapping out the VM on a running context.
- gem_ctx_persistence@replace* which test whether a client can escape a non-persistent context by submitting a hanging batch and then swapping out the engine set before the hang is detected.
- api_intel_bb@bb-with-vm which tests the that
intel_bb_assign_vmworks properly. This API is never used by any other IGT test.There is also an entire deferred flush and set state framework in
i915_gem_cotnext.cwhich exists for safely swapping out the VM while there is work in-flight on a context.So, clearly people knew that this API was inherently racy and difficult to implement but they landed it anyway. Why? The best explanation I've been given is because it makes the API more "unified" or "symmetric" for this stuff to go through
SET_CONTEXT_PARAM. It's not because any userspace actually wants to be able to swap out the VM or the set of engines on a running context. That would be utterly insane.This patch series cleans up this particular mess by introducing the concept of a
i915_gem_proto_contextdata structure which contains context creation information. When you initially callGEM_CONTEXT_CREATE, a proto-context in created instead of an actual context. Then, the first time something is done on the context besidesSET_CONTEXT_PARAM, an actual context is created. This allows us to keep the old drivers which useSET_CONTEXT_PARAMto set up the engine set (see also media) while ensuring that, once you have ani915_gem_context, the VM and the engine set are immutable state.Eventually, there are more clean-ups I'd like to do on top of this which should make working with contexts inside i915 simpler and safer:
1. Move the GEM handle -> vma LUT from
i915_gem_contextinto eitheri915_ppgttordrm_i915_file_privatedepending on whether or not the hardware has a full PPGTT.2. Move the delayed context destruction code into
intel_contextor a per-engine wrapper struct rather thani915_gem_context.3. Get rid of the separation between context close and context destroy
4. Get rid of the RCU on
i915_gem_contextHowever, these should probably be done as a separate patch series as this one is already starting to get longish, especially if you consider the 89 IGT patches that go along with it.
Test-with: [email protected]
Jason Ekstrand (30):
drm/i915: DropI915_CONTEXT_PARAM_RINGSIZE
drm/i915: Stop storing the ring size in the ring pointer (v3) drm/i915: DropI915_CONTEXT_PARAM_NO_ZEROMAP
drm/i915/gem: Set the watchdog timeout directly in
intel_context_set_gem(v2)
drm/i915/gem: Return void fromcontext_apply_all
drm/i915: Drop theCONTEXT_CLONEAPI (v2)
drm/i915: ImplementSINGLE_TIMELINEwith a syncobj (v4) drm/i915: Drop getparam support forI915_CONTEXT_PARAM_ENGINESdrm/i915/gem: Disallow bonding of virtual engines (v3) drm/i915/gem: Remove engine auto-magic withFENCE_SUBMIT(v2) drm/i915/request: Remove the hook fromawait_executiondrm/i915/gem: Disallow creating contexts with too many engines drm/i915: Stop manually RCU banging inreset_stats_ioctl(v2) drm/i915/gem: Add a separatevalidate_priorityhelper drm/i915: Addgem/i915_gem_context.hto the docs drm/i915/gem: Add an intermediateproto_contextstruct (v5) drm/i915/gem: Rework error handling indefault_enginesdrm/i915/gem: Optionally set SSEU inintel_context_set_gemdrm/i915: Add ani915_gem_vm_lookuphelper
drm/i915/gem: Make an alignment check more sensible drm/i915/gem: Use the proto-context to handle create parameters (v5) drm/i915/gem: Return an error ptr fromcontext_lookupdrm/i915/gt: Dropi915_address_space::file(v2) drm/i915/gem: Delay context creation (v3)
drm/i915/gem: Don't allow changing the VM on running contexts (v4) drm/i915/gem: Don't allow changing the engine set on running contexts
Quoted 6 lines
(v3)drm/i915/selftests: Take a VM in
kernel_context()i915/gem/selftests: Assign the VM at context creation in
igt_shared_ctx_exec
drm/i915/gem: Roll all of context creation together drm/i915: Finalize contexts inGEM_CONTEXT_CREATEon version 13+
Applied everything, thanks a lot.
Now, could we bake all this hard-earned knowledge about how the uapi actually works into some kerneldoc for the uapi header?
Thanks, Daniel
Quoted 40 lines
Documentation/gpu/i915.rst | 2 + drivers/gpu/drm/i915/Makefile | 1 - drivers/gpu/drm/i915/gem/i915_gem_context.c | 2926 ++++++++--------- drivers/gpu/drm/i915/gem/i915_gem_context.h | 3 + .../gpu/drm/i915/gem/i915_gem_context_types.h | 196 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 31 +- .../drm/i915/gem/selftests/i915_gem_context.c | 127 +- .../gpu/drm/i915/gem/selftests/mock_context.c | 67 +- .../gpu/drm/i915/gem/selftests/mock_context.h | 4 +- drivers/gpu/drm/i915/gt/intel_context.c | 3 +- drivers/gpu/drm/i915/gt/intel_context.h | 5 - drivers/gpu/drm/i915/gt/intel_context_param.c | 63 - drivers/gpu/drm/i915/gt/intel_context_param.h | 6 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 1 + drivers/gpu/drm/i915/gt/intel_engine_cs.c | 3 +- drivers/gpu/drm/i915/gt/intel_engine_types.h | 7 - .../drm/i915/gt/intel_execlists_submission.c | 114 - .../drm/i915/gt/intel_execlists_submission.h | 8 +- drivers/gpu/drm/i915/gt/intel_gtt.h | 11 - drivers/gpu/drm/i915/gt/intel_lrc.c | 2 +- drivers/gpu/drm/i915/gt/intel_migrate.c | 3 +- drivers/gpu/drm/i915/gt/selftest_execlists.c | 251 +- drivers/gpu/drm/i915/gt/selftest_hangcheck.c | 2 +- drivers/gpu/drm/i915/gt/selftest_mocs.c | 2 +- drivers/gpu/drm/i915/gt/selftest_timeline.c | 2 +- drivers/gpu/drm/i915/gvt/scheduler.c | 7 +- drivers/gpu/drm/i915/i915_drv.h | 82 +- drivers/gpu/drm/i915/i915_perf.c | 4 +- drivers/gpu/drm/i915/i915_request.c | 42 +- drivers/gpu/drm/i915/i915_request.h | 4 +- .../drm/i915/selftests/i915_mock_selftests.h | 1 - drivers/gpu/drm/i915/selftests/mock_gtt.c | 1 - include/uapi/drm/i915_drm.h | 40 +- 33 files changed, 1681 insertions(+), 2340 deletions(-) delete mode 100644 drivers/gpu/drm/i915/gt/intel_context_param.cSignature · 1 line
2.31.1
Signature · 7 lines
Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Patchwork
CI[Intel-gfx] ✗ Fi.CI.BUILD: failure for drm/i915/gem: ioctl clean-ups (rev8)
In reply to Jason Ekstrand== Series Details ==
== Series Details ==
Series: drm/i915/gem: ioctl clean-ups (rev8)
URL : https://patchwork.freedesktop.org/series/89443/
State : failure
== Summary ==
Applying: drm/i915: Drop I915_CONTEXT_PARAM_RINGSIZE
Using index info to reconstruct a base tree...
M drivers/gpu/drm/i915/Makefile
M drivers/gpu/drm/i915/gem/i915_gem_context.c
A drivers/gpu/drm/i915/gt/intel_context_param.c
M drivers/gpu/drm/i915/gt/intel_context_param.h
M include/uapi/drm/i915_drm.h
Falling back to patching base and 3-way merge...
Auto-merging include/uapi/drm/i915_drm.h
Auto-merging drivers/gpu/drm/i915/gt/intel_context_param.h
CONFLICT (content): Merge conflict in drivers/gpu/drm/i915/gt/intel_context_param.h Auto-merging drivers/gpu/drm/i915/gem/i915_gem_context.c
CONFLICT (content): Merge conflict in drivers/gpu/drm/i915/gem/i915_gem_context.c
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch Patch failed at 0001 drm/i915: Drop I915_CONTEXT_PARAM_RINGSIZE
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