Skip to contentSkip to conversation

sunrpc: clean up "swapper" xprt handling

PATCH Superseded Jeff Layton 3 participants 14 messages May 30, 2015 – Jun 4, 2015

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover sunrpc: clean up "swapper" xprt handling

This series is a (small) overhaul of the swap-over-NFS code. The main impetus is to fix the problem reported by Jerome Marchand. We currently hold the rcu_read_lock when calling xs_swapper and that's just plain wrong. The first patch in this series should fix that problem, and also clean up a bit of a layering violation.

The other focus of this set is to change how the swapper refcounting works. Right now, it's only tracked in the rpc_xprt, and there seem to be some gaps in its coverage -- places where we should taking or dropping references but aren't. This changes it so that the clnt tracks the number of swapfiles that it has, and the xprt tracks the number of "swappable" clients.

It also ensures that we only call sk_set_memalloc once per socket. I believe that's the correct thing to do as the main reason for the memalloc_socks counter is to track whether we have _any_ memalloc-enabled sockets.

There is still some work to be done here as I think there remains some potential for races between swapon/swapoff, and reconnect or migration events. That will take some careful thought that I haven't the time to spend on at the moment. I don't think this set will make those races any worse though.

Jeff Layton (4):
sunrpc: keep a count of swapfiles associated with the rpc_clnt
sunrpc: make xprt->swapper an atomic_t
sunrpc: if we're closing down a socket, clear memalloc on it first
sunrpc: lock xprt before trying to set memalloc on the sockets

 fs/nfs/file.c                | 11 ++------
 include/linux/sunrpc/clnt.h  |  1 +
 include/linux/sunrpc/sched.h | 16 +++++++++++
 include/linux/sunrpc/xprt.h  |  5 ++--
 net/sunrpc/clnt.c            | 67 ++++++++++++++++++++++++++++++++++++++------
 net/sunrpc/xprtsock.c        | 59 +++++++++++++++++++++++++++++---------
 6 files changed, 125 insertions(+), 34 deletions(-)
Signature · 1 line
2.4.1
#1 lore raw

PATCH 1/4 sunrpc: keep a count of swapfiles associated with the rpc_clnt

Jerome reported seeing a warning pop when working with a swapfile on NFS. The nfs_swap_activate can end up calling sk_set_memalloc while holding the rcu_read_lock and that function can sleep.

To fix that, we need to take a reference to the xprt while holding the rcu_read_lock, set the socket up for swapping and then drop that reference. But, xprt_put is not exported and having NFS deal with the underlying xprt is a bit of layering violation anyway.

Fix this by adding a set of activate/deactivate functions that take a rpc_clnt pointer instead of an rpc_xprt, and have nfs_swap_activate and nfs_swap_deactivate call those.

Also, add a per-rpc_clnt atomic counter to keep track of the number of active swapfiles associated with it. When the counter does a 0->1 transition, we enable swapping on the xprt, when we do a 1->0 transition we disable swapping on it.

This also allows us to be a bit more selective with the RPC_TASK_SWAPPER flag. If non-swapper and swapper clnts are sharing a xprt, then we only need to flag the tasks from the swapper clnt with that flag.

---
 fs/nfs/file.c                | 11 ++------
 include/linux/sunrpc/clnt.h  |  1 +
 include/linux/sunrpc/sched.h | 16 +++++++++++
 net/sunrpc/clnt.c            | 67 ++++++++++++++++++++++++++++++++++++++------
 net/sunrpc/xprtsock.c        |  1 -
 5 files changed, 77 insertions(+), 19 deletions(-)
fs/nfs/file.c +2 -9
index 8b8d83a526ce..7b26840ccfe1 100644
@@ -559,25 +559,18 @@ static int nfs_launder_page(struct page *page)
 static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
 						sector_t *span)
 {
-	int ret;
 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
 
 	*span = sis->pages;
 
-	rcu_read_lock();
-	ret = xs_swapper(rcu_dereference(clnt->cl_xprt), 1);
-	rcu_read_unlock();
-
-	return ret;
+	return rpc_clnt_swap_activate(clnt);
 }
 
 static void nfs_swap_deactivate(struct file *file)
 {
 	struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
 
-	rcu_read_lock();
-	xs_swapper(rcu_dereference(clnt->cl_xprt), 0);
-	rcu_read_unlock();
+	rpc_clnt_swap_deactivate(clnt);
 }
 #endif
 
include/linux/sunrpc/clnt.h +1 -0
index 598ba80ec30c..131032f15cc1 100644
@@ -56,6 +56,7 @@ struct rpc_clnt {
 	struct rpc_rtt *	cl_rtt;		/* RTO estimator data */
 	const struct rpc_timeout *cl_timeout;	/* Timeout strategy */
 
+	atomic_t		cl_swapper;	/* swapfile count */
 	int			cl_nodelen;	/* nodename length */
 	char 			cl_nodename[UNX_MAXNODENAME+1];
 	struct rpc_pipe_dir_head cl_pipedir_objects;
include/linux/sunrpc/sched.h +16 -0
index 5f1e6bd4c316..50472d716e72 100644
@@ -269,4 +269,20 @@ static inline void rpc_assign_waitqueue_name(struct rpc_wait_queue *q,
 }
 #endif
 
+#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
+int rpc_clnt_swap_activate(struct rpc_clnt *clnt);
+void rpc_clnt_swap_deactivate(struct rpc_clnt *clnt);
+#else
+static inline int
+rpc_clnt_swap_activate(struct rpc_clnt *clnt)
+{
+	return 0;
+}
+
+static inline void
+rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
+{
+}
+#endif /* CONFIG_SUNRPC_SWAP */
+
 #endif /* _LINUX_SUNRPC_SCHED_H_ */
net/sunrpc/clnt.c +58 -9

76 lines not on this page · raw message

net/sunrpc/xprtsock.c +0 -1
index 66891e32c5e3..b29703996028 100644
@@ -1992,7 +1992,6 @@ int xs_swapper(struct rpc_xprt *xprt, int enable)
 
 	return err;
 }
