io_uring: Fix XArray usage in io_uring_add_task_file
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 9 Oct 2020 12:49:52 +0000 (13:49 +0100)
committerJens Axboe <axboe@kernel.dk>
Fri, 9 Oct 2020 14:59:40 +0000 (08:59 -0600)
The xas_store() wasn't paired with an xas_nomem() loop, so if it couldn't
allocate memory using GFP_NOWAIT, it would leak the reference to the file
descriptor.  Also the node pointed to by the xas could be freed between
the call to xas_load() under the rcu_read_lock() and the acquisition of
the xa_lock.

It's easier to just use the normal xa_load/xa_store interface here.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
[axboe: fix missing assign after alloc, cur_uring -> tctx rename]
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/io_uring.c

index 2978cc7..13fc60a 100644 (file)
@@ -8586,27 +8586,24 @@ static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
  */
 static int io_uring_add_task_file(struct file *file)
 {
-       if (unlikely(!current->io_uring)) {
+       struct io_uring_task *tctx = current->io_uring;
+
+       if (unlikely(!tctx)) {
                int ret;
 
                ret = io_uring_alloc_task_context(current);
                if (unlikely(ret))
                        return ret;
+               tctx = current->io_uring;
        }
-       if (current->io_uring->last != file) {
-               XA_STATE(xas, &current->io_uring->xa, (unsigned long) file);
-               void *old;
+       if (tctx->last != file) {
+               void *old = xa_load(&tctx->xa, (unsigned long)file);
 
-               rcu_read_lock();
-               old = xas_load(&xas);
-               if (old != file) {
+               if (!old) {
                        get_file(file);
-                       xas_lock(&xas);
-                       xas_store(&xas, file);
-                       xas_unlock(&xas);
+                       xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL);
                }
-               rcu_read_unlock();
-               current->io_uring->last = file;
+               tctx->last = file;
        }
 
        return 0;