Commit a4e7ccda authored by Chris Wilson's avatar Chris Wilson

drm/i915: Move context management under GEM

Keep track of the GEM contexts underneath i915->gem.contexts and assign
them their own lock for the purposes of list management.

v2: Focus on lock tracking; ctx->vm is protected by ctx->mutex
v3: Correct split with removal of logical HW ID
Signed-off-by: default avatarChris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: default avatarTvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20191004134015.13204-15-chris@chris-wilson.co.uk
parent 2935ed53
This diff is collapsed.
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
#include "gt/intel_context.h" #include "gt/intel_context.h"
#include "i915_drv.h"
#include "i915_gem.h" #include "i915_gem.h"
#include "i915_gem_gtt.h"
#include "i915_scheduler.h" #include "i915_scheduler.h"
#include "intel_device_info.h" #include "intel_device_info.h"
...@@ -118,8 +120,8 @@ static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx) ...@@ -118,8 +120,8 @@ static inline bool i915_gem_context_is_kernel(struct i915_gem_context *ctx)
} }
/* i915_gem_context.c */ /* i915_gem_context.c */
int __must_check i915_gem_contexts_init(struct drm_i915_private *dev_priv); int __must_check i915_gem_init_contexts(struct drm_i915_private *i915);
void i915_gem_contexts_fini(struct drm_i915_private *dev_priv); void i915_gem_driver_release__contexts(struct drm_i915_private *i915);
int i915_gem_context_open(struct drm_i915_private *i915, int i915_gem_context_open(struct drm_i915_private *i915,
struct drm_file *file); struct drm_file *file);
...@@ -158,6 +160,27 @@ static inline void i915_gem_context_put(struct i915_gem_context *ctx) ...@@ -158,6 +160,27 @@ static inline void i915_gem_context_put(struct i915_gem_context *ctx)
kref_put(&ctx->ref, i915_gem_context_release); kref_put(&ctx->ref, i915_gem_context_release);
} }
static inline struct i915_address_space *
i915_gem_context_vm(struct i915_gem_context *ctx)
{
return rcu_dereference_protected(ctx->vm, lockdep_is_held(&ctx->mutex));
}
static inline struct i915_address_space *
i915_gem_context_get_vm_rcu(struct i915_gem_context *ctx)
{
struct i915_address_space *vm;
rcu_read_lock();
vm = rcu_dereference(ctx->vm);
if (!vm)
vm = &ctx->i915->ggtt.vm;
vm = i915_vm_get(vm);
rcu_read_unlock();
return vm;
}
static inline struct i915_gem_engines * static inline struct i915_gem_engines *
i915_gem_context_engines(struct i915_gem_context *ctx) i915_gem_context_engines(struct i915_gem_context *ctx)
{ {
......
...@@ -88,7 +88,7 @@ struct i915_gem_context { ...@@ -88,7 +88,7 @@ struct i915_gem_context {
* In other modes, this is a NULL pointer with the expectation that * In other modes, this is a NULL pointer with the expectation that
* the caller uses the shared global GTT. * the caller uses the shared global GTT.
*/ */
struct i915_address_space *vm; struct i915_address_space __rcu *vm;
/** /**
* @pid: process id of creator * @pid: process id of creator
......
...@@ -728,7 +728,7 @@ static int eb_select_context(struct i915_execbuffer *eb) ...@@ -728,7 +728,7 @@ static int eb_select_context(struct i915_execbuffer *eb)
return -ENOENT; return -ENOENT;
eb->gem_context = ctx; eb->gem_context = ctx;
if (ctx->vm) if (rcu_access_pointer(ctx->vm))
eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT; eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
eb->context_flags = 0; eb->context_flags = 0;
......
...@@ -766,7 +766,8 @@ i915_gem_userptr_ioctl(struct drm_device *dev, ...@@ -766,7 +766,8 @@ i915_gem_userptr_ioctl(struct drm_device *dev,
* On almost all of the older hw, we cannot tell the GPU that * On almost all of the older hw, we cannot tell the GPU that
* a page is readonly. * a page is readonly.
*/ */
vm = dev_priv->kernel_context->vm; vm = rcu_dereference_protected(dev_priv->kernel_context->vm,
true); /* static vm */
if (!vm || !vm->has_read_only) if (!vm || !vm->has_read_only)
return -ENODEV; return -ENODEV;
} }
......
...@@ -1322,15 +1322,15 @@ static int igt_ppgtt_pin_update(void *arg) ...@@ -1322,15 +1322,15 @@ static int igt_ppgtt_pin_update(void *arg)
struct i915_gem_context *ctx = arg; struct i915_gem_context *ctx = arg;
struct drm_i915_private *dev_priv = ctx->i915; struct drm_i915_private *dev_priv = ctx->i915;
unsigned long supported = INTEL_INFO(dev_priv)->page_sizes; unsigned long supported = INTEL_INFO(dev_priv)->page_sizes;
struct i915_address_space *vm = ctx->vm;
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
struct i915_gem_engines_iter it; struct i915_gem_engines_iter it;
struct i915_address_space *vm;
struct intel_context *ce; struct intel_context *ce;
struct i915_vma *vma; struct i915_vma *vma;
unsigned int flags = PIN_USER | PIN_OFFSET_FIXED; unsigned int flags = PIN_USER | PIN_OFFSET_FIXED;
unsigned int n; unsigned int n;
int first, last; int first, last;
int err; int err = 0;
/* /*
* Make sure there's no funny business when doing a PIN_UPDATE -- in the * Make sure there's no funny business when doing a PIN_UPDATE -- in the
...@@ -1340,9 +1340,10 @@ static int igt_ppgtt_pin_update(void *arg) ...@@ -1340,9 +1340,10 @@ static int igt_ppgtt_pin_update(void *arg)
* huge-gtt-pages. * huge-gtt-pages.
*/ */
if (!vm || !i915_vm_is_4lvl(vm)) { vm = i915_gem_context_get_vm_rcu(ctx);
if (!i915_vm_is_4lvl(vm)) {
pr_info("48b PPGTT not supported, skipping\n"); pr_info("48b PPGTT not supported, skipping\n");
return 0; goto out_vm;
} }
first = ilog2(I915_GTT_PAGE_SIZE_64K); first = ilog2(I915_GTT_PAGE_SIZE_64K);
...@@ -1451,6 +1452,8 @@ static int igt_ppgtt_pin_update(void *arg) ...@@ -1451,6 +1452,8 @@ static int igt_ppgtt_pin_update(void *arg)
i915_vma_close(vma); i915_vma_close(vma);
out_put: out_put:
i915_gem_object_put(obj); i915_gem_object_put(obj);
out_vm:
i915_vm_put(vm);
return err; return err;
} }
...@@ -1460,7 +1463,7 @@ static int igt_tmpfs_fallback(void *arg) ...@@ -1460,7 +1463,7 @@ static int igt_tmpfs_fallback(void *arg)
struct i915_gem_context *ctx = arg; struct i915_gem_context *ctx = arg;
struct drm_i915_private *i915 = ctx->i915; struct drm_i915_private *i915 = ctx->i915;
struct vfsmount *gemfs = i915->mm.gemfs; struct vfsmount *gemfs = i915->mm.gemfs;
struct i915_address_space *vm = ctx->vm ?: &i915->ggtt.vm; struct i915_address_space *vm = i915_gem_context_get_vm_rcu(ctx);
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
struct i915_vma *vma; struct i915_vma *vma;
u32 *vaddr; u32 *vaddr;
...@@ -1510,6 +1513,7 @@ static int igt_tmpfs_fallback(void *arg) ...@@ -1510,6 +1513,7 @@ static int igt_tmpfs_fallback(void *arg)
out_restore: out_restore:
i915->mm.gemfs = gemfs; i915->mm.gemfs = gemfs;
i915_vm_put(vm);
return err; return err;
} }
...@@ -1517,14 +1521,14 @@ static int igt_shrink_thp(void *arg) ...@@ -1517,14 +1521,14 @@ static int igt_shrink_thp(void *arg)
{ {
struct i915_gem_context *ctx = arg; struct i915_gem_context *ctx = arg;
struct drm_i915_private *i915 = ctx->i915; struct drm_i915_private *i915 = ctx->i915;
struct i915_address_space *vm = ctx->vm ?: &i915->ggtt.vm; struct i915_address_space *vm = i915_gem_context_get_vm_rcu(ctx);
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
struct i915_gem_engines_iter it; struct i915_gem_engines_iter it;
struct intel_context *ce; struct intel_context *ce;
struct i915_vma *vma; struct i915_vma *vma;
unsigned int flags = PIN_USER; unsigned int flags = PIN_USER;
unsigned int n; unsigned int n;
int err; int err = 0;
/* /*
* Sanity check shrinking huge-paged object -- make sure nothing blows * Sanity check shrinking huge-paged object -- make sure nothing blows
...@@ -1533,12 +1537,14 @@ static int igt_shrink_thp(void *arg) ...@@ -1533,12 +1537,14 @@ static int igt_shrink_thp(void *arg)
if (!igt_can_allocate_thp(i915)) { if (!igt_can_allocate_thp(i915)) {
pr_info("missing THP support, skipping\n"); pr_info("missing THP support, skipping\n");
return 0; goto out_vm;
} }
obj = i915_gem_object_create_shmem(i915, SZ_2M); obj = i915_gem_object_create_shmem(i915, SZ_2M);
if (IS_ERR(obj)) if (IS_ERR(obj)) {
return PTR_ERR(obj); err = PTR_ERR(obj);
goto out_vm;
}
vma = i915_vma_instance(obj, vm, NULL); vma = i915_vma_instance(obj, vm, NULL);
if (IS_ERR(vma)) { if (IS_ERR(vma)) {
...@@ -1607,6 +1613,8 @@ static int igt_shrink_thp(void *arg) ...@@ -1607,6 +1613,8 @@ static int igt_shrink_thp(void *arg)
i915_vma_close(vma); i915_vma_close(vma);
out_put: out_put:
i915_gem_object_put(obj); i915_gem_object_put(obj);
out_vm:
i915_vm_put(vm);
return err; return err;
} }
...@@ -1675,6 +1683,7 @@ int i915_gem_huge_page_live_selftests(struct drm_i915_private *i915) ...@@ -1675,6 +1683,7 @@ int i915_gem_huge_page_live_selftests(struct drm_i915_private *i915)
}; };
struct drm_file *file; struct drm_file *file;
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
struct i915_address_space *vm;
intel_wakeref_t wakeref; intel_wakeref_t wakeref;
int err; int err;
...@@ -1699,8 +1708,11 @@ int i915_gem_huge_page_live_selftests(struct drm_i915_private *i915) ...@@ -1699,8 +1708,11 @@ int i915_gem_huge_page_live_selftests(struct drm_i915_private *i915)
goto out_unlock; goto out_unlock;
} }
if (ctx->vm) mutex_lock(&ctx->mutex);
ctx->vm->scrub_64K = true; vm = i915_gem_context_vm(ctx);
if (vm)
WRITE_ONCE(vm->scrub_64K, true);
mutex_unlock(&ctx->mutex);
err = i915_subtests(tests, ctx); err = i915_subtests(tests, ctx);
......
...@@ -42,7 +42,10 @@ mock_context(struct drm_i915_private *i915, ...@@ -42,7 +42,10 @@ mock_context(struct drm_i915_private *i915,
if (!ppgtt) if (!ppgtt)
goto err_put; goto err_put;
mutex_lock(&ctx->mutex);
__set_ppgtt(ctx, &ppgtt->vm); __set_ppgtt(ctx, &ppgtt->vm);
mutex_unlock(&ctx->mutex);
i915_vm_put(&ppgtt->vm); i915_vm_put(&ppgtt->vm);
} }
...@@ -65,7 +68,7 @@ void mock_context_close(struct i915_gem_context *ctx) ...@@ -65,7 +68,7 @@ void mock_context_close(struct i915_gem_context *ctx)
void mock_init_contexts(struct drm_i915_private *i915) void mock_init_contexts(struct drm_i915_private *i915)
{ {
init_contexts(i915); init_contexts(&i915->gem.contexts);
} }
struct i915_gem_context * struct i915_gem_context *
...@@ -74,8 +77,6 @@ live_context(struct drm_i915_private *i915, struct drm_file *file) ...@@ -74,8 +77,6 @@ live_context(struct drm_i915_private *i915, struct drm_file *file)
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
int err; int err;
lockdep_assert_held(&i915->drm.struct_mutex);
ctx = i915_gem_create_context(i915, 0); ctx = i915_gem_create_context(i915, 0);
if (IS_ERR(ctx)) if (IS_ERR(ctx))
return ctx; return ctx;
......
...@@ -221,12 +221,20 @@ intel_context_init(struct intel_context *ce, ...@@ -221,12 +221,20 @@ intel_context_init(struct intel_context *ce,
struct i915_gem_context *ctx, struct i915_gem_context *ctx,
struct intel_engine_cs *engine) struct intel_engine_cs *engine)
{ {
struct i915_address_space *vm;
GEM_BUG_ON(!engine->cops); GEM_BUG_ON(!engine->cops);
kref_init(&ce->ref); kref_init(&ce->ref);
ce->gem_context = ctx; ce->gem_context = ctx;
ce->vm = i915_vm_get(ctx->vm ?: &engine->gt->ggtt->vm); rcu_read_lock();
vm = rcu_dereference(ctx->vm);
if (vm)
ce->vm = i915_vm_get(vm);
else
ce->vm = i915_vm_get(&engine->gt->ggtt->vm);
rcu_read_unlock();
if (ctx->timeline) if (ctx->timeline)
ce->timeline = intel_timeline_get(ctx->timeline); ce->timeline = intel_timeline_get(ctx->timeline);
......
...@@ -155,13 +155,9 @@ static int live_context_size(void *arg) ...@@ -155,13 +155,9 @@ static int live_context_size(void *arg)
* HW tries to write past the end of one. * HW tries to write past the end of one.
*/ */
mutex_lock(&gt->i915->drm.struct_mutex);
fixme = kernel_context(gt->i915); fixme = kernel_context(gt->i915);
if (IS_ERR(fixme)) { if (IS_ERR(fixme))
err = PTR_ERR(fixme); return PTR_ERR(fixme);
goto unlock;
}
for_each_engine(engine, gt->i915, id) { for_each_engine(engine, gt->i915, id) {
struct { struct {
...@@ -201,8 +197,6 @@ static int live_context_size(void *arg) ...@@ -201,8 +197,6 @@ static int live_context_size(void *arg)
} }
kernel_context_close(fixme); kernel_context_close(fixme);
unlock:
mutex_unlock(&gt->i915->drm.struct_mutex);
return err; return err;
} }
...@@ -305,12 +299,10 @@ static int live_active_context(void *arg) ...@@ -305,12 +299,10 @@ static int live_active_context(void *arg)
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
mutex_lock(&gt->i915->drm.struct_mutex);
fixme = live_context(gt->i915, file); fixme = live_context(gt->i915, file);
if (IS_ERR(fixme)) { if (IS_ERR(fixme)) {
err = PTR_ERR(fixme); err = PTR_ERR(fixme);
goto unlock; goto out_file;
} }
for_each_engine(engine, gt->i915, id) { for_each_engine(engine, gt->i915, id) {
...@@ -323,8 +315,7 @@ static int live_active_context(void *arg) ...@@ -323,8 +315,7 @@ static int live_active_context(void *arg)
break; break;
} }
unlock: out_file:
mutex_unlock(&gt->i915->drm.struct_mutex);
mock_file_free(gt->i915, file); mock_file_free(gt->i915, file);
return err; return err;
} }
...@@ -418,12 +409,10 @@ static int live_remote_context(void *arg) ...@@ -418,12 +409,10 @@ static int live_remote_context(void *arg)
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
mutex_lock(&gt->i915->drm.struct_mutex);
fixme = live_context(gt->i915, file); fixme = live_context(gt->i915, file);
if (IS_ERR(fixme)) { if (IS_ERR(fixme)) {
err = PTR_ERR(fixme); err = PTR_ERR(fixme);
goto unlock; goto out_file;
} }
for_each_engine(engine, gt->i915, id) { for_each_engine(engine, gt->i915, id) {
...@@ -436,8 +425,7 @@ static int live_remote_context(void *arg) ...@@ -436,8 +425,7 @@ static int live_remote_context(void *arg)
break; break;
} }
unlock: out_file:
mutex_unlock(&gt->i915->drm.struct_mutex);
mock_file_free(gt->i915, file); mock_file_free(gt->i915, file);
return err; return err;
} }
......
...@@ -58,9 +58,7 @@ static int hang_init(struct hang *h, struct intel_gt *gt) ...@@ -58,9 +58,7 @@ static int hang_init(struct hang *h, struct intel_gt *gt)
memset(h, 0, sizeof(*h)); memset(h, 0, sizeof(*h));
h->gt = gt; h->gt = gt;
mutex_lock(&gt->i915->drm.struct_mutex);
h->ctx = kernel_context(gt->i915); h->ctx = kernel_context(gt->i915);
mutex_unlock(&gt->i915->drm.struct_mutex);
if (IS_ERR(h->ctx)) if (IS_ERR(h->ctx))
return PTR_ERR(h->ctx); return PTR_ERR(h->ctx);
...@@ -133,7 +131,7 @@ static struct i915_request * ...@@ -133,7 +131,7 @@ static struct i915_request *
hang_create_request(struct hang *h, struct intel_engine_cs *engine) hang_create_request(struct hang *h, struct intel_engine_cs *engine)
{ {
struct intel_gt *gt = h->gt; struct intel_gt *gt = h->gt;
struct i915_address_space *vm = h->ctx->vm ?: &engine->gt->ggtt->vm; struct i915_address_space *vm = i915_gem_context_get_vm_rcu(h->ctx);
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
struct i915_request *rq = NULL; struct i915_request *rq = NULL;
struct i915_vma *hws, *vma; struct i915_vma *hws, *vma;
...@@ -143,12 +141,15 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine) ...@@ -143,12 +141,15 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine)
int err; int err;
obj = i915_gem_object_create_internal(gt->i915, PAGE_SIZE); obj = i915_gem_object_create_internal(gt->i915, PAGE_SIZE);
if (IS_ERR(obj)) if (IS_ERR(obj)) {
i915_vm_put(vm);
return ERR_CAST(obj); return ERR_CAST(obj);
}
vaddr = i915_gem_object_pin_map(obj, i915_coherent_map_type(gt->i915)); vaddr = i915_gem_object_pin_map(obj, i915_coherent_map_type(gt->i915));
if (IS_ERR(vaddr)) { if (IS_ERR(vaddr)) {
i915_gem_object_put(obj); i915_gem_object_put(obj);
i915_vm_put(vm);
return ERR_CAST(vaddr); return ERR_CAST(vaddr);
} }
...@@ -159,16 +160,22 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine) ...@@ -159,16 +160,22 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine)
h->batch = vaddr; h->batch = vaddr;
vma = i915_vma_instance(h->obj, vm, NULL); vma = i915_vma_instance(h->obj, vm, NULL);
if (IS_ERR(vma)) if (IS_ERR(vma)) {
i915_vm_put(vm);
return ERR_CAST(vma); return ERR_CAST(vma);
}
hws = i915_vma_instance(h->hws, vm, NULL); hws = i915_vma_instance(h->hws, vm, NULL);
if (IS_ERR(hws)) if (IS_ERR(hws)) {
i915_vm_put(vm);
return ERR_CAST(hws); return ERR_CAST(hws);
}
err = i915_vma_pin(vma, 0, 0, PIN_USER); err = i915_vma_pin(vma, 0, 0, PIN_USER);
if (err) if (err) {
i915_vm_put(vm);
return ERR_PTR(err); return ERR_PTR(err);
}
err = i915_vma_pin(hws, 0, 0, PIN_USER); err = i915_vma_pin(hws, 0, 0, PIN_USER);
if (err) if (err)
...@@ -266,6 +273,7 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine) ...@@ -266,6 +273,7 @@ hang_create_request(struct hang *h, struct intel_engine_cs *engine)
i915_vma_unpin(hws); i915_vma_unpin(hws);
unpin_vma: unpin_vma:
i915_vma_unpin(vma); i915_vma_unpin(vma);
i915_vm_put(vm);
return err ? ERR_PTR(err) : rq; return err ? ERR_PTR(err) : rq;
} }
...@@ -382,9 +390,7 @@ static int igt_reset_nop(void *arg) ...@@ -382,9 +390,7 @@ static int igt_reset_nop(void *arg)
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
mutex_lock(&gt->i915->drm.struct_mutex);
ctx = live_context(gt->i915, file); ctx = live_context(gt->i915, file);
mutex_unlock(&gt->i915->drm.struct_mutex);
if (IS_ERR(ctx)) { if (IS_ERR(ctx)) {
err = PTR_ERR(ctx); err = PTR_ERR(ctx);
goto out; goto out;
...@@ -458,9 +464,7 @@ static int igt_reset_nop_engine(void *arg) ...@@ -458,9 +464,7 @@ static int igt_reset_nop_engine(void *arg)
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
mutex_lock(&gt->i915->drm.struct_mutex);
ctx = live_context(gt->i915, file); ctx = live_context(gt->i915, file);
mutex_unlock(&gt->i915->drm.struct_mutex);
if (IS_ERR(ctx)) { if (IS_ERR(ctx)) {
err = PTR_ERR(ctx); err = PTR_ERR(ctx);
goto out; goto out;
...@@ -705,9 +709,7 @@ static int active_engine(void *data) ...@@ -705,9 +709,7 @@ static int active_engine(void *data)
return PTR_ERR(file); return PTR_ERR(file);
for (count = 0; count < ARRAY_SIZE(ctx); count++) { for (count = 0; count < ARRAY_SIZE(ctx); count++) {
mutex_lock(&engine->i915->drm.struct_mutex);
ctx[count] = live_context(engine->i915, file); ctx[count] = live_context(engine->i915, file);
mutex_unlock(&engine->i915->drm.struct_mutex);
if (IS_ERR(ctx[count])) { if (IS_ERR(ctx[count])) {
err = PTR_ERR(ctx[count]); err = PTR_ERR(ctx[count]);
while (--count) while (--count)
...@@ -1291,6 +1293,7 @@ static int igt_reset_evict_ppgtt(void *arg) ...@@ -1291,6 +1293,7 @@ static int igt_reset_evict_ppgtt(void *arg)
{ {
struct intel_gt *gt = arg; struct intel_gt *gt = arg;
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
struct i915_address_space *vm;
struct drm_file *file; struct drm_file *file;
int err; int err;
...@@ -1298,18 +1301,20 @@ static int igt_reset_evict_ppgtt(void *arg) ...@@ -1298,18 +1301,20 @@ static int igt_reset_evict_ppgtt(void *arg)
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
mutex_lock(&gt->i915->drm.struct_mutex);
ctx = live_context(gt->i915, file); ctx = live_context(gt->i915, file);
mutex_unlock(&gt->i915->drm.struct_mutex);
if (IS_ERR(ctx)) { if (IS_ERR(ctx)) {
err = PTR_ERR(ctx); err = PTR_ERR(ctx);
goto out; goto out;
} }
err = 0; err = 0;
if (ctx->vm) /* aliasing == global gtt locking, covered above */ vm = i915_gem_context_get_vm_rcu(ctx);
err = __igt_reset_evict_vma(gt, ctx->vm, if (!i915_is_ggtt(vm)) {
/* aliasing == global gtt locking, covered above */
err = __igt_reset_evict_vma(gt, vm,
evict_vma, EXEC_OBJECT_WRITE); evict_vma, EXEC_OBJECT_WRITE);
}
i915_vm_put(vm);
out: out:
mock_file_free(gt->i915, file); mock_file_free(gt->i915, file);
......
...@@ -1631,7 +1631,11 @@ static int smoke_submit(struct preempt_smoke *smoke, ...@@ -1631,7 +1631,11 @@ static int smoke_submit(struct preempt_smoke *smoke,
int err = 0; int err = 0;
if (batch) { if (batch) {
vma = i915_vma_instance(batch, ctx->vm, NULL); struct i915_address_space *vm;
vm = i915_gem_context_get_vm_rcu(ctx);
vma = i915_vma_instance(batch, vm, NULL);
i915_vm_put(vm);
if (IS_ERR(vma)) if (IS_ERR(vma))
return PTR_ERR(vma); return PTR_ERR(vma);
......
...@@ -260,7 +260,6 @@ switch_to_scratch_context(struct intel_engine_cs *engine, ...@@ -260,7 +260,6 @@ switch_to_scratch_context(struct intel_engine_cs *engine,
rq = igt_spinner_create_request(spin, ce, MI_NOOP); rq = igt_spinner_create_request(spin, ce, MI_NOOP);
intel_context_put(ce); intel_context_put(ce);
kernel_context_close(ctx);
if (IS_ERR(rq)) { if (IS_ERR(rq)) {
spin = NULL; spin = NULL;
...@@ -279,6 +278,7 @@ switch_to_scratch_context(struct intel_engine_cs *engine, ...@@ -279,6 +278,7 @@ switch_to_scratch_context(struct intel_engine_cs *engine,
if (err && spin) if (err && spin)
igt_spinner_end(spin); igt_spinner_end(spin);
kernel_context_close(ctx);
return err; return err;
} }
...@@ -355,6 +355,7 @@ static int check_whitelist_across_reset(struct intel_engine_cs *engine, ...@@ -355,6 +355,7 @@ static int check_whitelist_across_reset(struct intel_engine_cs *engine,
static struct i915_vma *create_batch(struct i915_gem_context *ctx) static struct i915_vma *create_batch(struct i915_gem_context *ctx)
{ {
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
struct i915_address_space *vm;
struct i915_vma *vma; struct i915_vma *vma;
int err; int err;
...@@ -362,7 +363,9 @@ static struct i915_vma *create_batch(struct i915_gem_context *ctx) ...@@ -362,7 +363,9 @@ static struct i915_vma *create_batch(struct i915_gem_context *ctx)
if (IS_ERR(obj)) if (IS_ERR(obj))
return ERR_CAST(obj); return ERR_CAST(obj);
vma = i915_vma_instance(obj, ctx->vm, NULL); vm = i915_gem_context_get_vm_rcu(ctx);
vma = i915_vma_instance(obj, vm, NULL);
i915_vm_put(vm);
if (IS_ERR(vma)) { if (IS_ERR(vma)) {
err = PTR_ERR(vma); err = PTR_ERR(vma);
goto err_obj; goto err_obj;
...@@ -463,12 +466,15 @@ static int check_dirty_whitelist(struct i915_gem_context *ctx, ...@@ -463,12 +466,15 @@ static int check_dirty_whitelist(struct i915_gem_context *ctx,
0xffff00ff, 0xffff00ff,
0xffffffff, 0xffffffff,
}; };
struct i915_address_space *vm;
struct i915_vma *scratch; struct i915_vma *scratch;
struct i915_vma *batch; struct i915_vma *batch;
int err = 0, i, v; int err = 0, i, v;
u32 *cs, *results; u32 *cs, *results;
scratch = create_scratch(ctx->vm, 2 * ARRAY_SIZE(values) + 1); vm = i915_gem_context_get_vm_rcu(ctx);
scratch = create_scratch(vm, 2 * ARRAY_SIZE(values) + 1);
i915_vm_put(vm);
if (IS_ERR(scratch)) if (IS_ERR(scratch))
return PTR_ERR(scratch); return PTR_ERR(scratch);
...@@ -1010,6 +1016,7 @@ static int live_isolated_whitelist(void *arg) ...@@ -1010,6 +1016,7 @@ static int live_isolated_whitelist(void *arg)
return 0; return 0;
for (i = 0; i < ARRAY_SIZE(client); i++) { for (i = 0; i < ARRAY_SIZE(client); i++) {
struct i915_address_space *vm;
struct i915_gem_context *c; struct i915_gem_context *c;
c = kernel_context(i915); c = kernel_context(i915);
...@@ -1018,22 +1025,27 @@ static int live_isolated_whitelist(void *arg) ...@@ -1018,22 +1025,27 @@ static int live_isolated_whitelist(void *arg)
goto err; goto err;
} }
client[i].scratch[0] = create_scratch(c->vm, 1024); vm = i915_gem_context_get_vm_rcu(c);
client[i].scratch[0] = create_scratch(vm, 1024);
if (IS_ERR(client[i].scratch[0])) { if (IS_ERR(client[i].scratch[0])) {
err = PTR_ERR(client[i].scratch[0]); err = PTR_ERR(client[i].scratch[0]);
i915_vm_put(vm);
kernel_context_close(c); kernel_context_close(c);
goto err; goto err;
} }
client[i].scratch[1] = create_scratch(c->vm, 1024); client[i].scratch[1] = create_scratch(vm, 1024);
if (IS_ERR(client[i].scratch[1])) { if (IS_ERR(client[i].scratch[1])) {
err = PTR_ERR(client[i].scratch[1]); err = PTR_ERR(client[i].scratch[1]);
i915_vma_unpin_and_release(&client[i].scratch[0], 0); i915_vma_unpin_and_release(&client[i].scratch[0], 0);
i915_vm_put(vm);
kernel_context_close(c); kernel_context_close(c);
goto err; goto err;
} }
client[i].ctx = c; client[i].ctx = c;
i915_vm_put(vm);
} }
for_each_engine(engine, i915, id) { for_each_engine(engine, i915, id) {
......
...@@ -365,7 +365,8 @@ static void set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload, ...@@ -365,7 +365,8 @@ static void set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload,
struct i915_gem_context *ctx) struct i915_gem_context *ctx)
{ {
struct intel_vgpu_mm *mm = workload->shadow_mm; struct intel_vgpu_mm *mm = workload->shadow_mm;
struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(ctx->vm); struct i915_ppgtt *ppgtt =
i915_vm_to_ppgtt(i915_gem_context_get_vm_rcu(ctx));
int i = 0; int i = 0;
if (mm->ppgtt_mm.root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) { if (mm->ppgtt_mm.root_entry_type == GTT_TYPE_PPGTT_ROOT_L4_ENTRY) {
...@@ -378,6 +379,8 @@ static void set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload, ...@@ -378,6 +379,8 @@ static void set_context_ppgtt_from_shadow(struct intel_vgpu_workload *workload,
px_dma(pd) = mm->ppgtt_mm.shadow_pdps[i]; px_dma(pd) = mm->ppgtt_mm.shadow_pdps[i];
} }
} }
i915_vm_put(&ppgtt->vm);
} }
static int static int
...@@ -1213,20 +1216,18 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) ...@@ -1213,20 +1216,18 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
struct intel_vgpu_submission *s = &vgpu->submission; struct intel_vgpu_submission *s = &vgpu->submission;
struct intel_engine_cs *engine; struct intel_engine_cs *engine;
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
struct i915_ppgtt *ppgtt;
enum intel_engine_id i; enum intel_engine_id i;
int ret; int ret;
mutex_lock(&i915->drm.struct_mutex);
ctx = i915_gem_context_create_kernel(i915, I915_PRIORITY_MAX); ctx = i915_gem_context_create_kernel(i915, I915_PRIORITY_MAX);
if (IS_ERR(ctx)) { if (IS_ERR(ctx))
ret = PTR_ERR(ctx); return PTR_ERR(ctx);
goto out_unlock;
}
i915_gem_context_set_force_single_submission(ctx); i915_gem_context_set_force_single_submission(ctx);
i915_context_ppgtt_root_save(s, i915_vm_to_ppgtt(ctx->vm)); ppgtt = i915_vm_to_ppgtt(i915_gem_context_get_vm_rcu(ctx));
i915_context_ppgtt_root_save(s, ppgtt);
for_each_engine(engine, i915, i) { for_each_engine(engine, i915, i) {
struct intel_context *ce; struct intel_context *ce;
...@@ -1271,12 +1272,12 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) ...@@ -1271,12 +1272,12 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
atomic_set(&s->running_workload_num, 0); atomic_set(&s->running_workload_num, 0);
bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES); bitmap_zero(s->tlb_handle_pending, I915_NUM_ENGINES);
i915_vm_put(&ppgtt->vm);
i915_gem_context_put(ctx); i915_gem_context_put(ctx);
mutex_unlock(&i915->drm.struct_mutex);
return 0; return 0;
out_shadow_ctx: out_shadow_ctx:
i915_context_ppgtt_root_restore(s, i915_vm_to_ppgtt(ctx->vm)); i915_context_ppgtt_root_restore(s, ppgtt);
for_each_engine(engine, i915, i) { for_each_engine(engine, i915, i) {
if (IS_ERR(s->shadow[i])) if (IS_ERR(s->shadow[i]))
break; break;
...@@ -1284,9 +1285,8 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu) ...@@ -1284,9 +1285,8 @@ int intel_vgpu_setup_submission(struct intel_vgpu *vgpu)
intel_context_unpin(s->shadow[i]); intel_context_unpin(s->shadow[i]);
intel_context_put(s->shadow[i]); intel_context_put(s->shadow[i]);
} }
i915_vm_put(&ppgtt->vm);
i915_gem_context_put(ctx); i915_gem_context_put(ctx);
out_unlock:
mutex_unlock(&i915->drm.struct_mutex);
return ret; return ret;
} }
......
...@@ -316,12 +316,18 @@ static void print_context_stats(struct seq_file *m, ...@@ -316,12 +316,18 @@ static void print_context_stats(struct seq_file *m,
struct drm_i915_private *i915) struct drm_i915_private *i915)
{ {
struct file_stats kstats = {}; struct file_stats kstats = {};
struct i915_gem_context *ctx; struct i915_gem_context *ctx, *cn;
list_for_each_entry(ctx, &i915->contexts.list, link) { spin_lock(&i915->gem.contexts.lock);
list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
struct i915_gem_engines_iter it; struct i915_gem_engines_iter it;
struct intel_context *ce; struct intel_context *ce;
if (!kref_get_unless_zero(&ctx->ref))
continue;
spin_unlock(&i915->gem.contexts.lock);
for_each_gem_engine(ce, for_each_gem_engine(ce,
i915_gem_context_lock_engines(ctx), it) { i915_gem_context_lock_engines(ctx), it) {
intel_context_lock_pinned(ce); intel_context_lock_pinned(ce);
...@@ -338,7 +344,9 @@ static void print_context_stats(struct seq_file *m, ...@@ -338,7 +344,9 @@ static void print_context_stats(struct seq_file *m,
i915_gem_context_unlock_engines(ctx); i915_gem_context_unlock_engines(ctx);
if (!IS_ERR_OR_NULL(ctx->file_priv)) { if (!IS_ERR_OR_NULL(ctx->file_priv)) {
struct file_stats stats = { .vm = ctx->vm, }; struct file_stats stats = {
.vm = rcu_access_pointer(ctx->vm),
};
struct drm_file *file = ctx->file_priv->file; struct drm_file *file = ctx->file_priv->file;
struct task_struct *task; struct task_struct *task;
char name[80]; char name[80];
...@@ -355,7 +363,12 @@ static void print_context_stats(struct seq_file *m, ...@@ -355,7 +363,12 @@ static void print_context_stats(struct seq_file *m,
print_file_stats(m, name, stats); print_file_stats(m, name, stats);
} }
spin_lock(&i915->gem.contexts.lock);
list_safe_reset_next(ctx, cn, link);
i915_gem_context_put(ctx);
} }
spin_unlock(&i915->gem.contexts.lock);
print_file_stats(m, "[k]contexts", kstats); print_file_stats(m, "[k]contexts", kstats);
} }
...@@ -363,7 +376,6 @@ static void print_context_stats(struct seq_file *m, ...@@ -363,7 +376,6 @@ static void print_context_stats(struct seq_file *m,
static int i915_gem_object_info(struct seq_file *m, void *data) static int i915_gem_object_info(struct seq_file *m, void *data)
{ {
struct drm_i915_private *i915 = node_to_i915(m->private); struct drm_i915_private *i915 = node_to_i915(m->private);
int ret;
seq_printf(m, "%u shrinkable [%u free] objects, %llu bytes\n", seq_printf(m, "%u shrinkable [%u free] objects, %llu bytes\n",
i915->mm.shrink_count, i915->mm.shrink_count,
...@@ -372,12 +384,7 @@ static int i915_gem_object_info(struct seq_file *m, void *data) ...@@ -372,12 +384,7 @@ static int i915_gem_object_info(struct seq_file *m, void *data)
seq_putc(m, '\n'); seq_putc(m, '\n');
ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
if (ret)
return ret;
print_context_stats(m, i915); print_context_stats(m, i915);
mutex_unlock(&i915->drm.struct_mutex);
return 0; return 0;
} }
...@@ -1579,19 +1586,19 @@ static void describe_ctx_ring(struct seq_file *m, struct intel_ring *ring) ...@@ -1579,19 +1586,19 @@ static void describe_ctx_ring(struct seq_file *m, struct intel_ring *ring)
static int i915_context_status(struct seq_file *m, void *unused) static int i915_context_status(struct seq_file *m, void *unused)
{ {
struct drm_i915_private *dev_priv = node_to_i915(m->private); struct drm_i915_private *i915 = node_to_i915(m->private);
struct drm_device *dev = &dev_priv->drm; struct i915_gem_context *ctx, *cn;
struct i915_gem_context *ctx;
int ret;
ret = mutex_lock_interruptible(&dev->struct_mutex);
if (ret)
return ret;
list_for_each_entry(ctx, &dev_priv->contexts.list, link) { spin_lock(&i915->gem.contexts.lock);
list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
struct i915_gem_engines_iter it; struct i915_gem_engines_iter it;
struct intel_context *ce; struct intel_context *ce;
if (!kref_get_unless_zero(&ctx->ref))
continue;
spin_unlock(&i915->gem.contexts.lock);
seq_puts(m, "HW context "); seq_puts(m, "HW context ");
if (ctx->pid) { if (ctx->pid) {
struct task_struct *task; struct task_struct *task;
...@@ -1626,9 +1633,12 @@ static int i915_context_status(struct seq_file *m, void *unused) ...@@ -1626,9 +1633,12 @@ static int i915_context_status(struct seq_file *m, void *unused)
i915_gem_context_unlock_engines(ctx); i915_gem_context_unlock_engines(ctx);
seq_putc(m, '\n'); seq_putc(m, '\n');
}
mutex_unlock(&dev->struct_mutex); spin_lock(&i915->gem.contexts.lock);
list_safe_reset_next(ctx, cn, link);
i915_gem_context_put(ctx);
}
spin_unlock(&i915->gem.contexts.lock);
return 0; return 0;
} }
......
...@@ -1702,10 +1702,8 @@ static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file) ...@@ -1702,10 +1702,8 @@ static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
{ {
struct drm_i915_file_private *file_priv = file->driver_priv; struct drm_i915_file_private *file_priv = file->driver_priv;
mutex_lock(&dev->struct_mutex);
i915_gem_context_close(file); i915_gem_context_close(file);
i915_gem_release(dev, file); i915_gem_release(dev, file);
mutex_unlock(&dev->struct_mutex);
kfree_rcu(file_priv, rcu); kfree_rcu(file_priv, rcu);
......
...@@ -1536,13 +1536,6 @@ struct drm_i915_private { ...@@ -1536,13 +1536,6 @@ struct drm_i915_private {
int audio_power_refcount; int audio_power_refcount;
u32 audio_freq_cntrl; u32 audio_freq_cntrl;
struct {
struct mutex mutex;
struct list_head list;
struct llist_head free_list;
struct work_struct free_work;
} contexts;
u32 fdi_rx_config; u32 fdi_rx_config;
/* Shadow for DISPLAY_PHY_CONTROL which can't be safely read */ /* Shadow for DISPLAY_PHY_CONTROL which can't be safely read */
...@@ -1698,6 +1691,14 @@ struct drm_i915_private { ...@@ -1698,6 +1691,14 @@ struct drm_i915_private {
struct { struct {
struct notifier_block pm_notifier; struct notifier_block pm_notifier;
struct i915_gem_contexts {
spinlock_t lock; /* locks list */
struct list_head list;
struct llist_head free_list;
struct work_struct free_work;
} contexts;
} gem; } gem;
/* For i945gm vblank irq vs. C3 workaround */ /* For i945gm vblank irq vs. C3 workaround */
......
...@@ -1266,7 +1266,7 @@ int i915_gem_init(struct drm_i915_private *dev_priv) ...@@ -1266,7 +1266,7 @@ int i915_gem_init(struct drm_i915_private *dev_priv)
goto err_unlock; goto err_unlock;
} }
ret = i915_gem_contexts_init(dev_priv); ret = i915_gem_init_contexts(dev_priv);
if (ret) { if (ret) {
GEM_BUG_ON(ret == -EIO); GEM_BUG_ON(ret == -EIO);
goto err_scratch; goto err_scratch;
...@@ -1348,7 +1348,7 @@ int i915_gem_init(struct drm_i915_private *dev_priv) ...@@ -1348,7 +1348,7 @@ int i915_gem_init(struct drm_i915_private *dev_priv)
} }
err_context: err_context:
if (ret != -EIO) if (ret != -EIO)
i915_gem_contexts_fini(dev_priv); i915_gem_driver_release__contexts(dev_priv);
err_scratch: err_scratch:
intel_gt_driver_release(&dev_priv->gt); intel_gt_driver_release(&dev_priv->gt);
err_unlock: err_unlock:
...@@ -1416,11 +1416,9 @@ void i915_gem_driver_remove(struct drm_i915_private *dev_priv) ...@@ -1416,11 +1416,9 @@ void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
void i915_gem_driver_release(struct drm_i915_private *dev_priv) void i915_gem_driver_release(struct drm_i915_private *dev_priv)
{ {
mutex_lock(&dev_priv->drm.struct_mutex);
intel_engines_cleanup(dev_priv); intel_engines_cleanup(dev_priv);
i915_gem_contexts_fini(dev_priv); i915_gem_driver_release__contexts(dev_priv);
intel_gt_driver_release(&dev_priv->gt); intel_gt_driver_release(&dev_priv->gt);
mutex_unlock(&dev_priv->drm.struct_mutex);
intel_wa_list_free(&dev_priv->gt_wa_list); intel_wa_list_free(&dev_priv->gt_wa_list);
...@@ -1430,7 +1428,7 @@ void i915_gem_driver_release(struct drm_i915_private *dev_priv) ...@@ -1430,7 +1428,7 @@ void i915_gem_driver_release(struct drm_i915_private *dev_priv)
i915_gem_drain_freed_objects(dev_priv); i915_gem_drain_freed_objects(dev_priv);
WARN_ON(!list_empty(&dev_priv->contexts.list)); WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
} }
void i915_gem_init_mmio(struct drm_i915_private *i915) void i915_gem_init_mmio(struct drm_i915_private *i915)
......
...@@ -1366,7 +1366,9 @@ static int gen8_init_scratch(struct i915_address_space *vm) ...@@ -1366,7 +1366,9 @@ static int gen8_init_scratch(struct i915_address_space *vm)
if (vm->has_read_only && if (vm->has_read_only &&
vm->i915->kernel_context && vm->i915->kernel_context &&
vm->i915->kernel_context->vm) { vm->i915->kernel_context->vm) {
struct i915_address_space *clone = vm->i915->kernel_context->vm; struct i915_address_space *clone =
rcu_dereference_protected(vm->i915->kernel_context->vm,
true); /* static */
GEM_BUG_ON(!clone->has_read_only); GEM_BUG_ON(!clone->has_read_only);
......
...@@ -1853,8 +1853,8 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream, ...@@ -1853,8 +1853,8 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream,
}; };
#undef ctx_flexeuN #undef ctx_flexeuN
struct intel_engine_cs *engine; struct intel_engine_cs *engine;
struct i915_gem_context *ctx; struct i915_gem_context *ctx, *cn;
int i; int i, err;
for (i = 2; i < ARRAY_SIZE(regs); i++) for (i = 2; i < ARRAY_SIZE(regs); i++)
regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg); regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
...@@ -1877,17 +1877,28 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream, ...@@ -1877,17 +1877,28 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream,
* context. Contexts idle at the time of reconfiguration are not * context. Contexts idle at the time of reconfiguration are not
* trapped behind the barrier. * trapped behind the barrier.
*/ */
list_for_each_entry(ctx, &i915->contexts.list, link) { spin_lock(&i915->gem.contexts.lock);
int err; list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
if (ctx == i915->kernel_context) if (ctx == i915->kernel_context)
continue; continue;
if (!kref_get_unless_zero(&ctx->ref))
continue;
spin_unlock(&i915->gem.contexts.lock);
err = gen8_configure_context(ctx, regs, ARRAY_SIZE(regs)); err = gen8_configure_context(ctx, regs, ARRAY_SIZE(regs));
if (err) if (err) {
i915_gem_context_put(ctx);
return err; return err;
} }
spin_lock(&i915->gem.contexts.lock);
list_safe_reset_next(ctx, cn, link);
i915_gem_context_put(ctx);
}
spin_unlock(&i915->gem.contexts.lock);
/* /*
* After updating all other contexts, we need to modify ourselves. * After updating all other contexts, we need to modify ourselves.
* If we don't modify the kernel_context, we do not get events while * If we don't modify the kernel_context, we do not get events while
...@@ -1895,7 +1906,6 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream, ...@@ -1895,7 +1906,6 @@ static int gen8_configure_all_contexts(struct i915_perf_stream *stream,
*/ */
for_each_uabi_engine(engine, i915) { for_each_uabi_engine(engine, i915) {
struct intel_context *ce = engine->kernel_context; struct intel_context *ce = engine->kernel_context;
int err;
if (engine->class != RENDER_CLASS) if (engine->class != RENDER_CLASS)
continue; continue;
......
...@@ -176,16 +176,12 @@ i915_l3_read(struct file *filp, struct kobject *kobj, ...@@ -176,16 +176,12 @@ i915_l3_read(struct file *filp, struct kobject *kobj,
count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count); count = min_t(size_t, GEN7_L3LOG_SIZE - offset, count);
memset(buf, 0, count); memset(buf, 0, count);
ret = i915_mutex_lock_interruptible(&i915->drm); spin_lock(&i915->gem.contexts.lock);
if (ret)
return ret;
if (i915->l3_parity.remap_info[slice]) if (i915->l3_parity.remap_info[slice])
memcpy(buf, memcpy(buf,
i915->l3_parity.remap_info[slice] + offset / sizeof(u32), i915->l3_parity.remap_info[slice] + offset / sizeof(u32),
count); count);
spin_unlock(&i915->gem.contexts.lock);
mutex_unlock(&i915->drm.struct_mutex);
return count; return count;
} }
...@@ -198,8 +194,8 @@ i915_l3_write(struct file *filp, struct kobject *kobj, ...@@ -198,8 +194,8 @@ i915_l3_write(struct file *filp, struct kobject *kobj,
struct device *kdev = kobj_to_dev(kobj); struct device *kdev = kobj_to_dev(kobj);
struct drm_i915_private *i915 = kdev_minor_to_i915(kdev); struct drm_i915_private *i915 = kdev_minor_to_i915(kdev);
int slice = (int)(uintptr_t)attr->private; int slice = (int)(uintptr_t)attr->private;
u32 *remap_info, *freeme = NULL;
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
u32 **remap_info;
int ret; int ret;
ret = l3_access_valid(i915, offset); ret = l3_access_valid(i915, offset);
...@@ -209,37 +205,36 @@ i915_l3_write(struct file *filp, struct kobject *kobj, ...@@ -209,37 +205,36 @@ i915_l3_write(struct file *filp, struct kobject *kobj,
if (count < sizeof(u32)) if (count < sizeof(u32))
return -EINVAL; return -EINVAL;
ret = i915_mutex_lock_interruptible(&i915->drm); remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL);
if (ret) if (!remap_info)
return ret; return -ENOMEM;
remap_info = &i915->l3_parity.remap_info[slice]; spin_lock(&i915->gem.contexts.lock);
if (!*remap_info) {
*remap_info = kzalloc(GEN7_L3LOG_SIZE, GFP_KERNEL); if (i915->l3_parity.remap_info[slice]) {
if (!*remap_info) { freeme = remap_info;
ret = -ENOMEM; remap_info = i915->l3_parity.remap_info[slice];
goto out; } else {
} i915->l3_parity.remap_info[slice] = remap_info;
} }
count = round_down(count, sizeof(u32)); count = round_down(count, sizeof(u32));
memcpy(*remap_info + offset / sizeof(u32), buf, count); memcpy(remap_info + offset / sizeof(u32), buf, count);
/* NB: We defer the remapping until we switch to the context */ /* NB: We defer the remapping until we switch to the context */
list_for_each_entry(ctx, &i915->contexts.list, link) list_for_each_entry(ctx, &i915->gem.contexts.list, link)
ctx->remap_slice |= BIT(slice); ctx->remap_slice |= BIT(slice);
spin_unlock(&i915->gem.contexts.lock);
kfree(freeme);
/* /*
* TODO: Ideally we really want a GPU reset here to make sure errors * TODO: Ideally we really want a GPU reset here to make sure errors
* aren't propagated. Since I cannot find a stable way to reset the GPU * aren't propagated. Since I cannot find a stable way to reset the GPU
* at this point it is left as a TODO. * at this point it is left as a TODO.
*/ */
ret = count; return count;
out:
mutex_unlock(&i915->drm.struct_mutex);
return ret;
} }
static const struct bin_attribute dpf_attrs = { static const struct bin_attribute dpf_attrs = {
......
...@@ -952,7 +952,7 @@ DECLARE_EVENT_CLASS(i915_context, ...@@ -952,7 +952,7 @@ DECLARE_EVENT_CLASS(i915_context,
TP_fast_assign( TP_fast_assign(
__entry->dev = ctx->i915->drm.primary->index; __entry->dev = ctx->i915->drm.primary->index;
__entry->ctx = ctx; __entry->ctx = ctx;
__entry->vm = ctx->vm; __entry->vm = rcu_access_pointer(ctx->vm);
), ),
TP_printk("dev=%u, ctx=%p, ctx_vm=%p", TP_printk("dev=%u, ctx=%p, ctx_vm=%p",
......
...@@ -138,11 +138,9 @@ static int igt_gem_suspend(void *arg) ...@@ -138,11 +138,9 @@ static int igt_gem_suspend(void *arg)
return PTR_ERR(file); return PTR_ERR(file);
err = -ENOMEM; err = -ENOMEM;
mutex_lock(&i915->drm.struct_mutex);
ctx = live_context(i915, file); ctx = live_context(i915, file);
if (!IS_ERR(ctx)) if (!IS_ERR(ctx))
err = switch_to_context(i915, ctx); err = switch_to_context(i915, ctx);
mutex_unlock(&i915->drm.struct_mutex);
if (err) if (err)
goto out; goto out;
...@@ -157,9 +155,7 @@ static int igt_gem_suspend(void *arg) ...@@ -157,9 +155,7 @@ static int igt_gem_suspend(void *arg)
pm_resume(i915); pm_resume(i915);
mutex_lock(&i915->drm.struct_mutex);
err = switch_to_context(i915, ctx); err = switch_to_context(i915, ctx);
mutex_unlock(&i915->drm.struct_mutex);
out: out:
mock_file_free(i915, file); mock_file_free(i915, file);
return err; return err;
...@@ -177,11 +173,9 @@ static int igt_gem_hibernate(void *arg) ...@@ -177,11 +173,9 @@ static int igt_gem_hibernate(void *arg)
return PTR_ERR(file); return PTR_ERR(file);
err = -ENOMEM; err = -ENOMEM;
mutex_lock(&i915->drm.struct_mutex);
ctx = live_context(i915, file); ctx = live_context(i915, file);
if (!IS_ERR(ctx)) if (!IS_ERR(ctx))
err = switch_to_context(i915, ctx); err = switch_to_context(i915, ctx);
mutex_unlock(&i915->drm.struct_mutex);
if (err) if (err)
goto out; goto out;
...@@ -196,9 +190,7 @@ static int igt_gem_hibernate(void *arg) ...@@ -196,9 +190,7 @@ static int igt_gem_hibernate(void *arg)
pm_resume(i915); pm_resume(i915);
mutex_lock(&i915->drm.struct_mutex);
err = switch_to_context(i915, ctx); err = switch_to_context(i915, ctx);
mutex_unlock(&i915->drm.struct_mutex);
out: out:
mock_file_free(i915, file); mock_file_free(i915, file);
return err; return err;
......
...@@ -473,7 +473,6 @@ static int igt_evict_contexts(void *arg) ...@@ -473,7 +473,6 @@ static int igt_evict_contexts(void *arg)
} }
count = 0; count = 0;
mutex_lock(&i915->drm.struct_mutex);
onstack_fence_init(&fence); onstack_fence_init(&fence);
do { do {
struct i915_request *rq; struct i915_request *rq;
...@@ -510,8 +509,6 @@ static int igt_evict_contexts(void *arg) ...@@ -510,8 +509,6 @@ static int igt_evict_contexts(void *arg)
count++; count++;
err = 0; err = 0;
} while(1); } while(1);
mutex_unlock(&i915->drm.struct_mutex);
onstack_fence_fini(&fence); onstack_fence_fini(&fence);
pr_info("Submitted %lu contexts/requests on %s\n", pr_info("Submitted %lu contexts/requests on %s\n",
count, engine->name); count, engine->name);
......
...@@ -1246,6 +1246,7 @@ static int exercise_mock(struct drm_i915_private *i915, ...@@ -1246,6 +1246,7 @@ static int exercise_mock(struct drm_i915_private *i915,
unsigned long end_time)) unsigned long end_time))
{ {
const u64 limit = totalram_pages() << PAGE_SHIFT; const u64 limit = totalram_pages() << PAGE_SHIFT;
struct i915_address_space *vm;
struct i915_gem_context *ctx; struct i915_gem_context *ctx;
IGT_TIMEOUT(end_time); IGT_TIMEOUT(end_time);
int err; int err;
...@@ -1254,7 +1255,9 @@ static int exercise_mock(struct drm_i915_private *i915, ...@@ -1254,7 +1255,9 @@ static int exercise_mock(struct drm_i915_private *i915,
if (!ctx) if (!ctx)
return -ENOMEM; return -ENOMEM;
err = func(i915, ctx->vm, 0, min(ctx->vm->total, limit), end_time); vm = i915_gem_context_get_vm_rcu(ctx);
err = func(i915, vm, 0, min(vm->total, limit), end_time);
i915_vm_put(vm);
mock_context_close(ctx); mock_context_close(ctx);
return err; return err;
...@@ -1801,15 +1804,15 @@ static int igt_cs_tlb(void *arg) ...@@ -1801,15 +1804,15 @@ static int igt_cs_tlb(void *arg)
goto out_unlock; goto out_unlock;
} }
vm = ctx->vm; vm = i915_gem_context_get_vm_rcu(ctx);
if (!vm) if (i915_is_ggtt(vm))
goto out_unlock; goto out_vm;
/* Create two pages; dummy we prefill the TLB, and intended */ /* Create two pages; dummy we prefill the TLB, and intended */
bbe = i915_gem_object_create_internal(i915, PAGE_SIZE); bbe = i915_gem_object_create_internal(i915, PAGE_SIZE);
if (IS_ERR(bbe)) { if (IS_ERR(bbe)) {
err = PTR_ERR(bbe); err = PTR_ERR(bbe);
goto out_unlock; goto out_vm;
} }
batch = i915_gem_object_pin_map(bbe, I915_MAP_WC); batch = i915_gem_object_pin_map(bbe, I915_MAP_WC);
...@@ -2014,6 +2017,8 @@ static int igt_cs_tlb(void *arg) ...@@ -2014,6 +2017,8 @@ static int igt_cs_tlb(void *arg)
i915_gem_object_put(act); i915_gem_object_put(act);
out_put_bbe: out_put_bbe:
i915_gem_object_put(bbe); i915_gem_object_put(bbe);
out_vm:
i915_vm_put(vm);
out_unlock: out_unlock:
mutex_unlock(&i915->drm.struct_mutex); mutex_unlock(&i915->drm.struct_mutex);
mock_file_free(i915, file); mock_file_free(i915, file);
......
...@@ -181,9 +181,7 @@ static int igt_request_rewind(void *arg) ...@@ -181,9 +181,7 @@ static int igt_request_rewind(void *arg)
struct intel_context *ce; struct intel_context *ce;
int err = -EINVAL; int err = -EINVAL;
mutex_lock(&i915->drm.struct_mutex);
ctx[0] = mock_context(i915, "A"); ctx[0] = mock_context(i915, "A");
mutex_unlock(&i915->drm.struct_mutex);
ce = i915_gem_context_get_engine(ctx[0], RCS0); ce = i915_gem_context_get_engine(ctx[0], RCS0);
GEM_BUG_ON(IS_ERR(ce)); GEM_BUG_ON(IS_ERR(ce));
...@@ -197,9 +195,7 @@ static int igt_request_rewind(void *arg) ...@@ -197,9 +195,7 @@ static int igt_request_rewind(void *arg)
i915_request_get(request); i915_request_get(request);
i915_request_add(request); i915_request_add(request);
mutex_lock(&i915->drm.struct_mutex);
ctx[1] = mock_context(i915, "B"); ctx[1] = mock_context(i915, "B");
mutex_unlock(&i915->drm.struct_mutex);
ce = i915_gem_context_get_engine(ctx[1], RCS0); ce = i915_gem_context_get_engine(ctx[1], RCS0);
GEM_BUG_ON(IS_ERR(ce)); GEM_BUG_ON(IS_ERR(ce));
...@@ -438,9 +434,7 @@ static int mock_breadcrumbs_smoketest(void *arg) ...@@ -438,9 +434,7 @@ static int mock_breadcrumbs_smoketest(void *arg)
} }
for (n = 0; n < t.ncontexts; n++) { for (n = 0; n < t.ncontexts; n++) {
mutex_lock(&t.engine->i915->drm.struct_mutex);
t.contexts[n] = mock_context(t.engine->i915, "mock"); t.contexts[n] = mock_context(t.engine->i915, "mock");
mutex_unlock(&t.engine->i915->drm.struct_mutex);
if (!t.contexts[n]) { if (!t.contexts[n]) {
ret = -ENOMEM; ret = -ENOMEM;
goto out_contexts; goto out_contexts;
...@@ -734,9 +728,9 @@ static int live_empty_request(void *arg) ...@@ -734,9 +728,9 @@ static int live_empty_request(void *arg)
static struct i915_vma *recursive_batch(struct drm_i915_private *i915) static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
{ {
struct i915_gem_context *ctx = i915->kernel_context; struct i915_gem_context *ctx = i915->kernel_context;
struct i915_address_space *vm = ctx->vm ?: &i915->ggtt.vm;
struct drm_i915_gem_object *obj; struct drm_i915_gem_object *obj;
const int gen = INTEL_GEN(i915); const int gen = INTEL_GEN(i915);
struct i915_address_space *vm;
struct i915_vma *vma; struct i915_vma *vma;
u32 *cmd; u32 *cmd;
int err; int err;
...@@ -745,7 +739,9 @@ static struct i915_vma *recursive_batch(struct drm_i915_private *i915) ...@@ -745,7 +739,9 @@ static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
if (IS_ERR(obj)) if (IS_ERR(obj))
return ERR_CAST(obj); return ERR_CAST(obj);
vm = i915_gem_context_get_vm_rcu(ctx);
vma = i915_vma_instance(obj, vm, NULL); vma = i915_vma_instance(obj, vm, NULL);
i915_vm_put(vm);
if (IS_ERR(vma)) { if (IS_ERR(vma)) {
err = PTR_ERR(vma); err = PTR_ERR(vma);
goto err; goto err;
...@@ -1220,9 +1216,7 @@ static int live_breadcrumbs_smoketest(void *arg) ...@@ -1220,9 +1216,7 @@ static int live_breadcrumbs_smoketest(void *arg)
} }
for (n = 0; n < t[0].ncontexts; n++) { for (n = 0; n < t[0].ncontexts; n++) {
mutex_lock(&i915->drm.struct_mutex);
t[0].contexts[n] = live_context(i915, file); t[0].contexts[n] = live_context(i915, file);
mutex_unlock(&i915->drm.struct_mutex);
if (!t[0].contexts[n]) { if (!t[0].contexts[n]) {
ret = -ENOMEM; ret = -ENOMEM;
goto out_contexts; goto out_contexts;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/prime_numbers.h> #include <linux/prime_numbers.h>
#include "gem/i915_gem_context.h"
#include "gem/selftests/mock_context.h" #include "gem/selftests/mock_context.h"
#include "i915_scatterlist.h" #include "i915_scatterlist.h"
...@@ -38,7 +39,7 @@ static bool assert_vma(struct i915_vma *vma, ...@@ -38,7 +39,7 @@ static bool assert_vma(struct i915_vma *vma,
{ {
bool ok = true; bool ok = true;
if (vma->vm != ctx->vm) { if (vma->vm != rcu_access_pointer(ctx->vm)) {
pr_err("VMA created with wrong VM\n"); pr_err("VMA created with wrong VM\n");
ok = false; ok = false;
} }
...@@ -113,11 +114,13 @@ static int create_vmas(struct drm_i915_private *i915, ...@@ -113,11 +114,13 @@ static int create_vmas(struct drm_i915_private *i915,
list_for_each_entry(obj, objects, st_link) { list_for_each_entry(obj, objects, st_link) {
for (pinned = 0; pinned <= 1; pinned++) { for (pinned = 0; pinned <= 1; pinned++) {
list_for_each_entry(ctx, contexts, link) { list_for_each_entry(ctx, contexts, link) {
struct i915_address_space *vm = ctx->vm; struct i915_address_space *vm;
struct i915_vma *vma; struct i915_vma *vma;
int err; int err;
vm = i915_gem_context_get_vm_rcu(ctx);
vma = checked_vma_instance(obj, vm, NULL); vma = checked_vma_instance(obj, vm, NULL);
i915_vm_put(vm);
if (IS_ERR(vma)) if (IS_ERR(vma))
return PTR_ERR(vma); return PTR_ERR(vma);
......
...@@ -59,11 +59,9 @@ static void mock_device_release(struct drm_device *dev) ...@@ -59,11 +59,9 @@ static void mock_device_release(struct drm_device *dev)
i915_gem_drain_workqueue(i915); i915_gem_drain_workqueue(i915);
mutex_lock(&i915->drm.struct_mutex);
for_each_engine(engine, i915, id) for_each_engine(engine, i915, id)
mock_engine_free(engine); mock_engine_free(engine);
i915_gem_contexts_fini(i915); i915_gem_driver_release__contexts(i915);
mutex_unlock(&i915->drm.struct_mutex);
intel_timelines_fini(i915); intel_timelines_fini(i915);
...@@ -206,7 +204,7 @@ struct drm_i915_private *mock_gem_device(void) ...@@ -206,7 +204,7 @@ struct drm_i915_private *mock_gem_device(void)
return i915; return i915;
err_context: err_context:
i915_gem_contexts_fini(i915); i915_gem_driver_release__contexts(i915);
err_engine: err_engine:
mock_engine_free(i915->engine[RCS0]); mock_engine_free(i915->engine[RCS0]);
err_unlock: err_unlock:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment