Skip to content

Commit ee41d11

Browse files
Ian Munsiempe
authored andcommitted
cxl: Change contexts_lock to a mutex to fix sleep while atomic bug
We had a known sleep while atomic bug if a CXL device was forcefully unbound while it was in use. This could occur as a result of EEH, or manually induced with something like this while the device was in use: echo 0000:01:00.0 > /sys/bus/pci/drivers/cxl-pci/unbind The issue was that in this code path we iterated over each context and forcefully detached it with the contexts_lock spin lock held, however the detach also needed to take the spu_mutex, and call schedule. This patch changes the contexts_lock to a mutex so that we are not in atomic context while doing the detach, thereby avoiding the sleep while atomic. Also delete the related TODO comment, which suggested an alternate solution which turned out to not be workable. Cc: stable@vger.kernel.org Signed-off-by: Ian Munsie <imunsie@au1.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 7c5c92e commit ee41d11

5 files changed

Lines changed: 15 additions & 21 deletions

File tree

‎drivers/misc/cxl/context.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
8282
* Allocating IDR! We better make sure everything's setup that
8383
* dereferences from it.
8484
*/
85+
mutex_lock(&afu->contexts_lock);
8586
idr_preload(GFP_KERNEL);
86-
spin_lock(&afu->contexts_lock);
8787
i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
8888
ctx->afu->num_procs, GFP_NOWAIT);
89-
spin_unlock(&afu->contexts_lock);
9089
idr_preload_end();
90+
mutex_unlock(&afu->contexts_lock);
9191
if (i < 0)
9292
return i;
9393

@@ -168,21 +168,22 @@ void cxl_context_detach_all(struct cxl_afu *afu)
168168
struct cxl_context *ctx;
169169
int tmp;
170170

171-
rcu_read_lock();
172-
idr_for_each_entry(&afu->contexts_idr, ctx, tmp)
171+
mutex_lock(&afu->contexts_lock);
172+
idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
173173
/*
174174
* Anything done in here needs to be setup before the IDR is
175175
* created and torn down after the IDR removed
176176
*/
177177
__detach_context(ctx);
178-
rcu_read_unlock();
178+
}
179+
mutex_unlock(&afu->contexts_lock);
179180
}
180181

181182
void cxl_context_free(struct cxl_context *ctx)
182183
{
183-
spin_lock(&ctx->afu->contexts_lock);
184+
mutex_lock(&ctx->afu->contexts_lock);
184185
idr_remove(&ctx->afu->contexts_idr, ctx->pe);
185-
spin_unlock(&ctx->afu->contexts_lock);
186+
mutex_unlock(&ctx->afu->contexts_lock);
186187
synchronize_rcu();
187188

188189
free_page((u64)ctx->sstp);

‎drivers/misc/cxl/cxl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ struct cxl_afu {
351351
struct device *chardev_s, *chardev_m, *chardev_d;
352352
struct idr contexts_idr;
353353
struct dentry *debugfs;
354-
spinlock_t contexts_lock;
354+
struct mutex contexts_lock;
355355
struct mutex spa_mutex;
356356
spinlock_t afu_cntl_lock;
357357

‎drivers/misc/cxl/native.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,6 @@ static inline int detach_process_native_dedicated(struct cxl_context *ctx)
610610
return 0;
611611
}
612612

613-
/*
614-
* TODO: handle case when this is called inside a rcu_read_lock() which may
615-
* happen when we unbind the driver (ie. cxl_context_detach_all()) . Terminate
616-
* & remove use a mutex lock and schedule which will not good with lock held.
617-
* May need to write do_process_element_cmd() that handles outstanding page
618-
* faults synchronously.
619-
*/
620613
static inline int detach_process_native_afu_directed(struct cxl_context *ctx)
621614
{
622615
if (!ctx->pe_inserted)

‎drivers/misc/cxl/pci.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
502502
afu->dev.release = cxl_release_afu;
503503
afu->slice = slice;
504504
idr_init(&afu->contexts_idr);
505-
spin_lock_init(&afu->contexts_lock);
505+
mutex_init(&afu->contexts_lock);
506506
spin_lock_init(&afu->afu_cntl_lock);
507507
mutex_init(&afu->spa_mutex);
508508

‎drivers/misc/cxl/sysfs.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static ssize_t reset_store_afu(struct device *device,
121121
int rc;
122122

123123
/* Not safe to reset if it is currently in use */
124-
spin_lock(&afu->contexts_lock);
124+
mutex_lock(&afu->contexts_lock);
125125
if (!idr_is_empty(&afu->contexts_idr)) {
126126
rc = -EBUSY;
127127
goto err;
@@ -132,7 +132,7 @@ static ssize_t reset_store_afu(struct device *device,
132132

133133
rc = count;
134134
err:
135-
spin_unlock(&afu->contexts_lock);
135+
mutex_unlock(&afu->contexts_lock);
136136
return rc;
137137
}
138138

@@ -247,7 +247,7 @@ static ssize_t mode_store(struct device *device, struct device_attribute *attr,
247247
int rc = -EBUSY;
248248

249249
/* can't change this if we have a user */
250-
spin_lock(&afu->contexts_lock);
250+
mutex_lock(&afu->contexts_lock);
251251
if (!idr_is_empty(&afu->contexts_idr))
252252
goto err;
253253

@@ -271,7 +271,7 @@ static ssize_t mode_store(struct device *device, struct device_attribute *attr,
271271
afu->current_mode = 0;
272272
afu->num_procs = 0;
273273

274-
spin_unlock(&afu->contexts_lock);
274+
mutex_unlock(&afu->contexts_lock);
275275

276276
if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
277277
return rc;
@@ -280,7 +280,7 @@ static ssize_t mode_store(struct device *device, struct device_attribute *attr,
280280

281281
return count;
282282
err:
283-
spin_unlock(&afu->contexts_lock);
283+
mutex_unlock(&afu->contexts_lock);
284284
return rc;
285285
}
286286

0 commit comments

Comments
 (0)