Skip to content

Commit d3ac8d4

Browse files
dceraolorodrigovivi
authored andcommitted
drm/i915/pxp: interfaces for using protected objects
This api allow user mode to create protected buffers and to mark contexts as making use of such objects. Only when using contexts marked in such a way is the execution guaranteed to work as expected. Contexts can only be marked as using protected content at creation time (i.e. the parameter is immutable) and they must be both bannable and not recoverable. Given that the protected session gets invalidated on suspend, contexts created this way hold a runtime pm wakeref until they're either destroyed or invalidated. All protected objects and contexts will be considered invalid when the PXP session is destroyed and all new submissions using them will be rejected. All intel contexts within the invalidated gem contexts will be marked banned. Userspace can detect that an invalidation has occurred via the RESET_STATS ioctl, where we report it the same way as a ban due to a hang. v5: squash patches, rebase on proto_ctx, update kerneldoc v6: rebase on obj create_ext changes v7: Use session counter to check if an object it valid, hold wakeref in context, don't add a new flag to RESET_STATS (Daniel) v8: don't increase guilty count for contexts banned during pxp invalidation (Rodrigo) v9: better comments, avoid wakeref put race between pxp_inval and context_close, add usage examples (Rodrigo) v10: modify internal set/get-protected-context functions to not return -ENODEV when setting PXP param to false or getting param when running on pxp-unsupported hw or getting param when i915 was built with CONFIG_PXP off Signed-off-by: Alan Previn <alan.previn.teres.alexis@intel.com> Signed-off-by: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com> Signed-off-by: Bommu Krishnaiah <krishnaiah.bommu@intel.com> Cc: Rodrigo Vivi <rodrigo.vivi@intel.com> Cc: Chris Wilson <chris@chris-wilson.co.uk> Cc: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Cc: Jason Ekstrand <jason@jlekstrand.net> Cc: Daniel Vetter <daniel.vetter@intel.com> Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com> Signed-off-by: Rodrigo Vivi <rodrigo.vivi@intel.com> Link: https://patchwork.freedesktop.org/patch/msgid/20210924191452.1539378-11-alan.previn.teres.alexis@intel.com
1 parent 2ae0968 commit d3ac8d4

14 files changed

Lines changed: 402 additions & 35 deletions

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

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
#include "gt/intel_gpu_commands.h"
7878
#include "gt/intel_ring.h"
7979

80+
#include "pxp/intel_pxp.h"
81+
8082
#include "i915_gem_context.h"
8183
#include "i915_trace.h"
8284
#include "i915_user_extensions.h"
@@ -186,10 +188,13 @@ static int validate_priority(struct drm_i915_private *i915,
186188
return 0;
187189
}
188190

