1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2010, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/ldlm/ldlm_pool.c
35 * Author: Yury Umanets <umka@clusterfs.com>
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.
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.
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.
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);
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.
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).
77 * Glossary for terms used:
79 * pl_limit - Number of allowed locks in pool. Applies to server and client
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);
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.
96 #define DEBUG_SUBSYSTEM S_LDLM
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"
105 * 50 ldlm locks for 1MB of RAM.
107 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_SHIFT)) * 50)
110 * Maximal possible grant step plan in %.
112 #define LDLM_POOL_MAX_GSP (30)
115 * Minimal possible grant step plan in %.
117 #define LDLM_POOL_MIN_GSP (1)
120 * This controls the speed of reaching LDLM_POOL_MAX_GSP
121 * with increasing thread period.
123 #define LDLM_POOL_GSP_STEP_SHIFT (2)
126 * LDLM_POOL_GSP% of all locks is default GP.
128 #define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
131 * Max age for locks on clients.
133 #define LDLM_POOL_MAX_AGE (36000)
136 * The granularity of SLV calculation.
138 #define LDLM_POOL_SLV_SHIFT (10)
140 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
142 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
145 static inline __u64 ldlm_pool_slv_max(__u32 L)
148 * Allow to have all locks for 1 client for 10 hrs.
149 * Formula is the following: limit * 10h / 1 client.
151 __u64 lim = (__u64)L * LDLM_POOL_MAX_AGE / 1;
155 static inline __u64 ldlm_pool_slv_min(__u32 L)
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,
169 LDLM_POOL_SHRINK_REQTD_STAT,
170 LDLM_POOL_SHRINK_FREED_STAT,
171 LDLM_POOL_RECALC_STAT,
172 LDLM_POOL_TIMING_STAT,
177 * Calculates suggested grant_step in % of available locks for passed
178 * \a period. This is later used in grant_plan calculations.
180 static inline int ldlm_pool_t2gsp(unsigned int t)
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.
186 * How this will affect execution is the following:
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;
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
202 return LDLM_POOL_MAX_GSP -
203 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
204 (t >> LDLM_POOL_GSP_STEP_SHIFT));
208 * Recalculates next stats on passed \a pl.
210 * \pre ->pl_lock is locked.
212 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
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);
220 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
222 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
224 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
226 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
228 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
233 * Sets SLV and Limit from container_of(pl, struct ldlm_namespace,
234 * ns_pool)->ns_obd tp passed \a pl.
236 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
238 struct obd_device *obd;
241 * Get new SLV and Limit from obd which is updated with coming
244 obd = container_of(pl, struct ldlm_namespace,
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);
253 * Recalculates client size pool \a pl according to current SLV and Limit.
255 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
257 time64_t recalc_interval_sec;
260 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
261 if (recalc_interval_sec < pl->pl_recalc_period)
264 spin_lock(&pl->pl_lock);
266 * Check if we need to recalc lists now.
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);
275 * Make sure that pool knows last SLV and Limit from obd.
277 ldlm_cli_pool_pop_slv(pl);
279 spin_unlock(&pl->pl_lock);
282 * Do not cancel locks in case lru resize is disabled for this ns.
284 if (!ns_connect_lru_resize(container_of(pl, struct ldlm_namespace,
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.
296 ret = ldlm_cancel_lru(container_of(pl, struct ldlm_namespace, ns_pool),
297 0, LCF_ASYNC, LDLM_LRU_FLAG_LRUR);
300 spin_lock(&pl->pl_lock);
302 * Time of LRU resizing might be longer than period,
303 * so update after LRU resizing rather than before it.
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);
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.
317 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
318 int nr, gfp_t gfp_mask)
320 struct ldlm_namespace *ns;
323 ns = container_of(pl, struct ldlm_namespace, ns_pool);
326 * Do not cancel locks in case lru resize is disabled for this ns.
328 if (!ns_connect_lru_resize(ns))
332 * Make sure that pool knows last SLV and Limit from obd.
334 ldlm_cli_pool_pop_slv(pl);
336 spin_lock(&ns->ns_lock);
337 unused = ns->ns_nr_unused;
338 spin_unlock(&ns->ns_lock);
341 return (unused / 100) * sysctl_vfs_cache_pressure;
343 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_LRU_FLAG_SHRINK);
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
352 * Pool recalc wrapper. Will call either client or server pool recalc callback
353 * depending what pool \a pl is used.
355 static int ldlm_pool_recalc(struct ldlm_pool *pl)
357 u32 recalc_interval_sec;
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;
365 if (recalc_interval_sec > 0) {
367 * Update pool statistics every 1s.
369 ldlm_pool_recalc_stats(pl);
372 * Zero out all rates and speed for the last period.
374 atomic_set(&pl->pl_grant_rate, 0);
375 atomic_set(&pl->pl_cancel_rate, 0);
377 spin_unlock(&pl->pl_lock);
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,
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 */
391 "%s: Negative interval(%ld), too short period(%ld)\n",
392 pl->pl_name, (long)recalc_interval_sec,
393 (long)pl->pl_recalc_period);
395 /* Prevent too frequent recalculation. */
396 recalc_interval_sec = 1;
399 return recalc_interval_sec;
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.
407 static int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
411 if (pl->pl_ops->po_shrink) {
412 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
414 lprocfs_counter_add(pl->pl_stats,
415 LDLM_POOL_SHRINK_REQTD_STAT,
417 lprocfs_counter_add(pl->pl_stats,
418 LDLM_POOL_SHRINK_FREED_STAT,
421 "%s: request to shrink %d locks, shrunk %d\n",
422 pl->pl_name, nr, cancel);
428 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
430 int granted, grant_rate, cancel_rate;
431 int grant_speed, lvf;
432 struct ldlm_pool *pl = m->private;
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);
447 seq_printf(m, "LDLM pool state (%s):\n"
451 pl->pl_name, slv, clv, lvf);
453 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
455 grant_rate, cancel_rate, grant_speed,
461 LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
463 static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
466 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
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);
478 LUSTRE_RO_ATTR(grant_speed);
480 LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
481 LUSTRE_RO_ATTR(grant_plan);
483 LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
484 LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
485 LUSTRE_RW_ATTR(recalc_period);
487 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
488 LUSTRE_RO_ATTR(server_lock_volume);
490 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
491 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
492 LUSTRE_RW_ATTR(limit);
494 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
495 LUSTRE_RO_ATTR(granted);
497 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
498 LUSTRE_RO_ATTR(cancel_rate);
500 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
501 LUSTRE_RO_ATTR(grant_rate);
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);
507 #define LDLM_POOL_ADD_VAR(name, var, ops) \
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);\
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,
529 static void ldlm_pl_release(struct kobject *kobj)
531 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
533 complete(&pl->pl_kobj_unregister);
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,
542 static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
544 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
548 init_completion(&pl->pl_kobj_unregister);
549 err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
555 static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
557 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
559 struct dentry *debugfs_ns_parent;
560 struct lprocfs_vars pool_vars[2];
561 char *var_name = NULL;
564 var_name = kzalloc(MAX_STRING_SIZE + 1, GFP_NOFS);
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",
575 pl->pl_debugfs_entry = ldebugfs_register("pool", debugfs_ns_parent,
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;
584 var_name[MAX_STRING_SIZE] = '\0';
585 memset(pool_vars, 0, sizeof(pool_vars));
586 pool_vars[0].name = var_name;
588 LDLM_POOL_ADD_VAR(state, pl, &lprocfs_pool_state_fops);
590 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
591 LDLM_POOL_FIRST_STAT, 0);
597 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
598 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
600 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
601 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
603 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
604 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
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,
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",
638 static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
640 kobject_put(&pl->pl_kobj);
641 wait_for_completion(&pl->pl_kobj_unregister);
644 static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
647 lprocfs_free_stats(&pl->pl_stats);
650 if (pl->pl_debugfs_entry) {
651 ldebugfs_remove(&pl->pl_debugfs_entry);
652 pl->pl_debugfs_entry = NULL;
656 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
657 int idx, enum ldlm_side client)
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);
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);
670 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
671 ldlm_ns_name(ns), idx);
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);
682 rc = ldlm_pool_sysfs_init(pl);
686 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
691 void ldlm_pool_fini(struct ldlm_pool *pl)
693 ldlm_pool_sysfs_fini(pl);
694 ldlm_pool_debugfs_fini(pl);
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.
701 POISON(pl, 0x5a, sizeof(*pl));
705 * Add new taken ldlm lock \a lock into pool \a pl accounting.
707 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
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
715 if (lock->l_resource->lr_type == LDLM_FLOCK)
718 atomic_inc(&pl->pl_granted);
719 atomic_inc(&pl->pl_grant_rate);
720 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
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.
730 * Remove ldlm lock \a lock from pool \a pl accounting.
732 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
735 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
737 if (lock->l_resource->lr_type == LDLM_FLOCK)
740 LASSERT(atomic_read(&pl->pl_granted) > 0);
741 atomic_dec(&pl->pl_granted);
742 atomic_inc(&pl->pl_cancel_rate);
744 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
748 * Returns current \a pl SLV.
750 * \pre ->pl_lock is not locked.
752 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
756 spin_lock(&pl->pl_lock);
757 slv = pl->pl_server_lock_volume;
758 spin_unlock(&pl->pl_lock);
763 * Sets passed \a clv to \a pl.
765 * \pre ->pl_lock is not locked.
767 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
769 spin_lock(&pl->pl_lock);
770 pl->pl_client_lock_volume = clv;
771 spin_unlock(&pl->pl_lock);
775 * Returns current LVF from \a pl.
777 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
779 return atomic_read(&pl->pl_lock_volume_factor);
782 static int ldlm_pool_granted(struct ldlm_pool *pl)
784 return atomic_read(&pl->pl_granted);
787 static struct ptlrpc_thread *ldlm_pools_thread;
788 static struct completion ldlm_pools_comp;
791 * count locks from all namespaces (if possible). Returns number of
794 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
796 unsigned long total = 0;
798 struct ldlm_namespace *ns;
799 struct ldlm_namespace *ns_old = NULL; /* loop detection */
801 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
804 CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
805 client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
808 * Find out how many resources we may release.
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));
817 ns = ldlm_namespace_first_locked(client);
820 mutex_unlock(ldlm_namespace_lock(client));
824 if (ldlm_ns_empty(ns)) {
825 ldlm_namespace_move_to_inactive_locked(ns, client);
826 mutex_unlock(ldlm_namespace_lock(client));
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);
843 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
846 unsigned long freed = 0;
848 struct ldlm_namespace *ns;
850 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
854 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
856 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
858 int cancel, nr_locks;
861 * Do not call shrink under ldlm_namespace_lock(client)
863 mutex_lock(ldlm_namespace_lock(client));
864 if (list_empty(ldlm_namespace_list(client))) {
865 mutex_unlock(ldlm_namespace_lock(client));
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));
873 nr_locks = ldlm_pool_granted(&ns->ns_pool);
875 * We use to shrink propotionally but with new shrinker API,
876 * we lost the total number of freeable locks.
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);
883 * we only decrease the SLV in server pools shrinker, return
884 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
889 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
890 struct shrink_control *sc)
892 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
895 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
896 struct shrink_control *sc)
898 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
902 static int ldlm_pools_recalc(enum ldlm_side client)
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;
911 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
913 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
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.
922 mutex_lock(ldlm_namespace_lock(client));
923 if (list_empty(ldlm_namespace_list(client))) {
924 mutex_unlock(ldlm_namespace_lock(client));
927 ns = ldlm_namespace_first_locked(client);
929 if (ns_old == ns) { /* Full pass complete */
930 mutex_unlock(ldlm_namespace_lock(client));
934 /* We got an empty namespace, need to move it back to inactive
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
944 if (ldlm_ns_empty(ns)) {
945 ldlm_namespace_move_to_inactive_locked(ns, client);
946 mutex_unlock(ldlm_namespace_lock(client));
953 spin_lock(&ns->ns_lock);
955 * skip ns which is being freed, and we don't want to increase
956 * its refcount again, not even temporarily. bz21519 & LU-499.
958 if (ns->ns_stopping) {
962 ldlm_namespace_get(ns);
964 spin_unlock(&ns->ns_lock);
966 ldlm_namespace_move_to_active_locked(ns, client);
967 mutex_unlock(ldlm_namespace_lock(client));
970 * After setup is done - recalc the pool.
973 int ttime = ldlm_pool_recalc(&ns->ns_pool);
978 ldlm_namespace_put(ns);
982 /* Wake up the blocking threads from time to time. */
983 ldlm_bl_thread_wakeup();
988 static int ldlm_pools_thread_main(void *arg)
990 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
993 thread_set_flags(thread, SVC_RUNNING);
994 wake_up(&thread->t_ctl_waitq);
996 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
997 "ldlm_poold", current_pid());
1000 struct l_wait_info lwi;
1003 * Recal all pools on this tick.
1005 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1008 * Wait until the next check time, or until we're
1011 lwi = LWI_TIMEOUT(cfs_time_seconds(c_time),
1013 l_wait_event(thread->t_ctl_waitq,
1014 thread_is_stopping(thread) ||
1015 thread_is_event(thread),
1018 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1020 thread_test_and_clear_flags(thread, SVC_EVENT);
1023 thread_set_flags(thread, SVC_STOPPED);
1024 wake_up(&thread->t_ctl_waitq);
1026 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1027 "ldlm_poold", current_pid());
1029 complete_and_exit(&ldlm_pools_comp, 0);
1032 static int ldlm_pools_thread_start(void)
1034 struct l_wait_info lwi = { 0 };
1035 struct task_struct *task;
1037 if (ldlm_pools_thread)
1040 ldlm_pools_thread = kzalloc(sizeof(*ldlm_pools_thread), GFP_NOFS);
1041 if (!ldlm_pools_thread)
1044 init_completion(&ldlm_pools_comp);
1045 init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1047 task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
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);
1055 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1056 thread_is_running(ldlm_pools_thread), &lwi);
1060 static void ldlm_pools_thread_stop(void)
1062 if (!ldlm_pools_thread)
1065 thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1066 wake_up(&ldlm_pools_thread->t_ctl_waitq);
1069 * Make sure that pools thread is finished before freeing @thread.
1070 * This fixes possible race and oops due to accessing freed memory
1073 wait_for_completion(&ldlm_pools_comp);
1074 kfree(ldlm_pools_thread);
1075 ldlm_pools_thread = NULL;
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,
1084 int ldlm_pools_init(void)
1088 rc = ldlm_pools_thread_start();
1090 register_shrinker(&ldlm_pools_cli_shrinker);
1095 void ldlm_pools_fini(void)
1097 if (ldlm_pools_thread)
1098 unregister_shrinker(&ldlm_pools_cli_shrinker);
1100 ldlm_pools_thread_stop();