diff mbox series

[v3] RDMA/siw: Fix IPv6 addr_list locking

Message ID 20190828093841.21993-1-bmt@zurich.ibm.com (mailing list archive)
State Superseded
Headers show
Series [v3] RDMA/siw: Fix IPv6 addr_list locking | expand

Commit Message

Bernard Metzler Aug. 28, 2019, 9:38 a.m. UTC
Walking the address list of an inet6_dev requires
appropriate locking. Since the called function
siw_listen_address() may sleep, we have to use
rtnl_lock() instead of read_lock_bh().

Also introduces sanity checks if we got a device
from in_dev_get() or in6_dev_get().

Changes from v2:
- Use plain version of list_for_each_entry
  in exchange of list_for_each_entry_rcu.

Changes from v1:
- Remove rcu_read_lock()/_unlock().
- Add check for IFA_F_TENTATIVE and
  IFA_F_DEPRECATED flags.

Reported-by: Bart Van Assche <bvanassche@acm.org>
Fixes: 6c52fdc244b5 ("rdma/siw: connection management")
Signed-off-by: Bernard Metzler <bmt@zurich.ibm.com>
---
 drivers/infiniband/sw/siw/siw_cm.c | 31 +++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

Comments

Leon Romanovsky Aug. 28, 2019, 10:47 a.m. UTC | #1
On Wed, Aug 28, 2019 at 11:38:41AM +0200, Bernard Metzler wrote:
> Walking the address list of an inet6_dev requires
> appropriate locking. Since the called function
> siw_listen_address() may sleep, we have to use
> rtnl_lock() instead of read_lock_bh().
>
> Also introduces sanity checks if we got a device
> from in_dev_get() or in6_dev_get().
>
> Changes from v2:
> - Use plain version of list_for_each_entry
>   in exchange of list_for_each_entry_rcu.
>
> Changes from v1:
> - Remove rcu_read_lock()/_unlock().
> - Add check for IFA_F_TENTATIVE and
>   IFA_F_DEPRECATED flags.

You need to add changelogs after "---" line, they will be trimmed
automatically while applying to git.

Latest example:
https://lore.kernel.org/linux-rdma/26ae8c4006cb31ee8c26fb821451d43732c7a35a.camel@redhat.com/T/#m75d9725823fd3f437298528c427dcfc3a0fe9050

Thanks
Bernard Metzler Aug. 28, 2019, 12:59 p.m. UTC | #2
-----"Leon Romanovsky" <leon@kernel.org> wrote: -----

>To: "Bernard Metzler" <bmt@zurich.ibm.com>
>From: "Leon Romanovsky" <leon@kernel.org>
>Date: 08/28/2019 12:47PM
>Cc: linux-rdma@vger.kernel.org, bvanassche@acm.org, jgg@ziepe.ca,
>dledford@redhat.com
>Subject: [EXTERNAL] Re: [PATCH v3] RDMA/siw: Fix IPv6 addr_list
>locking
>
>On Wed, Aug 28, 2019 at 11:38:41AM +0200, Bernard Metzler wrote:
>> Walking the address list of an inet6_dev requires
>> appropriate locking. Since the called function
>> siw_listen_address() may sleep, we have to use
>> rtnl_lock() instead of read_lock_bh().
>>
>> Also introduces sanity checks if we got a device
>> from in_dev_get() or in6_dev_get().
>>
>> Changes from v2:
>> - Use plain version of list_for_each_entry
>>   in exchange of list_for_each_entry_rcu.
>>
>> Changes from v1:
>> - Remove rcu_read_lock()/_unlock().
>> - Add check for IFA_F_TENTATIVE and
>>   IFA_F_DEPRECATED flags.
>
>You need to add changelogs after "---" line, they will be trimmed
>automatically while applying to git.
>
Ah OK, next try...

Thanks for everybody's patience.

Bernard.
diff mbox series

Patch

diff --git a/drivers/infiniband/sw/siw/siw_cm.c b/drivers/infiniband/sw/siw/siw_cm.c
index 1db5ad3d9580..8c1931a57f4a 100644
--- a/drivers/infiniband/sw/siw/siw_cm.c
+++ b/drivers/infiniband/sw/siw/siw_cm.c
@@ -1962,6 +1962,10 @@  int siw_create_listen(struct iw_cm_id *id, int backlog)
 		struct sockaddr_in s_laddr, *s_raddr;
 		const struct in_ifaddr *ifa;
 
+		if (!in_dev) {
+			rv = -ENODEV;
+			goto out;
+		}
 		memcpy(&s_laddr, &id->local_addr, sizeof(s_laddr));
 		s_raddr = (struct sockaddr_in *)&id->remote_addr;
 
@@ -1991,22 +1995,27 @@  int siw_create_listen(struct iw_cm_id *id, int backlog)
 		struct sockaddr_in6 *s_laddr = &to_sockaddr_in6(id->local_addr),
 			*s_raddr = &to_sockaddr_in6(id->remote_addr);
 
+		if (!in6_dev) {
+			rv = -ENODEV;
+			goto out;
+		}
 		siw_dbg(id->device,
 			"laddr %pI6:%d, raddr %pI6:%d\n",
 			&s_laddr->sin6_addr, ntohs(s_laddr->sin6_port),
 			&s_raddr->sin6_addr, ntohs(s_raddr->sin6_port));
 
-		read_lock_bh(&in6_dev->lock);
+		rtnl_lock();
 		list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
-			struct sockaddr_in6 bind_addr;
-
+			if (ifp->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
+				continue;
 			if (ipv6_addr_any(&s_laddr->sin6_addr) ||
 			    ipv6_addr_equal(&s_laddr->sin6_addr, &ifp->addr)) {
-				bind_addr.sin6_family = AF_INET6;
-				bind_addr.sin6_port = s_laddr->sin6_port;
-				bind_addr.sin6_flowinfo = 0;
-				bind_addr.sin6_addr = ifp->addr;
-				bind_addr.sin6_scope_id = dev->ifindex;
+				struct sockaddr_in6 bind_addr  = {
+					.sin6_family = AF_INET6,
+					.sin6_port = s_laddr->sin6_port,
+					.sin6_flowinfo = 0,
+					.sin6_addr = ifp->addr,
+					.sin6_scope_id = dev->ifindex };
 
 				rv = siw_listen_address(id, backlog,
 						(struct sockaddr *)&bind_addr,
@@ -2015,12 +2024,12 @@  int siw_create_listen(struct iw_cm_id *id, int backlog)
 					listeners++;
 			}
 		}
-		read_unlock_bh(&in6_dev->lock);
-
+		rtnl_unlock();
 		in6_dev_put(in6_dev);
 	} else {
-		return -EAFNOSUPPORT;
+		rv = -EAFNOSUPPORT;
 	}
+out:
 	if (listeners)
 		rv = 0;
 	else if (!rv)