Skip to contentSkip to conversation

mm: allow mapping accounted kernel pages to userspace

PATCH Superseded Roman Gushchin 3 participants 17 messages Oct 1, 2020 – Oct 2, 2020

Subsystems (15)
Memory management VFS MM core Tracing +11 more subsystems

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover mm: allow mapping accounted kernel pages to userspace

Currently a non-slab kernel page which has been charged to a memory cgroup can't be mapped to userspace. The underlying reason is simple:
PageKmemcg flag is defined as a page type (like buddy, offline, etc), so it takes a bit from a page->mapped counter. Pages with a type set can't be mapped to userspace.

But in general the kmemcg flag has nothing to do with mapping to userspace. It only means that the page has been accounted by the page allocator, so it has to be properly uncharged on release.

Some bpf maps are mapping the vmalloc-based memory to userspace, and their memory can't be accounted because of this implementation detail.

This patchset removes this limitation by moving the PageKmemcg flag into one of the free bits of the page->mem_cgroup pointer. Also it formalizes all accesses to the page->mem_cgroup and page->obj_cgroups using new helpers, adds several checks and removes a couple of obsolete functions. As the result the code became more robust with fewer open-coded bits tricks.

v4:
- more cosmetic changes, by Johannes

v3:
- READ_ONCE() in page_memcg_rcu() and page_objcgs*(), by Johannes
- many cosmetic changes and renamings, by Johannes

v2:
- fixed a bug in page_obj_cgroups_check()
- moved some definitions between patches, by Shakeel
- dropped the memcg flags mutual exclusion requirement, by Shakeel

v1:
- added and fixed comments, by Shakeel
- added some VM_BUG_ON() checks
- fixed the debug output format of page->memcg_data

Roman Gushchin (4):
mm: memcontrol: use helpers to access page's memcg data
mm: memcontrol/slab: use helpers to access slab page's memcg_data
mm: introduce page memcg flags
mm: convert page kmemcg type to a page memcg flag

 fs/buffer.c                      |   2 +-
 fs/iomap/buffered-io.c           |   2 +-
 include/linux/memcontrol.h       | 242 +++++++++++++++++++++++++++++--
 include/linux/mm.h               |  22 ---
 include/linux/mm_types.h         |   5 +-
 include/linux/page-flags.h       |  11 +-
 include/trace/events/writeback.h |   2 +-
 kernel/fork.c                    |   7 +-
 mm/debug.c                       |   4 +-
 mm/huge_memory.c                 |   4 +-
 mm/memcontrol.c                  | 139 ++++++++----------
 mm/page_alloc.c                  |   8 +-
 mm/page_io.c                     |   6 +-
 mm/slab.h                        |  38 ++---
 mm/workingset.c                  |   2 +-
 15 files changed, 327 insertions(+), 167 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/4 mm: memcontrol: use helpers to access page's memcg data

Currently there are many open-coded reads and writes of the page->mem_cgroup pointer, as well as a couple of read helpers, which are barely used.

It creates an obstacle on a way to reuse some bits of the pointer for storing additional bits of information. In fact, we already do this for slab pages, where the last bit indicates that a pointer has an attached vector of objcg pointers instead of a regular memcg pointer.

This commits uses 2 existing helpers and introduces 2 new helpers to converts all raw accesses to page->mem_cgroup to calls of these helpers:
struct mem_cgroup *page_memcg(struct page *page);
struct mem_cgroup *page_memcg_rcu(struct page *page); struct mem_cgroup *page_memcg_check(struct page *page); void set_page_memcg(struct page *page, struct mem_cgroup *memcg);

page_memcg_check() is intended to be used in cases when the page can be a slab page and have a memcg pointer pointing at objcg vector. It does check the lowest bit, and if set, returns NULL. page_memcg() contains a VM_BUG_ON_PAGE() check for the page not being a slab page. So does set_page_memcg().

To make sure nobody uses a direct access, struct page's mem_cgroup/obj_cgroups is converted to unsigned long memcg_data. Only new helpers and a couple of slab-accounting related functions access this field directly.

---
 fs/buffer.c                      |   2 +-
 fs/iomap/buffered-io.c           |   2 +-
 include/linux/memcontrol.h       | 138 +++++++++++++++++++++++++++++--
 include/linux/mm.h               |  22 -----
 include/linux/mm_types.h         |   5 +-
 include/trace/events/writeback.h |   2 +-
 kernel/fork.c                    |   7 +-
 mm/debug.c                       |   4 +-
 mm/huge_memory.c                 |   4 +-
 mm/memcontrol.c                  | 121 +++++++++++++--------------
 mm/page_alloc.c                  |   4 +-
 mm/page_io.c                     |   6 +-
 mm/slab.h                        |   9 +-
 mm/workingset.c                  |   2 +-
 14 files changed, 208 insertions(+), 120 deletions(-)
fs/buffer.c +1 -1
index bf4d8037f91b..64564ac7dcc5 100644
@@ -657,7 +657,7 @@ int __set_page_dirty_buffers(struct page *page)
 		} while (bh != head);
 	}
 	/*
-	 * Lock out page->mem_cgroup migration to keep PageDirty
+	 * Lock out page's memcg migration to keep PageDirty
 	 * synchronized with per-memcg dirty page counters.
 	 */
 	lock_page_memcg(page);
fs/iomap/buffered-io.c +1 -1
index 8180061b9e16..f96094a32851 100644
@@ -650,7 +650,7 @@ iomap_set_page_dirty(struct page *page)
 		return !TestSetPageDirty(page);
 
 	/*
-	 * Lock out page->mem_cgroup migration to keep PageDirty
+	 * Lock out page's memcg migration to keep PageDirty
 	 * synchronized with per-memcg dirty page counters.
 	 */
 	lock_page_memcg(page);
include/linux/memcontrol.h +129 -9

187 lines not on this page · raw message

