Skip to content

Commit fbe3812

Browse files
committed
signalfd: convert to ->read_iter()
Rather than use the older style ->read() hook, use ->read_iter() so that signalfd can support both O_NONBLOCK and IOCB_NOWAIT for non-blocking read attempts. Split the fd setup into two parts, so that signalfd can mark the file mode with FMODE_NOWAIT before installing it into the process table. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 40f45fe commit fbe3812

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

fs/signalfd.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ static __poll_t signalfd_poll(struct file *file, poll_table *wait)
6868
/*
6969
* Copied from copy_siginfo_to_user() in kernel/signal.c
7070
*/
71-
static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
72-
kernel_siginfo_t const *kinfo)
71+
static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
7372
{
7473
struct signalfd_siginfo new;
7574

@@ -146,10 +145,10 @@ static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
146145
break;
147146
}
148147

149-
if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
148+
if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
150149
return -EFAULT;
151150

152-
return sizeof(*uinfo);
151+
return sizeof(struct signalfd_siginfo);
153152
}
154153

155154
static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
@@ -199,28 +198,27 @@ static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info
199198
* error code. The "count" parameter must be at least the size of a
200199
* "struct signalfd_siginfo".
201200
*/
202-
static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
203-
loff_t *ppos)
201+
static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
204202
{
203+
struct file *file = iocb->ki_filp;
205204
struct signalfd_ctx *ctx = file->private_data;
206-
struct signalfd_siginfo __user *siginfo;
207-
int nonblock = file->f_flags & O_NONBLOCK;
205+
size_t count = iov_iter_count(to);
208206
ssize_t ret, total = 0;
209207
kernel_siginfo_t info;
208+
bool nonblock;
210209

211210
count /= sizeof(struct signalfd_siginfo);
212211
if (!count)
213212
return -EINVAL;
214213

215-
siginfo = (struct signalfd_siginfo __user *) buf;
214+
nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
216215
do {
217216
ret = signalfd_dequeue(ctx, &info, nonblock);
218217
if (unlikely(ret <= 0))
219218
break;
220-
ret = signalfd_copyinfo(siginfo, &info);
219+
ret = signalfd_copyinfo(to, &info);
221220
if (ret < 0)
222221
break;
223-
siginfo++;
224222
total += ret;
225223
nonblock = 1;
226224
} while (--count);
@@ -246,7 +244,7 @@ static const struct file_operations signalfd_fops = {
246244
#endif
247245
.release = signalfd_release,
248246
.poll = signalfd_poll,
249-
.read = signalfd_read,
247+
.read_iter = signalfd_read_iter,
250248
.llseek = noop_llseek,
251249
};
252250

@@ -265,20 +263,34 @@ static int do_signalfd4(int ufd, sigset_t *mask, int flags)
265263
signotset(mask);
266264

267265
if (ufd == -1) {
266+
struct file *file;
267+
268268
ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
269269
if (!ctx)
270270
return -ENOMEM;
271271

272272
ctx->sigmask = *mask;
273273

274+
ufd = get_unused_fd_flags(flags & O_CLOEXEC);
275+
if (ufd < 0) {
276+
kfree(ctx);
277+
return ufd;
278+
}
279+
280+
file = anon_inode_getfile("[signalfd]", &signalfd_fops, ctx,
281+
O_RDWR | (flags & O_NONBLOCK));
282+
if (IS_ERR(file)) {
283+
put_unused_fd(ufd);
284+
kfree(ctx);
285+
return ufd;
286+
}
287+
file->f_mode |= FMODE_NOWAIT;
288+
274289
/*
275290
* When we call this, the initialization must be complete, since
276291
* anon_inode_getfd() will install the fd.
277292
*/
278-
ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
279-
O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
280-
if (ufd < 0)
281-
kfree(ctx);
293+
fd_install(ufd, file);
282294
} else {
283295
struct fd f = fdget(ufd);
284296
if (!f.file)

0 commit comments

Comments
 (0)