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, 2015, 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
40 #include <linux/math64.h>
41 #include "osc_cl_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);
52 /*****************************************************************************
57 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
59 struct cl_page *page = opg->ops_cl.cpl_page;
61 LASSERT(!opg->ops_transfer_pinned);
63 lu_ref_add_atomic(&page->cp_reference, label, page);
64 opg->ops_transfer_pinned = 1;
67 static void osc_page_transfer_put(const struct lu_env *env,
70 struct cl_page *page = opg->ops_cl.cpl_page;
72 if (opg->ops_transfer_pinned) {
73 opg->ops_transfer_pinned = 0;
74 lu_ref_del(&page->cp_reference, "transfer", page);
75 cl_page_put(env, page);
80 * This is called once for every page when it is submitted for a transfer
81 * either opportunistic (osc_page_cache_add()), or immediate
82 * (osc_page_submit()).
84 static void osc_page_transfer_add(const struct lu_env *env,
85 struct osc_page *opg, enum cl_req_type crt)
87 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
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);
98 osc_page_transfer_get(opg, "transfer\0cache");
99 result = osc_queue_async_io(env, io, opg);
101 osc_page_transfer_put(env, opg);
103 osc_page_transfer_add(env, opg, CRT_WRITE);
108 void osc_index2policy(union ldlm_policy_data *policy,
109 const struct cl_object *obj,
110 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 unsigned long 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: 1< %#x %d %u %s %s > 2< %llu %u %u %#x %#x | %p %p %p > 3< %d %lu %d > 4< %d %d %d %lu %s | %s %s %s %s > 5< %s %s %s %s | %d %s | %d %s %s>\n",
142 oap->oap_magic, oap->oap_cmd,
143 oap->oap_interrupted,
144 osc_list(&oap->oap_pending_item),
145 osc_list(&oap->oap_rpc_item),
147 oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
148 oap->oap_async_flags, oap->oap_brw_flags,
149 oap->oap_request, oap->oap_cli, obj,
151 opg->ops_transfer_pinned,
152 osc_submit_duration(opg), opg->ops_srvlock,
154 cli->cl_r_in_flight, cli->cl_w_in_flight,
155 cli->cl_max_rpcs_in_flight,
157 osc_list(&cli->cl_cache_waiters),
158 osc_list(&cli->cl_loi_ready_list),
159 osc_list(&cli->cl_loi_hp_ready_list),
160 osc_list(&cli->cl_loi_write_list),
161 osc_list(&cli->cl_loi_read_list),
163 osc_list(&obj->oo_ready_item),
164 osc_list(&obj->oo_hp_ready_item),
165 osc_list(&obj->oo_write_item),
166 osc_list(&obj->oo_read_item),
167 atomic_read(&obj->oo_nr_reads),
168 osc_list(&obj->oo_reading_exts),
169 atomic_read(&obj->oo_nr_writes),
170 osc_list(&obj->oo_hp_exts),
171 osc_list(&obj->oo_urgent_exts));
174 static void osc_page_delete(const struct lu_env *env,
175 const struct cl_page_slice *slice)
177 struct osc_page *opg = cl2osc_page(slice);
178 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
181 CDEBUG(D_TRACE, "%p\n", opg);
182 osc_page_transfer_put(env, opg);
183 rc = osc_teardown_async_page(env, obj, opg);
185 CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page,
186 "Trying to teardown failed: %d\n", rc);
190 osc_lru_del(osc_cli(obj), opg);
192 if (slice->cpl_page->cp_type == CPT_CACHEABLE) {
195 spin_lock(&obj->oo_tree_lock);
196 value = radix_tree_delete(&obj->oo_tree, osc_index(opg));
199 spin_unlock(&obj->oo_tree_lock);
201 LASSERT(ergo(value, value == opg));
205 static void osc_page_clip(const struct lu_env *env,
206 const struct cl_page_slice *slice, int from, int to)
208 struct osc_page *opg = cl2osc_page(slice);
209 struct osc_async_page *oap = &opg->ops_oap;
211 opg->ops_from = from;
213 spin_lock(&oap->oap_lock);
214 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
215 spin_unlock(&oap->oap_lock);
218 static int osc_page_cancel(const struct lu_env *env,
219 const struct cl_page_slice *slice)
221 struct osc_page *opg = cl2osc_page(slice);
224 /* Check if the transferring against this page
225 * is completed, or not even queued.
227 if (opg->ops_transfer_pinned)
228 /* FIXME: may not be interrupted.. */
229 rc = osc_cancel_async_page(env, opg);
230 LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
234 static int osc_page_flush(const struct lu_env *env,
235 const struct cl_page_slice *slice,
238 struct osc_page *opg = cl2osc_page(slice);
241 rc = osc_flush_async_page(env, io, opg);
245 static const struct cl_page_operations osc_page_ops = {
246 .cpo_print = osc_page_print,
247 .cpo_delete = osc_page_delete,
248 .cpo_clip = osc_page_clip,
249 .cpo_cancel = osc_page_cancel,
250 .cpo_flush = osc_page_flush
253 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
254 struct cl_page *page, pgoff_t index)
256 struct osc_object *osc = cl2osc(obj);
257 struct osc_page *opg = cl_object_page_slice(obj, page);
261 opg->ops_to = PAGE_SIZE;
263 result = osc_prep_async_page(osc, opg, page->cp_vmpage,
264 cl_offset(obj, index));
266 struct osc_io *oio = osc_env_io(env);
268 opg->ops_srvlock = osc_io_srvlock(oio);
269 cl_page_slice_add(page, &opg->ops_cl, obj, index,
272 INIT_LIST_HEAD(&opg->ops_lru);
274 /* reserve an LRU space for this page */
275 if (page->cp_type == CPT_CACHEABLE && result == 0) {
276 result = osc_lru_alloc(env, osc_cli(osc), opg);
278 spin_lock(&osc->oo_tree_lock);
279 result = radix_tree_insert(&osc->oo_tree, index, opg);
282 spin_unlock(&osc->oo_tree_lock);
283 LASSERT(result == 0);
291 * Helper function called by osc_io_submit() for every page in an immediate
292 * transfer (i.e., transferred synchronously).
294 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
295 enum cl_req_type crt, int brw_flags)
297 struct osc_async_page *oap = &opg->ops_oap;
299 LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, magic 0x%x\n",
300 oap, oap->oap_magic);
301 LASSERT(oap->oap_async_flags & ASYNC_READY);
302 LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
304 oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
305 oap->oap_page_off = opg->ops_from;
306 oap->oap_count = opg->ops_to - opg->ops_from;
307 oap->oap_brw_flags = brw_flags | OBD_BRW_SYNC;
309 if (capable(CFS_CAP_SYS_RESOURCE)) {
310 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
311 oap->oap_cmd |= OBD_BRW_NOQUOTA;
314 opg->ops_submit_time = cfs_time_current();
315 osc_page_transfer_get(opg, "transfer\0imm");
316 osc_page_transfer_add(env, opg, crt);
319 /* --------------- LRU page management ------------------ */
321 /* OSC is a natural place to manage LRU pages as applications are specialized
322 * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
323 * occupy more LRU slots. On the other hand, we should avoid using up all LRU
324 * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
325 * for free LRU slots - this will be very bad so the algorithm requires each
326 * OSC to free slots voluntarily to maintain a reasonable number of free slots
329 static DECLARE_WAIT_QUEUE_HEAD(osc_lru_waitq);
332 * LRU pages are freed in batch mode. OSC should at least free this
333 * number of pages to avoid running out of LRU slots.
335 static inline int lru_shrink_min(struct client_obd *cli)
337 return cli->cl_max_pages_per_rpc * 2;
341 * free this number at most otherwise it will take too long time to finish.
343 static inline int lru_shrink_max(struct client_obd *cli)
345 return cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
349 * Check if we can free LRU slots from this OSC. If there exists LRU waiters,
350 * we should free slots aggressively. In this way, slots are freed in a steady
351 * step to maintain fairness among OSCs.
353 * Return how many LRU pages should be freed.
355 static int osc_cache_too_much(struct client_obd *cli)
357 struct cl_client_cache *cache = cli->cl_cache;
358 long pages = atomic_long_read(&cli->cl_lru_in_list);
359 unsigned long budget;
361 budget = cache->ccc_lru_max / (atomic_read(&cache->ccc_users) - 2);
363 /* if it's going to run out LRU slots, we should free some, but not
364 * too much to maintain fairness among OSCs.
366 if (atomic_long_read(cli->cl_lru_left) < cache->ccc_lru_max >> 2) {
368 return lru_shrink_max(cli);
369 else if (pages >= budget / 2)
370 return lru_shrink_min(cli);
372 time64_t duration = ktime_get_real_seconds();
375 /* knock out pages by duration of no IO activity */
376 duration -= cli->cl_lru_last_used;
378 * The difference shouldn't be more than 70 years
379 * so we can safely case to a long. Round to
380 * approximately 1 minute.
382 timediff = (long)(duration >> 6);
383 if (timediff > 0 && pages >= budget / timediff)
384 return lru_shrink_min(cli);
389 int lru_queue_work(const struct lu_env *env, void *data)
391 struct client_obd *cli = data;
394 CDEBUG(D_CACHE, "%s: run LRU work for client obd\n", cli_name(cli));
396 count = osc_cache_too_much(cli);
398 int rc = osc_lru_shrink(env, cli, count, false);
400 CDEBUG(D_CACHE, "%s: shrank %d/%d pages from client obd\n",
401 cli_name(cli), rc, count);
403 CDEBUG(D_CACHE, "%s: queue again\n", cli_name(cli));
404 ptlrpcd_queue_work(cli->cl_lru_work);
411 void osc_lru_add_batch(struct client_obd *cli, struct list_head *plist)
414 struct osc_async_page *oap;
417 list_for_each_entry(oap, plist, oap_pending_item) {
418 struct osc_page *opg = oap2osc_page(oap);
420 if (!opg->ops_in_lru)
424 LASSERT(list_empty(&opg->ops_lru));
425 list_add(&opg->ops_lru, &lru);
429 spin_lock(&cli->cl_lru_list_lock);
430 list_splice_tail(&lru, &cli->cl_lru_list);
431 atomic_long_sub(npages, &cli->cl_lru_busy);
432 atomic_long_add(npages, &cli->cl_lru_in_list);
433 cli->cl_lru_last_used = ktime_get_real_seconds();
434 spin_unlock(&cli->cl_lru_list_lock);
436 if (waitqueue_active(&osc_lru_waitq))
437 (void)ptlrpcd_queue_work(cli->cl_lru_work);
441 static void __osc_lru_del(struct client_obd *cli, struct osc_page *opg)
443 LASSERT(atomic_long_read(&cli->cl_lru_in_list) > 0);
444 list_del_init(&opg->ops_lru);
445 atomic_long_dec(&cli->cl_lru_in_list);
449 * Page is being destroyed. The page may be not in LRU list, if the transfer
450 * has never finished(error occurred).
452 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg)
454 if (opg->ops_in_lru) {
455 spin_lock(&cli->cl_lru_list_lock);
456 if (!list_empty(&opg->ops_lru)) {
457 __osc_lru_del(cli, opg);
459 LASSERT(atomic_long_read(&cli->cl_lru_busy) > 0);
460 atomic_long_dec(&cli->cl_lru_busy);
462 spin_unlock(&cli->cl_lru_list_lock);
464 atomic_long_inc(cli->cl_lru_left);
465 /* this is a great place to release more LRU pages if
466 * this osc occupies too many LRU pages and kernel is
467 * stealing one of them.
469 if (osc_cache_too_much(cli)) {
470 CDEBUG(D_CACHE, "%s: queue LRU work\n", cli_name(cli));
471 (void)ptlrpcd_queue_work(cli->cl_lru_work);
473 wake_up(&osc_lru_waitq);
475 LASSERT(list_empty(&opg->ops_lru));
480 * Delete page from LRUlist for redirty.
482 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg)
484 /* If page is being transferred for the first time,
485 * ops_lru should be empty
487 if (opg->ops_in_lru && !list_empty(&opg->ops_lru)) {
488 spin_lock(&cli->cl_lru_list_lock);
489 __osc_lru_del(cli, opg);
490 spin_unlock(&cli->cl_lru_list_lock);
491 atomic_long_inc(&cli->cl_lru_busy);
495 static void discard_pagevec(const struct lu_env *env, struct cl_io *io,
496 struct cl_page **pvec, int max_index)
500 for (i = 0; i < max_index; i++) {
501 struct cl_page *page = pvec[i];
503 LASSERT(cl_page_is_owned(page, io));
504 cl_page_delete(env, page);
505 cl_page_discard(env, io, page);
506 cl_page_disown(env, io, page);
507 cl_page_put(env, page);
514 * Check if a cl_page can be released, i.e, it's not being used.
516 * If unstable account is turned on, bulk transfer may hold one refcount
517 * for recovery so we need to check vmpage refcount as well; otherwise,
518 * even we can destroy cl_page but the corresponding vmpage can't be reused.
520 static inline bool lru_page_busy(struct client_obd *cli, struct cl_page *page)
522 if (cl_page_in_use_noref(page))
525 if (cli->cl_cache->ccc_unstable_check) {
526 struct page *vmpage = cl_page_vmpage(page);
528 /* vmpage have two known users: cl_page and VM page cache */
529 if (page_count(vmpage) - page_mapcount(vmpage) > 2)
536 * Drop @target of pages from LRU at most.
538 long osc_lru_shrink(const struct lu_env *env, struct client_obd *cli,
539 long target, bool force)
542 struct cl_object *clobj = NULL;
543 struct cl_page **pvec;
544 struct osc_page *opg;
550 LASSERT(atomic_long_read(&cli->cl_lru_in_list) >= 0);
551 if (atomic_long_read(&cli->cl_lru_in_list) == 0 || target <= 0)
554 CDEBUG(D_CACHE, "%s: shrinkers: %d, force: %d\n",
555 cli_name(cli), atomic_read(&cli->cl_lru_shrinkers), force);
557 if (atomic_read(&cli->cl_lru_shrinkers) > 0)
560 if (atomic_inc_return(&cli->cl_lru_shrinkers) > 1) {
561 atomic_dec(&cli->cl_lru_shrinkers);
565 atomic_inc(&cli->cl_lru_shrinkers);
568 pvec = (struct cl_page **)osc_env_info(env)->oti_pvec;
569 io = &osc_env_info(env)->oti_io;
571 spin_lock(&cli->cl_lru_list_lock);
573 cli->cl_lru_reclaim++;
574 maxscan = min(target << 1, atomic_long_read(&cli->cl_lru_in_list));
575 while (!list_empty(&cli->cl_lru_list)) {
576 struct cl_page *page;
577 bool will_free = false;
579 if (!force && atomic_read(&cli->cl_lru_shrinkers) > 1)
585 opg = list_entry(cli->cl_lru_list.next, struct osc_page,
587 page = opg->ops_cl.cpl_page;
588 if (lru_page_busy(cli, page)) {
589 list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
593 LASSERT(page->cp_obj);
594 if (clobj != page->cp_obj) {
595 struct cl_object *tmp = page->cp_obj;
598 spin_unlock(&cli->cl_lru_list_lock);
601 discard_pagevec(env, io, pvec, index);
605 cl_object_put(env, clobj);
611 io->ci_ignore_layout = 1;
612 rc = cl_io_init(env, io, CIT_MISC, clobj);
614 spin_lock(&cli->cl_lru_list_lock);
623 if (cl_page_own_try(env, io, page) == 0) {
624 if (!lru_page_busy(cli, page)) {
625 /* remove it from lru list earlier to avoid
628 __osc_lru_del(cli, opg);
629 opg->ops_in_lru = 0; /* will be discarded */
634 cl_page_disown(env, io, page);
639 list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
643 /* Don't discard and free the page with cl_lru_list held */
644 pvec[index++] = page;
645 if (unlikely(index == OTI_PVEC_SIZE)) {
646 spin_unlock(&cli->cl_lru_list_lock);
647 discard_pagevec(env, io, pvec, index);
650 spin_lock(&cli->cl_lru_list_lock);
653 if (++count >= target)
656 spin_unlock(&cli->cl_lru_list_lock);
659 discard_pagevec(env, io, pvec, index);
662 cl_object_put(env, clobj);
665 atomic_dec(&cli->cl_lru_shrinkers);
667 atomic_long_add(count, cli->cl_lru_left);
668 wake_up_all(&osc_lru_waitq);
670 return count > 0 ? count : rc;
674 * Reclaim LRU pages by an IO thread. The caller wants to reclaim at least
675 * \@npages of LRU slots. For performance consideration, it's better to drop
676 * LRU pages in batch. Therefore, the actual number is adjusted at least
679 static long osc_lru_reclaim(struct client_obd *cli, unsigned long npages)
682 struct cl_client_cache *cache = cli->cl_cache;
689 env = cl_env_get(&refcheck);
693 npages = max_t(int, npages, cli->cl_max_pages_per_rpc);
694 CDEBUG(D_CACHE, "%s: start to reclaim %ld pages from LRU\n",
695 cli_name(cli), npages);
696 rc = osc_lru_shrink(env, cli, npages, true);
698 CDEBUG(D_CACHE, "%s: reclaimed %ld/%ld pages from LRU\n",
699 cli_name(cli), rc, npages);
700 if (osc_cache_too_much(cli) > 0)
701 ptlrpcd_queue_work(cli->cl_lru_work);
707 CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %ld/%ld, want: %ld\n",
708 cli_name(cli), cli, atomic_long_read(&cli->cl_lru_in_list),
709 atomic_long_read(&cli->cl_lru_busy), npages);
711 /* Reclaim LRU slots from other client_obd as it can't free enough
712 * from its own. This should rarely happen.
714 spin_lock(&cache->ccc_lru_lock);
715 LASSERT(!list_empty(&cache->ccc_lru));
717 cache->ccc_lru_shrinkers++;
718 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
720 max_scans = atomic_read(&cache->ccc_users) - 2;
721 while (--max_scans > 0 && !list_empty(&cache->ccc_lru)) {
722 cli = list_entry(cache->ccc_lru.next, struct client_obd,
725 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %ld, busy: %ld.\n",
727 atomic_long_read(&cli->cl_lru_in_list),
728 atomic_long_read(&cli->cl_lru_busy));
730 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
731 if (osc_cache_too_much(cli) > 0) {
732 spin_unlock(&cache->ccc_lru_lock);
734 rc = osc_lru_shrink(env, cli, npages, true);
735 spin_lock(&cache->ccc_lru_lock);
742 spin_unlock(&cache->ccc_lru_lock);
745 cl_env_put(env, &refcheck);
746 CDEBUG(D_CACHE, "%s: cli %p freed %ld pages.\n",
747 cli_name(cli), cli, rc);
752 * osc_lru_alloc() is called to reserve an LRU slot for a cl_page.
754 * Usually the LRU slots are reserved in osc_io_iter_rw_init().
755 * Only in the case that the LRU slots are in extreme shortage, it should
756 * have reserved enough slots for an IO.
758 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
759 struct osc_page *opg)
761 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
762 struct osc_io *oio = osc_env_io(env);
765 if (!cli->cl_cache) /* shall not be in LRU */
768 if (oio->oi_lru_reserved > 0) {
769 --oio->oi_lru_reserved;
773 LASSERT(atomic_long_read(cli->cl_lru_left) >= 0);
774 while (!atomic_long_add_unless(cli->cl_lru_left, -1, 0)) {
775 /* run out of LRU spaces, try to drop some by itself */
776 rc = osc_lru_reclaim(cli, 1);
784 rc = l_wait_event(osc_lru_waitq,
785 atomic_long_read(cli->cl_lru_left) > 0,
794 atomic_long_inc(&cli->cl_lru_busy);
803 * osc_lru_reserve() is called to reserve enough LRU slots for I/O.
805 * The benefit of doing this is to reduce contention against atomic counter
806 * cl_lru_left by changing it from per-page access to per-IO access.
808 unsigned long osc_lru_reserve(struct client_obd *cli, unsigned long npages)
810 unsigned long reserved = 0;
811 unsigned long max_pages;
815 * reserve a full RPC window at most to avoid that a thread accidentally
816 * consumes too many LRU slots
818 max_pages = cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
819 if (npages > max_pages)
822 c = atomic_long_read(cli->cl_lru_left);
823 if (c < npages && osc_lru_reclaim(cli, npages) > 0)
824 c = atomic_long_read(cli->cl_lru_left);
825 while (c >= npages) {
826 if (c == atomic_long_cmpxchg(cli->cl_lru_left, c, c - npages)) {
830 c = atomic_long_read(cli->cl_lru_left);
832 if (atomic_long_read(cli->cl_lru_left) < max_pages) {
834 * If there aren't enough pages in the per-OSC LRU then
835 * wake up the LRU thread to try and clear out space, so
836 * we don't block if pages are being dirtied quickly.
838 CDEBUG(D_CACHE, "%s: queue LRU, left: %lu/%ld.\n",
839 cli_name(cli), atomic_long_read(cli->cl_lru_left),
841 (void)ptlrpcd_queue_work(cli->cl_lru_work);
848 * osc_lru_unreserve() is called to unreserve LRU slots.
850 * LRU slots reserved by osc_lru_reserve() may have entries left due to several
851 * reasons such as page already existing or I/O error. Those reserved slots
852 * should be freed by calling this function.
854 void osc_lru_unreserve(struct client_obd *cli, unsigned long npages)
856 atomic_long_add(npages, cli->cl_lru_left);
857 wake_up_all(&osc_lru_waitq);
861 * Atomic operations are expensive. We accumulate the accounting for the
862 * same page pgdat to get better performance.
863 * In practice this can work pretty good because the pages in the same RPC
864 * are likely from the same page zone.
866 static inline void unstable_page_accounting(struct ptlrpc_bulk_desc *desc,
869 int page_count = desc->bd_iov_count;
870 pg_data_t *last = NULL;
874 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
876 for (i = 0; i < page_count; i++) {
877 pg_data_t *pgdat = page_pgdat(BD_GET_KIOV(desc, i).bv_page);
879 if (likely(pgdat == last)) {
885 mod_node_page_state(pgdat, NR_UNSTABLE_NFS,
893 mod_node_page_state(last, NR_UNSTABLE_NFS, factor * count);
896 static inline void add_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
898 unstable_page_accounting(desc, 1);
901 static inline void dec_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
903 unstable_page_accounting(desc, -1);
907 * Performs "unstable" page accounting. This function balances the
908 * increment operations performed in osc_inc_unstable_pages. It is
909 * registered as the RPC request callback, and is executed when the
910 * bulk RPC is committed on the server. Thus at this point, the pages
911 * involved in the bulk transfer are no longer considered unstable.
913 * If this function is called, the request should have been committed
914 * or req:rq_unstable must have been set; it implies that the unstable
915 * statistic have been added.
917 void osc_dec_unstable_pages(struct ptlrpc_request *req)
919 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
920 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
921 int page_count = desc->bd_iov_count;
924 LASSERT(page_count >= 0);
925 dec_unstable_page_accounting(desc);
927 unstable_count = atomic_long_sub_return(page_count,
928 &cli->cl_unstable_count);
929 LASSERT(unstable_count >= 0);
931 unstable_count = atomic_long_sub_return(page_count,
932 &cli->cl_cache->ccc_unstable_nr);
933 LASSERT(unstable_count >= 0);
935 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
937 if (waitqueue_active(&osc_lru_waitq))
938 (void)ptlrpcd_queue_work(cli->cl_lru_work);
942 * "unstable" page accounting. See: osc_dec_unstable_pages.
944 void osc_inc_unstable_pages(struct ptlrpc_request *req)
946 struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
947 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
948 long page_count = desc->bd_iov_count;
950 /* No unstable page tracking */
951 if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
954 add_unstable_page_accounting(desc);
955 atomic_long_add(page_count, &cli->cl_unstable_count);
956 atomic_long_add(page_count, &cli->cl_cache->ccc_unstable_nr);
959 * If the request has already been committed (i.e. brw_commit
960 * called via rq_commit_cb), we need to undo the unstable page
961 * increments we just performed because rq_commit_cb wont be
964 spin_lock(&req->rq_lock);
965 if (unlikely(req->rq_committed)) {
966 spin_unlock(&req->rq_lock);
968 osc_dec_unstable_pages(req);
970 req->rq_unstable = 1;
971 spin_unlock(&req->rq_lock);
976 * Check if it piggybacks SOFT_SYNC flag to OST from this OSC.
977 * This function will be called by every BRW RPC so it's critical
978 * to make this function fast.
980 bool osc_over_unstable_soft_limit(struct client_obd *cli)
982 long unstable_nr, osc_unstable_count;
984 /* Can't check cli->cl_unstable_count, therefore, no soft limit */
985 if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
988 osc_unstable_count = atomic_long_read(&cli->cl_unstable_count);
989 unstable_nr = atomic_long_read(&cli->cl_cache->ccc_unstable_nr);
992 "%s: cli: %p unstable pages: %lu, osc unstable pages: %lu\n",
993 cli_name(cli), cli, unstable_nr, osc_unstable_count);
996 * If the LRU slots are in shortage - 25% remaining AND this OSC
997 * has one full RPC window of unstable pages, it's a good chance
998 * to piggyback a SOFT_SYNC flag.
999 * Please notice that the OST won't take immediate response for the
1000 * SOFT_SYNC request so active OSCs will have more chance to carry
1001 * the flag, this is reasonable.
1003 return unstable_nr > cli->cl_cache->ccc_lru_max >> 2 &&
1004 osc_unstable_count > cli->cl_max_pages_per_rpc *
1005 cli->cl_max_rpcs_in_flight;
1009 * Return how many LRU pages in the cache of all OSC devices
1011 * Return: return # of cached LRU pages times reclaimation tendency
1012 * SHRINK_STOP if it cannot do any scanning in this time
1014 unsigned long osc_cache_shrink_count(struct shrinker *sk,
1015 struct shrink_control *sc)
1017 struct client_obd *cli;
1018 unsigned long cached = 0;
1020 spin_lock(&osc_shrink_lock);
1021 list_for_each_entry(cli, &osc_shrink_list, cl_shrink_list)
1022 cached += atomic_long_read(&cli->cl_lru_in_list);
1023 spin_unlock(&osc_shrink_lock);
1025 return (cached * sysctl_vfs_cache_pressure) / 100;
1029 * Scan and try to reclaim sc->nr_to_scan cached LRU pages
1031 * Return: number of cached LRU pages reclaimed
1032 * SHRINK_STOP if it cannot do any scanning in this time
1034 * Linux kernel will loop calling this shrinker scan routine with
1035 * sc->nr_to_scan = SHRINK_BATCH(128 for now) until kernel got enough memory.
1037 * If sc->nr_to_scan is 0, the VM is querying the cache size, we don't need
1038 * to scan and try to reclaim LRU pages, just return 0 and
1039 * osc_cache_shrink_count() will report the LRU page number.
1041 unsigned long osc_cache_shrink_scan(struct shrinker *sk,
1042 struct shrink_control *sc)
1044 struct client_obd *stop_anchor = NULL;
1045 struct client_obd *cli;
1051 if (!sc->nr_to_scan)
1054 if (!(sc->gfp_mask & __GFP_FS))
1057 env = cl_env_get(&refcheck);
1061 spin_lock(&osc_shrink_lock);
1062 while (!list_empty(&osc_shrink_list)) {
1063 cli = list_entry(osc_shrink_list.next, struct client_obd,
1068 else if (cli == stop_anchor)
1071 list_move_tail(&cli->cl_shrink_list, &osc_shrink_list);
1072 spin_unlock(&osc_shrink_lock);
1074 /* shrink no more than max_pages_per_rpc for an OSC */
1075 rc = osc_lru_shrink(env, cli, (sc->nr_to_scan - shrank) >
1076 cli->cl_max_pages_per_rpc ?
1077 cli->cl_max_pages_per_rpc :
1078 sc->nr_to_scan - shrank, true);
1082 if (shrank >= sc->nr_to_scan)
1085 spin_lock(&osc_shrink_lock);
1087 spin_unlock(&osc_shrink_lock);
1090 cl_env_put(env, &refcheck);