All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frederik Deweerdt <deweerdt@free.fr>
To: Andrew Morton <akpm@osdl.org>
Cc: linux-kernel@vger.kernel.org, acme@ghostprotocols.net,
	davem@davemloft.net, jet@gyve.org
Subject: [patch] Use rwsems instead of custom locking scheme in net/socket.c and net/dccp/ccid.c
Date: Thu, 10 Aug 2006 14:13:36 +0200	[thread overview]
Message-ID: <20060810121336.GB1462@slug> (raw)
In-Reply-To: <20060806030809.2cfb0b1e.akpm@osdl.org>

On Sun, Aug 06, 2006 at 03:08:09AM -0700, Andrew Morton wrote:
> 
> ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.18-rc3/2.6.18-rc3-mm2/
> 
Hi Andrew,

This patch aims at removing two implementations (spotted by Masatake YAMATO) of
pseudo-rwlocks using a spinlock_t and an atomic_t. One in net/socket.c
and another in net/bluetooth/af_bluetooth.c. I think that both could be
converted to rwsems, saving some lines of code.

Regards,
Frederik


Signed-off-by: Frederik Deweerdt <frederik.deweerdt@gmail.com>

 net/dccp/ccid.c |   63 ++++++++++++------------------------------------------------
 net/socket.c    |   58 +++++++------------------------------------------------
 2 files changed, 21 insertions(+), 100 deletions(-)

diff --git a/net/dccp/ccid.c b/net/dccp/ccid.c
--- a/net/dccp/ccid.c
+++ b/net/dccp/ccid.c
@@ -12,48 +12,11 @@
  */
 
 #include "ccid.h"
+#include <linux/rwsem.h>
 
 static struct ccid_operations *ccids[CCID_MAX];
-#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-static atomic_t ccids_lockct = ATOMIC_INIT(0);
-static DEFINE_SPINLOCK(ccids_lock);
+static DECLARE_RWSEM(ccids_sem);
 
