Commit 37c54f9b authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Linus Torvalds

kernel: set USER_DS in kthread_use_mm

Some architectures like arm64 and s390 require USER_DS to be set for
kernel threads to access user address space, which is the whole purpose of
kthread_use_mm, but other like x86 don't.  That has lead to a huge mess
where some callers are fixed up once they are tested on said
architectures, while others linger around and yet other like io_uring try
to do "clever" optimizations for what usually is just a trivial asignment
to a member in the thread_struct for most architectures.

Make kthread_use_mm set USER_DS, and kthread_unuse_mm restore to the
previous value instead.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Tested-by: default avatarJens Axboe <axboe@kernel.dk>
Reviewed-by: default avatarJens Axboe <axboe@kernel.dk>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Felipe Balbi <balbi@kernel.org>
Cc: Felix Kuehling <Felix.Kuehling@amd.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Zhenyu Wang <zhenyuw@linux.intel.com>
Cc: Zhi Wang <zhi.a.wang@intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: http://lkml.kernel.org/r/20200404094101.672954-7-hch@lst.deSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent f5678e7f
...@@ -824,13 +824,9 @@ static void ffs_user_copy_worker(struct work_struct *work) ...@@ -824,13 +824,9 @@ static void ffs_user_copy_worker(struct work_struct *work)
bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD; bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
if (io_data->read && ret > 0) { if (io_data->read && ret > 0) {
mm_segment_t oldfs = get_fs();
set_fs(USER_DS);
kthread_use_mm(io_data->mm); kthread_use_mm(io_data->mm);
ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data); ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
kthread_unuse_mm(io_data->mm); kthread_unuse_mm(io_data->mm);
set_fs(oldfs);
} }
io_data->kiocb->ki_complete(io_data->kiocb, ret, ret); io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
......
...@@ -329,9 +329,7 @@ static int vhost_worker(void *data) ...@@ -329,9 +329,7 @@ static int vhost_worker(void *data)
struct vhost_dev *dev = data; struct vhost_dev *dev = data;
struct vhost_work *work, *work_next; struct vhost_work *work, *work_next;
struct llist_node *node; struct llist_node *node;
mm_segment_t oldfs = get_fs();
set_fs(USER_DS);
kthread_use_mm(dev->mm); kthread_use_mm(dev->mm);
for (;;) { for (;;) {
...@@ -361,7 +359,6 @@ static int vhost_worker(void *data) ...@@ -361,7 +359,6 @@ static int vhost_worker(void *data)
} }
} }
kthread_unuse_mm(dev->mm); kthread_unuse_mm(dev->mm);
set_fs(oldfs);
return 0; return 0;
} }
......
...@@ -169,7 +169,6 @@ static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker) ...@@ -169,7 +169,6 @@ static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
dropped_lock = true; dropped_lock = true;
} }
__set_current_state(TASK_RUNNING); __set_current_state(TASK_RUNNING);
set_fs(KERNEL_DS);
kthread_unuse_mm(worker->mm); kthread_unuse_mm(worker->mm);
mmput(worker->mm); mmput(worker->mm);
worker->mm = NULL; worker->mm = NULL;
...@@ -421,14 +420,11 @@ static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work) ...@@ -421,14 +420,11 @@ static void io_wq_switch_mm(struct io_worker *worker, struct io_wq_work *work)
mmput(worker->mm); mmput(worker->mm);
worker->mm = NULL; worker->mm = NULL;
} }
if (!work->mm) { if (!work->mm)
set_fs(KERNEL_DS);
return; return;
}
if (mmget_not_zero(work->mm)) { if (mmget_not_zero(work->mm)) {
kthread_use_mm(work->mm); kthread_use_mm(work->mm);
if (!worker->mm)
set_fs(USER_DS);
worker->mm = work->mm; worker->mm = work->mm;
/* hang on to this mm */ /* hang on to this mm */
work->mm = NULL; work->mm = NULL;
......
...@@ -5989,15 +5989,12 @@ static int io_sq_thread(void *data) ...@@ -5989,15 +5989,12 @@ static int io_sq_thread(void *data)
{ {
struct io_ring_ctx *ctx = data; struct io_ring_ctx *ctx = data;
const struct cred *old_cred; const struct cred *old_cred;
mm_segment_t old_fs;
DEFINE_WAIT(wait); DEFINE_WAIT(wait);
unsigned long timeout; unsigned long timeout;
int ret = 0; int ret = 0;
complete(&ctx->sq_thread_comp); complete(&ctx->sq_thread_comp);
old_fs = get_fs();
set_fs(USER_DS);
old_cred = override_creds(ctx->creds); old_cred = override_creds(ctx->creds);
timeout = jiffies + ctx->sq_thread_idle; timeout = jiffies + ctx->sq_thread_idle;
...@@ -6102,7 +6099,6 @@ static int io_sq_thread(void *data) ...@@ -6102,7 +6099,6 @@ static int io_sq_thread(void *data)
if (current->task_works) if (current->task_works)
task_work_run(); task_work_run();
set_fs(old_fs);
io_sq_thread_drop_mm(ctx); io_sq_thread_drop_mm(ctx);
revert_creds(old_cred); revert_creds(old_cred);
......
...@@ -52,6 +52,7 @@ struct kthread { ...@@ -52,6 +52,7 @@ struct kthread {
unsigned long flags; unsigned long flags;
unsigned int cpu; unsigned int cpu;
void *data; void *data;
mm_segment_t oldfs;
struct completion parked; struct completion parked;
struct completion exited; struct completion exited;
#ifdef CONFIG_BLK_CGROUP #ifdef CONFIG_BLK_CGROUP
...@@ -1235,6 +1236,9 @@ void kthread_use_mm(struct mm_struct *mm) ...@@ -1235,6 +1236,9 @@ void kthread_use_mm(struct mm_struct *mm)
if (active_mm != mm) if (active_mm != mm)
mmdrop(active_mm); mmdrop(active_mm);
to_kthread(tsk)->oldfs = get_fs();
set_fs(USER_DS);
} }
EXPORT_SYMBOL_GPL(kthread_use_mm); EXPORT_SYMBOL_GPL(kthread_use_mm);
...@@ -1249,6 +1253,8 @@ void kthread_unuse_mm(struct mm_struct *mm) ...@@ -1249,6 +1253,8 @@ void kthread_unuse_mm(struct mm_struct *mm)
WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD));
WARN_ON_ONCE(!tsk->mm); WARN_ON_ONCE(!tsk->mm);
set_fs(to_kthread(tsk)->oldfs);
task_lock(tsk); task_lock(tsk);
sync_mm_rss(mm); sync_mm_rss(mm);
tsk->mm = NULL; tsk->mm = NULL;
......
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