Add a shrinker which frees unused slots and may ask the clients to use fewer slots on each session. Each session now tracks se_client_maxreqs which is the most recent max-requests-in-use reported by the client, and se_target_maxreqs which is a target number of requests which is reduced by the shrinker. The shrinker iterates over all sessions on all client in all net-namespaces and reduces the target by 1 for each. The shrinker may get called multiple times to reduce by more than 1 each. If se_target_maxreqs is above se_client_maxreqs, those slots can be freed immediately. If not the client will be ask to reduce its usage and as the usage goes down slots will be freed. Once the usage has dropped to match the target, the target can be increased if the client uses all available slots and if a GFP_NOWAIT allocation succeeds. Signed-off-by: NeilBrown <neilb@xxxxxxx> --- fs/nfsd/nfs4state.c | 82 ++++++++++++++++++++++++++++++++++++++++++++- fs/nfsd/nfs4xdr.c | 2 +- fs/nfsd/state.h | 3 ++ fs/nfsd/xdr4.h | 2 -- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 15de62416243..bbc365002885 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1925,6 +1925,8 @@ free_session_slots(struct nfsd4_session *ses, int from) ses->se_slots[i] = (void *)seqid; } ses->se_fchannel.maxreqs = from; + if (ses->se_target_maxreqs > from) + ses->se_target_maxreqs = from; } /* @@ -1968,6 +1970,7 @@ static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, fattrs->maxreqs = i; memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); + new->se_target_maxreqs = i; return new; out_free: @@ -2086,6 +2089,57 @@ static void free_session(struct nfsd4_session *ses) __free_session(ses); } + +static DEFINE_SPINLOCK(nfsd_session_list_lock); +static LIST_HEAD(nfsd_session_list); + +static unsigned long +nfsd_slot_count(struct shrinker *s, struct shrink_control *sc) +{ + struct nfsd4_session *ses; + unsigned long cnt = 0; + + spin_lock(&nfsd_session_list_lock); + list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) + if (ses->se_target_maxreqs > 1) + cnt += ses->se_target_maxreqs - 1; + spin_unlock(&nfsd_session_list_lock); + return cnt ? cnt : SHRINK_EMPTY; +} + +static unsigned long +nfsd_slot_scan(struct shrinker *s, struct shrink_control *sc) +{ + struct nfsd4_session *ses; + unsigned long scanned = 0; + unsigned long freed = 0; + + spin_lock(&nfsd_session_list_lock); + list_for_each_entry(ses, &nfsd_session_list, se_all_sessions) { + struct nfsd_net *nn = net_generic(ses->se_client->net, + nfsd_net_id); + + spin_lock(&nn->client_lock); + if (ses->se_fchannel.maxreqs > 1 && + ses->se_target_maxreqs > 1) { + freed += 1; + ses->se_target_maxreqs -= 1; + free_session_slots(ses, max(ses->se_target_maxreqs, + ses->se_client_maxreqs)); + } + spin_unlock(&nn->client_lock); + scanned += 1; + if (scanned >= sc->nr_to_scan) { + /* Move starting point for next scan */ + list_move(&nfsd_session_list, &ses->se_all_sessions); + break; + } + } + spin_unlock(&nfsd_session_list_lock); + sc->nr_scanned = scanned; + return freed; +} + static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) { int idx; @@ -2107,6 +2161,10 @@ static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, stru list_add(&new->se_perclnt, &clp->cl_sessions); spin_unlock(&clp->cl_lock); + spin_lock(&nfsd_session_list_lock); + list_add_tail(&new->se_all_sessions, &nfsd_session_list); + spin_unlock(&nfsd_session_list_lock); + { struct sockaddr *sa = svc_addr(rqstp); /* @@ -2176,6 +2234,9 @@ unhash_session(struct nfsd4_session *ses) spin_lock(&ses->se_client->cl_lock); list_del(&ses->se_perclnt); spin_unlock(&ses->se_client->cl_lock); + spin_lock(&nfsd_session_list_lock); + list_del(&ses->se_all_sessions); + spin_unlock(&nfsd_session_list_lock); } /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ @@ -4221,8 +4282,11 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, if (status) goto out_put_session; - /* If there are lots of unused slots, free some */ + /* we can safely free some slots */ free_session_slots(session, seq->maxslots + NFSD_MAX_UNUSED_SLOTS); + free_session_slots(session, max(seq->maxslots, + session->se_target_maxreqs)); + session->se_client_maxreqs = seq->maxslots; buflen = (seq->cachethis) ? session->se_fchannel.maxresp_cached : @@ -4251,6 +4315,7 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, * gently try to allocate another one. */ if (seq->slotid == session->se_fchannel.maxreqs - 1 && + session->se_target_maxreqs >= session->se_fchannel.maxreqs && session->se_fchannel.maxreqs < NFSD_MAX_SLOTS_PER_SESSION) { int s = session->se_fchannel.maxreqs; @@ -4260,9 +4325,11 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, slot->sl_seqid = (uintptr_t)session->se_slots[s]; session->se_slots[s] = slot; session->se_fchannel.maxreqs += 1; + session->se_target_maxreqs = session->se_fchannel.maxreqs; } } seq->maxslots = session->se_fchannel.maxreqs; + seq->target_maxslots = session->se_target_maxreqs; out: switch (clp->cl_cb_state) { @@ -8653,6 +8720,8 @@ nfs4_state_start_net(struct net *net) /* initialization to perform when the nfsd service is started: */ +static struct shrinker *nfsd_slot_shrinker; + int nfs4_state_start(void) { @@ -8662,6 +8731,16 @@ nfs4_state_start(void) if (ret) return ret; + nfsd_slot_shrinker = shrinker_alloc(0, "nfsd-DRC-slot"); + if (!nfsd_slot_shrinker) { + rhltable_destroy(&nfs4_file_rhltable); + return -ENOMEM; + } + nfsd_slot_shrinker->count_objects = nfsd_slot_count; + nfsd_slot_shrinker->scan_objects = nfsd_slot_scan; + nfsd_slot_shrinker->seeks = 1; + shrinker_register(nfsd_slot_shrinker); + set_max_delegations(); return 0; } @@ -8703,6 +8782,7 @@ void nfs4_state_shutdown(void) { rhltable_destroy(&nfs4_file_rhltable); + shrinker_free(nfsd_slot_shrinker); } static void diff --git a/fs/nfsd/nfs4xdr.c b/fs/nfsd/nfs4xdr.c index 846ed52fdaf5..ac3376c2e5cc 100644 --- a/fs/nfsd/nfs4xdr.c +++ b/fs/nfsd/nfs4xdr.c @@ -4956,7 +4956,7 @@ nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr, if (nfserr != nfs_ok) return nfserr; /* sr_target_highest_slotid */ - nfserr = nfsd4_encode_slotid4(xdr, seq->maxslots - 1); + nfserr = nfsd4_encode_slotid4(xdr, seq->target_maxslots - 1); if (nfserr != nfs_ok) return nfserr; /* sr_status_flags */ diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h index 012b68a0bafa..a25f3cfaab09 100644 --- a/fs/nfsd/state.h +++ b/fs/nfsd/state.h @@ -304,12 +304,15 @@ struct nfsd4_session { /* See SESSION4_PERSIST, etc. for standard flags; this is internal-only: */ #define NFS4_SESSION_DEAD 0x010 u32 se_flags; + struct list_head se_all_sessions;/* global list of sessions */ struct nfs4_client *se_client; struct nfs4_sessionid se_sessionid; struct nfsd4_channel_attrs se_fchannel; struct nfsd4_channel_attrs se_bchannel; struct nfsd4_cb_sec se_cb_sec; struct list_head se_conns; + u8 se_target_maxreqs; + u8 se_client_maxreqs; u32 se_cb_prog; u32 se_cb_seq_nr; struct nfsd4_slot *se_slots[NFSD_MAX_SLOTS_PER_SESSION]; /* forward channel slots */ diff --git a/fs/nfsd/xdr4.h b/fs/nfsd/xdr4.h index 2a21a7662e03..71b87190a4a6 100644 --- a/fs/nfsd/xdr4.h +++ b/fs/nfsd/xdr4.h @@ -575,9 +575,7 @@ struct nfsd4_sequence { u32 slotid; /* request/response */ u32 maxslots; /* request/response */ u32 cachethis; /* request */ -#if 0 u32 target_maxslots; /* response */ -#endif /* not yet */ u32 status_flags; /* response */ }; -- 2.47.0