4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_page for OSC layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_OSC
39 #include <lustre_osc.h>
41 #include "osc_internal.h"
43 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg);
44 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg);
45 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
46 struct osc_page *opg);
55 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
57 struct cl_page *page = opg->ops_cl.cpl_page;
59 LASSERT(!opg->ops_transfer_pinned);
61 lu_ref_add_atomic(&page->cp_reference, label, page);
62 opg->ops_transfer_pinned = 1;
65 static void osc_page_transfer_put(const struct lu_env *env,
68 struct cl_page *page = opg->ops_cl.cpl_page;
70 if (opg->ops_transfer_pinned) {
71 opg->ops_transfer_pinned = 0;
72 lu_ref_del(&page->cp_reference, "transfer", page);
73 cl_page_put(env, page);
78 * This is called once for every page when it is submitted for a transfer
79 * either opportunistic (osc_page_cache_add()), or immediate
80 * (osc_page_submit()).
82 static void osc_page_transfer_add(const struct lu_env *env,
83 struct osc_page *opg, enum cl_req_type crt)
85 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
87 /* ops_lru and ops_inflight share the same field, so take it from LRU
88 * first and then use it as inflight. */
89 osc_lru_use(osc_cli(obj), opg);
92 int osc_page_cache_add(const struct lu_env *env,
93 const struct cl_page_slice *slice, struct cl_io *io)
95 struct osc_page *opg = cl2osc_page(slice);
99 osc_page_transfer_get(opg, "transfer\0cache");
100 result = osc_queue_async_io(env, io, opg);
102 osc_page_transfer_put(env, opg);
104 osc_page_transfer_add(env, opg, CRT_WRITE);
109 void osc_index2policy(union ldlm_policy_data *policy,
110 const struct cl_object *obj, pgoff_t start, pgoff_t end)
112 memset(policy, 0, sizeof *policy);
113 policy->l_extent.start = cl_offset(obj, start);
114 policy->l_extent.end = cl_offset(obj, end + 1) - 1;
117 static const char *osc_list(struct list_head *head)
119 return list_empty(head) ? "-" : "+";
122 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
124 if (opg->ops_submit_time == 0)
127 return (cfs_time_current() - opg->ops_submit_time);
130 static int osc_page_print(const struct lu_env *env,
131 const struct cl_page_slice *slice,
132 void *cookie, lu_printer_t printer)
134 struct osc_page *opg = cl2osc_page(slice);
135 struct osc_async_page *oap = &opg->ops_oap;
136 struct osc_object *obj = cl2osc(slice->cpl_obj);
137 struct client_obd *cli = &osc_export(obj)->exp_obd->u.cli;
139 return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p %lu: "
140 "1< %#x %d %u %s %s > "
141 "2< %lld %u %u %#x %#x | %p %p %p > "
143 "4< %d %d %d %lu %s | %s %s %s %s > "
144 "5< %s %s %s %s | %d %s | %d %s %s>\n",
147 oap->oap_magic, oap->oap_cmd,
148 oap->oap_interrupted,
149 osc_list(&oap->oap_pending_item),
150 osc_list(&oap->oap_rpc_item),
152 oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
153 oap->oap_async_flags, oap->oap_brw_flags,
154 oap->oap_request, oap->oap_cli, obj,
156 opg->ops_transfer_pinned,
157 osc_submit_duration(opg), opg->ops_srvlock,
159 cli->cl_r_in_flight, cli->cl_w_in_flight,
160 cli->cl_max_rpcs_in_flight,
162 osc_list(&cli->cl_cache_waiters),
163 osc_list(&cli->cl_loi_ready_list),
164 osc_list(&cli->cl_loi_hp_ready_list),
165 osc_list(&cli->cl_loi_write_list),
166 osc_list(&cli->cl_loi_read_list),
168 osc_list(&obj->oo_ready_item),
169 osc_list(&obj->oo_hp_ready_item),
170 osc_list(&obj->oo_write_item),
171 osc_list(&obj->oo_read_item),
172 atomic_read(&obj->oo_nr_reads),
173 osc_list(&obj->oo_reading_exts),
174 atomic_read(&obj->oo_nr_writes),
175 osc_list(&obj->oo_hp_exts),
176 osc_list(&obj->oo_urgent_exts));
179 static void osc_page_delete(const struct lu_env *env,
180 const struct cl_page_slice *slice)
182 struct osc_page *opg = cl2osc_page(slice);
183 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
187 CDEBUG(D_TRACE, "%p\n", opg);
188 osc_page_transfer_put(env, opg);
189 rc = osc_teardown_async_page(env, obj, opg);
191 CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page,
192 "Trying to teardown failed: %d\n", rc);
196 osc_lru_del(osc_cli(obj), opg);
198 if (slice->cpl_page->cp_type == CPT_CACHEABLE) {
201 spin_lock(&obj->oo_tree_lock);
202 value = radix_tree_delete(&obj->oo_tree, osc_index(opg));
205 spin_unlock(&obj->oo_tree_lock);
207 LASSERT(ergo(value != NULL, value == opg));
213 static void osc_page_clip(const struct lu_env *env,
214 const struct cl_page_slice *slice,
217 struct osc_page *opg = cl2osc_page(slice);
218 struct osc_async_page *oap = &opg->ops_oap;
220 opg->ops_from = from;
222 spin_lock(&oap->oap_lock);
223 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
224 spin_unlock(&oap->oap_lock);
227 static int osc_page_cancel(const struct lu_env *env,
228 const struct cl_page_slice *slice)
230 struct osc_page *opg = cl2osc_page(slice);
233 /* Check if the transferring against this page
234 * is completed, or not even queued. */
235 if (opg->ops_transfer_pinned)
236 /* FIXME: may not be interrupted.. */
237 rc = osc_cancel_async_page(env, opg);
238 LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
242 static int osc_page_flush(const struct lu_env *env,
243 const struct cl_page_slice *slice,
246 struct osc_page *opg = cl2osc_page(slice);
249 rc = osc_flush_async_page(env, io, opg);
253 static const struct cl_page_operations osc_page_ops = {
254 .cpo_print = osc_page_print,
255 .cpo_delete = osc_page_delete,
256 .cpo_clip = osc_page_clip,
257 .cpo_cancel = osc_page_cancel,
258 .cpo_flush = osc_page_flush
261 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
262 struct cl_page *page, pgoff_t index)
264 struct osc_object *osc = cl2osc(obj);
265 struct osc_page *opg = cl_object_page_slice(obj, page);
266 struct osc_io *oio = osc_env_io(env);
270 opg->ops_to = PAGE_SIZE;
272 INIT_LIST_HEAD(&opg->ops_lru);
274 result = osc_prep_async_page(osc, opg, page->cp_vmpage,
275 cl_offset(obj, index));
279 opg->ops_srvlock = osc_io_srvlock(oio);
280 cl_page_slice_add(page, &opg->ops_cl, obj, index,
284 /* reserve an LRU space for this page */
285 if (page->cp_type == CPT_CACHEABLE) {
286 result = osc_lru_alloc(env, osc_cli(osc), opg);
288 result = radix_tree_preload(GFP_NOFS);
290 spin_lock(&osc->oo_tree_lock);
291 result = radix_tree_insert(&osc->oo_tree,
295 spin_unlock(&osc->oo_tree_lock);
297 radix_tree_preload_end();
306 * Helper function called by osc_io_submit() for every page in an immediate
307 * transfer (i.e., transferred synchronously).
309 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
310 enum cl_req_type crt, int brw_flags)
312 struct osc_async_page *oap = &opg->ops_oap;
314 LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, "
315 "magic 0x%x\n", oap, oap->oap_magic);
316 LASSERT(oap->oap_async_flags & ASYNC_READY);
317 LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
319 oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
320 oap->oap_page_off = opg->ops_from;
321 oap->oap_count = opg->ops_to - opg->ops_from;
322 oap->oap_brw_flags = OBD_BRW_SYNC | brw_flags;
324 if (cfs_capable(CFS_CAP_SYS_RESOURCE)) {
325 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
326 oap->oap_cmd |= OBD_BRW_NOQUOTA;
329 opg->ops_submit_time = cfs_time_current();
330 osc_page_transfer_get(opg, "transfer\0imm");
331 osc_page_transfer_add(env, opg, crt);
334 /* --------------- LRU page management ------------------ */
336 /* OSC is a natural place to manage LRU pages as applications are specialized
337 * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
338 * occupy more LRU slots. On the other hand, we should avoid using up all LRU
339 * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
340 * for free LRU slots - this will be very bad so the algorithm requires each
341 * OSC to free slots voluntarily to maintain a reasonable number of free slots
345 static DECLARE_WAIT_QUEUE_HEAD(osc_lru_waitq);
348 * LRU pages are freed in batch mode. OSC should at least free this
349 * number of pages to avoid running out of LRU slots.
351 static inline int lru_shrink_min(struct client_obd *cli)
353 return cli->cl_max_pages_per_rpc * 2;
357 * free this number at most otherwise it will take too long time to finsih.
359 static inline int lru_shrink_max(struct client_obd *cli)
361 return cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
365 * Check if we can free LRU slots from this OSC. If there exists LRU waiters,
366 * we should free slots aggressively. In this way, slots are freed in a steady
367 * step to maintain fairness among OSCs.
369 * Return how many LRU pages should be freed.
371 static int osc_cache_too_much(struct client_obd *cli)
373 struct cl_client_cache *cache = cli->cl_cache;
374 long pages = atomic_long_read(&cli->cl_lru_in_list);
375 unsigned long budget;
377 LASSERT(cache != NULL);
378 budget = cache->ccc_lru_max / (atomic_read(&cache->ccc_users) - 2);
380 /* if it's going to run out LRU slots, we should free some, but not
381 * too much to maintain faireness among OSCs. */
382 if (atomic_long_read(cli->cl_lru_left) < cache->ccc_lru_max >> 2) {
384 return lru_shrink_max(cli);
385 else if (pages >= budget / 2)
386 return lru_shrink_min(cli);
388 time64_t duration = ktime_get_real_seconds();
391 /* knock out pages by duration of no IO activity */
392 duration -= cli->cl_lru_last_used;
394 * The difference shouldn't be more than 70 years
395 * so we can safely case to a long. Round to
396 * approximately 1 minute.
398 timediff = (long)(duration >> 6);
399 if (timediff > 0 && pages >= budget / timediff)
400 return lru_shrink_min(cli);
405 int lru_queue_work(const struct lu_env *env, void *data)
407 struct client_obd *cli = data;
410 CDEBUG(D_CACHE, "%s: run LRU work for client obd\n", cli_name(cli));
411 count = osc_cache_too_much(cli);
413 int rc = osc_lru_shrink(env, cli, count, false);
415 CDEBUG(D_CACHE, "%s: shrank %d/%d pages from client obd\n",
416 cli_name(cli), rc, count);
418 CDEBUG(D_CACHE, "%s: queue again\n", cli_name(cli));
419 ptlrpcd_queue_work(cli->cl_lru_work);
426 void osc_lru_add_batch(struct client_obd *cli, struct list_head *plist)
428 struct list_head lru = LIST_HEAD_INIT(lru);
429 struct osc_async_page *oap;
432 list_for_each_entry(oap, plist, oap_pending_item) {
433 struct osc_page *opg = oap2osc_page(oap);
435 if (!opg->ops_in_lru)
439 LASSERT(list_empty(&opg->ops_lru));
440 list_add(&opg->ops_lru, &lru);
444 spin_lock(&cli->cl_lru_list_lock);
445 list_splice_tail(&lru, &cli->cl_lru_list);
446 atomic_long_sub(npages, &cli->cl_lru_busy);
447 atomic_long_add(npages, &cli->cl_lru_in_list);
448 cli->cl_lru_last_used = ktime_get_real_seconds();
449 spin_unlock(&cli->cl_lru_list_lock);
451 if (waitqueue_active(&osc_lru_waitq))
452 (void)ptlrpcd_queue_work(cli->cl_lru_work);
456 static void __osc_lru_del(struct client_obd *cli, struct osc_page *opg)
458 LASSERT(atomic_long_read(&cli->cl_lru_in_list) > 0);
459 list_del_init(&opg->ops_lru);
460 atomic_long_dec(&cli->cl_lru_in_list);
464 * Page is being destroyed. The page may be not in LRU list, if the transfer
465 * has never finished(error occurred).
467 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg)
469 if (opg->ops_in_lru) {
470 spin_lock(&cli->cl_lru_list_lock);
471 if (!list_empty(&opg->ops_lru)) {
472 __osc_lru_del(cli, opg);
474 LASSERT(atomic_long_read(&cli->cl_lru_busy) > 0);
475 atomic_long_dec(&cli->cl_lru_busy);
477 spin_unlock(&cli->cl_lru_list_lock);
479 atomic_long_inc(cli->cl_lru_left);
480 /* this is a great place to release more LRU pages if
481 * this osc occupies too many LRU pages and kernel is
482 * stealing one of them. */
483 if (osc_cache_too_much(cli)) {
484 CDEBUG(D_CACHE, "%s: queue LRU work\n", cli_name(cli));
485 (void)ptlrpcd_queue_work(cli->cl_lru_work);
487 wake_up(&osc_lru_waitq);
489 LASSERT(list_empty(&opg->ops_lru));
494 * Delete page from LRU list for redirty.
496 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg)
498 /* If page is being transferred for the first time,
499 * ops_lru should be empty */
500 if (opg->ops_in_lru) {
501 spin_lock(&cli->cl_lru_list_lock);
502 if (!list_empty(&opg->ops_lru)) {
503 __osc_lru_del(cli, opg);
504 atomic_long_inc(&cli->cl_lru_busy);
506 spin_unlock(&cli->cl_lru_list_lock);
510 static void discard_pagevec(const struct lu_env *env, struct cl_io *io,
511 struct cl_page **pvec, int max_index)
515 for (i = 0; i < max_index; i++) {
516 struct cl_page *page = pvec[i];
518 LASSERT(cl_page_is_owned(page, io));
519 cl_page_delete(env, page);
520 cl_page_discard(env, io, page);
521 cl_page_disown(env, io, page);
522 cl_page_put(env, page);
529 * Check if a cl_page can be released, i.e, it's not being used.
531 * If unstable account is turned on, bulk transfer may hold one refcount
532 * for recovery so we need to check vmpage refcount as well; otherwise,
533 * even we can destroy cl_page but the corresponding vmpage can't be reused.
535 static inline bool lru_page_busy(struct client_obd *cli, struct cl_page *page)
537 if (cl_page_in_use_noref(page))
540 if (cli->cl_cache->ccc_unstable_check) {
541 struct page *vmpage = cl_page_vmpage(page);
543 /* vmpage have two known users: cl_page and VM page cache */
544 if (page_count(vmpage) - page_mapcount(vmpage) > 2)
551 * Drop @target of pages from LRU at most.
553 long osc_lru_shrink(const struct lu_env *env, struct client_obd *cli,
554 long target, bool force)
557 struct cl_object *clobj = NULL;
558 struct cl_page **pvec;
559 struct osc_page *opg;
566 LASSERT(atomic_long_read(&cli->cl_lru_in_list) >= 0);
567 if (atomic_long_read(&cli->cl_lru_in_list) == 0 || target <= 0)
570 CDEBUG(D_CACHE, "%s: shrinkers: %d, force: %d\n",
571 cli_name(cli), atomic_read(&cli->cl_lru_shrinkers), force);
573 if (atomic_read(&cli->cl_lru_shrinkers) > 0)
576 if (atomic_inc_return(&cli->cl_lru_shrinkers) > 1) {
577 atomic_dec(&cli->cl_lru_shrinkers);
581 atomic_inc(&cli->cl_lru_shrinkers);
584 pvec = (struct cl_page **)osc_env_info(env)->oti_pvec;
585 io = &osc_env_info(env)->oti_io;
587 spin_lock(&cli->cl_lru_list_lock);
589 cli->cl_lru_reclaim++;
590 maxscan = min(target << 1, atomic_long_read(&cli->cl_lru_in_list));
591 while (!list_empty(&cli->cl_lru_list)) {
592 struct cl_page *page;
593 bool will_free = false;
595 if (!force && atomic_read(&cli->cl_lru_shrinkers) > 1)
601 opg = list_entry(cli->cl_lru_list.next, struct osc_page,
603 page = opg->ops_cl.cpl_page;
604 if (lru_page_busy(cli, page)) {
605 list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
609 LASSERT(page->cp_obj != NULL);
610 if (clobj != page->cp_obj) {
611 struct cl_object *tmp = page->cp_obj;
614 spin_unlock(&cli->cl_lru_list_lock);
617 discard_pagevec(env, io, pvec, index);
621 cl_object_put(env, clobj);
627 io->ci_ignore_layout = 1;
628 rc = cl_io_init(env, io, CIT_MISC, clobj);
630 spin_lock(&cli->cl_lru_list_lock);
639 if (cl_page_own_try(env, io, page) == 0) {
640 if (!lru_page_busy(cli, page)) {
641 /* remove it from lru list earlier to avoid
643 __osc_lru_del(cli, opg);
644 opg->ops_in_lru = 0; /* will be discarded */
649 cl_page_disown(env, io, page);
654 list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
658 /* Don't discard and free the page with cl_lru_list held */
659 pvec[index++] = page;
660 if (unlikely(index == OTI_PVEC_SIZE)) {
661 spin_unlock(&cli->cl_lru_list_lock);
662 discard_pagevec(env, io, pvec, index);
665 spin_lock(&cli->cl_lru_list_lock);
668 if (++count >= target)
671 spin_unlock(&cli->cl_lru_list_lock);
674 discard_pagevec(env, io, pvec, index);
677 cl_object_put(env, clobj);
680 atomic_dec(&cli->cl_lru_shrinkers);
682 atomic_long_add(count, cli->cl_lru_left);
683 wake_up_all(&osc_lru_waitq);
685 RETURN(count > 0 ? count : rc);
689 * Reclaim LRU pages by an IO thread. The caller wants to reclaim at least
690 * \@npages of LRU slots. For performance consideration, it's better to drop
691 * LRU pages in batch. Therefore, the actual number is adjusted at least
694 static long osc_lru_reclaim(struct client_obd *cli, unsigned long npages)
697 struct cl_client_cache *cache = cli->cl_cache;
703 LASSERT(cache != NULL);
705 env = cl_env_get(&refcheck);
709 npages = max_t(int, npages, cli->cl_max_pages_per_rpc);
710 CDEBUG(D_CACHE, "%s: start to reclaim %ld pages from LRU\n",
711 cli_name(cli), npages);
712 rc = osc_lru_shrink(env, cli, npages, true);
714 CDEBUG(D_CACHE, "%s: reclaimed %ld/%ld pages from LRU\n",
715 cli_name(cli), rc, npages);
716 if (osc_cache_too_much(cli) > 0)
717 ptlrpcd_queue_work(cli->cl_lru_work);
723 CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %ld/%ld, want: %ld\n",
724 cli_name(cli), cli, atomic_long_read(&cli->cl_lru_in_list),
725 atomic_long_read(&cli->cl_lru_busy), npages);
727 /* Reclaim LRU slots from other client_obd as it can't free enough
728 * from its own. This should rarely happen. */
729 spin_lock(&cache->ccc_lru_lock);
730 LASSERT(!list_empty(&cache->ccc_lru));
732 cache->ccc_lru_shrinkers++;
733 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
735 max_scans = atomic_read(&cache->ccc_users) - 2;
736 while (--max_scans > 0 && !list_empty(&cache->ccc_lru)) {
737 cli = list_entry(cache->ccc_lru.next, struct client_obd,
740 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %ld, busy: %ld.\n",
742 atomic_long_read(&cli->cl_lru_in_list),
743 atomic_long_read(&cli->cl_lru_busy));
745 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
746 if (osc_cache_too_much(cli) > 0) {
747 spin_unlock(&cache->ccc_lru_lock);
749 rc = osc_lru_shrink(env, cli, npages, true);
750 spin_lock(&cache->ccc_lru_lock);
757 spin_unlock(&cache->ccc_lru_lock);
760 cl_env_put(env, &refcheck);
761 CDEBUG(D_CACHE, "%s: cli %p freed %ld pages.\n",
762 cli_name(cli), cli, rc);
767 * osc_lru_alloc() is called to allocate an LRU slot for a cl_page.
769 * Usually the LRU slots are reserved in osc_io_iter_rw_init().
770 * Only in the case that the LRU slots are in extreme shortage, it should
771 * have reserved enough slots for an IO.
773 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
774 struct osc_page *opg)
776 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
777 struct osc_io *oio = osc_env_io(env);
781 if (cli->cl_cache == NULL) /* shall not be in LRU */
784 if (oio->oi_lru_reserved > 0) {
785 --oio->oi_lru_reserved;
789 LASSERT(atomic_long_read(cli->cl_lru_left) >= 0);
790 while (!atomic_long_add_unless(cli->cl_lru_left, -1, 0)) {
791 /* run out of LRU spaces, try to drop some by itself */
792 rc = osc_lru_reclaim(cli, 1);
799 rc = l_wait_event(osc_lru_waitq,
800 atomic_long_read(cli->cl_lru_left) > 0,
808 atomic_long_inc(&cli->cl_lru_busy);
817 * osc_lru_reserve() is called to reserve enough LRU slots for I/O.
819 * The benefit of doing this is to reduce contention against atomic counter
820 * cl_lru_left by changing it from per-page access to per-IO access.
822 unsigned long osc_lru_reserve(struct client_obd *cli, unsigned long npages)
824 unsigned long reserved = 0;
825 unsigned long max_pages;
828 /* reserve a full RPC window at most to avoid that a thread accidentally
829 * consumes too many LRU slots */
830 max_pages = cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
831 if (npages > max_pages)
834 c = atomic_long_read(cli->cl_lru_left);
835 if (c < npages && osc_lru_reclaim(cli, npages) > 0)
836 c = atomic_long_read(cli->cl_lru_left);
837 while (c >= npages) {
838 if (c == atomic_long_cmpxchg(cli->cl_lru_left, c, c - npages)) {
842 c = atomic_long_read(cli->cl_lru_left);
844 if (atomic_long_read(cli->cl_lru_left) < max_pages) {
845 /* If there aren't enough pages in the per-OSC LRU then
846 * wake up the LRU thread to try and clear out space, so
847 * we don't block if pages are being dirtied quickly. */
848 CDEBUG(D_CACHE, "%s: queue LRU, left: %lu/%ld.\n",
849 cli_name(cli), atomic_long_read(cli->cl_lru_left),
851 (void)ptlrpcd_queue_work(cli->cl_lru_work);
858 * osc_lru_unreserve() is called to unreserve LRU slots.
860 * LRU slots reserved by osc_lru_reserve() may have entries left due to several
861 * reasons such as page already existing or I/O error. Those reserved slots
862 * should be freed by calling this function.
864 void osc_lru_unreserve(struct client_obd *cli, unsigned long npages)
866 atomic_long_add(npages, cli->cl_lru_left);
867 wake_up_all(&osc_lru_waitq);
871 * Atomic operations are expensive. We accumulate the accounting for the
872 * same page zone to get better performance.
873 * In practice this can work pretty good because the pages in the same RPC
874 * are likely from the same page zone.
876 static inline void unstable_page_accounting(struct ptlrpc_bulk_desc *desc,
879 int page_count = desc->bd_iov_count;
884 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
886 for (i = 0; i < page_count; i++) {
887 void *pz = page_zone(BD_GET_KIOV(desc, i).kiov_page);
889 if (likely(pz == zone)) {
895 mod_zone_page_state(zone, NR_UNSTABLE_NFS,
903 mod_zone_page_state(zone, NR_UNSTABLE_NFS, factor * count);
906 static inline void add_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
908 unstable_page_accounting(desc, 1);
911 static inline void dec_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
913 unstable_page_accounting(desc, -1);
917 * Performs "unstable" page accounting. This function balances the
918 * increment operations performed in osc_inc_unstable_pages. It is
919 * registered as the RPC request callback, and is executed when the
920 * bulk RPC is committed on the server. Thus at this point, the pages
921 * involved in the bulk transfer are no longer considered unstable.
923 * If this function is called, the request should have been committed
924 * or req:rq_unstable must have been set; it implies that the unstable
925 * statistic have been added.
927 void osc_dec_unstable_pages(struct ptlrpc_request *req)
929 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
930 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
931 int page_count = desc->bd_iov_count;
934 LASSERT(page_count >= 0);
935 dec_unstable_page_accounting(desc);
937 unstable_count = atomic_long_sub_return(page_count,
938 &cli->cl_unstable_count);
939 LASSERT(unstable_count >= 0);
941 unstable_count = atomic_long_sub_return(page_count,
942 &cli->cl_cache->ccc_unstable_nr);
943 LASSERT(unstable_count >= 0);
944 if (unstable_count == 0)
945 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
947 if (waitqueue_active(&osc_lru_waitq))
948 (void)ptlrpcd_queue_work(cli->cl_lru_work);
952 * "unstable" page accounting. See: osc_dec_unstable_pages.
954 void osc_inc_unstable_pages(struct ptlrpc_request *req)
956 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
957 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
958 long page_count = desc->bd_iov_count;
960 /* No unstable page tracking */
961 if (cli->cl_cache == NULL || !cli->cl_cache->ccc_unstable_check)
964 add_unstable_page_accounting(desc);
965 atomic_long_add(page_count, &cli->cl_unstable_count);
966 atomic_long_add(page_count, &cli->cl_cache->ccc_unstable_nr);
968 /* If the request has already been committed (i.e. brw_commit
969 * called via rq_commit_cb), we need to undo the unstable page
970 * increments we just performed because rq_commit_cb wont be
972 spin_lock(&req->rq_lock);
973 if (unlikely(req->rq_committed)) {
974 spin_unlock(&req->rq_lock);
976 osc_dec_unstable_pages(req);
978 req->rq_unstable = 1;
979 spin_unlock(&req->rq_lock);
984 * Check if it piggybacks SOFT_SYNC flag to OST from this OSC.
985 * This function will be called by every BRW RPC so it's critical
986 * to make this function fast.
988 bool osc_over_unstable_soft_limit(struct client_obd *cli)
990 long unstable_nr, osc_unstable_count;
992 /* Can't check cli->cl_unstable_count, therefore, no soft limit */
993 if (cli->cl_cache == NULL || !cli->cl_cache->ccc_unstable_check)
996 osc_unstable_count = atomic_long_read(&cli->cl_unstable_count);
997 unstable_nr = atomic_long_read(&cli->cl_cache->ccc_unstable_nr);
1000 "%s: cli: %p unstable pages: %lu, osc unstable pages: %lu\n",
1001 cli_name(cli), cli, unstable_nr, osc_unstable_count);
1003 /* If the LRU slots are in shortage - 25% remaining AND this OSC
1004 * has one full RPC window of unstable pages, it's a good chance
1005 * to piggyback a SOFT_SYNC flag.
1006 * Please notice that the OST won't take immediate response for the
1007 * SOFT_SYNC request so active OSCs will have more chance to carry
1008 * the flag, this is reasonable. */
1009 return unstable_nr > cli->cl_cache->ccc_lru_max >> 2 &&
1010 osc_unstable_count > cli->cl_max_pages_per_rpc *
1011 cli->cl_max_rpcs_in_flight;
1015 * Return how many LRU pages in the cache of all OSC devices
1017 * \retval return # of cached LRU pages times reclaimation tendency
1018 * \retval SHRINK_STOP if it cannot do any scanning in this time
1020 unsigned long osc_cache_shrink_count(struct shrinker *sk,
1021 struct shrink_control *sc)
1023 struct client_obd *cli;
1024 unsigned long cached = 0;
1026 spin_lock(&osc_shrink_lock);
1027 list_for_each_entry(cli, &osc_shrink_list, cl_shrink_list)
1028 cached += atomic_long_read(&cli->cl_lru_in_list);
1029 spin_unlock(&osc_shrink_lock);
1031 return (cached * sysctl_vfs_cache_pressure) / 100;
1035 * Scan and try to reclaim sc->nr_to_scan cached LRU pages
1037 * \retval number of cached LRU pages reclaimed
1038 * \retval SHRINK_STOP if it cannot do any scanning in this time
1040 * Linux kernel will loop calling this shrinker scan routine with
1041 * sc->nr_to_scan = SHRINK_BATCH(128 for now) until kernel got enough memory.
1043 * If sc->nr_to_scan is 0, the VM is querying the cache size, we don't need
1044 * to scan and try to reclaim LRU pages, just return 0 and
1045 * osc_cache_shrink_count() will report the LRU page number.
1047 unsigned long osc_cache_shrink_scan(struct shrinker *sk,
1048 struct shrink_control *sc)
1050 struct client_obd *cli;
1051 struct client_obd *stop_anchor = NULL;
1057 if (sc->nr_to_scan == 0)
1060 if (!(sc->gfp_mask & __GFP_FS))
1063 env = cl_env_get(&refcheck);
1067 spin_lock(&osc_shrink_lock);
1068 while (!list_empty(&osc_shrink_list)) {
1069 cli = list_entry(osc_shrink_list.next, struct client_obd,
1072 if (stop_anchor == NULL)
1074 else if (cli == stop_anchor)
1077 list_move_tail(&cli->cl_shrink_list, &osc_shrink_list);
1078 spin_unlock(&osc_shrink_lock);
1080 /* shrink no more than max_pages_per_rpc for an OSC */
1081 rc = osc_lru_shrink(env, cli, (sc->nr_to_scan - shrank) >
1082 cli->cl_max_pages_per_rpc ?
1083 cli->cl_max_pages_per_rpc :
1084 sc->nr_to_scan - shrank, true);
1088 if (shrank >= sc->nr_to_scan)
1091 spin_lock(&osc_shrink_lock);
1093 spin_unlock(&osc_shrink_lock);
1096 cl_env_put(env, &refcheck);