-EXPORT_SYMBOL_GPL(xs_swapper);
 #else
 static void xs_set_memalloc(struct rpc_xprt *xprt)
 {
Signature · 1 line
2.4.1
Acked-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] sunrpc: keep a count of swapfiles associated with the rpc_clnt

Quoted 18 lines · Jeff Layton

Jerome reported seeing a warning pop when working with a swapfile on NFS. The nfs_swap_activate can end up calling sk_set_memalloc while holding the rcu_read_lock and that function can sleep.

To fix that, we need to take a reference to the xprt while holding the rcu_read_lock, set the socket up for swapping and then drop that reference. But, xprt_put is not exported and having NFS deal with the underlying xprt is a bit of layering violation anyway.

Fix this by adding a set of activate/deactivate functions that take a rpc_clnt pointer instead of an rpc_xprt, and have nfs_swap_activate and nfs_swap_deactivate call those.

Also, add a per-rpc_clnt atomic counter to keep track of the number of active swapfiles associated with it. When the counter does a 0->1 transition, we enable swapping on the xprt, when we do a 1->0 transition we disable swapping on it.

Quoted 7 lines

This also allows us to be a bit more selective with the RPC_TASK_SWAPPER flag. If non-swapper and swapper clnts are sharing a xprt, then we only need to flag the tasks from the swapper clnt with that flag.

Signature · 2 lines
Mel Gorman
SUSE Labs
#3 lore raw

PATCH 2/4 sunrpc: make xprt->swapper an atomic_t

In reply to Jeff Layton

Split xs_swapper into enable/disable functions and eliminate the "enable" flag.

Currently, it's racy if you have multiple swapon/swapoff operations running in parallel over the same xprt. Also fix it so that we only set it to a memalloc socket on a 0->1 transition and only clear it on a 1->0 transition.

---
 include/linux/sunrpc/xprt.h |  5 +++--
 net/sunrpc/clnt.c           |  4 ++--
 net/sunrpc/xprtsock.c       | 38 +++++++++++++++++++++++++-------------
 3 files changed, 30 insertions(+), 17 deletions(-)
include/linux/sunrpc/xprt.h +3 -2
index 8b93ef53df3c..26b1624128ec 100644
@@ -180,7 +180,7 @@ struct rpc_xprt {
 	atomic_t		num_reqs;	/* total slots */
 	unsigned long		state;		/* transport state */
 	unsigned char		resvport   : 1; /* use a reserved port */
-	unsigned int		swapper;	/* we're swapping over this
+	atomic_t		swapper;	/* we're swapping over this
 						   transport */
 	unsigned int		bind_index;	/* bind function index */
 
@@ -345,7 +345,8 @@ void			xprt_release_rqst_cong(struct rpc_task *task);
 void			xprt_disconnect_done(struct rpc_xprt *xprt);
 void			xprt_force_disconnect(struct rpc_xprt *xprt);
 void			xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie);
-int			xs_swapper(struct rpc_xprt *xprt, int enable);
+int			xs_swapper_enable(struct rpc_xprt *xprt);
+void			xs_swapper_disable(struct rpc_xprt *xprt);
 
 bool			xprt_lock_connect(struct rpc_xprt *, struct rpc_task *, void *);
 void			xprt_unlock_connect(struct rpc_xprt *, void *);
net/sunrpc/clnt.c +2 -2
index 383cb778179f..804a75e71e84 100644
@@ -2492,7 +2492,7 @@ retry:
 			goto retry;
 		}
 
-		ret = xs_swapper(xprt, 1);
+		ret = xs_swapper_enable(xprt);
 		xprt_put(xprt);
 	}
 	return ret;
@@ -2519,7 +2519,7 @@ retry:
 			goto retry;
 		}
 
-		xs_swapper(xprt, 0);
+		xs_swapper_disable(xprt);
 		xprt_put(xprt);
 	}
 }
net/sunrpc/xprtsock.c +25 -13
index b29703996028..a2861bbfd319 100644
@@ -1966,31 +1966,43 @@ static void xs_set_memalloc(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
-	if (xprt->swapper)
+	if (atomic_read(&xprt->swapper))
 		sk_set_memalloc(transport->inet);
 }
 
 /**
- * xs_swapper - Tag this transport as being used for swap.
+ * xs_swapper_enable - Tag this transport as being used for swap.
  * @xprt: transport to tag
- * @enable: enable/disable
  *
+ * Take a reference to this transport on behalf of the rpc_clnt, and
+ * optionally mark it for swapping if it wasn't already.
  */
-int xs_swapper(struct rpc_xprt *xprt, int enable)
+int
+xs_swapper_enable(struct rpc_xprt *xprt)
 {
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
-	int err = 0;
 
-	if (enable) {
-		xprt->swapper++;
-		xs_set_memalloc(xprt);
-	} else if (xprt->swapper) {
-		xprt->swapper--;
-		sk_clear_memalloc(transport->inet);
-	}
+	if (atomic_inc_return(&xprt->swapper) == 1)
+		sk_set_memalloc(transport->inet);
+	return 0;
+}
 
-	return err;
+/**
+ * xs_swapper_disable - Untag this transport as being used for swap.
+ * @xprt: transport to tag
+ *
+ * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
+ * swapper refcount goes to 0, untag the socket as a memalloc socket.
+ */
+void
+xs_swapper_disable(struct rpc_xprt *xprt)
+{
+	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
+			xprt);
+
+	if (atomic_dec_and_test(&xprt->swapper))
+		sk_clear_memalloc(transport->inet);
 }
 #else
 static void xs_set_memalloc(struct rpc_xprt *xprt)
Signature · 1 line
2.4.1
#4 lore raw

Re: [PATCH 2/4] sunrpc: make xprt->swapper an atomic_t

Hi Jeff-

Quoted 53 lines · Jeff Layton

Split xs_swapper into enable/disable functions and eliminate the "enable" flag.

Currently, it's racy if you have multiple swapon/swapoff operations running in parallel over the same xprt. Also fix it so that we only set it to a memalloc socket on a 0->1 transition and only clear it on a 1->0 transition.

---

