Skip to content

Commit 54843f8

Browse files
author
Christoph Hellwig
committed
aio: refactor read/write iocb setup
Don't reference the kiocb structure from the common aio code, and move any use of it into helper specific to the read/write path. This is in preparation for aio_poll support that wants to use the space for different fields. Signed-off-by: Christoph Hellwig <[email protected]> Acked-by: Jeff Moyer <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]>
1 parent 92ce472 commit 54843f8

File tree

1 file changed

+92
-69
lines changed

1 file changed

+92
-69
lines changed

fs/aio.c

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ struct kioctx {
170170
#define KIOCB_CANCELLED ((void *) (~0ULL))
171171

172172
struct aio_kiocb {
173-
struct kiocb common;
173+
union {
174+
struct kiocb rw;
175+
};
174176

175177
struct kioctx *ki_ctx;
176178
kiocb_cancel_fn *ki_cancel;
@@ -549,7 +551,7 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events)
549551

550552
void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel)
551553
{
552-
struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, common);
554+
struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw);
553555
struct kioctx *ctx = req->ki_ctx;
554556
unsigned long flags;
555557

@@ -581,7 +583,7 @@ static int kiocb_cancel(struct aio_kiocb *kiocb)
581583
cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
582584
} while (cancel != old);
583585

584-
return cancel(&kiocb->common);
586+
return cancel(&kiocb->rw);
585587
}
586588

587589
/*
@@ -1046,15 +1048,6 @@ static inline struct aio_kiocb *aio_get_req(struct kioctx *ctx)
10461048
return NULL;
10471049
}
10481050

1049-
static void kiocb_free(struct aio_kiocb *req)
1050-
{
1051-
if (req->common.ki_filp)
1052-
fput(req->common.ki_filp);
1053-
if (req->ki_eventfd != NULL)
1054-
eventfd_ctx_put(req->ki_eventfd);
1055-
kmem_cache_free(kiocb_cachep, req);
1056-
}
1057-
10581051
static struct kioctx *lookup_ioctx(unsigned long ctx_id)
10591052
{
10601053
struct aio_ring __user *ring = (void __user *)ctx_id;
@@ -1085,27 +1078,14 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
10851078
/* aio_complete
10861079
* Called when the io request on the given iocb is complete.
10871080
*/
1088-
static void aio_complete(struct kiocb *kiocb, long res, long res2)
1081+
static void aio_complete(struct aio_kiocb *iocb, long res, long res2)
10891082
{
1090-
struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, common);
10911083
struct kioctx *ctx = iocb->ki_ctx;
10921084
struct aio_ring *ring;
10931085
struct io_event *ev_page, *event;
10941086
unsigned tail, pos, head;
10951087
unsigned long flags;
10961088

