netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
@ 2021-03-31  4:36 Matthew Wilcox (Oracle)
  2021-03-31  5:23 ` Manivannan Sadhasivam
  2021-03-31 21:40 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 10+ messages in thread
From: Matthew Wilcox (Oracle) @ 2021-03-31  4:36 UTC (permalink / raw)
  To: Bjorn Andersson, Manivannan Sadhasivam, Jakub Kicinski,
	David S. Miller, netdev, Eric Biggers
  Cc: Matthew Wilcox (Oracle)

The XArray interface is easier for this driver to use.  Also fixes a
bug reported by the improper use of GFP_ATOMIC.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 net/qrtr/qrtr.c | 42 ++++++++++++++----------------------------
 1 file changed, 14 insertions(+), 28 deletions(-)

diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index dfc820ee553a..4b46c69e14ab 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -20,6 +20,8 @@
 /* auto-bind range */
 #define QRTR_MIN_EPH_SOCKET 0x4000
 #define QRTR_MAX_EPH_SOCKET 0x7fff
+#define QRTR_EPH_PORT_RANGE \
+		XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
 
 /**
  * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
@@ -106,8 +108,7 @@ static LIST_HEAD(qrtr_all_nodes);
 static DEFINE_MUTEX(qrtr_node_lock);
 
 /* local port allocation management */
-static DEFINE_IDR(qrtr_ports);
-static DEFINE_MUTEX(qrtr_port_lock);
+static DEFINE_XARRAY_ALLOC(qrtr_ports);
 
 /**
  * struct qrtr_node - endpoint node
@@ -653,7 +654,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
 		port = 0;
 
 	rcu_read_lock();
-	ipc = idr_find(&qrtr_ports, port);
+	ipc = xa_load(&qrtr_ports, port);
 	if (ipc)
 		sock_hold(&ipc->sk);
 	rcu_read_unlock();
@@ -695,9 +696,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
 
 	__sock_put(&ipc->sk);
 
-	mutex_lock(&qrtr_port_lock);
-	idr_remove(&qrtr_ports, port);
-	mutex_unlock(&qrtr_port_lock);
+	xa_erase(&qrtr_ports, port);
 
 	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
 	 * wait for it to up increment the refcount */
@@ -716,29 +715,20 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
  */
 static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 {
-	u32 min_port;
 	int rc;
 
-	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
-		min_port = QRTR_MIN_EPH_SOCKET;
-		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
-		if (!rc)
-			*port = min_port;
+		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE,
+				GFP_KERNEL);
 	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
 		rc = -EACCES;
 	} else if (*port == QRTR_PORT_CTRL) {
-		min_port = 0;
-		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC);
+		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
 	} else {
-		min_port = *port;
-		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC);
-		if (!rc)
-			*port = min_port;
+		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
 	}
-	mutex_unlock(&qrtr_port_lock);
 
-	if (rc == -ENOSPC)
+	if (rc == -EBUSY)
 		return -EADDRINUSE;
 	else if (rc < 0)
 		return rc;
@@ -752,20 +742,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 static void qrtr_reset_ports(void)
 {
 	struct qrtr_sock *ipc;
-	int id;
-
-	mutex_lock(&qrtr_port_lock);
-	idr_for_each_entry(&qrtr_ports, ipc, id) {
-		/* Don't reset control port */
-		if (id == 0)
-			continue;
+	unsigned long index;
 
+	rcu_read_lock();
+	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
 		sock_hold(&ipc->sk);
 		ipc->sk.sk_err = ENETRESET;
 		ipc->sk.sk_error_report(&ipc->sk);
 		sock_put(&ipc->sk);
 	}
-	mutex_unlock(&qrtr_port_lock);
+	rcu_read_unlock();
 }
 
 /* Bind socket to address.
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2021-03-31  4:36 [PATCH] qrtr: Convert qrtr_ports from IDR to XArray Matthew Wilcox (Oracle)
@ 2021-03-31  5:23 ` Manivannan Sadhasivam
  2021-03-31 21:40 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 10+ messages in thread
From: Manivannan Sadhasivam @ 2021-03-31  5:23 UTC (permalink / raw)
  To: Matthew Wilcox (Oracle)
  Cc: Bjorn Andersson, Jakub Kicinski, David S. Miller, netdev, Eric Biggers

On Wed, Mar 31, 2021 at 05:36:42AM +0100, Matthew Wilcox (Oracle) wrote:
> The XArray interface is easier for this driver to use.  Also fixes a
> bug reported by the improper use of GFP_ATOMIC.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

Thanks,
Mani

