Skip to content

Commit a0e0471

Browse files
committed
drm/i915/gem: Make context persistence optional
Our existing behaviour is to allow contexts and their GPU requests to persist past the point of closure until the requests are complete. This allows clients to operate in a 'fire-and-forget' manner where they can setup a rendering pipeline and hand it over to the display server and immediately exit. As the rendering pipeline is kept alive until completion, the display server (or other consumer) can use the results in the future and present them to the user. The compute model is a little different. They have little to no buffer sharing between processes as their kernels tend to operate on a continuous stream, feeding the results back to the client application. These kernels operate for an indeterminate length of time, with many clients wishing that the kernel was always running for as long as they keep feeding in the data, i.e. acting like a DSP. Not all clients want this persistent "desktop" behaviour and would prefer that the contexts are cleaned up immediately upon closure. This ensures that when clients are run without hangchecking (e.g. for compute kernels of indeterminate runtime), any GPU hang or other unexpected workloads are terminated with the process and does not continue to hog resources. The default behaviour for new contexts is the legacy persistence mode, as some desktop applications are dependent upon the existing behaviour. New clients will have to opt in to immediate cleanup on context closure. If the hangchecking modparam is disabled, so is persistent context support -- all contexts will be terminated on closure. We expect this behaviour change to be welcomed by compute users, who have often been caught between a rock and a hard place. They disable hangchecking to avoid their kernels being "unfairly" declared hung, but have also experienced true hangs that the system was then unable to clean up. Naturally, this leads to bug reports. Testcase: igt/gem_ctx_persistence Link: intel/compute-runtime#228 Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Cc: Michał Winiarski <michal.winiarski@intel.com> Cc: Jon Bloomfield <jon.bloomfield@intel.com> Reviewed-by: Jon Bloomfield <jon.bloomfield@intel.com> Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Reviewed-by: Joonas Lahtinen <joonas.lahtinen@linux.intel.com> Acked-by: Jason Ekstrand <jason@jlekstrand.net> Link: https://patchwork.freedesktop.org/patch/msgid/20191029202338.8841-1-chris@chris-wilson.co.uk
1 parent 5451646 commit a0e0471

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎drivers/gpu/drm/i915/gem/i915_gem_context.c‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,39 @@ static void context_close(struct i915_gem_context *ctx)
438438
* case we opt to forcibly kill off all remaining requests on
439439
* context close.
440440
*/
441-
if (!i915_modparams.enable_hangcheck)
441+
if (!i915_gem_context_is_persistent(ctx) ||
442+
!i915_modparams.enable_hangcheck)
442443
kill_context(ctx);
443444

444445
i915_gem_context_put(ctx);
445446
}
446447

