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