Merge tag 'v4.14-rc2' into next-general
[sfrench/cifs-2.6.git] / drivers / staging / lustre / 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, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ldlm/ldlm_pool.c
33  *
34  * Author: Yury Umanets <umka@clusterfs.com>
35  */
36
37 /*
38  * Idea of this code is rather simple. Each second, for each server namespace
39  * we have SLV - server lock volume which is calculated on current number of
40  * granted locks, grant speed for past period, etc - that is, locking load.
41  * This SLV number may be thought as a flow definition for simplicity. It is
42  * sent to clients with each occasion to let them know what is current load
43  * situation on the server. By default, at the beginning, SLV on server is
44  * set max value which is calculated as the following: allow to one client
45  * have all locks of limit ->pl_limit for 10h.
46  *
47  * Next, on clients, number of cached locks is not limited artificially in any
48  * way as it was before. Instead, client calculates CLV, that is, client lock
49  * volume for each lock and compares it with last SLV from the server. CLV is
50  * calculated as the number of locks in LRU * lock live time in seconds. If
51  * CLV > SLV - lock is canceled.
52  *
53  * Client has LVF, that is, lock volume factor which regulates how much
54  * sensitive client should be about last SLV from server. The higher LVF is the
55  * more locks will be canceled on client. Default value for it is 1. Setting LVF
56  * to 2 means that client will cancel locks 2 times faster.
57  *
58  * Locks on a client will be canceled more intensively in these cases:
59  * (1) if SLV is smaller, that is, load is higher on the server;
60  * (2) client has a lot of locks (the more locks are held by client, the bigger
61  *     chances that some of them should be canceled);
62  * (3) client has old locks (taken some time ago);
63  *
64  * Thus, according to flow paradigm that we use for better understanding SLV,
65  * CLV is the volume of particle in flow described by SLV. According to this,
66  * if flow is getting thinner, more and more particles become outside of it and
67  * as particles are locks, they should be canceled.
68  *
69  * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
70  * Andreas Dilger (adilger@clusterfs.com) proposed few nice ideas like using
71  * LVF and many cleanups. Flow definition to allow more easy understanding of
72  * the logic belongs to Nikita Danilov (nikita@clusterfs.com) as well as many
73  * cleanups and fixes. And design and implementation are done by Yury Umanets
74  * (umka@clusterfs.com).
75  *
76  * Glossary for terms used:
77  *
78  * pl_limit - Number of allowed locks in pool. Applies to server and client
79  * side (tunable);
80  *
81  * pl_granted - Number of granted locks (calculated);
82  * pl_grant_rate - Number of granted locks for last T (calculated);
83  * pl_cancel_rate - Number of canceled locks for last T (calculated);
84  * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
85  * pl_grant_plan - Planned number of granted locks for next T (calculated);
86  * pl_server_lock_volume - Current server lock volume (calculated);
87  *
88  * As it may be seen from list above, we have few possible tunables which may
89  * affect behavior much. They all may be modified via sysfs. However, they also
90  * give a possibility for constructing few pre-defined behavior policies. If
91  * none of predefines is suitable for a working pattern being used, new one may
92  * be "constructed" via sysfs tunables.
93  */
94
95 #define DEBUG_SUBSYSTEM S_LDLM
96
97 #include <lustre_dlm.h>
98 #include <cl_object.h>
99 #include <obd_class.h>
100 #include <obd_support.h>
101 #include "ldlm_internal.h"
102
103 /*
104  * 50 ldlm locks for 1MB of RAM.
105  */
106 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_SHIFT)) * 50)
107
108 /*
109  * Maximal possible grant step plan in %.
110  */
111 #define LDLM_POOL_MAX_GSP (30)
112
113 /*
114  * Minimal possible grant step plan in %.
115  */
116 #define LDLM_POOL_MIN_GSP (1)
117
118 /*
119  * This controls the speed of reaching LDLM_POOL_MAX_GSP
120  * with increasing thread period.
121  */
122 #define LDLM_POOL_GSP_STEP_SHIFT (2)
123
124 /*
125  * LDLM_POOL_GSP% of all locks is default GP.
126  */
127 #define LDLM_POOL_GP(L)   (((L) * LDLM_POOL_MAX_GSP) / 100)
128
129 /*
130  * Max age for locks on clients.
131  */
132 #define LDLM_POOL_MAX_AGE (36000)
133
134 /*
135  * The granularity of SLV calculation.
136  */
137 #define LDLM_POOL_SLV_SHIFT (10)
138
139 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
140 {
141         return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
142 }
143
144 static inline __u64 ldlm_pool_slv_max(__u32 L)
145 {
146         /*
147          * Allow to have all locks for 1 client for 10 hrs.
148          * Formula is the following: limit * 10h / 1 client.
149          */
150         __u64 lim = (__u64)L *  LDLM_POOL_MAX_AGE / 1;
151         return lim;
152 }
153
154 static inline __u64 ldlm_pool_slv_min(__u32 L)
155 {
156         return 1;
157 }
158
159 enum {
160         LDLM_POOL_FIRST_STAT = 0,
161         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
162         LDLM_POOL_GRANT_STAT,
163         LDLM_POOL_CANCEL_STAT,
164         LDLM_POOL_GRANT_RATE_STAT,
165         LDLM_POOL_CANCEL_RATE_STAT,
166         LDLM_POOL_GRANT_PLAN_STAT,
167         LDLM_POOL_SLV_STAT,
168         LDLM_POOL_SHRINK_REQTD_STAT,
169         LDLM_POOL_SHRINK_FREED_STAT,
170         LDLM_POOL_RECALC_STAT,
171         LDLM_POOL_TIMING_STAT,
172         LDLM_POOL_LAST_STAT
173 };
174
175 /**
176  * Calculates suggested grant_step in % of available locks for passed
177  * \a period. This is later used in grant_plan calculations.
178  */
179 static inline int ldlm_pool_t2gsp(unsigned int t)
180 {
181         /*
182          * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
183          * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
184          *
185          * How this will affect execution is the following:
186          *
187          * - for thread period 1s we will have grant_step 1% which good from
188          * pov of taking some load off from server and push it out to clients.
189          * This is like that because 1% for grant_step means that server will
190          * not allow clients to get lots of locks in short period of time and
191          * keep all old locks in their caches. Clients will always have to
192          * get some locks back if they want to take some new;
193          *
194          * - for thread period 10s (which is default) we will have 23% which
195          * means that clients will have enough of room to take some new locks
196          * without getting some back. All locks from this 23% which were not
197          * taken by clients in current period will contribute in SLV growing.
198          * SLV growing means more locks cached on clients until limit or grant
199          * plan is reached.
200          */
201         return LDLM_POOL_MAX_GSP -
202                 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
203                  (t >> LDLM_POOL_GSP_STEP_SHIFT));
204 }
205
206 /**
207  * Recalculates next stats on passed \a pl.
208  *
209  * \pre ->pl_lock is locked.
210  */
211 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
212 {
213         int grant_plan = pl->pl_grant_plan;
214         __u64 slv = pl->pl_server_lock_volume;
215         int granted = atomic_read(&pl->pl_granted);
216         int grant_rate = atomic_read(&pl->pl_grant_rate);
217         int cancel_rate = atomic_read(&pl->pl_cancel_rate);
218
219         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
220                             slv);
221         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
222                             granted);
223         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
224                             grant_rate);
225         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
226                             grant_plan);
227         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
228                             cancel_rate);
229 }
230
231 /**
232  * Sets SLV and Limit from container_of(pl, struct ldlm_namespace,
233  * ns_pool)->ns_obd tp passed \a pl.
234  */
235 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
236 {
237         struct obd_device *obd;
238
239         /*
240          * Get new SLV and Limit from obd which is updated with coming
241          * RPCs.
242          */
243         obd = container_of(pl, struct ldlm_namespace,
244                            ns_pool)->ns_obd;
245         read_lock(&obd->obd_pool_lock);
246         pl->pl_server_lock_volume = obd->obd_pool_slv;
247         atomic_set(&pl->pl_limit, obd->obd_pool_limit);
248         read_unlock(&obd->obd_pool_lock);
249 }
250
251 /**
252  * Recalculates client size pool \a pl according to current SLV and Limit.
253  */
254 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
255 {
256         time64_t recalc_interval_sec;
257         int ret;
258
259         recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
260         if (recalc_interval_sec < pl->pl_recalc_period)
261                 return 0;
262
263         spin_lock(&pl->pl_lock);
264         /*
265          * Check if we need to recalc lists now.
266          */
267         recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
268         if (recalc_interval_sec < pl->pl_recalc_period) {
269                 spin_unlock(&pl->pl_lock);
270                 return 0;
271         }
272
273         /*
274          * Make sure that pool knows last SLV and Limit from obd.
275          */
276         ldlm_cli_pool_pop_slv(pl);
277
278         spin_unlock(&pl->pl_lock);
279
280         /*
281          * Do not cancel locks in case lru resize is disabled for this ns.
282          */
283         if (!ns_connect_lru_resize(container_of(pl, struct ldlm_namespace,
284                                                 ns_pool))) {
285                 ret = 0;
286                 goto out;
287         }
288
289         /*
290          * In the time of canceling locks on client we do not need to maintain
291          * sharp timing, we only want to cancel locks asap according to new SLV.
292          * It may be called when SLV has changed much, this is why we do not
293          * take into account pl->pl_recalc_time here.
294          */
295         ret = ldlm_cancel_lru(container_of(pl, struct ldlm_namespace, ns_pool),
296                               0, LCF_ASYNC, LDLM_LRU_FLAG_LRUR);
297
298 out:
299         spin_lock(&pl->pl_lock);
300         /*
301          * Time of LRU resizing might be longer than period,
302          * so update after LRU resizing rather than before it.
303          */
304         pl->pl_recalc_time = ktime_get_real_seconds();
305         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
306                             recalc_interval_sec);
307         spin_unlock(&pl->pl_lock);
308         return ret;
309 }
310
311 /**
312  * This function is main entry point for memory pressure handling on client
313  * side.  Main goal of this function is to cancel some number of locks on
314  * passed \a pl according to \a nr and \a gfp_mask.
315  */
316 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
317                                 int nr, gfp_t gfp_mask)
318 {
319         struct ldlm_namespace *ns;
320         int unused;
321
322         ns = container_of(pl, struct ldlm_namespace, ns_pool);
323
324         /*
325          * Do not cancel locks in case lru resize is disabled for this ns.
326          */
327         if (!ns_connect_lru_resize(ns))
328                 return 0;
329
330         /*
331          * Make sure that pool knows last SLV and Limit from obd.
332          */
333         ldlm_cli_pool_pop_slv(pl);
334
335         spin_lock(&ns->ns_lock);
336         unused = ns->ns_nr_unused;
337         spin_unlock(&ns->ns_lock);
338
339         if (nr == 0)
340                 return (unused / 100) * sysctl_vfs_cache_pressure;
341         else
342                 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_LRU_FLAG_SHRINK);
343 }
344
345 static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
346         .po_recalc = ldlm_cli_pool_recalc,
347         .po_shrink = ldlm_cli_pool_shrink
348 };
349
350 /**
351  * Pool recalc wrapper. Will call either client or server pool recalc callback
352  * depending what pool \a pl is used.
353  */
354 static int ldlm_pool_recalc(struct ldlm_pool *pl)
355 {
356         u32 recalc_interval_sec;
357         int count;
358
359         recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
360         if (recalc_interval_sec > 0) {
361                 spin_lock(&pl->pl_lock);
362                 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
363
364                 if (recalc_interval_sec > 0) {
365                         /*
366                          * Update pool statistics every 1s.
367                          */
368                         ldlm_pool_recalc_stats(pl);
369
370                         /*
371                          * Zero out all rates and speed for the last period.
372                          */
373                         atomic_set(&pl->pl_grant_rate, 0);
374                         atomic_set(&pl->pl_cancel_rate, 0);
375                 }
376                 spin_unlock(&pl->pl_lock);
377         }
378
379         if (pl->pl_ops->po_recalc) {
380                 count = pl->pl_ops->po_recalc(pl);
381                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
382                                     count);
383         }
384
385         recalc_interval_sec = pl->pl_recalc_time - ktime_get_real_seconds() +
386                               pl->pl_recalc_period;
387         if (recalc_interval_sec <= 0) {
388                 /* DEBUG: should be re-removed after LU-4536 is fixed */
389                 CDEBUG(D_DLMTRACE, "%s: Negative interval(%ld), too short period(%ld)\n",
390                        pl->pl_name, (long)recalc_interval_sec,
391                        (long)pl->pl_recalc_period);
392
393                 /* Prevent too frequent recalculation. */
394                 recalc_interval_sec = 1;
395         }
396
397         return recalc_interval_sec;
398 }
399
400 /*
401  * Pool shrink wrapper. Will call either client or server pool recalc callback
402  * depending what pool pl is used. When nr == 0, just return the number of
403  * freeable locks. Otherwise, return the number of canceled locks.
404  */
405 static int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
406 {
407         int cancel = 0;
408
409         if (pl->pl_ops->po_shrink) {
410                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
411                 if (nr > 0) {
412                         lprocfs_counter_add(pl->pl_stats,
413                                             LDLM_POOL_SHRINK_REQTD_STAT,
414                                             nr);
415                         lprocfs_counter_add(pl->pl_stats,
416                                             LDLM_POOL_SHRINK_FREED_STAT,
417                                             cancel);
418                         CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, shrunk %d\n",
419                                pl->pl_name, nr, cancel);
420                 }
421         }
422         return cancel;
423 }
424
425 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
426 {
427         int granted, grant_rate, cancel_rate;
428         int grant_speed, lvf;
429         struct ldlm_pool *pl = m->private;
430         __u64 slv, clv;
431         __u32 limit;
432
433         spin_lock(&pl->pl_lock);
434         slv = pl->pl_server_lock_volume;
435         clv = pl->pl_client_lock_volume;
436         limit = atomic_read(&pl->pl_limit);
437         granted = atomic_read(&pl->pl_granted);
438         grant_rate = atomic_read(&pl->pl_grant_rate);
439         cancel_rate = atomic_read(&pl->pl_cancel_rate);
440         grant_speed = grant_rate - cancel_rate;
441         lvf = atomic_read(&pl->pl_lock_volume_factor);
442         spin_unlock(&pl->pl_lock);
443
444         seq_printf(m, "LDLM pool state (%s):\n"
445                       "  SLV: %llu\n"
446                       "  CLV: %llu\n"
447                       "  LVF: %d\n",
448                       pl->pl_name, slv, clv, lvf);
449
450         seq_printf(m, "  GR:  %d\n  CR:  %d\n  GS:  %d\n"
451                       "  G:   %d\n  L:   %d\n",
452                       grant_rate, cancel_rate, grant_speed,
453                       granted, limit);
454
455         return 0;
456 }
457
458 LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
459
460 static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
461                                 char *buf)
462 {
463         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
464                                             pl_kobj);
465
466         int            grant_speed;
467
468         spin_lock(&pl->pl_lock);
469         /* serialize with ldlm_pool_recalc */
470         grant_speed = atomic_read(&pl->pl_grant_rate) -
471                         atomic_read(&pl->pl_cancel_rate);
472         spin_unlock(&pl->pl_lock);
473         return sprintf(buf, "%d\n", grant_speed);
474 }
475 LUSTRE_RO_ATTR(grant_speed);
476
477 LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
478 LUSTRE_RO_ATTR(grant_plan);
479
480 LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
481 LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
482 LUSTRE_RW_ATTR(recalc_period);
483
484 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
485 LUSTRE_RO_ATTR(server_lock_volume);
486
487 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
488 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
489 LUSTRE_RW_ATTR(limit);
490
491 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
492 LUSTRE_RO_ATTR(granted);
493
494 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
495 LUSTRE_RO_ATTR(cancel_rate);
496
497 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
498 LUSTRE_RO_ATTR(grant_rate);
499
500 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(lock_volume_factor, atomic);
501 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(lock_volume_factor, atomic);
502 LUSTRE_RW_ATTR(lock_volume_factor);
503
504 #define LDLM_POOL_ADD_VAR(name, var, ops)                       \
505         do {                                                    \
506                 snprintf(var_name, MAX_STRING_SIZE, #name);     \
507                 pool_vars[0].data = var;                        \
508                 pool_vars[0].fops = ops;                        \
509                 ldebugfs_add_vars(pl->pl_debugfs_entry, pool_vars, NULL);\
510         } while (0)
511
512 /* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
513 static struct attribute *ldlm_pl_attrs[] = {
514         &lustre_attr_grant_speed.attr,
515         &lustre_attr_grant_plan.attr,
516         &lustre_attr_recalc_period.attr,
517         &lustre_attr_server_lock_volume.attr,
518         &lustre_attr_limit.attr,
519         &lustre_attr_granted.attr,
520         &lustre_attr_cancel_rate.attr,
521         &lustre_attr_grant_rate.attr,
522         &lustre_attr_lock_volume_factor.attr,
523         NULL,
524 };
525
526 static void ldlm_pl_release(struct kobject *kobj)
527 {
528         struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
529                                             pl_kobj);
530         complete(&pl->pl_kobj_unregister);
531 }
532
533 static struct kobj_type ldlm_pl_ktype = {
534         .default_attrs  = ldlm_pl_attrs,
535         .sysfs_ops      = &lustre_sysfs_ops,
536         .release        = ldlm_pl_release,
537 };
538
539 static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
540 {
541         struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
542                                                  ns_pool);
543         int err;
544
545         init_completion(&pl->pl_kobj_unregister);
546         err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
547                                    "pool");
548
549         return err;
550 }
551
552 static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
553 {
554         struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
555                                                  ns_pool);
556         struct dentry *debugfs_ns_parent;
557         struct lprocfs_vars pool_vars[2];
558         char *var_name = NULL;
559         int rc = 0;
560
561         var_name = kzalloc(MAX_STRING_SIZE + 1, GFP_NOFS);
562         if (!var_name)
563                 return -ENOMEM;
564
565         debugfs_ns_parent = ns->ns_debugfs_entry;
566         if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
567                 CERROR("%s: debugfs entry is not initialized\n",
568                        ldlm_ns_name(ns));
569                 rc = -EINVAL;
570                 goto out_free_name;
571         }
572         pl->pl_debugfs_entry = ldebugfs_register("pool", debugfs_ns_parent,
573                                                  NULL, NULL);
574         if (IS_ERR(pl->pl_debugfs_entry)) {
575                 CERROR("LdebugFS failed in ldlm-pool-init\n");
576                 rc = PTR_ERR(pl->pl_debugfs_entry);
577                 pl->pl_debugfs_entry = NULL;
578                 goto out_free_name;
579         }
580
581         var_name[MAX_STRING_SIZE] = '\0';
582         memset(pool_vars, 0, sizeof(pool_vars));
583         pool_vars[0].name = var_name;
584
585         LDLM_POOL_ADD_VAR(state, pl, &lprocfs_pool_state_fops);
586
587         pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
588                                            LDLM_POOL_FIRST_STAT, 0);
589         if (!pl->pl_stats) {
590                 rc = -ENOMEM;
591                 goto out_free_name;
592         }
593
594         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
595                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
596                              "granted", "locks");
597         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
598                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
599                              "grant", "locks");
600         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
601                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
602                              "cancel", "locks");
603         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
604                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
605                              "grant_rate", "locks/s");
606         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
607                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
608                              "cancel_rate", "locks/s");
609         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
610                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
611                              "grant_plan", "locks/s");
612         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
613                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
614                              "slv", "slv");
615         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
616                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
617                              "shrink_request", "locks");
618         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
619                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
620                              "shrink_freed", "locks");
621         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
622                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
623                              "recalc_freed", "locks");
624         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
625                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
626                              "recalc_timing", "sec");
627         rc = ldebugfs_register_stats(pl->pl_debugfs_entry, "stats",
628                                      pl->pl_stats);
629
630 out_free_name:
631         kfree(var_name);
632         return rc;
633 }
634
635 static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
636 {
637         kobject_put(&pl->pl_kobj);
638         wait_for_completion(&pl->pl_kobj_unregister);
639 }
640
641 static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
642 {
643         if (pl->pl_stats) {
644                 lprocfs_free_stats(&pl->pl_stats);
645                 pl->pl_stats = NULL;
646         }
647         if (pl->pl_debugfs_entry) {
648                 ldebugfs_remove(&pl->pl_debugfs_entry);
649                 pl->pl_debugfs_entry = NULL;
650         }
651 }
652
653 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
654                    int idx, enum ldlm_side client)
655 {
656         int rc;
657
658         spin_lock_init(&pl->pl_lock);
659         atomic_set(&pl->pl_granted, 0);
660         pl->pl_recalc_time = ktime_get_real_seconds();
661         atomic_set(&pl->pl_lock_volume_factor, 1);
662
663         atomic_set(&pl->pl_grant_rate, 0);
664         atomic_set(&pl->pl_cancel_rate, 0);
665         pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
666
667         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
668                  ldlm_ns_name(ns), idx);
669
670         atomic_set(&pl->pl_limit, 1);
671         pl->pl_server_lock_volume = 0;
672         pl->pl_ops = &ldlm_cli_pool_ops;
673         pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
674         pl->pl_client_lock_volume = 0;
675         rc = ldlm_pool_debugfs_init(pl);
676         if (rc)
677                 return rc;
678
679         rc = ldlm_pool_sysfs_init(pl);
680         if (rc)
681                 return rc;
682
683         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
684
685         return rc;
686 }
687
688 void ldlm_pool_fini(struct ldlm_pool *pl)
689 {
690         ldlm_pool_sysfs_fini(pl);
691         ldlm_pool_debugfs_fini(pl);
692
693         /*
694          * Pool should not be used after this point. We can't free it here as
695          * it lives in struct ldlm_namespace, but still interested in catching
696          * any abnormal using cases.
697          */
698         POISON(pl, 0x5a, sizeof(*pl));
699 }
700
701 /**
702  * Add new taken ldlm lock \a lock into pool \a pl accounting.
703  */
704 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
705 {
706         /*
707          * FLOCK locks are special in a sense that they are almost never
708          * cancelled, instead special kind of lock is used to drop them.
709          * also there is no LRU for flock locks, so no point in tracking
710          * them anyway.
711          */
712         if (lock->l_resource->lr_type == LDLM_FLOCK)
713                 return;
714
715         atomic_inc(&pl->pl_granted);
716         atomic_inc(&pl->pl_grant_rate);
717         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
718         /*
719          * Do not do pool recalc for client side as all locks which
720          * potentially may be canceled has already been packed into
721          * enqueue/cancel rpc. Also we do not want to run out of stack
722          * with too long call paths.
723          */
724 }
725
726 /**
727  * Remove ldlm lock \a lock from pool \a pl accounting.
728  */
729 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
730 {
731         /*
732          * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
733          */
734         if (lock->l_resource->lr_type == LDLM_FLOCK)
735                 return;
736
737         LASSERT(atomic_read(&pl->pl_granted) > 0);
738         atomic_dec(&pl->pl_granted);
739         atomic_inc(&pl->pl_cancel_rate);
740
741         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
742 }
743
744 /**
745  * Returns current \a pl SLV.
746  *
747  * \pre ->pl_lock is not locked.
748  */
749 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
750 {
751         __u64 slv;
752
753         spin_lock(&pl->pl_lock);
754         slv = pl->pl_server_lock_volume;
755         spin_unlock(&pl->pl_lock);
756         return slv;
757 }
758
759 /**
760  * Sets passed \a clv to \a pl.
761  *
762  * \pre ->pl_lock is not locked.
763  */
764 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
765 {
766         spin_lock(&pl->pl_lock);
767         pl->pl_client_lock_volume = clv;
768         spin_unlock(&pl->pl_lock);
769 }
770
771 /**
772  * Returns current LVF from \a pl.
773  */
774 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
775 {
776         return atomic_read(&pl->pl_lock_volume_factor);
777 }
778
779 static int ldlm_pool_granted(struct ldlm_pool *pl)
780 {
781         return atomic_read(&pl->pl_granted);
782 }
783
784 static struct ptlrpc_thread *ldlm_pools_thread;
785 static struct completion ldlm_pools_comp;
786
787 /*
788  * count locks from all namespaces (if possible). Returns number of
789  * cached locks.
790  */
791 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
792 {
793         unsigned long total = 0;
794         int nr_ns;
795         struct ldlm_namespace *ns;
796         struct ldlm_namespace *ns_old = NULL; /* loop detection */
797
798         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
799                 return 0;
800
801         CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
802                client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
803
804         /*
805          * Find out how many resources we may release.
806          */
807         for (nr_ns = ldlm_namespace_nr_read(client);
808              nr_ns > 0; nr_ns--) {
809                 mutex_lock(ldlm_namespace_lock(client));
810                 if (list_empty(ldlm_namespace_list(client))) {
811                         mutex_unlock(ldlm_namespace_lock(client));
812                         return 0;
813                 }
814                 ns = ldlm_namespace_first_locked(client);
815
816                 if (ns == ns_old) {
817                         mutex_unlock(ldlm_namespace_lock(client));
818                         break;
819                 }
820
821                 if (ldlm_ns_empty(ns)) {
822                         ldlm_namespace_move_to_inactive_locked(ns, client);
823                         mutex_unlock(ldlm_namespace_lock(client));
824                         continue;
825                 }
826
827                 if (!ns_old)
828                         ns_old = ns;
829
830                 ldlm_namespace_get(ns);
831                 ldlm_namespace_move_to_active_locked(ns, client);
832                 mutex_unlock(ldlm_namespace_lock(client));
833                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
834                 ldlm_namespace_put(ns);
835         }
836
837         return total;
838 }
839
840 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
841                                      gfp_t gfp_mask)
842 {
843         unsigned long freed = 0;
844         int tmp, nr_ns;
845         struct ldlm_namespace *ns;
846
847         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
848                 return -1;
849
850         /*
851          * Shrink at least ldlm_namespace_nr_read(client) namespaces.
852          */
853         for (tmp = nr_ns = ldlm_namespace_nr_read(client);
854              tmp > 0; tmp--) {
855                 int cancel, nr_locks;
856
857                 /*
858                  * Do not call shrink under ldlm_namespace_lock(client)
859                  */
860                 mutex_lock(ldlm_namespace_lock(client));
861                 if (list_empty(ldlm_namespace_list(client))) {
862                         mutex_unlock(ldlm_namespace_lock(client));
863                         break;
864                 }
865                 ns = ldlm_namespace_first_locked(client);
866                 ldlm_namespace_get(ns);
867                 ldlm_namespace_move_to_active_locked(ns, client);
868                 mutex_unlock(ldlm_namespace_lock(client));
869
870                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
871                 /*
872                  * We use to shrink propotionally but with new shrinker API,
873                  * we lost the total number of freeable locks.
874                  */
875                 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
876                 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
877                 ldlm_namespace_put(ns);
878         }
879         /*
880          * we only decrease the SLV in server pools shrinker, return
881          * SHRINK_STOP to kernel to avoid needless loop. LU-1128
882          */
883         return freed;
884 }
885
886 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
887                                           struct shrink_control *sc)
888 {
889         return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
890 }
891
892 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
893                                          struct shrink_control *sc)
894 {
895         return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
896                                sc->gfp_mask);
897 }
898
899 static int ldlm_pools_recalc(enum ldlm_side client)
900 {
901         struct ldlm_namespace *ns;
902         struct ldlm_namespace *ns_old = NULL;
903         /* seconds of sleep if no active namespaces */
904         int time = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
905         int nr;
906
907         /*
908          * Recalc at least ldlm_namespace_nr_read(client) namespaces.
909          */
910         for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
911                 int     skip;
912                 /*
913                  * Lock the list, get first @ns in the list, getref, move it
914                  * to the tail, unlock and call pool recalc. This way we avoid
915                  * calling recalc under @ns lock what is really good as we get
916                  * rid of potential deadlock on client nodes when canceling
917                  * locks synchronously.
918                  */
919                 mutex_lock(ldlm_namespace_lock(client));
920                 if (list_empty(ldlm_namespace_list(client))) {
921                         mutex_unlock(ldlm_namespace_lock(client));
922                         break;
923                 }
924                 ns = ldlm_namespace_first_locked(client);
925
926                 if (ns_old == ns) { /* Full pass complete */
927                         mutex_unlock(ldlm_namespace_lock(client));
928                         break;
929                 }
930
931                 /* We got an empty namespace, need to move it back to inactive
932                  * list.
933                  * The race with parallel resource creation is fine:
934                  * - If they do namespace_get before our check, we fail the
935                  *   check and they move this item to the end of the list anyway
936                  * - If we do the check and then they do namespace_get, then
937                  *   we move the namespace to inactive and they will move
938                  *   it back to active (synchronised by the lock, so no clash
939                  *   there).
940                  */
941                 if (ldlm_ns_empty(ns)) {
942                         ldlm_namespace_move_to_inactive_locked(ns, client);
943                         mutex_unlock(ldlm_namespace_lock(client));
944                         continue;
945                 }
946
947                 if (!ns_old)
948                         ns_old = ns;
949
950                 spin_lock(&ns->ns_lock);
951                 /*
952                  * skip ns which is being freed, and we don't want to increase
953                  * its refcount again, not even temporarily. bz21519 & LU-499.
954                  */
955                 if (ns->ns_stopping) {
956                         skip = 1;
957                 } else {
958                         skip = 0;
959                         ldlm_namespace_get(ns);
960                 }
961                 spin_unlock(&ns->ns_lock);
962
963                 ldlm_namespace_move_to_active_locked(ns, client);
964                 mutex_unlock(ldlm_namespace_lock(client));
965
966                 /*
967                  * After setup is done - recalc the pool.
968                  */
969                 if (!skip) {
970                         int ttime = ldlm_pool_recalc(&ns->ns_pool);
971
972                         if (ttime < time)
973                                 time = ttime;
974
975                         ldlm_namespace_put(ns);
976                 }
977         }
978
979         /* Wake up the blocking threads from time to time. */
980         ldlm_bl_thread_wakeup();
981
982         return time;
983 }
984
985 static int ldlm_pools_thread_main(void *arg)
986 {
987         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
988         int c_time;
989
990         thread_set_flags(thread, SVC_RUNNING);
991         wake_up(&thread->t_ctl_waitq);
992
993         CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
994                "ldlm_poold", current_pid());
995
996         while (1) {
997                 struct l_wait_info lwi;
998
999                 /*
1000                  * Recal all pools on this tick.
1001                  */
1002                 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1003
1004                 /*
1005                  * Wait until the next check time, or until we're
1006                  * stopped.
1007                  */
1008                 lwi = LWI_TIMEOUT(cfs_time_seconds(c_time),
1009                                   NULL, NULL);
1010                 l_wait_event(thread->t_ctl_waitq,
1011                              thread_is_stopping(thread) ||
1012                              thread_is_event(thread),
1013                              &lwi);
1014
1015                 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1016                         break;
1017                 thread_test_and_clear_flags(thread, SVC_EVENT);
1018         }
1019
1020         thread_set_flags(thread, SVC_STOPPED);
1021         wake_up(&thread->t_ctl_waitq);
1022
1023         CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1024                "ldlm_poold", current_pid());
1025
1026         complete_and_exit(&ldlm_pools_comp, 0);
1027 }
1028
1029 static int ldlm_pools_thread_start(void)
1030 {
1031         struct l_wait_info lwi = { 0 };
1032         struct task_struct *task;
1033
1034         if (ldlm_pools_thread)
1035                 return -EALREADY;
1036
1037         ldlm_pools_thread = kzalloc(sizeof(*ldlm_pools_thread), GFP_NOFS);
1038         if (!ldlm_pools_thread)
1039                 return -ENOMEM;
1040
1041         init_completion(&ldlm_pools_comp);
1042         init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1043
1044         task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1045                            "ldlm_poold");
1046         if (IS_ERR(task)) {
1047                 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
1048                 kfree(ldlm_pools_thread);
1049                 ldlm_pools_thread = NULL;
1050                 return PTR_ERR(task);
1051         }
1052         l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1053                      thread_is_running(ldlm_pools_thread), &lwi);
1054         return 0;
1055 }
1056
1057 static void ldlm_pools_thread_stop(void)
1058 {
1059         if (!ldlm_pools_thread)
1060                 return;
1061
1062         thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1063         wake_up(&ldlm_pools_thread->t_ctl_waitq);
1064
1065         /*
1066          * Make sure that pools thread is finished before freeing @thread.
1067          * This fixes possible race and oops due to accessing freed memory
1068          * in pools thread.
1069          */
1070         wait_for_completion(&ldlm_pools_comp);
1071         kfree(ldlm_pools_thread);
1072         ldlm_pools_thread = NULL;
1073 }
1074
1075 static struct shrinker ldlm_pools_cli_shrinker = {
1076         .count_objects  = ldlm_pools_cli_count,
1077         .scan_objects   = ldlm_pools_cli_scan,
1078         .seeks          = DEFAULT_SEEKS,
1079 };
1080
1081 int ldlm_pools_init(void)
1082 {
1083         int rc;
1084
1085         rc = ldlm_pools_thread_start();
1086         if (rc == 0)
1087                 register_shrinker(&ldlm_pools_cli_shrinker);
1088
1089         return rc;
1090 }
1091
1092 void ldlm_pools_fini(void)
1093 {
1094         if (ldlm_pools_thread)
1095                 unregister_shrinker(&ldlm_pools_cli_shrinker);
1096
1097         ldlm_pools_thread_stop();
1098 }