From 2264700c27a2548d9147e598b2b35f02cbb0e764 Mon Sep 17 00:00:00 2001
From: Matthew Wilcox <matthew@wil.cx>
Date: Sun, 27 Jan 2008 19:41:22 -0500
Subject: [PATCH] ia64: Implement down_killable

Signed-off-by: Matthew Wilcox <matthew@wil.cx>
---
 arch/ia64/kernel/ia64_ksyms.c |    1 +
 arch/ia64/kernel/semaphore.c  |   55 +++++++++++++++++++++++++++++++++++++++++
 include/asm-ia64/semaphore.h  |   12 +++++++++
 3 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/arch/ia64/kernel/ia64_ksyms.c b/arch/ia64/kernel/ia64_ksyms.c
index c3b4412..0ebbab5 100644
--- a/arch/ia64/kernel/ia64_ksyms.c
+++ b/arch/ia64/kernel/ia64_ksyms.c
@@ -19,6 +19,7 @@ EXPORT_SYMBOL(csum_ipv6_magic);
 #include <asm/semaphore.h>
 EXPORT_SYMBOL(__down);
 EXPORT_SYMBOL(__down_interruptible);
+EXPORT_SYMBOL(__down_killable);
 EXPORT_SYMBOL(__down_trylock);
 EXPORT_SYMBOL(__up);
 
diff --git a/arch/ia64/kernel/semaphore.c b/arch/ia64/kernel/semaphore.c
index 2724ef3..8f598f2 100644
--- a/arch/ia64/kernel/semaphore.c
+++ b/arch/ia64/kernel/semaphore.c
@@ -137,6 +137,61 @@ int __sched __down_interruptible (struct semaphore * sem)
 	return retval;
 }
 
+int __sched __down_killable (struct semaphore * sem)
+{
+	int retval = 0;
+	struct task_struct *tsk = current;
+	DECLARE_WAITQUEUE(wait, tsk);
+	unsigned long flags;
+
+	tsk->state = TASK_KILLABLE;
+	spin_lock_irqsave(&sem->wait.lock, flags);
+	add_wait_queue_exclusive_locked(&sem->wait, &wait);
+
+	sem->sleepers ++;
+	for (;;) {
+		int sleepers = sem->sleepers;
+
+		/*
+		 * With signals pending, this turns into
+		 * the trylock failure case - we won't be
+		 * sleeping, and we* can't get the lock as
+		 * it has contention. Just correct the count
+		 * and exit.
+		 */
+		if (fatal_signal_pending(current)) {
+			retval = -EINTR;
+			sem->sleepers = 0;
+			atomic_add(sleepers, &sem->count);
+			break;
+		}
+
+		/*
+		 * Add "everybody else" into it. They aren't
+		 * playing, because we own the spinlock in
+		 * wait_queue_head. The "-1" is because we're
+		 * still hoping to get the semaphore.
+		 */
+		if (!atomic_add_negative(sleepers - 1, &sem->count)) {
+			sem->sleepers = 0;
+			break;
+		}
+		sem->sleepers = 1;	/* us - see -1 above */
+		spin_unlock_irqrestore(&sem->wait.lock, flags);
+
+		schedule();
+
+		spin_lock_irqsave(&sem->wait.lock, flags);
+		tsk->state = TASK_KILLABLE;
+	}
+	remove_wait_queue_locked(&sem->wait, &wait);
+	wake_up_locked(&sem->wait);
+	spin_unlock_irqrestore(&sem->wait.lock, flags);
+
+	tsk->state = TASK_RUNNING;
+	return retval;
+}
+
 /*
  * Trylock failed - make sure we correct for having decremented the
  * count.
diff --git a/include/asm-ia64/semaphore.h b/include/asm-ia64/semaphore.h
index d8393d1..45e460b 100644
--- a/include/asm-ia64/semaphore.h
+++ b/include/asm-ia64/semaphore.h
@@ -49,6 +49,7 @@ init_MUTEX_LOCKED (struct semaphore *sem)
 
 extern void __down (struct semaphore * sem);
 extern int  __down_interruptible (struct semaphore * sem);
+extern int  __down_killable (struct semaphore * sem);
 extern int  __down_trylock (struct semaphore * sem);
 extern void __up (struct semaphore * sem);
 
@@ -80,6 +81,17 @@ down_interruptible (struct semaphore * sem)
 }
 
 static inline int
+down_killable (struct semaphore * sem)
+{
+	int ret = 0;
+
+	might_sleep();
+	if (ia64_fetchadd(-1, &sem->count.counter, acq) < 1)
+		ret = __down_killable(sem);
+	return ret;
+}
+
+static inline int
 down_trylock (struct semaphore *sem)
 {
 	int ret = 0;
-- 
1.5.3.8