include/linux/sunrpc/xprt.h |  5 +++--
net/sunrpc/clnt.c           |  4 ++--
net/sunrpc/xprtsock.c       | 38 +++++++++++++++++++++++++-------------
3 files changed, 30 insertions(+), 17 deletions(-)
include/linux/sunrpc/xprt.h +3 -2
index 8b93ef53df3c..26b1624128ec 100644
@@ -180,7 +180,7 @@ struct rpc_xprt {
 	atomic_t		num_reqs;	/* total slots */
 	unsigned long		state;		/* transport state */
 	unsigned char		resvport   : 1; /* use a reserved port */
-	unsigned int		swapper;	/* we're swapping over this
+	atomic_t		swapper;	/* we're swapping over this
 						   transport */
 	unsigned int		bind_index;	/* bind function index */
 
@@ -345,7 +345,8 @@ void			xprt_release_rqst_cong(struct rpc_task *task);
 void			xprt_disconnect_done(struct rpc_xprt *xprt);
 void			xprt_force_disconnect(struct rpc_xprt *xprt);
 void			xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie);
-int			xs_swapper(struct rpc_xprt *xprt, int enable);
+int			xs_swapper_enable(struct rpc_xprt *xprt);
+void			xs_swapper_disable(struct rpc_xprt *xprt);
 
 bool			xprt_lock_connect(struct rpc_xprt *, struct rpc_task *, void *);
 void			xprt_unlock_connect(struct rpc_xprt *, void *);
net/sunrpc/clnt.c +1 -1
index 383cb778179f..804a75e71e84 100644
@@ -2492,7 +2492,7 @@ retry:
 			goto retry;
 		}
 
-		ret = xs_swapper(xprt, 1);
+		ret = xs_swapper_enable(xprt);
 		xprt_put(xprt);
 	}
 	return ret;

net/sunrpc/clnt.c retry: · line 2525

Quoted 8 lines
net/sunrpc/clnt.c +1 -1
@@ -2519,7 +2519,7 @@ retry:
 			goto retry;
 		}
 
-		xs_swapper(xprt, 0);
+		xs_swapper_disable(xprt);
 		xprt_put(xprt);
 	}
 }

Seems like xs_swapper() is specific to socket-based transports.

There’s no struct sock * to use as an argument with RDMA, so xs_swapper() would probably dereference garbage if “swapon” was invoked on a proto=rdma mount point.

Should these new functions be made members of the rpc_xprt_ops? The RDMA version of the methods can be made no-ops for now.

Quoted 68 lines
net/sunrpc/xprtsock.c +25 -13
index b29703996028..a2861bbfd319 100644
@@ -1966,31 +1966,43 @@ static void xs_set_memalloc(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
-	if (xprt->swapper)
+	if (atomic_read(&xprt->swapper))
 		sk_set_memalloc(transport->inet);
 }
 
 /**
- * xs_swapper - Tag this transport as being used for swap.
+ * xs_swapper_enable - Tag this transport as being used for swap.
 * @xprt: transport to tag
- * @enable: enable/disable
 *
+ * Take a reference to this transport on behalf of the rpc_clnt, and
+ * optionally mark it for swapping if it wasn't already.
 */
-int xs_swapper(struct rpc_xprt *xprt, int enable)
+int
+xs_swapper_enable(struct rpc_xprt *xprt)
 {
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
-	int err = 0;
 
-	if (enable) {
-		xprt->swapper++;
-		xs_set_memalloc(xprt);
-	} else if (xprt->swapper) {
-		xprt->swapper--;
-		sk_clear_memalloc(transport->inet);
-	}
+	if (atomic_inc_return(&xprt->swapper) == 1)
+		sk_set_memalloc(transport->inet);
+	return 0;
+}
 
-	return err;
+/**
+ * xs_swapper_disable - Untag this transport as being used for swap.
+ * @xprt: transport to tag
+ *
+ * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
+ * swapper refcount goes to 0, untag the socket as a memalloc socket.
+ */
+void
+xs_swapper_disable(struct rpc_xprt *xprt)
+{
+	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
+			xprt);
+
+	if (atomic_dec_and_test(&xprt->swapper))
+		sk_clear_memalloc(transport->inet);
 }
 #else
 static void xs_set_memalloc(struct rpc_xprt *xprt)
Signature · 6 lines
2.4.1

--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [PATCH 2/4] sunrpc: make xprt->swapper an atomic_t

Quoted 68 lines · On Sat, 30 May 2015 13:55:44 -0400 Chuck Lever <[email protected]> wrote:

Hi Jeff-

Quoted 53 lines · Jeff Layton

Split xs_swapper into enable/disable functions and eliminate the "enable" flag.

Currently, it's racy if you have multiple swapon/swapoff operations running in parallel over the same xprt. Also fix it so that we only set it to a memalloc socket on a 0->1 transition and only clear it on a 1->0 transition.

---

include/linux/sunrpc/xprt.h |  5 +++--
net/sunrpc/clnt.c           |  4 ++--
net/sunrpc/xprtsock.c       | 38 +++++++++++++++++++++++++-------------
3 files changed, 30 insertions(+), 17 deletions(-)
include/linux/sunrpc/xprt.h +3 -2
index 8b93ef53df3c..26b1624128ec 100644
@@ -180,7 +180,7 @@ struct rpc_xprt {
 	atomic_t		num_reqs;	/* total slots */
 	unsigned long		state;		/* transport state */
 	unsigned char		resvport   : 1; /* use a reserved port */
-	unsigned int		swapper;	/* we're swapping over this
+	atomic_t		swapper;	/* we're swapping over this
 						   transport */
 	unsigned int		bind_index;	/* bind function index */
 
@@ -345,7 +345,8 @@ void			xprt_release_rqst_cong(struct rpc_task *task);
 void			xprt_disconnect_done(struct rpc_xprt *xprt);
 void			xprt_force_disconnect(struct rpc_xprt *xprt);
 void			xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie);
