Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / ldlm / ldlm_pool.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2010, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ldlm/ldlm_pool.c
32  *
33  * Author: Yury Umanets <umka@clusterfs.com>
34  */
35
36 /*
37  * Idea of this code is rather simple. Each second, for each server namespace
38  * we have SLV - server lock volume which is calculated on current number of
39  * granted locks, grant speed for past period, etc - that is, locking load.
40  * This SLV number may be thought as a flow definition for simplicity. It is
41  * sent to clients with each occasion to let them know what is current load
42  * situation on the server. By default, at the beginning, SLV on server is
43  * set max value which is calculated as the following: allow to one client
44  * have all locks of limit ->pl_limit for 10h.
45  *
46  * Next, on clients, number of cached locks is not limited artificially in any
47  * way as it was before. Instead, client calculates CLV, that is, client lock
48  * volume for each lock and compares it with last SLV from the server. CLV is
49  * calculated as the number of locks in LRU * lock live time in seconds. If
50  * CLV > SLV - lock is canceled.
51  *
52  * Client has LVF, that is, lock volume factor which regulates how much
53  * sensitive client should be about last SLV from server. The higher LVF is the
54  * more locks will be canceled on client. Default value for it is 1. Setting
55  * LVF to 2 means that client will cancel locks 2 times faster.
56  *
57  * Locks on a client will be canceled more intensively in these cases:
58  * (1) if SLV is smaller, that is, load is higher on the server;
59  * (2) client has a lot of locks (the more locks are held by client, the bigger
60  *     chances that some of them should be canceled);
61  * (3) client has old locks (taken some time ago);
62  *
63  * Thus, according to flow paradigm that we use for better understanding SLV,
64  * CLV is the volume of particle in flow described by SLV. According to this,
65  * if flow is getting thinner, more and more particles become outside of it and
66  * as particles are locks, they should be canceled.
67  *
68  * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
69  * Andreas Dilger(adilger@clusterfs.com) proposed few nice ideas like using LVF
70  * and many cleanups. Flow definition to allow more easy understanding of the
71  * logic belongs to Nikita Danilov(nikita@clusterfs.com) as well as many
72  * cleanups and fixes. And design and implementation are done by Yury Umanets
73  * (umka@clusterfs.com).
74  *
75  * Glossary for terms used:
76  *
77  * pl_limit - Number of allowed locks in pool. Applies to server and client
78  * side (tunable);
79  *
80  * pl_granted - Number of granted locks (calculated);
81  * pl_grant_rate - Number of granted locks for last T (calculated);
82  * pl_cancel_rate - Number of canceled locks for last T (calculated);
83  * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
84  * pl_grant_plan - Planned number of granted locks for next T (calculated);
85  * pl_server_lock_volume - Current server lock volume (calculated);
86  *
87  * As it may be seen from list above, we have few possible tunables which may
88  * affect behavior much. They all may be modified via sysfs. However, they also
89  * give a possibility for constructing few pre-defined behavior policies. If
90  * none of predefines is suitable for a working pattern being used, new one may
91  * be "constructed" via sysfs tunables.
92  */
93
94 #define DEBUG_SUBSYSTEM S_LDLM
95
96 #include <linux/workqueue.h>
97 #include <libcfs/linux/linux-mem.h>
98 #include <lustre_dlm.h>
99 #include <cl_object.h>
100 #include <obd_class.h>
101 #include <obd_support.h>
102 #include "ldlm_internal.h"
103
104 #ifdef HAVE_LRU_RESIZE_SUPPORT
105
106 /*
107  * 50 ldlm locks for 1MB of RAM.
108  */
109 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_SHIFT)) * 50)
110
111 /*
112  * Maximal possible grant step plan in %.
113  */
114 #define LDLM_POOL_MAX_GSP (30)
115
116 /*
117  * Minimal possible grant step plan in %.
118  */
119 #define LDLM_POOL_MIN_GSP (1)
120
121 /*
122  * This controls the speed of reaching LDLM_POOL_MAX_GSP
123  * with increasing thread period.
124  */
125 #define LDLM_POOL_GSP_STEP_SHIFT (2)
126
127 /*
128  * LDLM_POOL_GSP% of all locks is default GP.
129  */
130 #define LDLM_POOL_GP(L)   (((L) * LDLM_POOL_MAX_GSP) / 100)
131
132 /*
133  * Max age for locks on clients.
134  */
135 #define LDLM_POOL_MAX_AGE (36000)
136
137 /*
138  * The granularity of SLV calculation.
139  */
140 #define LDLM_POOL_SLV_SHIFT (10)
141
142 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
143 {
144         return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
145 }
146
147 static inline __u64 ldlm_pool_slv_max(__u32 L)
148 {
149         /*
150          * Allow to have all locks for 1 client for 10 hrs.
151          * Formula is the following: limit * 10h / 1 client.
152          */
153         __u64 lim = (__u64)L *  LDLM_POOL_MAX_AGE / 1;
154         return lim;
155 }
156
157 static inline __u64 ldlm_pool_slv_min(__u32 L)
158 {
159         return 1;
160 }
161
162 enum {
163         LDLM_POOL_FIRST_STAT = 0,
164         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
165         LDLM_POOL_GRANT_STAT,
166         LDLM_POOL_CANCEL_STAT,
167         LDLM_POOL_GRANT_RATE_STAT,
168         LDLM_POOL_CANCEL_RATE_STAT,
169         LDLM_POOL_GRANT_PLAN_STAT,
170         LDLM_POOL_SLV_STAT,
171         LDLM_POOL_SHRINK_REQTD_STAT,
172         LDLM_POOL_SHRINK_FREED_STAT,
173         LDLM_POOL_RECALC_STAT,
174         LDLM_POOL_TIMING_STAT,
175         LDLM_POOL_LAST_STAT
176 };
177
178 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
179 {
180         return container_of(pl, struct ldlm_namespace, ns_pool);
181 }
182
183 /**
184  * Calculates suggested grant_step in % of available locks for passed
185  * \a period. This is later used in grant_plan calculations.
186  */
187 static inline int ldlm_pool_t2gsp(unsigned int t)
188 {
189         /*
190          * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
191          * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
192          *
193          * How this will affect execution is the following:
194          *
195          * - for thread period 1s we will have grant_step 1% which good from
196          * pov of taking some load off from server and push it out to clients.
197          * This is like that because 1% for grant_step means that server will
198          * not allow clients to get lots of locks in short period of time and
199          * keep all old locks in their caches. Clients will always have to
200          * get some locks back if they want to take some new;
201          *
202          * - for thread period 10s (which is default) we will have 23% which
203          * means that clients will have enough of room to take some new locks
204          * without getting some back. All locks from this 23% which were not
205          * taken by clients in current period will contribute in SLV growing.
206          * SLV growing means more locks cached on clients until limit or grant
207          * plan is reached.
208          */
209         return LDLM_POOL_MAX_GSP -
210                 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
211                  (t >> LDLM_POOL_GSP_STEP_SHIFT));
212 }
213
214 static inline int ldlm_pool_granted(struct ldlm_pool *pl)
215 {
216         return atomic_read(&pl->pl_granted);
217 }
218
219 /**
220  * Recalculates next grant limit on passed \a pl.
221  *
222  * \pre ->pl_lock is locked.
223  */
224 static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
225 {
226         int granted, grant_step, limit;
227
228         limit = ldlm_pool_get_limit(pl);
229         granted = ldlm_pool_granted(pl);
230
231         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
232         grant_step = ((limit - granted) * grant_step) / 100;
233         pl->pl_grant_plan = granted + grant_step;
234         limit = (limit * 5) >> 2;
235         if (pl->pl_grant_plan > limit)
236                 pl->pl_grant_plan = limit;
237 }
238
239 /**
240  * Recalculates next SLV on passed \a pl.
241  *
242  * \pre ->pl_lock is locked.
243  */
244 static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
245 {
246         int granted;
247         int grant_plan;
248         int round_up;
249         __u64 slv;
250         __u64 slv_factor;
251         __u64 grant_usage;
252         __u32 limit;
253
254         slv = pl->pl_server_lock_volume;
255         grant_plan = pl->pl_grant_plan;
256         limit = ldlm_pool_get_limit(pl);
257         granted = ldlm_pool_granted(pl);
258         round_up = granted < limit;
259
260         grant_usage = max_t(int, limit - (granted - grant_plan), 1);
261
262         /*
263          * Find out SLV change factor which is the ratio of grant usage
264          * from limit. SLV changes as fast as the ratio of grant plan
265          * consumption. The more locks from grant plan are not consumed
266          * by clients in last interval (idle time), the faster grows
267          * SLV. And the opposite, the more grant plan is over-consumed
268          * (load time) the faster drops SLV.
269          */
270         slv_factor = (grant_usage << LDLM_POOL_SLV_SHIFT);
271         do_div(slv_factor, limit);
272         slv = slv * slv_factor;
273         slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
274
275         if (slv > ldlm_pool_slv_max(limit))
276                 slv = ldlm_pool_slv_max(limit);
277         else if (slv < ldlm_pool_slv_min(limit))
278                 slv = ldlm_pool_slv_min(limit);
279
280         pl->pl_server_lock_volume = slv;
281 }
282
283 /**
284  * Recalculates next stats on passed \a pl.
285  *
286  * \pre ->pl_lock is locked.
287  */
288 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl, timeout_t period)
289 {
290         int grant_plan = pl->pl_grant_plan;
291         __u64 slv = pl->pl_server_lock_volume;
292         int granted = ldlm_pool_granted(pl);
293         int grant_rate = atomic_read(&pl->pl_grant_rate) / period;
294         int cancel_rate = atomic_read(&pl->pl_cancel_rate) / period;
295
296         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
297                             slv);
298         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
299                             granted);
300         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
301                             grant_rate);
302         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
303                             grant_plan);
304         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
305                             cancel_rate);
306 }
307
308 /**
309  * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
310  */
311 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
312 {
313         struct obd_device *obd;
314
315         /*
316          * Set new SLV in obd field for using it later without accessing the
317          * pool. This is required to avoid race between sending reply to client
318          * with new SLV and cleanup server stack in which we can't guarantee
319          * that namespace is still alive. We know only that obd is alive as
320          * long as valid export is alive.
321          */
322         obd = ldlm_pl2ns(pl)->ns_obd;
323         LASSERT(obd != NULL);
324         write_lock(&obd->obd_pool_lock);
325         obd->obd_pool_slv = pl->pl_server_lock_volume;
326         write_unlock(&obd->obd_pool_lock);
327 }
328
329 /**
330  * Recalculates all pool fields on passed \a pl.
331  *
332  * \pre ->pl_lock is not locked.
333  */
334 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl, bool force)
335 {
336         timeout_t recalc_interval_sec;
337
338         ENTRY;
339
340         recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
341         if (!force && recalc_interval_sec < pl->pl_recalc_period)
342                 RETURN(0);
343
344         spin_lock(&pl->pl_lock);
345         recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
346         if (!force && recalc_interval_sec < pl->pl_recalc_period) {
347                 spin_unlock(&pl->pl_lock);
348                 RETURN(0);
349         }
350         /*
351          * Recalc SLV after last period. This should be done
352          * _before_ recalculating new grant plan.
353          */
354         ldlm_pool_recalc_slv(pl);
355
356         /*
357          * Make sure that pool informed obd of last SLV changes.
358          */
359         ldlm_srv_pool_push_slv(pl);
360
361         /*
362          * Update grant_plan for new period.
363          */
364         ldlm_pool_recalc_grant_plan(pl);
365
366         pl->pl_recalc_time = ktime_get_seconds();
367         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
368                             recalc_interval_sec);
369         spin_unlock(&pl->pl_lock);
370         RETURN(0);
371 }
372
373 /**
374  * This function is used on server side as main entry point for memory
375  * pressure handling. It decreases SLV on \a pl according to passed
376  * \a nr and \a gfp_mask.
377  *
378  * Our goal here is to decrease SLV such a way that clients hold \a nr
379  * locks smaller in next 10h.
380  */
381 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
382                                 int nr,  gfp_t gfp_mask)
383 {
384         __u32 limit;
385
386         /*
387          * VM is asking how many entries may be potentially freed.
388          */
389         if (nr == 0)
390                 return ldlm_pool_granted(pl);
391
392         /*
393          * Client already canceled locks but server is already in shrinker
394          * and can't cancel anything. Let's catch this race.
395          */
396         if (ldlm_pool_granted(pl) == 0)
397                 RETURN(0);
398
399         spin_lock(&pl->pl_lock);
400
401         /*
402          * We want shrinker to possibly cause cancellation of @nr locks from
403          * clients or grant approximately @nr locks smaller next intervals.
404          *
405          * This is why we decreased SLV by @nr. This effect will only be as
406          * long as one re-calc interval (1s these days) and this should be
407          * enough to pass this decreased SLV to all clients. On next recalc
408          * interval pool will either increase SLV if locks load is not high
409          * or will keep on same level or even decrease again, thus, shrinker
410          * decreased SLV will affect next recalc intervals and this way will
411          * make locking load lower.
412          */
413         if (nr < pl->pl_server_lock_volume) {
414                 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
415         } else {
416                 limit = ldlm_pool_get_limit(pl);
417                 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
418         }
419
420         /*
421          * Make sure that pool informed obd of last SLV changes.
422          */
423         ldlm_srv_pool_push_slv(pl);
424         spin_unlock(&pl->pl_lock);
425
426         /*
427          * We did not really free any memory here so far, it only will be
428          * freed later may be, so that we return 0 to not confuse VM.
429          */
430         return 0;
431 }
432
433 /**
434  * Setup server side pool \a pl with passed \a limit.
435  */
436 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
437 {
438         struct obd_device *obd;
439
440         obd = ldlm_pl2ns(pl)->ns_obd;
441         LASSERT(obd != NULL && obd != LP_POISON);
442         LASSERT(obd->obd_type != LP_POISON);
443         write_lock(&obd->obd_pool_lock);
444         obd->obd_pool_limit = limit;
445         write_unlock(&obd->obd_pool_lock);
446
447         ldlm_pool_set_limit(pl, limit);
448         return 0;
449 }
450
451 /**
452  * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
453  */
454 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
455 {
456         struct obd_device *obd;
457
458         /*
459          * Get new SLV and Limit from obd which is updated with coming
460          * RPCs.
461          */
462         obd = ldlm_pl2ns(pl)->ns_obd;
463         LASSERT(obd != NULL);
464         read_lock(&obd->obd_pool_lock);
465         pl->pl_server_lock_volume = obd->obd_pool_slv;
466         ldlm_pool_set_limit(pl, obd->obd_pool_limit);
467         read_unlock(&obd->obd_pool_lock);
468 }
469
470 /**
471  * Recalculates client size pool \a pl according to current SLV and Limit.
472  */
473 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl, bool force)
474 {
475         timeout_t recalc_interval_sec;
476         int ret;
477
478         ENTRY;
479
480         recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
481         if (!force && recalc_interval_sec < pl->pl_recalc_period)
482                 RETURN(0);
483
484         spin_lock(&pl->pl_lock);
485         /*
486          * Check if we need to recalc lists now.
487          */
488         recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
489         if (!force && recalc_interval_sec < pl->pl_recalc_period) {
490                 spin_unlock(&pl->pl_lock);
491                 RETURN(0);
492         }
493
494         /*
495          * Make sure that pool knows last SLV and Limit from obd.
496          */
497         ldlm_cli_pool_pop_slv(pl);
498         spin_unlock(&pl->pl_lock);
499
500         /*
501          * In the time of canceling locks on client we do not need to maintain
502          * sharp timing, we only want to cancel locks asap according to new SLV.
503          * It may be called when SLV has changed much, this is why we do not
504          * take into account pl->pl_recalc_time here.
505          */
506         ret = ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LCF_ASYNC, 0);
507
508         spin_lock(&pl->pl_lock);
509         /*
510          * Time of LRU resizing might be longer than period,
511          * so update after LRU resizing rather than before it.
512          */
513         pl->pl_recalc_time = ktime_get_seconds();
514         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
515                             recalc_interval_sec);
516         spin_unlock(&pl->pl_lock);
517         RETURN(ret);
518 }
519
520 /**
521  * This function is main entry point for memory pressure handling on client
522  * side.  Main goal of this function is to cancel some number of locks on
523  * passed \a pl according to \a nr and \a gfp_mask.
524  */
525 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
526                                 int nr, gfp_t gfp_mask)
527 {
528         struct ldlm_namespace *ns;
529         int unused;
530
531         ns = ldlm_pl2ns(pl);
532
533         /*
534          * Do not cancel locks in case lru resize is disabled for this ns.
535          */
536         if (!ns_connect_lru_resize(ns))
537                 RETURN(0);
538
539         /*
540          * Make sure that pool knows last SLV and Limit from obd.
541          */
542         spin_lock(&pl->pl_lock);
543         ldlm_cli_pool_pop_slv(pl);
544         spin_unlock(&pl->pl_lock);
545
546         spin_lock(&ns->ns_lock);
547         unused = ns->ns_nr_unused;
548         spin_unlock(&ns->ns_lock);
549
550         if (nr == 0)
551                 return (unused / 100) * sysctl_vfs_cache_pressure;
552         else
553                 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, 0);
554 }
555
556 static struct ldlm_pool_ops ldlm_srv_pool_ops = {
557         .po_recalc = ldlm_srv_pool_recalc,
558         .po_shrink = ldlm_srv_pool_shrink,
559         .po_setup  = ldlm_srv_pool_setup
560 };
561
562 static struct ldlm_pool_ops ldlm_cli_pool_ops = {
563         .po_recalc = ldlm_cli_pool_recalc,
564         .po_shrink = ldlm_cli_pool_shrink
565 };
566
567 /**
568  * Pool recalc wrapper. Will call either client or server pool recalc callback
569  * depending what pool \a pl is used.
570  *
571  * \retval              time in seconds for the next recalc of this pool
572  */
573 time64_t ldlm_pool_recalc(struct ldlm_pool *pl, bool force)
574 {
575         timeout_t recalc_interval_sec;
576         int count;
577
578         recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
579         if (recalc_interval_sec > 0) {
580                 spin_lock(&pl->pl_lock);
581                 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
582
583                 if (recalc_interval_sec > 0) {
584                         /*
585                          * Update pool statistics every recalc interval.
586                          */
587                         ldlm_pool_recalc_stats(pl, recalc_interval_sec);
588
589                         /*
590                          * Zero out all rates and speed for the last period.
591                          */
592                         atomic_set(&pl->pl_grant_rate, 0);
593                         atomic_set(&pl->pl_cancel_rate, 0);
594                 }
595                 spin_unlock(&pl->pl_lock);
596         }
597
598         if (pl->pl_ops->po_recalc != NULL) {
599                 count = pl->pl_ops->po_recalc(pl, force);
600                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
601                                     count);
602         }
603
604         return pl->pl_recalc_time + pl->pl_recalc_period;
605 }
606
607 /**
608  * Pool shrink wrapper. Will call either client or server pool recalc callback
609  * depending what pool \a pl is used.
610  */
611 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
612 {
613         int cancel = 0;
614
615         if (pl->pl_ops->po_shrink != NULL) {
616                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
617                 if (nr > 0) {
618                         lprocfs_counter_add(pl->pl_stats,
619                                             LDLM_POOL_SHRINK_REQTD_STAT,
620                                             nr);
621                         lprocfs_counter_add(pl->pl_stats,
622                                             LDLM_POOL_SHRINK_FREED_STAT,
623                                             cancel);
624                         CDEBUG(D_DLMTRACE,
625                                "%s: request to shrink %d locks, shrunk %d\n",
626                                pl->pl_name, nr, cancel);
627                 }
628         }
629         return cancel;
630 }
631
632 /**
633  * Pool setup wrapper. Will call either client or server pool recalc callback
634  * depending what pool \a pl is used.
635  *
636  * Sets passed \a limit into pool \a pl.
637  */
638 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
639 {
640         if (pl->pl_ops->po_setup != NULL)
641                 return pl->pl_ops->po_setup(pl, limit);
642         return 0;
643 }
644
645 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
646 {
647         int granted, grant_rate, cancel_rate, grant_step;
648         int grant_speed, grant_plan, lvf;
649         struct ldlm_pool *pl = m->private;
650         timeout_t period;
651         __u64 slv, clv;
652         __u32 limit;
653
654         spin_lock(&pl->pl_lock);
655         slv = pl->pl_server_lock_volume;
656         clv = pl->pl_client_lock_volume;
657         limit = ldlm_pool_get_limit(pl);
658         grant_plan = pl->pl_grant_plan;
659         granted = ldlm_pool_granted(pl);
660         period = ktime_get_seconds() - pl->pl_recalc_time;
661         if (period <= 0)
662                 period = 1;
663         grant_rate = atomic_read(&pl->pl_grant_rate) / period;
664         cancel_rate = atomic_read(&pl->pl_cancel_rate) / period;
665         grant_speed = grant_rate - cancel_rate;
666         lvf = atomic_read(&pl->pl_lock_volume_factor);
667         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
668         spin_unlock(&pl->pl_lock);
669
670         seq_printf(m, "LDLM pool state (%s):\n"
671                    "  SLV: %llu\n"
672                    "  CLV: %llu\n"
673                    "  LVF: %d\n",
674                    pl->pl_name, slv, clv, (lvf * 100) >> 8);
675
676         if (ns_is_server(ldlm_pl2ns(pl))) {
677                 seq_printf(m, "  GSP: %d%%\n", grant_step);
678                 seq_printf(m, "  GP:  %d\n", grant_plan);
679         }
680
681         seq_printf(m, "  GR:  %d\n  CR:  %d\n  GS:  %d\n  G:   %d\n  L:   %d\n",
682                    grant_rate, cancel_rate, grant_speed,
683                    granted, limit);
684         return 0;
685 }
686
687 LDEBUGFS_SEQ_FOPS_RO(lprocfs_pool_state);
688
689 static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
690                                 char *buf)
691 {
692         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
693                                             pl_kobj);
694         int grant_speed;
695         timeout_t period;
696
697         spin_lock(&pl->pl_lock);
698         /* serialize with ldlm_pool_recalc */
699         period = ktime_get_seconds() - pl->pl_recalc_time;
700         if (period <= 0)
701                 period = 1;
702         grant_speed = (atomic_read(&pl->pl_grant_rate) -
703                        atomic_read(&pl->pl_cancel_rate)) / period;
704         spin_unlock(&pl->pl_lock);
705         return sprintf(buf, "%d\n", grant_speed);
706 }
707 LUSTRE_RO_ATTR(grant_speed);
708
709 LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
710 LUSTRE_RO_ATTR(grant_plan);
711
712 LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
713 LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
714 LUSTRE_RW_ATTR(recalc_period);
715
716 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
717 LUSTRE_RO_ATTR(server_lock_volume);
718
719 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(client_lock_volume, u64);
720 LUSTRE_RO_ATTR(client_lock_volume);
721
722 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
723 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
724 LUSTRE_RW_ATTR(limit);
725
726 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
727 LUSTRE_RO_ATTR(granted);
728
729 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
730 LUSTRE_RO_ATTR(cancel_rate);
731
732 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
733 LUSTRE_RO_ATTR(grant_rate);
734
735 static ssize_t lock_volume_factor_show(struct kobject *kobj,
736                                        struct attribute *attr,
737                                        char *buf)
738 {
739         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
740         unsigned long tmp;
741
742         tmp = (atomic_read(&pl->pl_lock_volume_factor) * 100) >> 8;
743         return sprintf(buf, "%lu\n", tmp);
744 }
745
746 static ssize_t lock_volume_factor_store(struct kobject *kobj,
747                                         struct attribute *attr,
748                                         const char *buffer,
749                                         size_t count)
750 {
751         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
752         unsigned long tmp;
753         int rc;
754
755         rc = kstrtoul(buffer, 10, &tmp);
756         if (rc < 0)
757                 return rc;
758
759         tmp = (tmp << 8) / 100;
760         atomic_set(&pl->pl_lock_volume_factor, tmp);
761
762         return count;
763
764 }
765 LUSTRE_RW_ATTR(lock_volume_factor);
766
767 static ssize_t recalc_time_show(struct kobject *kobj,
768                                 struct attribute *attr,
769                                 char *buf)
770 {
771         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
772
773         return snprintf(buf, PAGE_SIZE, "%llu\n",
774                         ktime_get_seconds() - pl->pl_recalc_time);
775 }
776 LUSTRE_RO_ATTR(recalc_time);
777
778 /* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
779 static struct attribute *ldlm_pl_attrs[] = {
780         &lustre_attr_grant_speed.attr,
781         &lustre_attr_grant_plan.attr,
782         &lustre_attr_recalc_period.attr,
783         &lustre_attr_server_lock_volume.attr,
784         &lustre_attr_client_lock_volume.attr,
785         &lustre_attr_recalc_time.attr,
786         &lustre_attr_limit.attr,
787         &lustre_attr_granted.attr,
788         &lustre_attr_cancel_rate.attr,
789         &lustre_attr_grant_rate.attr,
790         &lustre_attr_lock_volume_factor.attr,
791         NULL,
792 };
793
794 KOBJ_ATTRIBUTE_GROUPS(ldlm_pl);
795
796 static void ldlm_pl_release(struct kobject *kobj)
797 {
798         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
799                                             pl_kobj);
800         complete(&pl->pl_kobj_unregister);
801 }
802
803 static struct kobj_type ldlm_pl_ktype = {
804         .default_groups = KOBJ_ATTR_GROUPS(ldlm_pl),
805         .sysfs_ops      = &lustre_sysfs_ops,
806         .release        = ldlm_pl_release,
807 };
808
809 static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
810 {
811         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
812         int err;
813
814         init_completion(&pl->pl_kobj_unregister);
815         err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
816                                    "pool");
817
818         return err;
819 }
820
821 static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
822 {
823         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
824         struct dentry *debugfs_ns_parent;
825         struct ldebugfs_vars pool_vars[2];
826         int rc = 0;
827
828         ENTRY;
829
830         debugfs_ns_parent = ns->ns_debugfs_entry;
831         if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
832                 CERROR("%s: debugfs entry is not initialized\n",
833                        ldlm_ns_name(ns));
834                 GOTO(out, rc = -EINVAL);
835         }
836         pl->pl_debugfs_entry = debugfs_create_dir("pool", debugfs_ns_parent);
837
838         memset(pool_vars, 0, sizeof(pool_vars));
839
840         ldlm_add_var(&pool_vars[0], pl->pl_debugfs_entry, "state", pl,
841                      &lprocfs_pool_state_fops);
842
843         pl->pl_stats = lprocfs_stats_alloc(LDLM_POOL_LAST_STAT -
844                                            LDLM_POOL_FIRST_STAT, 0);
845         if (!pl->pl_stats)
846                 GOTO(out, rc = -ENOMEM);
847
848         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
849                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
850                              "granted");
851         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
852                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
853                              "grant");
854         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
855                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
856                              "cancel");
857         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
858                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKSPS,
859                              "grant_rate");
860         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
861                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKSPS,
862                              "cancel_rate");
863         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
864                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKSPS,
865                              "grant_plan");
866         lprocfs_counter_init_units(pl->pl_stats, LDLM_POOL_SLV_STAT,
867                              LPROCFS_CNTR_AVGMINMAX, "slv", "lock.secs");
868         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
869                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
870                              "shrink_request");
871         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
872                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
873                              "shrink_freed");
874         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
875                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_LOCKS,
876                              "recalc_freed");
877         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
878                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_SECS,
879                              "recalc_timing");
880         debugfs_create_file("stats", 0644, pl->pl_debugfs_entry,
881                             pl->pl_stats, &ldebugfs_stats_seq_fops);
882
883         EXIT;
884 out:
885         return rc;
886 }
887
888 static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
889 {
890         kobject_put(&pl->pl_kobj);
891         wait_for_completion(&pl->pl_kobj_unregister);
892 }
893
894 static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
895 {
896         if (pl->pl_stats != NULL) {
897                 lprocfs_stats_free(&pl->pl_stats);
898                 pl->pl_stats = NULL;
899         }
900         debugfs_remove_recursive(pl->pl_debugfs_entry);
901         pl->pl_debugfs_entry = NULL;
902 }
903
904 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
905                    int idx, enum ldlm_side client)
906 {
907         int rc;
908
909         ENTRY;
910
911         spin_lock_init(&pl->pl_lock);
912         atomic_set(&pl->pl_granted, 0);
913         pl->pl_recalc_time = ktime_get_seconds();
914         atomic_set(&pl->pl_lock_volume_factor, 1 << 8);
915
916         atomic_set(&pl->pl_grant_rate, 0);
917         atomic_set(&pl->pl_cancel_rate, 0);
918         pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
919
920         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
921                  ldlm_ns_name(ns), idx);
922
923         if (client == LDLM_NAMESPACE_SERVER) {
924                 pl->pl_ops = &ldlm_srv_pool_ops;
925                 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
926                 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
927                 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
928         } else {
929                 ldlm_pool_set_limit(pl, 1);
930                 pl->pl_server_lock_volume = 0;
931                 pl->pl_ops = &ldlm_cli_pool_ops;
932                 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
933         }
934         pl->pl_client_lock_volume = 0;
935         rc = ldlm_pool_debugfs_init(pl);
936         if (rc)
937                 RETURN(rc);
938
939         rc = ldlm_pool_sysfs_init(pl);
940         if (rc)
941                 RETURN(rc);
942
943         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
944
945         RETURN(rc);
946 }
947
948 void ldlm_pool_fini(struct ldlm_pool *pl)
949 {
950         ENTRY;
951         ldlm_pool_sysfs_fini(pl);
952         ldlm_pool_debugfs_fini(pl);
953
954         /*
955          * Pool should not be used after this point. We can't free it here as
956          * it lives in struct ldlm_namespace, but still interested in catching
957          * any abnormal using cases.
958          */
959         POISON(pl, 0x5a, sizeof(*pl));
960         EXIT;
961 }
962
963 /**
964  * Add new taken ldlm lock \a lock into pool \a pl accounting.
965  */
966 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
967 {
968         /*
969          * FLOCK locks are special in a sense that they are almost never
970          * cancelled, instead special kind of lock is used to drop them.
971          * also there is no LRU for flock locks, so no point in tracking
972          * them anyway.
973          *
974          * PLAIN locks are used by config and quota, the quantity is small
975          * and usually they are not in LRU.
976          */
977         if (lock->l_resource->lr_type == LDLM_FLOCK ||
978             lock->l_resource->lr_type == LDLM_PLAIN)
979                 return;
980
981         ldlm_reclaim_add(lock);
982
983         atomic_inc(&pl->pl_granted);
984         atomic_inc(&pl->pl_grant_rate);
985         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
986         /*
987          * Do not do pool recalc for client side as all locks which
988          * potentially may be canceled has already been packed into
989          * enqueue/cancel rpc. Also we do not want to run out of stack
990          * with too long call paths.
991          */
992         if (ns_is_server(ldlm_pl2ns(pl)))
993                 ldlm_pool_recalc(pl, false);
994 }
995
996 /**
997  * Remove ldlm lock \a lock from pool \a pl accounting.
998  */
999 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1000 {
1001         /*
1002          * Filter out FLOCK & PLAIN locks. Read above comment in
1003          * ldlm_pool_add().
1004          */
1005         if (lock->l_resource->lr_type == LDLM_FLOCK ||
1006             lock->l_resource->lr_type == LDLM_PLAIN)
1007                 return;
1008
1009         ldlm_reclaim_del(lock);
1010
1011         LASSERT(atomic_read(&pl->pl_granted) > 0);
1012         atomic_dec(&pl->pl_granted);
1013         atomic_inc(&pl->pl_cancel_rate);
1014
1015         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
1016
1017         if (ns_is_server(ldlm_pl2ns(pl)))
1018                 ldlm_pool_recalc(pl, false);
1019 }
1020
1021 /**
1022  * Returns current \a pl SLV.
1023  *
1024  * \pre ->pl_lock is not locked.
1025  */
1026 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1027 {
1028         __u64 slv;
1029
1030         spin_lock(&pl->pl_lock);
1031         slv = pl->pl_server_lock_volume;
1032         spin_unlock(&pl->pl_lock);
1033         return slv;
1034 }
1035
1036 /**
1037  * Sets passed \a slv to \a pl.
1038  *
1039  * \pre ->pl_lock is not locked.
1040  */
1041 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1042 {
1043         spin_lock(&pl->pl_lock);
1044         pl->pl_server_lock_volume = slv;
1045         spin_unlock(&pl->pl_lock);
1046 }
1047
1048 /**
1049  * Returns current \a pl CLV.
1050  *
1051  * \pre ->pl_lock is not locked.
1052  */
1053 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1054 {
1055         __u64 slv;
1056
1057         spin_lock(&pl->pl_lock);
1058         slv = pl->pl_client_lock_volume;
1059         spin_unlock(&pl->pl_lock);
1060         return slv;
1061 }
1062
1063 /**
1064  * Sets passed \a clv to \a pl.
1065  *
1066  * \pre ->pl_lock is not locked.
1067  */
1068 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1069 {
1070         spin_lock(&pl->pl_lock);
1071         pl->pl_client_lock_volume = clv;
1072         spin_unlock(&pl->pl_lock);
1073 }
1074
1075 /**
1076  * Returns current \a pl limit.
1077  */
1078 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1079 {
1080         return atomic_read(&pl->pl_limit);
1081 }
1082
1083 /**
1084  * Sets passed \a limit to \a pl.
1085  */
1086 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1087 {
1088         atomic_set(&pl->pl_limit, limit);
1089 }
1090
1091 /**
1092  * Returns current LVF from \a pl.
1093  */
1094 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1095 {
1096         return atomic_read(&pl->pl_lock_volume_factor);
1097 }
1098
1099 /*
1100  * count locks from all namespaces (if possible). Returns number of
1101  * cached locks.
1102  */
1103 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
1104 {
1105         unsigned long total = 0;
1106         int nr_ns;
1107         struct ldlm_namespace *ns;
1108         struct ldlm_namespace *ns_old = NULL; /* loop detection */
1109
1110         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1111                 return 0;
1112
1113         /*
1114          * Find out how many resources we may release.
1115          */
1116         for (nr_ns = ldlm_namespace_nr_read(client);
1117              nr_ns > 0; nr_ns--) {
1118                 mutex_lock(ldlm_namespace_lock(client));
1119                 if (list_empty(ldlm_namespace_list(client))) {
1120                         mutex_unlock(ldlm_namespace_lock(client));
1121                         return 0;
1122                 }
1123                 ns = ldlm_namespace_first_locked(client);
1124
1125                 if (ns == ns_old) {
1126                         mutex_unlock(ldlm_namespace_lock(client));
1127                         break;
1128                 }
1129
1130                 if (ldlm_ns_empty(ns)) {
1131                         ldlm_namespace_move_to_inactive_locked(ns, client);
1132                         mutex_unlock(ldlm_namespace_lock(client));
1133                         continue;
1134                 }
1135
1136                 if (ns_old == NULL)
1137                         ns_old = ns;
1138
1139                 ldlm_namespace_get(ns);
1140                 ldlm_namespace_move_to_active_locked(ns, client);
1141                 mutex_unlock(ldlm_namespace_lock(client));
1142                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1143                 ldlm_namespace_put(ns);
1144         }
1145
1146         return total;
1147 }
1148
1149 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
1150                                      gfp_t gfp_mask)
1151 {
1152         unsigned long freed = 0;
1153         int tmp, nr_ns;
1154         struct ldlm_namespace *ns;
1155
1156         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1157                 return -1;
1158
1159         /*
1160          * Shrink at least ldlm_namespace_nr_read(client) namespaces.
1161          */
1162         for (tmp = nr_ns = ldlm_namespace_nr_read(client);
1163              tmp > 0; tmp--) {
1164                 int cancel, nr_locks;
1165
1166                 /*
1167                  * Do not call shrink under ldlm_namespace_lock(client)
1168                  */
1169                 mutex_lock(ldlm_namespace_lock(client));
1170                 if (list_empty(ldlm_namespace_list(client))) {
1171                         mutex_unlock(ldlm_namespace_lock(client));
1172                         break;
1173                 }
1174                 ns = ldlm_namespace_first_locked(client);
1175                 ldlm_namespace_get(ns);
1176                 ldlm_namespace_move_to_active_locked(ns, client);
1177                 mutex_unlock(ldlm_namespace_lock(client));
1178
1179                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1180                 /*
1181                  * We use to shrink propotionally but with new shrinker API,
1182                  * we lost the total number of freeable locks.
1183                  */
1184                 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
1185                 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1186                 ldlm_namespace_put(ns);
1187         }
1188         /*
1189          * we only decrease the SLV in server pools shrinker, return
1190          * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1191          */
1192         return (client == LDLM_NAMESPACE_SERVER) ? SHRINK_STOP : freed;
1193 }
1194
1195 #ifdef HAVE_SHRINKER_COUNT
1196 static unsigned long ldlm_pools_srv_count(struct shrinker *s,
1197                                           struct shrink_control *sc)
1198 {
1199         return ldlm_pools_count(LDLM_NAMESPACE_SERVER, sc->gfp_mask);
1200 }
1201
1202 static unsigned long ldlm_pools_srv_scan(struct shrinker *s,
1203                                          struct shrink_control *sc)
1204 {
1205         return ldlm_pools_scan(LDLM_NAMESPACE_SERVER, sc->nr_to_scan,
1206                                sc->gfp_mask);
1207 }
1208
1209 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
1210                                           struct shrink_control *sc)
1211 {
1212         return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1213 }
1214
1215 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1216                                          struct shrink_control *sc)
1217 {
1218         return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1219                                sc->gfp_mask);
1220 }
1221
1222 static struct ll_shrinker_ops ldlm_pools_srv_sh_ops = {
1223         .count_objects  = ldlm_pools_srv_count,
1224         .scan_objects   = ldlm_pools_srv_scan,
1225         .seeks          = DEFAULT_SEEKS,
1226 };
1227
1228 static struct ll_shrinker_ops ldlm_pools_cli_sh_ops = {
1229         .count_objects  = ldlm_pools_cli_count,
1230         .scan_objects   = ldlm_pools_cli_scan,
1231         .seeks          = DEFAULT_SEEKS,
1232 };
1233 #else
1234 /*
1235  * Cancel \a nr locks from all namespaces (if possible). Returns number of
1236  * cached locks after shrink is finished. All namespaces are asked to
1237  * cancel approximately equal amount of locks to keep balancing.
1238  */
1239 static int ldlm_pools_shrink(enum ldlm_side client, int nr, gfp_t gfp_mask)
1240 {
1241         unsigned long total = 0;
1242
1243         if (client == LDLM_NAMESPACE_CLIENT && nr != 0 &&
1244             !(gfp_mask & __GFP_FS))
1245                 return -1;
1246
1247         total = ldlm_pools_count(client, gfp_mask);
1248
1249         if (nr == 0 || total == 0)
1250                 return total;
1251
1252         return ldlm_pools_scan(client, nr, gfp_mask);
1253 }
1254
1255 static int ldlm_pools_srv_shrink(struct shrinker *shrinker,
1256                                  struct shrink_control *sc)
1257 {
1258         return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER,
1259                                  sc->nr_to_scan, sc->gfp_mask);
1260 }
1261
1262 static int ldlm_pools_cli_shrink(struct shrinker *shrinker,
1263                                  struct shrink_control *sc)
1264 {
1265         return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT,
1266                                  sc->nr_to_scan, sc->gfp_mask);
1267 }
1268
1269 static struct ll_shrinker_ops ldlm_pools_srv_sh_ops = {
1270         .shrink = ldlm_pools_srv_shrink,
1271         .seeks = DEFAULT_SEEKS,
1272 };
1273
1274 static struct ll_shrinker_ops ldlm_pools_cli_sh_ops = {
1275         .shrink = ldlm_pools_cli_shrink,
1276         .seeks = DEFAULT_SEEKS,
1277 };
1278 #endif /* HAVE_SHRINKER_COUNT */
1279
1280 static time64_t ldlm_pools_recalc_delay(enum ldlm_side side)
1281 {
1282         struct ldlm_namespace *ns;
1283         struct ldlm_namespace *ns_old = NULL;
1284         /* seconds of sleep if no active namespaces */
1285         time64_t delay = ktime_get_seconds() +
1286                          (side == LDLM_NAMESPACE_SERVER ?
1287                           LDLM_POOL_SRV_DEF_RECALC_PERIOD :
1288                           LDLM_POOL_CLI_DEF_RECALC_PERIOD);
1289         int nr;
1290
1291         /* Recalc at least ldlm_namespace_nr(side) namespaces. */
1292         for (nr = ldlm_namespace_nr_read(side); nr > 0; nr--) {
1293                 int skip;
1294                 /*
1295                  * Lock the list, get first @ns in the list, getref, move it
1296                  * to the tail, unlock and call pool recalc. This way we avoid
1297                  * calling recalc under @ns lock, which is really good as we
1298                  * get rid of potential deadlock on side nodes when canceling
1299                  * locks synchronously.
1300                  */
1301                 mutex_lock(ldlm_namespace_lock(side));
1302                 if (list_empty(ldlm_namespace_list(side))) {
1303                         mutex_unlock(ldlm_namespace_lock(side));
1304                         break;
1305                 }
1306                 ns = ldlm_namespace_first_locked(side);
1307
1308                 if (ns_old == ns) { /* Full pass complete */
1309                         mutex_unlock(ldlm_namespace_lock(side));
1310                         break;
1311                 }
1312
1313                 /* We got an empty namespace, need to move it back to inactive
1314                  * list.
1315                  * The race with parallel resource creation is fine:
1316                  * - If they do namespace_get before our check, we fail the
1317                  *   check and they move this item to the end of the list anyway
1318                  * - If we do the check and then they do namespace_get, then
1319                  *   we move the namespace to inactive and they will move
1320                  *   it back to active (synchronised by the lock, so no clash
1321                  *   there).
1322                  */
1323                 if (ldlm_ns_empty(ns)) {
1324                         ldlm_namespace_move_to_inactive_locked(ns, side);
1325                         mutex_unlock(ldlm_namespace_lock(side));
1326                         continue;
1327                 }
1328
1329                 if (ns_old == NULL)
1330                         ns_old = ns;
1331
1332                 spin_lock(&ns->ns_lock);
1333                 /*
1334                  * skip ns which is being freed, and we don't want to increase
1335                  * its refcount again, not even temporarily. bz21519 & LU-499.
1336                  */
1337                 if (ns->ns_stopping) {
1338                         skip = 1;
1339                 } else {
1340                         skip = 0;
1341                         ldlm_namespace_get(ns);
1342                 }
1343                 spin_unlock(&ns->ns_lock);
1344
1345                 ldlm_namespace_move_to_active_locked(ns, side);
1346                 mutex_unlock(ldlm_namespace_lock(side));
1347
1348                 /*
1349                  * After setup is done - recalc the pool.
1350                  */
1351                 if (!skip) {
1352                         delay = min(delay,
1353                                     ldlm_pool_recalc(&ns->ns_pool, false));
1354                         ldlm_namespace_put(ns);
1355                 }
1356         }
1357
1358         return delay;
1359 }
1360
1361 static void ldlm_pools_recalc_task(struct work_struct *ws);
1362 static DECLARE_DELAYED_WORK(ldlm_pools_recalc_work, ldlm_pools_recalc_task);
1363
1364 static void ldlm_pools_recalc_task(struct work_struct *ws)
1365 {
1366         /* seconds of sleep if no active namespaces */
1367         time64_t delay;
1368 #ifdef HAVE_SERVER_SUPPORT
1369         struct ldlm_namespace *ns;
1370         unsigned long nr_l = 0, nr_p = 0, l;
1371         int equal = 0;
1372
1373         /* Check all modest namespaces first. */
1374         mutex_lock(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
1375         list_for_each_entry(ns, ldlm_namespace_list(LDLM_NAMESPACE_SERVER),
1376                             ns_list_chain) {
1377                 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1378                         continue;
1379
1380                 l = ldlm_pool_granted(&ns->ns_pool);
1381                 if (l == 0)
1382                         l = 1;
1383
1384                 /*
1385                  * Set the modest pools limit equal to their avg granted
1386                  * locks + ~6%.
1387                  */
1388                 l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1389                 ldlm_pool_setup(&ns->ns_pool, l);
1390                 nr_l += l;
1391                 nr_p++;
1392         }
1393
1394         /*
1395          * Make sure than modest namespaces did not eat more that 2/3
1396          * of limit.
1397          */
1398         if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1399                 CWARN("'Modest' pools eat out 2/3 of server locks limit (%lu of %lu). This means that you have too many clients for this amount of server RAM. Upgrade server!\n",
1400                       nr_l, LDLM_POOL_HOST_L);
1401                 equal = 1;
1402         }
1403
1404         /* The rest is given to greedy namespaces. */
1405         list_for_each_entry(ns, ldlm_namespace_list(LDLM_NAMESPACE_SERVER),
1406                             ns_list_chain) {
1407                 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1408                         continue;
1409
1410                 if (equal) {
1411                         /*
1412                          * In the case 2/3 locks are eaten out by
1413                          * modest pools, we re-setup equal limit
1414                          * for _all_ pools.
1415                          */
1416                         l = LDLM_POOL_HOST_L /
1417                                 ldlm_namespace_nr_read(LDLM_NAMESPACE_SERVER);
1418                 } else {
1419                         /*
1420                          * All the rest of greedy pools will have
1421                          * all locks in equal parts.
1422                          */
1423                         l = (LDLM_POOL_HOST_L - nr_l) /
1424                                 (ldlm_namespace_nr_read(LDLM_NAMESPACE_SERVER) -
1425                                  nr_p);
1426                 }
1427                 ldlm_pool_setup(&ns->ns_pool, l);
1428         }
1429         mutex_unlock(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
1430
1431         delay = min(ldlm_pools_recalc_delay(LDLM_NAMESPACE_SERVER),
1432                     ldlm_pools_recalc_delay(LDLM_NAMESPACE_CLIENT));
1433 #else  /* !HAVE_SERVER_SUPPORT */
1434         delay = ldlm_pools_recalc_delay(LDLM_NAMESPACE_CLIENT);
1435 #endif /* HAVE_SERVER_SUPPORT */
1436
1437         /* Wake up the blocking threads from time to time. */
1438         ldlm_bl_thread_wakeup();
1439
1440         delay -= ktime_get_seconds();
1441         if (delay <= 0) {
1442                 /* Prevent too frequent recalculation. */
1443                 CDEBUG(D_DLMTRACE, "Negative interval(%lld)\n", delay);
1444                 delay = 1;
1445         }
1446
1447         schedule_delayed_work(&ldlm_pools_recalc_work, cfs_time_seconds(delay));
1448 }
1449
1450 static bool ldlm_pools_init_done;
1451
1452 static struct shrinker *ldlm_pools_srv_shrinker;
1453 static struct shrinker *ldlm_pools_cli_shrinker;
1454
1455 int ldlm_pools_init(void)
1456 {
1457         time64_t delay;
1458         int rc;
1459
1460         ENTRY;
1461
1462 #ifdef HAVE_SERVER_SUPPORT
1463         delay = min(LDLM_POOL_SRV_DEF_RECALC_PERIOD,
1464                     LDLM_POOL_CLI_DEF_RECALC_PERIOD);
1465 #else
1466         delay = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
1467 #endif
1468         ldlm_pools_srv_shrinker = ll_shrinker_create(&ldlm_pools_srv_sh_ops, 0,
1469                                                      "ldlm_pools_server");
1470         if (IS_ERR(ldlm_pools_srv_shrinker))
1471                 GOTO(out, rc = PTR_ERR(ldlm_pools_srv_shrinker));
1472
1473         ldlm_pools_cli_shrinker = ll_shrinker_create(&ldlm_pools_cli_sh_ops, 0,
1474                                                      "ldlm_pools_client");
1475         if (IS_ERR(ldlm_pools_cli_shrinker))
1476                 GOTO(out_shrinker, rc = PTR_ERR(ldlm_pools_cli_shrinker));
1477
1478         schedule_delayed_work(&ldlm_pools_recalc_work, delay);
1479         ldlm_pools_init_done = true;
1480         RETURN(0);
1481
1482 out_shrinker:
1483         shrinker_free(ldlm_pools_srv_shrinker);
1484 out:
1485         RETURN(rc);
1486 }
1487
1488 void ldlm_pools_fini(void)
1489 {
1490         if (ldlm_pools_init_done) {
1491                 cancel_delayed_work_sync(&ldlm_pools_recalc_work);
1492
1493                 shrinker_free(ldlm_pools_srv_shrinker);
1494                 shrinker_free(ldlm_pools_cli_shrinker);
1495         }
1496
1497         ldlm_pools_init_done = false;
1498 }
1499
1500 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1501 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
1502 {
1503         return 0;
1504 }
1505
1506 time64_t ldlm_pool_recalc(struct ldlm_pool *pl, bool force)
1507 {
1508         return 0;
1509 }
1510
1511 int ldlm_pool_shrink(struct ldlm_pool *pl,
1512                      int nr, gfp_t gfp_mask)
1513 {
1514         return 0;
1515 }
1516
1517 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1518                    int idx, enum ldlm_side client)
1519 {
1520         return 0;
1521 }
1522
1523 void ldlm_pool_fini(struct ldlm_pool *pl)
1524 {
1525 }
1526
1527 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1528 {
1529 }
1530
1531 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1532 {
1533 }
1534
1535 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1536 {
1537         return 1;
1538 }
1539
1540 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1541 {
1542 }
1543
1544 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1545 {
1546         return 1;
1547 }
1548
1549 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1550 {
1551 }
1552
1553 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1554 {
1555         return 0;
1556 }
1557
1558 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1559 {
1560 }
1561
1562 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1563 {
1564         return 0;
1565 }
1566
1567 int ldlm_pools_init(void)
1568 {
1569         return 0;
1570 }
1571
1572 void ldlm_pools_fini(void)
1573 {
1574 }
1575
1576 #endif /* HAVE_LRU_RESIZE_SUPPORT */