@@ -440,8 +440,6 @@ void exit_aio(struct mm_struct *mm)
440440static struct kiocb * __aio_get_req (struct kioctx * ctx )
441441{
442442 struct kiocb * req = NULL ;
443- struct aio_ring * ring ;
444- int okay = 0 ;
445443
446444 req = kmem_cache_alloc (kiocb_cachep , GFP_KERNEL );
447445 if (unlikely (!req ))
@@ -459,39 +457,114 @@ static struct kiocb *__aio_get_req(struct kioctx *ctx)
459457 INIT_LIST_HEAD (& req -> ki_run_list );
460458 req -> ki_eventfd = NULL ;
461459
462- /* Check if the completion queue has enough free space to
463- * accept an event from this io.
464- */
460+ return req ;
461+ }
462+
463+ /*
464+ * struct kiocb's are allocated in batches to reduce the number of
465+ * times the ctx lock is acquired and released.
466+ */
467+ #define KIOCB_BATCH_SIZE 32L
468+ struct kiocb_batch {
469+ struct list_head head ;
470+ long count ; /* number of requests left to allocate */
471+ };
472+
473+ static void kiocb_batch_init (struct kiocb_batch * batch , long total )
474+ {
475+ INIT_LIST_HEAD (& batch -> head );
476+ batch -> count = total ;
477+ }
478+
479+ static void kiocb_batch_free (struct kiocb_batch * batch )
480+ {
481+ struct kiocb * req , * n ;
482+
483+ list_for_each_entry_safe (req , n , & batch -> head , ki_batch ) {
484+ list_del (& req -> ki_batch );
485+ kmem_cache_free (kiocb_cachep , req );
486+ }
487+ }
488+
489+ /*
490+ * Allocate a batch of kiocbs. This avoids taking and dropping the
491+ * context lock a lot during setup.
492+ */
493+ static int kiocb_batch_refill (struct kioctx * ctx , struct kiocb_batch * batch )
494+ {
495+ unsigned short allocated , to_alloc ;
496+ long avail ;
497+ bool called_fput = false;
498+ struct kiocb * req , * n ;
499+ struct aio_ring * ring ;
500+
501+ to_alloc = min (batch -> count , KIOCB_BATCH_SIZE );
502+ for (allocated = 0 ; allocated < to_alloc ; allocated ++ ) {
503+ req = __aio_get_req (ctx );
504+ if (!req )
505+ /* allocation failed, go with what we've got */
506+ break ;
507+ list_add (& req -> ki_batch , & batch -> head );
508+ }
509+
510+ if (allocated == 0 )
511+ goto out ;
512+
513+ retry :
465514 spin_lock_irq (& ctx -> ctx_lock );
466- ring = kmap_atomic (ctx -> ring_info .ring_pages [0 ], KM_USER0 );
467- if (ctx -> reqs_active < aio_ring_avail (& ctx -> ring_info , ring )) {
515+ ring = kmap_atomic (ctx -> ring_info .ring_pages [0 ]);
516+
517+ avail = aio_ring_avail (& ctx -> ring_info , ring ) - ctx -> reqs_active ;
518+ BUG_ON (avail < 0 );
519+ if (avail == 0 && !called_fput ) {
520+ /*
521+ * Handle a potential starvation case. It is possible that
522+ * we hold the last reference on a struct file, causing us
523+ * to delay the final fput to non-irq context. In this case,
524+ * ctx->reqs_active is artificially high. Calling the fput
525+ * routine here may free up a slot in the event completion
526+ * ring, allowing this allocation to succeed.
527+ */
528+ kunmap_atomic (ring );
529+ spin_unlock_irq (& ctx -> ctx_lock );
530+ aio_fput_routine (NULL );
531+ called_fput = true;
532+ goto retry ;
533+ }
534+
535+ if (avail < allocated ) {
536+ /* Trim back the number of requests. */
537+ list_for_each_entry_safe (req , n , & batch -> head , ki_batch ) {
538+ list_del (& req -> ki_batch );
539+ kmem_cache_free (kiocb_cachep , req );
540+ if (-- allocated <= avail )
541+ break ;
542+ }
543+ }
544+
545+ batch -> count -= allocated ;
546+ list_for_each_entry (req , & batch -> head , ki_batch ) {
468547 list_add (& req -> ki_list , & ctx -> active_reqs );
469548 ctx -> reqs_active ++ ;
470- okay = 1 ;
471549 }
472- kunmap_atomic (ring , KM_USER0 );
473- spin_unlock_irq (& ctx -> ctx_lock );
474550
475- if (!okay ) {
476- kmem_cache_free (kiocb_cachep , req );
477- req = NULL ;
478- }
551+ kunmap_atomic (ring );
552+ spin_unlock_irq (& ctx -> ctx_lock );
479553
480- return req ;
554+ out :
555+ return allocated ;
481556}
482557
483- static inline struct kiocb * aio_get_req (struct kioctx * ctx )
558+ static inline struct kiocb * aio_get_req (struct kioctx * ctx ,
559+ struct kiocb_batch * batch )
484560{
485561 struct kiocb * req ;
486- /* Handle a potential starvation case -- should be exceedingly rare as
487- * requests will be stuck on fput_head only if the aio_fput_routine is
488- * delayed and the requests were the last user of the struct file.
489- */
490- req = __aio_get_req (ctx );
491- if (unlikely (NULL == req )) {
492- aio_fput_routine (NULL );
493- req = __aio_get_req (ctx );
494- }
562+
563+ if (list_empty (& batch -> head ))
564+ if (kiocb_batch_refill (ctx , batch ) == 0 )
565+ return NULL ;
566+ req = list_first_entry (& batch -> head , struct kiocb , ki_batch );
567+ list_del (& req -> ki_batch );
495568 return req ;
496569}
497570
@@ -1515,7 +1588,8 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
15151588}
15161589
15171590static int io_submit_one (struct kioctx * ctx , struct iocb __user * user_iocb ,
1518- struct iocb * iocb , bool compat )
1591+ struct iocb * iocb , struct kiocb_batch * batch ,
1592+ bool compat )
15191593{
15201594 struct kiocb * req ;
15211595 struct file * file ;
@@ -1541,7 +1615,7 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15411615 if (unlikely (!file ))
15421616 return - EBADF ;
15431617
1544- req = aio_get_req (ctx ); /* returns with 2 references to req */
1618+ req = aio_get_req (ctx , batch ); /* returns with 2 references to req */
15451619 if (unlikely (!req )) {
15461620 fput (file );
15471621 return - EAGAIN ;
@@ -1621,8 +1695,9 @@ long do_io_submit(aio_context_t ctx_id, long nr,
16211695{
16221696 struct kioctx * ctx ;
16231697 long ret = 0 ;
1624- int i ;
1698+ int i = 0 ;
16251699 struct blk_plug plug ;
1700+ struct kiocb_batch batch ;
16261701
16271702 if (unlikely (nr < 0 ))
16281703 return - EINVAL ;
@@ -1639,6 +1714,8 @@ long do_io_submit(aio_context_t ctx_id, long nr,
16391714 return - EINVAL ;
16401715 }
16411716
1717+ kiocb_batch_init (& batch , nr );
1718+
16421719 blk_start_plug (& plug );
16431720
16441721 /*
@@ -1659,12 +1736,13 @@ long do_io_submit(aio_context_t ctx_id, long nr,
16591736 break ;
16601737 }
16611738
1662- ret = io_submit_one (ctx , user_iocb , & tmp , compat );
1739+ ret = io_submit_one (ctx , user_iocb , & tmp , & batch , compat );
16631740 if (ret )
16641741 break ;
16651742 }
16661743 blk_finish_plug (& plug );
16671744
1745+ kiocb_batch_free (& batch );
16681746 put_ioctx (ctx );
16691747 return i ? i : ret ;
16701748}
0 commit comments