189-
static void proto_context_close(struct i915_gem_proto_context *pc)
191+
static void proto_context_close(struct drm_i915_private *i915,
192+
struct i915_gem_proto_context *pc)
190193
{
191194
int i;
192195

196+
if (pc->pxp_wakeref)
197+
intel_runtime_pm_put(&i915->runtime_pm, pc->pxp_wakeref);
193198
if (pc->vm)
194199
i915_vm_put(pc->vm);
195200
if (pc->user_engines) {
@@ -241,6 +246,33 @@ static int proto_context_set_persistence(struct drm_i915_private *i915,
241246
return 0;
242247
}
243248

249+
static int proto_context_set_protected(struct drm_i915_private *i915,
250+
struct i915_gem_proto_context *pc,
251+
bool protected)
252+
{
253+
int ret = 0;
254+
255+
if (!protected) {
256+
pc->uses_protected_content = false;
257+
} else if (!intel_pxp_is_enabled(&i915->gt.pxp)) {
258+
ret = -ENODEV;
259+
} else if ((pc->user_flags & BIT(UCONTEXT_RECOVERABLE)) ||
260+
!(pc->user_flags & BIT(UCONTEXT_BANNABLE))) {
261+
ret = -EPERM;
262+
} else {
263+
pc->uses_protected_content = true;
264+
265+
/*
266+
* protected context usage requires the PXP session to be up,
267+
* which in turn requires the device to be active.
268+
*/
269+
pc->pxp_wakeref = intel_runtime_pm_get(&i915->runtime_pm);
270+
ret = intel_pxp_wait_for_arb_start(&i915->gt.pxp);
271+
}
272+
273+
return ret;
274+
}
275+
244276
static struct i915_gem_proto_context *
245277
proto_context_create(struct drm_i915_private *i915, unsigned int flags)
246278
{
@@ -269,7 +301,7 @@ proto_context_create(struct drm_i915_private *i915, unsigned int flags)
269301
return pc;
270302

271303
proto_close:
272-
proto_context_close(pc);
304+
proto_context_close(i915, pc);
273305
return err;
274306
}
275307

@@ -693,17 +725,21 @@ static int set_proto_ctx_param(struct drm_i915_file_private *fpriv,
693725
ret = -EPERM;
694726
else if (args->value)
695727
pc->user_flags |= BIT(UCONTEXT_BANNABLE);
728+
else if (pc->uses_protected_content)
729+
ret = -EPERM;
696730
else
697731
pc->user_flags &= ~BIT(UCONTEXT_BANNABLE);
698732
break;
699733

700734
case I915_CONTEXT_PARAM_RECOVERABLE:
701735
if (args->size)
702736
ret = -EINVAL;
703-
else if (args->value)
704-
pc->user_flags |= BIT(UCONTEXT_RECOVERABLE);
705-
else
737+
else if (!args->value)
706738
pc->user_flags &= ~BIT(UCONTEXT_RECOVERABLE);
739+
else if (pc->uses_protected_content)
740+
ret = -EPERM;
741+
else
742+
pc->user_flags |= BIT(UCONTEXT_RECOVERABLE);
707743
break;
708744

709745
case I915_CONTEXT_PARAM_PRIORITY:
@@ -731,6 +767,11 @@ static int set_proto_ctx_param(struct drm_i915_file_private *fpriv,
731767
args->value);
732768
break;
733769

770+
case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
771+
ret = proto_context_set_protected(fpriv->dev_priv, pc,
772+
args->value);
773+
break;
774+
734775
case I915_CONTEXT_PARAM_NO_ZEROMAP:
735776
case I915_CONTEXT_PARAM_BAN_PERIOD:
736777
case I915_CONTEXT_PARAM_RINGSIZE:
@@ -956,6 +997,9 @@ static void i915_gem_context_release_work(struct work_struct *work)
956997
if (vm)
957998
i915_vm_put(vm);
958999

1000+
if (ctx->pxp_wakeref)
1001+
intel_runtime_pm_put(&ctx->i915->runtime_pm, ctx->pxp_wakeref);
1002+
9591003
mutex_destroy(&ctx->engines_mutex);
9601004
mutex_destroy(&ctx->lut_mutex);
9611005

@@ -1338,6 +1382,11 @@ i915_gem_create_context(struct drm_i915_private *i915,
13381382
goto err_engines;
13391383
}
13401384

1385+
if (pc->uses_protected_content) {
1386+
ctx->pxp_wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1387+
ctx->uses_protected_content = true;
1388+
}
1389+
13411390
trace_i915_context_create(ctx);
13421391

13431392
return ctx;
@@ -1409,7 +1458,7 @@ int i915_gem_context_open(struct drm_i915_private *i915,
14091458
}
14101459

14111460
ctx = i915_gem_create_context(i915, pc);
1412-
proto_context_close(pc);
1461+
proto_context_close(i915, pc);
14131462
if (IS_ERR(ctx)) {
14141463
err = PTR_ERR(ctx);
14151464
goto err;
@@ -1436,7 +1485,7 @@ void i915_gem_context_close(struct drm_file *file)
14361485
unsigned long idx;
14371486

14381487
xa_for_each(&file_priv->proto_context_xa, idx, pc)
1439-
proto_context_close(pc);
1488+
proto_context_close(file_priv->dev_priv, pc);
14401489
xa_destroy(&file_priv->proto_context_xa);
14411490
mutex_destroy(&file_priv->proto_context_lock);
14421491

@@ -1731,6 +1780,15 @@ static int set_priority(struct i915_gem_context *ctx,
17311780
return 0;
17321781
}
17331782

1783+
static int get_protected(struct i915_gem_context *ctx,
1784+
struct drm_i915_gem_context_param *args)
1785+
{
1786+
args->size = 0;
1787+
args->value = i915_gem_context_uses_protected_content(ctx);
1788+
1789+
return 0;
1790+
}
1791+
17341792
static int ctx_setparam(struct drm_i915_file_private *fpriv,
17351793
struct i915_gem_context *ctx,
17361794
struct drm_i915_gem_context_param *args)
@@ -1754,17 +1812,21 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,
17541812
ret = -EPERM;
17551813
else if (args->value)
17561814
i915_gem_context_set_bannable(ctx);
1815+
else if (i915_gem_context_uses_protected_content(ctx))
1816+
ret = -EPERM; /* can't clear this for protected contexts */
17571817
else
17581818
i915_gem_context_clear_bannable(ctx);
17591819
break;
17601820

17611821
case I915_CONTEXT_PARAM_RECOVERABLE:
17621822
if (args->size)
17631823
ret = -EINVAL;
1764-
else if (args->value)
1765-
i915_gem_context_set_recoverable(ctx);
1766-
else
1824+
else if (!args->value)
17671825
i915_gem_context_clear_recoverable(ctx);
1826+
else if (i915_gem_context_uses_protected_content(ctx))
1827+
ret = -EPERM; /* can't set this for protected contexts */
1828+
else
1829+
i915_gem_context_set_recoverable(ctx);
17681830
break;
17691831

17701832
case I915_CONTEXT_PARAM_PRIORITY:
@@ -1779,6 +1841,7 @@ static int ctx_setparam(struct drm_i915_file_private *fpriv,
17791841
ret = set_persistence(ctx, args);
17801842
break;
17811843

1844+
case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
17821845
case I915_CONTEXT_PARAM_NO_ZEROMAP:
17831846
case I915_CONTEXT_PARAM_BAN_PERIOD:
17841847
case I915_CONTEXT_PARAM_RINGSIZE:
@@ -1857,7 +1920,7 @@ finalize_create_context_locked(struct drm_i915_file_private *file_priv,
18571920

18581921
old = xa_erase(&file_priv->proto_context_xa, id);
18591922
GEM_BUG_ON(old != pc);
1860-
proto_context_close(pc);
1923+
proto_context_close(file_priv->dev_priv, pc);
18611924

18621925
/* One for the xarray and one for the caller */
18631926
return i915_gem_context_get(ctx);
@@ -1943,7 +2006,7 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
19432006
goto err_pc;
19442007
}
19452008

1946-
proto_context_close(ext_data.pc);
2009+
proto_context_close(i915, ext_data.pc);
19472010
gem_context_register(ctx, ext_data.fpriv, id);
19482011
} else {
19492012
ret = proto_context_register(ext_data.fpriv, ext_data.pc, &id);
@@ -1957,7 +2020,7 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
19572020
return 0;
19582021

19592022
err_pc:
1960-
proto_context_close(ext_data.pc);
2023+
proto_context_close(i915, ext_data.pc);
19612024
return ret;
19622025
}
19632026

@@ -1988,7 +2051,7 @@ int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
19882051
GEM_WARN_ON(ctx && pc);
19892052

19902053
if (pc)
1991-
proto_context_close(pc);
2054+
proto_context_close(file_priv->dev_priv, pc);
19922055

19932056
if (ctx)
19942057
context_close(ctx);
@@ -2106,6 +2169,10 @@ int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
21062169
args->value = i915_gem_context_is_persistent(ctx);
21072170
break;
21082171

2172+
case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
2173+
ret = get_protected(ctx, args);
2174+
break;
2175+
21092176
case I915_CONTEXT_PARAM_NO_ZEROMAP:
21102177
case I915_CONTEXT_PARAM_BAN_PERIOD:
21112178
case I915_CONTEXT_PARAM_ENGINES:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ i915_gem_context_clear_user_engines(struct i915_gem_context *ctx)
108108
clear_bit(CONTEXT_USER_ENGINES, &ctx->flags);
109109
}
110110

111+
static inline bool
112+
i915_gem_context_uses_protected_content(const struct i915_gem_context *ctx)
113+
{
114+
return ctx->uses_protected_content;
115+
}
116+
111117
/* i915_gem_context.c */
112118
void i915_gem_init__contexts(struct drm_i915_private *i915);
113119

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ struct i915_gem_proto_context {
198198

199199
/** @single_timeline: See See &i915_gem_context.syncobj */
200200
bool single_timeline;
201+
202+
/** @uses_protected_content: See &i915_gem_context.uses_protected_content */
203+
bool uses_protected_content;
204+
205+
/** @pxp_wakeref: See &i915_gem_context.pxp_wakeref */
206+
intel_wakeref_t pxp_wakeref;
201207
};
202208

203209
/**
@@ -321,6 +327,28 @@ struct i915_gem_context {
321327
#define CONTEXT_CLOSED 0
322328
#define CONTEXT_USER_ENGINES 1
323329

330+
/**
331+
* @uses_protected_content: context uses PXP-encrypted objects.
332+
*
333+
* This flag can only be set at ctx creation time and it's immutable for
334+
* the lifetime of the context. See I915_CONTEXT_PARAM_PROTECTED_CONTENT
335+
* in uapi/drm/i915_drm.h for more info on setting restrictions and
336+
* expected behaviour of marked contexts.
337+
*/
338+
bool uses_protected_content;
339+
340+
/**
341+
* @pxp_wakeref: wakeref to keep the device awake when PXP is in use
342+
*
343+
* PXP sessions are invalidated when the device is suspended, which in
344+
* turns invalidates all contexts and objects using it. To keep the
345+
* flow simple, we keep the device awake when contexts using PXP objects
346+
* are in use. It is expected that the userspace application only uses
347+
* PXP when the display is on, so taking a wakeref here shouldn't worsen
348+
* our power metrics.
349+
*/
350+
intel_wakeref_t pxp_wakeref;
351+
324352
/** @mutex: guards everything that isn't engines or handles_vma */
325353
struct mutex mutex;
326354

0 commit comments

Comments
 (0)