Skip to content

Commit d949799

Browse files
committed
timerfd: convert to ->read_iter()
Switch timerfd to using fops->read_iter(), so it can support not just O_NONBLOCK but IOCB_NOWAIT as well. With the latter, users like io_uring interact with timerfds a lot better, as they can be driven purely by the poll trigger. Manually get and install the required fd, so that FMODE_NOWAIT can be set before the file is installed into the file table. No functional changes intended in this patch, it's purely a straight conversion to using the read iterator method. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 1f65f52 commit d949799

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

fs/timerfd.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,18 @@ static __poll_t timerfd_poll(struct file *file, poll_table *wait)
262262
return events;
263263
}
264264

265-
static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
266-
loff_t *ppos)
265+
static ssize_t timerfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
267266
{
267+
struct file *file = iocb->ki_filp;
268268
struct timerfd_ctx *ctx = file->private_data;
269269
ssize_t res;
270270
u64 ticks = 0;
271271

272-
if (count < sizeof(ticks))
272+
if (iov_iter_count(to) < sizeof(ticks))
273273
return -EINVAL;
274+
274275
spin_lock_irq(&ctx->wqh.lock);
275-
if (file->f_flags & O_NONBLOCK)
276+
if (file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT)
276277
res = -EAGAIN;
277278
else
278279
res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
@@ -312,8 +313,11 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
312313
ctx->ticks = 0;
313314
}
314315
spin_unlock_irq(&ctx->wqh.lock);
315-
if (ticks)
316-
res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
316+
if (ticks) {
317+
res = copy_to_iter(&ticks, sizeof(ticks), to);
318+
if (!res)
319+
res = -EFAULT;
320+
}
317321
return res;
318322
}
319323

@@ -384,7 +388,7 @@ static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg
384388
static const struct file_operations timerfd_fops = {
385389
.release = timerfd_release,
386390
.poll = timerfd_poll,
387-
.read = timerfd_read,
391+
.read_iter = timerfd_read_iter,
388392
.llseek = noop_llseek,
389393
.show_fdinfo = timerfd_show,
390394
.unlocked_ioctl = timerfd_ioctl,
@@ -407,6 +411,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
407411
{
408412
int ufd;
409413
struct timerfd_ctx *ctx;
414+
struct file *file;
410415

411416
/* Check the TFD_* constants for consistency. */
412417
BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
@@ -443,11 +448,22 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
443448

444449
ctx->moffs = ktime_mono_to_real(0);
445450

446-
ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
447-
O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
448-
if (ufd < 0)
451+
ufd = get_unused_fd_flags(flags & TFD_SHARED_FCNTL_FLAGS);
452+
if (ufd < 0) {
453+
kfree(ctx);
454+
return ufd;
455+
}
456+
457+
file = anon_inode_getfile("[timerfd]", &timerfd_fops, ctx,
458+
O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
459+
if (IS_ERR(file)) {
460+
put_unused_fd(ufd);
449461
kfree(ctx);
462+
return PTR_ERR(file);
463+
}
450464

465+
file->f_mode |= FMODE_NOWAIT;
466+
fd_install(ufd, file);
451467
return ufd;
452468
}
453469

0 commit comments

Comments
 (0)