> ---
>  net/qrtr/qrtr.c | 42 ++++++++++++++----------------------------
>  1 file changed, 14 insertions(+), 28 deletions(-)
> 
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index dfc820ee553a..4b46c69e14ab 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -20,6 +20,8 @@
>  /* auto-bind range */
>  #define QRTR_MIN_EPH_SOCKET 0x4000
>  #define QRTR_MAX_EPH_SOCKET 0x7fff
> +#define QRTR_EPH_PORT_RANGE \
> +		XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
>  
>  /**
>   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> @@ -106,8 +108,7 @@ static LIST_HEAD(qrtr_all_nodes);
>  static DEFINE_MUTEX(qrtr_node_lock);
>  
>  /* local port allocation management */
> -static DEFINE_IDR(qrtr_ports);
> -static DEFINE_MUTEX(qrtr_port_lock);
> +static DEFINE_XARRAY_ALLOC(qrtr_ports);
>  
>  /**
>   * struct qrtr_node - endpoint node
> @@ -653,7 +654,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
>  		port = 0;
>  
>  	rcu_read_lock();
> -	ipc = idr_find(&qrtr_ports, port);
> +	ipc = xa_load(&qrtr_ports, port);
>  	if (ipc)
>  		sock_hold(&ipc->sk);
>  	rcu_read_unlock();
> @@ -695,9 +696,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
>  
>  	__sock_put(&ipc->sk);
>  
> -	mutex_lock(&qrtr_port_lock);
> -	idr_remove(&qrtr_ports, port);
> -	mutex_unlock(&qrtr_port_lock);
> +	xa_erase(&qrtr_ports, port);
>  
>  	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
>  	 * wait for it to up increment the refcount */
> @@ -716,29 +715,20 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
>   */
>  static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  {
> -	u32 min_port;
>  	int rc;
>  
> -	mutex_lock(&qrtr_port_lock);
>  	if (!*port) {
> -		min_port = QRTR_MIN_EPH_SOCKET;
> -		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC);
> -		if (!rc)
> -			*port = min_port;
> +		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE,
> +				GFP_KERNEL);
>  	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
>  		rc = -EACCES;
>  	} else if (*port == QRTR_PORT_CTRL) {
> -		min_port = 0;
> -		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC);
> +		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
>  	} else {
> -		min_port = *port;
> -		rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC);
> -		if (!rc)
> -			*port = min_port;
> +		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
>  
> -	if (rc == -ENOSPC)
> +	if (rc == -EBUSY)
>  		return -EADDRINUSE;
>  	else if (rc < 0)
>  		return rc;
> @@ -752,20 +742,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  static void qrtr_reset_ports(void)
>  {
>  	struct qrtr_sock *ipc;
> -	int id;
> -
> -	mutex_lock(&qrtr_port_lock);
> -	idr_for_each_entry(&qrtr_ports, ipc, id) {
> -		/* Don't reset control port */
> -		if (id == 0)
> -			continue;
> +	unsigned long index;
>  
> +	rcu_read_lock();
> +	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
>  		sock_hold(&ipc->sk);
>  		ipc->sk.sk_err = ENETRESET;
>  		ipc->sk.sk_error_report(&ipc->sk);
>  		sock_put(&ipc->sk);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
> +	rcu_read_unlock();
>  }
>  
>  /* Bind socket to address.
> -- 
> 2.30.2
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2021-03-31  4:36 [PATCH] qrtr: Convert qrtr_ports from IDR to XArray Matthew Wilcox (Oracle)
  2021-03-31  5:23 ` Manivannan Sadhasivam
@ 2021-03-31 21:40 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 10+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-03-31 21:40 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: bjorn.andersson, manivannan.sadhasivam, kuba, davem, netdev, ebiggers

Hello:

This patch was applied to netdev/net-next.git (refs/heads/master):

On Wed, 31 Mar 2021 05:36:42 +0100 you wrote:
> The XArray interface is easier for this driver to use.  Also fixes a
> bug reported by the improper use of GFP_ATOMIC.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  net/qrtr/qrtr.c | 42 ++++++++++++++----------------------------
>  1 file changed, 14 insertions(+), 28 deletions(-)

Here is the summary with links:
  - qrtr: Convert qrtr_ports from IDR to XArray
    https://git.kernel.org/netdev/net-next/c/3cbf7530a163

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2021-03-30 18:04 ` Manivannan Sadhasivam
@ 2021-03-30 22:28   ` Bjorn Andersson
  0 siblings, 0 replies; 10+ messages in thread
From: Bjorn Andersson @ 2021-03-30 22:28 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: Matthew Wilcox, Jakub Kicinski, David S. Miller, netdev, Eric Biggers

On Tue 30 Mar 13:04 CDT 2021, Manivannan Sadhasivam wrote:

> On Fri, Jun 05, 2020 at 05:00:37AM -0700, Matthew Wilcox wrote:
> > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> > 
> > The XArray interface is easier for this driver to use.  Also fixes a
> > bug reported by the improper use of GFP_ATOMIC.
> > 
> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> 
> Patch looks good to me. Can you please rebase it on top of netdev or
> v5.12-rc2 and resend?
> 

I asked Jay from Qualcomm to do this a few days ago, so we should
hopefully see this on the list shortly.

Regards,
Bjorn

> Thanks,
> Mani
> 
> > ---
> >  net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
> >  1 file changed, 13 insertions(+), 26 deletions(-)
> > 
> > diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> > index 2d8d6131bc5f..488f8f326ee5 100644
> > --- a/net/qrtr/qrtr.c
> > +++ b/net/qrtr/qrtr.c
> > @@ -20,6 +20,7 @@
> >  /* auto-bind range */
> >  #define QRTR_MIN_EPH_SOCKET 0x4000
> >  #define QRTR_MAX_EPH_SOCKET 0x7fff
> > +#define QRTR_PORT_RANGE	XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
> >  
> >  /**
> >   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> > @@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
> >  static DEFINE_MUTEX(qrtr_node_lock);
> >  
> >  /* local port allocation management */
> > -static DEFINE_IDR(qrtr_ports);
> > -static DEFINE_MUTEX(qrtr_port_lock);
> > +static DEFINE_XARRAY_ALLOC(qrtr_ports);
> >  
> >  /**
> >   * struct qrtr_node - endpoint node
> > @@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
> >  		port = 0;
> >  
> >  	rcu_read_lock();
> > -	ipc = idr_find(&qrtr_ports, port);
> > +	ipc = xa_load(&qrtr_ports, port);
> >  	if (ipc)
> >  		sock_hold(&ipc->sk);
> >  	rcu_read_unlock();
> > @@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
> >  
> >  	__sock_put(&ipc->sk);
> >  
> > -	mutex_lock(&qrtr_port_lock);
> > -	idr_remove(&qrtr_ports, port);
> > -	mutex_unlock(&qrtr_port_lock);
> > +	xa_erase(&qrtr_ports, port);
> >  
> >  	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
> >  	 * wait for it to up increment the refcount */
> > @@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> >  {
> >  	int rc;
> >  
> > -	mutex_lock(&qrtr_port_lock);
> >  	if (!*port) {
> > -		rc = idr_alloc(&qrtr_ports, ipc,
> > -			       QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
> > -			       GFP_ATOMIC);
> > -		if (rc >= 0)
> > -			*port = rc;
> > +		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
> > +				GFP_KERNEL);
> >  	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
> >  		rc = -EACCES;
> >  	} else if (*port == QRTR_PORT_CTRL) {
> > -		rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
> > +		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
> >  	} else {
> > -		rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
> > -		if (rc >= 0)
> > -			*port = rc;
> > +		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
> >  	}
> > -	mutex_unlock(&qrtr_port_lock);
> >  
> > -	if (rc == -ENOSPC)
> > +	if (rc == -EBUSY)
> >  		return -EADDRINUSE;
> >  	else if (rc < 0)
> >  		return rc;
> > @@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> >  static void qrtr_reset_ports(void)
> >  {
> >  	struct qrtr_sock *ipc;
> > -	int id;
> > -
> > -	mutex_lock(&qrtr_port_lock);
> > -	idr_for_each_entry(&qrtr_ports, ipc, id) {
> > -		/* Don't reset control port */
> > -		if (id == 0)
> > -			continue;
> > +	unsigned long index;
> >  
> > +	rcu_read_lock();
> > +	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
> >  		sock_hold(&ipc->sk);
> >  		ipc->sk.sk_err = ENETRESET;
> >  		ipc->sk.sk_error_report(&ipc->sk);
> >  		sock_put(&ipc->sk);
> >  	}
> > -	mutex_unlock(&qrtr_port_lock);
> > +	rcu_read_unlock();
> >  }
> >  
> >  /* Bind socket to address.
> > -- 
> > 2.26.2
> > 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2020-06-05 12:00 Matthew Wilcox
  2020-06-05 16:39 ` Eric Biggers
  2020-06-05 16:43 ` Eric Dumazet
@ 2021-03-30 18:04 ` Manivannan Sadhasivam
  2021-03-30 22:28   ` Bjorn Andersson
  2 siblings, 1 reply; 10+ messages in thread
From: Manivannan Sadhasivam @ 2021-03-30 18:04 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Bjorn Andersson, Jakub Kicinski, David S. Miller, netdev, Eric Biggers

On Fri, Jun 05, 2020 at 05:00:37AM -0700, Matthew Wilcox wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> 
> The XArray interface is easier for this driver to use.  Also fixes a
> bug reported by the improper use of GFP_ATOMIC.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Patch looks good to me. Can you please rebase it on top of netdev or
v5.12-rc2 and resend?

Thanks,
Mani

> ---
>  net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
>  1 file changed, 13 insertions(+), 26 deletions(-)
> 
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index 2d8d6131bc5f..488f8f326ee5 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -20,6 +20,7 @@
>  /* auto-bind range */
>  #define QRTR_MIN_EPH_SOCKET 0x4000
>  #define QRTR_MAX_EPH_SOCKET 0x7fff
> +#define QRTR_PORT_RANGE	XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
>  
>  /**
>   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> @@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
>  static DEFINE_MUTEX(qrtr_node_lock);
>  
>  /* local port allocation management */
> -static DEFINE_IDR(qrtr_ports);
> -static DEFINE_MUTEX(qrtr_port_lock);
> +static DEFINE_XARRAY_ALLOC(qrtr_ports);
>  
>  /**
>   * struct qrtr_node - endpoint node
> @@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
>  		port = 0;
>  
>  	rcu_read_lock();
> -	ipc = idr_find(&qrtr_ports, port);
> +	ipc = xa_load(&qrtr_ports, port);
>  	if (ipc)
>  		sock_hold(&ipc->sk);
>  	rcu_read_unlock();
> @@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
>  
>  	__sock_put(&ipc->sk);
>  
> -	mutex_lock(&qrtr_port_lock);
> -	idr_remove(&qrtr_ports, port);
> -	mutex_unlock(&qrtr_port_lock);
> +	xa_erase(&qrtr_ports, port);
>  
>  	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
>  	 * wait for it to up increment the refcount */
> @@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  {
>  	int rc;
>  
> -	mutex_lock(&qrtr_port_lock);
>  	if (!*port) {
> -		rc = idr_alloc(&qrtr_ports, ipc,
> -			       QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
> -			       GFP_ATOMIC);
> -		if (rc >= 0)
> -			*port = rc;
> +		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
> +				GFP_KERNEL);
>  	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
>  		rc = -EACCES;
>  	} else if (*port == QRTR_PORT_CTRL) {
> -		rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
> +		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
>  	} else {
> -		rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
> -		if (rc >= 0)
> -			*port = rc;
> +		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
>  
> -	if (rc == -ENOSPC)
> +	if (rc == -EBUSY)
>  		return -EADDRINUSE;
>  	else if (rc < 0)
>  		return rc;
> @@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  static void qrtr_reset_ports(void)
>  {
>  	struct qrtr_sock *ipc;
> -	int id;
> -
> -	mutex_lock(&qrtr_port_lock);
> -	idr_for_each_entry(&qrtr_ports, ipc, id) {
> -		/* Don't reset control port */
> -		if (id == 0)
> -			continue;
> +	unsigned long index;
>  
> +	rcu_read_lock();
> +	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
>  		sock_hold(&ipc->sk);
>  		ipc->sk.sk_err = ENETRESET;
>  		ipc->sk.sk_error_report(&ipc->sk);
>  		sock_put(&ipc->sk);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
> +	rcu_read_unlock();
>  }
>  
>  /* Bind socket to address.
> -- 
> 2.26.2
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2020-08-13 10:48   ` Dmitry Vyukov
@ 2020-08-13 11:48     ` Matthew Wilcox
  0 siblings, 0 replies; 10+ messages in thread
From: Matthew Wilcox @ 2020-08-13 11:48 UTC (permalink / raw)
  To: Dmitry Vyukov
  Cc: Eric Dumazet, Bjorn Andersson, Manivannan Sadhasivam,
	Jakub Kicinski, David S. Miller, netdev, Eric Biggers,
	Necip Fazil Yildiran

On Thu, Aug 13, 2020 at 12:48:10PM +0200, Dmitry Vyukov wrote:
> On Fri, Jun 5, 2020 at 6:44 PM Eric Dumazet <eric.dumazet@gmail.com> wrote:
> >
> > On 6/5/20 5:00 AM, Matthew Wilcox wrote:
> > > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> > >
> > > The XArray interface is easier for this driver to use.  Also fixes a
> > > bug reported by the improper use of GFP_ATOMIC.
> > >
> >
> > This does not look stable candidate.
> >
> > If you try to add a Fixes: tag, you might discover that this bug is old,
> > and I do not believe XArray has been backported to stable branches ?
> >
> >
> > Please submit a fix suitable for old kernels (as old as v4.7)
> >
> > Then when net-next is open in ~2 weeks, the Xarray stuff can be proposed.
> >
> > Thanks.
> 
> Hello,
> 
> What is the status of this patch? Was it merged/superseded by another patch?
> I don't see it in linux-next nor in net-net.

I don't know.  I didn't understand Eric's response.

> Thanks
> 
> > > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> > > ---
> > >  net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
> > >  1 file changed, 13 insertions(+), 26 deletions(-)
> > >
> > > diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> > > index 2d8d6131bc5f..488f8f326ee5 100644
> > > --- a/net/qrtr/qrtr.c
> > > +++ b/net/qrtr/qrtr.c
> > > @@ -20,6 +20,7 @@
> > >  /* auto-bind range */
> > >  #define QRTR_MIN_EPH_SOCKET 0x4000
> > >  #define QRTR_MAX_EPH_SOCKET 0x7fff
> > > +#define QRTR_PORT_RANGE      XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
> > >
> > >  /**
> > >   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> > > @@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
> > >  static DEFINE_MUTEX(qrtr_node_lock);
> > >
> > >  /* local port allocation management */
> > > -static DEFINE_IDR(qrtr_ports);
> > > -static DEFINE_MUTEX(qrtr_port_lock);
> > > +static DEFINE_XARRAY_ALLOC(qrtr_ports);
> > >
> > >  /**
> > >   * struct qrtr_node - endpoint node
> > > @@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
> > >               port = 0;
> > >
> > >       rcu_read_lock();
> > > -     ipc = idr_find(&qrtr_ports, port);
> > > +     ipc = xa_load(&qrtr_ports, port);
> > >       if (ipc)
> > >               sock_hold(&ipc->sk);
> > >       rcu_read_unlock();
> > > @@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
> > >
> > >       __sock_put(&ipc->sk);
> > >
> > > -     mutex_lock(&qrtr_port_lock);
> > > -     idr_remove(&qrtr_ports, port);
> > > -     mutex_unlock(&qrtr_port_lock);
> > > +     xa_erase(&qrtr_ports, port);
> > >
> > >       /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
> > >        * wait for it to up increment the refcount */
> > > @@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> > >  {
> > >       int rc;
> > >
> > > -     mutex_lock(&qrtr_port_lock);
> > >       if (!*port) {
> > > -             rc = idr_alloc(&qrtr_ports, ipc,
> > > -                            QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
> > > -                            GFP_ATOMIC);
> > > -             if (rc >= 0)
> > > -                     *port = rc;
> > > +             rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
> > > +                             GFP_KERNEL);
> > >       } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
> > >               rc = -EACCES;
> > >       } else if (*port == QRTR_PORT_CTRL) {
> > > -             rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
> > > +             rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
> > >       } else {
> > > -             rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
> > > -             if (rc >= 0)
> > > -                     *port = rc;
> > > +             rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
> > >       }
> > > -     mutex_unlock(&qrtr_port_lock);
> > >
> > > -     if (rc == -ENOSPC)
> > > +     if (rc == -EBUSY)
> > >               return -EADDRINUSE;
> > >       else if (rc < 0)
> > >               return rc;
> > > @@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> > >  static void qrtr_reset_ports(void)
> > >  {
> > >       struct qrtr_sock *ipc;
> > > -     int id;
> > > -
> > > -     mutex_lock(&qrtr_port_lock);
> > > -     idr_for_each_entry(&qrtr_ports, ipc, id) {
> > > -             /* Don't reset control port */
> > > -             if (id == 0)
> > > -                     continue;
> > > +     unsigned long index;
> > >
> > > +     rcu_read_lock();
> > > +     xa_for_each_start(&qrtr_ports, index, ipc, 1) {
> > >               sock_hold(&ipc->sk);
> > >               ipc->sk.sk_err = ENETRESET;
> > >               ipc->sk.sk_error_report(&ipc->sk);
> > >               sock_put(&ipc->sk);
> > >       }
> > > -     mutex_unlock(&qrtr_port_lock);
> > > +     rcu_read_unlock();
> > >  }
> > >
> > >  /* Bind socket to address.
> > >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2020-06-05 16:43 ` Eric Dumazet
@ 2020-08-13 10:48   ` Dmitry Vyukov
  2020-08-13 11:48     ` Matthew Wilcox
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry Vyukov @ 2020-08-13 10:48 UTC (permalink / raw)
  To: Eric Dumazet, Matthew Wilcox
  Cc: Bjorn Andersson, Manivannan Sadhasivam, Jakub Kicinski,
	David S. Miller, netdev, Eric Biggers, Necip Fazil Yildiran

On Fri, Jun 5, 2020 at 6:44 PM Eric Dumazet <eric.dumazet@gmail.com> wrote:
>
> On 6/5/20 5:00 AM, Matthew Wilcox wrote:
> > From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> >
> > The XArray interface is easier for this driver to use.  Also fixes a
> > bug reported by the improper use of GFP_ATOMIC.
> >
>
> This does not look stable candidate.
>
> If you try to add a Fixes: tag, you might discover that this bug is old,
> and I do not believe XArray has been backported to stable branches ?
>
>
> Please submit a fix suitable for old kernels (as old as v4.7)
>
> Then when net-next is open in ~2 weeks, the Xarray stuff can be proposed.
>
> Thanks.

Hello,

What is the status of this patch? Was it merged/superseded by another patch?
I don't see it in linux-next nor in net-net.

Thanks

> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> > ---
> >  net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
> >  1 file changed, 13 insertions(+), 26 deletions(-)
> >
> > diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> > index 2d8d6131bc5f..488f8f326ee5 100644
> > --- a/net/qrtr/qrtr.c
> > +++ b/net/qrtr/qrtr.c
> > @@ -20,6 +20,7 @@
> >  /* auto-bind range */
> >  #define QRTR_MIN_EPH_SOCKET 0x4000
> >  #define QRTR_MAX_EPH_SOCKET 0x7fff
> > +#define QRTR_PORT_RANGE      XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
> >
> >  /**
> >   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> > @@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
> >  static DEFINE_MUTEX(qrtr_node_lock);
> >
> >  /* local port allocation management */
> > -static DEFINE_IDR(qrtr_ports);
> > -static DEFINE_MUTEX(qrtr_port_lock);
> > +static DEFINE_XARRAY_ALLOC(qrtr_ports);
> >
> >  /**
> >   * struct qrtr_node - endpoint node
> > @@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
> >               port = 0;
> >
> >       rcu_read_lock();
> > -     ipc = idr_find(&qrtr_ports, port);
> > +     ipc = xa_load(&qrtr_ports, port);
> >       if (ipc)
> >               sock_hold(&ipc->sk);
> >       rcu_read_unlock();
> > @@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
> >
> >       __sock_put(&ipc->sk);
> >
> > -     mutex_lock(&qrtr_port_lock);
> > -     idr_remove(&qrtr_ports, port);
> > -     mutex_unlock(&qrtr_port_lock);
> > +     xa_erase(&qrtr_ports, port);
> >
> >       /* Ensure that if qrtr_port_lookup() did enter the RCU read section we
> >        * wait for it to up increment the refcount */
> > @@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> >  {
> >       int rc;
> >
> > -     mutex_lock(&qrtr_port_lock);
> >       if (!*port) {
> > -             rc = idr_alloc(&qrtr_ports, ipc,
> > -                            QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
> > -                            GFP_ATOMIC);
> > -             if (rc >= 0)
> > -                     *port = rc;
> > +             rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
> > +                             GFP_KERNEL);
> >       } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
> >               rc = -EACCES;
> >       } else if (*port == QRTR_PORT_CTRL) {
> > -             rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
> > +             rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
> >       } else {
> > -             rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
> > -             if (rc >= 0)
> > -                     *port = rc;
> > +             rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
> >       }
> > -     mutex_unlock(&qrtr_port_lock);
> >
> > -     if (rc == -ENOSPC)
> > +     if (rc == -EBUSY)
> >               return -EADDRINUSE;
> >       else if (rc < 0)
> >               return rc;
> > @@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
> >  static void qrtr_reset_ports(void)
> >  {
> >       struct qrtr_sock *ipc;
> > -     int id;
> > -
> > -     mutex_lock(&qrtr_port_lock);
> > -     idr_for_each_entry(&qrtr_ports, ipc, id) {
> > -             /* Don't reset control port */
> > -             if (id == 0)
> > -                     continue;
> > +     unsigned long index;
> >
> > +     rcu_read_lock();
> > +     xa_for_each_start(&qrtr_ports, index, ipc, 1) {
> >               sock_hold(&ipc->sk);
> >               ipc->sk.sk_err = ENETRESET;
> >               ipc->sk.sk_error_report(&ipc->sk);
> >               sock_put(&ipc->sk);
> >       }
> > -     mutex_unlock(&qrtr_port_lock);
> > +     rcu_read_unlock();
> >  }
> >
> >  /* Bind socket to address.
> >

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2020-06-05 12:00 Matthew Wilcox
  2020-06-05 16:39 ` Eric Biggers
@ 2020-06-05 16:43 ` Eric Dumazet
  2020-08-13 10:48   ` Dmitry Vyukov
  2021-03-30 18:04 ` Manivannan Sadhasivam
  2 siblings, 1 reply; 10+ messages in thread
From: Eric Dumazet @ 2020-06-05 16:43 UTC (permalink / raw)
  To: Matthew Wilcox, Bjorn Andersson, Manivannan Sadhasivam,
	Jakub Kicinski, David S. Miller, netdev, Eric Biggers



On 6/5/20 5:00 AM, Matthew Wilcox wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> 
> The XArray interface is easier for this driver to use.  Also fixes a
> bug reported by the improper use of GFP_ATOMIC.
> 

This does not look stable candidate.

If you try to add a Fixes: tag, you might discover that this bug is old,
and I do not believe XArray has been backported to stable branches ?


Please submit a fix suitable for old kernels (as old as v4.7)

Then when net-next is open in ~2 weeks, the Xarray stuff can be proposed.

Thanks.

> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
>  1 file changed, 13 insertions(+), 26 deletions(-)
> 
> diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
> index 2d8d6131bc5f..488f8f326ee5 100644
> --- a/net/qrtr/qrtr.c
> +++ b/net/qrtr/qrtr.c
> @@ -20,6 +20,7 @@
>  /* auto-bind range */
>  #define QRTR_MIN_EPH_SOCKET 0x4000
>  #define QRTR_MAX_EPH_SOCKET 0x7fff
> +#define QRTR_PORT_RANGE	XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
>  
>  /**
>   * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
> @@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
>  static DEFINE_MUTEX(qrtr_node_lock);
>  
>  /* local port allocation management */
> -static DEFINE_IDR(qrtr_ports);
> -static DEFINE_MUTEX(qrtr_port_lock);
> +static DEFINE_XARRAY_ALLOC(qrtr_ports);
>  
>  /**
>   * struct qrtr_node - endpoint node
> @@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
>  		port = 0;
>  
>  	rcu_read_lock();
> -	ipc = idr_find(&qrtr_ports, port);
> +	ipc = xa_load(&qrtr_ports, port);
>  	if (ipc)
>  		sock_hold(&ipc->sk);
>  	rcu_read_unlock();
> @@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
>  
>  	__sock_put(&ipc->sk);
>  
> -	mutex_lock(&qrtr_port_lock);
> -	idr_remove(&qrtr_ports, port);
> -	mutex_unlock(&qrtr_port_lock);
> +	xa_erase(&qrtr_ports, port);
>  
>  	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
>  	 * wait for it to up increment the refcount */
> @@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  {
>  	int rc;
>  
> -	mutex_lock(&qrtr_port_lock);
>  	if (!*port) {
> -		rc = idr_alloc(&qrtr_ports, ipc,
> -			       QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
> -			       GFP_ATOMIC);
> -		if (rc >= 0)
> -			*port = rc;
> +		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
> +				GFP_KERNEL);
>  	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
>  		rc = -EACCES;
>  	} else if (*port == QRTR_PORT_CTRL) {
> -		rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
> +		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
>  	} else {
> -		rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
> -		if (rc >= 0)
> -			*port = rc;
> +		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
>  
> -	if (rc == -ENOSPC)
> +	if (rc == -EBUSY)
>  		return -EADDRINUSE;
>  	else if (rc < 0)
>  		return rc;
> @@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
>  static void qrtr_reset_ports(void)
>  {
>  	struct qrtr_sock *ipc;
> -	int id;
> -
> -	mutex_lock(&qrtr_port_lock);
> -	idr_for_each_entry(&qrtr_ports, ipc, id) {
> -		/* Don't reset control port */
> -		if (id == 0)
> -			continue;
> +	unsigned long index;
>  
> +	rcu_read_lock();
> +	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
>  		sock_hold(&ipc->sk);
>  		ipc->sk.sk_err = ENETRESET;
>  		ipc->sk.sk_error_report(&ipc->sk);
>  		sock_put(&ipc->sk);
>  	}
> -	mutex_unlock(&qrtr_port_lock);
> +	rcu_read_unlock();
>  }
>  
>  /* Bind socket to address.
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
  2020-06-05 12:00 Matthew Wilcox
@ 2020-06-05 16:39 ` Eric Biggers
  2020-06-05 16:43 ` Eric Dumazet
  2021-03-30 18:04 ` Manivannan Sadhasivam
  2 siblings, 0 replies; 10+ messages in thread
From: Eric Biggers @ 2020-06-05 16:39 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Bjorn Andersson, Manivannan Sadhasivam, Jakub Kicinski,
	David S. Miller, netdev

On Fri, Jun 05, 2020 at 05:00:37AM -0700, Matthew Wilcox wrote:
> From: "Matthew Wilcox (Oracle)" <willy@infradead.org>
> 
> The XArray interface is easier for this driver to use.  Also fixes a
> bug reported by the improper use of GFP_ATOMIC.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

This fixes https://lkml.kernel.org/r/000000000000a363b205a74ca6a2@google.com,
so please add:

Reported-by: syzbot+3eec59e770685e3dc879@syzkaller.appspotmail.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH] qrtr: Convert qrtr_ports from IDR to XArray
@ 2020-06-05 12:00 Matthew Wilcox
  2020-06-05 16:39 ` Eric Biggers
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Matthew Wilcox @ 2020-06-05 12:00 UTC (permalink / raw)
  To: Bjorn Andersson, Manivannan Sadhasivam, Jakub Kicinski,
	David S. Miller, netdev, Eric Biggers
  Cc: Matthew Wilcox (Oracle)

From: "Matthew Wilcox (Oracle)" <willy@infradead.org>

The XArray interface is easier for this driver to use.  Also fixes a
bug reported by the improper use of GFP_ATOMIC.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 net/qrtr/qrtr.c | 39 +++++++++++++--------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)

diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c
index 2d8d6131bc5f..488f8f326ee5 100644
--- a/net/qrtr/qrtr.c
+++ b/net/qrtr/qrtr.c
@@ -20,6 +20,7 @@
 /* auto-bind range */
 #define QRTR_MIN_EPH_SOCKET 0x4000
 #define QRTR_MAX_EPH_SOCKET 0x7fff
+#define QRTR_PORT_RANGE	XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
 
 /**
  * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
@@ -106,8 +107,7 @@ static LIST_HEAD(qrtr_all_nodes);
 static DEFINE_MUTEX(qrtr_node_lock);
 
 /* local port allocation management */
-static DEFINE_IDR(qrtr_ports);
-static DEFINE_MUTEX(qrtr_port_lock);
+static DEFINE_XARRAY_ALLOC(qrtr_ports);
 
 /**
  * struct qrtr_node - endpoint node
@@ -623,7 +623,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port)
 		port = 0;
 
 	rcu_read_lock();
-	ipc = idr_find(&qrtr_ports, port);
+	ipc = xa_load(&qrtr_ports, port);
 	if (ipc)
 		sock_hold(&ipc->sk);
 	rcu_read_unlock();
@@ -665,9 +665,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
 
 	__sock_put(&ipc->sk);
 
-	mutex_lock(&qrtr_port_lock);
-	idr_remove(&qrtr_ports, port);
-	mutex_unlock(&qrtr_port_lock);
+	xa_erase(&qrtr_ports, port);
 
 	/* Ensure that if qrtr_port_lookup() did enter the RCU read section we
 	 * wait for it to up increment the refcount */
@@ -688,25 +686,18 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 {
 	int rc;
 
-	mutex_lock(&qrtr_port_lock);
 	if (!*port) {
-		rc = idr_alloc(&qrtr_ports, ipc,
-			       QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET + 1,
-			       GFP_ATOMIC);
-		if (rc >= 0)
-			*port = rc;
+		rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_PORT_RANGE,
+				GFP_KERNEL);
 	} else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) {
 		rc = -EACCES;
 	} else if (*port == QRTR_PORT_CTRL) {
-		rc = idr_alloc(&qrtr_ports, ipc, 0, 1, GFP_ATOMIC);
+		rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL);
 	} else {
-		rc = idr_alloc(&qrtr_ports, ipc, *port, *port + 1, GFP_ATOMIC);
-		if (rc >= 0)
-			*port = rc;
+		rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL);
 	}
-	mutex_unlock(&qrtr_port_lock);
 
-	if (rc == -ENOSPC)
+	if (rc == -EBUSY)
 		return -EADDRINUSE;
 	else if (rc < 0)
 		return rc;
@@ -720,20 +711,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port)
 static void qrtr_reset_ports(void)
 {
 	struct qrtr_sock *ipc;
-	int id;
-
-	mutex_lock(&qrtr_port_lock);
-	idr_for_each_entry(&qrtr_ports, ipc, id) {
-		/* Don't reset control port */
-		if (id == 0)
-			continue;
+	unsigned long index;
 
+	rcu_read_lock();
+	xa_for_each_start(&qrtr_ports, index, ipc, 1) {
 		sock_hold(&ipc->sk);
 		ipc->sk.sk_err = ENETRESET;
 		ipc->sk.sk_error_report(&ipc->sk);
 		sock_put(&ipc->sk);
 	}
-	mutex_unlock(&qrtr_port_lock);
+	rcu_read_unlock();
 }
 
 /* Bind socket to address.
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-03-31 21:40 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31  4:36 [PATCH] qrtr: Convert qrtr_ports from IDR to XArray Matthew Wilcox (Oracle)
2021-03-31  5:23 ` Manivannan Sadhasivam
2021-03-31 21:40 ` patchwork-bot+netdevbpf
  -- strict thread matches above, loose matches on Subject: below --
2020-06-05 12:00 Matthew Wilcox
2020-06-05 16:39 ` Eric Biggers
2020-06-05 16:43 ` Eric Dumazet
2020-08-13 10:48   ` Dmitry Vyukov
2020-08-13 11:48     ` Matthew Wilcox
2021-03-30 18:04 ` Manivannan Sadhasivam
2021-03-30 22:28   ` Bjorn Andersson

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).