include/linux/mm.h +0 -22
index 27c64d0d7520..6707801b014d 100644
@@ -1484,28 +1484,6 @@ static inline void set_page_links(struct page *page, enum zone_type zone,
 #endif
 }
 
-#ifdef CONFIG_MEMCG
-static inline struct mem_cgroup *page_memcg(struct page *page)
-{
-	return page->mem_cgroup;
-}
-static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
-{
-	WARN_ON_ONCE(!rcu_read_lock_held());
-	return READ_ONCE(page->mem_cgroup);
-}
-#else
-static inline struct mem_cgroup *page_memcg(struct page *page)
-{
-	return NULL;
-}
-static inline struct mem_cgroup *page_memcg_rcu(struct page *page)
-{
-	WARN_ON_ONCE(!rcu_read_lock_held());
-	return NULL;
-}
-#endif
-
 /*
  * Some inline functions in vmstat.h depend on page_zone()
  */
include/linux/mm_types.h +1 -4
index 5a9238f6caad..80f5d755c037 100644
@@ -199,10 +199,7 @@ struct page {
 	atomic_t _refcount;
 
 #ifdef CONFIG_MEMCG
-	union {
-		struct mem_cgroup *mem_cgroup;
-		struct obj_cgroup **obj_cgroups;
-	};
+	unsigned long memcg_data;
 #endif
 
 	/*
include/trace/events/writeback.h +1 -1
index e7cbccc7c14c..39a40dfb578a 100644
@@ -257,7 +257,7 @@ TRACE_EVENT(track_foreign_dirty,
 		__entry->ino		= inode ? inode->i_ino : 0;
 		__entry->memcg_id	= wb->memcg_css->id;
 		__entry->cgroup_ino	= __trace_wb_assign_cgroup(wb);
-		__entry->page_cgroup_ino = cgroup_ino(page->mem_cgroup->css.cgroup);
+		__entry->page_cgroup_ino = cgroup_ino(page_memcg(page)->css.cgroup);
 	),
 
 	TP_printk("bdi %s[%llu]: ino=%lu memcg_id=%u cgroup_ino=%lu page_cgroup_ino=%lu",
kernel/fork.c +4 -3
index 9be04e54de7f..74e5776f2cea 100644
@@ -403,9 +403,10 @@ static int memcg_charge_kernel_stack(struct task_struct *tsk)
 
 		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) {
 			/*
-			 * If memcg_kmem_charge_page() fails, page->mem_cgroup
-			 * pointer is NULL, and memcg_kmem_uncharge_page() in
-			 * free_thread_stack() will ignore this page.
+			 * If memcg_kmem_charge_page() fails, page's
+			 * memory cgroup pointer is NULL, and
+			 * memcg_kmem_uncharge_page() in free_thread_stack()
+			 * will ignore this page.
 			 */
 			ret = memcg_kmem_charge_page(vm->pages[i], GFP_KERNEL,
 						     0);
mm/debug.c +2 -2
index ccca576b2899..8a40b3fefbeb 100644
@@ -182,8 +182,8 @@ void __dump_page(struct page *page, const char *reason)
 		pr_warn("page dumped because: %s\n", reason);
 
 #ifdef CONFIG_MEMCG
-	if (!page_poisoned && page->mem_cgroup)
-		pr_warn("page->mem_cgroup:%px\n", page->mem_cgroup);
+	if (!page_poisoned && page->memcg_data)
+		pr_warn("pages's memcg:%lx\n", page->memcg_data);
 #endif
 }
 
