From 71a12caf4e5d7478b38647fee63c06a67e84f3b3 Mon Sep 17 00:00:00 2001
From: Matthew Wilcox <matthew@wil.cx>
Date: Sun, 27 Jan 2008 22:39:20 -0500
Subject: [PATCH] sh: Implement down_killable

Signed-off-by: Matthew Wilcox <matthew@wil.cx>
---
 arch/sh/kernel/semaphore.c        |   19 +++++++++++++++++++
 arch/sh/kernel/sh_ksyms.c         |    1 +
 include/asm-sh/semaphore-helper.h |   18 ++++++++++++++++++
 include/asm-sh/semaphore.h        |   12 ++++++++++++
 4 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/arch/sh/kernel/semaphore.c b/arch/sh/kernel/semaphore.c
index 184119e..4cdea93 100644
--- a/arch/sh/kernel/semaphore.c
+++ b/arch/sh/kernel/semaphore.c
@@ -133,6 +133,25 @@ int __sched __down_interruptible(struct semaphore * sem)
 	return ret;
 }
 
+int __sched __down_killable(struct semaphore * sem)
+{
+	int ret = 0;
+	DOWN_VAR
+	DOWN_HEAD(TASK_KILLABLE)
+
+	ret = waking_non_zero_killable(sem, tsk);
+	if (ret)
+	{
+		if (ret == 1)
+			/* ret != 0 only if we get interrupted -arca */
+			ret = 0;
+		break;
+	}
+	schedule();
+	DOWN_TAIL(TASK_KILLABLE)
+	return ret;
+}
+
 int __down_trylock(struct semaphore * sem)
 {
 	return waking_non_zero_trylock(sem);
diff --git a/arch/sh/kernel/sh_ksyms.c b/arch/sh/kernel/sh_ksyms.c
index e1a6de9..401aeb7 100644
--- a/arch/sh/kernel/sh_ksyms.c
+++ b/arch/sh/kernel/sh_ksyms.c
@@ -52,6 +52,7 @@ EXPORT_SYMBOL(get_vm_area);
 EXPORT_SYMBOL(__up);
 EXPORT_SYMBOL(__down);
 EXPORT_SYMBOL(__down_interruptible);
+EXPORT_SYMBOL(__down_killable);
 EXPORT_SYMBOL(__down_trylock);
 
 EXPORT_SYMBOL(__udelay);
diff --git a/include/asm-sh/semaphore-helper.h b/include/asm-sh/semaphore-helper.h
index bd8230c..c360f78 100644
--- a/include/asm-sh/semaphore-helper.h
+++ b/include/asm-sh/semaphore-helper.h
@@ -53,6 +53,24 @@ static __inline__ int waking_non_zero_interruptible(struct semaphore *sem,
 	if (sem->sleepers > 0) {
 		sem->sleepers--;
 		ret = 1;
+	} else if (fatal_signal_pending(tsk)) {
+		atomic_inc(&sem->count);
+		ret = -EINTR;
+	}
+	spin_unlock_irqrestore(&semaphore_wake_lock, flags);
+	return ret;
+}
+
+static __inline__ int waking_non_zero_killable(struct semaphore *sem,
+						struct task_struct *tsk)
+{
+	unsigned long flags;
+	int ret = 0;
+
+	spin_lock_irqsave(&semaphore_wake_lock, flags);
+	if (sem->sleepers > 0) {
+		sem->sleepers--;
+		ret = 1;
 	} else if (signal_pending(tsk)) {
 		atomic_inc(&sem->count);
 		ret = -EINTR;
diff --git a/include/asm-sh/semaphore.h b/include/asm-sh/semaphore.h
index 9e5a37c..7d0a422 100644
--- a/include/asm-sh/semaphore.h
+++ b/include/asm-sh/semaphore.h
@@ -64,12 +64,14 @@ static inline void init_MUTEX_LOCKED (struct semaphore *sem)
 #if 0
 asmlinkage void __down_failed(void /* special register calling convention */);
 asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
+asmlinkage int  __down_failed_killable(void  /* params in registers */);
 asmlinkage int  __down_failed_trylock(void  /* params in registers */);
 asmlinkage void __up_wakeup(void /* special register calling convention */);
 #endif
 
 asmlinkage void __down(struct semaphore * sem);
 asmlinkage int  __down_interruptible(struct semaphore * sem);
+asmlinkage int  __down_killable(struct semaphore * sem);
 asmlinkage int  __down_trylock(struct semaphore * sem);
 asmlinkage void __up(struct semaphore * sem);
 
@@ -92,6 +94,16 @@ static inline int down_interruptible(struct semaphore * sem)
 	return ret;
 }
 
+static inline int down_killable(struct semaphore * sem)
+{
+	int ret = 0;
+
+	might_sleep();
+	if (atomic_dec_return(&sem->count) < 0)
+		ret = __down_killable(sem);
+	return ret;
+}
+
 static inline int down_trylock(struct semaphore * sem)
 {
 	int ret = 0;
-- 
1.5.3.8


