History

mm, memcg: decouple e{low,min} state mutations from protection checks

mem_cgroup_protected currently is both used to set effective low and min and return a mem_cgroup_protection based on the result. As a user, this can be a little unexpected: it appears to be a simple predicate function, if not for the big warning in the comment above about the order in which it must be executed. This change makes it so that we separate the state mutations from the actual protection checks, which makes it more obvious where we need to be careful mutating internal state, and where we are simply checking and don't need to worry about that. [mhocko@suse.com - don't check protection on root memcgs]

Chris Down committed 6 years ago

Commit 45c7f7e1 parent 22f74963 files · +53 −45Browse files

Showing the changes to include/linux (1 of 3 files). Show all

Unified · Split

include/linux/memcontrol.h+42−11
@@ -47,12 +47,6 @@ enum memcg_memory_event { · 46 unmodified lines
4747 MEMCG_NR_MEMORY_EVENTS,
4848 };
4949
50−enum mem_cgroup_protection {
51− MEMCG_PROT_NONE,
52− MEMCG_PROT_LOW,
53− MEMCG_PROT_MIN,
54−};
55−
5650 struct mem_cgroup_reclaim_cookie {
5751 pg_data_t *pgdat;
5852 unsigned int generation;
@@ -405,8 +399,36 @@ static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root, · 346 unmodified lines
405399 READ_ONCE(memcg->memory.elow));
406400 }
407401
408−enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root,
409− struct mem_cgroup *memcg);
402+void mem_cgroup_calculate_protection(struct mem_cgroup *root,
403+ struct mem_cgroup *memcg);
404+
405+static inline bool mem_cgroup_supports_protection(struct mem_cgroup *memcg)
406+{
407+ /*
408+ * The root memcg doesn't account charges, and doesn't support
409+ * protection.
410+ */
411+ return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg);
412+
413+}
414+
415+static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
416+{
417+ if (!mem_cgroup_supports_protection(memcg))
418+ return false;
419+
420+ return READ_ONCE(memcg->memory.elow) >=
421+ page_counter_read(&memcg->memory);
422+}
423+
424+static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
425+{
426+ if (!mem_cgroup_supports_protection(memcg))
427+ return false;
428+
429+ return READ_ONCE(memcg->memory.emin) >=
430+ page_counter_read(&memcg->memory);
431+}
410432
411433 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask);
412434
@@ -935,10 +957,19 @@ static inline unsigned long mem_cgroup_protection(struct mem_cgroup *root, · 522 unmodified lines
935957 return 0;
936958 }
937959
938−static inline enum mem_cgroup_protection mem_cgroup_protected(
939− struct mem_cgroup *root, struct mem_cgroup *memcg)
960+static inline void mem_cgroup_calculate_protection(struct mem_cgroup *root,
961+ struct mem_cgroup *memcg)
940962 {
941− return MEMCG_PROT_NONE;
963+}
964+
965+static inline bool mem_cgroup_below_low(struct mem_cgroup *memcg)
966+{
967+ return false;
968+}
969+
970+static inline bool mem_cgroup_below_min(struct mem_cgroup *memcg)
971+{
972+ return false;
942973 }
943974
944975 static inline int mem_cgroup_charge(struct page *page, struct mm_struct *mm,