Skip to content

Commit f5678e7

Browse files
Christoph Hellwigtorvalds
authored andcommitted
kernel: better document the use_mm/unuse_mm API contract
Switch the function documentation to kerneldoc comments, and add WARN_ON_ONCE asserts that the calling thread is a kernel thread and does not have ->mm set (or has ->mm set in the case of unuse_mm). Also give the functions a kthread_ prefix to better document the use case. [[email protected]: fix a comment typo, cover the newly merged use_mm/unuse_mm caller in vfio] Link: http://lkml.kernel.org/r/[email protected] [[email protected]: powerpc/vas: fix up for {un}use_mm() rename] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Christoph Hellwig <[email protected]> Signed-off-by: Stephen Rothwell <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Tested-by: Jens Axboe <[email protected]> Reviewed-by: Jens Axboe <[email protected]> Acked-by: Felix Kuehling <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> [usb] Acked-by: Haren Myneni <[email protected]> Cc: Alex Deucher <[email protected]> Cc: Al Viro <[email protected]> Cc: Felipe Balbi <[email protected]> Cc: Jason Wang <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Zhenyu Wang <[email protected]> Cc: Zhi Wang <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4dbe59a commit f5678e7

File tree

12 files changed

+40
-41
lines changed

12 files changed

+40
-41
lines changed

arch/powerpc/platforms/powernv/vas-fault.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static void update_csb(struct vas_window *window,
127127
return;
128128
}
129129

130-
use_mm(window->mm);
130+
kthread_use_mm(window->mm);
131131
rc = copy_to_user(csb_addr, &csb, sizeof(csb));
132132
/*
133133
* User space polls on csb.flags (first byte). So add barrier
@@ -139,7 +139,7 @@ static void update_csb(struct vas_window *window,
139139
smp_mb();
140140
rc = copy_to_user(csb_addr, &csb, sizeof(u8));
141141
}
142-
unuse_mm(window->mm);
142+
kthread_unuse_mm(window->mm);
143143
put_task_struct(tsk);
144144

145145
/* Success */

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ uint8_t amdgpu_amdkfd_get_xgmi_hops_count(struct kgd_dev *dst, struct kgd_dev *s
197197
if ((mmptr) == current->mm) { \
198198
valid = !get_user((dst), (wptr)); \
199199
} else if (current->mm == NULL) { \
200-
use_mm(mmptr); \
200+
kthread_use_mm(mmptr); \
201201
valid = !get_user((dst), (wptr)); \
202-
unuse_mm(mmptr); \
202+
kthread_unuse_mm(mmptr); \
203203
} \
204204
pagefault_enable(); \
205205
} \

drivers/usb/gadget/function/f_fs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,9 @@ static void ffs_user_copy_worker(struct work_struct *work)
827827
mm_segment_t oldfs = get_fs();
828828

829829
set_fs(USER_DS);
830-
use_mm(io_data->mm);
830+
kthread_use_mm(io_data->mm);
831831
ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
832-
unuse_mm(io_data->mm);
832+
kthread_unuse_mm(io_data->mm);
833833
set_fs(oldfs);
834834
}
835835

drivers/usb/gadget/legacy/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ static void ep_user_copy_worker(struct work_struct *work)
462462
struct kiocb *iocb = priv->iocb;
463463
size_t ret;
464464

465-
use_mm(mm);
465+
kthread_use_mm(mm);
466466
ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
467-
unuse_mm(mm);
467+
kthread_unuse_mm(mm);
468468
if (!ret)
469469
ret = -EFAULT;
470470

drivers/vfio/vfio_iommu_type1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,7 +2817,7 @@ static int vfio_iommu_type1_dma_rw_chunk(struct vfio_iommu *iommu,
28172817
return -EPERM;
28182818

28192819
if (kthread)
2820-
use_mm(mm);
2820+
kthread_use_mm(mm);
28212821
else if (current->mm != mm)
28222822
goto out;
28232823

@@ -2844,7 +2844,7 @@ static int vfio_iommu_type1_dma_rw_chunk(struct vfio_iommu *iommu,
28442844
*copied = copy_from_user(data, (void __user *)vaddr,
28452845
count) ? 0 : count;
28462846
if (kthread)
2847-
unuse_mm(mm);
2847+
kthread_unuse_mm(mm);
28482848
out:
28492849
mmput(mm);
28502850
return *copied ? 0 : -EFAULT;

drivers/vhost/vhost.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static int vhost_worker(void *data)
332332
mm_segment_t oldfs = get_fs();
333333

334334
set_fs(USER_DS);
335-
use_mm(dev->mm);
335+
kthread_use_mm(dev->mm);
336336

337337
for (;;) {
338338
/* mb paired w/ kthread_stop */
@@ -360,7 +360,7 @@ static int vhost_worker(void *data)
360360
schedule();
361361
}
362362
}
363-
unuse_mm(dev->mm);
363+
kthread_unuse_mm(dev->mm);
364364
set_fs(oldfs);
365365
return 0;
366366
}

