From: Frederik Deweerdt <deweerdt@free.fr>
To: linux-kernel@vger.kernel.org
Cc: akpm@osdl.org, acme@mandriva.com, marcel@holtmann.org, jet@gyve.org
Subject: [01/04 mm-patch, rfc] Add lightweight rwlock (was Re: [mm-patch] bluetooth: use GFP_ATOMIC in *_sock_create's sk_alloc)
Date: Fri, 28 Jul 2006 18:15:17 +0200 [thread overview]
Message-ID: <20060728161515.GA1227@slug> (raw)
In-Reply-To: <20060728.221252.265353941.jet@gyve.org>
[-- Attachment #1: Type: text/plain, Size: 3376 bytes --]
On Fri, Jul 28, 2006 at 10:12:52PM +0900, Masatake YAMATO wrote:
> Hi,
> > On Fri, Jul 28, 2006 at 06:17:56PM +0900, Masatake YAMATO wrote:
> > > > I think that the bluetooth-guard-bt_proto-with-rwlock.patch introduced the following
> > > > BUG:
> > > > [ 43.232000] BUG: sleeping function called from invalid context at mm/slab.c:2903
> > > > [ 43.232000] in_atomic():1, irqs_disabled():0
> > > > [ 43.232000] [<c0104114>] show_trace_log_lvl+0x197/0x1ba
> > > > [ 43.232000] [<c010415e>] show_trace+0x27/0x29
> > > > [ 43.232000] [<c010426e>] dump_stack+0x26/0x28
> > > > [ 43.232000] [<c011ad1c>] __might_sleep+0xa2/0xaa
> > > > [ 43.232000] [<c0173085>] __kmalloc+0x9c/0xb3
> > > > [ 43.232000] [<c02f9295>] sk_alloc+0x1bc/0x1de
> > > > [ 43.232000] [<c036d689>] hci_sock_create+0x42/0x8a
> > > > [ 43.236000] [<c0366f40>] bt_sock_create+0xb5/0x154
> > > > [ 43.236000] [<c02f69dc>] __sock_create+0x131/0x356
> > > > [ 43.236000] [<c02f6c2f>] sock_create+0x2e/0x30
> > > > [ 43.236000] [<c02f6c88>] sys_socket+0x27/0x53
> > > > [ 43.240000] [<c02f7db5>] sys_socketcall+0xa9/0x277
> > > > [ 43.240000] [<c0103131>] sysenter_past_esp+0x56/0x8d
> > > > [ 43.240000] [<b7f38410>] 0xb7f38410
> > > >
> > > >
> > > > This patch makes sk_alloc GFP_ATOMIC, because we are holding the bt_proto_rwlock, for
> > > > the following functions:
> > > > - bnep_sock_create
> > > > - cmtp_sock_create
> > > > - hci_sock_create
> > > > - hidp_sock_create
> > > > - l2cap_sock_create
> > > > - rfcomm_sock_create
> > > > - sco_sock_create
> > >
> > > There is very similar code in i net/socket.c(I guess some part of
> > > bluetooth/af_bluetooth.c is derived from net/socket.c):
> > >
> > [... skip net/socket.c code ...]
> > >
> > > So there are two ways to avoid the bug:
> > > 1. As proposed by Frederik, use sk_alloc with GFP_ATOMIC or
> > > 2. use net_family_{read|writ}_{lock|unlock} in af_bluetooth.c.
> > >
> > I'd say that using a net_family_* like function set is much better,
> > if only in terms of preemptibility.
> >
> > I'll write an implementation that allows to use the same code
> > in socket.c and in af_bluetooth.c
> >
> I found one more similar code set at net/dccp/ccid.c for the same purpose:
OK, thanks, I modified it too.
The following set of patches adds a struct lw_rwlock (for lightweight
rwlock) which contains a spin_lock_t and an atomic_t. It is defined
in include/linux/lw_rwlock.h.
This lw_rwlock aims at protecting structures that are modified very rarely
and quickly. This assumptions allow the reader to merely increment the
atomic_t, allowing it to sleep why the lw_rwlock is help.
There were already two users of this kind of API: net/socket.c to protect
'net_families' and net/dccp/ccid.c to protect 'ccids'. As suggested
by Masatake YAMATO, this looked like good way to protect 'bt_proto'
in net/bluetooth/af_bluetooth.c as well. This patchset aims at having
only one implementation in the kernel.
Signed-off-by: Frederik Deweerdt <frederik.deweerdt@gmail.com>
include/linux/lw_rwlock.h | 71 +++++++++++++++++++++++++++++++++++++++++++
net/bluetooth/af_bluetooth.c | 15 ++++-----
net/dccp/ccid.c | 63 +++++++-------------------------------
net/socket.c | 58 ++++-------------------------------
4 files changed, 100 insertions(+), 107 deletions(-)
[-- Attachment #2: lw_rwlock-include-file.patch --]
[-- Type: text/plain, Size: 1789 bytes --]
--- v2.6.18-rc2-mm1~ori/include/linux/lw_rwlock.h 1970-01-01 01:00:00.000000000 +0100
+++ v2.6.18-rc2-mm1/include/linux/lw_rwlock.h 2006-07-28 17:25:04.000000000 +0200
@@ -0,0 +1,71 @@
+#ifndef __LINUX_LW_RWLOCK_H
+#define __LINUX_LW_RWLOCK_H
+
+/*
+ * LightWeight readwriter lock.
+ * The strategy is: modifications while the lock is held are short, do not
+ * sleep and veeery rare, but read access should be free of any exclusive
+ * locks.
+ * The original implementation was written for net/socket.c
+ */
+
+#include <linux/spinlock.h>
+
+
+struct lw_rwlock {
+ /* tracks down the number of current readers */
+ atomic_t readers;
+ /* the actual lock, only held by writers */
+ spinlock_t lock;
+};
+
+#define __LW_RWLOCK_UNLOCKED(lockname) \
+ { { 0 }, __SPIN_LOCK_UNLOCKED(lockname) }
+
+#define lw_rwlock_init(x) \
+ do { *(x) = (lw_rwlock_t) __LW_RWLOCK_UNLOCKED(x); } while (0)
+
+#define DEFINE_LW_RWLOCK(x) \
+ struct lw_rwlock x = __LW_RWLOCK_UNLOCKED(x)
+
+#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
+
+static inline void lw_write_lock(struct lw_rwlock *l)
+{
+ spin_lock(&l->lock);
+ while (atomic_read(&l->readers) != 0) {
+ spin_unlock(&l->lock);
+
+ yield();
+
+ spin_lock(&l->lock);
+ }
+}
+
+static inline void lw_write_unlock(struct lw_rwlock *l)
+{
+ spin_unlock(&l->lock);
+}
+
+static inline void lw_read_lock(struct lw_rwlock *l)
+{
+ atomic_inc(&l->readers);
+ spin_unlock_wait(&l->lock);
+}
+
+static inline void lw_read_unlock(struct lw_rwlock *l)
+{
+ atomic_dec(&l->readers);
+}
+
+#else
+
+#define lw_write_lock(x) do { } while(0)
+#define lw_write_unlock(x) do { } while(0)
+#define lw_read_lock(x) do { } while(0)
+#define lw_read_unlock() do { } while(0)
+
+#endif /* CONFIG_SMP || CONFIG_PREEMPT */
+
+
+#endif /* __LINUX_LW_RWLOCK_H */
next prev parent reply other threads:[~2006-07-28 16:15 UTC|newest]
Thread overview: 124+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-27 8:56 2.6.18-rc2-mm1 Andrew Morton
2006-07-27 10:27 ` [PATCH] Require mmap handler for a.out executables (was Re: 2.6.18-rc2-mm1) Eugene Teo
2006-07-27 11:40 ` [patch -mm] s390: remove s390 touch_nmi_watchdog() define Heiko Carstens
2006-07-27 12:26 ` 2.6.18-rc2-mm1 Frederik Deweerdt
2006-07-27 12:39 ` [patch] fix "efi_init_e820_map undefined" warning Frederik Deweerdt
2006-07-27 13:12 ` Should cpuset ABBA deadlock fix be in 2.6.18-rc2-mmx? Paul Jackson
2006-07-27 18:22 ` Andrew Morton
2006-07-27 19:32 ` Paul Jackson
2006-07-27 13:32 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-27 18:59 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-29 12:15 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-29 12:17 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-28 8:17 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-28 8:34 ` 2.6.18-rc2-mm1 Andrew Morton
2006-07-28 18:49 ` 2.6.18-rc2-mm1 Matt Helsley
2006-07-28 19:53 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-28 20:39 ` 2.6.18-rc2-mm1 Matt Helsley
2006-07-28 21:34 ` 2.6.18-rc2-mm1 Andrew Morton
2006-07-29 2:04 ` 2.6.18-rc2-mm1 Valdis.Kletnieks
2006-07-29 22:34 ` 2.6.18-rc2-mm1 Shailabh Nagar
2006-07-29 23:38 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-28 17:57 ` 2.6.18-rc2-mm1 Matt Helsley
2006-07-27 14:04 ` 2.6.18-rc2-mm1 Andy Whitcroft
2006-07-27 14:48 ` 2.6.18-rc2-mm1 Andy Whitcroft
2006-07-27 15:37 ` [PATCH] highmem: fixed ip27-memory.c build error Yoichi Yuasa
2006-07-27 18:16 ` [-mm patch] arch/i386/pci/mmconfig.c: fixes Adrian Bunk
2006-07-28 8:09 ` 2.6.18-rc2-mm1 Reuben Farrelly
2006-07-28 8:35 ` [mm-patch] bluetooth: use GFP_ATOMIC in *_sock_create's sk_alloc Frederik Deweerdt
2006-07-28 9:00 ` Marcel Holtmann
2006-07-28 12:36 ` Frederik Deweerdt
2006-07-28 9:17 ` Masatake YAMATO
2006-07-28 12:32 ` Frederik Deweerdt
2006-07-28 13:12 ` Masatake YAMATO
2006-07-28 16:15 ` Frederik Deweerdt [this message]
2006-07-28 16:23 ` [02/04 mm-patch, rfc] Add lightweight rwlock (was Re: [mm-patch] bluetooth: use GFP_ATOMIC in *_sock_create's sk_alloc) Frederik Deweerdt
2006-07-28 16:28 ` [03/04 mm-patch, rfc] Add lightweight rwlock to net/dccp/ccid.c " Frederik Deweerdt
2006-07-28 16:33 ` [04/04 mm-patch, rfc] Add lightweight rwlock to net/bluetooth/af_bluetooth.c " Frederik Deweerdt
2006-07-31 7:06 ` [01/04 mm-patch, rfc] Add lightweight rwlock Masatake YAMATO
2006-08-01 9:06 ` Frederik Deweerdt
2006-07-28 8:56 ` 2.6.18-rc2-mm1 Michal Piotrowski
2006-07-28 9:23 ` 2.6.18-rc2-mm1 Andrew Morton
2006-07-28 15:53 ` [PATCH] 2.6.18-rc2-mm1 i386 add_memory_region undefined Valdis.Kletnieks
2006-07-28 18:20 ` 2.6.18-rc2-mm1 - hard lockups on Dell C840 Valdis.Kletnieks
2006-07-28 18:44 ` 2.6.18-rc2-mm1 timer int 0 doesn't work Paul Fulghum
2006-07-28 21:48 ` Andrew Morton
2006-07-28 22:10 ` Paul Fulghum
2006-07-28 23:38 ` Andi Kleen
2006-07-29 0:15 ` Paul Fulghum
2006-07-29 1:16 ` Paul Fulghum
2006-07-29 1:24 ` Andrew Morton
2006-07-29 2:37 ` Paul Fulghum
2006-07-29 2:58 ` Eric W. Biederman
2006-07-29 4:03 ` Ingo Molnar
2006-07-30 23:00 ` Steven Rostedt
2006-07-29 2:36 ` Andi Kleen
2006-07-29 15:33 ` Paul Fulghum
2006-07-29 19:50 ` Eric W. Biederman
2006-07-29 22:05 ` Paul Fulghum
2006-07-31 5:31 ` Andi Kleen
2006-07-31 13:32 ` Paul Fulghum
2006-07-28 19:46 ` Kubuntu's udev broken with 2.6.18-rc2-mm1 Andrew James Wade
2006-07-27 19:56 ` Andrew Morton
2006-07-27 20:12 ` Greg KH
2006-07-28 14:33 ` Andrew James Wade
2006-07-30 14:01 ` Laurent Riffard
2006-07-31 0:03 ` Greg KH
2006-07-31 2:27 ` Andrew James Wade
2006-07-31 3:37 ` Greg KH
2006-07-31 4:22 ` Andrew Morton
2006-07-31 4:35 ` Greg KH
2006-07-31 4:50 ` Andrew Morton
2006-07-31 5:15 ` Greg KH
2006-07-31 6:00 ` Andrew Morton
2006-07-31 7:54 ` bert hubert
2006-07-31 8:30 ` Jesper Juhl
2006-07-31 11:14 ` Alan Cox
2006-07-31 8:10 ` Laurent Riffard
2006-08-01 3:01 ` Andrew James Wade
2006-07-27 21:28 ` Valdis.Kletnieks
2006-07-29 17:48 ` [-mm patch] security/selinux/hooks.c: make 4 functions static Adrian Bunk
2006-07-30 0:37 ` James Morris
2006-07-29 17:58 ` swsusp regression (s2dsk) [Was: 2.6.18-rc2-mm1] Jiri Slaby
2006-07-29 17:58 ` Jiri Slaby
2006-07-29 17:58 ` Jiri Slaby
2006-07-29 18:59 ` Rafael J. Wysocki
2006-07-29 18:59 ` Rafael J. Wysocki
2006-07-29 23:06 ` Jiri Slaby
2006-07-29 23:06 ` Jiri Slaby
2006-07-29 23:10 ` Rafael J. Wysocki
2006-07-29 23:10 ` Rafael J. Wysocki
2006-07-29 23:59 ` Jiri Slaby
2006-07-29 23:59 ` Jiri Slaby
2006-07-30 0:03 ` Jiri Slaby
2006-07-30 0:03 ` Jiri Slaby
2006-07-29 23:22 ` Pavel Machek
2006-07-29 23:22 ` Pavel Machek
2006-07-29 23:58 ` Jiri Slaby
2006-07-29 23:58 ` Jiri Slaby
2006-07-30 0:06 ` Pavel Machek
2006-07-30 0:06 ` Pavel Machek
2006-07-30 7:31 ` Rafael J. Wysocki
2006-07-30 7:31 ` Rafael J. Wysocki
2006-07-30 8:08 ` Jiri Slaby
2006-07-30 8:08 ` Jiri Slaby
2006-07-30 9:28 ` Rafael J. Wysocki
2006-07-30 9:28 ` Rafael J. Wysocki
2006-07-30 10:54 ` Jiri Slaby
2006-07-30 10:54 ` Jiri Slaby
2006-07-30 11:08 ` Pavel Machek
2006-07-30 11:34 ` Rafael J. Wysocki
2006-07-30 11:34 ` Rafael J. Wysocki
2006-07-31 13:59 ` Takashi Iwai
2006-07-31 13:59 ` [Alsa-devel] " Takashi Iwai
2006-07-31 14:03 ` Pavel Machek
2006-07-30 11:36 ` James Courtier-Dutton
2006-07-30 11:36 ` James Courtier-Dutton
2006-07-30 11:35 ` 2.6.18-rc2-mm1 fails to reboot properly on Dell Latitude CPiA Christian Trefzer
2006-07-31 4:42 ` 2.6.18-rc2-mm1 Reuben Farrelly
2006-07-31 4:57 ` 2.6.18-rc2-mm1 Andrew Morton
2006-07-31 5:25 ` 2.6.18-rc2-mm1 Andi Kleen
2006-08-03 15:59 ` [2.6 patch] DVB_CORE must select I2C Adrian Bunk
2006-08-03 16:10 ` [v4l-dvb-maintainer] " Manu Abraham
2006-08-03 16:30 ` Trent Piepho
2006-08-03 19:13 ` Mauro Carvalho Chehab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20060728161515.GA1227@slug \
--to=deweerdt@free.fr \
--cc=acme@mandriva.com \
--cc=akpm@osdl.org \
--cc=jet@gyve.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marcel@holtmann.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.