NFSD: memory shrinker for NFSv4 clients
Subsystems (2)
Dai Ngo
reviewerv3 cover NFSD: memory shrinker for NFSv4 clients
This patch series implements the memory shrinker for NFSv4 clients to react to system low memory condition.
The memory shrinker's count callback is used to trigger the laundromat. The actual work of destroying the expired clients is done by the laundromat itself. We can not destroying the expired clients on the memory shrinler's scan callback context to avoid possible deadlock.
By destroying the expired clients, all states associated with these clients are also released.
v2:
. fix kernel test robot errors in nfsd.h when CONFIG_NFSD_V4 not defined.
v3:
. add mod_delayed_work in nfsd_courtesy_client_scan to kick start
the laundromat.
---
Dai Ngo (2):
NFSD: keep track of the number of courtesy clients in the system
NFSD: add shrinker to reap courtesy clients on low memory condition
fs/nfsd/netns.h | 5 ++++ fs/nfsd/nfs4state.c | 66 +++++++++++++++++++++++++++++++++++++++++++----- fs/nfsd/nfsctl.c | 6 +++-- fs/nfsd/nfsd.h | 9 +++++-- 4 files changed, 75 insertions(+), 11 deletions(-)
Dai Ngo
reviewerPATCH v3 1/2 NFSD: keep track of the number of courtesy clients in the system
Add counter nfs4_courtesy_client_count to nfsd_net to keep track of the number of courtesy clients in the system.
--- fs/nfsd/netns.h | 2 ++ fs/nfsd/nfs4state.c | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
fs/nfsd/netns.h +2 -0
@@ -192,6 +192,8 @@ struct nfsd_net {atomic_t nfs4_client_count;int nfs4_max_clients; + +atomic_t nfsd_courtesy_client_count; };/* Simple check to find out if a given net was properly initialized */
fs/nfsd/nfs4state.c +11 -3
@@ -169,7 +169,8 @@ static __be32 get_client_locked(struct nfs4_client *clp)if (is_client_expired (clp))return nfserr_expired;atomic_inc (&clp->cl_rpc_users); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );return nfs_ok; }
@@ -190,7 +191,8 @@ renew_client_locked(struct nfs4_client *clp)list_move_tail (&clp->cl_lru, &nn->client_lru); clp->cl_time =ktime_get_boottime_seconds (); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 ); }static void put_client_renew_locked (struct nfs4_client *clp)
@@ -2233,6 +2235,8 @@ __destroy_client(struct nfs4_client *clp)if (clp->cl_cb_conn.cb_xprt)svc_xprt_put (clp->cl_cb_conn.cb_xprt);atomic_add_unless (&nn->nfs4_client_count,-1 ,0 ); +if (clp->cl_state !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );free_client (clp);wake_up_all (&expiry_wq); }
@@ -4356,6 +4360,8 @@ void nfsd4_init_leases_net(struct nfsd_net *nn) max_clients = (u64 )si.totalram * si.mem_unit / (1024 *1024 *1024 ); max_clients *=NFS4_CLIENTS_PER_GB ; nn->nfs4_max_clients =max_t (int, max_clients,NFS4_CLIENTS_PER_GB ); + +atomic_set (&nn->nfsd_courtesy_client_count,0 ); }static void init_nfs4_replay (struct nfs4_replay *rp)
@@ -5879,7 +5885,9 @@ nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,if (!state_expired (lt, clp->cl_time))break ;if (!atomic_read (&clp->cl_rpc_users)) - clp->cl_state =NFSD4_COURTESY ; +if (xchg (&clp->cl_state,NFSD4_COURTESY ) == +NFSD4_ACTIVE ) +atomic_inc (&nn->nfsd_courtesy_client_count);if (!client_has_state (clp)) goto exp_client;if (!nfs4_anylock_blockers (clp))
Signature · 1 line
2.9.5
Jeff Layton
maintainerRe: [PATCH v3 1/2] NFSD: keep track of the number of courtesy clients in the system
Quoted 27 lines · Dai Ngo
Add counter
nfs4_courtesy_client_counttonfsd_netto keep track of the number of courtesy clients in the system.Signed-off-by Dai Ngo <[email protected]>--- fs/nfsd/netns.h | 2 ++ fs/nfsd/nfs4state.c | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)fs/nfsd/netns.h +2
-0@@ -192,6 +192,8 @@ struct nfsd_net {atomic_t nfs4_client_count;int nfs4_max_clients; + +atomic_t nfsd_courtesy_client_count; };/* Simple check to find out if a given net was properly initialized */ fs/nfsd/nfs4state.c +0
-0
fs/nfsd/nfs4state.c static __be32 get_client_locked(struct nfs4_client *clp) · line 173
Quoted 6 lines
fs/nfsd/nfs4state.c +2
-1@@ -169,7 +169,8 @@ static __be32 get_client_locked(struct nfs4_client *clp)if (is_client_expired (clp))return nfserr_expired;atomic_inc (&clp->cl_rpc_users); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );
I'd prefer this:
if (clp->cl_state != NFSD4_ACTIVE) atomic_add_unless(... clp->cl_state = NFSD4_ACTIVE;
It's more lines, but it's easier to read. It's may also be more efficient since you're protected by a spinlock anyway.
I think it's also less deceptive when reading the code. When I see xchg() operations being used like that, my immediate thought is that they must be needed for synchronization purposes. It's not in this case.
Quoted 42 lines
fs/nfsd/nfs4state.c +9
-2return nfs_ok; }@@ -190,7 +191,8 @@ renew_client_locked(struct nfs4_client *clp)list_move_tail (&clp->cl_lru, &nn->client_lru); clp->cl_time =ktime_get_boottime_seconds (); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 ); }static void put_client_renew_locked (struct nfs4_client *clp)@@ -2233,6 +2235,8 @@ __destroy_client(struct nfs4_client *clp)if (clp->cl_cb_conn.cb_xprt)svc_xprt_put (clp->cl_cb_conn.cb_xprt);atomic_add_unless (&nn->nfs4_client_count,-1 ,0 ); +if (clp->cl_state !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );free_client (clp);wake_up_all (&expiry_wq); }@@ -4356,6 +4360,8 @@ void nfsd4_init_leases_net(struct nfsd_net *nn) max_clients = (u64 )si.totalram * si.mem_unit / (1024 *1024 *1024 ); max_clients *=NFS4_CLIENTS_PER_GB ; nn->nfs4_max_clients =max_t (int, max_clients,NFS4_CLIENTS_PER_GB ); + +atomic_set (&nn->nfsd_courtesy_client_count,0 ); }static void init_nfs4_replay (struct nfs4_replay *rp)@@ -5879,7 +5885,9 @@ nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,if (!state_expired (lt, clp->cl_time))break ;if (!atomic_read (&clp->cl_rpc_users)) - clp->cl_state =NFSD4_COURTESY ; +if (xchg (&clp->cl_state,NFSD4_COURTESY ) == +NFSD4_ACTIVE ) +atomic_inc (&nn->nfsd_courtesy_client_count);if (!client_has_state (clp)) goto exp_client;if (!nfs4_anylock_blockers (clp))
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v3 1/2] NFSD: keep track of the number of courtesy clients in the system
Quoted 45 lines · Jeff Layton
Quoted 27 lines · Dai Ngo
Add counter
nfs4_courtesy_client_counttonfsd_netto keep track of the number of courtesy clients in the system.Signed-off-by Dai Ngo <[email protected]>---
fs/nfsd/netns.h| 2 ++fs/nfsd/nfs4state.c| 14 +++++++++++---2 files changed, 13 insertions(+), 3 deletions(-)fs/nfsd/netns.h +2
-0@@ -192,6 +192,8 @@ struct nfsd_net {atomic_t nfs4_client_count;int nfs4_max_clients; + +atomic_t nfsd_courtesy_client_count; };/* Simple check to find out if a given net was properly initialized */ fs/nfsd/nfs4state.c +0
-0fs/nfsd/nfs4state.c static __be32 get_client_locked(struct nfs4_client *clp) · line 173
Quoted 6 lines
fs/nfsd/nfs4state.c +2
-1@@ -169,7 +169,8 @@ static __be32 get_client_locked(struct nfs4_client *clp)if (is_client_expired (clp))return nfserr_expired;atomic_inc (&clp->cl_rpc_users); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );I'd prefer this:
if (clp->cl_state != NFSD4_ACTIVE) atomic_add_unless(... clp->cl_state = NFSD4_ACTIVE;
Quoted 6 lines
It's more lines, but it's easier to read. It's may also be more efficient since you're protected by a spinlock anyway.
I think it's also less deceptive when reading the code. When I see
xchg()operations being used like that, my immediate thought is that they must be needed for synchronization purposes. It's not in this case.
Agree: generally speaking, primitives like xchg() are used only when hardware-assisted synchronization is needed. These are expensive operations, to be used sparingly.
Let's replace these with plain vanilla "=", as Jeff suggested.
Likewise, I'm not totally convinced that nfsd_courtesy_client_count has to be an atomic.
Quoted 30 lines
Quoted 30 lines
fs/nfsd/nfs4state.c +6
-1return nfs_ok; }@@ -190,7 +191,8 @@ renew_client_locked(struct nfs4_client *clp)list_move_tail (&clp->cl_lru, &nn->client_lru); clp->cl_time =ktime_get_boottime_seconds (); - clp->cl_state =NFSD4_ACTIVE ; +if (xchg (&clp->cl_state,NFSD4_ACTIVE ) !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 ); }static void put_client_renew_locked (struct nfs4_client *clp)@@ -2233,6 +2235,8 @@ __destroy_client(struct nfs4_client *clp)if (clp->cl_cb_conn.cb_xprt)svc_xprt_put (clp->cl_cb_conn.cb_xprt);atomic_add_unless (&nn->nfs4_client_count,-1 ,0 ); +if (clp->cl_state !=NFSD4_ACTIVE ) +atomic_add_unless (&nn->nfsd_courtesy_client_count,-1 ,0 );free_client (clp);wake_up_all (&expiry_wq); }@@ -4356,6 +4360,8 @@ void nfsd4_init_leases_net(struct nfsd_net *nn) max_clients = (u64 )si.totalram * si.mem_unit / (1024 *1024 *1024 ); max_clients *=NFS4_CLIENTS_PER_GB ; nn->nfs4_max_clients =max_t (int, max_clients,NFS4_CLIENTS_PER_GB ); + +atomic_set (&nn->nfsd_courtesy_client_count,0 ); }
Quoted 15 lines
Quoted 12 lines
fs/nfsd/nfs4state.c +3
-1static void init_nfs4_replay (struct nfs4_replay *rp)@@ -5879,7 +5885,9 @@ nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,if (!state_expired (lt, clp->cl_time))break ;if (!atomic_read (&clp->cl_rpc_users)) - clp->cl_state =NFSD4_COURTESY ; +if (xchg (&clp->cl_state,NFSD4_COURTESY ) == +NFSD4_ACTIVE ) +atomic_inc (&nn->nfsd_courtesy_client_count);if (!client_has_state (clp)) goto exp_client;if (!nfs4_anylock_blockers (clp))Signature · 1 line
Jeff Layton <[email protected]>
--
Chuck Lever
Dai Ngo
reviewerPATCH v3 2/2 NFSD: add shrinker to reap courtesy clients on low memory condition
In reply to Dai NgoAdd the courtesy client shrinker to react to low memory condition triggered by the memory shrinker.
On the shrinker's count callback, we increment a callback counter and return the number of outstanding courtesy clients. When the laundromat runs, it checks if this counter is not zero and starts reaping old courtesy clients. The maximum number of clients to be reaped is limited to NFSD_CIENT_MAX_TRIM_PER_RUN (128). This limit is to prevent the laundromat from spending too much time reaping the clients and not processing other tasks in a timely manner.
The laundromat is rescheduled to run sooner if it detects low low memory condition and there are more clients to reap.
On the shrinker's scan callback, we return the number of clients That were reaped since the last scan callback. We can not reap the clients on the scan callback context since destroying the client might require call into the underlying filesystem or other subsystems which might allocate memory which can cause deadlock.
--- fs/nfsd/netns.h | 3 +++ fs/nfsd/nfs4state.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- fs/nfsd/nfsctl.c | 6 ++++-- fs/nfsd/nfsd.h | 9 +++++++-- 4 files changed, 62 insertions(+), 8 deletions(-)
fs/nfsd/netns.h +3 -0
@@ -194,6 +194,9 @@ struct nfsd_net {int nfs4_max_clients;atomic_t nfsd_courtesy_client_count; +atomic_t nfsd_client_shrinker_cb_count; +atomic_t nfsd_client_shrinker_reapcount; +struct shrinker nfsd_client_shrinker; };/* Simple check to find out if a given net was properly initialized */
fs/nfsd/nfs4state.c +48 -4
85 lines not on this page · raw message
fs/nfsd/nfsctl.c +4 -2
@@ -1481,11 +1481,12 @@ static __net_init int nfsd_init_net(struct net *net) goto out_idmap_error; nn->nfsd_versions =NULL ; nn->nfsd4_minorversions =NULL ; + retval =nfsd4_init_leases_net (nn); +if (retval) + goto out_drc_error; retval =nfsd_reply_cache_init (nn);if (retval) goto out_drc_error; -nfsd4_init_leases_net (nn); -get_random_bytes (&nn->siphash_key,sizeof (nn->siphash_key));seqlock_init (&nn->writeverf_lock);
@@ -1507,6 +1508,7 @@ static __net_exit void nfsd_exit_net(struct net *net)nfsd_idmap_shutdown (net);nfsd_export_shutdown (net);nfsd_netns_free_versions (net_generic (net, nfsd_net_id)); +nfsd4_leases_net_shutdown (nn); }static struct pernet_operations nfsd_net_ops = {
fs/nfsd/nfsd.h +7 -2
@@ -498,7 +498,11 @@ extern void unregister_cld_notifier(void);extern void nfsd4_ssc_init_umount_work (struct nfsd_net *nn);#endif -extern void nfsd4_init_leases_net (struct nfsd_net *nn); +extern int nfsd4_init_leases_net (struct nfsd_net *nn); +static inline void nfsd4_leases_net_shutdown (struct nfsd_net *nn) +{ +unregister_shrinker (&nn->nfsd_client_shrinker); +};#else /* CONFIG_NFSD_V4 */ static inline int nfsd4_is_junction (struct dentry *dentry)
@@ -506,7 +510,8 @@ static inline int nfsd4_is_junction(struct dentry *dentry)return 0 ; } -static inline void nfsd4_init_leases_net (struct nfsd_net *nn) {}; +static inline int nfsd4_init_leases_net (struct nfsd_net *nn) {return 0 ; }; +static inline void nfsd4_leases_net_shutdown (struct nfsd_net *nn) { };#define register_cld_notifier () 0#define unregister_cld_notifier () do { } while(0)
Signature · 1 line
2.9.5