fs/io-wq.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
170170
}
171171
__set_current_state(TASK_RUNNING);
172172
set_fs(KERNEL_DS);
173-
unuse_mm(worker->mm);
173+
kthread_unuse_mm(worker->mm);
174174
mmput(worker->mm);
175175
worker->mm = NULL;
176176
}
@@ -417,7 +417,7 @@ static struct io_wq_work *io_get_next_work(struct io_wqe *wqe)
417417
static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
418418
{
419419
if (worker->mm) {
420-
unuse_mm(worker->mm);
420+
kthread_unuse_mm(worker->mm);
421421
mmput(worker->mm);
422422
worker->mm = NULL;
423423
}
@@ -426,7 +426,7 @@ static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
426426
return;
427427
}
428428
if (mmget_not_zero(work->mm)) {
429-
use_mm(work->mm);
429+
kthread_use_mm(work->mm);
430430
if (!worker->mm)
431431
set_fs(USER_DS);
432432
worker->mm = work->mm;

fs/io_uring.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5866,7 +5866,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
58665866
if (io_op_defs[req->opcode].needs_mm && !current->mm) {
58675867
if (unlikely(!mmget_not_zero(ctx->sqo_mm)))
58685868
return -EFAULT;
5869-
use_mm(ctx->sqo_mm);
5869+
kthread_use_mm(ctx->sqo_mm);
58705870
}
58715871

58725872
sqe_flags = READ_ONCE(sqe->flags);
@@ -5980,7 +5980,7 @@ static inline void io_sq_thread_drop_mm(struct io_ring_ctx *ctx)
59805980
struct mm_struct *mm = current->mm;
59815981

59825982
if (mm) {
5983-
unuse_mm(mm);
5983+
kthread_unuse_mm(mm);
59845984
mmput(mm);
59855985
}
59865986
}

include/linux/kthread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
200200

201201
void kthread_destroy_worker(struct kthread_worker *worker);
202202

203-
void use_mm(struct mm_struct *mm);
204-
void unuse_mm(struct mm_struct *mm);
203+
void kthread_use_mm(struct mm_struct *mm);
204+
void kthread_unuse_mm(struct mm_struct *mm);
205205

206206
struct cgroup_subsys_state;
207207

kernel/kthread.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,18 +1208,18 @@ void kthread_destroy_worker(struct kthread_worker *worker)
12081208
}
12091209
EXPORT_SYMBOL(kthread_destroy_worker);
12101210

1211-
/*
1212-
* use_mm
1213-
* Makes the calling kernel thread take on the specified
1214-
* mm context.
1215-
* (Note: this routine is intended to be called only
1216-
* from a kernel thread context)
1211+
/**
1212+
* kthread_use_mm - make the calling kthread operate on an address space
1213+
* @mm: address space to operate on
12171214
*/
1218-
void use_mm(struct mm_struct *mm)
1215+
void kthread_use_mm(struct mm_struct *mm)
12191216
{
12201217
struct mm_struct *active_mm;
12211218
struct task_struct *tsk = current;
12221219

1220+
WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1221+
WARN_ON_ONCE(tsk->mm);
1222+
12231223
task_lock(tsk);
12241224
active_mm = tsk->active_mm;
12251225
if (active_mm != mm) {
@@ -1236,28 +1236,27 @@ void use_mm(struct mm_struct *mm)
12361236
if (active_mm != mm)
12371237
mmdrop(active_mm);
12381238
}
1239-
EXPORT_SYMBOL_GPL(use_mm);
1239+
EXPORT_SYMBOL_GPL(kthread_use_mm);
12401240

1241-
/*
1242-
* unuse_mm
1243-
* Reverses the effect of use_mm, i.e. releases the
1244-
* specified mm context which was earlier taken on
1245-
* by the calling kernel thread
1246-
* (Note: this routine is intended to be called only
1247-
* from a kernel thread context)
1241+
/**
1242+
* kthread_unuse_mm - reverse the effect of kthread_use_mm()
1243+
* @mm: address space to operate on
12481244
*/
1249-
void unuse_mm(struct mm_struct *mm)
1245+
void kthread_unuse_mm(struct mm_struct *mm)
12501246
{
12511247
struct task_struct *tsk = current;
12521248

1249+
WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
1250+
WARN_ON_ONCE(!tsk->mm);
1251+
12531252
task_lock(tsk);
12541253
sync_mm_rss(mm);
12551254
tsk->mm = NULL;
12561255
/* active_mm is still 'mm' */
12571256
enter_lazy_tlb(mm, tsk);
12581257
task_unlock(tsk);
12591258
}
1260-
EXPORT_SYMBOL_GPL(unuse_mm);
1259+
EXPORT_SYMBOL_GPL(kthread_unuse_mm);
12611260

12621261
#ifdef CONFIG_BLK_CGROUP
12631262
/**

0 commit comments

Comments
 (0)