Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright IBM Corporation, 2012 |
| 4 | * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> |
| 5 | * |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 6 | * Cgroup v2 |
| 7 | * Copyright (C) 2019 Red Hat, Inc. |
| 8 | * Author: Giuseppe Scrivano <gscrivan@redhat.com> |
| 9 | * |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of version 2.1 of the GNU Lesser General Public License |
| 12 | * as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it would be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/cgroup.h> |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 21 | #include <linux/page_counter.h> |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/hugetlb.h> |
| 24 | #include <linux/hugetlb_cgroup.h> |
| 25 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 26 | enum hugetlb_memory_event { |
| 27 | HUGETLB_MAX, |
| 28 | HUGETLB_NR_MEMORY_EVENTS, |
| 29 | }; |
| 30 | |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 31 | struct hugetlb_cgroup { |
| 32 | struct cgroup_subsys_state css; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 33 | |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 34 | /* |
| 35 | * the counter to account for hugepages from hugetlb. |
| 36 | */ |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 37 | struct page_counter hugepage[HUGE_MAX_HSTATE]; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 38 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 39 | /* |
| 40 | * the counter to account for hugepage reservations from hugetlb. |
| 41 | */ |
| 42 | struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE]; |
| 43 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 44 | atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; |
| 45 | atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; |
| 46 | |
| 47 | /* Handle for "hugetlb.events" */ |
| 48 | struct cgroup_file events_file[HUGE_MAX_HSTATE]; |
| 49 | |
| 50 | /* Handle for "hugetlb.events.local" */ |
| 51 | struct cgroup_file events_local_file[HUGE_MAX_HSTATE]; |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 54 | #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) |
| 55 | #define MEMFILE_IDX(val) (((val) >> 16) & 0xffff) |
| 56 | #define MEMFILE_ATTR(val) ((val) & 0xffff) |
| 57 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 58 | #define hugetlb_cgroup_from_counter(counter, idx) \ |
| 59 | container_of(counter, struct hugetlb_cgroup, hugepage[idx]) |
| 60 | |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 61 | static struct hugetlb_cgroup *root_h_cgroup __read_mostly; |
| 62 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 63 | static inline struct page_counter * |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 64 | __hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx, |
| 65 | bool rsvd) |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 66 | { |
| 67 | if (rsvd) |
| 68 | return &h_cg->rsvd_hugepage[idx]; |
| 69 | return &h_cg->hugepage[idx]; |
| 70 | } |
| 71 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 72 | static inline struct page_counter * |
| 73 | hugetlb_cgroup_counter_from_cgroup(struct hugetlb_cgroup *h_cg, int idx) |
| 74 | { |
| 75 | return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, false); |
| 76 | } |
| 77 | |
| 78 | static inline struct page_counter * |
| 79 | hugetlb_cgroup_counter_from_cgroup_rsvd(struct hugetlb_cgroup *h_cg, int idx) |
| 80 | { |
| 81 | return __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, true); |
| 82 | } |
| 83 | |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 84 | static inline |
| 85 | struct hugetlb_cgroup *hugetlb_cgroup_from_css(struct cgroup_subsys_state *s) |
| 86 | { |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 87 | return s ? container_of(s, struct hugetlb_cgroup, css) : NULL; |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static inline |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 91 | struct hugetlb_cgroup *hugetlb_cgroup_from_task(struct task_struct *task) |
| 92 | { |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 93 | return hugetlb_cgroup_from_css(task_css(task, hugetlb_cgrp_id)); |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static inline bool hugetlb_cgroup_is_root(struct hugetlb_cgroup *h_cg) |
| 97 | { |
| 98 | return (h_cg == root_h_cgroup); |
| 99 | } |
| 100 | |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 101 | static inline struct hugetlb_cgroup * |
| 102 | parent_hugetlb_cgroup(struct hugetlb_cgroup *h_cg) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 103 | { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 104 | return hugetlb_cgroup_from_css(h_cg->css.parent); |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 107 | static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 108 | { |
| 109 | int idx; |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 110 | |
| 111 | for (idx = 0; idx < hugetlb_max_hstate; idx++) { |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 112 | if (page_counter_read( |
| 113 | hugetlb_cgroup_counter_from_cgroup(h_cg, idx)) || |
| 114 | page_counter_read(hugetlb_cgroup_counter_from_cgroup_rsvd( |
| 115 | h_cg, idx))) { |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 116 | return true; |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 117 | } |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 122 | static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup, |
| 123 | struct hugetlb_cgroup *parent_h_cgroup) |
| 124 | { |
| 125 | int idx; |
| 126 | |
| 127 | for (idx = 0; idx < HUGE_MAX_HSTATE; idx++) { |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 128 | struct page_counter *fault_parent = NULL; |
| 129 | struct page_counter *rsvd_parent = NULL; |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 130 | unsigned long limit; |
| 131 | int ret; |
| 132 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 133 | if (parent_h_cgroup) { |
| 134 | fault_parent = hugetlb_cgroup_counter_from_cgroup( |
| 135 | parent_h_cgroup, idx); |
| 136 | rsvd_parent = hugetlb_cgroup_counter_from_cgroup_rsvd( |
| 137 | parent_h_cgroup, idx); |
| 138 | } |
| 139 | page_counter_init(hugetlb_cgroup_counter_from_cgroup(h_cgroup, |
| 140 | idx), |
| 141 | fault_parent); |
| 142 | page_counter_init( |
| 143 | hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx), |
| 144 | rsvd_parent); |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 145 | |
| 146 | limit = round_down(PAGE_COUNTER_MAX, |
| 147 | 1 << huge_page_order(&hstates[idx])); |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 148 | |
| 149 | ret = page_counter_set_max( |
| 150 | hugetlb_cgroup_counter_from_cgroup(h_cgroup, idx), |
| 151 | limit); |
| 152 | VM_BUG_ON(ret); |
| 153 | ret = page_counter_set_max( |
| 154 | hugetlb_cgroup_counter_from_cgroup_rsvd(h_cgroup, idx), |
| 155 | limit); |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 156 | VM_BUG_ON(ret); |
| 157 | } |
| 158 | } |
| 159 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 160 | static struct cgroup_subsys_state * |
| 161 | hugetlb_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 162 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 163 | struct hugetlb_cgroup *parent_h_cgroup = hugetlb_cgroup_from_css(parent_css); |
| 164 | struct hugetlb_cgroup *h_cgroup; |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 165 | |
| 166 | h_cgroup = kzalloc(sizeof(*h_cgroup), GFP_KERNEL); |
| 167 | if (!h_cgroup) |
| 168 | return ERR_PTR(-ENOMEM); |
| 169 | |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 170 | if (!parent_h_cgroup) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 171 | root_h_cgroup = h_cgroup; |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 172 | |
| 173 | hugetlb_cgroup_init(h_cgroup, parent_h_cgroup); |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 174 | return &h_cgroup->css; |
| 175 | } |
| 176 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 177 | static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 178 | { |
| 179 | struct hugetlb_cgroup *h_cgroup; |
| 180 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 181 | h_cgroup = hugetlb_cgroup_from_css(css); |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 182 | kfree(h_cgroup); |
| 183 | } |
| 184 | |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 185 | /* |
| 186 | * Should be called with hugetlb_lock held. |
| 187 | * Since we are holding hugetlb_lock, pages cannot get moved from |
| 188 | * active list or uncharged from the cgroup, So no need to get |
| 189 | * page reference and test for page active here. This function |
| 190 | * cannot fail. |
| 191 | */ |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 192 | static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg, |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 193 | struct page *page) |
| 194 | { |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 195 | unsigned int nr_pages; |
| 196 | struct page_counter *counter; |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 197 | struct hugetlb_cgroup *page_hcg; |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 198 | struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 199 | |
| 200 | page_hcg = hugetlb_cgroup_from_page(page); |
| 201 | /* |
| 202 | * We can have pages in active list without any cgroup |
| 203 | * ie, hugepage with less than 3 pages. We can safely |
| 204 | * ignore those pages. |
| 205 | */ |
| 206 | if (!page_hcg || page_hcg != h_cg) |
| 207 | goto out; |
| 208 | |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 209 | nr_pages = compound_nr(page); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 210 | if (!parent) { |
| 211 | parent = root_h_cgroup; |
| 212 | /* root has no limit */ |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 213 | page_counter_charge(&parent->hugepage[idx], nr_pages); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 214 | } |
| 215 | counter = &h_cg->hugepage[idx]; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 216 | /* Take the pages off the local counter */ |
| 217 | page_counter_cancel(counter, nr_pages); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 218 | |
| 219 | set_hugetlb_cgroup(page, parent); |
| 220 | out: |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Force the hugetlb cgroup to empty the hugetlb resources by moving them to |
| 226 | * the parent cgroup. |
| 227 | */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 228 | static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css) |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 229 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 230 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 231 | struct hstate *h; |
| 232 | struct page *page; |
Michal Hocko | 9d093cb | 2012-10-26 13:37:33 +0200 | [diff] [blame] | 233 | int idx = 0; |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 234 | |
| 235 | do { |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 236 | for_each_hstate(h) { |
| 237 | spin_lock(&hugetlb_lock); |
| 238 | list_for_each_entry(page, &h->hugepage_activelist, lru) |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 239 | hugetlb_cgroup_move_parent(idx, h_cg, page); |
Aneesh Kumar K.V | da1def5 | 2012-07-31 16:42:21 -0700 | [diff] [blame] | 240 | |
| 241 | spin_unlock(&hugetlb_lock); |
| 242 | idx++; |
| 243 | } |
| 244 | cond_resched(); |
Tejun Heo | 3f79851 | 2013-08-08 20:11:22 -0400 | [diff] [blame] | 245 | } while (hugetlb_cgroup_have_usage(h_cg)); |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 248 | static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx, |
| 249 | enum hugetlb_memory_event event) |
| 250 | { |
| 251 | atomic_long_inc(&hugetlb->events_local[idx][event]); |
| 252 | cgroup_file_notify(&hugetlb->events_local_file[idx]); |
| 253 | |
| 254 | do { |
| 255 | atomic_long_inc(&hugetlb->events[idx][event]); |
| 256 | cgroup_file_notify(&hugetlb->events_file[idx]); |
| 257 | } while ((hugetlb = parent_hugetlb_cgroup(hugetlb)) && |
| 258 | !hugetlb_cgroup_is_root(hugetlb)); |
| 259 | } |
| 260 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 261 | static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, |
| 262 | struct hugetlb_cgroup **ptr, |
| 263 | bool rsvd) |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 264 | { |
| 265 | int ret = 0; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 266 | struct page_counter *counter; |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 267 | struct hugetlb_cgroup *h_cg = NULL; |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 268 | |
| 269 | if (hugetlb_cgroup_disabled()) |
| 270 | goto done; |
| 271 | /* |
| 272 | * We don't charge any cgroup if the compound page have less |
| 273 | * than 3 pages. |
| 274 | */ |
| 275 | if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER) |
| 276 | goto done; |
| 277 | again: |
| 278 | rcu_read_lock(); |
| 279 | h_cg = hugetlb_cgroup_from_task(current); |
Roman Gushchin | 0362f32 | 2019-11-15 17:34:46 -0800 | [diff] [blame] | 280 | if (!css_tryget(&h_cg->css)) { |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 281 | rcu_read_unlock(); |
| 282 | goto again; |
| 283 | } |
| 284 | rcu_read_unlock(); |
| 285 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 286 | if (!page_counter_try_charge( |
| 287 | __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd), |
| 288 | nr_pages, &counter)) { |
Johannes Weiner | 6071ca5 | 2015-11-05 18:50:26 -0800 | [diff] [blame] | 289 | ret = -ENOMEM; |
Mina Almasry | 726b7bb | 2020-03-28 19:17:22 -0700 | [diff] [blame] | 290 | hugetlb_event(h_cg, idx, HUGETLB_MAX); |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 291 | css_put(&h_cg->css); |
| 292 | goto done; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 293 | } |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 294 | /* Reservations take a reference to the css because they do not get |
| 295 | * reparented. |
| 296 | */ |
| 297 | if (!rsvd) |
| 298 | css_put(&h_cg->css); |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 299 | done: |
| 300 | *ptr = h_cg; |
| 301 | return ret; |
| 302 | } |
| 303 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 304 | int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, |
| 305 | struct hugetlb_cgroup **ptr) |
| 306 | { |
| 307 | return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, false); |
| 308 | } |
| 309 | |
| 310 | int hugetlb_cgroup_charge_cgroup_rsvd(int idx, unsigned long nr_pages, |
| 311 | struct hugetlb_cgroup **ptr) |
| 312 | { |
| 313 | return __hugetlb_cgroup_charge_cgroup(idx, nr_pages, ptr, true); |
| 314 | } |
| 315 | |
Aneesh Kumar K.V | 94ae8ba | 2012-07-31 16:42:35 -0700 | [diff] [blame] | 316 | /* Should be called with hugetlb_lock held */ |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 317 | static void __hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, |
| 318 | struct hugetlb_cgroup *h_cg, |
| 319 | struct page *page, bool rsvd) |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 320 | { |
| 321 | if (hugetlb_cgroup_disabled() || !h_cg) |
| 322 | return; |
| 323 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 324 | __set_hugetlb_cgroup(page, h_cg, rsvd); |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 328 | void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages, |
| 329 | struct hugetlb_cgroup *h_cg, |
| 330 | struct page *page) |
| 331 | { |
| 332 | __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, false); |
| 333 | } |
| 334 | |
| 335 | void hugetlb_cgroup_commit_charge_rsvd(int idx, unsigned long nr_pages, |
| 336 | struct hugetlb_cgroup *h_cg, |
| 337 | struct page *page) |
| 338 | { |
| 339 | __hugetlb_cgroup_commit_charge(idx, nr_pages, h_cg, page, true); |
| 340 | } |
| 341 | |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 342 | /* |
| 343 | * Should be called with hugetlb_lock held |
| 344 | */ |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 345 | static void __hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages, |
| 346 | struct page *page, bool rsvd) |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 347 | { |
| 348 | struct hugetlb_cgroup *h_cg; |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 349 | |
| 350 | if (hugetlb_cgroup_disabled()) |
| 351 | return; |
Michal Hocko | 7ea8574 | 2014-08-29 15:18:42 -0700 | [diff] [blame] | 352 | lockdep_assert_held(&hugetlb_lock); |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 353 | h_cg = __hugetlb_cgroup_from_page(page, rsvd); |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 354 | if (unlikely(!h_cg)) |
| 355 | return; |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 356 | __set_hugetlb_cgroup(page, NULL, rsvd); |
| 357 | |
| 358 | page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx, |
| 359 | rsvd), |
| 360 | nr_pages); |
| 361 | |
| 362 | if (rsvd) |
| 363 | css_put(&h_cg->css); |
| 364 | |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 365 | return; |
| 366 | } |
| 367 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 368 | void hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages, |
| 369 | struct page *page) |
| 370 | { |
| 371 | __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, false); |
| 372 | } |
| 373 | |
| 374 | void hugetlb_cgroup_uncharge_page_rsvd(int idx, unsigned long nr_pages, |
| 375 | struct page *page) |
| 376 | { |
| 377 | __hugetlb_cgroup_uncharge_page(idx, nr_pages, page, true); |
| 378 | } |
| 379 | |
| 380 | static void __hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, |
| 381 | struct hugetlb_cgroup *h_cg, |
| 382 | bool rsvd) |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 383 | { |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 384 | if (hugetlb_cgroup_disabled() || !h_cg) |
| 385 | return; |
| 386 | |
| 387 | if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER) |
| 388 | return; |
| 389 | |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 390 | page_counter_uncharge(__hugetlb_cgroup_counter_from_cgroup(h_cg, idx, |
| 391 | rsvd), |
| 392 | nr_pages); |
| 393 | |
| 394 | if (rsvd) |
| 395 | css_put(&h_cg->css); |
| 396 | } |
| 397 | |
| 398 | void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, |
| 399 | struct hugetlb_cgroup *h_cg) |
| 400 | { |
| 401 | __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, false); |
| 402 | } |
| 403 | |
| 404 | void hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages, |
| 405 | struct hugetlb_cgroup *h_cg) |
| 406 | { |
| 407 | __hugetlb_cgroup_uncharge_cgroup(idx, nr_pages, h_cg, true); |
| 408 | } |
| 409 | |
| 410 | void hugetlb_cgroup_uncharge_counter(struct page_counter *p, |
| 411 | unsigned long nr_pages, |
| 412 | struct cgroup_subsys_state *css) |
| 413 | { |
| 414 | if (hugetlb_cgroup_disabled() || !p || !css) |
| 415 | return; |
| 416 | |
| 417 | page_counter_uncharge(p, nr_pages); |
| 418 | css_put(css); |
Aneesh Kumar K.V | 6d76dcf | 2012-07-31 16:42:18 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 421 | enum { |
| 422 | RES_USAGE, |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 423 | RES_RSVD_USAGE, |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 424 | RES_LIMIT, |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 425 | RES_RSVD_LIMIT, |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 426 | RES_MAX_USAGE, |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 427 | RES_RSVD_MAX_USAGE, |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 428 | RES_FAILCNT, |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 429 | RES_RSVD_FAILCNT, |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 430 | }; |
| 431 | |
Tejun Heo | 716f479 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 432 | static u64 hugetlb_cgroup_read_u64(struct cgroup_subsys_state *css, |
| 433 | struct cftype *cft) |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 434 | { |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 435 | struct page_counter *counter; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 436 | struct page_counter *rsvd_counter; |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 437 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css); |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 438 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 439 | counter = &h_cg->hugepage[MEMFILE_IDX(cft->private)]; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 440 | rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(cft->private)]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 441 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 442 | switch (MEMFILE_ATTR(cft->private)) { |
| 443 | case RES_USAGE: |
| 444 | return (u64)page_counter_read(counter) * PAGE_SIZE; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 445 | case RES_RSVD_USAGE: |
| 446 | return (u64)page_counter_read(rsvd_counter) * PAGE_SIZE; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 447 | case RES_LIMIT: |
Roman Gushchin | bbec2e1 | 2018-06-07 17:06:18 -0700 | [diff] [blame] | 448 | return (u64)counter->max * PAGE_SIZE; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 449 | case RES_RSVD_LIMIT: |
| 450 | return (u64)rsvd_counter->max * PAGE_SIZE; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 451 | case RES_MAX_USAGE: |
| 452 | return (u64)counter->watermark * PAGE_SIZE; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 453 | case RES_RSVD_MAX_USAGE: |
| 454 | return (u64)rsvd_counter->watermark * PAGE_SIZE; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 455 | case RES_FAILCNT: |
| 456 | return counter->failcnt; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 457 | case RES_RSVD_FAILCNT: |
| 458 | return rsvd_counter->failcnt; |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 459 | default: |
| 460 | BUG(); |
| 461 | } |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 464 | static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v) |
| 465 | { |
| 466 | int idx; |
| 467 | u64 val; |
| 468 | struct cftype *cft = seq_cft(seq); |
| 469 | unsigned long limit; |
| 470 | struct page_counter *counter; |
| 471 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq)); |
| 472 | |
| 473 | idx = MEMFILE_IDX(cft->private); |
| 474 | counter = &h_cg->hugepage[idx]; |
| 475 | |
| 476 | limit = round_down(PAGE_COUNTER_MAX, |
| 477 | 1 << huge_page_order(&hstates[idx])); |
| 478 | |
| 479 | switch (MEMFILE_ATTR(cft->private)) { |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 480 | case RES_RSVD_USAGE: |
| 481 | counter = &h_cg->rsvd_hugepage[idx]; |
| 482 | /* Fall through. */ |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 483 | case RES_USAGE: |
| 484 | val = (u64)page_counter_read(counter); |
| 485 | seq_printf(seq, "%llu\n", val * PAGE_SIZE); |
| 486 | break; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 487 | case RES_RSVD_LIMIT: |
| 488 | counter = &h_cg->rsvd_hugepage[idx]; |
| 489 | /* Fall through. */ |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 490 | case RES_LIMIT: |
| 491 | val = (u64)counter->max; |
| 492 | if (val == limit) |
| 493 | seq_puts(seq, "max\n"); |
| 494 | else |
| 495 | seq_printf(seq, "%llu\n", val * PAGE_SIZE); |
| 496 | break; |
| 497 | default: |
| 498 | BUG(); |
| 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 504 | static DEFINE_MUTEX(hugetlb_limit_mutex); |
| 505 | |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 506 | static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of, |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 507 | char *buf, size_t nbytes, loff_t off, |
| 508 | const char *max) |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 509 | { |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 510 | int ret, idx; |
| 511 | unsigned long nr_pages; |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 512 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of)); |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 513 | bool rsvd = false; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 514 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 515 | if (hugetlb_cgroup_is_root(h_cg)) /* Can't set limit on root */ |
| 516 | return -EINVAL; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 517 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 518 | buf = strstrip(buf); |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 519 | ret = page_counter_memparse(buf, max, &nr_pages); |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 520 | if (ret) |
| 521 | return ret; |
| 522 | |
| 523 | idx = MEMFILE_IDX(of_cft(of)->private); |
David Rientjes | 297880f | 2016-05-20 16:57:50 -0700 | [diff] [blame] | 524 | nr_pages = round_down(nr_pages, 1 << huge_page_order(&hstates[idx])); |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 525 | |
| 526 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 527 | case RES_RSVD_LIMIT: |
| 528 | rsvd = true; |
| 529 | /* Fall through. */ |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 530 | case RES_LIMIT: |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 531 | mutex_lock(&hugetlb_limit_mutex); |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 532 | ret = page_counter_set_max( |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 533 | __hugetlb_cgroup_counter_from_cgroup(h_cg, idx, rsvd), |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 534 | nr_pages); |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 535 | mutex_unlock(&hugetlb_limit_mutex); |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 536 | break; |
| 537 | default: |
| 538 | ret = -EINVAL; |
| 539 | break; |
| 540 | } |
Tejun Heo | 451af50 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 541 | return ret ?: nbytes; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 544 | static ssize_t hugetlb_cgroup_write_legacy(struct kernfs_open_file *of, |
| 545 | char *buf, size_t nbytes, loff_t off) |
| 546 | { |
| 547 | return hugetlb_cgroup_write(of, buf, nbytes, off, "-1"); |
| 548 | } |
| 549 | |
| 550 | static ssize_t hugetlb_cgroup_write_dfl(struct kernfs_open_file *of, |
| 551 | char *buf, size_t nbytes, loff_t off) |
| 552 | { |
| 553 | return hugetlb_cgroup_write(of, buf, nbytes, off, "max"); |
| 554 | } |
| 555 | |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 556 | static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of, |
| 557 | char *buf, size_t nbytes, loff_t off) |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 558 | { |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 559 | int ret = 0; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 560 | struct page_counter *counter, *rsvd_counter; |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 561 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(of_css(of)); |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 562 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 563 | counter = &h_cg->hugepage[MEMFILE_IDX(of_cft(of)->private)]; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 564 | rsvd_counter = &h_cg->rsvd_hugepage[MEMFILE_IDX(of_cft(of)->private)]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 565 | |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 566 | switch (MEMFILE_ATTR(of_cft(of)->private)) { |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 567 | case RES_MAX_USAGE: |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 568 | page_counter_reset_watermark(counter); |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 569 | break; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 570 | case RES_RSVD_MAX_USAGE: |
| 571 | page_counter_reset_watermark(rsvd_counter); |
| 572 | break; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 573 | case RES_FAILCNT: |
Johannes Weiner | 71f87bee | 2014-12-10 15:42:34 -0800 | [diff] [blame] | 574 | counter->failcnt = 0; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 575 | break; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 576 | case RES_RSVD_FAILCNT: |
| 577 | rsvd_counter->failcnt = 0; |
| 578 | break; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 579 | default: |
| 580 | ret = -EINVAL; |
| 581 | break; |
| 582 | } |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 583 | return ret ?: nbytes; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | static char *mem_fmt(char *buf, int size, unsigned long hsize) |
| 587 | { |
| 588 | if (hsize >= (1UL << 30)) |
| 589 | snprintf(buf, size, "%luGB", hsize >> 30); |
| 590 | else if (hsize >= (1UL << 20)) |
| 591 | snprintf(buf, size, "%luMB", hsize >> 20); |
| 592 | else |
| 593 | snprintf(buf, size, "%luKB", hsize >> 10); |
| 594 | return buf; |
| 595 | } |
| 596 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 597 | static int __hugetlb_events_show(struct seq_file *seq, bool local) |
| 598 | { |
| 599 | int idx; |
| 600 | long max; |
| 601 | struct cftype *cft = seq_cft(seq); |
| 602 | struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(seq_css(seq)); |
| 603 | |
| 604 | idx = MEMFILE_IDX(cft->private); |
| 605 | |
| 606 | if (local) |
| 607 | max = atomic_long_read(&h_cg->events_local[idx][HUGETLB_MAX]); |
| 608 | else |
| 609 | max = atomic_long_read(&h_cg->events[idx][HUGETLB_MAX]); |
| 610 | |
| 611 | seq_printf(seq, "max %lu\n", max); |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int hugetlb_events_show(struct seq_file *seq, void *v) |
| 617 | { |
| 618 | return __hugetlb_events_show(seq, false); |
| 619 | } |
| 620 | |
| 621 | static int hugetlb_events_local_show(struct seq_file *seq, void *v) |
| 622 | { |
| 623 | return __hugetlb_events_show(seq, true); |
| 624 | } |
| 625 | |
| 626 | static void __init __hugetlb_cgroup_file_dfl_init(int idx) |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 627 | { |
| 628 | char buf[32]; |
| 629 | struct cftype *cft; |
| 630 | struct hstate *h = &hstates[idx]; |
| 631 | |
| 632 | /* format the size */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 633 | mem_fmt(buf, sizeof(buf), huge_page_size(h)); |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 634 | |
| 635 | /* Add the limit file */ |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 636 | cft = &h->cgroup_files_dfl[0]; |
| 637 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max", buf); |
| 638 | cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT); |
| 639 | cft->seq_show = hugetlb_cgroup_read_u64_max; |
| 640 | cft->write = hugetlb_cgroup_write_dfl; |
| 641 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 642 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 643 | /* Add the reservation limit file */ |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 644 | cft = &h->cgroup_files_dfl[1]; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 645 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max", buf); |
| 646 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT); |
| 647 | cft->seq_show = hugetlb_cgroup_read_u64_max; |
| 648 | cft->write = hugetlb_cgroup_write_dfl; |
| 649 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 650 | |
| 651 | /* Add the current usage file */ |
| 652 | cft = &h->cgroup_files_dfl[2]; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 653 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.current", buf); |
| 654 | cft->private = MEMFILE_PRIVATE(idx, RES_USAGE); |
| 655 | cft->seq_show = hugetlb_cgroup_read_u64_max; |
| 656 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 657 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 658 | /* Add the current reservation usage file */ |
| 659 | cft = &h->cgroup_files_dfl[3]; |
| 660 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.current", buf); |
| 661 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE); |
| 662 | cft->seq_show = hugetlb_cgroup_read_u64_max; |
| 663 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 664 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 665 | /* Add the events file */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 666 | cft = &h->cgroup_files_dfl[4]; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 667 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events", buf); |
| 668 | cft->private = MEMFILE_PRIVATE(idx, 0); |
| 669 | cft->seq_show = hugetlb_events_show; |
| 670 | cft->file_offset = offsetof(struct hugetlb_cgroup, events_file[idx]), |
| 671 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 672 | |
| 673 | /* Add the events.local file */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 674 | cft = &h->cgroup_files_dfl[5]; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 675 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.events.local", buf); |
| 676 | cft->private = MEMFILE_PRIVATE(idx, 0); |
| 677 | cft->seq_show = hugetlb_events_local_show; |
| 678 | cft->file_offset = offsetof(struct hugetlb_cgroup, |
| 679 | events_local_file[idx]), |
| 680 | cft->flags = CFTYPE_NOT_ON_ROOT; |
| 681 | |
| 682 | /* NULL terminate the last cft */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 683 | cft = &h->cgroup_files_dfl[6]; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 684 | memset(cft, 0, sizeof(*cft)); |
| 685 | |
| 686 | WARN_ON(cgroup_add_dfl_cftypes(&hugetlb_cgrp_subsys, |
| 687 | h->cgroup_files_dfl)); |
| 688 | } |
| 689 | |
| 690 | static void __init __hugetlb_cgroup_file_legacy_init(int idx) |
| 691 | { |
| 692 | char buf[32]; |
| 693 | struct cftype *cft; |
| 694 | struct hstate *h = &hstates[idx]; |
| 695 | |
| 696 | /* format the size */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 697 | mem_fmt(buf, sizeof(buf), huge_page_size(h)); |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 698 | |
| 699 | /* Add the limit file */ |
| 700 | cft = &h->cgroup_files_legacy[0]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 701 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.limit_in_bytes", buf); |
| 702 | cft->private = MEMFILE_PRIVATE(idx, RES_LIMIT); |
Tejun Heo | 716f479 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 703 | cft->read_u64 = hugetlb_cgroup_read_u64; |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 704 | cft->write = hugetlb_cgroup_write_legacy; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 705 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 706 | /* Add the reservation limit file */ |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 707 | cft = &h->cgroup_files_legacy[1]; |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 708 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.limit_in_bytes", buf); |
| 709 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_LIMIT); |
| 710 | cft->read_u64 = hugetlb_cgroup_read_u64; |
| 711 | cft->write = hugetlb_cgroup_write_legacy; |
| 712 | |
| 713 | /* Add the usage file */ |
| 714 | cft = &h->cgroup_files_legacy[2]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 715 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.usage_in_bytes", buf); |
| 716 | cft->private = MEMFILE_PRIVATE(idx, RES_USAGE); |
Tejun Heo | 716f479 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 717 | cft->read_u64 = hugetlb_cgroup_read_u64; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 718 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 719 | /* Add the reservation usage file */ |
| 720 | cft = &h->cgroup_files_legacy[3]; |
| 721 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.usage_in_bytes", buf); |
| 722 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_USAGE); |
| 723 | cft->read_u64 = hugetlb_cgroup_read_u64; |
| 724 | |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 725 | /* Add the MAX usage file */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 726 | cft = &h->cgroup_files_legacy[4]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 727 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.max_usage_in_bytes", buf); |
| 728 | cft->private = MEMFILE_PRIVATE(idx, RES_MAX_USAGE); |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 729 | cft->write = hugetlb_cgroup_reset; |
Tejun Heo | 716f479 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 730 | cft->read_u64 = hugetlb_cgroup_read_u64; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 731 | |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 732 | /* Add the MAX reservation usage file */ |
| 733 | cft = &h->cgroup_files_legacy[5]; |
| 734 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.max_usage_in_bytes", buf); |
| 735 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_MAX_USAGE); |
| 736 | cft->write = hugetlb_cgroup_reset; |
| 737 | cft->read_u64 = hugetlb_cgroup_read_u64; |
| 738 | |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 739 | /* Add the failcntfile */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 740 | cft = &h->cgroup_files_legacy[6]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 741 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.failcnt", buf); |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 742 | cft->private = MEMFILE_PRIVATE(idx, RES_FAILCNT); |
| 743 | cft->write = hugetlb_cgroup_reset; |
| 744 | cft->read_u64 = hugetlb_cgroup_read_u64; |
| 745 | |
| 746 | /* Add the reservation failcntfile */ |
| 747 | cft = &h->cgroup_files_legacy[7]; |
| 748 | snprintf(cft->name, MAX_CFTYPE_NAME, "%s.rsvd.failcnt", buf); |
| 749 | cft->private = MEMFILE_PRIVATE(idx, RES_RSVD_FAILCNT); |
Tejun Heo | 6770c64 | 2014-05-13 12:16:21 -0400 | [diff] [blame] | 750 | cft->write = hugetlb_cgroup_reset; |
Tejun Heo | 716f479 | 2013-12-05 12:28:03 -0500 | [diff] [blame] | 751 | cft->read_u64 = hugetlb_cgroup_read_u64; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 752 | |
| 753 | /* NULL terminate the last cft */ |
Mina Almasry | cdc2fcf | 2020-04-01 21:11:11 -0700 | [diff] [blame] | 754 | cft = &h->cgroup_files_legacy[8]; |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 755 | memset(cft, 0, sizeof(*cft)); |
| 756 | |
Tejun Heo | 2cf669a | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 757 | WARN_ON(cgroup_add_legacy_cftypes(&hugetlb_cgrp_subsys, |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 758 | h->cgroup_files_legacy)); |
| 759 | } |
| 760 | |
| 761 | static void __init __hugetlb_cgroup_file_init(int idx) |
| 762 | { |
| 763 | __hugetlb_cgroup_file_dfl_init(idx); |
| 764 | __hugetlb_cgroup_file_legacy_init(idx); |
Jianguo Wu | 7179e7b | 2012-12-18 14:23:19 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void __init hugetlb_cgroup_file_init(void) |
| 768 | { |
| 769 | struct hstate *h; |
| 770 | |
| 771 | for_each_hstate(h) { |
| 772 | /* |
| 773 | * Add cgroup control files only if the huge page consists |
| 774 | * of more than two normal pages. This is because we use |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 775 | * page[2].private for storing cgroup details. |
Jianguo Wu | 7179e7b | 2012-12-18 14:23:19 -0800 | [diff] [blame] | 776 | */ |
| 777 | if (huge_page_order(h) >= HUGETLB_CGROUP_MIN_ORDER) |
| 778 | __hugetlb_cgroup_file_init(hstate_index(h)); |
| 779 | } |
Aneesh Kumar K.V | abb8206 | 2012-07-31 16:42:24 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Aneesh Kumar K.V | 7575468 | 2012-07-31 16:42:36 -0700 | [diff] [blame] | 782 | /* |
| 783 | * hugetlb_lock will make sure a parallel cgroup rmdir won't happen |
| 784 | * when we migrate hugepages |
| 785 | */ |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 786 | void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage) |
| 787 | { |
| 788 | struct hugetlb_cgroup *h_cg; |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 789 | struct hugetlb_cgroup *h_cg_rsvd; |
Aneesh Kumar K.V | 94ae8ba | 2012-07-31 16:42:35 -0700 | [diff] [blame] | 790 | struct hstate *h = page_hstate(oldhpage); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 791 | |
| 792 | if (hugetlb_cgroup_disabled()) |
| 793 | return; |
| 794 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 795 | VM_BUG_ON_PAGE(!PageHuge(oldhpage), oldhpage); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 796 | spin_lock(&hugetlb_lock); |
| 797 | h_cg = hugetlb_cgroup_from_page(oldhpage); |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 798 | h_cg_rsvd = hugetlb_cgroup_from_page_rsvd(oldhpage); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 799 | set_hugetlb_cgroup(oldhpage, NULL); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 800 | |
| 801 | /* move the h_cg details to new cgroup */ |
Mina Almasry | 1adc4d4 | 2020-04-01 21:11:15 -0700 | [diff] [blame^] | 802 | set_hugetlb_cgroup_rsvd(newhpage, h_cg_rsvd); |
Aneesh Kumar K.V | 94ae8ba | 2012-07-31 16:42:35 -0700 | [diff] [blame] | 803 | list_move(&newhpage->lru, &h->hugepage_activelist); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 804 | spin_unlock(&hugetlb_lock); |
Aneesh Kumar K.V | 8e6ac7f | 2012-07-31 16:42:27 -0700 | [diff] [blame] | 805 | return; |
| 806 | } |
| 807 | |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 808 | static struct cftype hugetlb_files[] = { |
| 809 | {} /* terminate */ |
| 810 | }; |
| 811 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 812 | struct cgroup_subsys hugetlb_cgrp_subsys = { |
Tejun Heo | 92fb974 | 2012-11-19 08:13:38 -0800 | [diff] [blame] | 813 | .css_alloc = hugetlb_cgroup_css_alloc, |
| 814 | .css_offline = hugetlb_cgroup_css_offline, |
| 815 | .css_free = hugetlb_cgroup_css_free, |
Giuseppe Scrivano | faced7e | 2019-12-16 20:38:31 +0100 | [diff] [blame] | 816 | .dfl_cftypes = hugetlb_files, |
| 817 | .legacy_cftypes = hugetlb_files, |
Aneesh Kumar K.V | 2bc64a2 | 2012-07-31 16:42:12 -0700 | [diff] [blame] | 818 | }; |