mm/huge_memory.c +2 -2
index cba3812a5c3e..999f8a4dcb56 100644
@@ -470,7 +470,7 @@ pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
 #ifdef CONFIG_MEMCG
 static inline struct deferred_split *get_deferred_split_queue(struct page *page)
 {
-	struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
+	struct mem_cgroup *memcg = page_memcg(compound_head(page));
 	struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
 
 	if (memcg)
@@ -2766,7 +2766,7 @@ void deferred_split_huge_page(struct page *page)
 {
 	struct deferred_split *ds_queue = get_deferred_split_queue(page);
 #ifdef CONFIG_MEMCG
-	struct mem_cgroup *memcg = compound_head(page)->mem_cgroup;
+	struct mem_cgroup *memcg = page_memcg(compound_head(page));
 #endif
 	unsigned long flags;
 
mm/memcontrol.c +56 -65

357 lines not on this page · raw message

mm/page_alloc.c +2 -2
index eb0962976a0e..e9f0fe4a143e 100644
@@ -1057,7 +1057,7 @@ static inline bool page_expected_state(struct page *page,
 	if (unlikely((unsigned long)page->mapping |
 			page_ref_count(page) |
 #ifdef CONFIG_MEMCG
-			(unsigned long)page->mem_cgroup |
+			(unsigned long)page_memcg(page) |
 #endif
 			(page->flags & check_flags)))
 		return false;
@@ -1082,7 +1082,7 @@ static const char *page_bad_reason(struct page *page, unsigned long flags)
 			bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
 	}
 #ifdef CONFIG_MEMCG
-	if (unlikely(page->mem_cgroup))
+	if (unlikely(page_memcg(page)))
 		bad_reason = "page still charged to cgroup";
 #endif
 	return bad_reason;
mm/page_io.c +4 -2
index 433df1263349..9bca17ecc4df 100644
@@ -291,12 +291,14 @@ static inline void count_swpout_vm_event(struct page *page)
 static void bio_associate_blkg_from_page(struct bio *bio, struct page *page)
 {
 	struct cgroup_subsys_state *css;
+	struct mem_cgroup *memcg;
 
-	if (!page->mem_cgroup)
+	memcg = page_memcg(page);
+	if (!memcg)
 		return;
 
 	rcu_read_lock();
-	css = cgroup_e_css(page->mem_cgroup->css.cgroup, &io_cgrp_subsys);
+	css = cgroup_e_css(memcg->css.cgroup, &io_cgrp_subsys);
 	bio_associate_blkg_from_css(bio, css);
 	rcu_read_unlock();
 }
mm/slab.h +4 -5
index 4a24e1702923..5ac89260f329 100644
@@ -242,18 +242,17 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla
 static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
 {
 	/*
-	 * page->mem_cgroup and page->obj_cgroups are sharing the same
+	 * Page's memory cgroup and obj_cgroups vector are sharing the same
 	 * space. To distinguish between them in case we don't know for sure
 	 * that the page is a slab page (e.g. page_cgroup_ino()), let's
 	 * always set the lowest bit of obj_cgroups.
 	 */
-	return (struct obj_cgroup **)
-		((unsigned long)page->obj_cgroups & ~0x1UL);
+	return (struct obj_cgroup **)(page->memcg_data & ~0x1UL);
 }
 
 static inline bool page_has_obj_cgroups(struct page *page)
 {
-	return ((unsigned long)page->obj_cgroups & 0x1UL);
+	return page->memcg_data & 0x1UL;
 }
 
 int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
@@ -262,7 +261,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
 static inline void memcg_free_page_obj_cgroups(struct page *page)
 {
 	kfree(page_obj_cgroups(page));
-	page->obj_cgroups = NULL;
+	page->memcg_data = 0;
 }
 
 static inline size_t obj_full_size(struct kmem_cache *s)
mm/workingset.c +1 -1
index 8ed8e6296d8c..ef563f94a0e3 100644
@@ -257,7 +257,7 @@ void *workingset_eviction(struct page *page, struct mem_cgroup *target_memcg)
 	struct lruvec *lruvec;
 	int memcgid;
 
-	/* Page is fully exclusive and pins page->mem_cgroup */
+	/* Page is fully exclusive and pins page's memory cgroup pointer */
 	VM_BUG_ON_PAGE(PageLRU(page), page);
 	VM_BUG_ON_PAGE(page_count(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH v4 1/4] mm: memcontrol: use helpers to access page's memcg data

Quoted 7 lines · Roman Gushchin
include/linux/memcontrol.h +7 -0
+/*
+ * set_page_memcg - associate a page with a memory cgroup
+ * @page: a pointer to the page struct
+ * @memcg: a pointer to the memory cgroup
+ *
+ * Associates a page with a memory cgroup.
+ */

include/linux/memcontrol.h struct mem_cgroup { · line 435

Quoted 10 lines
include/linux/memcontrol.h +10 -0
+static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
+{
+	VM_BUG_ON_PAGE(PageSlab(page), page);
+
+	/*
+	 * Please, refer to page_memcg()'s description for the page and memcg
+	 * binding stability requirements.
+	 */
+	page->memcg_data = (unsigned long)memcg;
+}

Please delete and inline this as per previous feedback, thanks.

#3 lore raw

Re: [PATCH v4 1/4] mm: memcontrol: use helpers to access page's memcg data

Quoted 12 lines · Johannes Weiner
Quoted 11 lines · Roman Gushchin
include/linux/memcontrol.h +11 -0
+/*
+ * set_page_memcg - associate a page with a memory cgroup
+ * @page: a pointer to the page struct
+ * @memcg: a pointer to the memory cgroup
+ *
+ * Associates a page with a memory cgroup.
+ */
+static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
+{
+	VM_BUG_ON_PAGE(PageSlab(page), page);
+
Quoted 8 lines

include/linux/memcontrol.h struct mem_cgroup { · line 435

Quoted 6 lines
include/linux/memcontrol.h +6 -0
+	/*
+	 * Please, refer to page_memcg()'s description for the page and memcg
+	 * binding stability requirements.
+	 */
+	page->memcg_data = (unsigned long)memcg;
+}

Please delete and inline this as per previous feedback, thanks.

Why it's better?
It's ok for set_page_memcg(), but obviously worse for set_page_objcgs():
it was nice to have all bit magic in one place, in few helper functions. And now it spills into several places. What's the win?

Thanks.

maintainer
#4 lore raw

Re: [PATCH v4 1/4] mm: memcontrol: use helpers to access page's memcg data

Quoted 22 lines · Roman Gushchin
Quoted 12 lines · Johannes Weiner
Quoted 11 lines · Roman Gushchin
include/linux/memcontrol.h +11 -0
+/*
+ * set_page_memcg - associate a page with a memory cgroup
+ * @page: a pointer to the page struct
+ * @memcg: a pointer to the memory cgroup
+ *
+ * Associates a page with a memory cgroup.
+ */
+static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
+{
+	VM_BUG_ON_PAGE(PageSlab(page), page);
+
Quoted 8 lines

include/linux/memcontrol.h struct mem_cgroup { · line 435

Quoted 6 lines
include/linux/memcontrol.h +6 -0
+	/*
+	 * Please, refer to page_memcg()'s description for the page and memcg
+	 * binding stability requirements.
+	 */
+	page->memcg_data = (unsigned long)memcg;
+}

Please delete and inline this as per previous feedback, thanks.

Quoted 4 lines

Why it's better?
It's ok for set_page_memcg(), but obviously worse for set_page_objcgs():
it was nice to have all bit magic in one place, in few helper functions. And now it spills into several places. What's the win?

set_page_objcgs() is a worthwhile abstraction because it includes the synchronization primitives that make it safe to use wrt page_objcgs(). They encapsulate the cmpxchg and the READ_ONCE().

set_page_memcg() doesn't do any synchronization and relies fully on the contextual locking. The name implies that it includes things to make it safe wrt page_memcg(), which isn't true at all. It's a long and misleading name for '='.

Btw, I really don't mind having this discussion, but please don't send revisions that silently ignore feedback you don't agree with.

Thanks

#5 lore raw

Re: [PATCH v4 1/4] mm: memcontrol: use helpers to access page's memcg data

Quoted 32 lines · Johannes Weiner
Quoted 22 lines · Roman Gushchin
Quoted 12 lines · Johannes Weiner
Quoted 11 lines · Roman Gushchin
include/linux/memcontrol.h +11 -0
+/*
+ * set_page_memcg - associate a page with a memory cgroup
+ * @page: a pointer to the page struct
+ * @memcg: a pointer to the memory cgroup
+ *
+ * Associates a page with a memory cgroup.
+ */
+static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
+{
+	VM_BUG_ON_PAGE(PageSlab(page), page);
+
Quoted 8 lines

include/linux/memcontrol.h struct mem_cgroup { · line 435

Quoted 6 lines
include/linux/memcontrol.h +6 -0
+	/*
+	 * Please, refer to page_memcg()'s description for the page and memcg
+	 * binding stability requirements.
+	 */
+	page->memcg_data = (unsigned long)memcg;
+}

Please delete and inline this as per previous feedback, thanks.

Quoted 4 lines

Why it's better?
It's ok for set_page_memcg(), but obviously worse for set_page_objcgs():
it was nice to have all bit magic in one place, in few helper functions. And now it spills into several places. What's the win?

set_page_objcgs() is a worthwhile abstraction because it includes the synchronization primitives that make it safe to use wrt page_objcgs(). They encapsulate the cmpxchg and the READ_ONCE().

Quoted 7 lines

set_page_memcg() doesn't do any synchronization and relies fully on the contextual locking. The name implies that it includes things to make it safe wrt page_memcg(), which isn't true at all. It's a long and misleading name for '='.

Btw, I really don't mind having this discussion, but please don't send revisions that silently ignore feedback you don't agree with.

I'm not ignoring: I thought you was looking to remove clear_page_* functions, but it wasn't clear you want eliminate set_page_memcg() function. Please, when you asking for some "style" changes, please, provide some rationale, it's way less obvious than you think, what exactly you don't like in the proposed version.

Thanks.

maintainer
#6 lore raw

Re: [PATCH v4 1/4] mm: memcontrol: use helpers to access page's memcg data

Quoted 38 lines · Roman Gushchin
Quoted 37 lines · Johannes Weiner
Quoted 22 lines · Roman Gushchin
Quoted 12 lines · Johannes Weiner
Quoted 11 lines · Roman Gushchin
include/linux/memcontrol.h +11 -0
+/*
+ * set_page_memcg - associate a page with a memory cgroup
+ * @page: a pointer to the page struct
+ * @memcg: a pointer to the memory cgroup
+ *
+ * Associates a page with a memory cgroup.
+ */
+static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
+{
+	VM_BUG_ON_PAGE(PageSlab(page), page);
+
Quoted 8 lines

include/linux/memcontrol.h struct mem_cgroup { · line 435

Quoted 6 lines
include/linux/memcontrol.h +6 -0
+	/*
+	 * Please, refer to page_memcg()'s description for the page and memcg
+	 * binding stability requirements.
+	 */
+	page->memcg_data = (unsigned long)memcg;
+}

Please delete and inline this as per previous feedback, thanks.

Quoted 4 lines

Why it's better?
It's ok for set_page_memcg(), but obviously worse for set_page_objcgs():
it was nice to have all bit magic in one place, in few helper functions. And now it spills into several places. What's the win?

set_page_objcgs() is a worthwhile abstraction because it includes the synchronization primitives that make it safe to use wrt page_objcgs(). They encapsulate the cmpxchg and the READ_ONCE().

set_page_memcg() doesn't do any synchronization and relies fully on the contextual locking. The name implies that it includes things to make it safe wrt page_memcg(), which isn't true at all. It's a long and misleading name for '='.

Quoted 7 lines
Quoted 2 lines

Btw, I really don't mind having this discussion, but please don't send revisions that silently ignore feedback you don't agree with.

I'm not ignoring: I thought you was looking to remove clear_page_* functions, but it wasn't clear you want eliminate set_page_memcg() function. Please, when you asking for some "style" changes, please, provide some rationale, it's way less obvious than you think, what exactly you don't like in the proposed version.

https://lore.kernel.org/lkml/[email protected]/
mentions and provides a rationale for inlining the setting and clearing (unnecessary abstraction/encapsulation).

This is a code base shared by many different developers. It's routine to be asked for style changes that make the new code blend in with its surroundings, follow precedents and use idiomatic constructs, or avoid anti-patterns that are known to cause maintenance burden or make it harder to hack or debug the code.

If I'm being too terse and something isn't clear, please just ask for clarification.

#7 lore raw

PATCH v4 2/4 mm: memcontrol/slab: use helpers to access slab page's memcg_data

In reply to Roman Gushchin

To gather all direct accesses to struct page's memcg_data field in one place, let's introduce 3 new helpers to use in the slab accounting code:
struct obj_cgroup **page_objcgs(struct page *page); struct obj_cgroup **page_objcgs_check(struct page *page); bool set_page_objcgs(struct page *page, struct obj_cgroup **objcgs);

They are similar to the corresponding API for generic pages, except that the setter can return false, indicating that the value has been already set from a different thread.

---
 include/linux/memcontrol.h | 64 ++++++++++++++++++++++++++++++++++++++
 mm/memcontrol.c            |  6 ++--
 mm/slab.h                  | 35 +++++----------------
 3 files changed, 75 insertions(+), 30 deletions(-)
include/linux/memcontrol.h +64 -0
index 9609f919350e..7e3da7cdc1ba 100644
@@ -435,6 +435,70 @@ static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
 }
 
 
+#ifdef CONFIG_MEMCG_KMEM
+/*
+ * page_objcgs - get the object cgroups vector associated with a page
+ * @page: a pointer to the page struct
+ *
+ * Returns a pointer to the object cgroups vector associated with the page,
+ * or NULL. This function assumes that the page is known to have an
+ * associated object cgroups vector. It's not safe to call this function
+ * against pages, which might have an associated memory cgroup: e.g.
+ * kernel stack pages.
+ */
+static inline struct obj_cgroup **page_objcgs(struct page *page)
+{
+	return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL);
+}
+
+/*
+ * page_objcgs_check - get the object cgroups vector associated with a page
+ * @page: a pointer to the page struct
+ *
+ * Returns a pointer to the object cgroups vector associated with the page,
+ * or NULL. This function is safe to use if the page can be directly associated
+ * with a memory cgroup.
+ */
+static inline struct obj_cgroup **page_objcgs_check(struct page *page)
+{
+	unsigned long memcg_data = READ_ONCE(page->memcg_data);
+
+	if (memcg_data && (memcg_data & 0x1UL))
+		return (struct obj_cgroup **)(memcg_data & ~0x1UL);
+
+	return NULL;
+}
+
+/*
+ * set_page_objcgs - associate a page with a object cgroups vector
+ * @page: a pointer to the page struct
+ * @objcgs: a pointer to the object cgroups vector
+ *
+ * Atomically associates a page with a vector of object cgroups.
+ */
+static inline bool set_page_objcgs(struct page *page,
+					struct obj_cgroup **objcgs)
+{
+	return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL);
+}
+#else
+static inline struct obj_cgroup **page_objcgs(struct page *page)
+{
+	return NULL;
+}
+
+static inline struct obj_cgroup **page_objcgs_check(struct page *page)
+{
+	return NULL;
+}
+
+static inline bool set_page_objcgs(struct page *page,
+					struct obj_cgroup **objcgs)
+{
+	return true;
+}
+#endif
+
 static __always_inline bool memcg_stat_item_in_bytes(int idx)
 {
 	if (idx == MEMCG_PERCPU_B)
mm/memcontrol.c +3 -3
index e97454623c62..7e690424a917 100644
@@ -2899,7 +2899,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
 	if (!vec)
 		return -ENOMEM;
 
-	if (cmpxchg(&page->memcg_data, 0, (unsigned long)vec | 0x1UL))
+	if (!set_page_objcgs(page, vec))
 		kfree(vec);
 	else
 		kmemleak_not_leak(vec);
@@ -2933,12 +2933,12 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
 	 * Memcg membership data for each individual object is saved in
 	 * the page->obj_cgroups.
 	 */
-	if (page_has_obj_cgroups(page)) {
+	if (page_objcgs_check(page)) {
 		struct obj_cgroup *objcg;
 		unsigned int off;
 
 		off = obj_to_index(page->slab_cache, page, p);
-		objcg = page_obj_cgroups(page)[off];
+		objcg = page_objcgs(page)[off];
 		if (objcg)
 			return obj_cgroup_memcg(objcg);
 
mm/slab.h +8 -27
index 5ac89260f329..9b70d30d21c7 100644
@@ -239,28 +239,12 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla
 }
 
 #ifdef CONFIG_MEMCG_KMEM
-static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
-{
-	/*
-	 * Page's memory cgroup and obj_cgroups vector are sharing the same
-	 * space. To distinguish between them in case we don't know for sure
-	 * that the page is a slab page (e.g. page_cgroup_ino()), let's
-	 * always set the lowest bit of obj_cgroups.
-	 */
-	return (struct obj_cgroup **)(page->memcg_data & ~0x1UL);
-}
-
-static inline bool page_has_obj_cgroups(struct page *page)
-{
-	return page->memcg_data & 0x1UL;
-}
-
 int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
 				 gfp_t gfp);
 
 static inline void memcg_free_page_obj_cgroups(struct page *page)
 {
-	kfree(page_obj_cgroups(page));
+	kfree(page_objcgs(page));
 	page->memcg_data = 0;
 }
 
@@ -322,7 +306,7 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
 		if (likely(p[i])) {
 			page = virt_to_head_page(p[i]);
 
-			if (!page_has_obj_cgroups(page) &&
+			if (!page_objcgs(page) &&
 			    memcg_alloc_page_obj_cgroups(page, s, flags)) {
 				obj_cgroup_uncharge(objcg, obj_full_size(s));
 				continue;
@@ -330,7 +314,7 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
 
 			off = obj_to_index(s, page, p[i]);
 			obj_cgroup_get(objcg);
-			page_obj_cgroups(page)[off] = objcg;
+			page_objcgs(page)[off] = objcg;
 			mod_objcg_state(objcg, page_pgdat(page),
 					cache_vmstat_idx(s), obj_full_size(s));
 		} else {
@@ -343,18 +327,20 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
 					void *p)
 {
+	struct obj_cgroup **objcgs;
 	struct obj_cgroup *objcg;
 	unsigned int off;
 
 	if (!memcg_kmem_enabled())
 		return;
 
-	if (!page_has_obj_cgroups(page))
+	objcgs = page_objcgs(page);
+	if (!objcgs)
 		return;
 
 	off = obj_to_index(s, page, p);
-	objcg = page_obj_cgroups(page)[off];
-	page_obj_cgroups(page)[off] = NULL;
+	objcg = objcgs[off];
+	objcgs[off] = NULL;
 
 	if (!objcg)
 		return;
@@ -367,11 +353,6 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
 }
 
 #else /* CONFIG_MEMCG_KMEM */
-static inline bool page_has_obj_cgroups(struct page *page)
-{
-	return false;
-}
-
 static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
 {
 	return NULL;
Signature · 1 line
2.26.2
#8 lore raw

PATCH v4 3/4 mm: introduce page memcg flags

In reply to Roman Gushchin

The lowest bit in page->memcg_data is used to distinguish between struct memory_cgroup pointer and a pointer to a objcgs array. All checks and modifications of this bit are open-coded.

Let's formalize it using page memcg flags, defined in enum page_memcg_data_flags.

Additional flags might be added later.

---
 include/linux/memcontrol.h | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
include/linux/memcontrol.h +24 -11
index 7e3da7cdc1ba..5e05599e1f74 100644
@@ -343,6 +343,15 @@ struct mem_cgroup {
 
 extern struct mem_cgroup *root_mem_cgroup;
 
+enum page_memcg_data_flags {
+	/* page->memcg_data is a pointer to an objcgs vector */
+	MEMCG_DATA_OBJCGS = (1UL << 0),
+	/* the next bit after the last actual flag */
+	__NR_MEMCG_DATA_FLAGS  = (1UL << 1),
+};
+
+#define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
+
 /*
  * page_memcg - get the memory cgroup associated with a page
  * @page: a pointer to the page struct
@@ -404,13 +413,7 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page)
 	 */
 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
 
-	/*
-	 * The lowest bit set means that memcg isn't a valid
-	 * memcg pointer, but a obj_cgroups pointer.
-	 * In this case the page is shared and doesn't belong
-	 * to any specific memory cgroup.
-	 */
-	if (memcg_data & 0x1UL)
+	if (memcg_data & MEMCG_DATA_OBJCGS)
 		return NULL;
 
 	return (struct mem_cgroup *)memcg_data;
@@ -448,7 +451,12 @@ static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
  */
 static inline struct obj_cgroup **page_objcgs(struct page *page)
 {
-	return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL);
+	unsigned long memcg_data = READ_ONCE(page->memcg_data);
+
+	VM_BUG_ON_PAGE(memcg_data &&
+		       !(memcg_data & MEMCG_DATA_OBJCGS), page);
+
+	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }
 
 /*
@@ -463,8 +471,9 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
 {
 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
 
-	if (memcg_data && (memcg_data & 0x1UL))
-		return (struct obj_cgroup **)(memcg_data & ~0x1UL);
+	if (memcg_data && (memcg_data & MEMCG_DATA_OBJCGS))
+		return (struct obj_cgroup **)
+			(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 
 	return NULL;
 }
@@ -479,7 +488,11 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
 static inline bool set_page_objcgs(struct page *page,
 					struct obj_cgroup **objcgs)
 {
-	return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL);
+	unsigned long memcg_data = (unsigned long)objcgs;
+
+	memcg_data |= MEMCG_DATA_OBJCGS;
+
+	return !cmpxchg(&page->memcg_data, 0, memcg_data);
 }
 #else
 static inline struct obj_cgroup **page_objcgs(struct page *page)
Signature · 1 line
2.26.2
maintainer
#9 lore raw

Re: [PATCH v4 3/4] mm: introduce page memcg flags

include/linux/memcontrol.h static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg) · line 457

Quoted 9 lines · Roman Gushchin
include/linux/memcontrol.h +4 -1
@@ -448,7 +451,12 @@ static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
  */
 static inline struct obj_cgroup **page_objcgs(struct page *page)
 {
-	return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL);
+	unsigned long memcg_data = READ_ONCE(page->memcg_data);
+
+	VM_BUG_ON_PAGE(memcg_data &&
+		       !(memcg_data & MEMCG_DATA_OBJCGS), page);

This fits on a single line.

include/linux/memcontrol.h static inline struct obj_cgroup **page_objcgs_check(struct page *page) · line 476

Quoted 13 lines
include/linux/memcontrol.h +4 -2
+	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }
 
 /*
@@ -463,8 +471,9 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
 {
 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
 
-	if (memcg_data && (memcg_data & 0x1UL))
-		return (struct obj_cgroup **)(memcg_data & ~0x1UL);
+	if (memcg_data && (memcg_data & MEMCG_DATA_OBJCGS))
+		return (struct obj_cgroup **)
+			(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
	if (!memcg_data || (memcg_data & MEMCG_DATA_OBJCGS))
		return NULL;

	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);

include/linux/memcontrol.h static inline struct obj_cgroup **page_objcgs_check(struct page *page) · line 495

Quoted 10 lines
include/linux/memcontrol.h +5 -1
@@ -479,7 +488,11 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
 static inline bool set_page_objcgs(struct page *page,
 					struct obj_cgroup **objcgs)
 {
-	return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL);
+	unsigned long memcg_data = (unsigned long)objcgs;
+
+	memcg_data |= MEMCG_DATA_OBJCGS;
+
+	return !cmpxchg(&page->memcg_data, 0, memcg_data);
	return !cmpxchg(&page->memcg_data, 0, memcg_data | MEMCG_DATA_OBJCGS);
#10 lore raw

PATCH v4 4/4 mm: convert page kmemcg type to a page memcg flag

In reply to Roman Gushchin

PageKmemcg flag is currently defined as a page type (like buddy, offline, table and guard). Semantically it means that the page was accounted as a kernel memory by the page allocator and has to be uncharged on the release.

As a side effect of defining the flag as a page type, the accounted page can't be mapped to userspace (look at page_has_type() and comments above). In particular, this blocks the accounting of vmalloc-backed memory used by some bpf maps, because these maps do map the memory to userspace.

One option is to fix it by complicating the access to page->mapcount, which provides some free bits for page->page_type.

But it's way better to move this flag into page->memcg_data flags. Indeed, the flag makes no sense without enabled memory cgroups and memory cgroup pointer set in particular.

This commit replaces PageKmemcg() and __SetPageKmemcg() with PageMemcgKmem() and an open-coded OR operation setting the memcg pointer with the MEMCG_DATA_KMEM bit. __ClearPageKmemcg() can be simple deleted, as the whole memcg_data is zeroed at once.

As a bonus, on !CONFIG_MEMCG build the PageMemcgKmem() check will be compiled out.

---
 include/linux/memcontrol.h | 39 ++++++++++++++++++++++++++++++++------
 include/linux/page-flags.h | 11 ++---------
 mm/memcontrol.c            | 16 +++++-----------
 mm/page_alloc.c            |  4 ++--
 4 files changed, 42 insertions(+), 28 deletions(-)
include/linux/memcontrol.h +33 -6
index 5e05599e1f74..8c15978294b2 100644
@@ -346,8 +346,10 @@ extern struct mem_cgroup *root_mem_cgroup;
 enum page_memcg_data_flags {
 	/* page->memcg_data is a pointer to an objcgs vector */
 	MEMCG_DATA_OBJCGS = (1UL << 0),
+	/* page has been accounted as a non-slab kernel page */
+	MEMCG_DATA_KMEM = (1UL << 1),
 	/* the next bit after the last actual flag */
-	__NR_MEMCG_DATA_FLAGS  = (1UL << 1),
+	__NR_MEMCG_DATA_FLAGS  = (1UL << 2),
 };
 
 #define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1)
@@ -369,8 +371,12 @@ enum page_memcg_data_flags {
  */
 static inline struct mem_cgroup *page_memcg(struct page *page)
 {
+	unsigned long memcg_data = page->memcg_data;
+
 	VM_BUG_ON_PAGE(PageSlab(page), page);
-	return (struct mem_cgroup *)page->memcg_data;
+	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
+
+	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }
 
 /*
@@ -416,7 +422,7 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page)
 	if (memcg_data & MEMCG_DATA_OBJCGS)
 		return NULL;
 
-	return (struct mem_cgroup *)memcg_data;
+	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }
 
 /*
@@ -438,6 +444,20 @@ static inline void set_page_memcg(struct page *page, struct mem_cgroup *memcg)
 }
 
 
+/*
+ * PageMemcgKmem - check if the page has MemcgKmem flag set
+ * @page: a pointer to the page struct
+ *
+ * Checks if the page has MemcgKmem flag set. The caller must ensure that
+ * the page has an associated memory cgroup. It's not safe to call this function
+ * against some types of pages, e.g. slab pages.
+ */
+static inline bool PageMemcgKmem(struct page *page)
+{
+	VM_BUG_ON_PAGE(page->memcg_data & MEMCG_DATA_OBJCGS, page);
+	return page->memcg_data & MEMCG_DATA_KMEM;
+}
+
 #ifdef CONFIG_MEMCG_KMEM
 /*
  * page_objcgs - get the object cgroups vector associated with a page
@@ -453,8 +473,8 @@ static inline struct obj_cgroup **page_objcgs(struct page *page)
 {
 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
 
-	VM_BUG_ON_PAGE(memcg_data &&
-		       !(memcg_data & MEMCG_DATA_OBJCGS), page);
+	VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), page);
+	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
 
 	return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }
@@ -471,9 +491,11 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
 {
 	unsigned long memcg_data = READ_ONCE(page->memcg_data);
 
-	if (memcg_data && (memcg_data & MEMCG_DATA_OBJCGS))
+	if (memcg_data && (memcg_data & MEMCG_DATA_OBJCGS)) {
+		VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, page);
 		return (struct obj_cgroup **)
 			(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
+	}
 
 	return NULL;
 }
@@ -1138,6 +1160,11 @@ static inline void set_page_memcg(struct page *page,
 {
 }
 
+static inline bool PageMemcgKmem(struct page *page)
+{
+	return false;
+}
+
 static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
 {
 	return true;
include/linux/page-flags.h +2 -9
index 4f6ba9379112..fc0e1bd48e73 100644
@@ -715,9 +715,8 @@ PAGEFLAG_FALSE(DoubleMap)
 #define PAGE_MAPCOUNT_RESERVE	-128
 #define PG_buddy	0x00000080
 #define PG_offline	0x00000100
-#define PG_kmemcg	0x00000200
-#define PG_table	0x00000400
-#define PG_guard	0x00000800
+#define PG_table	0x00000200
+#define PG_guard	0x00000400
 
 #define PageType(page, flag)						\
 	((page->page_type & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE)
@@ -768,12 +767,6 @@ PAGE_TYPE_OPS(Buddy, buddy)
  */
 PAGE_TYPE_OPS(Offline, offline)
 
-/*
- * If kmemcg is enabled, the buddy allocator will set PageKmemcg() on
- * pages allocated with __GFP_ACCOUNT. It gets cleared on page free.
- */
-PAGE_TYPE_OPS(Kmemcg, kmemcg)
-
 /*
  * Marks pages in use as page tables.
  */
mm/memcontrol.c +5 -11
index 7e690424a917..ff9bb3e8333a 100644
@@ -3090,8 +3090,8 @@ int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
 	if (memcg && !mem_cgroup_is_root(memcg)) {
 		ret = __memcg_kmem_charge(memcg, gfp, 1 << order);
 		if (!ret) {
-			set_page_memcg(page, memcg);
-			__SetPageKmemcg(page);
+			page->memcg_data = (unsigned long)memcg |
+				MEMCG_DATA_KMEM;
 			return 0;
 		}
 		css_put(&memcg->css);
@@ -3116,10 +3116,6 @@ void __memcg_kmem_uncharge_page(struct page *page, int order)
 	__memcg_kmem_uncharge(memcg, nr_pages);
 	page->memcg_data = 0;
 	css_put(&memcg->css);
-
-	/* slab pages do not have PageKmemcg flag set */
-	if (PageKmemcg(page))
-		__ClearPageKmemcg(page);
 }
 
 static bool consume_obj_stock(struct obj_cgroup *objcg, unsigned int nr_bytes)
@@ -6863,12 +6859,10 @@ static void uncharge_page(struct page *page, struct uncharge_gather *ug)
 	nr_pages = compound_nr(page);
 	ug->nr_pages += nr_pages;
 
-	if (!PageKmemcg(page)) {
-		ug->pgpgout++;
-	} else {
+	if (PageMemcgKmem(page))
 		ug->nr_kmem += nr_pages;
-		__ClearPageKmemcg(page);
-	}
+	else
+		ug->pgpgout++;
 
 	ug->dummy_page = page;
 	page->memcg_data = 0;
mm/page_alloc.c +2 -2
index e9f0fe4a143e..5ebd50183d93 100644
@@ -1179,7 +1179,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 		 * Do not let hwpoison pages hit pcplists/buddy
 		 * Untie memcg state and reset page's owner
 		 */
-		if (memcg_kmem_enabled() && PageKmemcg(page))
+		if (memcg_kmem_enabled() && PageMemcgKmem(page))
 			__memcg_kmem_uncharge_page(page, order);
 		reset_page_owner(page, order);
 		return false;
@@ -1209,7 +1209,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 	}
 	if (PageMappingFlags(page))
 		page->mapping = NULL;
-	if (memcg_kmem_enabled() && PageKmemcg(page))
+	if (memcg_kmem_enabled() && PageMemcgKmem(page))
 		__memcg_kmem_uncharge_page(page, order);
 	if (check_free)
 		bad += check_free_page(page);
Signature · 1 line
2.26.2
maintainer Acked-by on v4 4/4
#11 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

Quoted 23 lines · Roman Gushchin

PageKmemcg flag is currently defined as a page type (like buddy, offline, table and guard). Semantically it means that the page was accounted as a kernel memory by the page allocator and has to be uncharged on the release.

As a side effect of defining the flag as a page type, the accounted page can't be mapped to userspace (look at page_has_type() and comments above). In particular, this blocks the accounting of vmalloc-backed memory used by some bpf maps, because these maps do map the memory to userspace.

One option is to fix it by complicating the access to page->mapcount, which provides some free bits for page->page_type.

But it's way better to move this flag into page->memcg_data flags. Indeed, the flag makes no sense without enabled memory cgroups and memory cgroup pointer set in particular.

This commit replaces PageKmemcg() and __SetPageKmemcg() with PageMemcgKmem() and an open-coded OR operation setting the memcg pointer with the MEMCG_DATA_KMEM bit. __ClearPageKmemcg() can be simple deleted, as the whole memcg_data is zeroed at once.

Quoted 4 lines

As a bonus, on !CONFIG_MEMCG build the PageMemcgKmem() check will be compiled out.

#12 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

In reply to Roman Gushchin

Hi.

include/linux/memcontrol.h enum page_memcg_data_flags { · line 380

Quoted 12 lines · Roman Gushchin
include/linux/memcontrol.h +5 -1
@@ -369,8 +371,12 @@ enum page_memcg_data_flags {
  */
 static inline struct mem_cgroup *page_memcg(struct page *page)
 {
+	unsigned long memcg_data = page->memcg_data;
+
 	VM_BUG_ON_PAGE(PageSlab(page), page);
-	return (struct mem_cgroup *)page->memcg_data;
+	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
+
+	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }

Shouldn't this change go also into page_memcg_rcu()? (I don't think the current single user (workingset_activation() would pass a non-slab kernel page but for consistency sake.)

Alternatively, I'm thinking why (in its single use) is there page_memcg_rcu() a separate function to page_memcg() (cross memcg page migration?).

Regards,
Michal

maintainer
#13 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

Quoted 18 lines · Michal Koutný

Hi.

include/linux/memcontrol.h enum page_memcg_data_flags { · line 380

Quoted 12 lines · Roman Gushchin
include/linux/memcontrol.h +5 -1
@@ -369,8 +371,12 @@ enum page_memcg_data_flags {
  */
 static inline struct mem_cgroup *page_memcg(struct page *page)
 {
+	unsigned long memcg_data = page->memcg_data;
+
 	VM_BUG_ON_PAGE(PageSlab(page), page);
-	return (struct mem_cgroup *)page->memcg_data;
+	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
+
+	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }

Shouldn't this change go also into page_memcg_rcu()? (I don't think the current single user (workingset_activation() would pass a non-slab kernel page but for consistency sake.)

+1

Quoted 3 lines

Alternatively, I'm thinking why (in its single use) is there page_memcg_rcu() a separate function to page_memcg() (cross memcg page migration?).

It goes back to commit 55779ec759ccc3c12b917b3712a7716e1140c652.

The activation code is the only path where page migration is not excluded. Because unlike with page state statistics, we don't really mind a race when counting an activation event.

I do think there is a bug, though: mem_cgroup_move_account() should use WRITE_ONCE() on page->mem_cgroup.

#14 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

Quoted 3 lines · Johannes Weiner

The activation code is the only path where page migration is not excluded. Because unlike with page state statistics, we don't really mind a race when counting an activation event.

Thanks for the explanation. I see why the accessor trio is justified.

Quoted 2 lines

I do think there is a bug, though: mem_cgroup_move_account() should use WRITE_ONCE() on page->mem_cgroup.

If this were a bug, wouldn't be the proper approach rcu_assign_pointer()/rcu_dereference() pair?

Michal

maintainer
#15 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

Quoted 5 lines · Michal Koutný
Quoted 2 lines · Johannes Weiner

I do think there is a bug, though: mem_cgroup_move_account() should use WRITE_ONCE() on page->mem_cgroup.

If this were a bug, wouldn't be the proper approach rcu_assign_pointer()/rcu_dereference() pair?

Yes, you're right.

I'll double check these code paths and follow up with a patch if necessary.

#16 lore raw

Re: [PATCH v4 4/4] mm: convert page kmemcg type to a page memcg flag

In reply to Michal Koutný
Quoted 18 lines · Michal Koutny

Hi.

include/linux/memcontrol.h enum page_memcg_data_flags { · line 380

Quoted 12 lines · Roman Gushchin
include/linux/memcontrol.h +5 -1
@@ -369,8 +371,12 @@ enum page_memcg_data_flags {
  */
 static inline struct mem_cgroup *page_memcg(struct page *page)
 {
+	unsigned long memcg_data = page->memcg_data;
+
 	VM_BUG_ON_PAGE(PageSlab(page), page);
-	return (struct mem_cgroup *)page->memcg_data;
+	VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_OBJCGS, page);
+
+	return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
 }

Shouldn't this change go also into page_memcg_rcu()? (I don't think the current single user (workingset_activation() would pass a non-slab kernel page but for consistency sake.)

Yes, a good idea, I will add it.

Thanks!