1097-
if (kiocb->ki_flags & IOCB_WRITE) {
1098-
struct file *file = kiocb->ki_filp;
1099-
1100-
/*
1101-
* Tell lockdep we inherited freeze protection from submission
1102-
* thread.
1103-
*/
1104-
if (S_ISREG(file_inode(file)->i_mode))
1105-
__sb_writers_acquired(file_inode(file)->i_sb, SB_FREEZE_WRITE);
1106-
file_end_write(file);
1107-
}
1108-
11091089
if (!list_empty_careful(&iocb->ki_list)) {
11101090
unsigned long flags;
11111091

@@ -1167,11 +1147,12 @@ static void aio_complete(struct kiocb *kiocb, long res, long res2)
11671147
* eventfd. The eventfd_signal() function is safe to be called
11681148
* from IRQ context.
11691149
*/
1170-
if (iocb->ki_eventfd != NULL)
1150+
if (iocb->ki_eventfd) {
11711151
eventfd_signal(iocb->ki_eventfd, 1);
1152+
eventfd_ctx_put(iocb->ki_eventfd);
1153+
}
11721154

1173-
/* everything turned out well, dispose of the aiocb. */
1174-
kiocb_free(iocb);
1155+
kmem_cache_free(kiocb_cachep, iocb);
11751156

11761157
/*
11771158
* We have to order our ring_info tail store above and test
@@ -1434,6 +1415,45 @@ SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
14341415
return -EINVAL;
14351416
}
14361417

1418+
static void aio_complete_rw(struct kiocb *kiocb, long res, long res2)
1419+
{
1420+
struct aio_kiocb *iocb = container_of(kiocb, struct aio_kiocb, rw);
1421+
1422+
if (kiocb->ki_flags & IOCB_WRITE) {
1423+
struct inode *inode = file_inode(kiocb->ki_filp);
1424+
1425+
/*
1426+
* Tell lockdep we inherited freeze protection from submission
1427+
* thread.
1428+
*/
1429+
if (S_ISREG(inode->i_mode))
1430+
__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
1431+
file_end_write(kiocb->ki_filp);
1432+
}
1433+
1434+
fput(kiocb->ki_filp);
1435+
aio_complete(iocb, res, res2);
1436+
}
1437+
1438+
static int aio_prep_rw(struct kiocb *req, struct iocb *iocb)
1439+
{
1440+
int ret;
1441+
1442+
req->ki_filp = fget(iocb->aio_fildes);
1443+
if (unlikely(!req->ki_filp))
1444+
return -EBADF;
1445+
req->ki_complete = aio_complete_rw;
1446+
req->ki_pos = iocb->aio_offset;
1447+
req->ki_flags = iocb_flags(req->ki_filp);
1448+
if (iocb->aio_flags & IOCB_FLAG_RESFD)
1449+
req->ki_flags |= IOCB_EVENTFD;
1450+
req->ki_hint = file_write_hint(req->ki_filp);
1451+
ret = kiocb_set_rw_flags(req, iocb->aio_rw_flags);
1452+
if (unlikely(ret))
1453+
fput(req->ki_filp);
1454+
return ret;
1455+
}
1456+
14371457
static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
14381458
bool vectored, bool compat, struct iov_iter *iter)
14391459
{
@@ -1453,7 +1473,7 @@ static int aio_setup_rw(int rw, struct iocb *iocb, struct iovec **iovec,
14531473
return import_iovec(rw, buf, len, UIO_FASTIOV, iovec, iter);
14541474
}
14551475

1456-
static inline ssize_t aio_ret(struct kiocb *req, ssize_t ret)
1476+
static inline ssize_t aio_rw_ret(struct kiocb *req, ssize_t ret)
14571477
{
14581478
switch (ret) {
14591479
case -EIOCBQUEUED:
@@ -1469,75 +1489,94 @@ static inline ssize_t aio_ret(struct kiocb *req, ssize_t ret)
14691489
ret = -EINTR;
14701490
/*FALLTHRU*/
14711491
default:
1472-
aio_complete(req, ret, 0);
1492+
aio_complete_rw(req, ret, 0);
14731493
return 0;
14741494
}
14751495
}
14761496

14771497
static ssize_t aio_read(struct kiocb *req, struct iocb *iocb, bool vectored,
14781498
bool compat)
14791499
{
1480-
struct file *file = req->ki_filp;
14811500
struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
14821501
struct iov_iter iter;
1502+
struct file *file;
14831503
ssize_t ret;
14841504

1505+
ret = aio_prep_rw(req, iocb);
1506+
if (ret)
1507+
return ret;
1508+
file = req->ki_filp;
1509+
1510+
ret = -EBADF;
14851511
if (unlikely(!(file->f_mode & FMODE_READ)))
1486-
return -EBADF;
1512+
goto out_fput;
1513+
ret = -EINVAL;
14871514
if (unlikely(!file->f_op->read_iter))
1488-
return -EINVAL;
1515+
goto out_fput;
14891516

14901517
ret = aio_setup_rw(READ, iocb, &iovec, vectored, compat, &iter);
14911518
if (ret)
1492-
return ret;
1519+
goto out_fput;
14931520
ret = rw_verify_area(READ, file, &req->ki_pos, iov_iter_count(&iter));
14941521
if (!ret)
1495-
ret = aio_ret(req, call_read_iter(file, req, &iter));
1522+
ret = aio_rw_ret(req, call_read_iter(file, req, &iter));
14961523
kfree(iovec);
1524+
out_fput:
1525+
if (unlikely(ret && ret != -EIOCBQUEUED))
1526+
fput(file);
14971527
return ret;
14981528
}
14991529

15001530
static ssize_t aio_write(struct kiocb *req, struct iocb *iocb, bool vectored,
15011531
bool compat)
15021532
{
1503-
struct file *file = req->ki_filp;
15041533
struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
15051534
struct iov_iter iter;
1535+
struct file *file;
15061536
ssize_t ret;
15071537

1538+
ret = aio_prep_rw(req, iocb);
1539+
if (ret)
1540+
return ret;
1541+
file = req->ki_filp;
1542+
1543+
ret = -EBADF;
15081544
if (unlikely(!(file->f_mode & FMODE_WRITE)))
1509-
return -EBADF;
1545+
goto out_fput;
1546+
ret = -EINVAL;
15101547
if (unlikely(!file->f_op->write_iter))
1511-
return -EINVAL;
1548+
goto out_fput;
15121549

15131550
ret = aio_setup_rw(WRITE, iocb, &iovec, vectored, compat, &iter);
15141551
if (ret)
1515-
return ret;
1552+
goto out_fput;
15161553
ret = rw_verify_area(WRITE, file, &req->ki_pos, iov_iter_count(&iter));
15171554
if (!ret) {
15181555
/*
15191556
* Open-code file_start_write here to grab freeze protection,
1520-
* which will be released by another thread in aio_complete().
1521-
* Fool lockdep by telling it the lock got released so that it
1522-
* doesn't complain about the held lock when we return to
1523-
* userspace.
1557+
* which will be released by another thread in
1558+
* aio_complete_rw(). Fool lockdep by telling it the lock got
1559+
* released so that it doesn't complain about the held lock when
1560+
* we return to userspace.
15241561
*/
15251562
if (S_ISREG(file_inode(file)->i_mode)) {
15261563
__sb_start_write(file_inode(file)->i_sb, SB_FREEZE_WRITE, true);
15271564
__sb_writers_release(file_inode(file)->i_sb, SB_FREEZE_WRITE);
15281565
}
15291566
req->ki_flags |= IOCB_WRITE;
1530-
ret = aio_ret(req, call_write_iter(file, req, &iter));
1567+
ret = aio_rw_ret(req, call_write_iter(file, req, &iter));
15311568
}
15321569
kfree(iovec);
1570+
out_fput:
1571+
if (unlikely(ret && ret != -EIOCBQUEUED))
1572+
fput(file);
15331573
return ret;
15341574
}
15351575

15361576
static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15371577
struct iocb *iocb, bool compat)
15381578
{
15391579
struct aio_kiocb *req;
1540-
struct file *file;
15411580
ssize_t ret;
15421581

15431582
/* enforce forwards compatibility on users */
@@ -1560,16 +1599,6 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15601599
if (unlikely(!req))
15611600
return -EAGAIN;
15621601

1563-
req->common.ki_filp = file = fget(iocb->aio_fildes);
1564-
if (unlikely(!req->common.ki_filp)) {
1565-
ret = -EBADF;
1566-
goto out_put_req;
1567-
}
1568-
req->common.ki_pos = iocb->aio_offset;
1569-
req->common.ki_complete = aio_complete;
1570-
req->common.ki_flags = iocb_flags(req->common.ki_filp);
1571-
req->common.ki_hint = file_write_hint(file);
1572-
15731602
if (iocb->aio_flags & IOCB_FLAG_RESFD) {
15741603
/*
15751604
* If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
@@ -1583,14 +1612,6 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15831612
req->ki_eventfd = NULL;
15841613
goto out_put_req;
15851614
}
1586-
1587-
req->common.ki_flags |= IOCB_EVENTFD;
1588-
}
1589-
1590-
ret = kiocb_set_rw_flags(&req->common, iocb->aio_rw_flags);
1591-
if (unlikely(ret)) {
1592-
pr_debug("EINVAL: aio_rw_flags\n");
1593-
goto out_put_req;
15941615
}
15951616

15961617
ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
@@ -1604,16 +1625,16 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
16041625

16051626
switch (iocb->aio_lio_opcode) {
16061627
case IOCB_CMD_PREAD:
1607-
ret = aio_read(&req->common, iocb, false, compat);
1628+
ret = aio_read(&req->rw, iocb, false, compat);
16081629
break;
16091630
case IOCB_CMD_PWRITE:
1610-
ret = aio_write(&req->common, iocb, false, compat);
1631+
ret = aio_write(&req->rw, iocb, false, compat);
16111632
break;
16121633
case IOCB_CMD_PREADV:
1613-
ret = aio_read(&req->common, iocb, true, compat);
1634+
ret = aio_read(&req->rw, iocb, true, compat);
16141635
break;
16151636
case IOCB_CMD_PWRITEV:
1616-
ret = aio_write(&req->common, iocb, true, compat);
1637+
ret = aio_write(&req->rw, iocb, true, compat);
16171638
break;
16181639
default:
16191640
pr_debug("invalid aio operation %d\n", iocb->aio_lio_opcode);
@@ -1633,7 +1654,9 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
16331654
out_put_req:
16341655
put_reqs_available(ctx, 1);
16351656
percpu_ref_put(&ctx->reqs);
1636-
kiocb_free(req);
1657+
if (req->ki_eventfd)
1658+
eventfd_ctx_put(req->ki_eventfd);
1659+
kmem_cache_free(kiocb_cachep, req);
16371660
return ret;
16381661
}
16391662

0 commit comments

Comments
 (0)