@@ -251,7 +251,7 @@ struct io_rsrc_data {
251
251
struct io_buffer {
252
252
struct list_head list ;
253
253
__u64 addr ;
254
- __s32 len ;
254
+ __u32 len ;
255
255
__u16 bid ;
256
256
};
257
257
@@ -456,6 +456,7 @@ struct io_ring_ctx {
456
456
spinlock_t rsrc_ref_lock ;
457
457
struct io_rsrc_node * rsrc_node ;
458
458
struct io_rsrc_node * rsrc_backup_node ;
459
+ struct io_mapped_ubuf * dummy_ubuf ;
459
460
460
461
struct io_restriction restrictions ;
461
462
@@ -702,7 +703,8 @@ enum {
702
703
REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT ,
703
704
REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT ,
704
705
705
- REQ_F_FAIL_LINK_BIT ,
706
+ /* first byte is taken by user flags, shift it to not overlap */
707
+ REQ_F_FAIL_LINK_BIT = 8 ,
706
708
REQ_F_INFLIGHT_BIT ,
707
709
REQ_F_CUR_POS_BIT ,
708
710
REQ_F_NOWAIT_BIT ,
@@ -1157,6 +1159,12 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1157
1159
goto err ;
1158
1160
__hash_init (ctx -> cancel_hash , 1U << hash_bits );
1159
1161
1162
+ ctx -> dummy_ubuf = kzalloc (sizeof (* ctx -> dummy_ubuf ), GFP_KERNEL );
1163
+ if (!ctx -> dummy_ubuf )
1164
+ goto err ;
1165
+ /* set invalid range, so io_import_fixed() fails meeting it */
1166
+ ctx -> dummy_ubuf -> ubuf = -1UL ;
1167
+
1160
1168
if (percpu_ref_init (& ctx -> refs , io_ring_ctx_ref_free ,
1161
1169
PERCPU_REF_ALLOW_REINIT , GFP_KERNEL ))
1162
1170
goto err ;
@@ -1184,6 +1192,7 @@ static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1184
1192
INIT_LIST_HEAD (& ctx -> submit_state .comp .locked_free_list );
1185
1193
return ctx ;
1186
1194
err :
1195
+ kfree (ctx -> dummy_ubuf );
1187
1196
kfree (ctx -> cancel_hash );
1188
1197
kfree (ctx );
1189
1198
return NULL ;
@@ -3977,7 +3986,7 @@ static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3977
3986
break ;
3978
3987
3979
3988
buf -> addr = addr ;
3980
- buf -> len = pbuf -> len ;
3989
+ buf -> len = min_t ( __u32 , pbuf -> len , MAX_RW_COUNT ) ;
3981
3990
buf -> bid = bid ;
3982
3991
addr += pbuf -> len ;
3983
3992
bid ++ ;
@@ -6503,14 +6512,10 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6503
6512
req -> work .creds = NULL ;
6504
6513
6505
6514
/* enforce forwards compatibility on users */
6506
- if (unlikely (sqe_flags & ~SQE_VALID_FLAGS )) {
6507
- req -> flags = 0 ;
6515
+ if (unlikely (sqe_flags & ~SQE_VALID_FLAGS ))
6508
6516
return - EINVAL ;
6509
- }
6510
-
6511
6517
if (unlikely (req -> opcode >= IORING_OP_LAST ))
6512
6518
return - EINVAL ;
6513
-
6514
6519
if (unlikely (!io_check_restriction (ctx , req , sqe_flags )))
6515
6520
return - EACCES ;
6516
6521
@@ -7539,6 +7544,7 @@ static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
7539
7544
io_ring_submit_lock (ctx , lock_ring );
7540
7545
spin_lock_irqsave (& ctx -> completion_lock , flags );
7541
7546
io_cqring_fill_event (ctx , prsrc -> tag , 0 , 0 );
7547
+ ctx -> cq_extra ++ ;
7542
7548
io_commit_cqring (ctx );
7543
7549
spin_unlock_irqrestore (& ctx -> completion_lock , flags );
7544
7550
io_cqring_ev_posted (ctx );
@@ -8111,11 +8117,13 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slo
8111
8117
struct io_mapped_ubuf * imu = * slot ;
8112
8118
unsigned int i ;
8113
8119
8114
- for (i = 0 ; i < imu -> nr_bvecs ; i ++ )
8115
- unpin_user_page (imu -> bvec [i ].bv_page );
8116
- if (imu -> acct_pages )
8117
- io_unaccount_mem (ctx , imu -> acct_pages );
8118
- kvfree (imu );
8120
+ if (imu != ctx -> dummy_ubuf ) {
8121
+ for (i = 0 ; i < imu -> nr_bvecs ; i ++ )
8122
+ unpin_user_page (imu -> bvec [i ].bv_page );
8123
+ if (imu -> acct_pages )
8124
+ io_unaccount_mem (ctx , imu -> acct_pages );
8125
+ kvfree (imu );
8126
+ }
8119
8127
* slot = NULL ;
8120
8128
}
8121
8129
@@ -8132,7 +8140,7 @@ static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8132
8140
for (i = 0 ; i < ctx -> nr_user_bufs ; i ++ )
8133
8141
io_buffer_unmap (ctx , & ctx -> user_bufs [i ]);
8134
8142
kfree (ctx -> user_bufs );
8135
- kfree (ctx -> buf_data );
8143
+ io_rsrc_data_free (ctx -> buf_data );
8136
8144
ctx -> user_bufs = NULL ;
8137
8145
ctx -> buf_data = NULL ;
8138
8146
ctx -> nr_user_bufs = 0 ;
@@ -8255,6 +8263,11 @@ static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8255
8263
size_t size ;
8256
8264
int ret , pret , nr_pages , i ;
8257
8265
8266
+ if (!iov -> iov_base ) {
8267
+ * pimu = ctx -> dummy_ubuf ;
8268
+ return 0 ;
8269
+ }
8270
+
8258
8271
ubuf = (unsigned long ) iov -> iov_base ;
8259
8272
end = (ubuf + iov -> iov_len + PAGE_SIZE - 1 ) >> PAGE_SHIFT ;
8260
8273
start = ubuf >> PAGE_SHIFT ;
@@ -8352,7 +8365,9 @@ static int io_buffer_validate(struct iovec *iov)
8352
8365
* constraints here, we'll -EINVAL later when IO is
8353
8366
* submitted if they are wrong.
8354
8367
*/
8355
- if (!iov -> iov_base || !iov -> iov_len )
8368
+ if (!iov -> iov_base )
8369
+ return iov -> iov_len ? - EFAULT : 0 ;
8370
+ if (!iov -> iov_len )
8356
8371
return - EFAULT ;
8357
8372
8358
8373
/* arbitrary limit, but we need something */
@@ -8385,7 +8400,7 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8385
8400
return - ENOMEM ;
8386
8401
ret = io_buffers_map_alloc (ctx , nr_args );
8387
8402
if (ret ) {
8388
- kfree (data );
8403
+ io_rsrc_data_free (data );
8389
8404
return ret ;
8390
8405
}
8391
8406
@@ -8402,6 +8417,10 @@ static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8402
8417
ret = io_buffer_validate (& iov );
8403
8418
if (ret )
8404
8419
break ;
8420
+ if (!iov .iov_base && tag ) {
8421
+ ret = - EINVAL ;
8422
+ break ;
8423
+ }
8405
8424
8406
8425
ret = io_sqe_buffer_register (ctx , & iov , & ctx -> user_bufs [i ],
8407
8426
& last_hpage );
@@ -8451,12 +8470,16 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8451
8470
err = io_buffer_validate (& iov );
8452
8471
if (err )
8453
8472
break ;
8473
+ if (!iov .iov_base && tag ) {
8474
+ err = - EINVAL ;
8475
+ break ;
8476
+ }
8454
8477
err = io_sqe_buffer_register (ctx , & iov , & imu , & last_hpage );
8455
8478
if (err )
8456
8479
break ;
8457
8480
8458
8481
i = array_index_nospec (offset , ctx -> nr_user_bufs );
8459
- if (ctx -> user_bufs [i ]) {
8482
+ if (ctx -> user_bufs [i ] != ctx -> dummy_ubuf ) {
8460
8483
err = io_queue_rsrc_removal (ctx -> buf_data , offset ,
8461
8484
ctx -> rsrc_node , ctx -> user_bufs [i ]);
8462
8485
if (unlikely (err )) {
@@ -8604,6 +8627,7 @@ static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8604
8627
if (ctx -> hash_map )
8605
8628
io_wq_put_hash (ctx -> hash_map );
8606
8629
kfree (ctx -> cancel_hash );
8630
+ kfree (ctx -> dummy_ubuf );
8607
8631
kfree (ctx );
8608
8632
}
8609
8633
@@ -9607,7 +9631,9 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p,
9607
9631
if (ret )
9608
9632
goto err ;
9609
9633
/* always set a rsrc node */
9610
- io_rsrc_node_switch_start (ctx );
9634
+ ret = io_rsrc_node_switch_start (ctx );
9635
+ if (ret )
9636
+ goto err ;
9611
9637
io_rsrc_node_switch (ctx , NULL );
9612
9638
9613
9639
memset (& p -> sq_off , 0 , sizeof (p -> sq_off ));
@@ -10136,6 +10162,13 @@ static int __init io_uring_init(void)
10136
10162
BUILD_BUG_SQE_ELEM (42 , __u16 , personality );
10137
10163
BUILD_BUG_SQE_ELEM (44 , __s32 , splice_fd_in );
10138
10164
10165
+ BUILD_BUG_ON (sizeof (struct io_uring_files_update ) !=
10166
+ sizeof (struct io_uring_rsrc_update ));
10167
+ BUILD_BUG_ON (sizeof (struct io_uring_rsrc_update ) >
10168
+ sizeof (struct io_uring_rsrc_update2 ));
10169
+ /* should fit into one byte */
10170
+ BUILD_BUG_ON (SQE_VALID_FLAGS >= (1 << 8 ));
10171
+
10139
10172
BUILD_BUG_ON (ARRAY_SIZE (io_op_defs ) != IORING_OP_LAST );
10140
10173
BUILD_BUG_ON (__REQ_F_LAST_BIT >= 8 * sizeof (int ));
10141
10174
req_cachep = KMEM_CACHE (io_kiocb , SLAB_HWCACHE_ALIGN | SLAB_PANIC |
0 commit comments