@@ -168,6 +168,13 @@ struct io_kiocb {
168
168
struct io_submit_state {
169
169
struct blk_plug plug ;
170
170
171
+ /*
172
+ * io_kiocb alloc cache
173
+ */
174
+ void * reqs [IO_IOPOLL_BATCH ];
175
+ unsigned int free_reqs ;
176
+ unsigned int cur_req ;
177
+
171
178
/*
172
179
* File reference cache
173
180
*/
@@ -305,20 +312,40 @@ static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
305
312
wake_up (& ctx -> wait );
306
313
}
307
314
308
- static struct io_kiocb * io_get_req (struct io_ring_ctx * ctx )
315
+ static struct io_kiocb * io_get_req (struct io_ring_ctx * ctx ,
316
+ struct io_submit_state * state )
309
317
{
310
318
struct io_kiocb * req ;
311
319
312
320
if (!percpu_ref_tryget (& ctx -> refs ))
313
321
return NULL ;
314
322
315
- req = kmem_cache_alloc (req_cachep , __GFP_NOWARN );
316
- if (req ) {
317
- req -> ctx = ctx ;
318
- req -> flags = 0 ;
319
- return req ;
323
+ if (!state ) {
324
+ req = kmem_cache_alloc (req_cachep , __GFP_NOWARN );
325
+ if (unlikely (!req ))
326
+ goto out ;
327
+ } else if (!state -> free_reqs ) {
328
+ size_t sz ;
329
+ int ret ;
330
+
331
+ sz = min_t (size_t , state -> ios_left , ARRAY_SIZE (state -> reqs ));
332
+ ret = kmem_cache_alloc_bulk (req_cachep , __GFP_NOWARN , sz ,
333
+ state -> reqs );
334
+ if (unlikely (ret <= 0 ))
335
+ goto out ;
336
+ state -> free_reqs = ret - 1 ;
337
+ state -> cur_req = 1 ;
338
+ req = state -> reqs [0 ];
339
+ } else {
340
+ req = state -> reqs [state -> cur_req ];
341
+ state -> free_reqs -- ;
342
+ state -> cur_req ++ ;
320
343
}
321
344
345
+ req -> ctx = ctx ;
346
+ req -> flags = 0 ;
347
+ return req ;
348
+ out :
322
349
io_ring_drop_ctx_refs (ctx , 1 );
323
350
return NULL ;
324
351
}
@@ -1007,7 +1034,7 @@ static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1007
1034
if (unlikely (s -> sqe -> flags ))
1008
1035
return - EINVAL ;
1009
1036
1010
- req = io_get_req (ctx );
1037
+ req = io_get_req (ctx , state );
1011
1038
if (unlikely (!req ))
1012
1039
return - EAGAIN ;
1013
1040
@@ -1041,6 +1068,9 @@ static void io_submit_state_end(struct io_submit_state *state)
1041
1068
{
1042
1069
blk_finish_plug (& state -> plug );
1043
1070
io_file_put (state , NULL );
1071
+ if (state -> free_reqs )
1072
+ kmem_cache_free_bulk (req_cachep , state -> free_reqs ,
1073
+ & state -> reqs [state -> cur_req ]);
1044
1074
}
1045
1075
1046
1076
/*
@@ -1050,6 +1080,7 @@ static void io_submit_state_start(struct io_submit_state *state,
1050
1080
struct io_ring_ctx * ctx , unsigned max_ios )
1051
1081
{
1052
1082
blk_start_plug (& state -> plug );
1083
+ state -> free_reqs = 0 ;
1053
1084
state -> file = NULL ;
1054
1085
state -> ios_left = max_ios ;
1055
1086
}
0 commit comments