-int			xs_swapper(struct rpc_xprt *xprt, int enable);
+int			xs_swapper_enable(struct rpc_xprt *xprt);
+void			xs_swapper_disable(struct rpc_xprt *xprt);
 
 bool			xprt_lock_connect(struct rpc_xprt *, struct rpc_task *, void *);
 void			xprt_unlock_connect(struct rpc_xprt *, void *);
net/sunrpc/clnt.c +1 -1
index 383cb778179f..804a75e71e84 100644
@@ -2492,7 +2492,7 @@ retry:
 			goto retry;
 		}
 
-		ret = xs_swapper(xprt, 1);
+		ret = xs_swapper_enable(xprt);
 		xprt_put(xprt);
 	}
 	return ret;

net/sunrpc/clnt.c retry: · line 2525

Quoted 8 lines
net/sunrpc/clnt.c +1 -1
@@ -2519,7 +2519,7 @@ retry:
 			goto retry;
 		}
 
-		xs_swapper(xprt, 0);
+		xs_swapper_disable(xprt);
 		xprt_put(xprt);
 	}
 }

Seems like xs_swapper() is specific to socket-based transports.

Quoted 7 lines

There’s no struct sock * to use as an argument with RDMA, so xs_swapper() would probably dereference garbage if “swapon” was invoked on a proto=rdma mount point.

Should these new functions be made members of the rpc_xprt_ops? The RDMA version of the methods can be made no-ops for now.

Oh, right -- rdma uses rpcrdma_xprt instead. Yeah, adding a new ops to handle this sounds like the right thing to do. I'll roll that into the v2 set.

Thanks!
Jeff

Quoted 81 lines
Quoted 64 lines
net/sunrpc/xprtsock.c +3 -3
index b29703996028..a2861bbfd319 100644
@@ -1966,31 +1966,43 @@ static void xs_set_memalloc(struct rpc_xprt
 *xprt) struct sock_xprt *transport = container_of(xprt, struct
 sock_xprt, xprt);
 
-	if (xprt->swapper)
+	if (atomic_read(&xprt->swapper))
 		sk_set_memalloc(transport->inet);
 }
 
 /**
- * xs_swapper - Tag this transport as being used for swap.
+ * xs_swapper_enable - Tag this transport as being used for swap.
 * @xprt: transport to tag
- * @enable: enable/disable
 *
+ * Take a reference to this transport on behalf of the rpc_clnt,

and
+ * optionally mark it for swapping if it wasn't already. */

net/sunrpc/xprtsock.c +2 -1
-int xs_swapper(struct rpc_xprt *xprt, int enable)
+int
+xs_swapper_enable(struct rpc_xprt *xprt)

{
struct sock_xprt *transport = container_of(xprt, struct
sock_xprt, xprt);

net/sunrpc/xprtsock.c +6 -9
-	int err = 0;
 
-	if (enable) {
-		xprt->swapper++;
-		xs_set_memalloc(xprt);
-	} else if (xprt->swapper) {
-		xprt->swapper--;
-		sk_clear_memalloc(transport->inet);
-	}
+	if (atomic_inc_return(&xprt->swapper) == 1)
+		sk_set_memalloc(transport->inet);
+	return 0;
+}
 
-	return err;
+/**
+ * xs_swapper_disable - Untag this transport as being used for

swap.
+ * @xprt: transport to tag
+ *
+ * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
+ * swapper refcount goes to 0, untag the socket as a memalloc socket.
+ */

net/sunrpc/xprtsock.c +4 -0
+void
+xs_swapper_disable(struct rpc_xprt *xprt)
+{
+	struct sock_xprt *transport = container_of(xprt, struct

sock_xprt,

net/sunrpc/xprtsock.c +4 -0
+			xprt);
+
+	if (atomic_dec_and_test(&xprt->swapper))
+		sk_clear_memalloc(transport->inet);
Quoted 10 lines

}
#else
static void xs_set_memalloc(struct rpc_xprt *xprt)

Signature · 6 lines
2.4.1

--
To unsubscribe from this list: send the line "unsubscribe
linux-nfs" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com

Signature · 1 line
Jeff Layton <[email protected]>
#6 lore raw

PATCH 3/4 sunrpc: if we're closing down a socket, clear memalloc on it first

In reply to Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

---
 net/sunrpc/xprtsock.c | 3 +++
 1 file changed, 3 insertions(+)
net/sunrpc/xprtsock.c +3 -0
index a2861bbfd319..359446442112 100644
@@ -827,6 +827,9 @@ static void xs_reset_transport(struct sock_xprt *transport)
 	if (sk == NULL)
 		return;
 
+	if (atomic_read(&transport->xprt.swapper))
+		sk_clear_memalloc(sk);
+
 	write_lock_bh(&sk->sk_callback_lock);
 	transport->inet = NULL;
 	transport->sock = NULL;
Signature · 1 line
2.4.1
Acked-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] sunrpc: if we're closing down a socket, clear memalloc on it first

Quoted 10 lines · Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

Signature · 2 lines
Mel Gorman
SUSE Labs
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] sunrpc: if we're closing down a socket, clear memalloc on it first

In reply to Jeff Layton
Quoted 10 lines · Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

Signature · 2 lines
Mel Gorman
SUSE Labs
#9 lore raw

Re: [PATCH 3/4] sunrpc: if we're closing down a socket, clear memalloc on it first

Quoted 6 lines · On Tue, 2 Jun 2015 13:40:26 +0100 Mel Gorman <[email protected]> wrote:
Quoted 5 lines · Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

Quoted 8 lines
Quoted 5 lines

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

Thanks Mel,

I should also mention that I see this warning pop when working with swapfiles on NFS. This trace is with this patchset, but I see a similar one without it:

