CINXE.COM
LKML: David Howells: [PATCH 8/12]: MUTEX: Rename DECLARE_MUTEX for kernel/ dir
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>LKML: David Howells: [PATCH 8/12]: MUTEX: Rename DECLARE_MUTEX for kernel/ dir</title><link href="/css/message.css" rel="stylesheet" type="text/css" /><link href="/css/wrap.css" rel="alternate stylesheet" type="text/css" title="wrap" /><link href="/css/nowrap.css" rel="stylesheet" type="text/css" title="nowrap" /><link href="/favicon.ico" rel="shortcut icon" /><script src="/js/simple-calendar.js" type="text/javascript"></script><script src="/js/styleswitcher.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="lkml.org : last 100 messages" href="/rss.php" /><link rel="alternate" type="application/rss+xml" title="lkml.org : last messages by David Howells" href="/groupie.php?aid=345" /><!--Matomo--><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//m.lkml.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!--End Matomo Code--></head><body onload="es.jasper.simpleCalendar.init();" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"><table border="0" cellpadding="0" cellspacing="0"><tr><td width="180" align="center"><a href="/"><img style="border:0;width:135px;height:32px" src="/images/toprowlk.gif" alt="lkml.org" /></a></td><td width="32">聽</td><td class="nb"><div><a class="nb" href="/lkml"> [lkml]</a> 聽 <a class="nb" href="/lkml/2005"> [2005]</a> 聽 <a class="nb" href="/lkml/2005/12"> [Dec]</a> 聽 <a class="nb" href="/lkml/2005/12/16"> [16]</a> 聽 <a class="nb" href="/lkml/last100"> [last100]</a> 聽 <a href="/rss.php"><img src="/images/rss-or.gif" border="0" alt="RSS Feed" /></a></div><div>Views: <a href="#" class="nowrap" onclick="setActiveStyleSheet('wrap');return false;">[wrap]</a><a href="#" class="wrap" onclick="setActiveStyleSheet('nowrap');return false;">[no wrap]</a> 聽 <a class="nb" href="/lkml/mheaders/2005/12/16/278" onclick="this.href='/lkml/headers'+'/2005/12/16/278';">[headers]</a>聽 <a href="/lkml/bounce/2005/12/16/278">[forward]</a>聽 </div></td><td width="32">聽</td></tr><tr><td valign="top"><div class="es-jasper-simpleCalendar" baseurl="/lkml/"></div><div class="threadlist">Messages in this thread</div><ul class="threadlist"><li class="root"><a href="/lkml/2005/12/16/277">First message in thread</a></li><li><a href="/lkml/2005/12/16/277">David Howells</a><ul><li><a href="/lkml/2005/12/16/270">David Howells</a></li><li><a href="/lkml/2005/12/16/271">David Howells</a></li><li><a href="/lkml/2005/12/16/272">David Howells</a></li><li><a href="/lkml/2005/12/16/273">David Howells</a></li><li><a href="/lkml/2005/12/16/274">David Howells</a></li><li><a href="/lkml/2005/12/16/275">David Howells</a></li><li><a href="/lkml/2005/12/16/276">David Howells</a></li><li class="origin"><a href="">David Howells</a></li><li><a href="/lkml/2005/12/16/279">David Howells</a></li><li><a href="/lkml/2005/12/16/280">David Howells</a></li><li><a href="/lkml/2005/12/16/281">David Howells</a></li><li><a href="/lkml/2005/12/16/282">David Howells</a></li><li><a href="/lkml/2005/12/16/335">Steven Rostedt</a><ul><li><a href="/lkml/2005/12/17/13">Linus Torvalds</a><ul><li><a href="/lkml/2005/12/17/60">David Howells</a><ul><li><a href="/lkml/2005/12/17/63">Linus Torvalds</a></li></ul></li></ul></li><li><a href="/lkml/2005/12/17/15">Nick Piggin</a><ul><li><a href="/lkml/2005/12/17/24">Steven Rostedt</a></li></ul></li></ul></li></ul></li></ul><div class="threadlist">Patch in this message</div><ul class="threadlist"><li><a href="/lkml/diff/2005/12/16/278/1">Get diff 1</a></li></ul></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerl.gif" width="32" height="32" alt="/" /></td><td class="c" rowspan="2" valign="top" style="padding-top: 1em"><table><tr><td><table><tr><td class="lp">Date</td><td class="rp" itemprop="datePublished">Fri, 16 Dec 2005 23:13:08 GMT</td></tr><tr><td class="lp">From</td><td class="rp" itemprop="author">David Howells <></td></tr><tr><td class="lp">Subject</td><td class="rp" itemprop="name">[PATCH 8/12]: MUTEX: Rename DECLARE_MUTEX for kernel/ dir</td></tr></table></td><td></td></tr></table><pre itemprop="articleBody">The attached patch renames DECLARE_MUTEX*() to DECLARE_SEM_MUTEX*() for the<br />kernel/ directory.<br /><br />Signed-Off-By: David Howells <dhowells@redhat.com><br />---<br />warthog>diffstat -p1 mutex-kernel-2615rc5-2.diff<br /> kernel/Makefile | 3 <br /> kernel/audit.c | 2 <br /> kernel/cpu.c | 2 <br /> kernel/cpuset.c | 4 <br /> kernel/irq/autoprobe.c | 2 <br /> kernel/kthread.c | 2 <br /> kernel/module.c | 4 <br /> kernel/mutex-cmpxchg.c | 298 +++++++++++++++++++++++++++++++++++++++++++++++++<br /> kernel/mutex-xchg.c | 220 ++++++++++++++++++++++++++++++++++++<br /> kernel/posix-timers.c | 2 <br /> kernel/power/main.c | 2 <br /> kernel/power/pm.c | 2 <br /> kernel/printk.c | 2 <br /> kernel/profile.c | 2 <br /> kernel/stop_machine.c | 2 <br /> 15 files changed, 535 insertions(+), 14 deletions(-)<br /><br />diff -uNrp linux-2.6.15-rc5/kernel/Makefile linux-2.6.15-rc5-mutex/kernel/Makefile<br />--- linux-2.6.15-rc5/kernel/Makefile 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/Makefile 2005-12-16 17:38:19.000000000 +0000<br />@@ -33,6 +33,9 @@ obj-$(CONFIG_CRASH_DUMP) += crash_dump.o<br /> obj-$(CONFIG_SECCOMP) += seccomp.o<br /> obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o<br /> <br />+obj-$(CONFIG_ARCH_CMPXCHG_MUTEX) += mutex-cmpxchg.o<br />+obj-$(CONFIG_ARCH_XCHG_MUTEX) += mutex-xchg.o<br />+<br /> ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y)<br /> # According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is<br /> # needed for x86 only. Why this used to be enabled for all architectures is beyond<br />diff -uNrp linux-2.6.15-rc5/kernel/audit.c linux-2.6.15-rc5-mutex/kernel/audit.c<br />--- linux-2.6.15-rc5/kernel/audit.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/audit.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -113,7 +113,7 @@ static DECLARE_WAIT_QUEUE_HEAD(audit_bac<br /> /* The netlink socket is only to be read by 1 CPU, which lets us assume<br /> * that list additions and deletions never happen simultaneously in<br /> * auditsc.c */<br />-DECLARE_MUTEX(audit_netlink_sem);<br />+DECLARE_SEM_MUTEX(audit_netlink_sem);<br /> <br /> /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting<br /> * audit records. Since printk uses a 1024 byte buffer, this buffer<br />diff -uNrp linux-2.6.15-rc5/kernel/cpu.c linux-2.6.15-rc5-mutex/kernel/cpu.c<br />--- linux-2.6.15-rc5/kernel/cpu.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/cpu.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -16,7 +16,7 @@<br /> #include <asm/semaphore.h><br /> <br /> /* This protects CPUs going up and down... */<br />-static DECLARE_MUTEX(cpucontrol);<br />+static DECLARE_SEM_MUTEX(cpucontrol);<br /> <br /> static struct notifier_block *cpu_chain;<br /> <br />diff -uNrp linux-2.6.15-rc5/kernel/cpuset.c linux-2.6.15-rc5-mutex/kernel/cpuset.c<br />--- linux-2.6.15-rc5/kernel/cpuset.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/cpuset.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -229,8 +229,8 @@ static struct super_block *cpuset_sb = N<br /> * such matters.<br /> */<br /> <br />-static DECLARE_MUTEX(manage_sem);<br />-static DECLARE_MUTEX(callback_sem);<br />+static DECLARE_SEM_MUTEX(manage_sem);<br />+static DECLARE_SEM_MUTEX(callback_sem);<br /> <br /> /*<br /> * A couple of forward declarations required, due to cyclic reference loop:<br />diff -uNrp linux-2.6.15-rc5/kernel/irq/autoprobe.c linux-2.6.15-rc5-mutex/kernel/irq/autoprobe.c<br />--- linux-2.6.15-rc5/kernel/irq/autoprobe.c 2005-08-30 13:56:39.000000000 +0100<br />+++ linux-2.6.15-rc5-mutex/kernel/irq/autoprobe.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -16,7 +16,7 @@<br /> * comes in on to an unassigned handler will get stuck with<br /> * "IRQ_WAITING" cleared and the interrupt disabled.<br /> */<br />-static DECLARE_MUTEX(probe_sem);<br />+static DECLARE_SEM_MUTEX(probe_sem);<br /> <br /> /**<br /> * probe_irq_on - begin an interrupt autodetect<br />diff -uNrp linux-2.6.15-rc5/kernel/kthread.c linux-2.6.15-rc5-mutex/kernel/kthread.c<br />--- linux-2.6.15-rc5/kernel/kthread.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/kthread.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -41,7 +41,7 @@ struct kthread_stop_info<br /> <br /> /* Thread stopping is done by setthing this var: lock serializes<br /> * multiple kthread_stop calls. */<br />-static DECLARE_MUTEX(kthread_stop_lock);<br />+static DECLARE_SEM_MUTEX(kthread_stop_lock);<br /> static struct kthread_stop_info kthread_stop_info;<br /> <br /> int kthread_should_stop(void)<br />diff -uNrp linux-2.6.15-rc5/kernel/module.c linux-2.6.15-rc5-mutex/kernel/module.c<br />--- linux-2.6.15-rc5/kernel/module.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/module.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -59,10 +59,10 @@<br /> static DEFINE_SPINLOCK(modlist_lock);<br /> <br /> /* List of modules, protected by module_mutex AND modlist_lock */<br />-static DECLARE_MUTEX(module_mutex);<br />+static DECLARE_SEM_MUTEX(module_mutex);<br /> static LIST_HEAD(modules);<br /> <br />-static DECLARE_MUTEX(notify_mutex);<br />+static DECLARE_SEM_MUTEX(notify_mutex);<br /> static struct notifier_block * module_notify_list;<br /> <br /> int register_module_notifier(struct notifier_block * nb)<br />diff -uNrp linux-2.6.15-rc5/kernel/mutex-cmpxchg.c linux-2.6.15-rc5-mutex/kernel/mutex-cmpxchg.c<br />--- linux-2.6.15-rc5/kernel/mutex-cmpxchg.c 1970-01-01 01:00:00.000000000 +0100<br />+++ linux-2.6.15-rc5-mutex/kernel/mutex-cmpxchg.c 2005-12-15 17:25:06.000000000 +0000<br />@@ -0,0 +1,298 @@<br />+/* mutex-cmpxchg.c: cmpxchg-based mutex slow paths<br />+ *<br />+ * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.<br />+ * Written by David Howells (dhowells@redhat.com)<br />+ *<br />+ * This program is free software; you can redistribute it and/or<br />+ * modify it under the terms of the GNU General Public License<br />+ * as published by the Free Software Foundation; either version<br />+ * 2 of the License, or (at your option) any later version.<br />+ */<br />+<br />+#include <linux/config.h><br />+#include <linux/sched.h><br />+#include <linux/module.h><br />+#include <linux/mutex.h><br />+<br />+struct mutex_waiter {<br />+ struct list_head list;<br />+ struct task_struct *task;<br />+};<br />+<br />+#define CONTENTION_FLAG 1UL<br />+<br />+/*<br />+ * wait for a token to be granted from a mutex<br />+ * - noinlined to improve the fastpath case<br />+ */<br />+static noinline fastcall __sched<br />+void __mutex_lock(struct mutex *mutex, unsigned long me)<br />+{<br />+ struct mutex_waiter waiter;<br />+ struct task_struct *tsk;<br />+ unsigned long flags, state, old;<br />+<br />+ me = (unsigned long) current_thread_info();<br />+<br />+ /* slow path */<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ do {<br />+ /* see if we can grab the mutex immediately */<br />+ state = __mutex_cmpxchg(mutex, 0, me);<br />+ if (!state) {<br />+ /* we did */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+ return;<br />+ }<br />+<br />+ if (state & CONTENTION_FLAG)<br />+ goto sleep; /* already flagged for contention */<br />+<br />+ /* attempt to mark the mutex as being contended<br />+ * - it may get released whilst we're doing this<br />+ */<br />+ do {<br />+ old = state;<br />+ state = __mutex_cmpxchg(mutex, state,<br />+ old | CONTENTION_FLAG);<br />+<br />+ } while (state && state != old);<br />+ } while (!state);<br />+<br />+sleep:<br />+ /* need to sleep; set up my own style of waitqueue */<br />+ tsk = current;<br />+ waiter.task = tsk;<br />+<br />+ get_task_struct(tsk);<br />+ list_add_tail(&waiter.list, &mutex->wait_list);<br />+<br />+ /* we don't need to touch the mutex struct anymore */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* wait to be given the mutex */<br />+ set_task_state(tsk, TASK_UNINTERRUPTIBLE);<br />+<br />+ for (;;) {<br />+ if (!waiter.task)<br />+ break;<br />+ schedule();<br />+ set_task_state(tsk, TASK_UNINTERRUPTIBLE);<br />+ }<br />+<br />+ tsk->state = TASK_RUNNING;<br />+}<br />+<br />+/*<br />+ * fast path for attempting to grab the mutex<br />+ */<br />+fastcall __sched void mutex_lock(struct mutex *mutex)<br />+{<br />+ unsigned long me = __mutex_owner();<br />+<br />+ if (likely(__mutex_cmpxchg(mutex, 0, me) == 0))<br />+ return;<br />+<br />+ /* tail-call the slow path */<br />+ __mutex_lock(mutex, me);<br />+}<br />+<br />+EXPORT_SYMBOL(mutex_lock);<br />+<br />+/*<br />+ * slow path to interruptibly wait for a mutex to be granted to the caller<br />+ * - noinlined to improve the fastpath case<br />+ */<br />+static noinline fastcall __sched<br />+int __mutex_lock_interruptible(struct mutex *mutex, unsigned long me)<br />+{<br />+ struct mutex_waiter waiter;<br />+ struct task_struct *tsk;<br />+ unsigned long flags, state, old;<br />+ int ret;<br />+<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ do {<br />+ /* see if we can grab the mutex immediately */<br />+ state = __mutex_cmpxchg(mutex, 0, me);<br />+ if (!state) {<br />+ /* we did */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+ return 0;<br />+ }<br />+<br />+ if (unlikely(signal_pending(current)))<br />+ goto early_interrupt;<br />+<br />+ if (state & CONTENTION_FLAG)<br />+ goto sleep; /* already flagged for contention */<br />+<br />+ /* attempt to mark the mutex as being contended<br />+ * - it may get released whilst we're doing this<br />+ */<br />+ do {<br />+ old = state;<br />+ state = __mutex_cmpxchg(mutex, state,<br />+ old | CONTENTION_FLAG);<br />+<br />+ } while (state && state != old);<br />+ } while (!state);<br />+<br />+sleep:<br />+ /* need to sleep; set up my own style of waitqueue */<br />+ tsk = current;<br />+ waiter.task = tsk;<br />+<br />+ get_task_struct(tsk);<br />+ list_add_tail(&waiter.list, &mutex->wait_list);<br />+<br />+ /* we don't need to touch the mutex struct anymore */<br />+ set_task_state(tsk, TASK_INTERRUPTIBLE);<br />+<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* wait to be given the mutex */<br />+ for (;;) {<br />+ if (!waiter.task)<br />+ break;<br />+ if (unlikely(signal_pending(current)))<br />+ goto interrupted;<br />+ schedule();<br />+ set_task_state(tsk, TASK_INTERRUPTIBLE);<br />+ }<br />+<br />+ tsk->state = TASK_RUNNING;<br />+ return 0;<br />+<br />+interrupted:<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+ list_del(&waiter.list);<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* we may still have been given the mutex */<br />+ ret = 0;<br />+ if (waiter.task) {<br />+ put_task_struct(current);<br />+ ret = -EINTR;<br />+ }<br />+ return ret;<br />+<br />+early_interrupt:<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+ return -EINTR;<br />+}<br />+<br />+/*<br />+ * fast path for attempting to grab the mutex interruptibly<br />+ */<br />+fastcall __sched<br />+int mutex_lock_interruptible(struct mutex *mutex)<br />+{<br />+ unsigned long me = __mutex_owner();<br />+<br />+ if (likely(__mutex_cmpxchg(mutex, 0, me) == 0))<br />+ return 0;<br />+<br />+ /* tail-call the slow path */<br />+ return __mutex_lock_interruptible(mutex, me);<br />+}<br />+<br />+EXPORT_SYMBOL(mutex_lock_interruptible);<br />+<br />+/*<br />+ * report an unlock that doesn't balance a lock in the right context<br />+ */<br />+#ifdef CONFIG_DEBUG_MUTEX_OWNER<br />+static void __bad_mutex_state(struct mutex *mutex)<br />+{<br />+ struct task_struct *owner =<br />+ ((struct thread_info *)<br />+ (mutex->state & ~CONTENTION_FLAG)<br />+ )->task;<br />+<br />+ BUG_ON(mutex->state == CONTENTION_FLAG);<br />+<br />+ if (!mutex->state) {<br />+ printk(KERN_ERR<br />+ "BUG: process %d [%s] releasing unowned mutex\n",<br />+ current->pid,<br />+ current->comm);<br />+ }<br />+ else {<br />+ printk(KERN_ERR<br />+ "BUG: process %d [%s] releasing mutex owned by %d [%s]\n",<br />+ current->pid,<br />+ current->comm,<br />+ owner->pid,<br />+ owner->comm);<br />+ }<br />+}<br />+#endif<br />+<br />+/*<br />+ * slow path to release a mutex that's under contention<br />+ * - the queue should not be empty<br />+ * - noinlined to improve the fastpath case<br />+ */<br />+static noinline fastcall __sched<br />+void __mutex_unlock(struct mutex *mutex, unsigned long me)<br />+{<br />+ struct mutex_waiter *waiter;<br />+ struct task_struct *tsk;<br />+ unsigned long flags, state;<br />+<br />+ /* slow path */<br />+#ifdef CONFIG_DEBUG_MUTEX_OWNER<br />+ if ((mutex->state & ~CONTENTION_FLAG) != me)<br />+ __bad_mutex_state(mutex);<br />+#endif<br />+<br />+ /* must prevent a race */<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ if (!list_empty(&mutex->wait_list)) {<br />+ /* grant the token to the process at the front of the queue */<br />+ waiter = list_entry(mutex->wait_list.next,<br />+ struct mutex_waiter, list);<br />+<br />+ /* we must be careful not to touch 'waiter' after we set ->task<br />+ * to NULL.<br />+ * - it is an allocated on the waiter's stack and may become<br />+ * invalid at any time after that point (due to a wakeup from<br />+ * another source).<br />+ */<br />+ list_del_init(&waiter->list);<br />+ tsk = waiter->task;<br />+ state = (unsigned long) tsk;<br />+ if (!list_empty(&waiter->list))<br />+ state |= CONTENTION_FLAG;<br />+ mutex->state = state;<br />+ smp_mb();<br />+ waiter->task = NULL;<br />+ wake_up_process(tsk);<br />+ put_task_struct(tsk);<br />+ }<br />+ else {<br />+ mutex->state = 0;<br />+ }<br />+<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+}<br />+<br />+/*<br />+ * fast path to release a mutex<br />+ */<br />+fastcall __sched<br />+void mutex_unlock(struct mutex *mutex)<br />+{<br />+ unsigned long me = __mutex_owner();<br />+<br />+ if (__mutex_cmpxchg(mutex, me, 0) == me)<br />+ return;<br />+<br />+ __mutex_unlock(mutex, me);<br />+}<br />+<br />+EXPORT_SYMBOL(mutex_unlock);<br />diff -uNrp linux-2.6.15-rc5/kernel/mutex-xchg.c linux-2.6.15-rc5-mutex/kernel/mutex-xchg.c<br />--- linux-2.6.15-rc5/kernel/mutex-xchg.c 1970-01-01 01:00:00.000000000 +0100<br />+++ linux-2.6.15-rc5-mutex/kernel/mutex-xchg.c 2005-12-15 19:44:13.000000000 +0000<br />@@ -0,0 +1,220 @@<br />+/* mutex-xchg.c: simple exchange-based mutex slow paths<br />+ *<br />+ * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.<br />+ * Written by David Howells (dhowells@redhat.com)<br />+ *<br />+ * This program is free software; you can redistribute it and/or<br />+ * modify it under the terms of the GNU General Public License<br />+ * as published by the Free Software Foundation; either version<br />+ * 2 of the License, or (at your option) any later version.<br />+ */<br />+<br />+#include <linux/config.h><br />+#include <linux/sched.h><br />+#include <linux/module.h><br />+#include <linux/mutex.h><br />+<br />+struct mutex_waiter {<br />+ struct list_head list;<br />+ struct task_struct *task;<br />+};<br />+<br />+/*<br />+ * wait for a token to be granted from a mutex<br />+ */<br />+noinline fastcall __sched<br />+void __mutex_lock(struct mutex *mutex)<br />+{<br />+ struct mutex_waiter waiter;<br />+ struct task_struct *tsk = current;<br />+ unsigned long flags;<br />+<br />+ /* set up my own style of waitqueue */<br />+ waiter.task = tsk;<br />+<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ if (mutex_trylock(mutex)) {<br />+ /* we got the mutex anyway */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+ return;<br />+ }<br />+<br />+ /* need to sleep */<br />+ get_task_struct(tsk);<br />+ list_add_tail(&waiter.list, &mutex->wait_list);<br />+<br />+ /* we don't need to touch the mutex struct anymore */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* wait to be given the mutex */<br />+ set_task_state(tsk, TASK_UNINTERRUPTIBLE);<br />+<br />+ for (;;) {<br />+ if (!waiter.task)<br />+ break;<br />+ schedule();<br />+ set_task_state(tsk, TASK_UNINTERRUPTIBLE);<br />+ }<br />+<br />+ tsk->state = TASK_RUNNING;<br />+}<br />+<br />+/*<br />+ * fast path for attempting to grab the mutex<br />+ */<br />+#if 0<br />+fastcall __sched<br />+void mutex_lock(struct mutex *mutex)<br />+{<br />+ if (!mutex_trylock(mutex))<br />+ __mutex_lock(mutex);<br />+}<br />+#endif<br />+<br />+EXPORT_SYMBOL(__mutex_lock);<br />+<br />+/*<br />+ * slow path to interruptibly wait for a mutex to be granted to the caller<br />+ */<br />+noinline fastcall __sched<br />+int __mutex_lock_interruptible(struct mutex *mutex)<br />+{<br />+ struct mutex_waiter waiter;<br />+ struct task_struct *tsk = current;<br />+ unsigned long flags;<br />+ int ret;<br />+<br />+ /* set up my own style of waitqueue */<br />+ waiter.task = tsk;<br />+<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ if (mutex_trylock(mutex)) {<br />+ /* we got the mutex anyway */<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+ return 0;<br />+ }<br />+<br />+ /* need to sleep */<br />+ get_task_struct(tsk);<br />+ list_add_tail(&waiter.list, &mutex->wait_list);<br />+<br />+ /* we don't need to touch the mutex struct anymore */<br />+ set_task_state(tsk, TASK_INTERRUPTIBLE);<br />+<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* wait to be given the mutex */<br />+ for (;;) {<br />+ if (!waiter.task)<br />+ break;<br />+ if (unlikely(signal_pending(current)))<br />+ goto interrupted;<br />+ schedule();<br />+ set_task_state(tsk, TASK_INTERRUPTIBLE);<br />+ }<br />+<br />+ tsk->state = TASK_RUNNING;<br />+ return 0;<br />+<br />+interrupted:<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+ list_del(&waiter.list);<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+<br />+ /* we may still have been given the mutex */<br />+ ret = 0;<br />+ if (waiter.task) {<br />+ put_task_struct(current);<br />+ ret = -EINTR;<br />+ }<br />+ return ret;<br />+}<br />+<br />+/*<br />+ * fast path for attempting to grab the mutex interruptibly<br />+ */<br />+#if 0<br />+fastcall __sched<br />+int mutex_lock_interruptible(struct mutex *mutex)<br />+{<br />+ if (mutex_trylock(mutex))<br />+ return 0;<br />+<br />+ return __mutex_lock_interruptible(mutex);<br />+}<br />+#endif<br />+<br />+EXPORT_SYMBOL(__mutex_lock_interruptible);<br />+<br />+/*<br />+ * report an up() that doesn't match a mutex_lock()<br />+ */<br />+#ifdef CONFIG_DEBUG_MUTEX_OWNER<br />+static void __bad_mutex_state(struct mutex *mutex)<br />+{<br />+ if (!mutex->__owner) {<br />+ printk(KERN_ERR<br />+ "BUG: process %d [%s] releasing unowned mutex\n",<br />+ current->pid,<br />+ current->comm);<br />+ }<br />+ else {<br />+ printk(KERN_ERR<br />+ "BUG: process %d [%s] releasing mutex owned by %d [%s]\n",<br />+ current->pid,<br />+ current->comm,<br />+ mutex->__owner->pid,<br />+ mutex->__owner->comm);<br />+ }<br />+}<br />+#endif<br />+<br />+/*<br />+ * release a mutex<br />+ */<br />+void fastcall __sched mutex_unlock(struct mutex *mutex)<br />+{<br />+ struct mutex_waiter *waiter;<br />+ struct task_struct *tsk;<br />+ unsigned long flags;<br />+<br />+#ifdef CONFIG_DEBUG_MUTEX_OWNER<br />+ if (mutex->__owner != current)<br />+ __bad_mutex_state(mutex);<br />+ mutex->__owner = NULL;<br />+#endif<br />+<br />+ /* must prevent a race */<br />+ spin_lock_irqsave(&mutex->wait_lock, flags);<br />+<br />+ if (!list_empty(&mutex->wait_list)) {<br />+ /* grant the token to the process at the front of the queue */<br />+ waiter = list_entry(mutex->wait_list.next,<br />+ struct mutex_waiter, list);<br />+<br />+ /* we must be careful not to touch 'waiter' after we set ->task<br />+ * to NULL.<br />+ * - it is an allocated on the waiter's stack and may become<br />+ * invalid at any time after that point (due to a wakeup from<br />+ * another source).<br />+ */<br />+ list_del_init(&waiter->list);<br />+ tsk = waiter->task;<br />+#ifdef CONFIG_DEBUG_MUTEX_OWNER<br />+ mutex->__owner = tsk;<br />+#endif<br />+ smp_mb();<br />+ waiter->task = NULL;<br />+ wake_up_process(tsk);<br />+ put_task_struct(tsk);<br />+ }<br />+ else {<br />+ __mutex_release(mutex);<br />+ }<br />+<br />+ spin_unlock_irqrestore(&mutex->wait_lock, flags);<br />+}<br />+<br />+EXPORT_SYMBOL(mutex_unlock);<br />diff -uNrp linux-2.6.15-rc5/kernel/posix-timers.c linux-2.6.15-rc5-mutex/kernel/posix-timers.c<br />--- linux-2.6.15-rc5/kernel/posix-timers.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/posix-timers.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -1312,7 +1312,7 @@ sys_clock_getres(clockid_t which_clock, <br /> static DECLARE_WAIT_QUEUE_HEAD(nanosleep_abs_wqueue);<br /> static DECLARE_WORK(clock_was_set_work, (void(*)(void*))clock_was_set, NULL);<br /> <br />-static DECLARE_MUTEX(clock_was_set_lock);<br />+static DECLARE_SEM_MUTEX(clock_was_set_lock);<br /> <br /> void clock_was_set(void)<br /> {<br />diff -uNrp linux-2.6.15-rc5/kernel/power/main.c linux-2.6.15-rc5-mutex/kernel/power/main.c<br />--- linux-2.6.15-rc5/kernel/power/main.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/power/main.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -22,7 +22,7 @@<br /> /*This is just an arbitrary number */<br /> #define FREE_PAGE_NUMBER (100)<br /> <br />-DECLARE_MUTEX(pm_sem);<br />+DECLARE_SEM_MUTEX(pm_sem);<br /> <br /> struct pm_ops *pm_ops;<br /> suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;<br />diff -uNrp linux-2.6.15-rc5/kernel/power/pm.c linux-2.6.15-rc5-mutex/kernel/power/pm.c<br />--- linux-2.6.15-rc5/kernel/power/pm.c 2005-12-08 16:23:55.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/power/pm.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -40,7 +40,7 @@ int pm_active;<br /> * until a resume but that will be fine.<br /> */<br /> <br />-static DECLARE_MUTEX(pm_devs_lock);<br />+static DECLARE_SEM_MUTEX(pm_devs_lock);<br /> static LIST_HEAD(pm_devs);<br /> <br /> /**<br />diff -uNrp linux-2.6.15-rc5/kernel/printk.c linux-2.6.15-rc5-mutex/kernel/printk.c<br />--- linux-2.6.15-rc5/kernel/printk.c 2005-12-08 16:23:56.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/printk.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -66,7 +66,7 @@ EXPORT_SYMBOL(oops_in_progress);<br /> * provides serialisation for access to the entire console<br /> * driver system.<br /> */<br />-static DECLARE_MUTEX(console_sem);<br />+static DECLARE_SEM_MUTEX(console_sem);<br /> struct console *console_drivers;<br /> /*<br /> * This is used for debugging the mess that is the VT code by<br />diff -uNrp linux-2.6.15-rc5/kernel/profile.c linux-2.6.15-rc5-mutex/kernel/profile.c<br />--- linux-2.6.15-rc5/kernel/profile.c 2005-08-30 13:56:40.000000000 +0100<br />+++ linux-2.6.15-rc5-mutex/kernel/profile.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -44,7 +44,7 @@ static cpumask_t prof_cpu_mask = CPU_MAS<br /> #ifdef CONFIG_SMP<br /> static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits);<br /> static DEFINE_PER_CPU(int, cpu_profile_flip);<br />-static DECLARE_MUTEX(profile_flip_mutex);<br />+static DECLARE_SEM_MUTEX(profile_flip_mutex);<br /> #endif /* CONFIG_SMP */<br /> <br /> static int __init profile_setup(char * str)<br />diff -uNrp linux-2.6.15-rc5/kernel/stop_machine.c linux-2.6.15-rc5-mutex/kernel/stop_machine.c<br />--- linux-2.6.15-rc5/kernel/stop_machine.c 2005-12-08 16:23:56.000000000 +0000<br />+++ linux-2.6.15-rc5-mutex/kernel/stop_machine.c 2005-12-15 17:14:57.000000000 +0000<br />@@ -23,7 +23,7 @@ enum stopmachine_state {<br /> static enum stopmachine_state stopmachine_state;<br /> static unsigned int stopmachine_num_threads;<br /> static atomic_t stopmachine_thread_ack;<br />-static DECLARE_MUTEX(stopmachine_mutex);<br />+static DECLARE_SEM_MUTEX(stopmachine_mutex);<br /> <br /> static int stopmachine(void *cpu)<br /> {<br />-<br />To unsubscribe from this list: send the line "unsubscribe linux-kernel" in<br />the body of a message to majordomo@vger.kernel.org<br />More majordomo info at <a href="http://vger.kernel.org/majordomo-info.html">http://vger.kernel.org/majordomo-info.html</a><br />Please read the FAQ at <a href="http://www.tux.org/lkml/">http://www.tux.org/lkml/</a><br /></pre></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerr.gif" width="32" height="32" alt="\" /></td></tr><tr><td align="right" valign="bottom"> 聽 </td></tr><tr><td align="right" valign="bottom">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerl.gif" width="32" height="32" alt="\" /></td><td class="c">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerr.gif" width="32" height="32" alt="/" /></td></tr><tr><td align="right" valign="top" colspan="2"> 聽 </td><td class="lm">Last update: 2005-12-17 00:18 聽聽 [from the cache]<br />漏2003-2020 <a href="http://blog.jasper.es/"><span itemprop="editor">Jasper Spaans</span></a>|hosted at <a href="https://www.digitalocean.com/?refcode=9a8e99d24cf9">Digital Ocean</a> and my Meterkast|<a href="http://blog.jasper.es/categories.html#lkml-ref">Read the blog</a></td><td>聽</td></tr></table><script language="javascript" src="/js/styleswitcher.js" type="text/javascript"></script></body></html>