sunrpc: make xprt->swapper an atomic_t
authorJeff Layton <jlayton@poochiereds.net>
Wed, 3 Jun 2015 20:14:26 +0000 (16:14 -0400)
committerTrond Myklebust <trond.myklebust@primarydata.com>
Wed, 10 Jun 2015 22:26:18 +0000 (18:26 -0400)
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.

Cc: Mel Gorman <mgorman@suse.de>
Signed-off-by: Jeff Layton <jeff.layton@primarydata.com>
Reviewed-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Trond Myklebust <trond.myklebust@primarydata.com>
include/linux/sunrpc/xprt.h
net/sunrpc/clnt.c
net/sunrpc/xprtsock.c

index bc9c39d6d30dc0985e083bb33b3ce95776868b85..9a3308703c159a2c40818961d34038ed323bce8c 100644 (file)
@@ -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 */
 
@@ -346,7 +346,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 *);
index 0ba65156a62b93fe6c306c74a1dff439bf266723..801044aeeeddbed05f7e160b190963890dc8a518 100644 (file)
@@ -2495,7 +2495,7 @@ retry:
                        goto retry;
                }
 
-               ret = xs_swapper(xprt, 1);
+               ret = xs_swapper_enable(xprt);
                xprt_put(xprt);
        }
        return ret;
@@ -2522,7 +2522,7 @@ retry:
                        goto retry;
                }
 
-               xs_swapper(xprt, 0);
+               xs_swapper_disable(xprt);
                xprt_put(xprt);
        }
 }
index f344c0f8974c1f3740cba39f7c3c80bd07628d71..6538e6fbb7bab7bcf7a45c3d83636676bf9ff8c9 100644 (file)
@@ -1961,31 +1961,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)