[   74.232485] ------------[ cut here ]------------
[   74.233354] WARNING: CPU: 2 PID: 754 at net/core/sock.c:364 sk_clear_memalloc+0x51/0x80()
[   74.234790] Modules linked in: cts rpcsec_gss_krb5 nfsv4 dns_resolver nfs fscache xfs libcrc32c snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hda_core snd_hwdep snd_seq snd_seq_device nfsd snd_pcm snd_timer snd e1000 ppdev parport_pc joydev parport pvpanic soundcore floppy serio_raw i2c_piix4 pcspkr nfs_acl lockd virtio_balloon acpi_cpufreq auth_rpcgss grace sunrpc qxl drm_kms_helper ttm drm virtio_console virtio_blk virtio_pci ata_generic virtio_ring pata_acpi virtio
[   74.243599] CPU: 2 PID: 754 Comm: swapoff Not tainted 4.1.0-rc6+ #5
[   74.244635] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[   74.245546]  0000000000000000 0000000079e69e31 ffff8800d066bde8 ffffffff8179263d
[   74.246786]  0000000000000000 0000000000000000 ffff8800d066be28 ffffffff8109e6fa
[   74.248175]  0000000000000000 ffff880118d48000 ffff8800d58f5c08 ffff880036e380a8
[   74.249483] Call Trace:
[   74.249872]  [<ffffffff8179263d>] dump_stack+0x45/0x57
[   74.250703]  [<ffffffff8109e6fa>] warn_slowpath_common+0x8a/0xc0
[   74.251655]  [<ffffffff8109e82a>] warn_slowpath_null+0x1a/0x20
[   74.252585]  [<ffffffff81661241>] sk_clear_memalloc+0x51/0x80
[   74.253519]  [<ffffffffa0116c72>] xs_disable_swap+0x42/0x80 [sunrpc]
[   74.254537]  [<ffffffffa01109de>] rpc_clnt_swap_deactivate+0x7e/0xc0 [sunrpc]
[   74.255610]  [<ffffffffa03e4fd7>] nfs_swap_deactivate+0x27/0x30 [nfs]
[   74.256582]  [<ffffffff811e99d4>] destroy_swap_extents+0x74/0x80
[   74.257496]  [<ffffffff811ecb52>] SyS_swapoff+0x222/0x5c0
[   74.258318]  [<ffffffff81023f27>] ? syscall_trace_leave+0xc7/0x140
[   74.259253]  [<ffffffff81798dae>] system_call_fastpath+0x12/0x71
[   74.260158] ---[ end trace 2530722966429f10 ]---

...that comes from this in sk_clear_memalloc:

/*
* SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
* progress of swapping. However, if SOCK_MEMALLOC is cleared while
* it has rmem allocations there is a risk that the user of the
* socket cannot make forward progress due to exceeding the rmem
* limits. By rights, sk_clear_memalloc() should only be called
* on sockets being torn down but warn and reset the accounting if
* that assumption breaks.
*/
if (WARN_ON(sk->sk_forward_alloc))
sk_mem_reclaim(sk);

Is it wrong to call sk_clear_memalloc on swapoff? Should we try to keep it set up as a memalloc socket on the last swapoff and just wait until the socket is being freed to clear it? If so, then maybe the right thing to do is to call sk_clear_memalloc in __sk_free or somewhere similar if it's set up for memalloc?

Signature · 1 line
Jeff Layton <[email protected]>
#10 lore raw

Re: [PATCH 3/4] sunrpc: if we're closing down a socket, clear memalloc on it first

Quoted 60 lines · Jeff Layton
Quoted 6 lines · On Tue, 2 Jun 2015 13:40:26 +0100 Mel Gorman <[email protected]> wrote:
Quoted 5 lines · Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

Quoted 8 lines
Quoted 5 lines

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

Thanks Mel,

I should also mention that I see this warning pop when working with swapfiles on NFS. This trace is with this patchset, but I see a similar one without it:

[   74.232485] ------------[ cut here ]------------
[   74.233354] WARNING: CPU: 2 PID: 754 at net/core/sock.c:364 sk_clear_memalloc+0x51/0x80()
[   74.234790] Modules linked in: cts rpcsec_gss_krb5 nfsv4 dns_resolver nfs fscache xfs libcrc32c snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hda_core snd_hwdep snd_seq snd_seq_device nfsd snd_pcm snd_timer snd e1000 ppdev parport_pc joydev parport pvpanic soundcore floppy serio_raw i2c_piix4 pcspkr nfs_acl lockd virtio_balloon acpi_cpufreq auth_rpcgss grace sunrpc qxl drm_kms_helper ttm drm virtio_console virtio_blk virtio_pci ata_generic virtio_ring pata_acpi virtio
[   74.243599] CPU: 2 PID: 754 Comm: swapoff Not tainted 4.1.0-rc6+ #5
[   74.244635] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[   74.245546]  0000000000000000 0000000079e69e31 ffff8800d066bde8 ffffffff8179263d
[   74.246786]  0000000000000000 0000000000000000 ffff8800d066be28 ffffffff8109e6fa
[   74.248175]  0000000000000000 ffff880118d48000 ffff8800d58f5c08 ffff880036e380a8
[   74.249483] Call Trace:
[   74.249872]  [<ffffffff8179263d>] dump_stack+0x45/0x57
[   74.250703]  [<ffffffff8109e6fa>] warn_slowpath_common+0x8a/0xc0
[   74.251655]  [<ffffffff8109e82a>] warn_slowpath_null+0x1a/0x20
[   74.252585]  [<ffffffff81661241>] sk_clear_memalloc+0x51/0x80
[   74.253519]  [<ffffffffa0116c72>] xs_disable_swap+0x42/0x80 [sunrpc]
[   74.254537]  [<ffffffffa01109de>] rpc_clnt_swap_deactivate+0x7e/0xc0 [sunrpc]
[   74.255610]  [<ffffffffa03e4fd7>] nfs_swap_deactivate+0x27/0x30 [nfs]
[   74.256582]  [<ffffffff811e99d4>] destroy_swap_extents+0x74/0x80
[   74.257496]  [<ffffffff811ecb52>] SyS_swapoff+0x222/0x5c0
[   74.258318]  [<ffffffff81023f27>] ? syscall_trace_leave+0xc7/0x140
[   74.259253]  [<ffffffff81798dae>] system_call_fastpath+0x12/0x71
[   74.260158] ---[ end trace 2530722966429f10 ]---

...that comes from this in sk_clear_memalloc:

