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