-/*
- * The strategy is: modifications ccids vector are short, do not sleep and
- * veeery rare, but read access should be free of any exclusive locks.
- */
-static void ccids_write_lock(void)
-{
-	spin_lock(&ccids_lock);
-	while (atomic_read(&ccids_lockct) != 0) {
-		spin_unlock(&ccids_lock);
-		yield();
-		spin_lock(&ccids_lock);
-	}
-}
-
-static inline void ccids_write_unlock(void)
-{
-	spin_unlock(&ccids_lock);
-}
-
-static inline void ccids_read_lock(void)
-{
-	atomic_inc(&ccids_lockct);
-	spin_unlock_wait(&ccids_lock);
-}
-
-static inline void ccids_read_unlock(void)
-{
-	atomic_dec(&ccids_lockct);
-}
-
-#else
-#define ccids_write_lock() do { } while(0)
-#define ccids_write_unlock() do { } while(0)
-#define ccids_read_lock() do { } while(0)
-#define ccids_read_unlock() do { } while(0)
-#endif
 
 static kmem_cache_t *ccid_kmem_cache_create(int obj_size, const char *fmt,...)
 {
@@ -103,13 +66,13 @@ int ccid_register(struct ccid_operations
 	if (ccid_ops->ccid_hc_tx_slab == NULL)
 		goto out_free_rx_slab;
 
-	ccids_write_lock();
+	down_write(&ccids_sem);
 	err = -EEXIST;
 	if (ccids[ccid_ops->ccid_id] == NULL) {
 		ccids[ccid_ops->ccid_id] = ccid_ops;
 		err = 0;
 	}
-	ccids_write_unlock();
+	up_write(&ccids_sem);
 	if (err != 0)
 		goto out_free_tx_slab;
 
@@ -131,9 +94,9 @@ EXPORT_SYMBOL_GPL(ccid_register);
 
 int ccid_unregister(struct ccid_operations *ccid_ops)
 {
-	ccids_write_lock();
+	down_write(&ccids_sem);
 	ccids[ccid_ops->ccid_id] = NULL;
-	ccids_write_unlock();
+	up_write(&ccids_sem);
 
 	ccid_kmem_cache_destroy(ccid_ops->ccid_hc_tx_slab);
 	ccid_ops->ccid_hc_tx_slab = NULL;
@@ -152,15 +115,15 @@ struct ccid *ccid_new(unsigned char id, 
 	struct ccid_operations *ccid_ops;
 	struct ccid *ccid = NULL;
 
-	ccids_read_lock();
+	down_read(&ccids_sem);
 #ifdef CONFIG_KMOD
 	if (ccids[id] == NULL) {
 		/* We only try to load if in process context */
-		ccids_read_unlock();
+		up_read(&ccids_sem);
 		if (gfp & GFP_ATOMIC)
 			goto out;
 		request_module("net-dccp-ccid-%d", id);
-		ccids_read_lock();
+		down_read(&ccids_sem);
 	}
 #endif
 	ccid_ops = ccids[id];
@@ -170,7 +133,7 @@ #endif
 	if (!try_module_get(ccid_ops->ccid_owner))
 		goto out_unlock;
 
-	ccids_read_unlock();
+	up_read(&ccids_sem);
 
 	ccid = kmem_cache_alloc(rx ? ccid_ops->ccid_hc_rx_slab :
 				     ccid_ops->ccid_hc_tx_slab, gfp);
@@ -191,7 +154,7 @@ #endif
 out:
 	return ccid;
 out_unlock:
-	ccids_read_unlock();
+	up_read(&ccids_sem);
 	goto out;
 out_free_ccid:
 	kmem_cache_free(rx ? ccid_ops->ccid_hc_rx_slab :
@@ -235,10 +198,10 @@ static void ccid_delete(struct ccid *cci
 			ccid_ops->ccid_hc_tx_exit(sk);
 		kmem_cache_free(ccid_ops->ccid_hc_tx_slab,  ccid);
 	}
-	ccids_read_lock();
+	down_read(&ccids_sem);
 	if (ccids[ccid_ops->ccid_id] != NULL)
 		module_put(ccid_ops->ccid_owner);
-	ccids_read_unlock();
+	up_read(&ccids_sem);
 }
 
 void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk)
diff --git a/net/socket.c b/net/socket.c
index 53cb85b..bc52aeb 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -85,6 +85,7 @@ #include <linux/compat.h>
 #include <linux/kmod.h>
 #include <linux/audit.h>
 #include <linux/wireless.h>
+#include <linux/rwsem.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -143,50 +144,7 @@ #endif
 
 static struct net_proto_family *net_families[NPROTO];
 
-#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
-static atomic_t net_family_lockct = ATOMIC_INIT(0);
-static DEFINE_SPINLOCK(net_family_lock);
-
-/* The strategy is: modifications net_family vector are short, do not
-   sleep and veeery rare, but read access should be free of any exclusive
-   locks.
- */
-
-static void net_family_write_lock(void)
-{
-	spin_lock(&net_family_lock);
-	while (atomic_read(&net_family_lockct) != 0) {
-		spin_unlock(&net_family_lock);
-
-		yield();
-
-		spin_lock(&net_family_lock);
-	}
-}
-
-static __inline__ void net_family_write_unlock(void)
-{
-	spin_unlock(&net_family_lock);
-}
-
-static __inline__ void net_family_read_lock(void)
-{
-	atomic_inc(&net_family_lockct);
-	spin_unlock_wait(&net_family_lock);
-}
-
-static __inline__ void net_family_read_unlock(void)
-{
-	atomic_dec(&net_family_lockct);
-}
-
-#else
-#define net_family_write_lock() do { } while(0)
-#define net_family_write_unlock() do { } while(0)
-#define net_family_read_lock() do { } while(0)
-#define net_family_read_unlock() do { } while(0)
-#endif
-
+static DECLARE_RWSEM(net_family_sem);
 
 /*
  *	Statistics counters of the socket lists
@@ -1132,7 +1090,7 @@ #if defined(CONFIG_KMOD)
 	}
 #endif
 
-	net_family_read_lock();
+	down_read(&net_family_sem);
 	if (net_families[family] == NULL) {
 		err = -EAFNOSUPPORT;
 		goto out;
@@ -1185,7 +1143,7 @@ #endif
 		goto out_release;
 
 out:
-	net_family_read_unlock();
+	up_read(&net_family_sem);
 	return err;
 out_module_put:
 	module_put(net_families[family]->owner);
@@ -2034,13 +1992,13 @@ int sock_register(struct net_proto_famil
 		printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
 		return -ENOBUFS;
 	}
-	net_family_write_lock();
+	down_write(&net_family_sem);
 	err = -EEXIST;
 	if (net_families[ops->family] == NULL) {
 		net_families[ops->family]=ops;
 		err = 0;
 	}
-	net_family_write_unlock();
+	up_write(&net_family_sem);
 	printk(KERN_INFO "NET: Registered protocol family %d\n",
 	       ops->family);
 	return err;
@@ -2057,9 +2015,9 @@ int sock_unregister(int family)
 	if (family < 0 || family >= NPROTO)
 		return -1;
 
-	net_family_write_lock();
+	down_write(&net_family_sem);
 	net_families[family]=NULL;
-	net_family_write_unlock();
+	up_write(&net_family_sem);
 	printk(KERN_INFO "NET: Unregistered protocol family %d\n",
 	       family);
 	return 0;

  parent reply	other threads:[~2006-08-10 12:13 UTC|newest]

Thread overview: 131+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-06 10:08 2.6.18-rc3-mm2 Andrew Morton
2006-08-06 11:09 ` 2.6.18-rc3-mm2 Michal Piotrowski
2006-08-07  9:52   ` 2.6.18-rc3-mm2 Balbir Singh
2006-08-07 12:16     ` 2.6.18-rc3-mm2 Michal Piotrowski
2006-08-07 14:05       ` 2.6.18-rc3-mm2 Balbir Singh
2006-08-06 13:33 ` 2.6.18-rc3-mm2 Mattia Dongili
2006-08-06 14:55   ` 2.6.18-rc3-mm2 [BUG at mm/vmscan.c:383!] Hugh Dickins
2006-08-06 17:02     ` Mattia Dongili
2006-08-06 14:11 ` 2.6.18-rc3-mm2 Reuben Farrelly
     [not found] ` <b637ec0b0608060848k22af58cbo6f13cee19498c2d2@mail.gmail.com>
2006-08-06 19:09   ` 2.6.18-rc3-mm2 Andrew Morton
2006-08-07  2:18     ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-07 18:47       ` 2.6.18-rc3-mm2 Fabio Comolli
2006-08-07 19:00         ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-08 14:41           ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-08 17:42             ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-08 18:16               ` 2.6.18-rc3-mm2 Fabio Comolli
2006-08-08 18:24                 ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-08 18:36                   ` 2.6.18-rc3-mm2 Fabio Comolli
2006-08-09  3:47                     ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-09  7:11                       ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-09 19:47                       ` 2.6.18-rc3-mm2 Fabio Comolli
2006-08-09 20:13                         ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-08 20:32               ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-08 18:14           ` 2.6.18-rc3-mm2 Fabio Comolli
2006-08-06 22:42 ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-06 22:54   ` 2.6.18-rc3-mm2 Andrew Morton
2006-08-07  9:15     ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-07 20:34       ` 2.6.18-rc3-mm2 Rafael J. Wysocki
2006-08-07 20:55         ` 2.6.18-rc3-mm2 Andrew Morton
2006-08-08  5:21           ` 2.6.18-rc3-mm2 Jens Axboe
2006-08-07  2:18   ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-07  2:20     ` 2.6.18-rc3-mm2 Dmitry Torokhov
2006-08-07  2:07 ` 2.6.18-rc3-mm2 Grant Coady
2006-08-07  9:28 ` swsusp regression [Was: 2.6.18-rc3-mm2] Jiri Slaby
2006-08-07 16:23   ` Jason Lunz
2006-08-07 20:47     ` Rafael J. Wysocki
2006-08-07 20:47       ` Rafael J. Wysocki
2006-08-08  8:41       ` Jens Axboe
2006-08-08  9:49         ` Jiri Slaby
2006-08-08 10:43           ` Jens Axboe
2006-08-08 10:08       ` Jiri Slaby
2006-08-08 10:43         ` Jens Axboe
2006-08-08 10:43           ` Jens Axboe
2006-08-08 10:59           ` Rafael J. Wysocki
2006-08-08 11:04             ` Jens Axboe
2006-08-08 11:07               ` Jens Axboe
2006-08-08 11:16                 ` Rafael J. Wysocki
2006-08-08 11:16                   ` Rafael J. Wysocki
2006-08-08 11:19                   ` Jens Axboe
2006-08-08 13:50                     ` Rafael J. Wysocki
2006-08-08 13:50                       ` Rafael J. Wysocki
2006-08-08 14:06                       ` Jens Axboe
2006-08-08 16:41                         ` Jiri Slaby
2006-08-08 17:53                           ` Jens Axboe
2006-08-07 21:09     ` Jiri Slaby
2006-08-07 13:40 ` x86_64 command line truncated Andy Whitcroft
2006-08-07 14:05   ` Andi Kleen
2006-08-07 14:37     ` x86_64 command line truncated II Andi Kleen
2006-08-07 14:42       ` Andy Whitcroft
2006-08-07 14:46         ` Andi Kleen
2006-08-07 15:04           ` Andy Whitcroft
2006-08-07 15:12             ` [PATCH] x86_64 dirty fix to restore dual command line store Andy Whitcroft
2006-08-07 21:47               ` Keith Mannthey
2006-08-07 21:59                 ` Keith Mannthey
2006-08-07 14:38     ` x86_64 command line truncated Andy Whitcroft
2006-08-07 15:15       ` Andrew Morton
2006-08-07 15:58         ` Andi Kleen
2006-08-07 15:49 ` [-mm patch] make arch/i386/kernel/acpi/boot.c:acpi_force static Adrian Bunk
2006-08-07 16:07   ` Andi Kleen
2006-08-07 15:49 ` [-mm patch] make arch/i386/kernel/apic.c:enable_local_apic static Adrian Bunk
2006-08-07 15:49 ` [-mm patch] net/: make code static Adrian Bunk
2006-08-08  4:51   ` David Miller
2006-08-07 15:50 ` [-mm patch] drivers/crypto/geode-aes.c: cleanups Adrian Bunk
2006-08-07 19:38 ` resume from S3 regression [Was: 2.6.18-rc3-mm2] Mattia Dongili
2006-08-07 19:38   ` Mattia Dongili
2006-08-07 20:02   ` Andrew Morton
2006-08-07 20:57     ` Mattia Dongili
2006-08-07 22:09       ` Mattia Dongili
2006-08-07 21:04 ` [RFC: -mm patch] bcm43xx_main.c: remove 3 functions Adrian Bunk
2006-08-08 18:32   ` Michael Buesch
2006-08-08 19:42     ` Adrian Bunk
2006-08-09  4:47       ` Michael Buesch
2006-08-08 22:14     ` Jeff Garzik
2006-08-08 14:39 ` 2.6.18-rc3-mm2: reiserfs problem? Rafael J. Wysocki
2006-08-08 15:12   ` Andrew Morton
     [not found]   ` <20060804192540.17098.39244.stgit@warthog.cambridge.redhat.com>
2006-08-08 17:23     ` [PATCH] ReiserFS: Make sure all dentries refs are released before calling kill_block_super() David Howells
2006-08-08 23:16       ` Rafael J. Wysocki
2006-08-09 10:14         ` David Howells
2006-08-09 10:23           ` Rafael J. Wysocki
2006-08-09 11:00         ` David Howells
2006-08-09 13:43     ` [PATCH] ReiserFS: Make sure all dentries refs are released before calling kill_block_super() [try #2] David Howells
2006-08-09 21:56       ` Rafael J. Wysocki
2006-08-10 10:16         ` David Howells
2006-08-09 19:06 ` 2.6.18-rc3-mm2 - ext3 locking issue? Valdis.Kletnieks
2006-08-09 20:01   ` Andrew Morton
2006-08-09 20:43     ` Valdis.Kletnieks
2006-08-10  3:32       ` Valdis.Kletnieks
2006-08-10 11:40         ` Jiri Slaby
2006-08-10 15:27           ` Andrew Morton
2006-08-10 17:33             ` Mattia Dongili
2006-08-10 17:43               ` Jiri Slaby
2006-08-10 17:44               ` Valdis.Kletnieks
2006-08-11  6:17                 ` Andrew Morton
2006-08-11  6:55                   ` Valdis.Kletnieks
2006-08-11 22:39           ` Laurent Riffard
2006-08-15 23:38   ` Valdis.Kletnieks
2006-08-10  9:04 ` 2.6.18-rc3-mm2 - OOM storm Laurent Riffard
2006-08-10  9:19   ` Andrew Morton
2006-08-10 23:20     ` Laurent Riffard
2006-08-11 12:31       ` Laurent Riffard
2006-08-11 21:50         ` Mike Galbraith
2006-08-11  8:33     ` Mike Galbraith
2006-08-11  6:55       ` Andrew Morton
2006-08-11  9:37         ` Mike Galbraith
2006-08-12 15:07     ` [patch] " Mike Galbraith
2006-08-12 21:26       ` Laurent Riffard
2006-08-10 12:13 ` Frederik Deweerdt [this message]
2006-08-10 12:57   ` [patch] Use rwsems instead of custom locking scheme in net/socket.c and net/dccp/ccid.c David Miller
2006-08-10 13:19     ` Frederik Deweerdt
2006-08-10 13:43 ` 2.6.18-rc3-mm2 [oops: shrink_dcache_for_umount_subtree ?] Reuben Farrelly
2006-08-10 15:38   ` Andrew Morton
2006-08-10 17:38 ` 2.6.18-rc3-mm2 - IPV6_MULTIPLE_TABLES borked Valdis.Kletnieks
2006-08-10 20:02   ` Patrick McHardy
2006-08-10 21:44     ` Valdis.Kletnieks
2006-08-11  2:15 ` 2.6.18-rc3-mm2 - BUG in rt6_lookup() from ipv6_del_addr() Valdis.Kletnieks
2006-08-11  4:20   ` David Miller
2006-08-11 18:11 ` 2.6.18-rc3-mm2 Mark Haverkamp
2006-08-11 18:36   ` 2.6.18-rc3-mm2 Andrew Morton
2006-08-11 20:31     ` 2.6.18-rc3-mm2 Mark Haverkamp
2006-08-11 22:58       ` 2.6.18-rc3-mm2 Andrew Morton
2006-08-23 17:02         ` 2.6.18-rc3-mm2 Mark Haverkamp

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=20060810121336.GB1462@slug \
    --to=deweerdt@free.fr \
    --cc=acme@ghostprotocols.net \
    --cc=akpm@osdl.org \
    --cc=davem@davemloft.net \
    --cc=jet@gyve.org \
    --cc=linux-kernel@vger.kernel.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.