/*
* SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
* progress of swapping. However, if SOCK_MEMALLOC is cleared while
* it has rmem allocations there is a risk that the user of the
* socket cannot make forward progress due to exceeding the rmem
* limits. By rights, sk_clear_memalloc() should only be called
* on sockets being torn down but warn and reset the accounting if
* that assumption breaks.
*/
if (WARN_ON(sk->sk_forward_alloc))
sk_mem_reclaim(sk);

Quoted 6 lines

Is it wrong to call sk_clear_memalloc on swapoff? Should we try to keep it set up as a memalloc socket on the last swapoff and just wait until the socket is being freed to clear it? If so, then maybe the right thing to do is to call sk_clear_memalloc in __sk_free or somewhere similar if it's set up for memalloc?

I think it is perfectly reasonable to remove the warning after your series. When I had it in mind, I was primarily thinking of the shutdown case and a single swap file. With your series applied, the disabling of swap is called at the correct time. So, something like this to tack on to the end of your series?

---8<---
net, swap: Remove a warning and clarify why sk_mem_reclaim is required when deactivating swap

Jeff Layton reported the following;

 [   74.232485] ------------[ cut here ]------------
 [   74.233354] WARNING: CPU: 2 PID: 754 at net/core/sock.c:364 sk_clear_memalloc+0x51/0x80()
 [   74.234790] Modules linked in: cts rpcsec_gss_krb5 nfsv4 dns_resolver nfs fscache xfs libcrc32c snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hda_core snd_hwdep snd_seq snd_seq_device nfsd snd_pcm snd_timer snd e1000 ppdev parport_pc joydev parport pvpanic soundcore floppy serio_raw i2c_piix4 pcspkr nfs_acl lockd virtio_balloon acpi_cpufreq auth_rpcgss grace sunrpc qxl drm_kms_helper ttm drm virtio_console virtio_blk virtio_pci ata_generic virtio_ring pata_acpi virtio
 [   74.243599] CPU: 2 PID: 754 Comm: swapoff Not tainted 4.1.0-rc6+ #5
 [   74.244635] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
 [   74.245546]  0000000000000000 0000000079e69e31 ffff8800d066bde8 ffffffff8179263d
 [   74.246786]  0000000000000000 0000000000000000 ffff8800d066be28 ffffffff8109e6fa
 [   74.248175]  0000000000000000 ffff880118d48000 ffff8800d58f5c08 ffff880036e380a8
 [   74.249483] Call Trace:
 [   74.249872]  [<ffffffff8179263d>] dump_stack+0x45/0x57
 [   74.250703]  [<ffffffff8109e6fa>] warn_slowpath_common+0x8a/0xc0
 [   74.251655]  [<ffffffff8109e82a>] warn_slowpath_null+0x1a/0x20
 [   74.252585]  [<ffffffff81661241>] sk_clear_memalloc+0x51/0x80
 [   74.253519]  [<ffffffffa0116c72>] xs_disable_swap+0x42/0x80 [sunrpc]
 [   74.254537]  [<ffffffffa01109de>] rpc_clnt_swap_deactivate+0x7e/0xc0 [sunrpc]
 [   74.255610]  [<ffffffffa03e4fd7>] nfs_swap_deactivate+0x27/0x30 [nfs]
 [   74.256582]  [<ffffffff811e99d4>] destroy_swap_extents+0x74/0x80
 [   74.257496]  [<ffffffff811ecb52>] SyS_swapoff+0x222/0x5c0
 [   74.258318]  [<ffffffff81023f27>] ? syscall_trace_leave+0xc7/0x140
 [   74.259253]  [<ffffffff81798dae>] system_call_fastpath+0x12/0x71
 [   74.260158] ---[ end trace 2530722966429f10 ]---

The warning in question was unnecessary but with Jeff's series the rules are also clearer. This patch removes the warning and updates the comment to explain why sk_mem_reclaim() may still be called.

---
 net/core/sock.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/core/sock.c +5 -7
index 71e3e5f1eaa0..1ebf706b5847 100644
@@ -354,14 +354,12 @@ void sk_clear_memalloc(struct sock *sk)
 
 	/*
 	 * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
-	 * progress of swapping. However, if SOCK_MEMALLOC is cleared while
-	 * it has rmem allocations there is a risk that the user of the
-	 * socket cannot make forward progress due to exceeding the rmem
-	 * limits. By rights, sk_clear_memalloc() should only be called
-	 * on sockets being torn down but warn and reset the accounting if
-	 * that assumption breaks.
+	 * progress of swapping. SOCK_MEMALLOC may be cleared while
+	 * it has rmem allocations due to the last swapfile being deactivated
+	 * but there is a risk that the socket is unusable due to exceeding
+	 * the rmem limits. Reclaim the reserves and obey rmem limits again.
 	 */
-	if (WARN_ON(sk->sk_forward_alloc))
+	if (sk->sk_forward_alloc)
 		sk_mem_reclaim(sk);
 }
 EXPORT_SYMBOL_GPL(sk_clear_memalloc);
#11 lore raw

Re: [PATCH 3/4] sunrpc: if we're closing down a socket, clear memalloc on it first

Quoted 124 lines · On Thu, 4 Jun 2015 14:08:30 +0100 Mel Gorman <[email protected]> wrote:
Quoted 60 lines · Jeff Layton
Quoted 6 lines · On Tue, 2 Jun 2015 13:40:26 +0100 Mel Gorman <[email protected]> wrote:
Quoted 5 lines · Jeff Layton

We currently increment the memalloc_socks counter if we have a xprt that is associated with a swapfile. That socket can be replaced however during a reconnect event, and the memalloc_socks counter is never decremented if that occurs.

Quoted 8 lines
Quoted 5 lines

When tearing down a xprt socket, check to see if the xprt is set up for swapping and sk_clear_memalloc before releasing the socket if so.

Thanks Mel,

I should also mention that I see this warning pop when working with swapfiles on NFS. This trace is with this patchset, but I see a similar one without it:

