vmscan, memcg: do softlimit reclaim also for targeted reclaim
[platform/adaptation/renesas_rcar/renesas_kernel.git] / include / linux / memcontrol.h
1 /* memcontrol.h - Memory Controller
2  *
3  * Copyright IBM Corporation, 2007
4  * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5  *
6  * Copyright 2007 OpenVZ SWsoft Inc
7  * Author: Pavel Emelianov <xemul@openvz.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 #ifndef _LINUX_MEMCONTROL_H
21 #define _LINUX_MEMCONTROL_H
22 #include <linux/cgroup.h>
23 #include <linux/vm_event_item.h>
24 #include <linux/hardirq.h>
25 #include <linux/jump_label.h>
26
27 struct mem_cgroup;
28 struct page_cgroup;
29 struct page;
30 struct mm_struct;
31 struct kmem_cache;
32
33 /* Stats that can be updated by kernel. */
34 enum mem_cgroup_page_stat_item {
35         MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
36 };
37
38 struct mem_cgroup_reclaim_cookie {
39         struct zone *zone;
40         int priority;
41         unsigned int generation;
42 };
43
44 #ifdef CONFIG_MEMCG
45 /*
46  * All "charge" functions with gfp_mask should use GFP_KERNEL or
47  * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
48  * alloc memory but reclaims memory from all available zones. So, "where I want
49  * memory from" bits of gfp_mask has no meaning. So any bits of that field is
50  * available but adding a rule is better. charge functions' gfp_mask should
51  * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
52  * codes.
53  * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
54  */
55
56 extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
57                                 gfp_t gfp_mask);
58 /* for swap handling */
59 extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
60                 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
61 extern void mem_cgroup_commit_charge_swapin(struct page *page,
62                                         struct mem_cgroup *memcg);
63 extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
64
65 extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
66                                         gfp_t gfp_mask);
67
68 struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
69 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
70
71 /* For coalescing uncharge for reducing memcg' overhead*/
72 extern void mem_cgroup_uncharge_start(void);
73 extern void mem_cgroup_uncharge_end(void);
74
75 extern void mem_cgroup_uncharge_page(struct page *page);
76 extern void mem_cgroup_uncharge_cache_page(struct page *page);
77
78 bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
79                                   struct mem_cgroup *memcg);
80 bool task_in_mem_cgroup(struct task_struct *task,
81                         const struct mem_cgroup *memcg);
82
83 extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
84 extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
85 extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
86
87 extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
88 extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
89
90 static inline
91 bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
92 {
93         struct mem_cgroup *task_memcg;
94         bool match;
95
96         rcu_read_lock();
97         task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
98         match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
99         rcu_read_unlock();
100         return match;
101 }
102
103 extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
104
105 extern void
106 mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
107                              struct mem_cgroup **memcgp);
108 extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
109         struct page *oldpage, struct page *newpage, bool migration_ok);
110
111 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
112                                    struct mem_cgroup *,
113                                    struct mem_cgroup_reclaim_cookie *);
114 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
115
116 /*
117  * For memory reclaim.
118  */
119 int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
120 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
121 unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
122 void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
123 extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
124                                         struct task_struct *p);
125 extern void mem_cgroup_replace_page_cache(struct page *oldpage,
126                                         struct page *newpage);
127
128 #ifdef CONFIG_MEMCG_SWAP
129 extern int do_swap_account;
130 #endif
131
132 static inline bool mem_cgroup_disabled(void)
133 {
134         if (mem_cgroup_subsys.disabled)
135                 return true;
136         return false;
137 }
138
139 void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
140                                          unsigned long *flags);
141
142 extern atomic_t memcg_moving;
143
144 static inline void mem_cgroup_begin_update_page_stat(struct page *page,
145                                         bool *locked, unsigned long *flags)
146 {
147         if (mem_cgroup_disabled())
148                 return;
149         rcu_read_lock();
150         *locked = false;
151         if (atomic_read(&memcg_moving))
152                 __mem_cgroup_begin_update_page_stat(page, locked, flags);
153 }
154
155 void __mem_cgroup_end_update_page_stat(struct page *page,
156                                 unsigned long *flags);
157 static inline void mem_cgroup_end_update_page_stat(struct page *page,
158                                         bool *locked, unsigned long *flags)
159 {
160         if (mem_cgroup_disabled())
161                 return;
162         if (*locked)
163                 __mem_cgroup_end_update_page_stat(page, flags);
164         rcu_read_unlock();
165 }
166
167 void mem_cgroup_update_page_stat(struct page *page,
168                                  enum mem_cgroup_page_stat_item idx,
169                                  int val);
170
171 static inline void mem_cgroup_inc_page_stat(struct page *page,
172                                             enum mem_cgroup_page_stat_item idx)
173 {
174         mem_cgroup_update_page_stat(page, idx, 1);
175 }
176
177 static inline void mem_cgroup_dec_page_stat(struct page *page,
178                                             enum mem_cgroup_page_stat_item idx)
179 {
180         mem_cgroup_update_page_stat(page, idx, -1);
181 }
182
183 bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
184                 struct mem_cgroup *root);
185
186 void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
187 static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
188                                              enum vm_event_item idx)
189 {
190         if (mem_cgroup_disabled())
191                 return;
192         __mem_cgroup_count_vm_event(mm, idx);
193 }
194 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
195 void mem_cgroup_split_huge_fixup(struct page *head);
196 #endif
197
198 #ifdef CONFIG_DEBUG_VM
199 bool mem_cgroup_bad_page_check(struct page *page);
200 void mem_cgroup_print_bad_page(struct page *page);
201 #endif
202 #else /* CONFIG_MEMCG */
203 struct mem_cgroup;
204
205 static inline int mem_cgroup_newpage_charge(struct page *page,
206                                         struct mm_struct *mm, gfp_t gfp_mask)
207 {
208         return 0;
209 }
210
211 static inline int mem_cgroup_cache_charge(struct page *page,
212                                         struct mm_struct *mm, gfp_t gfp_mask)
213 {
214         return 0;
215 }
216
217 static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
218                 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
219 {
220         return 0;
221 }
222
223 static inline void mem_cgroup_commit_charge_swapin(struct page *page,
224                                           struct mem_cgroup *memcg)
225 {
226 }
227
228 static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
229 {
230 }
231
232 static inline void mem_cgroup_uncharge_start(void)
233 {
234 }
235
236 static inline void mem_cgroup_uncharge_end(void)
237 {
238 }
239
240 static inline void mem_cgroup_uncharge_page(struct page *page)
241 {
242 }
243
244 static inline void mem_cgroup_uncharge_cache_page(struct page *page)
245 {
246 }
247
248 static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
249                                                     struct mem_cgroup *memcg)
250 {
251         return &zone->lruvec;
252 }
253
254 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
255                                                     struct zone *zone)
256 {
257         return &zone->lruvec;
258 }
259
260 static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
261 {
262         return NULL;
263 }
264
265 static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
266 {
267         return NULL;
268 }
269
270 static inline bool mm_match_cgroup(struct mm_struct *mm,
271                 struct mem_cgroup *memcg)
272 {
273         return true;
274 }
275
276 static inline bool task_in_mem_cgroup(struct task_struct *task,
277                                       const struct mem_cgroup *memcg)
278 {
279         return true;
280 }
281
282 static inline struct cgroup_subsys_state
283                 *mem_cgroup_css(struct mem_cgroup *memcg)
284 {
285         return NULL;
286 }
287
288 static inline void
289 mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
290                              struct mem_cgroup **memcgp)
291 {
292 }
293
294 static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
295                 struct page *oldpage, struct page *newpage, bool migration_ok)
296 {
297 }
298
299 static inline struct mem_cgroup *
300 mem_cgroup_iter(struct mem_cgroup *root,
301                 struct mem_cgroup *prev,
302                 struct mem_cgroup_reclaim_cookie *reclaim)
303 {
304         return NULL;
305 }
306
307 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
308                                          struct mem_cgroup *prev)
309 {
310 }
311
312 static inline bool mem_cgroup_disabled(void)
313 {
314         return true;
315 }
316
317 static inline int
318 mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
319 {
320         return 1;
321 }
322
323 static inline unsigned long
324 mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
325 {
326         return 0;
327 }
328
329 static inline void
330 mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
331                               int increment)
332 {
333 }
334
335 static inline void
336 mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
337 {
338 }
339
340 static inline void mem_cgroup_begin_update_page_stat(struct page *page,
341                                         bool *locked, unsigned long *flags)
342 {
343 }
344
345 static inline void mem_cgroup_end_update_page_stat(struct page *page,
346                                         bool *locked, unsigned long *flags)
347 {
348 }
349
350 static inline void mem_cgroup_inc_page_stat(struct page *page,
351                                             enum mem_cgroup_page_stat_item idx)
352 {
353 }
354
355 static inline void mem_cgroup_dec_page_stat(struct page *page,
356                                             enum mem_cgroup_page_stat_item idx)
357 {
358 }
359
360 static inline
361 bool mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
362                 struct mem_cgroup *root)
363 {
364         return false;
365 }
366
367 static inline void mem_cgroup_split_huge_fixup(struct page *head)
368 {
369 }
370
371 static inline
372 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
373 {
374 }
375 static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
376                                 struct page *newpage)
377 {
378 }
379 #endif /* CONFIG_MEMCG */
380
381 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
382 static inline bool
383 mem_cgroup_bad_page_check(struct page *page)
384 {
385         return false;
386 }
387
388 static inline void
389 mem_cgroup_print_bad_page(struct page *page)
390 {
391 }
392 #endif
393
394 enum {
395         UNDER_LIMIT,
396         SOFT_LIMIT,
397         OVER_LIMIT,
398 };
399
400 struct sock;
401 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
402 void sock_update_memcg(struct sock *sk);
403 void sock_release_memcg(struct sock *sk);
404 #else
405 static inline void sock_update_memcg(struct sock *sk)
406 {
407 }
408 static inline void sock_release_memcg(struct sock *sk)
409 {
410 }
411 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
412
413 #ifdef CONFIG_MEMCG_KMEM
414 extern struct static_key memcg_kmem_enabled_key;
415
416 extern int memcg_limited_groups_array_size;
417
418 /*
419  * Helper macro to loop through all memcg-specific caches. Callers must still
420  * check if the cache is valid (it is either valid or NULL).
421  * the slab_mutex must be held when looping through those caches
422  */
423 #define for_each_memcg_cache_index(_idx)        \
424         for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
425
426 static inline bool memcg_kmem_enabled(void)
427 {
428         return static_key_false(&memcg_kmem_enabled_key);
429 }
430
431 /*
432  * In general, we'll do everything in our power to not incur in any overhead
433  * for non-memcg users for the kmem functions. Not even a function call, if we
434  * can avoid it.
435  *
436  * Therefore, we'll inline all those functions so that in the best case, we'll
437  * see that kmemcg is off for everybody and proceed quickly.  If it is on,
438  * we'll still do most of the flag checking inline. We check a lot of
439  * conditions, but because they are pretty simple, they are expected to be
440  * fast.
441  */
442 bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
443                                         int order);
444 void __memcg_kmem_commit_charge(struct page *page,
445                                        struct mem_cgroup *memcg, int order);
446 void __memcg_kmem_uncharge_pages(struct page *page, int order);
447
448 int memcg_cache_id(struct mem_cgroup *memcg);
449 int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
450                          struct kmem_cache *root_cache);
451 void memcg_release_cache(struct kmem_cache *cachep);
452 void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
453
454 int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
455 void memcg_update_array_size(int num_groups);
456
457 struct kmem_cache *
458 __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
459
460 void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
461 void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
462
463 /**
464  * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
465  * @gfp: the gfp allocation flags.
466  * @memcg: a pointer to the memcg this was charged against.
467  * @order: allocation order.
468  *
469  * returns true if the memcg where the current task belongs can hold this
470  * allocation.
471  *
472  * We return true automatically if this allocation is not to be accounted to
473  * any memcg.
474  */
475 static inline bool
476 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
477 {
478         if (!memcg_kmem_enabled())
479                 return true;
480
481         /*
482          * __GFP_NOFAIL allocations will move on even if charging is not
483          * possible. Therefore we don't even try, and have this allocation
484          * unaccounted. We could in theory charge it with
485          * res_counter_charge_nofail, but we hope those allocations are rare,
486          * and won't be worth the trouble.
487          */
488         if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
489                 return true;
490         if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
491                 return true;
492
493         /* If the test is dying, just let it go. */
494         if (unlikely(fatal_signal_pending(current)))
495                 return true;
496
497         return __memcg_kmem_newpage_charge(gfp, memcg, order);
498 }
499
500 /**
501  * memcg_kmem_uncharge_pages: uncharge pages from memcg
502  * @page: pointer to struct page being freed
503  * @order: allocation order.
504  *
505  * there is no need to specify memcg here, since it is embedded in page_cgroup
506  */
507 static inline void
508 memcg_kmem_uncharge_pages(struct page *page, int order)
509 {
510         if (memcg_kmem_enabled())
511                 __memcg_kmem_uncharge_pages(page, order);
512 }
513
514 /**
515  * memcg_kmem_commit_charge: embeds correct memcg in a page
516  * @page: pointer to struct page recently allocated
517  * @memcg: the memcg structure we charged against
518  * @order: allocation order.
519  *
520  * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
521  * failure of the allocation. if @page is NULL, this function will revert the
522  * charges. Otherwise, it will commit the memcg given by @memcg to the
523  * corresponding page_cgroup.
524  */
525 static inline void
526 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
527 {
528         if (memcg_kmem_enabled() && memcg)
529                 __memcg_kmem_commit_charge(page, memcg, order);
530 }
531
532 /**
533  * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
534  * @cachep: the original global kmem cache
535  * @gfp: allocation flags.
536  *
537  * This function assumes that the task allocating, which determines the memcg
538  * in the page allocator, belongs to the same cgroup throughout the whole
539  * process.  Misacounting can happen if the task calls memcg_kmem_get_cache()
540  * while belonging to a cgroup, and later on changes. This is considered
541  * acceptable, and should only happen upon task migration.
542  *
543  * Before the cache is created by the memcg core, there is also a possible
544  * imbalance: the task belongs to a memcg, but the cache being allocated from
545  * is the global cache, since the child cache is not yet guaranteed to be
546  * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
547  * passed and the page allocator will not attempt any cgroup accounting.
548  */
549 static __always_inline struct kmem_cache *
550 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
551 {
552         if (!memcg_kmem_enabled())
553                 return cachep;
554         if (gfp & __GFP_NOFAIL)
555                 return cachep;
556         if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
557                 return cachep;
558         if (unlikely(fatal_signal_pending(current)))
559                 return cachep;
560
561         return __memcg_kmem_get_cache(cachep, gfp);
562 }
563 #else
564 #define for_each_memcg_cache_index(_idx)        \
565         for (; NULL; )
566
567 static inline bool memcg_kmem_enabled(void)
568 {
569         return false;
570 }
571
572 static inline bool
573 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
574 {
575         return true;
576 }
577
578 static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
579 {
580 }
581
582 static inline void
583 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
584 {
585 }
586
587 static inline int memcg_cache_id(struct mem_cgroup *memcg)
588 {
589         return -1;
590 }
591
592 static inline int
593 memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
594                      struct kmem_cache *root_cache)
595 {
596         return 0;
597 }
598
599 static inline void memcg_release_cache(struct kmem_cache *cachep)
600 {
601 }
602
603 static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
604                                         struct kmem_cache *s)
605 {
606 }
607
608 static inline struct kmem_cache *
609 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
610 {
611         return cachep;
612 }
613
614 static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
615 {
616 }
617 #endif /* CONFIG_MEMCG_KMEM */
618 #endif /* _LINUX_MEMCONTROL_H */
619