@@ -151,6 +151,38 @@ struct kioctx {
151151 unsigned id ;
152152};
153153
154+ /*
155+ * We use ki_cancel == KIOCB_CANCELLED to indicate that a kiocb has been either
156+ * cancelled or completed (this makes a certain amount of sense because
157+ * successful cancellation - io_cancel() - does deliver the completion to
158+ * userspace).
159+ *
160+ * And since most things don't implement kiocb cancellation and we'd really like
161+ * kiocb completion to be lockless when possible, we use ki_cancel to
162+ * synchronize cancellation and completion - we only set it to KIOCB_CANCELLED
163+ * with xchg() or cmpxchg(), see batch_complete_aio() and kiocb_cancel().
164+ */
165+ #define KIOCB_CANCELLED ((void *) (~0ULL))
166+
167+ struct aio_kiocb {
168+ struct kiocb common ;
169+
170+ struct kioctx * ki_ctx ;
171+ kiocb_cancel_fn * ki_cancel ;
172+
173+ struct iocb __user * ki_user_iocb ; /* user's aiocb */
174+ __u64 ki_user_data ; /* user's data for completion */
175+
176+ struct list_head ki_list ; /* the aio core uses this
177+ * for cancellation */
178+
179+ /*
180+ * If the aio_resfd field of the userspace iocb is not zero,
181+ * this is the underlying eventfd context to deliver events to.
182+ */
183+ struct eventfd_ctx * ki_eventfd ;
184+ };
185+
154186/*------ sysctl variables----*/
155187static DEFINE_SPINLOCK (aio_nr_lock );
156188unsigned long aio_nr ; /* current system wide number of aio requests */
@@ -220,7 +252,7 @@ static int __init aio_setup(void)
220252 if (IS_ERR (aio_mnt ))
221253 panic ("Failed to create aio fs mount." );
222254
223- kiocb_cachep = KMEM_CACHE (kiocb , SLAB_HWCACHE_ALIGN |SLAB_PANIC );
255+ kiocb_cachep = KMEM_CACHE (aio_kiocb , SLAB_HWCACHE_ALIGN |SLAB_PANIC );
224256 kioctx_cachep = KMEM_CACHE (kioctx ,SLAB_HWCACHE_ALIGN |SLAB_PANIC );
225257
226258 pr_debug ("sizeof(struct page) = %zu\n" , sizeof (struct page ));
@@ -480,8 +512,9 @@ static int aio_setup_ring(struct kioctx *ctx)
480512#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
481513#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
482514
483- void kiocb_set_cancel_fn (struct kiocb * req , kiocb_cancel_fn * cancel )
515+ void kiocb_set_cancel_fn (struct kiocb * iocb , kiocb_cancel_fn * cancel )
484516{
517+ struct aio_kiocb * req = container_of (iocb , struct aio_kiocb , common );
485518 struct kioctx * ctx = req -> ki_ctx ;
486519 unsigned long flags ;
487520
@@ -496,7 +529,7 @@ void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
496529}
497530EXPORT_SYMBOL (kiocb_set_cancel_fn );
498531
499- static int kiocb_cancel (struct kiocb * kiocb )
532+ static int kiocb_cancel (struct aio_kiocb * kiocb )
500533{
501534 kiocb_cancel_fn * old , * cancel ;
502535
@@ -514,7 +547,7 @@ static int kiocb_cancel(struct kiocb *kiocb)
514547 cancel = cmpxchg (& kiocb -> ki_cancel , old , KIOCB_CANCELLED );
515548 } while (cancel != old );
516549
517- return cancel (kiocb );
550+ return cancel (& kiocb -> common );
518551}
519552
520553static void free_ioctx (struct work_struct * work )
@@ -550,13 +583,13 @@ static void free_ioctx_reqs(struct percpu_ref *ref)
550583static void free_ioctx_users (struct percpu_ref * ref )
551584{
552585 struct kioctx * ctx = container_of (ref , struct kioctx , users );
553- struct kiocb * req ;
586+ struct aio_kiocb * req ;
554587
555588 spin_lock_irq (& ctx -> ctx_lock );
556589
557590 while (!list_empty (& ctx -> active_reqs )) {
558591 req = list_first_entry (& ctx -> active_reqs ,
559- struct kiocb , ki_list );
592+ struct aio_kiocb , ki_list );
560593
561594 list_del_init (& req -> ki_list );
562595 kiocb_cancel (req );
@@ -932,9 +965,9 @@ static void user_refill_reqs_available(struct kioctx *ctx)
932965 * Allocate a slot for an aio request.
933966 * Returns NULL if no requests are free.
934967 */
935- static inline struct kiocb * aio_get_req (struct kioctx * ctx )
968+ static inline struct aio_kiocb * aio_get_req (struct kioctx * ctx )
936969{
937- struct kiocb * req ;
970+ struct aio_kiocb * req ;
938971
939972 if (!get_reqs_available (ctx )) {
940973 user_refill_reqs_available (ctx );
@@ -955,10 +988,10 @@ static inline struct kiocb *aio_get_req(struct kioctx *ctx)
955988 return NULL ;
956989}
957990
958- static void kiocb_free (struct kiocb * req )
991+ static void kiocb_free (struct aio_kiocb * req )
959992{
960- if (req -> ki_filp )
961- fput (req -> ki_filp );
993+ if (req -> common . ki_filp )
994+ fput (req -> common . ki_filp );
962995 if (req -> ki_eventfd != NULL )
963996 eventfd_ctx_put (req -> ki_eventfd );
964997 kmem_cache_free (kiocb_cachep , req );
@@ -994,8 +1027,9 @@ static struct kioctx *lookup_ioctx(unsigned long ctx_id)
9941027/* aio_complete
9951028 * Called when the io request on the given iocb is complete.
9961029 */
997- void aio_complete (struct kiocb * iocb , long res , long res2 )
1030+ static void aio_complete (struct kiocb * kiocb , long res , long res2 )
9981031{
1032+ struct aio_kiocb * iocb = container_of (kiocb , struct aio_kiocb , common );
9991033 struct kioctx * ctx = iocb -> ki_ctx ;
10001034 struct aio_ring * ring ;
10011035 struct io_event * ev_page , * event ;
@@ -1009,7 +1043,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10091043 * ref, no other paths have a way to get another ref
10101044 * - the sync task helpfully left a reference to itself in the iocb
10111045 */
1012- BUG_ON (is_sync_kiocb (iocb ));
1046+ BUG_ON (is_sync_kiocb (kiocb ));
10131047
10141048 if (iocb -> ki_list .next ) {
10151049 unsigned long flags ;
@@ -1035,7 +1069,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10351069 ev_page = kmap_atomic (ctx -> ring_pages [pos / AIO_EVENTS_PER_PAGE ]);
10361070 event = ev_page + pos % AIO_EVENTS_PER_PAGE ;
10371071
1038- event -> obj = (u64 )(unsigned long )iocb -> ki_obj . user ;
1072+ event -> obj = (u64 )(unsigned long )iocb -> ki_user_iocb ;
10391073 event -> data = iocb -> ki_user_data ;
10401074 event -> res = res ;
10411075 event -> res2 = res2 ;
@@ -1044,7 +1078,7 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10441078 flush_dcache_page (ctx -> ring_pages [pos / AIO_EVENTS_PER_PAGE ]);
10451079
10461080 pr_debug ("%p[%u]: %p: %p %Lx %lx %lx\n" ,
1047- ctx , tail , iocb , iocb -> ki_obj . user , iocb -> ki_user_data ,
1081+ ctx , tail , iocb , iocb -> ki_user_iocb , iocb -> ki_user_data ,
10481082 res , res2 );
10491083
10501084 /* after flagging the request as done, we
@@ -1091,7 +1125,6 @@ void aio_complete(struct kiocb *iocb, long res, long res2)
10911125
10921126 percpu_ref_put (& ctx -> reqs );
10931127}
1094- EXPORT_SYMBOL (aio_complete );
10951128
10961129/* aio_read_events_ring
10971130 * Pull an event off of the ioctx's event ring. Returns the number of
@@ -1480,7 +1513,7 @@ static ssize_t aio_run_iocb(struct kiocb *req, unsigned opcode,
14801513static int io_submit_one (struct kioctx * ctx , struct iocb __user * user_iocb ,
14811514 struct iocb * iocb , bool compat )
14821515{
1483- struct kiocb * req ;
1516+ struct aio_kiocb * req ;
14841517 ssize_t ret ;
14851518
14861519 /* enforce forwards compatibility on users */
@@ -1503,11 +1536,14 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15031536 if (unlikely (!req ))
15041537 return - EAGAIN ;
15051538
1506- req -> ki_filp = fget (iocb -> aio_fildes );
1507- if (unlikely (!req -> ki_filp )) {
1539+ req -> common . ki_filp = fget (iocb -> aio_fildes );
1540+ if (unlikely (!req -> common . ki_filp )) {
15081541 ret = - EBADF ;
15091542 goto out_put_req ;
15101543 }
1544+ req -> common .ki_pos = iocb -> aio_offset ;
1545+ req -> common .ki_complete = aio_complete ;
1546+ req -> common .ki_flags = 0 ;
15111547
15121548 if (iocb -> aio_flags & IOCB_FLAG_RESFD ) {
15131549 /*
@@ -1522,6 +1558,8 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15221558 req -> ki_eventfd = NULL ;
15231559 goto out_put_req ;
15241560 }
1561+
1562+ req -> common .ki_flags |= IOCB_EVENTFD ;
15251563 }
15261564
15271565 ret = put_user (KIOCB_KEY , & user_iocb -> aio_key );
@@ -1530,11 +1568,10 @@ static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
15301568 goto out_put_req ;
15311569 }
15321570
1533- req -> ki_obj . user = user_iocb ;
1571+ req -> ki_user_iocb = user_iocb ;
15341572 req -> ki_user_data = iocb -> aio_data ;
1535- req -> ki_pos = iocb -> aio_offset ;
15361573
1537- ret = aio_run_iocb (req , iocb -> aio_lio_opcode ,
1574+ ret = aio_run_iocb (& req -> common , iocb -> aio_lio_opcode ,
15381575 (char __user * )(unsigned long )iocb -> aio_buf ,
15391576 iocb -> aio_nbytes ,
15401577 compat );
@@ -1623,20 +1660,19 @@ SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
16231660/* lookup_kiocb
16241661 * Finds a given iocb for cancellation.
16251662 */
1626- static struct kiocb * lookup_kiocb ( struct kioctx * ctx , struct iocb __user * iocb ,
1627- u32 key )
1663+ static struct aio_kiocb *
1664+ lookup_kiocb ( struct kioctx * ctx , struct iocb __user * iocb , u32 key )
16281665{
1629- struct list_head * pos ;
1666+ struct aio_kiocb * kiocb ;
16301667
16311668 assert_spin_locked (& ctx -> ctx_lock );
16321669
16331670 if (key != KIOCB_KEY )
16341671 return NULL ;
16351672
16361673 /* TODO: use a hash or array, this sucks. */
1637- list_for_each (pos , & ctx -> active_reqs ) {
1638- struct kiocb * kiocb = list_kiocb (pos );
1639- if (kiocb -> ki_obj .user == iocb )
1674+ list_for_each_entry (kiocb , & ctx -> active_reqs , ki_list ) {
1675+ if (kiocb -> ki_user_iocb == iocb )
16401676 return kiocb ;
16411677 }
16421678 return NULL ;
@@ -1656,7 +1692,7 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
16561692 struct io_event __user * , result )
16571693{
16581694 struct kioctx * ctx ;
1659- struct kiocb * kiocb ;
1695+ struct aio_kiocb * kiocb ;
16601696 u32 key ;
16611697 int ret ;
16621698
0 commit comments