[   74.232485] ------------[ cut here ]------------
[   74.233354] WARNING: CPU: 2 PID: 754 at net/core/sock.c:364 sk_clear_memalloc+0x51/0x80()
[   74.234790] Modules linked in: cts rpcsec_gss_krb5 nfsv4 dns_resolver nfs fscache xfs libcrc32c snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hda_core snd_hwdep snd_seq snd_seq_device nfsd snd_pcm snd_timer snd e1000 ppdev parport_pc joydev parport pvpanic soundcore floppy serio_raw i2c_piix4 pcspkr nfs_acl lockd virtio_balloon acpi_cpufreq auth_rpcgss grace sunrpc qxl drm_kms_helper ttm drm virtio_console virtio_blk virtio_pci ata_generic virtio_ring pata_acpi virtio
[   74.243599] CPU: 2 PID: 754 Comm: swapoff Not tainted 4.1.0-rc6+ #5
[   74.244635] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[   74.245546]  0000000000000000 0000000079e69e31 ffff8800d066bde8 ffffffff8179263d
[   74.246786]  0000000000000000 0000000000000000 ffff8800d066be28 ffffffff8109e6fa
[   74.248175]  0000000000000000 ffff880118d48000 ffff8800d58f5c08 ffff880036e380a8
[   74.249483] Call Trace:
[   74.249872]  [<ffffffff8179263d>] dump_stack+0x45/0x57
[   74.250703]  [<ffffffff8109e6fa>] warn_slowpath_common+0x8a/0xc0
[   74.251655]  [<ffffffff8109e82a>] warn_slowpath_null+0x1a/0x20
[   74.252585]  [<ffffffff81661241>] sk_clear_memalloc+0x51/0x80
[   74.253519]  [<ffffffffa0116c72>] xs_disable_swap+0x42/0x80 [sunrpc]
[   74.254537]  [<ffffffffa01109de>] rpc_clnt_swap_deactivate+0x7e/0xc0 [sunrpc]
[   74.255610]  [<ffffffffa03e4fd7>] nfs_swap_deactivate+0x27/0x30 [nfs]
[   74.256582]  [<ffffffff811e99d4>] destroy_swap_extents+0x74/0x80
[   74.257496]  [<ffffffff811ecb52>] SyS_swapoff+0x222/0x5c0
[   74.258318]  [<ffffffff81023f27>] ? syscall_trace_leave+0xc7/0x140
[   74.259253]  [<ffffffff81798dae>] system_call_fastpath+0x12/0x71
[   74.260158] ---[ end trace 2530722966429f10 ]---

...that comes from this in sk_clear_memalloc:

/*
* SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
* progress of swapping. However, if SOCK_MEMALLOC is cleared while
* it has rmem allocations there is a risk that the user of the
* socket cannot make forward progress due to exceeding the rmem
* limits. By rights, sk_clear_memalloc() should only be called
* on sockets being torn down but warn and reset the accounting if
* that assumption breaks.
*/
if (WARN_ON(sk->sk_forward_alloc))
sk_mem_reclaim(sk);

Quoted 6 lines

Is it wrong to call sk_clear_memalloc on swapoff? Should we try to keep it set up as a memalloc socket on the last swapoff and just wait until the socket is being freed to clear it? If so, then maybe the right thing to do is to call sk_clear_memalloc in __sk_free or somewhere similar if it's set up for memalloc?

I think it is perfectly reasonable to remove the warning after your series. When I had it in mind, I was primarily thinking of the shutdown case and a single swap file. With your series applied, the disabling of swap is called at the correct time. So, something like this to tack on to the end of your series?

---8<---
net, swap: Remove a warning and clarify why sk_mem_reclaim is required when deactivating swap

Jeff Layton reported the following;

 [   74.232485] ------------[ cut here ]------------
 [   74.233354] WARNING: CPU: 2 PID: 754 at net/core/sock.c:364 sk_clear_memalloc+0x51/0x80()
 [   74.234790] Modules linked in: cts rpcsec_gss_krb5 nfsv4 dns_resolver nfs fscache xfs libcrc32c snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hda_core snd_hwdep snd_seq snd_seq_device nfsd snd_pcm snd_timer snd e1000 ppdev parport_pc joydev parport pvpanic soundcore floppy serio_raw i2c_piix4 pcspkr nfs_acl lockd virtio_balloon acpi_cpufreq auth_rpcgss grace sunrpc qxl drm_kms_helper ttm drm virtio_console virtio_blk virtio_pci ata_generic virtio_ring pata_acpi virtio
 [   74.243599] CPU: 2 PID: 754 Comm: swapoff Not tainted 4.1.0-rc6+ #5
 [   74.244635] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
 [   74.245546]  0000000000000000 0000000079e69e31 ffff8800d066bde8 ffffffff8179263d
 [   74.246786]  0000000000000000 0000000000000000 ffff8800d066be28 ffffffff8109e6fa
 [   74.248175]  0000000000000000 ffff880118d48000 ffff8800d58f5c08 ffff880036e380a8
 [   74.249483] Call Trace:
 [   74.249872]  [<ffffffff8179263d>] dump_stack+0x45/0x57
 [   74.250703]  [<ffffffff8109e6fa>] warn_slowpath_common+0x8a/0xc0
 [   74.251655]  [<ffffffff8109e82a>] warn_slowpath_null+0x1a/0x20
 [   74.252585]  [<ffffffff81661241>] sk_clear_memalloc+0x51/0x80
 [   74.253519]  [<ffffffffa0116c72>] xs_disable_swap+0x42/0x80 [sunrpc]
 [   74.254537]  [<ffffffffa01109de>] rpc_clnt_swap_deactivate+0x7e/0xc0 [sunrpc]
 [   74.255610]  [<ffffffffa03e4fd7>] nfs_swap_deactivate+0x27/0x30 [nfs]
 [   74.256582]  [<ffffffff811e99d4>] destroy_swap_extents+0x74/0x80
 [   74.257496]  [<ffffffff811ecb52>] SyS_swapoff+0x222/0x5c0
 [   74.258318]  [<ffffffff81023f27>] ? syscall_trace_leave+0xc7/0x140
 [   74.259253]  [<ffffffff81798dae>] system_call_fastpath+0x12/0x71
 [   74.260158] ---[ end trace 2530722966429f10 ]---

