]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/staging/lustre/lustre/ldlm/ldlm_pool.c
staging/lustre: Remove ns_is_server()
[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 *
30 * Copyright (c) 2010, 2012, Intel Corporation.
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
107
108/*
109 * 50 ldlm locks for 1MB of RAM.
110 */
111#define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_CACHE_SHIFT)) * 50)
112
113/*
114 * Maximal possible grant step plan in %.
115 */
116#define LDLM_POOL_MAX_GSP (30)
117
118/*
119 * Minimal possible grant step plan in %.
120 */
121#define LDLM_POOL_MIN_GSP (1)
122
123/*
124 * This controls the speed of reaching LDLM_POOL_MAX_GSP
125 * with increasing thread period.
126 */
127#define LDLM_POOL_GSP_STEP_SHIFT (2)
128
129/*
130 * LDLM_POOL_GSP% of all locks is default GP.
131 */
132#define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
133
134/*
135 * Max age for locks on clients.
136 */
137#define LDLM_POOL_MAX_AGE (36000)
138
139/*
140 * The granularity of SLV calculation.
141 */
142#define LDLM_POOL_SLV_SHIFT (10)
143
d7e09d03
PT
144static inline __u64 dru(__u64 val, __u32 shift, int round_up)
145{
146 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
147}
148
149static inline __u64 ldlm_pool_slv_max(__u32 L)
150{
151 /*
152 * Allow to have all locks for 1 client for 10 hrs.
153 * Formula is the following: limit * 10h / 1 client.
154 */
155 __u64 lim = (__u64)L * LDLM_POOL_MAX_AGE / 1;
156 return lim;
157}
158
159static inline __u64 ldlm_pool_slv_min(__u32 L)
160{
161 return 1;
162}
163
164enum {
165 LDLM_POOL_FIRST_STAT = 0,
166 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
167 LDLM_POOL_GRANT_STAT,
168 LDLM_POOL_CANCEL_STAT,
169 LDLM_POOL_GRANT_RATE_STAT,
170 LDLM_POOL_CANCEL_RATE_STAT,
171 LDLM_POOL_GRANT_PLAN_STAT,
172 LDLM_POOL_SLV_STAT,
173 LDLM_POOL_SHRINK_REQTD_STAT,
174 LDLM_POOL_SHRINK_FREED_STAT,
175 LDLM_POOL_RECALC_STAT,
176 LDLM_POOL_TIMING_STAT,
177 LDLM_POOL_LAST_STAT
178};
179
180static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
181{
182 return container_of(pl, struct ldlm_namespace, ns_pool);
183}
184
185/**
186 * Calculates suggested grant_step in % of available locks for passed
187 * \a period. This is later used in grant_plan calculations.
188 */
189static inline int ldlm_pool_t2gsp(unsigned int t)
190{
191 /*
192 * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
193 * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
194 *
195 * How this will affect execution is the following:
196 *
197 * - for thread period 1s we will have grant_step 1% which good from
198 * pov of taking some load off from server and push it out to clients.
199 * This is like that because 1% for grant_step means that server will
200 * not allow clients to get lots of locks in short period of time and
201 * keep all old locks in their caches. Clients will always have to
202 * get some locks back if they want to take some new;
203 *
204 * - for thread period 10s (which is default) we will have 23% which
205 * means that clients will have enough of room to take some new locks
206 * without getting some back. All locks from this 23% which were not
207 * taken by clients in current period will contribute in SLV growing.
208 * SLV growing means more locks cached on clients until limit or grant
209 * plan is reached.
210 */
211 return LDLM_POOL_MAX_GSP -
212 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
213 (t >> LDLM_POOL_GSP_STEP_SHIFT));
214}
215
216/**
217 * Recalculates next grant limit on passed \a pl.
218 *
219 * \pre ->pl_lock is locked.
220 */
221static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
222{
223 int granted, grant_step, limit;
224
225 limit = ldlm_pool_get_limit(pl);
226 granted = atomic_read(&pl->pl_granted);
227
228 grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
229 grant_step = ((limit - granted) * grant_step) / 100;
230 pl->pl_grant_plan = granted + grant_step;
231 limit = (limit * 5) >> 2;
232 if (pl->pl_grant_plan > limit)
233 pl->pl_grant_plan = limit;
234}
235
236/**
237 * Recalculates next SLV on passed \a pl.
238 *
239 * \pre ->pl_lock is locked.
240 */
241static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
242{
243 int granted;
244 int grant_plan;
245 int round_up;
246 __u64 slv;
247 __u64 slv_factor;
248 __u64 grant_usage;
249 __u32 limit;
250
251 slv = pl->pl_server_lock_volume;
252 grant_plan = pl->pl_grant_plan;
253 limit = ldlm_pool_get_limit(pl);
254 granted = atomic_read(&pl->pl_granted);
255 round_up = granted < limit;
256
257 grant_usage = max_t(int, limit - (granted - grant_plan), 1);
258
259 /*
260 * Find out SLV change factor which is the ratio of grant usage
261 * from limit. SLV changes as fast as the ratio of grant plan
262 * consumption. The more locks from grant plan are not consumed
263 * by clients in last interval (idle time), the faster grows
264 * SLV. And the opposite, the more grant plan is over-consumed
265 * (load time) the faster drops SLV.
266 */
1d06bb4e 267 slv_factor = grant_usage << LDLM_POOL_SLV_SHIFT;
d7e09d03
PT
268 do_div(slv_factor, limit);
269 slv = slv * slv_factor;
270 slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
271
8d2ff65d 272 if (slv > ldlm_pool_slv_max(limit))
d7e09d03 273 slv = ldlm_pool_slv_max(limit);
8d2ff65d 274 else if (slv < ldlm_pool_slv_min(limit))
d7e09d03 275 slv = ldlm_pool_slv_min(limit);
d7e09d03
PT
276
277 pl->pl_server_lock_volume = slv;
278}
279
280/**
281 * Recalculates next stats on passed \a pl.
282 *
283 * \pre ->pl_lock is locked.
284 */
285static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
286{
287 int grant_plan = pl->pl_grant_plan;
288 __u64 slv = pl->pl_server_lock_volume;
289 int granted = atomic_read(&pl->pl_granted);
290 int grant_rate = atomic_read(&pl->pl_grant_rate);
291 int cancel_rate = atomic_read(&pl->pl_cancel_rate);
292
293 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
294 slv);
295 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
296 granted);
297 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
298 grant_rate);
299 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
300 grant_plan);
301 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
302 cancel_rate);
303}
304
305/**
306 * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
307 */
308static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
309{
310 struct obd_device *obd;
311
312 /*
313 * Set new SLV in obd field for using it later without accessing the
314 * pool. This is required to avoid race between sending reply to client
315 * with new SLV and cleanup server stack in which we can't guarantee
316 * that namespace is still alive. We know only that obd is alive as
317 * long as valid export is alive.
318 */
319 obd = ldlm_pl2ns(pl)->ns_obd;
320 LASSERT(obd != NULL);
321 write_lock(&obd->obd_pool_lock);
322 obd->obd_pool_slv = pl->pl_server_lock_volume;
323 write_unlock(&obd->obd_pool_lock);
324}
325
326/**
327 * Recalculates all pool fields on passed \a pl.
328 *
329 * \pre ->pl_lock is not locked.
330 */
331static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
332{
8f83409c 333 time64_t recalc_interval_sec;
d7e09d03 334
8f83409c 335 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
d7e09d03 336 if (recalc_interval_sec < pl->pl_recalc_period)
0a3bdb00 337 return 0;
d7e09d03
PT
338
339 spin_lock(&pl->pl_lock);
8f83409c 340 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
d7e09d03
PT
341 if (recalc_interval_sec < pl->pl_recalc_period) {
342 spin_unlock(&pl->pl_lock);
0a3bdb00 343 return 0;
d7e09d03
PT
344 }
345 /*
346 * Recalc SLV after last period. This should be done
347 * _before_ recalculating new grant plan.
348 */
349 ldlm_pool_recalc_slv(pl);
350
351 /*
352 * Make sure that pool informed obd of last SLV changes.
353 */
354 ldlm_srv_pool_push_slv(pl);
355
356 /*
357 * Update grant_plan for new period.
358 */
359 ldlm_pool_recalc_grant_plan(pl);
360
8f83409c 361 pl->pl_recalc_time = ktime_get_real_seconds();
d7e09d03
PT
362 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
363 recalc_interval_sec);
364 spin_unlock(&pl->pl_lock);
0a3bdb00 365 return 0;
d7e09d03
PT
366}
367
368/**
369 * This function is used on server side as main entry point for memory
370 * pressure handling. It decreases SLV on \a pl according to passed
371 * \a nr and \a gfp_mask.
372 *
373 * Our goal here is to decrease SLV such a way that clients hold \a nr
374 * locks smaller in next 10h.
375 */
376static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
5802572e 377 int nr, gfp_t gfp_mask)
d7e09d03
PT
378{
379 __u32 limit;
380
381 /*
382 * VM is asking how many entries may be potentially freed.
383 */
384 if (nr == 0)
385 return atomic_read(&pl->pl_granted);
386
387 /*
388 * Client already canceled locks but server is already in shrinker
389 * and can't cancel anything. Let's catch this race.
390 */
391 if (atomic_read(&pl->pl_granted) == 0)
0a3bdb00 392 return 0;
d7e09d03
PT
393
394 spin_lock(&pl->pl_lock);
395
396 /*
397 * We want shrinker to possibly cause cancellation of @nr locks from
398 * clients or grant approximately @nr locks smaller next intervals.
399 *
400 * This is why we decreased SLV by @nr. This effect will only be as
401 * long as one re-calc interval (1s these days) and this should be
402 * enough to pass this decreased SLV to all clients. On next recalc
403 * interval pool will either increase SLV if locks load is not high
404 * or will keep on same level or even decrease again, thus, shrinker
405 * decreased SLV will affect next recalc intervals and this way will
406 * make locking load lower.
407 */
408 if (nr < pl->pl_server_lock_volume) {
409 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
410 } else {
411 limit = ldlm_pool_get_limit(pl);
412 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
413 }
414
415 /*
416 * Make sure that pool informed obd of last SLV changes.
417 */
418 ldlm_srv_pool_push_slv(pl);
419 spin_unlock(&pl->pl_lock);
420
421 /*
422 * We did not really free any memory here so far, it only will be
423 * freed later may be, so that we return 0 to not confuse VM.
424 */
425 return 0;
426}
427
428/**
429 * Setup server side pool \a pl with passed \a limit.
430 */
431static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
432{
433 struct obd_device *obd;
434
435 obd = ldlm_pl2ns(pl)->ns_obd;
436 LASSERT(obd != NULL && obd != LP_POISON);
437 LASSERT(obd->obd_type != LP_POISON);
438 write_lock(&obd->obd_pool_lock);
439 obd->obd_pool_limit = limit;
440 write_unlock(&obd->obd_pool_lock);
441
442 ldlm_pool_set_limit(pl, limit);
443 return 0;
444}
445
446/**
447 * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
448 */
449static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
450{
451 struct obd_device *obd;
452
453 /*
454 * Get new SLV and Limit from obd which is updated with coming
455 * RPCs.
456 */
457 obd = ldlm_pl2ns(pl)->ns_obd;
458 LASSERT(obd != NULL);
459 read_lock(&obd->obd_pool_lock);
460 pl->pl_server_lock_volume = obd->obd_pool_slv;
461 ldlm_pool_set_limit(pl, obd->obd_pool_limit);
462 read_unlock(&obd->obd_pool_lock);
463}
464
465/**
466 * Recalculates client size pool \a pl according to current SLV and Limit.
467 */
468static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
469{
8f83409c 470 time64_t recalc_interval_sec;
4d2c7b30 471 int ret;
d7e09d03 472
8f83409c 473 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
d7e09d03 474 if (recalc_interval_sec < pl->pl_recalc_period)
0a3bdb00 475 return 0;
d7e09d03
PT
476
477 spin_lock(&pl->pl_lock);
478 /*
479 * Check if we need to recalc lists now.
480 */
8f83409c 481 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
d7e09d03
PT
482 if (recalc_interval_sec < pl->pl_recalc_period) {
483 spin_unlock(&pl->pl_lock);
0a3bdb00 484 return 0;
d7e09d03
PT
485 }
486
487 /*
488 * Make sure that pool knows last SLV and Limit from obd.
489 */
490 ldlm_cli_pool_pop_slv(pl);
491
d7e09d03
PT
492 spin_unlock(&pl->pl_lock);
493
494 /*
495 * Do not cancel locks in case lru resize is disabled for this ns.
496 */
4d2c7b30
LX
497 if (!ns_connect_lru_resize(ldlm_pl2ns(pl))) {
498 ret = 0;
499 goto out;
500 }
d7e09d03
PT
501
502 /*
503 * In the time of canceling locks on client we do not need to maintain
504 * sharp timing, we only want to cancel locks asap according to new SLV.
505 * It may be called when SLV has changed much, this is why we do not
506 * take into account pl->pl_recalc_time here.
507 */
4d2c7b30
LX
508 ret = ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LCF_ASYNC, LDLM_CANCEL_LRUR);
509
510out:
511 spin_lock(&pl->pl_lock);
512 /*
513 * Time of LRU resizing might be longer than period,
514 * so update after LRU resizing rather than before it.
515 */
8f83409c 516 pl->pl_recalc_time = ktime_get_real_seconds();
4d2c7b30
LX
517 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
518 recalc_interval_sec);
519 spin_unlock(&pl->pl_lock);
520 return ret;
d7e09d03
PT
521}
522
523/**
524 * This function is main entry point for memory pressure handling on client
525 * side. Main goal of this function is to cancel some number of locks on
526 * passed \a pl according to \a nr and \a gfp_mask.
527 */
528static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
5802572e 529 int nr, gfp_t gfp_mask)
d7e09d03
PT
530{
531 struct ldlm_namespace *ns;
cbc3769e 532 int unused;
d7e09d03
PT
533
534 ns = ldlm_pl2ns(pl);
535
536 /*
537 * Do not cancel locks in case lru resize is disabled for this ns.
538 */
539 if (!ns_connect_lru_resize(ns))
0a3bdb00 540 return 0;
d7e09d03
PT
541
542 /*
543 * Make sure that pool knows last SLV and Limit from obd.
544 */
545 ldlm_cli_pool_pop_slv(pl);
546
547 spin_lock(&ns->ns_lock);
548 unused = ns->ns_nr_unused;
549 spin_unlock(&ns->ns_lock);
550
cbc3769e
PT
551 if (nr == 0)
552 return (unused / 100) * sysctl_vfs_cache_pressure;
553 else
554 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_CANCEL_SHRINK);
d7e09d03
PT
555}
556
b9c98cfa 557static const struct ldlm_pool_ops ldlm_srv_pool_ops = {
d7e09d03
PT
558 .po_recalc = ldlm_srv_pool_recalc,
559 .po_shrink = ldlm_srv_pool_shrink,
560 .po_setup = ldlm_srv_pool_setup
561};
562
b9c98cfa 563static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
d7e09d03
PT
564 .po_recalc = ldlm_cli_pool_recalc,
565 .po_shrink = ldlm_cli_pool_shrink
566};
567
568/**
569 * Pool recalc wrapper. Will call either client or server pool recalc callback
570 * depending what pool \a pl is used.
571 */
572int ldlm_pool_recalc(struct ldlm_pool *pl)
573{
8f83409c 574 u32 recalc_interval_sec;
d7e09d03
PT
575 int count;
576
8f83409c 577 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
d7e09d03
PT
578 if (recalc_interval_sec <= 0)
579 goto recalc;
580
581 spin_lock(&pl->pl_lock);
d7e09d03
PT
582 if (recalc_interval_sec > 0) {
583 /*
584 * Update pool statistics every 1s.
585 */
586 ldlm_pool_recalc_stats(pl);
587
588 /*
589 * Zero out all rates and speed for the last period.
590 */
591 atomic_set(&pl->pl_grant_rate, 0);
592 atomic_set(&pl->pl_cancel_rate, 0);
593 }
594 spin_unlock(&pl->pl_lock);
595
596 recalc:
597 if (pl->pl_ops->po_recalc != NULL) {
598 count = pl->pl_ops->po_recalc(pl);
599 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
600 count);
d7e09d03 601 }
8f83409c 602 recalc_interval_sec = pl->pl_recalc_time - ktime_get_seconds() +
3eface59 603 pl->pl_recalc_period;
4d2c7b30
LX
604 if (recalc_interval_sec <= 0) {
605 /* Prevent too frequent recalculation. */
8f83409c
AB
606 CDEBUG(D_DLMTRACE,
607 "Negative interval(%d), too short period(%lld)",
4d2c7b30 608 recalc_interval_sec,
8f83409c 609 (s64)pl->pl_recalc_period);
4d2c7b30
LX
610 recalc_interval_sec = 1;
611 }
d7e09d03 612
3eface59 613 return recalc_interval_sec;
d7e09d03 614}
d7e09d03 615
cbc3769e 616/*
d7e09d03 617 * Pool shrink wrapper. Will call either client or server pool recalc callback
cbc3769e
PT
618 * depending what pool pl is used. When nr == 0, just return the number of
619 * freeable locks. Otherwise, return the number of canceled locks.
d7e09d03
PT
620 */
621int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
5802572e 622 gfp_t gfp_mask)
d7e09d03
PT
623{
624 int cancel = 0;
625
626 if (pl->pl_ops->po_shrink != NULL) {
627 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
628 if (nr > 0) {
629 lprocfs_counter_add(pl->pl_stats,
630 LDLM_POOL_SHRINK_REQTD_STAT,
631 nr);
632 lprocfs_counter_add(pl->pl_stats,
633 LDLM_POOL_SHRINK_FREED_STAT,
634 cancel);
2d00bd17
JP
635 CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, shrunk %d\n",
636 pl->pl_name, nr, cancel);
d7e09d03
PT
637 }
638 }
639 return cancel;
640}
641EXPORT_SYMBOL(ldlm_pool_shrink);
642
643/**
644 * Pool setup wrapper. Will call either client or server pool recalc callback
645 * depending what pool \a pl is used.
646 *
647 * Sets passed \a limit into pool \a pl.
648 */
649int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
650{
651 if (pl->pl_ops->po_setup != NULL)
e8291974 652 return pl->pl_ops->po_setup(pl, limit);
d7e09d03
PT
653 return 0;
654}
655EXPORT_SYMBOL(ldlm_pool_setup);
656
73bb1da6 657static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
d7e09d03 658{
71570b98
OD
659 int granted, grant_rate, cancel_rate;
660 int grant_speed, lvf;
73bb1da6 661 struct ldlm_pool *pl = m->private;
d7e09d03
PT
662 __u64 slv, clv;
663 __u32 limit;
664
665 spin_lock(&pl->pl_lock);
666 slv = pl->pl_server_lock_volume;
667 clv = pl->pl_client_lock_volume;
668 limit = ldlm_pool_get_limit(pl);
d7e09d03
PT
669 granted = atomic_read(&pl->pl_granted);
670 grant_rate = atomic_read(&pl->pl_grant_rate);
671 cancel_rate = atomic_read(&pl->pl_cancel_rate);
672 grant_speed = grant_rate - cancel_rate;
673 lvf = atomic_read(&pl->pl_lock_volume_factor);
d7e09d03
PT
674 spin_unlock(&pl->pl_lock);
675
73bb1da6 676 seq_printf(m, "LDLM pool state (%s):\n"
b0f5aad5
GKH
677 " SLV: %llu\n"
678 " CLV: %llu\n"
73bb1da6
PT
679 " LVF: %d\n",
680 pl->pl_name, slv, clv, lvf);
d7e09d03 681
2c2b7c05
HM
682 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
683 " G: %d\n L: %d\n",
73bb1da6
PT
684 grant_rate, cancel_rate, grant_speed,
685 granted, limit);
686
687 return 0;
d7e09d03 688}
73bb1da6 689LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
d7e09d03 690
24b8c88a
OD
691static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
692 char *buf)
d7e09d03 693{
24b8c88a
OD
694 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
695 pl_kobj);
696
d7e09d03
PT
697 int grant_speed;
698
699 spin_lock(&pl->pl_lock);
700 /* serialize with ldlm_pool_recalc */
701 grant_speed = atomic_read(&pl->pl_grant_rate) -
702 atomic_read(&pl->pl_cancel_rate);
703 spin_unlock(&pl->pl_lock);
24b8c88a 704 return sprintf(buf, "%d\n", grant_speed);
d7e09d03 705}
24b8c88a 706LUSTRE_RO_ATTR(grant_speed);
d7e09d03 707
24b8c88a
OD
708LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
709LUSTRE_RO_ATTR(grant_plan);
73bb1da6 710
24b8c88a
OD
711LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
712LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
713LUSTRE_RW_ATTR(recalc_period);
73bb1da6 714
24b8c88a
OD
715LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
716LUSTRE_RO_ATTR(server_lock_volume);
717
718LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
719LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
720LUSTRE_RW_ATTR(limit);
721
722LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
723LUSTRE_RO_ATTR(granted);
724
725LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
726LUSTRE_RO_ATTR(cancel_rate);
73bb1da6 727
24b8c88a
OD
728LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
729LUSTRE_RO_ATTR(grant_rate);
73bb1da6 730
24b8c88a
OD
731LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(lock_volume_factor, atomic);
732LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(lock_volume_factor, atomic);
733LUSTRE_RW_ATTR(lock_volume_factor);
73bb1da6
PT
734
735#define LDLM_POOL_ADD_VAR(name, var, ops) \
736 do { \
737 snprintf(var_name, MAX_STRING_SIZE, #name); \
738 pool_vars[0].data = var; \
739 pool_vars[0].fops = ops; \
700815d4 740 ldebugfs_add_vars(pl->pl_debugfs_entry, pool_vars, NULL);\
73bb1da6 741 } while (0)
d7e09d03 742
f2825e03
OD
743/* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
744static struct attribute *ldlm_pl_attrs[] = {
24b8c88a
OD
745 &lustre_attr_grant_speed.attr,
746 &lustre_attr_grant_plan.attr,
747 &lustre_attr_recalc_period.attr,
748 &lustre_attr_server_lock_volume.attr,
749 &lustre_attr_limit.attr,
750 &lustre_attr_granted.attr,
751 &lustre_attr_cancel_rate.attr,
752 &lustre_attr_grant_rate.attr,
753 &lustre_attr_lock_volume_factor.attr,
f2825e03
OD
754 NULL,
755};
756
757static void ldlm_pl_release(struct kobject *kobj)
758{
759 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
760 pl_kobj);
761 complete(&pl->pl_kobj_unregister);
762}
763
764static struct kobj_type ldlm_pl_ktype = {
765 .default_attrs = ldlm_pl_attrs,
766 .sysfs_ops = &lustre_sysfs_ops,
767 .release = ldlm_pl_release,
768};
769
770static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
771{
772 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
773 int err;
774
775 init_completion(&pl->pl_kobj_unregister);
776 err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
777 "pool");
778
779 return err;
780}
781
700815d4 782static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
d7e09d03
PT
783{
784 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
700815d4 785 struct dentry *debugfs_ns_parent;
d7e09d03
PT
786 struct lprocfs_vars pool_vars[2];
787 char *var_name = NULL;
788 int rc = 0;
d7e09d03 789
352f7891 790 var_name = kzalloc(MAX_STRING_SIZE + 1, GFP_NOFS);
d7e09d03 791 if (!var_name)
0a3bdb00 792 return -ENOMEM;
d7e09d03 793
700815d4
DE
794 debugfs_ns_parent = ns->ns_debugfs_entry;
795 if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
796 CERROR("%s: debugfs entry is not initialized\n",
d7e09d03 797 ldlm_ns_name(ns));
d1c0d446
JL
798 rc = -EINVAL;
799 goto out_free_name;
d7e09d03 800 }
700815d4
DE
801 pl->pl_debugfs_entry = ldebugfs_register("pool", debugfs_ns_parent,
802 NULL, NULL);
803 if (IS_ERR(pl->pl_debugfs_entry)) {
804 CERROR("LdebugFS failed in ldlm-pool-init\n");
805 rc = PTR_ERR(pl->pl_debugfs_entry);
806 pl->pl_debugfs_entry = NULL;
d1c0d446 807 goto out_free_name;
d7e09d03
PT
808 }
809
810 var_name[MAX_STRING_SIZE] = '\0';
811 memset(pool_vars, 0, sizeof(pool_vars));
812 pool_vars[0].name = var_name;
813
700815d4 814 LDLM_POOL_ADD_VAR(state, pl, &lprocfs_pool_state_fops);
d7e09d03
PT
815
816 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
817 LDLM_POOL_FIRST_STAT, 0);
d1c0d446
JL
818 if (!pl->pl_stats) {
819 rc = -ENOMEM;
820 goto out_free_name;
821 }
d7e09d03
PT
822
823 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
824 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
825 "granted", "locks");
826 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
827 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
828 "grant", "locks");
829 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
830 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
831 "cancel", "locks");
832 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
833 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
834 "grant_rate", "locks/s");
835 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
836 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
837 "cancel_rate", "locks/s");
838 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
839 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
840 "grant_plan", "locks/s");
841 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
842 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
843 "slv", "slv");
844 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
845 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
846 "shrink_request", "locks");
847 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
848 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
849 "shrink_freed", "locks");
850 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
851 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
852 "recalc_freed", "locks");
853 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
854 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
855 "recalc_timing", "sec");
700815d4
DE
856 rc = ldebugfs_register_stats(pl->pl_debugfs_entry, "stats",
857 pl->pl_stats);
d7e09d03 858
d7e09d03 859out_free_name:
352f7891 860 kfree(var_name);
d7e09d03
PT
861 return rc;
862}
863
f2825e03
OD
864static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
865{
866 kobject_put(&pl->pl_kobj);
867 wait_for_completion(&pl->pl_kobj_unregister);
868}
869
700815d4 870static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
d7e09d03
PT
871{
872 if (pl->pl_stats != NULL) {
873 lprocfs_free_stats(&pl->pl_stats);
874 pl->pl_stats = NULL;
875 }
700815d4
DE
876 if (pl->pl_debugfs_entry != NULL) {
877 ldebugfs_remove(&pl->pl_debugfs_entry);
878 pl->pl_debugfs_entry = NULL;
d7e09d03
PT
879 }
880}
881
882int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
883 int idx, ldlm_side_t client)
884{
885 int rc;
d7e09d03
PT
886
887 spin_lock_init(&pl->pl_lock);
888 atomic_set(&pl->pl_granted, 0);
8f83409c 889 pl->pl_recalc_time = ktime_get_seconds();
d7e09d03
PT
890 atomic_set(&pl->pl_lock_volume_factor, 1);
891
892 atomic_set(&pl->pl_grant_rate, 0);
893 atomic_set(&pl->pl_cancel_rate, 0);
894 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
895
896 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
897 ldlm_ns_name(ns), idx);
898
899 if (client == LDLM_NAMESPACE_SERVER) {
900 pl->pl_ops = &ldlm_srv_pool_ops;
901 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
902 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
903 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
904 } else {
905 ldlm_pool_set_limit(pl, 1);
906 pl->pl_server_lock_volume = 0;
907 pl->pl_ops = &ldlm_cli_pool_ops;
908 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
909 }
910 pl->pl_client_lock_volume = 0;
700815d4 911 rc = ldlm_pool_debugfs_init(pl);
d7e09d03 912 if (rc)
0a3bdb00 913 return rc;
d7e09d03 914
f2825e03
OD
915 rc = ldlm_pool_sysfs_init(pl);
916 if (rc)
917 return rc;
918
d7e09d03
PT
919 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
920
0a3bdb00 921 return rc;
d7e09d03
PT
922}
923EXPORT_SYMBOL(ldlm_pool_init);
924
925void ldlm_pool_fini(struct ldlm_pool *pl)
926{
f2825e03 927 ldlm_pool_sysfs_fini(pl);
700815d4 928 ldlm_pool_debugfs_fini(pl);
d7e09d03
PT
929
930 /*
931 * Pool should not be used after this point. We can't free it here as
932 * it lives in struct ldlm_namespace, but still interested in catching
933 * any abnormal using cases.
934 */
935 POISON(pl, 0x5a, sizeof(*pl));
d7e09d03
PT
936}
937EXPORT_SYMBOL(ldlm_pool_fini);
938
939/**
940 * Add new taken ldlm lock \a lock into pool \a pl accounting.
941 */
942void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
943{
944 /*
945 * FLOCK locks are special in a sense that they are almost never
946 * cancelled, instead special kind of lock is used to drop them.
947 * also there is no LRU for flock locks, so no point in tracking
948 * them anyway.
949 */
950 if (lock->l_resource->lr_type == LDLM_FLOCK)
951 return;
952
953 atomic_inc(&pl->pl_granted);
954 atomic_inc(&pl->pl_grant_rate);
955 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
956 /*
957 * Do not do pool recalc for client side as all locks which
958 * potentially may be canceled has already been packed into
959 * enqueue/cancel rpc. Also we do not want to run out of stack
960 * with too long call paths.
961 */
d7e09d03
PT
962}
963EXPORT_SYMBOL(ldlm_pool_add);
964
965/**
966 * Remove ldlm lock \a lock from pool \a pl accounting.
967 */
968void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
969{
970 /*
971 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
972 */
973 if (lock->l_resource->lr_type == LDLM_FLOCK)
974 return;
975
976 LASSERT(atomic_read(&pl->pl_granted) > 0);
977 atomic_dec(&pl->pl_granted);
978 atomic_inc(&pl->pl_cancel_rate);
979
980 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
d7e09d03
PT
981}
982EXPORT_SYMBOL(ldlm_pool_del);
983
984/**
985 * Returns current \a pl SLV.
986 *
987 * \pre ->pl_lock is not locked.
988 */
989__u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
990{
991 __u64 slv;
902f3bb1 992
d7e09d03
PT
993 spin_lock(&pl->pl_lock);
994 slv = pl->pl_server_lock_volume;
995 spin_unlock(&pl->pl_lock);
996 return slv;
997}
998EXPORT_SYMBOL(ldlm_pool_get_slv);
999
d7e09d03
PT
1000/**
1001 * Sets passed \a clv to \a pl.
1002 *
1003 * \pre ->pl_lock is not locked.
1004 */
1005void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1006{
1007 spin_lock(&pl->pl_lock);
1008 pl->pl_client_lock_volume = clv;
1009 spin_unlock(&pl->pl_lock);
1010}
1011EXPORT_SYMBOL(ldlm_pool_set_clv);
1012
1013/**
1014 * Returns current \a pl limit.
1015 */
1016__u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1017{
1018 return atomic_read(&pl->pl_limit);
1019}
1020EXPORT_SYMBOL(ldlm_pool_get_limit);
1021
1022/**
1023 * Sets passed \a limit to \a pl.
1024 */
1025void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1026{
1027 atomic_set(&pl->pl_limit, limit);
1028}
1029EXPORT_SYMBOL(ldlm_pool_set_limit);
1030
1031/**
1032 * Returns current LVF from \a pl.
1033 */
1034__u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1035{
1036 return atomic_read(&pl->pl_lock_volume_factor);
1037}
1038EXPORT_SYMBOL(ldlm_pool_get_lvf);
1039
1040static int ldlm_pool_granted(struct ldlm_pool *pl)
1041{
1042 return atomic_read(&pl->pl_granted);
1043}
1044
1045static struct ptlrpc_thread *ldlm_pools_thread;
d7e09d03
PT
1046static struct completion ldlm_pools_comp;
1047
1048/*
cbc3769e
PT
1049 * count locks from all namespaces (if possible). Returns number of
1050 * cached locks.
d7e09d03 1051 */
5802572e 1052static unsigned long ldlm_pools_count(ldlm_side_t client, gfp_t gfp_mask)
d7e09d03 1053{
cbc3769e 1054 int total = 0, nr_ns;
d7e09d03 1055 struct ldlm_namespace *ns;
91a50030 1056 struct ldlm_namespace *ns_old = NULL; /* loop detection */
d7e09d03
PT
1057 void *cookie;
1058
cbc3769e
PT
1059 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1060 return 0;
d7e09d03 1061
cbc3769e
PT
1062 CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
1063 client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
d7e09d03
PT
1064
1065 cookie = cl_env_reenter();
1066
1067 /*
1068 * Find out how many resources we may release.
1069 */
91a50030 1070 for (nr_ns = ldlm_namespace_nr_read(client);
cbc3769e 1071 nr_ns > 0; nr_ns--) {
d7e09d03
PT
1072 mutex_lock(ldlm_namespace_lock(client));
1073 if (list_empty(ldlm_namespace_list(client))) {
1074 mutex_unlock(ldlm_namespace_lock(client));
1075 cl_env_reexit(cookie);
1076 return 0;
1077 }
1078 ns = ldlm_namespace_first_locked(client);
91a50030
OD
1079
1080 if (ns == ns_old) {
1081 mutex_unlock(ldlm_namespace_lock(client));
1082 break;
1083 }
1084
1085 if (ldlm_ns_empty(ns)) {
1086 ldlm_namespace_move_to_inactive_locked(ns, client);
1087 mutex_unlock(ldlm_namespace_lock(client));
1088 continue;
1089 }
1090
1091 if (ns_old == NULL)
1092 ns_old = ns;
1093
d7e09d03 1094 ldlm_namespace_get(ns);
91a50030 1095 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1096 mutex_unlock(ldlm_namespace_lock(client));
1097 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1098 ldlm_namespace_put(ns);
1099 }
1100
cbc3769e
PT
1101 cl_env_reexit(cookie);
1102 return total;
1103}
1104
5802572e 1105static unsigned long ldlm_pools_scan(ldlm_side_t client, int nr, gfp_t gfp_mask)
cbc3769e
PT
1106{
1107 unsigned long freed = 0;
1108 int tmp, nr_ns;
1109 struct ldlm_namespace *ns;
1110 void *cookie;
1111
1112 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1113 return -1;
1114
1115 cookie = cl_env_reenter();
d7e09d03
PT
1116
1117 /*
cbc3769e 1118 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
d7e09d03 1119 */
cbc3769e
PT
1120 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
1121 tmp > 0; tmp--) {
d7e09d03
PT
1122 int cancel, nr_locks;
1123
1124 /*
1125 * Do not call shrink under ldlm_namespace_lock(client)
1126 */
1127 mutex_lock(ldlm_namespace_lock(client));
1128 if (list_empty(ldlm_namespace_list(client))) {
1129 mutex_unlock(ldlm_namespace_lock(client));
d7e09d03
PT
1130 break;
1131 }
1132 ns = ldlm_namespace_first_locked(client);
1133 ldlm_namespace_get(ns);
91a50030 1134 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1135 mutex_unlock(ldlm_namespace_lock(client));
1136
1137 nr_locks = ldlm_pool_granted(&ns->ns_pool);
cbc3769e
PT
1138 /*
1139 * We use to shrink propotionally but with new shrinker API,
1140 * we lost the total number of freeable locks.
1141 */
1142 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
1143 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
d7e09d03
PT
1144 ldlm_namespace_put(ns);
1145 }
1146 cl_env_reexit(cookie);
cbc3769e
PT
1147 /*
1148 * we only decrease the SLV in server pools shrinker, return
1149 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1150 */
1151 return (client == LDLM_NAMESPACE_SERVER) ? SHRINK_STOP : freed;
1152}
1153
e7ddc48c
AR
1154static unsigned long ldlm_pools_srv_count(struct shrinker *s,
1155 struct shrink_control *sc)
cbc3769e
PT
1156{
1157 return ldlm_pools_count(LDLM_NAMESPACE_SERVER, sc->gfp_mask);
d7e09d03
PT
1158}
1159
e7ddc48c
AR
1160static unsigned long ldlm_pools_srv_scan(struct shrinker *s,
1161 struct shrink_control *sc)
d7e09d03 1162{
cbc3769e
PT
1163 return ldlm_pools_scan(LDLM_NAMESPACE_SERVER, sc->nr_to_scan,
1164 sc->gfp_mask);
d7e09d03
PT
1165}
1166
e7ddc48c
AR
1167static unsigned long ldlm_pools_cli_count(struct shrinker *s,
1168 struct shrink_control *sc)
d7e09d03 1169{
cbc3769e
PT
1170 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1171}
1172
e7ddc48c
AR
1173static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1174 struct shrink_control *sc)
cbc3769e
PT
1175{
1176 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1177 sc->gfp_mask);
d7e09d03
PT
1178}
1179
3eface59 1180int ldlm_pools_recalc(ldlm_side_t client)
d7e09d03
PT
1181{
1182 __u32 nr_l = 0, nr_p = 0, l;
1183 struct ldlm_namespace *ns;
91a50030 1184 struct ldlm_namespace *ns_old = NULL;
d7e09d03 1185 int nr, equal = 0;
3eface59 1186 int time = 50; /* seconds of sleep if no active namespaces */
d7e09d03
PT
1187
1188 /*
1189 * No need to setup pool limit for client pools.
1190 */
1191 if (client == LDLM_NAMESPACE_SERVER) {
1192 /*
1193 * Check all modest namespaces first.
1194 */
1195 mutex_lock(ldlm_namespace_lock(client));
1196 list_for_each_entry(ns, ldlm_namespace_list(client),
9d0b2b7a 1197 ns_list_chain) {
d7e09d03
PT
1198 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1199 continue;
1200
1201 l = ldlm_pool_granted(&ns->ns_pool);
1202 if (l == 0)
1203 l = 1;
1204
1205 /*
1206 * Set the modest pools limit equal to their avg granted
1207 * locks + ~6%.
1208 */
1209 l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1210 ldlm_pool_setup(&ns->ns_pool, l);
1211 nr_l += l;
1212 nr_p++;
1213 }
1214
1215 /*
1216 * Make sure that modest namespaces did not eat more that 2/3
1217 * of limit.
1218 */
1219 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
2d00bd17
JP
1220 CWARN("\"Modest\" pools eat out 2/3 of server locks limit (%d of %lu). This means that you have too many clients for this amount of server RAM. Upgrade server!\n",
1221 nr_l, LDLM_POOL_HOST_L);
d7e09d03
PT
1222 equal = 1;
1223 }
1224
1225 /*
1226 * The rest is given to greedy namespaces.
1227 */
1228 list_for_each_entry(ns, ldlm_namespace_list(client),
e9e2fa69 1229 ns_list_chain) {
d7e09d03
PT
1230 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1231 continue;
1232
1233 if (equal) {
1234 /*
1235 * In the case 2/3 locks are eaten out by
1236 * modest pools, we re-setup equal limit
1237 * for _all_ pools.
1238 */
1239 l = LDLM_POOL_HOST_L /
91a50030 1240 ldlm_namespace_nr_read(client);
d7e09d03
PT
1241 } else {
1242 /*
1243 * All the rest of greedy pools will have
1244 * all locks in equal parts.
1245 */
1246 l = (LDLM_POOL_HOST_L - nr_l) /
91a50030 1247 (ldlm_namespace_nr_read(client) -
d7e09d03
PT
1248 nr_p);
1249 }
1250 ldlm_pool_setup(&ns->ns_pool, l);
1251 }
1252 mutex_unlock(ldlm_namespace_lock(client));
1253 }
1254
1255 /*
cbc3769e 1256 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
d7e09d03 1257 */
91a50030 1258 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
d7e09d03
PT
1259 int skip;
1260 /*
1261 * Lock the list, get first @ns in the list, getref, move it
1262 * to the tail, unlock and call pool recalc. This way we avoid
1263 * calling recalc under @ns lock what is really good as we get
1264 * rid of potential deadlock on client nodes when canceling
1265 * locks synchronously.
1266 */
1267 mutex_lock(ldlm_namespace_lock(client));
1268 if (list_empty(ldlm_namespace_list(client))) {
1269 mutex_unlock(ldlm_namespace_lock(client));
1270 break;
1271 }
1272 ns = ldlm_namespace_first_locked(client);
1273
91a50030
OD
1274 if (ns_old == ns) { /* Full pass complete */
1275 mutex_unlock(ldlm_namespace_lock(client));
1276 break;
1277 }
1278
1279 /* We got an empty namespace, need to move it back to inactive
1280 * list.
1281 * The race with parallel resource creation is fine:
1282 * - If they do namespace_get before our check, we fail the
1283 * check and they move this item to the end of the list anyway
1284 * - If we do the check and then they do namespace_get, then
1285 * we move the namespace to inactive and they will move
1286 * it back to active (synchronised by the lock, so no clash
1287 * there).
1288 */
1289 if (ldlm_ns_empty(ns)) {
1290 ldlm_namespace_move_to_inactive_locked(ns, client);
1291 mutex_unlock(ldlm_namespace_lock(client));
1292 continue;
1293 }
1294
1295 if (ns_old == NULL)
1296 ns_old = ns;
1297
d7e09d03
PT
1298 spin_lock(&ns->ns_lock);
1299 /*
1300 * skip ns which is being freed, and we don't want to increase
1301 * its refcount again, not even temporarily. bz21519 & LU-499.
1302 */
1303 if (ns->ns_stopping) {
1304 skip = 1;
1305 } else {
1306 skip = 0;
1307 ldlm_namespace_get(ns);
1308 }
1309 spin_unlock(&ns->ns_lock);
1310
91a50030 1311 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1312 mutex_unlock(ldlm_namespace_lock(client));
1313
1314 /*
1315 * After setup is done - recalc the pool.
1316 */
1317 if (!skip) {
3eface59
OD
1318 int ttime = ldlm_pool_recalc(&ns->ns_pool);
1319
1320 if (ttime < time)
1321 time = ttime;
1322
d7e09d03
PT
1323 ldlm_namespace_put(ns);
1324 }
1325 }
3eface59 1326 return time;
d7e09d03
PT
1327}
1328EXPORT_SYMBOL(ldlm_pools_recalc);
1329
1330static int ldlm_pools_thread_main(void *arg)
1331{
1332 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
3eface59 1333 int s_time, c_time;
d7e09d03
PT
1334
1335 thread_set_flags(thread, SVC_RUNNING);
1336 wake_up(&thread->t_ctl_waitq);
1337
1338 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1339 "ldlm_poold", current_pid());
1340
1341 while (1) {
1342 struct l_wait_info lwi;
1343
1344 /*
1345 * Recal all pools on this tick.
1346 */
3eface59
OD
1347 s_time = ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1348 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
d7e09d03
PT
1349
1350 /*
1351 * Wait until the next check time, or until we're
1352 * stopped.
1353 */
3eface59 1354 lwi = LWI_TIMEOUT(cfs_time_seconds(min(s_time, c_time)),
d7e09d03
PT
1355 NULL, NULL);
1356 l_wait_event(thread->t_ctl_waitq,
1357 thread_is_stopping(thread) ||
1358 thread_is_event(thread),
1359 &lwi);
1360
1361 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1362 break;
71e8dd9a 1363 thread_test_and_clear_flags(thread, SVC_EVENT);
d7e09d03
PT
1364 }
1365
1366 thread_set_flags(thread, SVC_STOPPED);
1367 wake_up(&thread->t_ctl_waitq);
1368
1369 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1370 "ldlm_poold", current_pid());
1371
1372 complete_and_exit(&ldlm_pools_comp, 0);
1373}
1374
1375static int ldlm_pools_thread_start(void)
1376{
1377 struct l_wait_info lwi = { 0 };
68b636b6 1378 struct task_struct *task;
d7e09d03
PT
1379
1380 if (ldlm_pools_thread != NULL)
0a3bdb00 1381 return -EALREADY;
d7e09d03 1382
352f7891 1383 ldlm_pools_thread = kzalloc(sizeof(*ldlm_pools_thread), GFP_NOFS);
94e67761 1384 if (!ldlm_pools_thread)
0a3bdb00 1385 return -ENOMEM;
d7e09d03
PT
1386
1387 init_completion(&ldlm_pools_comp);
1388 init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1389
1390 task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1391 "ldlm_poold");
1392 if (IS_ERR(task)) {
1393 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
352f7891 1394 kfree(ldlm_pools_thread);
d7e09d03 1395 ldlm_pools_thread = NULL;
0a3bdb00 1396 return PTR_ERR(task);
d7e09d03
PT
1397 }
1398 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1399 thread_is_running(ldlm_pools_thread), &lwi);
0a3bdb00 1400 return 0;
d7e09d03
PT
1401}
1402
1403static void ldlm_pools_thread_stop(void)
1404{
8d2ff65d 1405 if (ldlm_pools_thread == NULL)
d7e09d03 1406 return;
d7e09d03
PT
1407
1408 thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1409 wake_up(&ldlm_pools_thread->t_ctl_waitq);
1410
1411 /*
1412 * Make sure that pools thread is finished before freeing @thread.
1413 * This fixes possible race and oops due to accessing freed memory
1414 * in pools thread.
1415 */
1416 wait_for_completion(&ldlm_pools_comp);
352f7891 1417 kfree(ldlm_pools_thread);
d7e09d03 1418 ldlm_pools_thread = NULL;
d7e09d03
PT
1419}
1420
cbc3769e
PT
1421static struct shrinker ldlm_pools_srv_shrinker = {
1422 .count_objects = ldlm_pools_srv_count,
1423 .scan_objects = ldlm_pools_srv_scan,
1424 .seeks = DEFAULT_SEEKS,
1425};
1426
1427static struct shrinker ldlm_pools_cli_shrinker = {
1428 .count_objects = ldlm_pools_cli_count,
1429 .scan_objects = ldlm_pools_cli_scan,
1430 .seeks = DEFAULT_SEEKS,
1431};
1432
d7e09d03
PT
1433int ldlm_pools_init(void)
1434{
1435 int rc;
d7e09d03
PT
1436
1437 rc = ldlm_pools_thread_start();
1438 if (rc == 0) {
cbc3769e
PT
1439 register_shrinker(&ldlm_pools_srv_shrinker);
1440 register_shrinker(&ldlm_pools_cli_shrinker);
d7e09d03 1441 }
0a3bdb00 1442 return rc;
d7e09d03
PT
1443}
1444EXPORT_SYMBOL(ldlm_pools_init);
1445
1446void ldlm_pools_fini(void)
1447{
faa7a4e3
OD
1448 if (ldlm_pools_thread) {
1449 unregister_shrinker(&ldlm_pools_srv_shrinker);
1450 unregister_shrinker(&ldlm_pools_cli_shrinker);
1451 }
d7e09d03
PT
1452 ldlm_pools_thread_stop();
1453}
1454EXPORT_SYMBOL(ldlm_pools_fini);