[AIO] aio_down() for i386 and x86-64 The patch below (which applies on top of the previous wait_private patch and the do_sync_(read|write) fix) introduces a new pair of primatives for semaphores: aio_down() and aio_up(). The aio_down() function takes an iocb and locks the semaphore for use within the iocb's context. If taking the semaphore would block, aio_down() returns -EIOCBRETRY and will do an kick_iocb() on the iocb once the semaphore is acquired. Signed-off-by: Benjamin LaHaise diff -purN --exclude=description 15_x86_down/arch/i386/kernel/semaphore.c 20_aio_down/arch/i386/kernel/semaphore.c --- 15_x86_down/arch/i386/kernel/semaphore.c 2005-08-08 17:15:28.000000000 -0400 +++ 20_aio_down/arch/i386/kernel/semaphore.c 2005-08-08 17:15:30.000000000 -0400 @@ -13,6 +13,7 @@ * rw semaphores implemented November 1999 by Benjamin LaHaise */ #include +#include #include /* @@ -49,6 +50,28 @@ asm( asm( ".section .sched.text\n" ".align 4\n" +".globl __aio_down_failed\n" +"__aio_down_failed:\n\t" +#if defined(CONFIG_FRAME_POINTER) + "pushl %ebp\n\t" + "movl %esp,%ebp\n\t" +#endif + "pushl %edx\n\t" + "pushl %ecx\n\t" + "call __aio_down\n\t" + "popl %ecx\n\t" + "popl %edx\n\t" +#if defined(CONFIG_FRAME_POINTER) + "movl %ebp,%esp\n\t" + "popl %ebp\n\t" +#endif + "ret" +); +EXPORT_SYMBOL(__aio_down_failed); + +asm( +".section .sched.text\n" +".align 4\n" ".globl __down_failed_interruptible\n" "__down_failed_interruptible:\n\t" #if defined(CONFIG_FRAME_POINTER) diff -purN --exclude=description 15_x86_down/arch/x86_64/lib/thunk.S 20_aio_down/arch/x86_64/lib/thunk.S --- 15_x86_down/arch/x86_64/lib/thunk.S 2004-12-24 16:34:44.000000000 -0500 +++ 20_aio_down/arch/x86_64/lib/thunk.S 2005-08-08 17:15:30.000000000 -0400 @@ -47,6 +47,7 @@ thunk __down_failed,__down thunk_retrax __down_failed_interruptible,__down_interruptible thunk_retrax __down_failed_trylock,__down_trylock + thunk_retrax __aio_down_failed,__aio_down thunk __up_wakeup,__up /* SAVE_ARGS below is used only for the .cfi directives it contains. */ diff -purN --exclude=description 15_x86_down/include/asm-i386/semaphore.h 20_aio_down/include/asm-i386/semaphore.h --- 15_x86_down/include/asm-i386/semaphore.h 2005-06-20 13:33:36.000000000 -0400 +++ 20_aio_down/include/asm-i386/semaphore.h 2005-08-08 17:15:30.000000000 -0400 @@ -41,10 +41,12 @@ #include #include +struct kiocb; struct semaphore { atomic_t count; int sleepers; wait_queue_head_t wait; + struct kiocb *aio_owner; }; @@ -52,7 +54,8 @@ struct semaphore { { \ .count = ATOMIC_INIT(n), \ .sleepers = 0, \ - .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ + .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \ + .aio_owner = NULL \ } #define __MUTEX_INITIALIZER(name) \ @@ -75,6 +78,7 @@ static inline void sema_init (struct sem atomic_set(&sem->count, val); sem->sleepers = 0; init_waitqueue_head(&sem->wait); + sem->aio_owner = NULL; } static inline void init_MUTEX (struct semaphore *sem) @@ -87,6 +91,7 @@ static inline void init_MUTEX_LOCKED (st sema_init(sem, 0); } +fastcall void __aio_down_failed(void /* special register calling convention */); fastcall void __down_failed(void /* special register calling convention */); fastcall int __down_failed_interruptible(void /* params in registers */); fastcall int __down_failed_trylock(void /* params in registers */); @@ -142,6 +147,32 @@ static inline int down_interruptible(str } /* + * Non-blockingly attempt to down() a semaphore for use with aio. + * Returns zero if we acquired it + */ +static inline int aio_down(struct kiocb *iocb, struct semaphore * sem) +{ + int result; + + __asm__ __volatile__( + "# atomic aio down operation\n\t" + LOCK "decl %1\n\t" /* --sem->count */ + "js 2f\n\t" + "movl %3,%2\n" + "xorl %0,%0\n" + "1:\n" + LOCK_SECTION_START("") + "2:\tlea %1,%%edx\n\t" + "call __aio_down_failed\n\t" + "jmp 1b\n" + LOCK_SECTION_END + :"=a" (result), "+m" (sem->count), "=m" (sem->aio_owner) + :"0" (iocb) + :"memory","cc","dx"); + return result; +} + +/* * Non-blockingly attempt to down() a semaphore. * Returns zero if we acquired it */ @@ -190,5 +221,14 @@ static inline void up(struct semaphore * :"memory","ax"); } +static inline void aio_up(struct kiocb *iocb, struct semaphore *sem) +{ +#ifdef CONFIG_DEBUG_KERNEL + BUG_ON(sem->aio_owner != iocb); +#endif + sem->aio_owner = NULL; + up(sem); +} + #endif #endif diff -purN --exclude=description 15_x86_down/include/asm-x86_64/semaphore.h 20_aio_down/include/asm-x86_64/semaphore.h --- 15_x86_down/include/asm-x86_64/semaphore.h 2004-12-24 16:33:48.000000000 -0500 +++ 20_aio_down/include/asm-x86_64/semaphore.h 2005-08-08 17:15:30.000000000 -0400 @@ -43,17 +43,20 @@ #include #include +struct kiocb; struct semaphore { atomic_t count; int sleepers; wait_queue_head_t wait; + struct kiocb *aio_owner; }; #define __SEMAPHORE_INITIALIZER(name, n) \ { \ .count = ATOMIC_INIT(n), \ .sleepers = 0, \ - .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \ + .wait = __WAIT_QUEUE_HEAD_INITIALIZER((name).wait), \ + .aio_owner = NULL \ } #define __MUTEX_INITIALIZER(name) \ @@ -76,6 +79,7 @@ static inline void sema_init (struct sem atomic_set(&sem->count, val); sem->sleepers = 0; init_waitqueue_head(&sem->wait); + sem->aio_owner = NULL; } static inline void init_MUTEX (struct semaphore *sem) @@ -88,11 +92,13 @@ static inline void init_MUTEX_LOCKED (st sema_init(sem, 0); } +asmlinkage long __aio_down_failed(void /* special register calling convention */); asmlinkage void __down_failed(void /* special register calling convention */); asmlinkage int __down_failed_interruptible(void /* params in registers */); asmlinkage int __down_failed_trylock(void /* params in registers */); asmlinkage void __up_wakeup(void /* special register calling convention */); +asmlinkage long __aio_down(struct kiocb *iocb, struct semaphore * sem); asmlinkage void __down(struct semaphore * sem); asmlinkage int __down_interruptible(struct semaphore * sem); asmlinkage int __down_trylock(struct semaphore * sem); @@ -148,6 +154,32 @@ static inline int down_interruptible(str } /* + * Non-blockingly attempt to down() a semaphore for use with aio. + * Returns zero if we acquired it, -EIOCBRETRY if the operation was + * queued and the iocb will receive a kick_iocb() on completion. + */ +static inline long aio_down(struct kiocb *iocb, struct semaphore * sem) +{ + long result; + + __asm__ __volatile__( + "# atomic aio_down operation\n\t" + LOCK "decl %1\n\t" /* --sem->count */ + "js 2f\n\t" + "movq %3,%2\n" /* sem->aio_owner = iocb */ + "xorq %0,%0\n\t" + "1:\n" + LOCK_SECTION_START("") + "2:\tcall __aio_down_failed\n\t" + "jmp 1b\n" + LOCK_SECTION_END + :"=a" (result), "+m" (sem->count), "=m" (sem->aio_owner) + : "D" (iocb), "S" (sem) + :"memory"); + return result; +} + +/* * Non-blockingly attempt to down() a semaphore. * Returns zero if we acquired it */ @@ -192,5 +224,15 @@ static inline void up(struct semaphore * :"D" (sem) :"memory"); } + +static inline void aio_up(struct kiocb *iocb, struct semaphore *sem) +{ +#ifdef CONFIG_DEBUG_KERNEL + BUG_ON(sem->aio_owner != iocb); +#endif + sem->aio_owner = NULL; + up(sem); +} + #endif /* __KERNEL__ */ #endif diff -purN --exclude=description 15_x86_down/lib/semaphore-sleepers.c 20_aio_down/lib/semaphore-sleepers.c --- 15_x86_down/lib/semaphore-sleepers.c 2005-08-08 17:15:28.000000000 -0400 +++ 20_aio_down/lib/semaphore-sleepers.c 2005-08-08 17:15:30.000000000 -0400 @@ -91,6 +91,51 @@ fastcall void __sched __down(struct sema tsk->state = TASK_RUNNING; } +static int aio_down_wait(wait_queue_t *wait, unsigned mode, int sync, void *key) +{ + struct kiocb *iocb = io_wait_to_kiocb(wait); + struct semaphore *sem = wait->private; + int sleepers = sem->sleepers; + + /* + * Add "everybody else" into it. They aren't + * playing, because we own the spinlock in + * the wait_queue_head. + */ + if (!atomic_add_negative(sleepers - 1, &sem->count)) { + sem->sleepers = 0; + sem->aio_owner = iocb; + list_del_init(&wait->task_list); + wake_up_locked(&sem->wait); + kick_iocb(iocb); + return 1; + } + sem->sleepers = 1; /* us - see -1 above */ + + return 1; +} + +fastcall long __sched __aio_down(struct kiocb *iocb, struct semaphore * sem) +{ + unsigned long flags; + + if (sem->aio_owner == iocb) { + atomic_inc(&sem->count); /* undo dec in aio_down() */ + return 0; + } + + spin_lock_irqsave(&sem->wait.lock, flags); + iocb->ki_wait.private = sem; + iocb->ki_wait.func = aio_down_wait; + add_wait_queue_exclusive_locked(&sem->wait, &iocb->ki_wait); + + sem->sleepers++; + + aio_down_wait(&iocb->ki_wait, 0, 0, NULL); + spin_unlock_irqrestore(&sem->wait.lock, flags); + return -EIOCBRETRY; +} + fastcall int __sched __down_interruptible(struct semaphore * sem) { int retval = 0;