@@ -322,6 +322,8 @@ struct io_kiocb {
322
322
#define REQ_F_FAIL_LINK 256 /* fail rest of links */
323
323
#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
324
324
#define REQ_F_TIMEOUT 1024 /* timeout request */
325
+ #define REQ_F_ISREG 2048 /* regular file */
326
+ #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
325
327
u64 user_data ;
326
328
u32 result ;
327
329
u32 sequence ;
@@ -914,26 +916,26 @@ static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
914
916
return ret ;
915
917
}
916
918
917
- static void kiocb_end_write (struct kiocb * kiocb )
919
+ static void kiocb_end_write (struct io_kiocb * req )
918
920
{
919
- if (kiocb -> ki_flags & IOCB_WRITE ) {
920
- struct inode * inode = file_inode (kiocb -> ki_filp );
921
+ /*
922
+ * Tell lockdep we inherited freeze protection from submission
923
+ * thread.
924
+ */
925
+ if (req -> flags & REQ_F_ISREG ) {
926
+ struct inode * inode = file_inode (req -> file );
921
927
922
- /*
923
- * Tell lockdep we inherited freeze protection from submission
924
- * thread.
925
- */
926
- if (S_ISREG (inode -> i_mode ))
927
- __sb_writers_acquired (inode -> i_sb , SB_FREEZE_WRITE );
928
- file_end_write (kiocb -> ki_filp );
928
+ __sb_writers_acquired (inode -> i_sb , SB_FREEZE_WRITE );
929
929
}
930
+ file_end_write (req -> file );
930
931
}
931
932
932
933
static void io_complete_rw (struct kiocb * kiocb , long res , long res2 )
933
934
{
934
935
struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
935
936
936
- kiocb_end_write (kiocb );
937
+ if (kiocb -> ki_flags & IOCB_WRITE )
938
+ kiocb_end_write (req );
937
939
938
940
if ((req -> flags & REQ_F_LINK ) && res != req -> result )
939
941
req -> flags |= REQ_F_FAIL_LINK ;
@@ -945,7 +947,8 @@ static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
945
947
{
946
948
struct io_kiocb * req = container_of (kiocb , struct io_kiocb , rw );
947
949
948
- kiocb_end_write (kiocb );
950
+ if (kiocb -> ki_flags & IOCB_WRITE )
951
+ kiocb_end_write (req );
949
952
950
953
if ((req -> flags & REQ_F_LINK ) && res != req -> result )
951
954
req -> flags |= REQ_F_FAIL_LINK ;
@@ -1059,8 +1062,17 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
1059
1062
if (!req -> file )
1060
1063
return - EBADF ;
1061
1064
1062
- if (force_nonblock && !io_file_supports_async (req -> file ))
1063
- force_nonblock = false;
1065
+ if (S_ISREG (file_inode (req -> file )-> i_mode ))
1066
+ req -> flags |= REQ_F_ISREG ;
1067
+
1068
+ /*
1069
+ * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1070
+ * we know to async punt it even if it was opened O_NONBLOCK
1071
+ */
1072
+ if (force_nonblock && !io_file_supports_async (req -> file )) {
1073
+ req -> flags |= REQ_F_MUST_PUNT ;
1074
+ return - EAGAIN ;
1075
+ }
1064
1076
1065
1077
kiocb -> ki_pos = READ_ONCE (sqe -> off );
1066
1078
kiocb -> ki_flags = iocb_flags (kiocb -> ki_filp );
@@ -1081,7 +1093,8 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
1081
1093
return ret ;
1082
1094
1083
1095
/* don't allow async punt if RWF_NOWAIT was requested */
1084
- if (kiocb -> ki_flags & IOCB_NOWAIT )
1096
+ if ((kiocb -> ki_flags & IOCB_NOWAIT ) ||
1097
+ (req -> file -> f_flags & O_NONBLOCK ))
1085
1098
req -> flags |= REQ_F_NOWAIT ;
1086
1099
1087
1100
if (force_nonblock )
@@ -1382,7 +1395,9 @@ static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
1382
1395
* need async punt anyway, so it's more efficient to do it
1383
1396
* here.
1384
1397
*/
1385
- if (force_nonblock && ret2 > 0 && ret2 < read_size )
1398
+ if (force_nonblock && !(req -> flags & REQ_F_NOWAIT ) &&
1399
+ (req -> flags & REQ_F_ISREG ) &&
1400
+ ret2 > 0 && ret2 < read_size )
1386
1401
ret2 = - EAGAIN ;
1387
1402
/* Catch -EAGAIN return for forced non-blocking submission */
1388
1403
if (!force_nonblock || ret2 != - EAGAIN ) {
@@ -1447,7 +1462,7 @@ static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
1447
1462
* released so that it doesn't complain about the held lock when
1448
1463
* we return to userspace.
1449
1464
*/
1450
- if (S_ISREG ( file_inode ( file ) -> i_mode ) ) {
1465
+ if (req -> flags & REQ_F_ISREG ) {
1451
1466
__sb_start_write (file_inode (file )-> i_sb ,
1452
1467
SB_FREEZE_WRITE , true);
1453
1468
__sb_writers_release (file_inode (file )-> i_sb ,
@@ -2282,7 +2297,13 @@ static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2282
2297
int ret ;
2283
2298
2284
2299
ret = __io_submit_sqe (ctx , req , s , force_nonblock );
2285
- if (ret == - EAGAIN && !(req -> flags & REQ_F_NOWAIT )) {
2300
+
2301
+ /*
2302
+ * We async punt it if the file wasn't marked NOWAIT, or if the file
2303
+ * doesn't support non-blocking read/write attempts
2304
+ */
2305
+ if (ret == - EAGAIN && (!(req -> flags & REQ_F_NOWAIT ) ||
2306
+ (req -> flags & REQ_F_MUST_PUNT ))) {
2286
2307
struct io_uring_sqe * sqe_copy ;
2287
2308
2288
2309
sqe_copy = kmemdup (s -> sqe , sizeof (* sqe_copy ), GFP_KERNEL );
0 commit comments