GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / lustre / lustre / osc / osc_page.c
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_page for OSC layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSC
39
40 #include <linux/math64.h>
41 #include "osc_cl_internal.h"
42
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);
47
48 /** \addtogroup osc
49  *  @{
50  */
51
52 /*****************************************************************************
53  *
54  * Page operations.
55  *
56  */
57 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
58 {
59         struct cl_page *page = opg->ops_cl.cpl_page;
60
61         LASSERT(!opg->ops_transfer_pinned);
62         cl_page_get(page);
63         lu_ref_add_atomic(&page->cp_reference, label, page);
64         opg->ops_transfer_pinned = 1;
65 }
66
67 static void osc_page_transfer_put(const struct lu_env *env,
68                                   struct osc_page *opg)
69 {
70         struct cl_page *page = opg->ops_cl.cpl_page;
71
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);
76         }
77 }
78
79 /**
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()).
83  */
84 static void osc_page_transfer_add(const struct lu_env *env,
85                                   struct osc_page *opg, enum cl_req_type crt)
86 {
87         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
88
89         osc_lru_use(osc_cli(obj), opg);
90 }
91
92 int osc_page_cache_add(const struct lu_env *env,
93                        const struct cl_page_slice *slice, struct cl_io *io)
94 {
95         struct osc_page *opg = cl2osc_page(slice);
96         int result;
97
98         osc_page_transfer_get(opg, "transfer\0cache");
99         result = osc_queue_async_io(env, io, opg);
100         if (result != 0)
101                 osc_page_transfer_put(env, opg);
102         else
103                 osc_page_transfer_add(env, opg, CRT_WRITE);
104
105         return result;
106 }
107
108 void osc_index2policy(union ldlm_policy_data *policy,
109                       const struct cl_object *obj,
110                       pgoff_t start, pgoff_t end)
111 {
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;
115 }
116
117 static const char *osc_list(struct list_head *head)
118 {
119         return list_empty(head) ? "-" : "+";
120 }
121
122 static inline unsigned long osc_submit_duration(struct osc_page *opg)
123 {
124         if (opg->ops_submit_time == 0)
125                 return 0;
126
127         return (cfs_time_current() - opg->ops_submit_time);
128 }
129
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)
133 {
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;
138
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",
140                           opg, osc_index(opg),
141                           /* 1 */
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),
146                           /* 2 */
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,
150                           /* 3 */
151                           opg->ops_transfer_pinned,
152                           osc_submit_duration(opg), opg->ops_srvlock,
153                           /* 4 */
154                           cli->cl_r_in_flight, cli->cl_w_in_flight,
155                           cli->cl_max_rpcs_in_flight,
156                           cli->cl_avail_grant,
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),
162                           /* 5 */
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));
172 }
173
174 static void osc_page_delete(const struct lu_env *env,
175                             const struct cl_page_slice *slice)
176 {
177         struct osc_page *opg = cl2osc_page(slice);
178         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
179         int rc;
180
181         CDEBUG(D_TRACE, "%p\n", opg);
182         osc_page_transfer_put(env, opg);
183         rc = osc_teardown_async_page(env, obj, opg);
184         if (rc) {
185                 CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page,
186                               "Trying to teardown failed: %d\n", rc);
187                 LASSERT(0);
188         }
189
190         osc_lru_del(osc_cli(obj), opg);
191
192         if (slice->cpl_page->cp_type == CPT_CACHEABLE) {
193                 void *value;
194
195                 spin_lock(&obj->oo_tree_lock);
196                 value = radix_tree_delete(&obj->oo_tree, osc_index(opg));
197                 if (value)
198                         --obj->oo_npages;
199                 spin_unlock(&obj->oo_tree_lock);
200
201                 LASSERT(ergo(value, value == opg));
202         }
203 }
204
205 static void osc_page_clip(const struct lu_env *env,
206                           const struct cl_page_slice *slice, int from, int to)
207 {
208         struct osc_page *opg = cl2osc_page(slice);
209         struct osc_async_page *oap = &opg->ops_oap;
210
211         opg->ops_from = from;
212         opg->ops_to = to;
213         spin_lock(&oap->oap_lock);
214         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
215         spin_unlock(&oap->oap_lock);
216 }
217
218 static int osc_page_cancel(const struct lu_env *env,
219                            const struct cl_page_slice *slice)
220 {
221         struct osc_page *opg = cl2osc_page(slice);
222         int rc = 0;
223
224         /* Check if the transferring against this page
225          * is completed, or not even queued.
226          */
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));
231         return rc;
232 }
233
234 static int osc_page_flush(const struct lu_env *env,
235                           const struct cl_page_slice *slice,
236                           struct cl_io *io)
237 {
238         struct osc_page *opg = cl2osc_page(slice);
239         int rc;
240
241         rc = osc_flush_async_page(env, io, opg);
242         return rc;
243 }
244
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
251 };
252
253 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
254                   struct cl_page *page, pgoff_t index)
255 {
256         struct osc_object *osc = cl2osc(obj);
257         struct osc_page *opg = cl_object_page_slice(obj, page);
258         int result;
259
260         opg->ops_from = 0;
261         opg->ops_to = PAGE_SIZE;
262
263         result = osc_prep_async_page(osc, opg, page->cp_vmpage,
264                                      cl_offset(obj, index));
265         if (result == 0) {
266                 struct osc_io *oio = osc_env_io(env);
267
268                 opg->ops_srvlock = osc_io_srvlock(oio);
269                 cl_page_slice_add(page, &opg->ops_cl, obj, index,
270                                   &osc_page_ops);
271         }
272         INIT_LIST_HEAD(&opg->ops_lru);
273
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);
277                 if (result == 0) {
278                         spin_lock(&osc->oo_tree_lock);
279                         result = radix_tree_insert(&osc->oo_tree, index, opg);
280                         if (result == 0)
281                                 ++osc->oo_npages;
282                         spin_unlock(&osc->oo_tree_lock);
283                         LASSERT(result == 0);
284                 }
285         }
286
287         return result;
288 }
289
290 /**
291  * Helper function called by osc_io_submit() for every page in an immediate
292  * transfer (i.e., transferred synchronously).
293  */
294 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
295                      enum cl_req_type crt, int brw_flags)
296 {
297         struct osc_async_page *oap = &opg->ops_oap;
298
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);
303
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;
308
309         if (capable(CFS_CAP_SYS_RESOURCE)) {
310                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
311                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
312         }
313
314         opg->ops_submit_time = cfs_time_current();
315         osc_page_transfer_get(opg, "transfer\0imm");
316         osc_page_transfer_add(env, opg, crt);
317 }
318
319 /* --------------- LRU page management ------------------ */
320
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
327  * at any time.
328  */
329 static DECLARE_WAIT_QUEUE_HEAD(osc_lru_waitq);
330
331 /**
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.
334  */
335 static inline int lru_shrink_min(struct client_obd *cli)
336 {
337         return cli->cl_max_pages_per_rpc * 2;
338 }
339
340 /**
341  * free this number at most otherwise it will take too long time to finish.
342  */
343 static inline int lru_shrink_max(struct client_obd *cli)
344 {
345         return cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
346 }
347
348 /**
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.
352  *
353  * Return how many LRU pages should be freed.
354  */
355 static int osc_cache_too_much(struct client_obd *cli)
356 {
357         struct cl_client_cache *cache = cli->cl_cache;
358         long pages = atomic_long_read(&cli->cl_lru_in_list);
359         unsigned long budget;
360
361         budget = cache->ccc_lru_max / (atomic_read(&cache->ccc_users) - 2);
362
363         /* if it's going to run out LRU slots, we should free some, but not
364          * too much to maintain fairness among OSCs.
365          */
366         if (atomic_long_read(cli->cl_lru_left) < cache->ccc_lru_max >> 2) {
367                 if (pages >= budget)
368                         return lru_shrink_max(cli);
369                 else if (pages >= budget / 2)
370                         return lru_shrink_min(cli);
371         } else {
372                 time64_t duration = ktime_get_real_seconds();
373                 long timediff;
374
375                 /* knock out pages by duration of no IO activity */
376                 duration -= cli->cl_lru_last_used;
377                 /*
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.
381                  */
382                 timediff = (long)(duration >> 6);
383                 if (timediff > 0 && pages >= budget / timediff)
384                         return lru_shrink_min(cli);
385         }
386         return 0;
387 }
388
389 int lru_queue_work(const struct lu_env *env, void *data)
390 {
391         struct client_obd *cli = data;
392         int count;
393
394         CDEBUG(D_CACHE, "%s: run LRU work for client obd\n", cli_name(cli));
395
396         count = osc_cache_too_much(cli);
397         if (count > 0) {
398                 int rc = osc_lru_shrink(env, cli, count, false);
399
400                 CDEBUG(D_CACHE, "%s: shrank %d/%d pages from client obd\n",
401                        cli_name(cli), rc, count);
402                 if (rc >= count) {
403                         CDEBUG(D_CACHE, "%s: queue again\n", cli_name(cli));
404                         ptlrpcd_queue_work(cli->cl_lru_work);
405                 }
406         }
407
408         return 0;
409 }
410
411 void osc_lru_add_batch(struct client_obd *cli, struct list_head *plist)
412 {
413         LIST_HEAD(lru);
414         struct osc_async_page *oap;
415         long npages = 0;
416
417         list_for_each_entry(oap, plist, oap_pending_item) {
418                 struct osc_page *opg = oap2osc_page(oap);
419
420                 if (!opg->ops_in_lru)
421                         continue;
422
423                 ++npages;
424                 LASSERT(list_empty(&opg->ops_lru));
425                 list_add(&opg->ops_lru, &lru);
426         }
427
428         if (npages > 0) {
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);
435
436                 if (waitqueue_active(&osc_lru_waitq))
437                         (void)ptlrpcd_queue_work(cli->cl_lru_work);
438         }
439 }
440
441 static void __osc_lru_del(struct client_obd *cli, struct osc_page *opg)
442 {
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);
446 }
447
448 /**
449  * Page is being destroyed. The page may be not in LRU list, if the transfer
450  * has never finished(error occurred).
451  */
452 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg)
453 {
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);
458                 } else {
459                         LASSERT(atomic_long_read(&cli->cl_lru_busy) > 0);
460                         atomic_long_dec(&cli->cl_lru_busy);
461                 }
462                 spin_unlock(&cli->cl_lru_list_lock);
463
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.
468                  */
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);
472                 }
473                 wake_up(&osc_lru_waitq);
474         } else {
475                 LASSERT(list_empty(&opg->ops_lru));
476         }
477 }
478
479 /**
480  * Delete page from LRUlist for redirty.
481  */
482 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg)
483 {
484         /* If page is being transferred for the first time,
485          * ops_lru should be empty
486          */
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);
492         }
493 }
494
495 static void discard_pagevec(const struct lu_env *env, struct cl_io *io,
496                             struct cl_page **pvec, int max_index)
497 {
498         int i;
499
500         for (i = 0; i < max_index; i++) {
501                 struct cl_page *page = pvec[i];
502
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);
508
509                 pvec[i] = NULL;
510         }
511 }
512
513 /**
514  * Check if a cl_page can be released, i.e, it's not being used.
515  *
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.
519  */
520 static inline bool lru_page_busy(struct client_obd *cli, struct cl_page *page)
521 {
522         if (cl_page_in_use_noref(page))
523                 return true;
524
525         if (cli->cl_cache->ccc_unstable_check) {
526                 struct page *vmpage = cl_page_vmpage(page);
527
528                 /* vmpage have two known users: cl_page and VM page cache */
529                 if (page_count(vmpage) - page_mapcount(vmpage) > 2)
530                         return true;
531         }
532         return false;
533 }
534
535 /**
536  * Drop @target of pages from LRU at most.
537  */
538 long osc_lru_shrink(const struct lu_env *env, struct client_obd *cli,
539                     long target, bool force)
540 {
541         struct cl_io *io;
542         struct cl_object *clobj = NULL;
543         struct cl_page **pvec;
544         struct osc_page *opg;
545         int maxscan = 0;
546         long count = 0;
547         int index = 0;
548         int rc = 0;
549
550         LASSERT(atomic_long_read(&cli->cl_lru_in_list) >= 0);
551         if (atomic_long_read(&cli->cl_lru_in_list) == 0 || target <= 0)
552                 return 0;
553
554         CDEBUG(D_CACHE, "%s: shrinkers: %d, force: %d\n",
555                cli_name(cli), atomic_read(&cli->cl_lru_shrinkers), force);
556         if (!force) {
557                 if (atomic_read(&cli->cl_lru_shrinkers) > 0)
558                         return -EBUSY;
559
560                 if (atomic_inc_return(&cli->cl_lru_shrinkers) > 1) {
561                         atomic_dec(&cli->cl_lru_shrinkers);
562                         return -EBUSY;
563                 }
564         } else {
565                 atomic_inc(&cli->cl_lru_shrinkers);
566         }
567
568         pvec = (struct cl_page **)osc_env_info(env)->oti_pvec;
569         io = &osc_env_info(env)->oti_io;
570
571         spin_lock(&cli->cl_lru_list_lock);
572         if (force)
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;
578
579                 if (!force && atomic_read(&cli->cl_lru_shrinkers) > 1)
580                         break;
581
582                 if (--maxscan < 0)
583                         break;
584
585                 opg = list_entry(cli->cl_lru_list.next, struct osc_page,
586                                  ops_lru);
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);
590                         continue;
591                 }
592
593                 LASSERT(page->cp_obj);
594                 if (clobj != page->cp_obj) {
595                         struct cl_object *tmp = page->cp_obj;
596
597                         cl_object_get(tmp);
598                         spin_unlock(&cli->cl_lru_list_lock);
599
600                         if (clobj) {
601                                 discard_pagevec(env, io, pvec, index);
602                                 index = 0;
603
604                                 cl_io_fini(env, io);
605                                 cl_object_put(env, clobj);
606                                 clobj = NULL;
607                         }
608
609                         clobj = tmp;
610                         io->ci_obj = clobj;
611                         io->ci_ignore_layout = 1;
612                         rc = cl_io_init(env, io, CIT_MISC, clobj);
613
614                         spin_lock(&cli->cl_lru_list_lock);
615
616                         if (rc != 0)
617                                 break;
618
619                         ++maxscan;
620                         continue;
621                 }
622
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
626                                  * lock contention
627                                  */
628                                 __osc_lru_del(cli, opg);
629                                 opg->ops_in_lru = 0; /* will be discarded */
630
631                                 cl_page_get(page);
632                                 will_free = true;
633                         } else {
634                                 cl_page_disown(env, io, page);
635                         }
636                 }
637
638                 if (!will_free) {
639                         list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
640                         continue;
641                 }
642
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);
648                         index = 0;
649
650                         spin_lock(&cli->cl_lru_list_lock);
651                 }
652
653                 if (++count >= target)
654                         break;
655         }
656         spin_unlock(&cli->cl_lru_list_lock);
657
658         if (clobj) {
659                 discard_pagevec(env, io, pvec, index);
660
661                 cl_io_fini(env, io);
662                 cl_object_put(env, clobj);
663         }
664
665         atomic_dec(&cli->cl_lru_shrinkers);
666         if (count > 0) {
667                 atomic_long_add(count, cli->cl_lru_left);
668                 wake_up_all(&osc_lru_waitq);
669         }
670         return count > 0 ? count : rc;
671 }
672
673 /**
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
677  * max_pages_per_rpc.
678  */
679 static long osc_lru_reclaim(struct client_obd *cli, unsigned long npages)
680 {
681         struct lu_env *env;
682         struct cl_client_cache *cache = cli->cl_cache;
683         int max_scans;
684         u16 refcheck;
685         long rc = 0;
686
687         LASSERT(cache);
688
689         env = cl_env_get(&refcheck);
690         if (IS_ERR(env))
691                 return 0;
692
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);
697         if (rc >= npages) {
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);
702                 goto out;
703         } else if (rc > 0) {
704                 npages -= rc;
705         }
706
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);
710
711         /* Reclaim LRU slots from other client_obd as it can't free enough
712          * from its own. This should rarely happen.
713          */
714         spin_lock(&cache->ccc_lru_lock);
715         LASSERT(!list_empty(&cache->ccc_lru));
716
717         cache->ccc_lru_shrinkers++;
718         list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
719
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,
723                                  cl_lru_osc);
724
725                 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %ld, busy: %ld.\n",
726                        cli_name(cli), cli,
727                        atomic_long_read(&cli->cl_lru_in_list),
728                        atomic_long_read(&cli->cl_lru_busy));
729
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);
733
734                         rc = osc_lru_shrink(env, cli, npages, true);
735                         spin_lock(&cache->ccc_lru_lock);
736                         if (rc >= npages)
737                                 break;
738                         if (rc > 0)
739                                 npages -= rc;
740                 }
741         }
742         spin_unlock(&cache->ccc_lru_lock);
743
744 out:
745         cl_env_put(env, &refcheck);
746         CDEBUG(D_CACHE, "%s: cli %p freed %ld pages.\n",
747                cli_name(cli), cli, rc);
748         return rc;
749 }
750
751 /**
752  * osc_lru_alloc() is called to reserve an LRU slot for a cl_page.
753  *
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.
757  */
758 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
759                          struct osc_page *opg)
760 {
761         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
762         struct osc_io *oio = osc_env_io(env);
763         int rc = 0;
764
765         if (!cli->cl_cache) /* shall not be in LRU */
766                 return 0;
767
768         if (oio->oi_lru_reserved > 0) {
769                 --oio->oi_lru_reserved;
770                 goto out;
771         }
772
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);
777                 if (rc < 0)
778                         break;
779                 if (rc > 0)
780                         continue;
781
782                 cond_resched();
783
784                 rc = l_wait_event(osc_lru_waitq,
785                                   atomic_long_read(cli->cl_lru_left) > 0,
786                                   &lwi);
787
788                 if (rc < 0)
789                         break;
790         }
791
792 out:
793         if (rc >= 0) {
794                 atomic_long_inc(&cli->cl_lru_busy);
795                 opg->ops_in_lru = 1;
796                 rc = 0;
797         }
798
799         return rc;
800 }
801
802 /**
803  * osc_lru_reserve() is called to reserve enough LRU slots for I/O.
804  *
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.
807  */
808 unsigned long osc_lru_reserve(struct client_obd *cli, unsigned long npages)
809 {
810         unsigned long reserved = 0;
811         unsigned long max_pages;
812         unsigned long c;
813
814         /*
815          * reserve a full RPC window at most to avoid that a thread accidentally
816          * consumes too many LRU slots
817          */
818         max_pages = cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
819         if (npages > max_pages)
820                 npages = max_pages;
821
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)) {
827                         reserved = npages;
828                         break;
829                 }
830                 c = atomic_long_read(cli->cl_lru_left);
831         }
832         if (atomic_long_read(cli->cl_lru_left) < max_pages) {
833                 /*
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.
837                  */
838                 CDEBUG(D_CACHE, "%s: queue LRU, left: %lu/%ld.\n",
839                        cli_name(cli), atomic_long_read(cli->cl_lru_left),
840                        max_pages);
841                 (void)ptlrpcd_queue_work(cli->cl_lru_work);
842         }
843
844         return reserved;
845 }
846
847 /**
848  * osc_lru_unreserve() is called to unreserve LRU slots.
849  *
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.
853  */
854 void osc_lru_unreserve(struct client_obd *cli, unsigned long npages)
855 {
856         atomic_long_add(npages, cli->cl_lru_left);
857         wake_up_all(&osc_lru_waitq);
858 }
859
860 /**
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.
865  */
866 static inline void unstable_page_accounting(struct ptlrpc_bulk_desc *desc,
867                                             int factor)
868 {
869         int page_count = desc->bd_iov_count;
870         pg_data_t *last = NULL;
871         int count = 0;
872         int i;
873
874         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
875
876         for (i = 0; i < page_count; i++) {
877                 pg_data_t *pgdat = page_pgdat(BD_GET_KIOV(desc, i).bv_page);
878
879                 if (likely(pgdat == last)) {
880                         ++count;
881                         continue;
882                 }
883
884                 if (count > 0) {
885                         mod_node_page_state(pgdat, NR_UNSTABLE_NFS,
886                                             factor * count);
887                         count = 0;
888                 }
889                 last = pgdat;
890                 ++count;
891         }
892         if (count > 0)
893                 mod_node_page_state(last, NR_UNSTABLE_NFS, factor * count);
894 }
895
896 static inline void add_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
897 {
898         unstable_page_accounting(desc, 1);
899 }
900
901 static inline void dec_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
902 {
903         unstable_page_accounting(desc, -1);
904 }
905
906 /**
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.
912  *
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.
916  */
917 void osc_dec_unstable_pages(struct ptlrpc_request *req)
918 {
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;
922         long unstable_count;
923
924         LASSERT(page_count >= 0);
925         dec_unstable_page_accounting(desc);
926
927         unstable_count = atomic_long_sub_return(page_count,
928                                                 &cli->cl_unstable_count);
929         LASSERT(unstable_count >= 0);
930
931         unstable_count = atomic_long_sub_return(page_count,
932                                                 &cli->cl_cache->ccc_unstable_nr);
933         LASSERT(unstable_count >= 0);
934         if (!unstable_count)
935                 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
936
937         if (waitqueue_active(&osc_lru_waitq))
938                 (void)ptlrpcd_queue_work(cli->cl_lru_work);
939 }
940
941 /**
942  * "unstable" page accounting. See: osc_dec_unstable_pages.
943  */
944 void osc_inc_unstable_pages(struct ptlrpc_request *req)
945 {
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;
949
950         /* No unstable page tracking */
951         if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
952                 return;
953
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);
957
958         /*
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
962          * called again.
963          */
964         spin_lock(&req->rq_lock);
965         if (unlikely(req->rq_committed)) {
966                 spin_unlock(&req->rq_lock);
967
968                 osc_dec_unstable_pages(req);
969         } else {
970                 req->rq_unstable = 1;
971                 spin_unlock(&req->rq_lock);
972         }
973 }
974
975 /**
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.
979  */
980 bool osc_over_unstable_soft_limit(struct client_obd *cli)
981 {
982         long unstable_nr, osc_unstable_count;
983
984         /* Can't check cli->cl_unstable_count, therefore, no soft limit */
985         if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
986                 return false;
987
988         osc_unstable_count = atomic_long_read(&cli->cl_unstable_count);
989         unstable_nr = atomic_long_read(&cli->cl_cache->ccc_unstable_nr);
990
991         CDEBUG(D_CACHE,
992                "%s: cli: %p unstable pages: %lu, osc unstable pages: %lu\n",
993                cli_name(cli), cli, unstable_nr, osc_unstable_count);
994
995         /*
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.
1002          */
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;
1006 }
1007
1008 /**
1009  * Return how many LRU pages in the cache of all OSC devices
1010  *
1011  * Return:      return # of cached LRU pages times reclaimation tendency
1012  *              SHRINK_STOP if it cannot do any scanning in this time
1013  */
1014 unsigned long osc_cache_shrink_count(struct shrinker *sk,
1015                                      struct shrink_control *sc)
1016 {
1017         struct client_obd *cli;
1018         unsigned long cached = 0;
1019
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);
1024
1025         return (cached  * sysctl_vfs_cache_pressure) / 100;
1026 }
1027
1028 /**
1029  * Scan and try to reclaim sc->nr_to_scan cached LRU pages
1030  *
1031  * Return:      number of cached LRU pages reclaimed
1032  *              SHRINK_STOP if it cannot do any scanning in this time
1033  *
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.
1036  *
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.
1040  */
1041 unsigned long osc_cache_shrink_scan(struct shrinker *sk,
1042                                     struct shrink_control *sc)
1043 {
1044         struct client_obd *stop_anchor = NULL;
1045         struct client_obd *cli;
1046         struct lu_env *env;
1047         long shrank = 0;
1048         u16 refcheck;
1049         int rc;
1050
1051         if (!sc->nr_to_scan)
1052                 return 0;
1053
1054         if (!(sc->gfp_mask & __GFP_FS))
1055                 return SHRINK_STOP;
1056
1057         env = cl_env_get(&refcheck);
1058         if (IS_ERR(env))
1059                 return SHRINK_STOP;
1060
1061         spin_lock(&osc_shrink_lock);
1062         while (!list_empty(&osc_shrink_list)) {
1063                 cli = list_entry(osc_shrink_list.next, struct client_obd,
1064                                  cl_shrink_list);
1065
1066                 if (!stop_anchor)
1067                         stop_anchor = cli;
1068                 else if (cli == stop_anchor)
1069                         break;
1070
1071                 list_move_tail(&cli->cl_shrink_list, &osc_shrink_list);
1072                 spin_unlock(&osc_shrink_lock);
1073
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);
1079                 if (rc > 0)
1080                         shrank += rc;
1081
1082                 if (shrank >= sc->nr_to_scan)
1083                         goto out;
1084
1085                 spin_lock(&osc_shrink_lock);
1086         }
1087         spin_unlock(&osc_shrink_lock);
1088
1089 out:
1090         cl_env_put(env, &refcheck);
1091
1092         return shrank;
1093 }
1094
1095 /** @} osc */