linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gustavo Padovan <padovan@profusion.mobi>
To: Andrei Emeltchenko <andrei.emeltchenko.news@gmail.com>,
	linux-bluetooth@vger.kernel.org
Subject: Re: [RFC 19/22] Bluetooth: Change l2cap chan_list to use RCU
Date: Mon, 19 Dec 2011 11:53:30 -0200	[thread overview]
Message-ID: <20111219135330.GD2610@joana> (raw)
In-Reply-To: <20111219104254.GC3982@aemeltch-MOBL1>

Hi Andrei,

* Andrei Emeltchenko <andrei.emeltchenko.news@gmail.com> [2011-12-19 12:42:57 +0200]:

> Hi Gustavo,
> 
> On Sat, Dec 17, 2011 at 07:29:39PM -0200, Gustavo F. Padovan wrote:
> > From: "Gustavo F. Padovan" <padovan@profusion.mobi>
> > 
> > This list has much more reads than writes, so RCU makes senses here, also
> > it avoid deadlock against the socket lock.
> > 
> > Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
> > ---
> >  net/bluetooth/l2cap_core.c |  117 +++++++++++++++++++++----------------------
> >  1 files changed, 57 insertions(+), 60 deletions(-)
> > 
> > diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> > index d616519..a212295 100644
> > --- a/net/bluetooth/l2cap_core.c
> > +++ b/net/bluetooth/l2cap_core.c
> > @@ -89,24 +89,36 @@ static inline void chan_put(struct l2cap_chan *c)
> >  
> >  static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
> >  {
> > -	struct l2cap_chan *c;
> > +	struct l2cap_chan *c, *r = NULL;
> >  
> > -	list_for_each_entry(c, &conn->chan_l, list) {
> > -		if (c->dcid == cid)
> > -			return c;
> > +	rcu_read_lock();
> > +
> > +	list_for_each_entry_rcu(c, &conn->chan_l, list) {
> > +		if (c->dcid == cid) {
> > +			r = c;
> > +			break;
> > +		}
> >  	}
> > +
> > +	rcu_read_unlock();
> >  	return NULL;
> >  }
> >  
> >  static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
> >  {
> > -	struct l2cap_chan *c;
> > +	struct l2cap_chan *c, *r = NULL;
> >  
> > -	list_for_each_entry(c, &conn->chan_l, list) {
> > -		if (c->scid == cid)
> > -			return c;
> > +	rcu_read_lock();
> > +
> > +	list_for_each_entry_rcu(c, &conn->chan_l, list) {
> > +		if (c->scid == cid) {
> > +			r = c;
> > +			break;
> > +		}
> >  	}
> > -	return NULL;
> > +
> > +	rcu_read_unlock();
> > +	return r;
> >  }
> >  
> >  /* Find channel with given SCID.
> > @@ -115,34 +127,36 @@ static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 ci
> >  {
> >  	struct l2cap_chan *c;
> >  
> > -	mutex_lock(&conn->chan_lock);
> >  	c = __l2cap_get_chan_by_scid(conn, cid);
> >  	if (c)
> >  		lock_sock(c->sk);
> > -	mutex_unlock(&conn->chan_lock);
> >  	return c;
> >  }
> >  
> >  static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
> >  {
> > -	struct l2cap_chan *c;
> > +	struct l2cap_chan *c, *r = NULL;
> >  
> > -	list_for_each_entry(c, &conn->chan_l, list) {
> > -		if (c->ident == ident)
> > -			return c;
> > +	rcu_read_lock();
> > +
> > +	list_for_each_entry_rcu(c, &conn->chan_l, list) {
> > +		if (c->ident == ident) {
> > +			r = c;
> > +			break;
> > +		}
> >  	}
> > -	return NULL;
> > +
> > +	rcu_read_unlock();
> > +	return r;
> >  }
> >  
> >  static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
> >  {
> >  	struct l2cap_chan *c;
> >  
> > -	mutex_lock(&conn->chan_lock);
> >  	c = __l2cap_get_chan_by_ident(conn, ident);
> >  	if (c)
> >  		lock_sock(c->sk);
> > -	mutex_unlock(&conn->chan_lock);
> >  	return c;
> >  }
> >  
> > @@ -323,7 +337,7 @@ void l2cap_chan_destroy(struct l2cap_chan *chan)
> >  	chan_put(chan);
> >  }
> >  
> > -static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
> > +static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
> >  {
> >  	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
> >  			chan->psm, chan->dcid);
> > @@ -364,7 +378,7 @@ static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
> >  
> >  	chan_hold(chan);
> >  
> > -	list_add(&chan->list, &conn->chan_l);
> > +	list_add_rcu(&chan->list, &conn->chan_l);
> >  }
> >  
> >  /* Delete channel.
> > @@ -381,9 +395,9 @@ static void l2cap_chan_del(struct l2cap_chan *chan, int err)
> >  
> >  	if (conn) {
> >  		/* Delete from channel list */
> > -		mutex_lock(&conn->chan_lock);
> > -		list_del(&chan->list);
> > -		mutex_unlock(&conn->chan_lock);
> > +		list_del_rcu(&chan->list);
> > +		synchronize_rcu();
> > +
> >  		chan_put(chan);
> >  
> >  		chan->conn = NULL;
> > @@ -750,13 +764,13 @@ static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *c
> >  /* ---- L2CAP connections ---- */
> >  static void l2cap_conn_start(struct l2cap_conn *conn)
> >  {
> > -	struct l2cap_chan *chan, *tmp;
> > +	struct l2cap_chan *chan;
> >  
> >  	BT_DBG("conn %p", conn);
> >  
> > -	mutex_lock(&conn->chan_lock);
> > +	rcu_read_lock();
> >  
> > -	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
> > +	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
> >  		struct sock *sk = chan->sk;
> >  
> >  		bh_lock_sock(sk);
> > @@ -780,9 +794,7 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
> >  					&chan->conf_state)) {
> >  				/* l2cap_chan_close() calls list_del(chan)
> >  				 * so release the lock */
> > -				mutex_unlock(&conn->chan_lock);
> >  				l2cap_chan_close(chan, ECONNRESET);
> > -				utex_lock(&conn->chan_lock);
> 
> OK, I see why this works. BTW: IMO the mutex and rcu patches shall be
> amended, otherwise we do unneeded work of implementing mutexes and then
> changing them to RCU.

Indeed, but unfortunately I pushed the patches already and forgot to ammend
both path patches. I was trying to push this ASAP so you guys could rebase
your work on top of it.

	Gustavo

  reply	other threads:[~2011-12-19 13:53 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-17 21:29 [RFC 00/22] Bluetooth: change tasklets to workqueue Gustavo F. Padovan
2011-12-17 21:29 ` [RFC 01/22] Bluetooth: Process recv path in a workqueue instead of a tasklet Gustavo F. Padovan
2011-12-17 21:29   ` [RFC 02/22] Bluetooth: Replace spin_lock by mutex in hci_dev Gustavo F. Padovan
2011-12-17 21:29     ` [RFC 03/22] Bluetooth: Use delayed_work for connection timeout Gustavo F. Padovan
2011-12-17 21:29       ` [RFC 04/22] Bluetooth: Use delayed work for advertisiment cache timeout Gustavo F. Padovan
2011-12-17 21:29         ` [RFC 05/22] Bluetooth: hci_conn_auto_accept() doesn't need locking Gustavo F. Padovan
2011-12-17 21:29           ` [RFC 06/22] Bluetooth: Move L2CAP timers to workqueue Gustavo F. Padovan
2011-12-17 21:29             ` [RFC 07/22] Bluetooth: Don't use spin_lock socket lock anymore Gustavo F. Padovan
2011-12-17 21:29               ` [RFC 08/22] Bluetooth: Remove sk_backlog usage from L2CAP Gustavo F. Padovan
2011-12-17 21:29                 ` [RFC 09/22] Bluetooth: move hci_task_lock to mutex Gustavo F. Padovan
2011-12-17 21:29                   ` [RFC 10/22] Bluetooth: convert chan_lock " Gustavo F. Padovan
2011-12-17 21:29                     ` [RFC 11/22] Bluetooth: Use RCU to manipulate chan_list Gustavo F. Padovan
2011-12-17 21:29                       ` [RFC 12/22] Bluetooth: convert conn hash to RCU Gustavo F. Padovan
2011-12-17 21:29                         ` [RFC 13/22] Bluetooth: Don't disable tasklets to call hdev->notify() Gustavo F. Padovan
2011-12-17 21:29                           ` [RFC 14/22] Bluetooth: Move command task to workqueue Gustavo F. Padovan
2011-12-17 21:29                             ` [RFC 15/22] Bluetooth: convert tx_task " Gustavo F. Padovan
2011-12-17 21:29                               ` [RFC 16/22] Bluetooth: convert info timer to delayed_work Gustavo F. Padovan
2011-12-17 21:29                                 ` [RFC 17/22] Bluetooth: remove power_on work_struct Gustavo F. Padovan
2011-12-17 21:29                                   ` [RFC 18/22] Bluetooth: invert locking order in connect path Gustavo F. Padovan
2011-12-17 21:29                                     ` [RFC 19/22] Bluetooth: Change l2cap chan_list to use RCU Gustavo F. Padovan
2011-12-17 21:29                                       ` [RFC 20/22] Bluetooth: move power_off to system workqueue Gustavo F. Padovan
2011-12-17 21:29                                         ` [RFC 21/22] Bluetooth: Use new alloc_workqueue() Gustavo F. Padovan
2011-12-17 21:29                                           ` [RFC 22/22] Bluetooth: Remove work_add and work_del from hci_sysfs Gustavo F. Padovan
2011-12-17 22:15                                       ` [RFC 19/22] Bluetooth: Change l2cap chan_list to use RCU Marcel Holtmann
2011-12-19 10:42                                       ` Andrei Emeltchenko
2011-12-19 13:53                                         ` Gustavo Padovan [this message]
2011-12-17 22:13                                   ` [RFC 17/22] Bluetooth: remove power_on work_struct Marcel Holtmann
2012-01-26 13:20                       ` [RFC 11/22] Bluetooth: Use RCU to manipulate chan_list Andrei Emeltchenko
2011-12-19  9:58                     ` [RFC 10/22] Bluetooth: convert chan_lock to mutex Andrei Emeltchenko
2011-12-19  9:53               ` [RFC 07/22] Bluetooth: Don't use spin_lock socket lock anymore Andrei Emeltchenko
2011-12-19 11:05             ` [RFC 06/22] Bluetooth: Move L2CAP timers to workqueue Andrei Emeltchenko
2011-12-19 12:59               ` Ulisses Furquim
2011-12-17 21:34 ` [RFC 00/22] Bluetooth: change tasklets " Gustavo Padovan
2011-12-17 22:17 ` Marcel Holtmann

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=20111219135330.GD2610@joana \
    --to=padovan@profusion.mobi \
    --cc=andrei.emeltchenko.news@gmail.com \
    --cc=linux-bluetooth@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).