448+
static int __context_set_persistence(struct i915_gem_context *ctx, bool state)
449+
{
450+
if (i915_gem_context_is_persistent(ctx) == state)
451+
return 0;
452+
453+
if (state) {
454+
/*
455+
* Only contexts that are short-lived [that will expire or be
456+
* reset] are allowed to survive past termination. We require
457+
* hangcheck to ensure that the persistent requests are healthy.
458+
*/
459+
if (!i915_modparams.enable_hangcheck)
460+
return -EINVAL;
461+
462+
i915_gem_context_set_persistence(ctx);
463+
} else {
464+
/* To cancel a context we use "preempt-to-idle" */
465+
if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION))
466+
return -ENODEV;
467+
468+
i915_gem_context_clear_persistence(ctx);
469+
}
470+
471+
return 0;
472+
}
473+
447474
static struct i915_gem_context *
448475
__create_context(struct drm_i915_private *i915)
449476
{
@@ -478,6 +505,7 @@ __create_context(struct drm_i915_private *i915)
478505

479506
i915_gem_context_set_bannable(ctx);
480507
i915_gem_context_set_recoverable(ctx);
508+
__context_set_persistence(ctx, true /* cgroup hook? */);
481509

482510
for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
483511
ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
@@ -634,6 +662,7 @@ i915_gem_context_create_kernel(struct drm_i915_private *i915, int prio)
634662
return ctx;
635663

636664
i915_gem_context_clear_bannable(ctx);
665+
i915_gem_context_set_persistence(ctx);
637666
ctx->sched.priority = I915_USER_PRIORITY(prio);
638667

639668
GEM_BUG_ON(!i915_gem_context_is_kernel(ctx));
@@ -1744,6 +1773,16 @@ get_engines(struct i915_gem_context *ctx,
17441773
return err;
17451774
}
17461775

1776+
static int
1777+
set_persistence(struct i915_gem_context *ctx,
1778+
const struct drm_i915_gem_context_param *args)
1779+
{
1780+
if (args->size)
1781+
return -EINVAL;
1782+
1783+
return __context_set_persistence(ctx, args->value);
1784+
}
1785+
17471786
static int ctx_setparam(struct drm_i915_file_private *fpriv,
17481787
struct i915_gem_context *ctx,
17491788
struct drm_i915_gem_context_param *args)
@@ -1821,6 +1860,10 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,
18211860
ret = set_engines(ctx, args);
18221861
break;
18231862

1863+
case I915_CONTEXT_PARAM_PERSISTENCE:
1864+
ret = set_persistence(ctx, args);
1865+
break;
1866+
18241867
case I915_CONTEXT_PARAM_BAN_PERIOD:
18251868
default:
18261869
ret = -EINVAL;
@@ -2273,6 +2316,11 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
22732316
ret = get_engines(ctx, args);
22742317
break;
22752318

2319+
case I915_CONTEXT_PARAM_PERSISTENCE:
2320+
args->size = 0;
2321+
args->value = i915_gem_context_is_persistent(ctx);
2322+
break;
2323+
22762324
case I915_CONTEXT_PARAM_BAN_PERIOD:
22772325
default:
22782326
ret = -EINVAL;

‎drivers/gpu/drm/i915/gem/i915_gem_context.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ static inline void i915_gem_context_clear_recoverable(struct i915_gem_context *c
7676
clear_bit(UCONTEXT_RECOVERABLE, &ctx->user_flags);
7777
}
7878

79+
static inline bool i915_gem_context_is_persistent(const struct i915_gem_context *ctx)
80+
{
81+
return test_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
82+
}
83+
84+
static inline void i915_gem_context_set_persistence(struct i915_gem_context *ctx)
85+
{
86+
set_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
87+
}
88+
89+
static inline void i915_gem_context_clear_persistence(struct i915_gem_context *ctx)
90+
{
91+
clear_bit(UCONTEXT_PERSISTENCE, &ctx->user_flags);
92+
}
93+
7994
static inline bool i915_gem_context_is_banned(const struct i915_gem_context *ctx)
8095
{
8196
return test_bit(CONTEXT_BANNED, &ctx->flags);

‎drivers/gpu/drm/i915/gem/i915_gem_context_types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct i915_gem_context {
137137
#define UCONTEXT_NO_ERROR_CAPTURE 1
138138
#define UCONTEXT_BANNABLE 2
139139
#define UCONTEXT_RECOVERABLE 3
140+
#define UCONTEXT_PERSISTENCE 4
140141

141142
/**
142143
* @flags: small set of booleans

‎drivers/gpu/drm/i915/gem/selftests/mock_context.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mock_context(struct drm_i915_private *i915,
2222
INIT_LIST_HEAD(&ctx->link);
2323
ctx->i915 = i915;
2424

25+
i915_gem_context_set_persistence(ctx);
26+
2527
mutex_init(&ctx->engines_mutex);
2628
e = default_engines(ctx);
2729
if (IS_ERR(e))

‎include/uapi/drm/i915_drm.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,21 @@ struct drm_i915_gem_context_param {
15721572
* i915_context_engines_bond (I915_CONTEXT_ENGINES_EXT_BOND)
15731573
*/
15741574
#define I915_CONTEXT_PARAM_ENGINES 0xa
1575+
1576+
/*
1577+
* I915_CONTEXT_PARAM_PERSISTENCE:
1578+
*
1579+
* Allow the context and active rendering to survive the process until
1580+
* completion. Persistence allows fire-and-forget clients to queue up a
1581+
* bunch of work, hand the output over to a display server and then quit.
1582+
* If the context is marked as not persistent, upon closing (either via
1583+
* an explicit DRM_I915_GEM_CONTEXT_DESTROY or implicitly from file closure
1584+
* or process termination), the context and any outstanding requests will be
1585+
* cancelled (and exported fences for cancelled requests marked as -EIO).
1586+
*
1587+
* By default, new contexts allow persistence.
1588+
*/
1589+
#define I915_CONTEXT_PARAM_PERSISTENCE 0xb
15751590
/* Must be kept compact -- no holes and well documented */
15761591

15771592
__u64 value;

0 commit comments

Comments
 (0)