/dev/mem: nowait zero/null ops
authorPavel Begunkov <asml.silence@gmail.com>
Wed, 8 Sep 2021 19:10:38 +0000 (20:10 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 14 Sep 2021 08:46:19 +0000 (10:46 +0200)
Make read_iter_zero() to honor IOCB_NOWAIT, so /dev/zero can be
advertised as FMODE_NOWAIT. It's useful for io_uring, which needs it to
apply certain optimisations when doing I/O against the device.

Set FMODE_NOWAIT for /dev/null as well, it never waits and therefore
trivially meets the criteria.

Reviewed-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/f11090f97ddc2b2ce49ea1211258658ddfbc5563.1631127867.git.asml.silence@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/mem.c

index 1c596b5cdb279e594c84e3068447bcf661a46928..cc296f0823bd0bcee8a9d4e2cfa10a6c385eb9b0 100644 (file)
@@ -495,6 +495,10 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
                written += n;
                if (signal_pending(current))
                        return written ? written : -ERESTARTSYS;
+               if (!need_resched())
+                       continue;
+               if (iocb->ki_flags & IOCB_NOWAIT)
+                       return written ? written : -EAGAIN;
                cond_resched();
        }
        return written;
@@ -696,11 +700,11 @@ static const struct memdev {
 #ifdef CONFIG_DEVMEM
         [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
 #endif
-        [3] = { "null", 0666, &null_fops, 0 },
+        [3] = { "null", 0666, &null_fops, FMODE_NOWAIT },
 #ifdef CONFIG_DEVPORT
         [4] = { "port", 0, &port_fops, 0 },
 #endif
-        [5] = { "zero", 0666, &zero_fops, 0 },
+        [5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT },
         [7] = { "full", 0666, &full_fops, 0 },
         [8] = { "random", 0666, &random_fops, 0 },
         [9] = { "urandom", 0666, &urandom_fops, 0 },