@@ -332,6 +332,13 @@ struct io_timeout {
332
332
int flags ;
333
333
};
334
334
335
+ struct io_rw {
336
+ /* NOTE: kiocb has the file as the first member, so don't do it here */
337
+ struct kiocb kiocb ;
338
+ u64 addr ;
339
+ u64 len ;
340
+ };
341
+
335
342
struct io_async_connect {
336
343
struct sockaddr_storage address ;
337
344
};
@@ -369,7 +376,7 @@ struct io_async_ctx {
369
376
struct io_kiocb {
370
377
union {
371
378
struct file * file ;
372
- struct kiocb rw ;
379
+ struct io_rw rw ;
373
380
struct io_poll_iocb poll ;
374
381
struct io_accept accept ;
375
382
struct io_sync sync ;
@@ -1180,7 +1187,7 @@ static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1180
1187
1181
1188
ret = 0 ;
1182
1189
list_for_each_entry_safe (req , tmp , & ctx -> poll_list , list ) {
1183
- struct kiocb * kiocb = & req -> rw ;
1190
+ struct kiocb * kiocb = & req -> rw . kiocb ;
1184
1191
1185
1192
/*
1186
1193
* Move completed entries to our local list. If we find a
@@ -1335,7 +1342,7 @@ static inline void req_set_fail_links(struct io_kiocb *req)
1335
1342
1336
1343
static void io_complete_rw_common (struct kiocb * kiocb , long res )
1337
1344
{
1338
- struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
1345
+ struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw . kiocb );
1339
1346
1340
1347
if (kiocb -> ki_flags & IOCB_WRITE )
1341
1348
kiocb_end_write (req );
@@ -1347,15 +1354,15 @@ static void io_complete_rw_common(struct kiocb *kiocb, long res)
1347
1354
1348
1355
static void io_complete_rw (struct kiocb * kiocb , long res , long res2 )
1349
1356
{
1350
- struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
1357
+ struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw . kiocb );
1351
1358
1352
1359
io_complete_rw_common (kiocb , res );
1353
1360
io_put_req (req );
1354
1361
}
1355
1362
1356
1363
static struct io_kiocb * __io_complete_rw (struct kiocb * kiocb , long res )
1357
1364
{
1358
- struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
1365
+ struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw . kiocb );
1359
1366
struct io_kiocb * nxt = NULL ;
1360
1367
1361
1368
io_complete_rw_common (kiocb , res );
@@ -1366,7 +1373,7 @@ static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1366
1373
1367
1374
static void io_complete_rw_iopoll (struct kiocb * kiocb , long res , long res2 )
1368
1375
{
1369
- struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
1376
+ struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw . kiocb );
1370
1377
1371
1378
if (kiocb -> ki_flags & IOCB_WRITE )
1372
1379
kiocb_end_write (req );
@@ -1400,7 +1407,7 @@ static void io_iopoll_req_issued(struct io_kiocb *req)
1400
1407
1401
1408
list_req = list_first_entry (& ctx -> poll_list , struct io_kiocb ,
1402
1409
list );
1403
- if (list_req -> rw . ki_filp != req -> rw . ki_filp )
1410
+ if (list_req -> file != req -> file )
1404
1411
ctx -> poll_multi_file = true;
1405
1412
}
1406
1413
@@ -1475,7 +1482,7 @@ static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
1475
1482
{
1476
1483
const struct io_uring_sqe * sqe = req -> sqe ;
1477
1484
struct io_ring_ctx * ctx = req -> ctx ;
1478
- struct kiocb * kiocb = & req -> rw ;
1485
+ struct kiocb * kiocb = & req -> rw . kiocb ;
1479
1486
unsigned ioprio ;
1480
1487
int ret ;
1481
1488
@@ -1524,6 +1531,12 @@ static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
1524
1531
return - EINVAL ;
1525
1532
kiocb -> ki_complete = io_complete_rw ;
1526
1533
}
1534
+
1535
+ req -> rw .addr = READ_ONCE (req -> sqe -> addr );
1536
+ req -> rw .len = READ_ONCE (req -> sqe -> len );
1537
+ /* we own ->private, reuse it for the buffer index */
1538
+ req -> rw .kiocb .private = (void * ) (unsigned long )
1539
+ READ_ONCE (req -> sqe -> buf_index );
1527
1540
return 0 ;
1528
1541
}
1529
1542
@@ -1557,11 +1570,11 @@ static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1557
1570
io_rw_done (kiocb , ret );
1558
1571
}
1559
1572
1560
- static ssize_t io_import_fixed (struct io_ring_ctx * ctx , int rw ,
1561
- const struct io_uring_sqe * sqe ,
1573
+ static ssize_t io_import_fixed (struct io_kiocb * req , int rw ,
1562
1574
struct iov_iter * iter )
1563
1575
{
1564
- size_t len = READ_ONCE (sqe -> len );
1576
+ struct io_ring_ctx * ctx = req -> ctx ;
1577
+ size_t len = req -> rw .len ;
1565
1578
struct io_mapped_ubuf * imu ;
1566
1579
unsigned index , buf_index ;
1567
1580
size_t offset ;
@@ -1571,13 +1584,13 @@ static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1571
1584
if (unlikely (!ctx -> user_bufs ))
1572
1585
return - EFAULT ;
1573
1586
1574
- buf_index = READ_ONCE ( sqe -> buf_index ) ;
1587
+ buf_index = ( unsigned long ) req -> rw . kiocb . private ;
1575
1588
if (unlikely (buf_index >= ctx -> nr_user_bufs ))
1576
1589
return - EFAULT ;
1577
1590
1578
1591
index = array_index_nospec (buf_index , ctx -> nr_user_bufs );
1579
1592
imu = & ctx -> user_bufs [index ];
1580
- buf_addr = READ_ONCE ( sqe -> addr ) ;
1593
+ buf_addr = req -> rw . addr ;
1581
1594
1582
1595
/* overflow */
1583
1596
if (buf_addr + len < buf_addr )
@@ -1634,25 +1647,20 @@ static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1634
1647
static ssize_t io_import_iovec (int rw , struct io_kiocb * req ,
1635
1648
struct iovec * * iovec , struct iov_iter * iter )
1636
1649
{
1637
- const struct io_uring_sqe * sqe = req -> sqe ;
1638
- void __user * buf = u64_to_user_ptr (READ_ONCE (sqe -> addr ));
1639
- size_t sqe_len = READ_ONCE (sqe -> len );
1650
+ void __user * buf = u64_to_user_ptr (req -> rw .addr );
1651
+ size_t sqe_len = req -> rw .len ;
1640
1652
u8 opcode ;
1641
1653
1642
- /*
1643
- * We're reading ->opcode for the second time, but the first read
1644
- * doesn't care whether it's _FIXED or not, so it doesn't matter
1645
- * whether ->opcode changes concurrently. The first read does care
1646
- * about whether it is a READ or a WRITE, so we don't trust this read
1647
- * for that purpose and instead let the caller pass in the read/write
1648
- * flag.
1649
- */
1650
1654
opcode = req -> opcode ;
1651
1655
if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED ) {
1652
1656
* iovec = NULL ;
1653
- return io_import_fixed (req -> ctx , rw , sqe , iter );
1657
+ return io_import_fixed (req , rw , iter );
1654
1658
}
1655
1659
1660
+ /* buffer index only valid with fixed read/write */
1661
+ if (req -> rw .kiocb .private )
1662
+ return - EINVAL ;
1663
+
1656
1664
if (req -> io ) {
1657
1665
struct io_async_rw * iorw = & req -> io -> rw ;
1658
1666
@@ -1801,9 +1809,8 @@ static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1801
1809
bool force_nonblock )
1802
1810
{
1803
1811
struct iovec inline_vecs [UIO_FASTIOV ], * iovec = inline_vecs ;
1804
- struct kiocb * kiocb = & req -> rw ;
1812
+ struct kiocb * kiocb = & req -> rw . kiocb ;
1805
1813
struct iov_iter iter ;
1806
- struct file * file ;
1807
1814
size_t iov_count ;
1808
1815
ssize_t io_size , ret ;
1809
1816
@@ -1819,9 +1826,8 @@ static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1819
1826
1820
1827
/* Ensure we clear previously set non-block flag */
1821
1828
if (!force_nonblock )
1822
- req -> rw .ki_flags &= ~IOCB_NOWAIT ;
1829
+ req -> rw .kiocb . ki_flags &= ~IOCB_NOWAIT ;
1823
1830
1824
- file = req -> file ;
1825
1831
io_size = ret ;
1826
1832
if (req -> flags & REQ_F_LINK )
1827
1833
req -> result = io_size ;
@@ -1830,20 +1836,20 @@ static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1830
1836
* If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1831
1837
* we know to async punt it even if it was opened O_NONBLOCK
1832
1838
*/
1833
- if (force_nonblock && !io_file_supports_async (file )) {
1839
+ if (force_nonblock && !io_file_supports_async (req -> file )) {
1834
1840
req -> flags |= REQ_F_MUST_PUNT ;
1835
1841
goto copy_iov ;
1836
1842
}
1837
1843
1838
1844
iov_count = iov_iter_count (& iter );
1839
- ret = rw_verify_area (READ , file , & kiocb -> ki_pos , iov_count );
1845
+ ret = rw_verify_area (READ , req -> file , & kiocb -> ki_pos , iov_count );
1840
1846
if (!ret ) {
1841
1847
ssize_t ret2 ;
1842
1848
1843
- if (file -> f_op -> read_iter )
1844
- ret2 = call_read_iter (file , kiocb , & iter );
1849
+ if (req -> file -> f_op -> read_iter )
1850
+ ret2 = call_read_iter (req -> file , kiocb , & iter );
1845
1851
else
1846
- ret2 = loop_rw_iter (READ , file , kiocb , & iter );
1852
+ ret2 = loop_rw_iter (READ , req -> file , kiocb , & iter );
1847
1853
1848
1854
/*
1849
1855
* In case of a short read, punt to async. This can happen
@@ -1894,9 +1900,8 @@ static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1894
1900
bool force_nonblock )
1895
1901
{
1896
1902
struct iovec inline_vecs [UIO_FASTIOV ], * iovec = inline_vecs ;
1897
- struct kiocb * kiocb = & req -> rw ;
1903
+ struct kiocb * kiocb = & req -> rw . kiocb ;
1898
1904
struct iov_iter iter ;
1899
- struct file * file ;
1900
1905
size_t iov_count ;
1901
1906
ssize_t ret , io_size ;
1902
1907
@@ -1912,9 +1917,8 @@ static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1912
1917
1913
1918
/* Ensure we clear previously set non-block flag */
1914
1919
if (!force_nonblock )
1915
- req -> rw .ki_flags &= ~IOCB_NOWAIT ;
1920
+ req -> rw .kiocb . ki_flags &= ~IOCB_NOWAIT ;
1916
1921
1917
- file = kiocb -> ki_filp ;
1918
1922
io_size = ret ;
1919
1923
if (req -> flags & REQ_F_LINK )
1920
1924
req -> result = io_size ;
@@ -1934,7 +1938,7 @@ static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1934
1938
goto copy_iov ;
1935
1939
1936
1940
iov_count = iov_iter_count (& iter );
1937
- ret = rw_verify_area (WRITE , file , & kiocb -> ki_pos , iov_count );
1941
+ ret = rw_verify_area (WRITE , req -> file , & kiocb -> ki_pos , iov_count );
1938
1942
if (!ret ) {
1939
1943
ssize_t ret2 ;
1940
1944
@@ -1946,17 +1950,17 @@ static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1946
1950
* we return to userspace.
1947
1951
*/
1948
1952
if (req -> flags & REQ_F_ISREG ) {
1949
- __sb_start_write (file_inode (file )-> i_sb ,
1953
+ __sb_start_write (file_inode (req -> file )-> i_sb ,
1950
1954
SB_FREEZE_WRITE , true);
1951
- __sb_writers_release (file_inode (file )-> i_sb ,
1955
+ __sb_writers_release (file_inode (req -> file )-> i_sb ,
1952
1956
SB_FREEZE_WRITE );
1953
1957
}
1954
1958
kiocb -> ki_flags |= IOCB_WRITE ;
1955
1959
1956
- if (file -> f_op -> write_iter )
1957
- ret2 = call_write_iter (file , kiocb , & iter );
1960
+ if (req -> file -> f_op -> write_iter )
1961
+ ret2 = call_write_iter (req -> file , kiocb , & iter );
1958
1962
else
1959
- ret2 = loop_rw_iter (WRITE , file , kiocb , & iter );
1963
+ ret2 = loop_rw_iter (WRITE , req -> file , kiocb , & iter );
1960
1964
if (!force_nonblock || ret2 != - EAGAIN ) {
1961
1965
kiocb_done (kiocb , ret2 , nxt , req -> in_async );
1962
1966
} else {
@@ -2036,7 +2040,7 @@ static void io_fsync_finish(struct io_wq_work **workptr)
2036
2040
if (io_req_cancelled (req ))
2037
2041
return ;
2038
2042
2039
- ret = vfs_fsync_range (req -> rw . ki_filp , req -> sync .off ,
2043
+ ret = vfs_fsync_range (req -> file , req -> sync .off ,
2040
2044
end > 0 ? end : LLONG_MAX ,
2041
2045
req -> sync .flags & IORING_FSYNC_DATASYNC );
2042
2046
if (ret < 0 )
@@ -2102,7 +2106,7 @@ static void io_sync_file_range_finish(struct io_wq_work **workptr)
2102
2106
if (io_req_cancelled (req ))
2103
2107
return ;
2104
2108
2105
- ret = sync_file_range (req -> rw . ki_filp , req -> sync .off , req -> sync .len ,
2109
+ ret = sync_file_range (req -> file , req -> sync .off , req -> sync .len ,
2106
2110
req -> sync .flags );
2107
2111
if (ret < 0 )
2108
2112
req_set_fail_links (req );
0 commit comments