The warning in question was unnecessary but with Jeff's series the rules are also clearer. This patch removes the warning and updates the comment to explain why sk_mem_reclaim() may still be called.

---
 net/core/sock.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
net/core/sock.c +0 -6
index 71e3e5f1eaa0..1ebf706b5847 100644
@@ -354,14 +354,12 @@ void sk_clear_memalloc(struct sock *sk)
 
 	/*
 	 * SOCK_MEMALLOC is allowed to ignore rmem limits to ensure forward
-	 * progress of swapping. However, if SOCK_MEMALLOC is cleared while
-	 * it has rmem allocations there is a risk that the user of the
-	 * socket cannot make forward progress due to exceeding the rmem
-	 * limits. By rights, sk_clear_memalloc() should only be called
-	 * on sockets being torn down but warn and reset the accounting if
-	 * that assumption breaks.

net/core/sock.c void sk_clear_memalloc(struct sock *sk) · line 365

Quoted 10 lines
net/core/sock.c +5 -1
+	 * progress of swapping. SOCK_MEMALLOC may be cleared while
+	 * it has rmem allocations due to the last swapfile being deactivated
+	 * but there is a risk that the socket is unusable due to exceeding
+	 * the rmem limits. Reclaim the reserves and obey rmem limits again.
 	 */
-	if (WARN_ON(sk->sk_forward_alloc))
+	if (sk->sk_forward_alloc)
 		sk_mem_reclaim(sk);
 }
 EXPORT_SYMBOL_GPL(sk_clear_memalloc);

Sure, sounds reasonable. If I need to do a respin of the series, I'll roll this into it. Otherwise you or I can just send it as a separate patch afterward.

Thanks,

Signature · 7 lines
Jeff Layton <[email protected]>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#12 lore raw

PATCH 4/4 sunrpc: lock xprt before trying to set memalloc on the sockets

In reply to Jeff Layton

It's possible that we could race with a call to xs_reset_transport, in which case the xprt->inet pointer could be zeroed out while we're accessing it. Lock the xprt before we try to set memalloc on it.

---
 net/sunrpc/xprtsock.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
net/sunrpc/xprtsock.c +17 -0
index 359446442112..91770ccab608 100644
@@ -1964,11 +1964,22 @@ static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
 }
 
 #ifdef CONFIG_SUNRPC_SWAP
+/*
+ * Note that this should be called with XPRT_LOCKED held (or when we otherwise
+ * know that we have exclusive access to the socket), to guard against
+ * races with xs_reset_transport.
+ */
 static void xs_set_memalloc(struct rpc_xprt *xprt)
 {
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	/*
+	 * If there's no sock, then we have nothing to set. The
+	 * reconnecting process will get it for us.
+	 */
+	if (!transport->inet)
+		return;
 	if (atomic_read(&xprt->swapper))
 		sk_set_memalloc(transport->inet);
 }
@@ -1986,8 +1997,11 @@ xs_swapper_enable(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
+		return -ERESTARTSYS;
 	if (atomic_inc_return(&xprt->swapper) == 1)
 		sk_set_memalloc(transport->inet);
+	xprt_release_xprt(xprt, NULL);
 	return 0;
 }
 
@@ -2004,8 +2018,11 @@ xs_swapper_disable(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE))
+		return;
 	if (atomic_dec_and_test(&xprt->swapper))
 		sk_clear_memalloc(transport->inet);
+	xprt_release_xprt(xprt, NULL);
 }
 #else
 static void xs_set_memalloc(struct rpc_xprt *xprt)
Signature · 1 line
2.4.1
#13 lore raw

Re: [PATCH 4/4] sunrpc: lock xprt before trying to set memalloc on the sockets

Quoted 38 lines · On Sat, 30 May 2015 08:03:13 -0400 Jeff Layton <[email protected]> wrote:

It's possible that we could race with a call to xs_reset_transport, in which case the xprt->inet pointer could be zeroed out while we're accessing it. Lock the xprt before we try to set memalloc on it.

---
 net/sunrpc/xprtsock.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
net/sunrpc/xprtsock.c +11 -0
index 359446442112..91770ccab608 100644
@@ -1964,11 +1964,22 @@ static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
 }
 
 #ifdef CONFIG_SUNRPC_SWAP
+/*
+ * Note that this should be called with XPRT_LOCKED held (or when we otherwise
+ * know that we have exclusive access to the socket), to guard against
+ * races with xs_reset_transport.
+ */
 static void xs_set_memalloc(struct rpc_xprt *xprt)
 {
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	/*
+	 * If there's no sock, then we have nothing to set. The
+	 * reconnecting process will get it for us.
+	 */
+	if (!transport->inet)
+		return;
 	if (atomic_read(&xprt->swapper))
 		sk_set_memalloc(transport->inet);
 }

net/sunrpc/xprtsock.c xs_swapper_enable(struct rpc_xprt *xprt) · line 2003

Quoted 7 lines
net/sunrpc/xprtsock.c +2 -0
@@ -1986,8 +1997,11 @@ xs_swapper_enable(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
+		return -ERESTARTSYS;
 	if (atomic_inc_return(&xprt->swapper) == 1)
 		sk_set_memalloc(transport->inet);

Oh, we need to check that transport->inet is not NULL before we call sk_set/clear_memalloc on it. I'll respin with that fix once I give everyone a chance to comment on the rest...

Quoted 16 lines
net/sunrpc/xprtsock.c +4 -0
+	xprt_release_xprt(xprt, NULL);
 	return 0;
 }
 
@@ -2004,8 +2018,11 @@ xs_swapper_disable(struct rpc_xprt *xprt)
 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
 			xprt);
 
+	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE))
+		return;
 	if (atomic_dec_and_test(&xprt->swapper))
 		sk_clear_memalloc(transport->inet);
+	xprt_release_xprt(xprt, NULL);
 }
 #else
 static void xs_set_memalloc(struct rpc_xprt *xprt)
Signature · 1 line
Jeff Layton <[email protected]>