SUNRPC/auth: async tasks mustn't block waiting for memory
authorNeilBrown <neilb@suse.de>
Sun, 6 Mar 2022 23:41:44 +0000 (10:41 +1100)
committerTrond Myklebust <trond.myklebust@hammerspace.com>
Sun, 13 Mar 2022 16:59:35 +0000 (12:59 -0400)
When memory is short, new worker threads cannot be created and we depend
on the minimum one rpciod thread to be able to handle everything.  So it
must not block waiting for memory.

mempools are particularly a problem as memory can only be released back
to the mempool by an async rpc task running.  If all available workqueue
threads are waiting on the mempool, no thread is available to return
anything.

lookup_cred() can block on a mempool or kmalloc - and this can cause
deadlocks.  So add a new RPCAUTH_LOOKUP flag for async lookups and don't
block on memory.  If the -ENOMEM gets back to call_refreshresult(), wait
a short while and try again.  HZ>>4 is chosen as it is used elsewhere
for -ENOMEM retries.

Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
include/linux/sunrpc/auth.h
net/sunrpc/auth.c
net/sunrpc/auth_gss/auth_gss.c
net/sunrpc/auth_unix.c
net/sunrpc/clnt.c

index 98da816b5fc2ad064355f0baba1f13d43afe799a..3e6ce288a7fc0159bddf1ee69131e8392c631f8b 100644 (file)
@@ -99,6 +99,7 @@ struct rpc_auth_create_args {
 
 /* Flags for rpcauth_lookupcred() */
 #define RPCAUTH_LOOKUP_NEW             0x01    /* Accept an uninitialised cred */
+#define RPCAUTH_LOOKUP_ASYNC           0x02    /* Don't block waiting for memory */
 
 /*
  * Client authentication ops
index a9f0d17fdb0d64dce0bd0829ee58b31f024d1a0c..6bfa19f9fa6a99b6384b04a20c492410f72f7df2 100644 (file)
@@ -615,6 +615,8 @@ rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
        };
        struct rpc_cred *ret;
 
+       if (RPC_IS_ASYNC(task))
+               lookupflags |= RPCAUTH_LOOKUP_ASYNC;
        ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
        put_cred(acred.cred);
        return ret;
@@ -631,6 +633,8 @@ rpcauth_bind_machine_cred(struct rpc_task *task, int lookupflags)
 
        if (!acred.principal)
                return NULL;
+       if (RPC_IS_ASYNC(task))
+               lookupflags |= RPCAUTH_LOOKUP_ASYNC;
        return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
 }
 
@@ -654,7 +658,7 @@ rpcauth_bindcred(struct rpc_task *task, const struct cred *cred, int flags)
        };
 
        if (flags & RPC_TASK_ASYNC)
-               lookupflags |= RPCAUTH_LOOKUP_NEW;
+               lookupflags |= RPCAUTH_LOOKUP_NEW | RPCAUTH_LOOKUP_ASYNC;
        if (task->tk_op_cred)
                /* Task must use exactly this rpc_cred */
                new = get_rpccred(task->tk_op_cred);
index affd64a54f02d3195fbcaaced5668ad658aba770..ac082810820482e948053ac4876c5542d60522de 100644 (file)
@@ -1341,7 +1341,11 @@ gss_hash_cred(struct auth_cred *acred, unsigned int hashbits)
 static struct rpc_cred *
 gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
 {
-       return rpcauth_lookup_credcache(auth, acred, flags, GFP_KERNEL);
+       gfp_t gfp = GFP_KERNEL;
+
+       if (flags & RPCAUTH_LOOKUP_ASYNC)
+               gfp = GFP_NOWAIT | __GFP_NOWARN;
+       return rpcauth_lookup_credcache(auth, acred, flags, gfp);
 }
 
 static struct rpc_cred *
index 3600d864164482ca8e60d195c9b90ab94490d171..c629d366030ee2ffc26f7db37920197f3d990c2b 100644 (file)
@@ -43,8 +43,14 @@ unx_destroy(struct rpc_auth *auth)
 static struct rpc_cred *
 unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
 {
-       struct rpc_cred *ret = mempool_alloc(unix_pool, GFP_KERNEL);
-
+       gfp_t gfp = GFP_KERNEL;
+       struct rpc_cred *ret;
+
+       if (flags & RPCAUTH_LOOKUP_ASYNC)
+               gfp = GFP_NOWAIT | __GFP_NOWARN;
+       ret = mempool_alloc(unix_pool, gfp);
+       if (!ret)
+               return ERR_PTR(-ENOMEM);
        rpcauth_init_cred(ret, acred, auth, &unix_credops);
        ret->cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
        return ret;
index 97165a545cb3c900ba8fd00e533f9df44c9d0114..9556eb7b065b611f14c4eca4c6d59a024d077649 100644 (file)
@@ -1745,6 +1745,9 @@ call_refreshresult(struct rpc_task *task)
                task->tk_cred_retry--;
                trace_rpc_retry_refresh_status(task);
                return;
+       case -ENOMEM:
+               rpc_delay(task, HZ >> 4);
+               return;
        }
        trace_rpc_refresh_status(task);
        rpc_call_rpcerror(task, status);