@@ -2843,7 +2843,8 @@ static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
2843
2843
return __io_file_supports_nowait (req -> file , rw );
2844
2844
}
2845
2845
2846
- static int io_prep_rw (struct io_kiocb * req , const struct io_uring_sqe * sqe )
2846
+ static int io_prep_rw (struct io_kiocb * req , const struct io_uring_sqe * sqe ,
2847
+ int rw )
2847
2848
{
2848
2849
struct io_ring_ctx * ctx = req -> ctx ;
2849
2850
struct kiocb * kiocb = & req -> rw .kiocb ;
@@ -2865,8 +2866,13 @@ static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2865
2866
if (unlikely (ret ))
2866
2867
return ret ;
2867
2868
2868
- /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2869
- if ((kiocb -> ki_flags & IOCB_NOWAIT ) || (file -> f_flags & O_NONBLOCK ))
2869
+ /*
2870
+ * If the file is marked O_NONBLOCK, still allow retry for it if it
2871
+ * supports async. Otherwise it's impossible to use O_NONBLOCK files
2872
+ * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
2873
+ */
2874
+ if ((kiocb -> ki_flags & IOCB_NOWAIT ) ||
2875
+ ((file -> f_flags & O_NONBLOCK ) && !io_file_supports_nowait (req , rw )))
2870
2876
req -> flags |= REQ_F_NOWAIT ;
2871
2877
2872
2878
ioprio = READ_ONCE (sqe -> ioprio );
@@ -3263,12 +3269,15 @@ static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
3263
3269
ret = nr ;
3264
3270
break ;
3265
3271
}
3272
+ if (!iov_iter_is_bvec (iter )) {
3273
+ iov_iter_advance (iter , nr );
3274
+ } else {
3275
+ req -> rw .len -= nr ;
3276
+ req -> rw .addr += nr ;
3277
+ }
3266
3278
ret += nr ;
3267
3279
if (nr != iovec .iov_len )
3268
3280
break ;
3269
- req -> rw .len -= nr ;
3270
- req -> rw .addr += nr ;
3271
- iov_iter_advance (iter , nr );
3272
3281
}
3273
3282
3274
3283
return ret ;
@@ -3346,7 +3355,7 @@ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3346
3355
{
3347
3356
if (unlikely (!(req -> file -> f_mode & FMODE_READ )))
3348
3357
return - EBADF ;
3349
- return io_prep_rw (req , sqe );
3358
+ return io_prep_rw (req , sqe , READ );
3350
3359
}
3351
3360
3352
3361
/*
@@ -3539,7 +3548,7 @@ static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3539
3548
{
3540
3549
if (unlikely (!(req -> file -> f_mode & FMODE_WRITE )))
3541
3550
return - EBADF ;
3542
- return io_prep_rw (req , sqe );
3551
+ return io_prep_rw (req , sqe , WRITE );
3543
3552
}
3544
3553
3545
3554
static int io_write (struct io_kiocb * req , unsigned int issue_flags )
@@ -7515,6 +7524,14 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
7515
7524
break ;
7516
7525
} while (1 );
7517
7526
7527
+ if (uts ) {
7528
+ struct timespec64 ts ;
7529
+
7530
+ if (get_timespec64 (& ts , uts ))
7531
+ return - EFAULT ;
7532
+ timeout = timespec64_to_jiffies (& ts );
7533
+ }
7534
+
7518
7535
if (sig ) {
7519
7536
#ifdef CONFIG_COMPAT
7520
7537
if (in_compat_syscall ())
@@ -7528,14 +7545,6 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
7528
7545
return ret ;
7529
7546
}
7530
7547
7531
- if (uts ) {
7532
- struct timespec64 ts ;
7533
-
7534
- if (get_timespec64 (& ts , uts ))
7535
- return - EFAULT ;
7536
- timeout = timespec64_to_jiffies (& ts );
7537
- }
7538
-
7539
7548
init_waitqueue_func_entry (& iowq .wq , io_wake_function );
7540
7549
iowq .wq .private = current ;
7541
7550
INIT_LIST_HEAD (& iowq .wq .entry );
@@ -8284,11 +8293,27 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8284
8293
#endif
8285
8294
}
8286
8295
8296
+ static int io_queue_rsrc_removal (struct io_rsrc_data * data , unsigned idx ,
8297
+ struct io_rsrc_node * node , void * rsrc )
8298
+ {
8299
+ struct io_rsrc_put * prsrc ;
8300
+
8301
+ prsrc = kzalloc (sizeof (* prsrc ), GFP_KERNEL );
8302
+ if (!prsrc )
8303
+ return - ENOMEM ;
8304
+
8305
+ prsrc -> tag = * io_get_tag_slot (data , idx );
8306
+ prsrc -> rsrc = rsrc ;
8307
+ list_add (& prsrc -> list , & node -> rsrc_list );
8308
+ return 0 ;
8309
+ }
8310
+
8287
8311
static int io_install_fixed_file (struct io_kiocb * req , struct file * file ,
8288
8312
unsigned int issue_flags , u32 slot_index )
8289
8313
{
8290
8314
struct io_ring_ctx * ctx = req -> ctx ;
8291
8315
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK ;
8316
+ bool needs_switch = false;
8292
8317
struct io_fixed_file * file_slot ;
8293
8318
int ret = - EBADF ;
8294
8319
@@ -8304,9 +8329,22 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8304
8329
8305
8330
slot_index = array_index_nospec (slot_index , ctx -> nr_user_files );
8306
8331
file_slot = io_fixed_file_slot (& ctx -> file_table , slot_index );
8307
- ret = - EBADF ;
8308
- if (file_slot -> file_ptr )
8309
- goto err ;
8332
+
8333
+ if (file_slot -> file_ptr ) {
8334
+ struct file * old_file ;
8335
+
8336
+ ret = io_rsrc_node_switch_start (ctx );
8337
+ if (ret )
8338
+ goto err ;
8339
+
8340
+ old_file = (struct file * )(file_slot -> file_ptr & FFS_MASK );
8341
+ ret = io_queue_rsrc_removal (ctx -> file_data , slot_index ,
8342
+ ctx -> rsrc_node , old_file );
8343
+ if (ret )
8344
+ goto err ;
8345
+ file_slot -> file_ptr = 0 ;
8346
+ needs_switch = true;
8347
+ }
8310
8348
8311
8349
* io_get_tag_slot (ctx -> file_data , slot_index ) = 0 ;
8312
8350
io_fixed_file_set (file_slot , file );
@@ -8318,27 +8356,14 @@ static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8318
8356
8319
8357
ret = 0 ;
8320
8358
err :
8359
+ if (needs_switch )
8360
+ io_rsrc_node_switch (ctx , ctx -> file_data );
8321
8361
io_ring_submit_unlock (ctx , !force_nonblock );
8322
8362
if (ret )
8323
8363
fput (file );
8324
8364
return ret ;
8325
8365
}
8326
8366
8327
- static int io_queue_rsrc_removal (struct io_rsrc_data * data , unsigned idx ,
8328
- struct io_rsrc_node * node , void * rsrc )
8329
- {
8330
- struct io_rsrc_put * prsrc ;
8331
-
8332
- prsrc = kzalloc (sizeof (* prsrc ), GFP_KERNEL );
8333
- if (!prsrc )
8334
- return - ENOMEM ;
8335
-
8336
- prsrc -> tag = * io_get_tag_slot (data , idx );
8337
- prsrc -> rsrc = rsrc ;
8338
- list_add (& prsrc -> list , & node -> rsrc_list );
8339
- return 0 ;
8340
- }
8341
-
8342
8367
static int __io_sqe_files_update (struct io_ring_ctx * ctx ,
8343
8368
struct io_uring_rsrc_update2 * up ,
8344
8369
unsigned nr_args )
@@ -10560,10 +10585,12 @@ static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10560
10585
* ordering. Fine to drop uring_lock here, we hold
10561
10586
* a ref to the ctx.
10562
10587
*/
10588
+ refcount_inc (& sqd -> refs );
10563
10589
mutex_unlock (& ctx -> uring_lock );
10564
10590
mutex_lock (& sqd -> lock );
10565
10591
mutex_lock (& ctx -> uring_lock );
10566
- tctx = sqd -> thread -> io_uring ;
10592
+ if (sqd -> thread )
10593
+ tctx = sqd -> thread -> io_uring ;
10567
10594
}
10568
10595
} else {
10569
10596
tctx = current -> io_uring ;
@@ -10577,16 +10604,20 @@ static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10577
10604
if (ret )
10578
10605
goto err ;
10579
10606
10580
- if (sqd )
10607
+ if (sqd ) {
10581
10608
mutex_unlock (& sqd -> lock );
10609
+ io_put_sq_data (sqd );
10610
+ }
10582
10611
10583
10612
if (copy_to_user (arg , new_count , sizeof (new_count )))
10584
10613
return - EFAULT ;
10585
10614
10586
10615
return 0 ;
10587
10616
err :
10588
- if (sqd )
10617
+ if (sqd ) {
10589
10618
mutex_unlock (& sqd -> lock );
10619
+ io_put_sq_data (sqd );
10620
+ }
10590
10621
return ret ;
10591
10622
}
10592
10623
0 commit comments