Skip to contentSkip to conversation

Fix compound_head() race

PATCH Superseded Kirill A. Shutemov 9 participants 48 messages Aug 19, 2015 – Aug 27, 2015

Subsystems (9)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix compound_head() race

Here's my attempt on fixing recently discovered race in compound_head(). It should make compound_head() reliable in all contexts.

The patchset is against Linus' tree. Let me know if it need to be rebased onto different baseline.

It's expected to have conflicts with my page-flags patchset and probably should be applied before it.

v3:
- Fix build without hugetlb;
- Drop page->first_page;
- Update comment for free_compound_page();
- Use 'unsigned int' for page order;

v2: Per Hugh's suggestion page->compound_head is moved into third double
word. This way we can avoid memory overhead which v1 had in some cases.

This place in struct page is rather overloaded. More testing is required to make sure we don't collide with anyone.

Kirill A. Shutemov (5):
mm: drop page->slab_page
zsmalloc: use page->private instead of page->first_page
mm: pack compound_dtor and compound_order into one word in struct page
mm: make compound_head() robust
mm: use 'unsigned int' for page order

 Documentation/vm/split_page_table_lock |  4 +-
 arch/xtensa/configs/iss_defconfig      |  1 -
 include/linux/mm.h                     | 82 +++++++++++-----------------------
 include/linux/mm_types.h               | 21 ++++++---
 include/linux/page-flags.h             | 80 ++++++++-------------------------
 mm/Kconfig                             | 12 -----
 mm/debug.c                             |  5 ---
 mm/huge_memory.c                       |  3 +-
 mm/hugetlb.c                           | 35 +++++++--------
 mm/internal.h                          |  8 ++--
 mm/memory-failure.c                    |  7 ---
 mm/page_alloc.c                        | 76 ++++++++++++++++++-------------
 mm/swap.c                              |  4 +-
 mm/zsmalloc.c                          | 11 +++--
 14 files changed, 133 insertions(+), 216 deletions(-)
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH v3 1/5 mm: drop page->slab_page

Since 8456a648cf44 ("slab: use struct page for slab management") nobody uses slab_page field in struct page.

Let's drop it.

---
 include/linux/mm_types.h | 1 -
 1 file changed, 1 deletion(-)
include/linux/mm_types.h +0 -1
index 0038ac7466fd..58620ac7f15c 100644
@@ -140,7 +140,6 @@ struct page {
 #endif
 		};
 
-		struct slab *slab_page; /* slab fields */
 		struct rcu_head rcu_head;	/* Used by SLAB
 						 * when destroying via RCU
 						 */
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v3 1/5
#2 lore raw

Re: [PATCHv3 1/5] mm: drop page->slab_page

Quoted 10 lines · Kirill A. Shutemov

Since 8456a648cf44 ("slab: use struct page for slab management") nobody uses slab_page field in struct page.

Let's drop it.

Quoted 10 lines
---
  include/linux/mm_types.h | 1 -
  1 file changed, 1 deletion(-)
include/linux/mm_types.h +0 -0
index 0038ac7466fd..58620ac7f15c 100644

include/linux/mm_types.h struct page { · line 145

Quoted 7 lines
include/linux/mm_types.h +0 -1
@@ -140,7 +140,6 @@ struct page {
  #endif
  		};
 
-		struct slab *slab_page; /* slab fields */
  		struct rcu_head rcu_head;	/* Used by SLAB
  						 * when destroying via RCU
  						 */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [PATCHv3 1/5] mm: drop page->slab_page

In reply to Kirill A. Shutemov
Quoted 4 lines · Kirill A. Shutemov

Since 8456a648cf44 ("slab: use struct page for slab management") nobody uses slab_page field in struct page.

Let's drop it.

Ah, how about dropping this comment in mm/slab.c:slab_destroy() as well?

/*
* RCU free overloads the RCU head over the LRU.
* slab_page has been overloeaded over the LRU,
* however it is not used from now on so that
* we can use it safely.
*/

Quoted 15 lines
---
  include/linux/mm_types.h | 1 -
  1 file changed, 1 deletion(-)
include/linux/mm_types.h +0 -0
index 0038ac7466fd..58620ac7f15c 100644

include/linux/mm_types.h struct page { · line 145

Quoted 7 lines
include/linux/mm_types.h +0 -1
@@ -140,7 +140,6 @@ struct page {
  #endif
  		};
 
-		struct slab *slab_page; /* slab fields */
  		struct rcu_head rcu_head;	/* Used by SLAB
  						 * when destroying via RCU
  						 */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCHv3 1/5] mm: drop page->slab_page

Quoted 6 lines · Vlastimil Babka
Quoted 4 lines · Kirill A. Shutemov

Since 8456a648cf44 ("slab: use struct page for slab management") nobody uses slab_page field in struct page.

Let's drop it.

Quoted 8 lines

Ah, how about dropping this comment in mm/slab.c:slab_destroy() as well?

/*
* RCU free overloads the RCU head over the LRU.
* slab_page has been overloeaded over the LRU,
* however it is not used from now on so that
* we can use it safely.
*/

Actually, whole block can be replaced by

	call_rcu(&page->rcu_head, kmem_rcu_free);

Thanks.

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

PATCH v3 2/5 zsmalloc: use page->private instead of page->first_page

In reply to Kirill A. Shutemov

We are going to rework how compound_head() work. It will not use page->first_page as we have it now.

The only other user of page->fisrt_page beyond compound pages is zsmalloc.

Let's use page->private instead of page->first_page here. It occupies the same storage space.

---
 mm/zsmalloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
mm/zsmalloc.c +5 -6
index 0a7f81aa2249..a85754e69879 100644
@@ -16,7 +16,7 @@
  * struct page(s) to form a zspage.
  *
  * Usage of struct page fields:
- *	page->first_page: points to the first component (0-order) page
+ *	page->private: points to the first component (0-order) page
  *	page->index (union with page->freelist): offset of the first object
  *		starting in this page. For the first page, this is
  *		always 0, so we use this field (aka freelist) to point
@@ -26,8 +26,7 @@
  *
  *	For _first_ page only:
  *
- *	page->private (union with page->first_page): refers to the
- *		component page after the first page
+ *	page->private: refers to the component page after the first page
  *		If the page is first_page for huge object, it stores handle.
  *		Look at size_class->huge.
  *	page->freelist: points to the first free object in zspage.
@@ -770,7 +769,7 @@ static struct page *get_first_page(struct page *page)
 	if (is_first_page(page))
 		return page;
 	else
-		return page->first_page;
+		return (struct page *)page_private(page);
 }
 
 static struct page *get_next_page(struct page *page)
@@ -955,7 +954,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 	 * Allocate individual pages and link them together as:
 	 * 1. first page->private = first sub-page
 	 * 2. all sub-pages are linked together using page->lru
-	 * 3. each sub-page is linked to the first page using page->first_page
+	 * 3. each sub-page is linked to the first page using page->private
 	 *
 	 * For each size class, First/Head pages are linked together using
 	 * page->lru. Also, we set PG_private to identify the first page
@@ -980,7 +979,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
 		if (i == 1)
 			set_page_private(first_page, (unsigned long)page);
 		if (i >= 1)
-			page->first_page = first_page;
+			set_page_private(first_page, (unsigned long)first_page);
 		if (i >= 2)
 			list_add(&page->lru, &prev_page->lru);
 		if (i == class->pages_per_zspage - 1)	/* last page */
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v3 2/5
#6 lore raw

Re: [PATCHv3 2/5] zsmalloc: use page->private instead of page->first_page

Quoted 4 lines · Kirill A. Shutemov

We are going to rework how compound_head() work. It will not use page->first_page as we have it now.

The only other user of page->fisrt_page beyond compound pages is

^ typo

Quoted 6 lines

zsmalloc.

Let's use page->private instead of page->first_page here. It occupies the same storage space.

Quoted 47 lines
---
  mm/zsmalloc.c | 11 +++++------
  1 file changed, 5 insertions(+), 6 deletions(-)
mm/zsmalloc.c +4 -5
index 0a7f81aa2249..a85754e69879 100644
@@ -16,7 +16,7 @@
   * struct page(s) to form a zspage.
   *
   * Usage of struct page fields:
- *	page->first_page: points to the first component (0-order) page
+ *	page->private: points to the first component (0-order) page
   *	page->index (union with page->freelist): offset of the first object
   *		starting in this page. For the first page, this is
   *		always 0, so we use this field (aka freelist) to point
@@ -26,8 +26,7 @@
   *
   *	For _first_ page only:
   *
- *	page->private (union with page->first_page): refers to the
- *		component page after the first page
+ *	page->private: refers to the component page after the first page
   *		If the page is first_page for huge object, it stores handle.
   *		Look at size_class->huge.
   *	page->freelist: points to the first free object in zspage.
@@ -770,7 +769,7 @@ static struct page *get_first_page(struct page *page)
  	if (is_first_page(page))
  		return page;
  	else
-		return page->first_page;
+		return (struct page *)page_private(page);
  }
 
  static struct page *get_next_page(struct page *page)
@@ -955,7 +954,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  	 * Allocate individual pages and link them together as:
  	 * 1. first page->private = first sub-page
  	 * 2. all sub-pages are linked together using page->lru
-	 * 3. each sub-page is linked to the first page using page->first_page
+	 * 3. each sub-page is linked to the first page using page->private
  	 *
  	 * For each size class, First/Head pages are linked together using
  	 * page->lru. Also, we set PG_private to identify the first page

mm/zsmalloc.c static struct page *alloc_zspage(struct size_class *class, gfp_t flags) · line 985

Quoted 8 lines
mm/zsmalloc.c +1 -1
@@ -980,7 +979,7 @@ static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  		if (i == 1)
  			set_page_private(first_page, (unsigned long)page);
  		if (i >= 1)
-			page->first_page = first_page;
+			set_page_private(first_page, (unsigned long)first_page);
  		if (i >= 2)
  			list_add(&page->lru, &prev_page->lru);
  		if (i == class->pages_per_zspage - 1)	/* last page */

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

PATCH v3 3/5 mm: pack compound_dtor and compound_order into one word in struct page

In reply to Kirill A. Shutemov

The patch halves space occupied by compound_dtor and compound_order in struct page.

For compound_order, it's trivial long -> int/short conversion.

For get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.

This patch free up one word in tail pages for reuse. This is preparation for the next patch.

---
 include/linux/mm.h       | 24 +++++++++++++++++++-----
 include/linux/mm_types.h | 11 +++++++----
 mm/hugetlb.c             |  8 ++++----
 mm/page_alloc.c          | 11 ++++++++++-
 4 files changed, 40 insertions(+), 14 deletions(-)
include/linux/mm.h +19 -5
index 2e872f92dbac..0735bc0a351a 100644
@@ -575,18 +575,32 @@ int split_free_page(struct page *page);
 /*
  * Compound pages have a destructor function.  Provide a
  * prototype for that function and accessor functions.
- * These are _only_ valid on the head of a PG_compound page.
+ * These are _only_ valid on the head of a compound page.
  */
+typedef void compound_page_dtor(struct page *);
+
+/* Keep the enum in sync with compound_page_dtors array in mm/page_alloc.c */
+enum {
+	NULL_COMPOUND_DTOR,
+	COMPOUND_PAGE_DTOR,
+#ifdef CONFIG_HUGETLB_PAGE
+	HUGETLB_PAGE_DTOR,
+#endif
+	NR_COMPOUND_DTORS,
+};
+extern compound_page_dtor * const compound_page_dtors[];
 
 static inline void set_compound_page_dtor(struct page *page,
-						compound_page_dtor *dtor)
+		unsigned int compound_dtor)
 {
-	page[1].compound_dtor = dtor;
+	VM_BUG_ON_PAGE(compound_dtor >= NR_COMPOUND_DTORS, page);
+	page[1].compound_dtor = compound_dtor;
 }
 
 static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
 {
-	return page[1].compound_dtor;
+	VM_BUG_ON_PAGE(page[1].compound_dtor >= NR_COMPOUND_DTORS, page);
+	return compound_page_dtors[page[1].compound_dtor];
 }
 
 static inline int compound_order(struct page *page)
@@ -596,7 +610,7 @@ static inline int compound_order(struct page *page)
 	return page[1].compound_order;
 }
 
-static inline void set_compound_order(struct page *page, unsigned long order)
+static inline void set_compound_order(struct page *page, unsigned int order)
 {
 	page[1].compound_order = order;
 }
include/linux/mm_types.h +7 -4
index 58620ac7f15c..63cdfe7ec336 100644
@@ -28,8 +28,6 @@ struct mem_cgroup;
 		IS_ENABLED(CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK))
 #define ALLOC_SPLIT_PTLOCKS	(SPINLOCK_SIZE > BITS_PER_LONG/8)
 
-typedef void compound_page_dtor(struct page *);
-
 /*
  * Each physical page in the system has a struct page associated with
  * it to keep track of whatever it is we are using the page for at the
@@ -145,8 +143,13 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
-			compound_page_dtor *compound_dtor;
-			unsigned long compound_order;
+#ifdef CONFIG_64BIT
+			unsigned int compound_dtor;
+			unsigned int compound_order;
+#else
+			unsigned short int compound_dtor;
+			unsigned short int compound_order;
+#endif
 		};
 
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && USE_SPLIT_PMD_PTLOCKS
mm/hugetlb.c +4 -4
index a8c3087089d8..8ea74caa1fa8 100644
@@ -969,7 +969,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)
 				1 << PG_writeback);
 	}
 	VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
-	set_compound_page_dtor(page, NULL);
+	set_compound_page_dtor(page, NULL_COMPOUND_DTOR);
 	set_page_refcounted(page);
 	if (hstate_is_gigantic(h)) {
 		destroy_compound_gigantic_page(page, huge_page_order(h));
@@ -1065,7 +1065,7 @@ void free_huge_page(struct page *page)
 static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
 {
 	INIT_LIST_HEAD(&page->lru);
-	set_compound_page_dtor(page, free_huge_page);
+	set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
 	spin_lock(&hugetlb_lock);
 	set_hugetlb_cgroup(page, NULL);
 	h->nr_huge_pages++;
@@ -1117,7 +1117,7 @@ int PageHuge(struct page *page)
 		return 0;
 
 	page = compound_head(page);
-	return get_compound_page_dtor(page) == free_huge_page;
+	return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
 }
 EXPORT_SYMBOL_GPL(PageHuge);
 
@@ -1314,7 +1314,7 @@ static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
 	if (page) {
 		INIT_LIST_HEAD(&page->lru);
 		r_nid = page_to_nid(page);
-		set_compound_page_dtor(page, free_huge_page);
+		set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
 		set_hugetlb_cgroup(page, NULL);
 		/*
 		 * We incremented the global counters already
mm/page_alloc.c +10 -1
index df959b7d6085..c6733cc3cbce 100644
@@ -208,6 +208,15 @@ static char * const zone_names[MAX_NR_ZONES] = {
 	 "Movable",
 };
 
+static void free_compound_page(struct page *page);
+compound_page_dtor * const compound_page_dtors[] = {
+	NULL,
+	free_compound_page,
+#ifdef CONFIG_HUGETLB_PAGE
+	free_huge_page,
+#endif
+};
+
 int min_free_kbytes = 1024;
 int user_min_free_kbytes = -1;
 
@@ -437,7 +446,7 @@ void prep_compound_page(struct page *page, unsigned long order)
 	int i;
 	int nr_pages = 1 << order;
 
-	set_compound_page_dtor(page, free_compound_page);
+	set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
 	set_compound_order(page, order);
 	__SetPageHead(page);
 	for (i = 1; i < nr_pages; i++) {
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#8 lore raw

Re: [PATCHv3 3/5] mm: pack compound_dtor and compound_order into one word in struct page

Quoted 18 lines · On Wed, 19 Aug 2015 12:21:44 +0300 "Kirill A. Shutemov"…

The patch halves space occupied by compound_dtor and compound_order in struct page.

For compound_order, it's trivial long -> int/short conversion.

For get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.

This patch free up one word in tail pages for reuse. This is preparation for the next patch.

...

include/linux/mm_types.h +0 -0
@@ -145,8 +143,13 @@ struct page {
 						 */
 		/* First tail page of compound page */

include/linux/mm_types.h struct page { · line 152

Quoted 10 lines
include/linux/mm_types.h +7 -2
 		struct {
-			compound_page_dtor *compound_dtor;
-			unsigned long compound_order;
+#ifdef CONFIG_64BIT
+			unsigned int compound_dtor;
+			unsigned int compound_order;
+#else
+			unsigned short int compound_dtor;
+			unsigned short int compound_order;
+#endif

Why not use ushort for 64-bit as well?

It would be clearer if that new enum had a name, so we use "enum compound_dtor_id" everywhere instead of a bare uint.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw

Re: [PATCHv3 3/5] mm: pack compound_dtor and compound_order into one word in struct page

Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Wed, 19 Aug 2015 12:21:44 +0300 "Kirill A. Shutemov"…

The patch halves space occupied by compound_dtor and compound_order in struct page.

For compound_order, it's trivial long -> int/short conversion.

For get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.

This patch free up one word in tail pages for reuse. This is preparation for the next patch.

...

Quoted 15 lines

include/linux/mm_types.h struct page { · line 152

Quoted 13 lines
include/linux/mm_types.h +7 -2
@@ -145,8 +143,13 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
-			compound_page_dtor *compound_dtor;
-			unsigned long compound_order;
+#ifdef CONFIG_64BIT
+			unsigned int compound_dtor;
+			unsigned int compound_order;
+#else
+			unsigned short int compound_dtor;
+			unsigned short int compound_order;
+#endif

Why not use ushort for 64-bit as well?

Yeah, I have asked the same in the previous round. So I've tried to compile with ushort. The resulting code was slightly larger
text data bss dec hex filename

 476370   90811   44632  611813   955e5 mm/built-in.o.prev
 476418   90811   44632  611861   95615 mm/built-in.o.after

E.g. prep_compound_page
before:

4c6b:       c7 47 68 01 00 00 00    movl   $0x1,0x68(%rdi)
4c72:       89 77 6c                mov    %esi,0x6c(%rdi)
after:
4c6c:       66 c7 47 68 01 00       movw   $0x1,0x68(%rdi)
4c72:       66 89 77 6a             mov    %si,0x6a(%rdi)

which looks very similar to me but I am not an expert here so it might possible that movw is slower.

__free_pages_ok
before:
63af: 8b 77 6c mov 0x6c(%rdi),%esi
after:
63b1: 0f b7 77 6a movzwl 0x6a(%rdi),%esi

which looks like a worse code to me. Whether this all is measurable or worth it I dunno. The ifdef is ugly but maybe the ugliness is a destiny for struct page.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw

Re: [PATCHv3 3/5] mm: pack compound_dtor and compound_order into one word in struct page

Quoted 57 lines · Michal Hocko
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Wed, 19 Aug 2015 12:21:44 +0300 "Kirill A. Shutemov"…

The patch halves space occupied by compound_dtor and compound_order in struct page.

For compound_order, it's trivial long -> int/short conversion.

For get_compound_page_dtor(), we now use hardcoded table for destructor lookup and store its index in the struct page instead of direct pointer to destructor. It shouldn't be a big trouble to maintain the table: we have only two destructor and NULL currently.

This patch free up one word in tail pages for reuse. This is preparation for the next patch.

...

Quoted 15 lines

include/linux/mm_types.h struct page { · line 152

Quoted 13 lines
include/linux/mm_types.h +7 -2
@@ -145,8 +143,13 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
-			compound_page_dtor *compound_dtor;
-			unsigned long compound_order;
+#ifdef CONFIG_64BIT
+			unsigned int compound_dtor;
+			unsigned int compound_order;
+#else
+			unsigned short int compound_dtor;
+			unsigned short int compound_order;
+#endif

Why not use ushort for 64-bit as well?

Yeah, I have asked the same in the previous round. So I've tried to compile with ushort. The resulting code was slightly larger
text data bss dec hex filename

 476370   90811   44632  611813   955e5 mm/built-in.o.prev
 476418   90811   44632  611861   95615 mm/built-in.o.after

E.g. prep_compound_page
before:

4c6b:       c7 47 68 01 00 00 00    movl   $0x1,0x68(%rdi)
4c72:       89 77 6c                mov    %esi,0x6c(%rdi)
after:
4c6c:       66 c7 47 68 01 00       movw   $0x1,0x68(%rdi)
4c72:       66 89 77 6a             mov    %si,0x6a(%rdi)

which looks very similar to me but I am not an expert here so it might possible that movw is slower.

__free_pages_ok
before:
63af: 8b 77 6c mov 0x6c(%rdi),%esi
after:
63b1: 0f b7 77 6a movzwl 0x6a(%rdi),%esi

Quoted 3 lines

which looks like a worse code to me. Whether this all is measurable or worth it I dunno. The ifdef is ugly but maybe the ugliness is a destiny for struct page.

I don't care about the ifdef that much. If you guys prefer to drop it I'm fine with that.

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#11 lore raw

Re: [PATCHv3 3/5] mm: pack compound_dtor and compound_order into one word in struct page

Quoted 2 lines · Kirill A. Shutemov
Quoted 1 line · Michal Hocko

On Thu 20-08-15 16:26:04, Andrew Morton wrote:

[...]

Quoted 25 lines
Quoted 25 lines
Quoted 1 line

Why not use ushort for 64-bit as well?

Yeah, I have asked the same in the previous round. So I've tried to compile with ushort. The resulting code was slightly larger
text data bss dec hex filename

 476370   90811   44632  611813   955e5 mm/built-in.o.prev
 476418   90811   44632  611861   95615 mm/built-in.o.after

E.g. prep_compound_page
before:

4c6b:       c7 47 68 01 00 00 00    movl   $0x1,0x68(%rdi)
4c72:       89 77 6c                mov    %esi,0x6c(%rdi)
after:
4c6c:       66 c7 47 68 01 00       movw   $0x1,0x68(%rdi)
4c72:       66 89 77 6a             mov    %si,0x6a(%rdi)

which looks very similar to me but I am not an expert here so it might possible that movw is slower.

__free_pages_ok
before:
63af: 8b 77 6c mov 0x6c(%rdi),%esi
after:
63b1: 0f b7 77 6a movzwl 0x6a(%rdi),%esi

Quoted 6 lines
Quoted 3 lines

which looks like a worse code to me. Whether this all is measurable or worth it I dunno. The ifdef is ugly but maybe the ugliness is a destiny for struct page.

I don't care about the ifdef that much. If you guys prefer to drop it I'm fine with that.

I can live with it. It makes the struct more complicated which is what struck me. If there is a good reason and a better generated code is a good one then I do not object but please make it a separate patch so that we do not wonder why this has been done in the future.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#12 lore raw

PATCH v3 4/5 mm: make compound_head() robust

In reply to Kirill A. Shutemov

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

	CPU0					CPU1

isolate_migratepages_block()
page_count()

    compound_head()
      !!PageTail() == true
					put_page()
					  tail->first_page = NULL
      head = tail->first_page
					alloc_pages(__GFP_COMP)
					   prep_compound_page()
					     tail->first_page = head
					     __SetPageTail(p);
      !!PageTail() == true
    <head == NULL dereferencing>

The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.

We can fix the race by changing how encode PageTail() and compound_head() within struct page to be able to update them in one shot.

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word. It can be used to encode PageTail(). And if the bit set, rest of the word is pointer to head page.

---
 Documentation/vm/split_page_table_lock |  4 +-
 arch/xtensa/configs/iss_defconfig      |  1 -
 include/linux/mm.h                     | 53 ++--------------------
 include/linux/mm_types.h               |  9 +++-
 include/linux/page-flags.h             | 80 ++++++++--------------------------
 mm/Kconfig                             | 12 -----
 mm/debug.c                             |  5 ---
 mm/huge_memory.c                       |  3 +-
 mm/hugetlb.c                           |  8 +---
 mm/internal.h                          |  4 +-
 mm/memory-failure.c                    |  7 ---
 mm/page_alloc.c                        | 38 ++++++++--------
 mm/swap.c                              |  4 +-
 13 files changed, 58 insertions(+), 170 deletions(-)
Documentation/vm/split_page_table_lock +2 -2
index 6dea4fd5c961..62842a857dab 100644
@@ -54,8 +54,8 @@ everything required is done by pgtable_page_ctor() and pgtable_page_dtor(),
 which must be called on PTE table allocation / freeing.
 
 Make sure the architecture doesn't use slab allocator for page table
-allocation: slab uses page->slab_cache and page->first_page for its pages.
-These fields share storage with page->ptl.
+allocation: slab uses page->slab_cache for its pages.
+This field shares storage with page->ptl.
 
 PMD split lock only makes sense if you have more than two page table
 levels.
arch/xtensa/configs/iss_defconfig +0 -1
index e4d193e7a300..5c7c385f21c4 100644
@@ -169,7 +169,6 @@ CONFIG_FLATMEM_MANUAL=y
 # CONFIG_SPARSEMEM_MANUAL is not set
 CONFIG_FLATMEM=y
 CONFIG_FLAT_NODE_MEM_MAP=y
-CONFIG_PAGEFLAGS_EXTENDED=y
 CONFIG_SPLIT_PTLOCK_CPUS=4
 # CONFIG_PHYS_ADDR_T_64BIT is not set
 CONFIG_ZONE_DMA_FLAG=1
include/linux/mm.h +3 -50

77 lines not on this page · raw message

include/linux/mm_types.h +7 -2
index 63cdfe7ec336..e324768b6cc7 100644
@@ -120,7 +120,12 @@ struct page {
 		};
 	};
 
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
+	 * for non-tail pages.
+	 */
 	union {
 		struct list_head lru;	/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !
@@ -143,6 +148,7 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
+			unsigned long compound_head; /* If bit zero is set */
 #ifdef CONFIG_64BIT
 			unsigned int compound_dtor;
 			unsigned int compound_order;
@@ -174,7 +180,6 @@ struct page {
 #endif
 #endif
 		struct kmem_cache *slab_cache;	/* SL[AU]B: Pointer to slab */
-		struct page *first_page;	/* Compound tail pages */
 	};
 
 #ifdef CONFIG_MEMCG
include/linux/page-flags.h +18 -62

115 lines not on this page · raw message

mm/Kconfig +0 -12
index e79de2bd12cd..454579d31081 100644
@@ -200,18 +200,6 @@ config MEMORY_HOTREMOVE
 	depends on MEMORY_HOTPLUG && ARCH_ENABLE_MEMORY_HOTREMOVE
 	depends on MIGRATION
 
-#
-# If we have space for more page flags then we can enable additional
-# optimizations and functionality.
-#
-# Regular Sparsemem takes page flag bits for the sectionid if it does not
-# use a virtual memmap. Disable extended page flags for 32 bit platforms
-# that require the use of a sectionid in the page flags.
-#
-config PAGEFLAGS_EXTENDED
-	def_bool y
-	depends on 64BIT || SPARSEMEM_VMEMMAP || !SPARSEMEM
-
 # Heavily threaded applications may benefit from splitting the mm-wide
 # page_table_lock, so that faults on different parts of the user address
 # space can be handled with less contention: split it at this NR_CPUS.
mm/debug.c +0 -5
index 76089ddf99ea..205e5ef957ab 100644
@@ -25,12 +25,7 @@ static const struct trace_print_flags pageflag_names[] = {
 	{1UL << PG_private,		"private"	},
 	{1UL << PG_private_2,		"private_2"	},
 	{1UL << PG_writeback,		"writeback"	},
-#ifdef CONFIG_PAGEFLAGS_EXTENDED
 	{1UL << PG_head,		"head"		},
-	{1UL << PG_tail,		"tail"		},
-#else
-	{1UL << PG_compound,		"compound"	},
-#endif
 	{1UL << PG_swapcache,		"swapcache"	},
 	{1UL << PG_mappedtodisk,	"mappedtodisk"	},
 	{1UL << PG_reclaim,		"reclaim"	},
mm/huge_memory.c +1 -2
index 097c7a4bfbd9..330377f83ac7 100644
@@ -1686,8 +1686,7 @@ static void __split_huge_page_refcount(struct page *page,
 				      (1L << PG_unevictable)));
 		page_tail->flags |= (1L << PG_dirty);
 
-		/* clear PageTail before overwriting first_page */
-		smp_wmb();
+		clear_compound_head(page_tail);
 
 		/*
 		 * __split_huge_page_splitting() already set the
mm/hugetlb.c +2 -6
index 8ea74caa1fa8..53c0709fd87b 100644
@@ -824,9 +824,8 @@ static void destroy_compound_gigantic_page(struct page *page,
 	struct page *p = page + 1;
 
 	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
-		__ClearPageTail(p);
+		clear_compound_head(p);
 		set_page_refcounted(p);
-		p->first_page = NULL;
 	}
 
 	set_compound_order(page, 0);
@@ -1099,10 +1098,7 @@ static void prep_compound_gigantic_page(struct page *page, unsigned long order)
 		 */
 		__ClearPageReserved(p);
 		set_page_count(p, 0);
-		p->first_page = page;
-		/* Make sure p->first_page is always valid for PageTail() */
-		smp_wmb();
-		__SetPageTail(p);
+		set_compound_head(p, page);
 	}
 }
 
mm/internal.h +2 -2
index 36b23f1e2ca6..89e21a07080a 100644
@@ -61,9 +61,9 @@ static inline void __get_page_tail_foll(struct page *page,
 	 * speculative page access (like in
 	 * page_cache_get_speculative()) on tail pages.
 	 */
-	VM_BUG_ON_PAGE(atomic_read(&page->first_page->_count) <= 0, page);
+	VM_BUG_ON_PAGE(atomic_read(&compound_head(page)->_count) <= 0, page);
 	if (get_page_head)
-		atomic_inc(&page->first_page->_count);
+		atomic_inc(&compound_head(page)->_count);
 	get_huge_page_tail(page);
 }
 
mm/memory-failure.c +0 -7
index 1f4446a90cef..4d1a5de9653d 100644
@@ -787,8 +787,6 @@ static int me_huge_page(struct page *p, unsigned long pfn)
 #define lru		(1UL << PG_lru)
 #define swapbacked	(1UL << PG_swapbacked)
 #define head		(1UL << PG_head)
-#define tail		(1UL << PG_tail)
-#define compound	(1UL << PG_compound)
 #define slab		(1UL << PG_slab)
 #define reserved	(1UL << PG_reserved)
 
@@ -811,12 +809,7 @@ static struct page_state {
 	 */
 	{ slab,		slab,		MF_MSG_SLAB,	me_kernel },
 
-#ifdef CONFIG_PAGEFLAGS_EXTENDED
 	{ head,		head,		MF_MSG_HUGE,		me_huge_page },
-	{ tail,		tail,		MF_MSG_HUGE,		me_huge_page },
-#else
-	{ compound,	compound,	MF_MSG_HUGE,		me_huge_page },
-#endif
 
 	{ sc|dirty,	sc|dirty,	MF_MSG_DIRTY_SWAPCACHE,	me_swapcache_dirty },
 	{ sc|dirty,	sc,		MF_MSG_CLEAN_SWAPCACHE,	me_swapcache_clean },
mm/page_alloc.c +21 -17
index c6733cc3cbce..78859d47aaf4 100644
@@ -424,15 +424,15 @@ out:
 /*
  * Higher-order pages are called "compound pages".  They are structured thusly:
  *
- * The first PAGE_SIZE page is called the "head page".
+ * The first PAGE_SIZE page is called the "head page" and have PG_head set.
  *
- * The remaining PAGE_SIZE pages are called "tail pages".
+ * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
+ * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
  *
- * All pages have PG_compound set.  All tail pages have their ->first_page
- * pointing at the head page.
+ * The first tail page's ->compound_dtor holds the offset in array of compound
+ * page destructors. See compound_page_dtors.
  *
- * The first tail page's ->lru.next holds the address of the compound page's
- * put_page() function.  Its ->lru.prev holds the order of allocation.
+ * The first tail page's ->compound_order holds the order of allocation.
  * This usage means that zero-order pages may not be compound.
  */
 
@@ -452,10 +452,7 @@ void prep_compound_page(struct page *page, unsigned long order)
 	for (i = 1; i < nr_pages; i++) {
 		struct page *p = page + i;
 		set_page_count(p, 0);
-		p->first_page = page;
-		/* Make sure p->first_page is always valid for PageTail() */
-		smp_wmb();
-		__SetPageTail(p);
+		set_compound_head(p, page);
 	}
 }
 
@@ -830,17 +827,24 @@ static void free_one_page(struct zone *zone,
 
 static int free_tail_pages_check(struct page *head_page, struct page *page)
 {
-	if (!IS_ENABLED(CONFIG_DEBUG_VM))
-		return 0;
+	int ret = 1;
+
+	if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
+		ret = 0;
+		goto out;
+	}
 	if (unlikely(!PageTail(page))) {
 		bad_page(page, "PageTail not set", 0);
-		return 1;
+		goto out;
 	}
-	if (unlikely(page->first_page != head_page)) {
-		bad_page(page, "first_page not consistent", 0);
-		return 1;
+	if (unlikely(compound_head(page) != head_page)) {
+		bad_page(page, "compound_head not consistent", 0);
+		goto out;
 	}
-	return 0;
+	ret = 0;
+out:
+	clear_compound_head(page);
+	return ret;
 }
 
 static void __meminit __init_single_page(struct page *page, unsigned long pfn,
mm/swap.c +2 -2
index a3a0a2f1f7c3..faa9e1687dea 100644
@@ -200,7 +200,7 @@ out_put_single:
 				__put_single_page(page);
 			return;
 		}
-		VM_BUG_ON_PAGE(page_head != page->first_page, page);
+		VM_BUG_ON_PAGE(page_head != compound_head(page), page);
 		/*
 		 * We can release the refcount taken by
 		 * get_page_unless_zero() now that
@@ -261,7 +261,7 @@ static void put_compound_page(struct page *page)
 	 *  Case 3 is possible, as we may race with
 	 *  __split_huge_page_refcount tearing down a THP page.
 	 */
-	page_head = compound_head_by_tail(page);
+	page_head = compound_head(page);
 	if (!__compound_tail_refcounted(page_head))
 		put_unrefcounted_compound_page(page_head, page);
 	else
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#13 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 29 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

	CPU0					CPU1

isolate_migratepages_block()
page_count()

    compound_head()
      !!PageTail() == true
					put_page()
					  tail->first_page = NULL
      head = tail->first_page
					alloc_pages(__GFP_COMP)
					   prep_compound_page()
					     tail->first_page = head
					     __SetPageTail(p);
      !!PageTail() == true
    <head == NULL dereferencing>

The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.

We can fix the race by changing how encode PageTail() and compound_head() within struct page to be able to update them in one shot.

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

Quoted 8 lines

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word. It can be used to encode PageTail(). And if the bit set, rest of the word is pointer to head page.

So nothing else which participates in the union in the "Third double word block" is allowed to use bit zero of the first word.

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

Anyway, this is quite subtle and there's a risk that people will accidentally break it later on. I don't think the patch puts sufficient documentation in place to prevent this. And even documentation might not be enough to prevent accidents.

Quoted 20 lines

...

include/linux/mm_types.h +6 -1
@@ -120,7 +120,12 @@ struct page {
 		};
 	};
 
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
+	 * for non-tail pages.
+	 */
 	union {
 		struct list_head lru;	/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !

include/linux/mm_types.h struct page { · line 151

Quoted 4 lines
include/linux/mm_types.h +1 -0
@@ -143,6 +148,7 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
+			unsigned long compound_head; /* If bit zero is set */

I think the comments around here should have more details and should be louder!

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#14 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 36 lines · Andrew Morton
Quoted 34 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

	CPU0					CPU1

isolate_migratepages_block()
page_count()

    compound_head()
      !!PageTail() == true
					put_page()
					  tail->first_page = NULL
      head = tail->first_page
					alloc_pages(__GFP_COMP)
					   prep_compound_page()
					     tail->first_page = head
					     __SetPageTail(p);
      !!PageTail() == true
    <head == NULL dereferencing>

The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.

We can fix the race by changing how encode PageTail() and compound_head() within struct page to be able to update them in one shot.

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 6 lines
Quoted 3 lines

That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word. It can be used to encode PageTail(). And if the bit set, rest of the word is pointer to head page.

So nothing else which participates in the union in the "Third double word block" is allowed to use bit zero of the first word.

Correct.

Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

For head/small pages it gets trickier. I convinced myself that it should be safe this way:

All fields it shares space with are pointers (with possible exception of pmd_huge_pte, see below) to objects with sizeof() > 1. I think it's reasonable to expect that the bit 0 in such pointers would be clear due alignment. We do the same for page->mapping.

On pmd_huge_pte: it's pgtable_t which on most architectures is typedef to struct page *. That should not create any conflicts. On some architectures it's pte_t *, which is fine too. On arc it's virtual address of the page in form of unsigned long. It should work.

The worry I have about pmd_huge_pte is that some new architecture may choose to implement pgtable_t as pfn and that will collide on bit 0. :-/

We can address this worry by shifting pmd_huge_pte to the second word in the double word block. But I'm not sure if we should.

And of course there's chance that these field are used not according to its type. I didn't find such cases, but I can't guarantee that they don't exist.

I tested patched kernel with all three SLAB allocator and was not able to crash it under trinity. More testing is required.

Quoted 3 lines

Anyway, this is quite subtle and there's a risk that people will accidentally break it later on. I don't think the patch puts sufficient documentation in place to prevent this.

I would appreciate for suggestion on place and form of documentation.

Quoted 1 line

And even documentation might not be enough to prevent accidents.

The only think I can propose is VM_BUG_ON() in PageTail() and compound_head() which would ensure that page->compound_page points to place within MAX_ORDER_NR_PAGES before the current page if bit 0 is set.

Do you consider this helpful?

Quoted 19 lines
Quoted 19 lines

...

include/linux/mm_types.h +6 -1
@@ -120,7 +120,12 @@ struct page {
 		};
 	};
 
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
+	 * for non-tail pages.
+	 */
 	union {
 		struct list_head lru;	/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !
Quoted 8 lines

include/linux/mm_types.h struct page { · line 151

Quoted 5 lines
include/linux/mm_types.h +1 -0
@@ -143,6 +148,7 @@ struct page {
 						 */
 		/* First tail page of compound page */
 		struct {
+			unsigned long compound_head; /* If bit zero is set */

I think the comments around here should have more details and should be louder!

I'm always bad when it comes to documentation. Is it enough?

/*
* Third double word block
*
* WARNING: bit 0 of the first word encode PageTail(). That means
* the rest users of the storage space MUST NOT use the bit to
* avoid collision and false-positive PageTail().
*/

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#15 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 7 lines · Kirill A. Shutemov
Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

Sl*b allocators only do a virt_to_head_page on tail pages.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 11 lines · Christoph Lameter
Quoted 7 lines · Kirill A. Shutemov
Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

Sl*b allocators only do a virt_to_head_page on tail pages.

The question was whether it's safe to assume that the bit 0 is always zero in the word as this bit will encode PageTail().

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#17 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 8 lines · On Fri, 21 Aug 2015 22:31:09 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 7 lines · Christoph Lameter
Quoted 5 lines · Kirill A. Shutemov
Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

Sl*b allocators only do a virt_to_head_page on tail pages.

The question was whether it's safe to assume that the bit 0 is always zero in the word as this bit will encode PageTail().

That wasn't my question actually...

What I'm wondering is: if this page is being used for slab, will any code path ever run PageTail() against it? If not, we don't need to be concerned about that bit.

And slab was just the example I chose. The same question petains to all other uses of that union.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Fri, 21 Aug 2015 22:31:09 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 11 lines · Christoph Lameter
Quoted 7 lines · Kirill A. Shutemov
Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

Sl*b allocators only do a virt_to_head_page on tail pages.

Quoted 8 lines
Quoted 2 lines

The question was whether it's safe to assume that the bit 0 is always zero in the word as this bit will encode PageTail().

That wasn't my question actually...

What I'm wondering is: if this page is being used for slab, will any code path ever run PageTail() against it? If not, we don't need to be concerned about that bit.

virt_to_head_page will run PageTail because it uses compound_head(). And compound_head needs to use the first_page pointer if its a tail page.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#19 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Andrew Morton
Quoted 15 lines · Andrew Morton
Quoted 13 lines · On Fri, 21 Aug 2015 22:31:09 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 11 lines · Christoph Lameter
Quoted 7 lines · Kirill A. Shutemov
Quoted 2 lines

Is this really true? For example if it's a slab page, will that page ever be inspected by code which is looking for the PageTail bit?

+Christoph.

What we know for sure is that space is not used in tail pages, otherwise it would collide with current compound_dtor.

Sl*b allocators only do a virt_to_head_page on tail pages.

Quoted 8 lines
Quoted 2 lines

The question was whether it's safe to assume that the bit 0 is always zero in the word as this bit will encode PageTail().

That wasn't my question actually...

What I'm wondering is: if this page is being used for slab, will any code path ever run PageTail() against it? If not, we don't need to be concerned about that bit.

Pfn scanners such as compaction might inspect such pages and run compound_head() (and thus PageTail) on them. I think no kind of page within a zone (slab or otherwise) is "protected" from this, which is why it needs to be robust.

Quoted 3 lines

And slab was just the example I chose. The same question petains to all other uses of that union.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Kirill A. Shutemov
Quoted 7 lines · Kirill A. Shutemov
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?
For pgtable_t I can't think of anything better than a warning in the generic definition in include/asm-generic/page.h and hope that anyone reimplementing it for a new arch will look there first.
The lru part is probably the hardest to prevent danger. It can be used for any private purposes. Hopefully everyone currently uses only standard list operations here, and the list poison values don't set bit 0. But I see there can be some arbitrary CONFIG_ILLEGAL_POINTER_VALUE added to the poisons, so maybe that's worth some build error check? Anyway we would be imposing restrictions on types that are not ours, so there might be some resistance...

Quoted 5 lines
Quoted 4 lines

Anyway, this is quite subtle and there's a risk that people will accidentally break it later on. I don't think the patch puts sufficient documentation in place to prevent this.

Quoted 7 lines

I would appreciate for suggestion on place and form of documentation.

Quoted 1 line

And even documentation might not be enough to prevent accidents.

The only think I can propose is VM_BUG_ON() in PageTail() and compound_head() which would ensure that page->compound_page points to place within MAX_ORDER_NR_PAGES before the current page if bit 0 is set.

That should probably catch some bad stuff, but probably only moments before it would crash anyway if the pointer was bogus. But I also don't see better way, because we can't proactively put checks in those who would "misbehave", as we don't know who they are. Putting more debug checks in e.g. page freeing might help, but probably not much.

Quoted 11 lines

Do you consider this helpful?

Quoted 9 lines
Quoted 9 lines

...

include/linux/mm_types.h +0 -0
@@ -120,7 +120,12 @@ struct page {
  		};
  	};
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
include/linux/mm_types.h +4 -1
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
Quoted 8 lines

include/linux/mm_types.h struct page { · line 149

Quoted 8 lines
include/linux/mm_types.h +2 -0
+	 * for non-tail pages.
+	 */
  	union {
  		struct list_head lru;	/* Pageout list, eg. active_list
  					 * protected by zone->lru_lock !
@@ -143,6 +148,7 @@ struct page {
  						 */
  		/* First tail page of compound page */

Note that compound_head is not just in the *first* tail page. Only the rest is.

Quoted 8 lines
Quoted 5 lines

include/linux/mm_types.h struct page { · line 151

Quoted 2 lines
include/linux/mm_types.h +1 -0
  		struct {
+			unsigned long compound_head; /* If bit zero is set */

I think the comments around here should have more details and should be louder!

I'm always bad when it comes to documentation. Is it enough?

Quoted 8 lines

/*
* Third double word block
*
* WARNING: bit 0 of the first word encode PageTail(). That means
* the rest users of the storage space MUST NOT use the bit to
* avoid collision and false-positive PageTail().
*/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 8 lines · Vlastimil Babka
Quoted 7 lines · Kirill A. Shutemov
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

Quoted 3 lines

For pgtable_t I can't think of anything better than a warning in the generic definition in include/asm-generic/page.h and hope that anyone reimplementing it for a new arch will look there first.

I will move it to other word, just in case.

Quoted 7 lines

The lru part is probably the hardest to prevent danger. It can be used for any private purposes. Hopefully everyone currently uses only standard list operations here, and the list poison values don't set bit 0. But I see there can be some arbitrary CONFIG_ILLEGAL_POINTER_VALUE added to the poisons, so maybe that's worth some build error check? Anyway we would be imposing restrictions on types that are not ours, so there might be some resistance...

I will add BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);

Quoted 12 lines
Quoted 11 lines
Quoted 3 lines

Anyway, this is quite subtle and there's a risk that people will accidentally break it later on. I don't think the patch puts sufficient documentation in place to prevent this.

I would appreciate for suggestion on place and form of documentation.

Quoted 1 line

And even documentation might not be enough to prevent accidents.

The only think I can propose is VM_BUG_ON() in PageTail() and compound_head() which would ensure that page->compound_page points to place within MAX_ORDER_NR_PAGES before the current page if bit 0 is set.

Quoted 5 lines

That should probably catch some bad stuff, but probably only moments before it would crash anyway if the pointer was bogus. But I also don't see better way, because we can't proactively put checks in those who would "misbehave", as we don't know who they are. Putting more debug checks in e.g. page freeing might help, but probably not much.

So, do you think it worth it or not after all?

Quoted 12 lines
Quoted 12 lines

Do you consider this helpful?

Quoted 9 lines
Quoted 9 lines

...

include/linux/mm_types.h +0 -0
@@ -120,7 +120,12 @@ struct page {
 		};
 	};
 
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
include/linux/mm_types.h +4 -1
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
Quoted 8 lines
Quoted 8 lines

include/linux/mm_types.h struct page { · line 149

Quoted 8 lines
include/linux/mm_types.h +2 -0
+	 * for non-tail pages.
+	 */
 	union {
 		struct list_head lru;	/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !
@@ -143,6 +148,7 @@ struct page {
 						 */
 		/* First tail page of compound page */

Note that compound_head is not just in the *first* tail page. Only the rest is.

Right.

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#22 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Thanx, Paul

Quoted 64 lines
Quoted 3 lines

For pgtable_t I can't think of anything better than a warning in the generic definition in include/asm-generic/page.h and hope that anyone reimplementing it for a new arch will look there first.

I will move it to other word, just in case.

Quoted 7 lines

The lru part is probably the hardest to prevent danger. It can be used for any private purposes. Hopefully everyone currently uses only standard list operations here, and the list poison values don't set bit 0. But I see there can be some arbitrary CONFIG_ILLEGAL_POINTER_VALUE added to the poisons, so maybe that's worth some build error check? Anyway we would be imposing restrictions on types that are not ours, so there might be some resistance...

I will add BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);

Quoted 12 lines
Quoted 11 lines
Quoted 3 lines

Anyway, this is quite subtle and there's a risk that people will accidentally break it later on. I don't think the patch puts sufficient documentation in place to prevent this.

I would appreciate for suggestion on place and form of documentation.

Quoted 1 line

And even documentation might not be enough to prevent accidents.

The only think I can propose is VM_BUG_ON() in PageTail() and compound_head() which would ensure that page->compound_page points to place within MAX_ORDER_NR_PAGES before the current page if bit 0 is set.

Quoted 5 lines

That should probably catch some bad stuff, but probably only moments before it would crash anyway if the pointer was bogus. But I also don't see better way, because we can't proactively put checks in those who would "misbehave", as we don't know who they are. Putting more debug checks in e.g. page freeing might help, but probably not much.

So, do you think it worth it or not after all?

Quoted 12 lines
Quoted 12 lines

Do you consider this helpful?

Quoted 9 lines
Quoted 9 lines

...

include/linux/mm_types.h +0 -0
@@ -120,7 +120,12 @@ struct page {
 		};
 	};
 
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
include/linux/mm_types.h +4 -1
-	/* Third double word block */
+	/*
+	 * Third double word block
+	 *
+	 * WARNING: bit 0 of the first word encode PageTail and *must* be 0
Quoted 8 lines
Quoted 8 lines

include/linux/mm_types.h struct page { · line 149

Quoted 8 lines
include/linux/mm_types.h +2 -0
+	 * for non-tail pages.
+	 */
 	union {
 		struct list_head lru;	/* Pageout list, eg. active_list
 					 * protected by zone->lru_lock !
@@ -143,6 +148,7 @@ struct page {
 						 */
 		/* First tail page of compound page */

Note that compound_head is not just in the *first* tail page. Only the rest is.

Quoted 5 lines

Right.

Signature · 1 line
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 16 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

You mean that it sets the bit 0 of rcu_head.next during its processing? That's bad news then. It's not that we would trigger that bit when the rcu_head part of the union is "active". It's that pfn scanners could inspect such page at arbitrary time, see the bit 0 set (due to RCU processing) and think that it's a tail page of a compound page, and interpret the rest of the pointer as a pointer to the head page (to test it for flags etc).

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 20 lines · Vlastimil Babka
Quoted 19 lines · Paul E. McKenney
Quoted 18 lines · Kirill A. Shutemov
Quoted 16 lines · Vlastimil Babka
Quoted 7 lines · Kirill A. Shutemov
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Quoted 6 lines

That's
bad news then. It's not that we would trigger that bit when the rcu_head part of the union is "active". It's that pfn scanners could inspect such page at arbitrary time, see the bit 0 set (due to RCU processing) and think that it's a tail page of a compound page, and interpret the rest of the pointer as a pointer to the head page (to test it for flags etc).

On the other hand, if you avoid scanning rcu_head structures for pages that are currently waiting for a grace period, no problem. RCU does not use the rcu_head structure at all except for during the time between when call_rcu() is invoked on that rcu_head structure and the time that the callback is invoked.

Is there some other page state that indicates that the page is waiting for a grace period? If so, you could simply avoid testing that bit in that case.

Thanx, Paul

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#25 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Ugh.. :-/

Quoted 13 lines
Quoted 6 lines

That's
bad news then. It's not that we would trigger that bit when the rcu_head part of the union is "active". It's that pfn scanners could inspect such page at arbitrary time, see the bit 0 set (due to RCU processing) and think that it's a tail page of a compound page, and interpret the rest of the pointer as a pointer to the head page (to test it for flags etc).

On the other hand, if you avoid scanning rcu_head structures for pages that are currently waiting for a grace period, no problem. RCU does not use the rcu_head structure at all except for during the time between when call_rcu() is invoked on that rcu_head structure and the time that the callback is invoked.

Quoted 3 lines

Is there some other page state that indicates that the page is waiting for a grace period? If so, you could simply avoid testing that bit in that case.

No, I don't think so.

For compound pages most of info of its state is stored in head page (e.g. page_count(), flags, etc). So if we examine random page (pfn scanner case) the very first thing we want to know if we stepped on tail page. PageTail() is what I wanted to encode in the bit...

What if we change order of fields within rcu_head and put ->func first? Can we expect this pointer to have bit 0 always clear?

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#26 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 17 lines · Kirill A. Shutemov
Quoted 13 lines
Quoted 6 lines

That's
bad news then. It's not that we would trigger that bit when the rcu_head part of the union is "active". It's that pfn scanners could inspect such page at arbitrary time, see the bit 0 set (due to RCU processing) and think that it's a tail page of a compound page, and interpret the rest of the pointer as a pointer to the head page (to test it for flags etc).

On the other hand, if you avoid scanning rcu_head structures for pages that are currently waiting for a grace period, no problem. RCU does not use the rcu_head structure at all except for during the time between when call_rcu() is invoked on that rcu_head structure and the time that the callback is invoked.

Quoted 3 lines

Is there some other page state that indicates that the page is waiting for a grace period? If so, you could simply avoid testing that bit in that case.

Quoted 8 lines

No, I don't think so.

For compound pages most of info of its state is stored in head page (e.g. page_count(), flags, etc). So if we examine random page (pfn scanner case) the very first thing we want to know if we stepped on tail page. PageTail() is what I wanted to encode in the bit...

What if we change order of fields within rcu_head and put ->func first?

Or change the order of compound_head wrt the rest?

Quoted 1 line

Can we expect this pointer to have bit 0 always clear?

That's probably a question whether $compiler is guaranteed to align functions on all architectures...

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#27 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Kirill A. Shutemov
Quoted 49 lines · Kirill A. Shutemov
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Ugh.. :-/

Quoted 12 lines
Quoted 5 lines

bad news then. It's not that we would trigger that bit when the rcu_head part of the union is "active". It's that pfn scanners could inspect such page at arbitrary time, see the bit 0 set (due to RCU processing) and think that it's a tail page of a compound page, and interpret the rest of the pointer as a pointer to the head page (to test it for flags etc).

On the other hand, if you avoid scanning rcu_head structures for pages that are currently waiting for a grace period, no problem. RCU does not use the rcu_head structure at all except for during the time between when call_rcu() is invoked on that rcu_head structure and the time that the callback is invoked.

Quoted 5 lines
Quoted 3 lines

Is there some other page state that indicates that the page is waiting for a grace period? If so, you could simply avoid testing that bit in that case.

No, I don't think so.

OK, I'll bite... How do you know that it is safe to invoke call_rcu(), given that you are not allowed to invoke call_rcu() until the previous callback has been invoked?

Quoted 4 lines

For compound pages most of info of its state is stored in head page (e.g. page_count(), flags, etc). So if we examine random page (pfn scanner case) the very first thing we want to know if we stepped on tail page. PageTail() is what I wanted to encode in the bit...

Ah, so that would require the page scanner to do reverse mapping or some such, then. Which is perhaps what you are trying to avoid.

Quoted 2 lines

What if we change order of fields within rcu_head and put ->func first? Can we expect this pointer to have bit 0 always clear?

I asked that question some time back, and the answer was "no". You can apparently have functions that start at odd addresses on some architectures.

That said, there are likely to be reserved bits somewhere in the function address, perhaps varying depending on architecture and/or boot, in the case of address-space randomization. Perhaps some way of identifying those bits with architecture-independent ways of querying and setting them?

Thanx, Paul

#28 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Paul E. McKenney
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

hUgh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#29 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 35 lines · Hugh Dickins
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

Thanx, Paul

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#30 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 42 lines · Paul E. McKenney
Quoted 35 lines · Hugh Dickins
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 7 lines
Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

That's exactly what we want: sounds like we have no problem, thanks Paul.

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#31 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 48 lines · Hugh Dickins
Quoted 47 lines · Paul E. McKenney
Quoted 35 lines · Hugh Dickins
Quoted 26 lines · Paul E. McKenney
Quoted 25 lines · Vlastimil Babka
Quoted 23 lines · Paul E. McKenney
Quoted 15 lines · Kirill A. Shutemov
Quoted 14 lines · Vlastimil Babka
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 9 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

Quoted 4 lines
Quoted 2 lines

We should probably ask Paul about the chances that rcu_head.next would like to use the bit too one day?

+Paul.

The call_rcu() function does stomp that bit, but if you stop using that bit before you invoke call_rcu(), no problem.

Quoted 7 lines
Quoted 1 line

You mean that it sets the bit 0 of rcu_head.next during its processing?

Not at the moment, though RCU will splat if given a misaligned rcu_head structure because of the possibility to use that bit to flag callbacks that do nothing but free memory. If RCU needs to do that (e.g., to promote energy efficiency), then that bit might well be set during RCU grace-period processing.

Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

Quoted 4 lines
Quoted 2 lines

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

That's exactly what we want: sounds like we have no problem, thanks Paul.

Whew! ;-)

Thanx, Paul

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#32 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Paul E. McKenney
Quoted 1 line · Paul E. McKenney

On Wed, Aug 26, 2015 at 11:18:45AM -0700, Hugh Dickins wrote:

[...]

Quoted 6 lines
Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 7 lines
Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

I am really not sure I udnerstand. What will prevent call_rcu(&page->rcu_head, free_page_rcu) done in a random driver?

Cannot the RCU simply claim bit1? I can see 1146edcbef37 ("rcu: Loosen __call_rcu()'s rcu_head alignment constraint") but AFAIU all it would take to fix this would be to require struct rcu_head to be aligned to 32b no?

Btw. Do we need the same think for page::mapping and KSM?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#33 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

On Thu 27-08-15 17:09:17, Michal Hocko wrote:
[...]

Quoted 1 line

Btw. Do we need the same think for page::mapping and KSM?

I guess we are safe here because the address for mappings comes from kmalloc and that aligned properly, right?

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#34 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 6 lines · Michal Hocko

On Thu 27-08-15 17:09:17, Michal Hocko wrote:
[...]

Quoted 1 line

Btw. Do we need the same think for page::mapping and KSM?

I guess we are safe here because the address for mappings comes from kmalloc and that aligned properly, right?

Not quite right, in fact. Because usually the struct address_space is embedded within the struct inode (at i_data), and the struct inode embedded within the fs-dependent inode, and that's what's kmalloc'ed.

What makes the mapping pointer low bits safe is include/linux/fs.h:

struct address_space {
	...
} __attribute__((aligned(sizeof(long))));

Which we first had to add in for the cris architecture, which stumbled not on a genuine allocated address_space, but on that funny statically declared swapper_space in mm/swap_state.c.

But struct anon_vma and KSM's struct stable_node (which depend on the same scheme for low bits of page->mapping) have no such alignment attribute specified: those ones are indeed relying on the kmalloc guarantee as you suppose.

Does struct rcu_head have no __attribute__((aligned(whatever)))? Perhaps that attribute should be added when it's needed.

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#35 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 8 lines · Hugh Dickins
Quoted 6 lines · Michal Hocko

On Thu 27-08-15 17:09:17, Michal Hocko wrote:
[...]

Quoted 1 line

Btw. Do we need the same think for page::mapping and KSM?

I guess we are safe here because the address for mappings comes from kmalloc and that aligned properly, right?

Quoted 8 lines

Not quite right, in fact. Because usually the struct address_space is embedded within the struct inode (at i_data), and the struct inode embedded within the fs-dependent inode, and that's what's kmalloc'ed.

What makes the mapping pointer low bits safe is include/linux/fs.h:

struct address_space {
	...
} __attribute__((aligned(sizeof(long))));

Oh, right you are.

Quoted 3 lines

Which we first had to add in for the cris architecture, which stumbled not on a genuine allocated address_space, but on that funny statically declared swapper_space in mm/swap_state.c.

Thanks for the clarification.

Quoted 7 lines

But struct anon_vma and KSM's struct stable_node (which depend on the same scheme for low bits of page->mapping) have no such alignment attribute specified: those ones are indeed relying on the kmalloc guarantee as you suppose.

Does struct rcu_head have no __attribute__((aligned(whatever)))? Perhaps that attribute should be added when it's needed.

That's basically what I meant in the previous email.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#36 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Michal Hocko
Quoted 14 lines · Michal Hocko
Quoted 1 line · Paul E. McKenney

On Wed, Aug 26, 2015 at 11:18:45AM -0700, Hugh Dickins wrote:

[...]

Quoted 11 lines
Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

Quoted 5 lines
Quoted 2 lines

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

I am really not sure I udnerstand. What will prevent call_rcu(&page->rcu_head, free_page_rcu) done in a random driver?

As long as it uses call_rcu(), call_rcu_bh(), call_rcu_sched(), or call_srcu() and not some future call_rcu_lazy(), no problem.

But yes, if you are going to assume that RCU leaves the bottom bit of the rcu_head structure's ->next field zero, then everything everywhere in the kernel might in the future need to be careful of exactly what variant of call_rcu() is used.

Quoted 4 lines

Cannot the RCU simply claim bit1? I can see 1146edcbef37 ("rcu: Loosen __call_rcu()'s rcu_head alignment constraint") but AFAIU all it would take to fix this would be to require struct rcu_head to be aligned to 32b no?

There are some architectures that guarantee only 16-bit alignment. If those architectures are fixed to do 32-bit alignment, or if support for them is dropped, then the future restrictions mentioned above could be dropped.

Thanx, Paul

Quoted 5 lines

Btw. Do we need the same think for page::mapping and KSM?

Signature · 2 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#37 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 21 lines · Paul E. McKenney
Quoted 14 lines · Michal Hocko
Quoted 1 line · Paul E. McKenney

On Wed, Aug 26, 2015 at 11:18:45AM -0700, Hugh Dickins wrote:

[...]

Quoted 11 lines
Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

Quoted 5 lines
Quoted 2 lines

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

I am really not sure I udnerstand. What will prevent call_rcu(&page->rcu_head, free_page_rcu) done in a random driver?

Quoted 7 lines

As long as it uses call_rcu(), call_rcu_bh(), call_rcu_sched(), or call_srcu() and not some future call_rcu_lazy(), no problem.

But yes, if you are going to assume that RCU leaves the bottom bit of the rcu_head structure's ->next field zero, then everything everywhere in the kernel might in the future need to be careful of exactly what variant of call_rcu() is used.

OK, so it would be call_rcu_$special to use the bit. This wasn't entirely clear to me. I thought it would be opposite.

Quoted 9 lines
Quoted 4 lines

Cannot the RCU simply claim bit1? I can see 1146edcbef37 ("rcu: Loosen __call_rcu()'s rcu_head alignment constraint") but AFAIU all it would take to fix this would be to require struct rcu_head to be aligned to 32b no?

There are some architectures that guarantee only 16-bit alignment. If those architectures are fixed to do 32-bit alignment, or if support for them is dropped, then the future restrictions mentioned above could be dropped.

My understanding of the discussion which led to the above patch is that m68k allows for 32b alignment you just have to be explicit about that
(http://thread.gmane.org/gmane.linux.ports.m68k/5932/focus=5960). Which other archs would be affected?

I mean, this patch allows for quite some simplification in the mm code. And I think that RCU can live with mm of the low bits without any issues. You've said that one bit should be sufficient for the RCU use case. So having 2 bits sounds like a good thing.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#38 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 25 lines · Michal Hocko
Quoted 24 lines · Paul E. McKenney
Quoted 14 lines · Michal Hocko
Quoted 1 line · Paul E. McKenney

On Wed, Aug 26, 2015 at 11:18:45AM -0700, Hugh Dickins wrote:

[...]

Quoted 11 lines
Quoted 3 lines

But if you do one day implement that, wouldn't sl?b.c have to use call_rcu_with_added_meaning() instead of call_rcu(), to be in danger of getting that bit set? (No rcu_head is placed in a PageTail page.)

Good point, call_rcu_lazy(), but yes.

Quoted 4 lines

So although it might be a little strange not to use a variant intended for freeing memory when indeed that's what it's doing, it would not be the end of the world for SLAB_DESTROY_BY_RCU to carry on using straight call_rcu(), in defence of the struct page safety Kirill is proposing.

Quoted 5 lines
Quoted 2 lines

As long as you are OK with the bottom bit being zero throughout the RCU processing, yes.

I am really not sure I udnerstand. What will prevent call_rcu(&page->rcu_head, free_page_rcu) done in a random driver?

As long as it uses call_rcu(), call_rcu_bh(), call_rcu_sched(), or call_srcu() and not some future call_rcu_lazy(), no problem.

Quoted 7 lines
Quoted 4 lines

But yes, if you are going to assume that RCU leaves the bottom bit of the rcu_head structure's ->next field zero, then everything everywhere in the kernel might in the future need to be careful of exactly what variant of call_rcu() is used.

OK, so it would be call_rcu_$special to use the bit. This wasn't entirely clear to me. I thought it would be opposite.

Yes. And I cannot resist adding that the need to avoid call_rcu_$special() would be with respect to a given rcu_head structure, not global. Though I believe that you already figured that out. ;-)

Quoted 15 lines
Quoted 9 lines
Quoted 4 lines

Cannot the RCU simply claim bit1? I can see 1146edcbef37 ("rcu: Loosen __call_rcu()'s rcu_head alignment constraint") but AFAIU all it would take to fix this would be to require struct rcu_head to be aligned to 32b no?

There are some architectures that guarantee only 16-bit alignment. If those architectures are fixed to do 32-bit alignment, or if support for them is dropped, then the future restrictions mentioned above could be dropped.

My understanding of the discussion which led to the above patch is that m68k allows for 32b alignment you just have to be explicit about that
(http://thread.gmane.org/gmane.linux.ports.m68k/5932/focus=5960). Which other archs would be affected?

Quoted 4 lines

I mean, this patch allows for quite some simplification in the mm code. And I think that RCU can live with mm of the low bits without any issues. You've said that one bit should be sufficient for the RCU use case. So having 2 bits sounds like a good thing.

As long as MM doesn't use call_rcu_$special() for the rcu_head structure in question, as long as MM is OK with the bottom bit of ->next always being zero during a grace period, and as long as MM avoids writing to ->next during a grace period, we should be good as is, even if a call_rcu_$special() becomes necessary.

Thanx, Paul

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#39 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Kirill A. Shutemov
Quoted 3 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

[...]

Quoted 4 lines
include/linux/mm.h +0 -0
index 0735bc0a351a..a4c4b7d07473 100644

[...]

Quoted 12 lines
include/linux/mm.h +0 -12
-/*
- * If we access compound page synchronously such as access to
- * allocated page, there is no need to handle tail flag race, so we can
- * check tail flag directly without any synchronization primitive.
- */
-static inline struct page *compound_head_fast(struct page *page)
-{
-	if (unlikely(PageTail(page)))
-		return page->first_page;
-	return page;
-}
-

[...]

include/linux/mm.h static inline struct page *virt_to_head_page(const void *x) · line 512

Quoted 13 lines
include/linux/mm.h +1 -7
@@ -548,13 +508,7 @@ static inline struct page *virt_to_head_page(const void *x)
 {
 	struct page *page = virt_to_page(x);
 
-	/*
-	 * We don't need to worry about synchronization of tail flag
-	 * when we call virt_to_head_page() since it is only called for
-	 * already allocated page and this page won't be freed until
-	 * this virt_to_head_page() is finished. So use _fast variant.
-	 */
-	return compound_head_fast(page);
+	return compound_head(page);
 }

I hope this does not slow down the SLAB/slub allocator? (which calls virt_to_head_page() frequently)

Signature · 11 lines
Best regards,
  Jesper Dangaard Brouer
  MSc.CS, Sr. Network Kernel Developer at Red Hat
  Author of http://www.iptv-analyzer.org
  LinkedIn: http://www.linkedin.com/in/brouer

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#40 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

Quoted 32 lines · Jesper Dangaard Brouer
Quoted 3 lines · On Wed, 19 Aug 2015 12:21:45 +0300 "Kirill A. Shutemov"…

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

[...]

Quoted 4 lines
include/linux/mm.h +0 -0
index 0735bc0a351a..a4c4b7d07473 100644

[...]

Quoted 12 lines
include/linux/mm.h +0 -12
-/*
- * If we access compound page synchronously such as access to
- * allocated page, there is no need to handle tail flag race, so we can
- * check tail flag directly without any synchronization primitive.
- */
-static inline struct page *compound_head_fast(struct page *page)
-{
-	if (unlikely(PageTail(page)))
-		return page->first_page;
-	return page;
-}
-

[...]

Quoted 4 lines
include/linux/mm.h +0 -0
@@ -548,13 +508,7 @@ static inline struct page *virt_to_head_page(const void *x)
 {
 	struct page *page = virt_to_page(x);
 
Quoted 12 lines

include/linux/mm.h static inline struct page *virt_to_head_page(const void *x) · line 512

Quoted 9 lines
include/linux/mm.h +1 -7
-	/*
-	 * We don't need to worry about synchronization of tail flag
-	 * when we call virt_to_head_page() since it is only called for
-	 * already allocated page and this page won't be freed until
-	 * this virt_to_head_page() is finished. So use _fast variant.
-	 */
-	return compound_head_fast(page);
+	return compound_head(page);
 }

I hope this does not slow down the SLAB/slub allocator? (which calls virt_to_head_page() frequently)

It should be slightly faster.

Before:

00002e90 <test_virt_to_head_page>:

    2e90:	8b 15 00 00 00 00    	mov    0x0,%edx
    2e96:	05 00 00 00 40       	add    $0x40000000,%eax
    2e9b:	c1 e8 0c             	shr    $0xc,%eax
    2e9e:	c1 e0 05             	shl    $0x5,%eax
    2ea1:	01 d0                	add    %edx,%eax
    2ea3:	8b 10                	mov    (%eax),%edx
    2ea5:	f6 c6 80             	test   $0x80,%dh
    2ea8:	75 06                	jne    2eb0 <test_virt_to_head_page+0x20>
    2eaa:	c3                   	ret    
    2eab:	90                   	nop
    2eac:	8d 74 26 00          	lea    0x0(%esi,%eiz,1),%esi
    2eb0:	8b 40 1c             	mov    0x1c(%eax),%eax
    2eb3:	c3                   	ret    

After:

00003070 <test_virt_to_head_page>:

    3070:	8b 15 00 00 00 00    	mov    0x0,%edx
    3076:	05 00 00 00 40       	add    $0x40000000,%eax
    307b:	c1 e8 0c             	shr    $0xc,%eax
    307e:	c1 e0 05             	shl    $0x5,%eax
    3081:	01 d0                	add    %edx,%eax
    3083:	8b 50 14             	mov    0x14(%eax),%edx
    3086:	8d 4a ff             	lea    -0x1(%edx),%ecx
    3089:	f6 c2 01             	test   $0x1,%dl
    308c:	0f 45 c1             	cmovne %ecx,%eax
    308f:	c3                   	ret    
Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#41 lore raw

Re: [PATCHv3 4/5] mm: make compound_head() robust

In reply to Kirill A. Shutemov
Quoted 38 lines · Kirill A. Shutemov

Hugh has pointed that compound_head() call can be unsafe in some context. There's one example:

	CPU0					CPU1

isolate_migratepages_block()
page_count()

    compound_head()
      !!PageTail() == true
					put_page()
					  tail->first_page = NULL
      head = tail->first_page
					alloc_pages(__GFP_COMP)
					   prep_compound_page()
					     tail->first_page = head
					     __SetPageTail(p);
      !!PageTail() == true
    <head == NULL dereferencing>

The race is pure theoretical. I don't it's possible to trigger it in practice. But who knows.

We can fix the race by changing how encode PageTail() and compound_head() within struct page to be able to update them in one shot.

The patch introduces page->compound_head into third double word block in front of compound_dtor and compound_order. That means it shares storage space with:

- page->lru.next;
- page->next;
- page->rcu_head.next;
- page->pmd_huge_pte;

That's too long list to be absolutely sure, but looks like nobody uses bit 0 of the word. It can be used to encode PageTail(). And if the bit set, rest of the word is pointer to head page.

Quoted 5 lines

If DEFERRED_STRUCT_PAGE_INIT=n, combining this patchset with my page-flags patches causes oops in SetPageReserved() called from reserve_bootmem_region().

It happens because we haven't yet initilized the word in struct page and PageTail() inside SetPageReserved() can give false-positive, which leads to bogus compound_head() result.

IIUC, we initialize the word only on first allocation of the page. It can be too late: pfn scanner can see false-positive PageTail() from not yet allocated pages too.

Here's fixlet for patch to address the issue.

mm/page_alloc.c +2 -0
index 347724850665..d0e3fca830f8 100644
@@ -892,6 +892,8 @@ static void init_reserved_page(unsigned long pfn)
 #else
 static inline void init_reserved_page(unsigned long pfn)
 {
+	/* Avoid false-positive PageTail() */
+	INIT_LIST_HEAD(&pfn_to_page(pfn)->lru);
 }
 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
 
Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#42 lore raw

PATCH v3 5/5 mm: use 'unsigned int' for page order

In reply to Kirill A. Shutemov

Let's try to be consistent about data type of page order.

---
 include/linux/mm.h |  5 +++--
 mm/hugetlb.c       | 19 ++++++++++---------
 mm/internal.h      |  4 ++--
 mm/page_alloc.c    | 27 +++++++++++++++------------
 4 files changed, 30 insertions(+), 25 deletions(-)
include/linux/mm.h +3 -2
index a4c4b7d07473..a75bbb3f7142 100644
@@ -557,7 +557,7 @@ static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
 	return compound_page_dtors[page[1].compound_dtor];
 }
 
-static inline int compound_order(struct page *page)
+static inline unsigned int compound_order(struct page *page)
 {
 	if (!PageHead(page))
 		return 0;
@@ -1718,7 +1718,8 @@ extern void si_meminfo(struct sysinfo * val);
 extern void si_meminfo_node(struct sysinfo *val, int nid);
 
 extern __printf(3, 4)
-void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...);
+void warn_alloc_failed(gfp_t gfp_mask, unsigned int order,
+		const char *fmt, ...);
 
 extern void setup_per_cpu_pageset(void);
 
mm/hugetlb.c +10 -9
index 53c0709fd87b..bf64bfebc473 100644
@@ -817,7 +817,7 @@ static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
 
 #if defined(CONFIG_CMA) && defined(CONFIG_X86_64)
 static void destroy_compound_gigantic_page(struct page *page,
-					unsigned long order)
+					unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -832,7 +832,7 @@ static void destroy_compound_gigantic_page(struct page *page,
 	__ClearPageHead(page);
 }
 
-static void free_gigantic_page(struct page *page, unsigned order)
+static void free_gigantic_page(struct page *page, unsigned int order)
 {
 	free_contig_range(page_to_pfn(page), 1 << order);
 }
@@ -876,7 +876,7 @@ static bool zone_spans_last_pfn(const struct zone *zone,
 	return zone_spans_pfn(zone, last_pfn);
 }
 
-static struct page *alloc_gigantic_page(int nid, unsigned order)
+static struct page *alloc_gigantic_page(int nid, unsigned int order)
 {
 	unsigned long nr_pages = 1 << order;
 	unsigned long ret, pfn, flags;
@@ -912,7 +912,7 @@ static struct page *alloc_gigantic_page(int nid, unsigned order)
 }
 
 static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
-static void prep_compound_gigantic_page(struct page *page, unsigned long order);
+static void prep_compound_gigantic_page(struct page *page, unsigned int order);
 
 static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid)
 {
@@ -945,9 +945,9 @@ static int alloc_fresh_gigantic_page(struct hstate *h,
 static inline bool gigantic_page_supported(void) { return true; }
 #else
 static inline bool gigantic_page_supported(void) { return false; }
-static inline void free_gigantic_page(struct page *page, unsigned order) { }
+static inline void free_gigantic_page(struct page *page, unsigned int order) { }
 static inline void destroy_compound_gigantic_page(struct page *page,
-						unsigned long order) { }
+						unsigned int order) { }
 static inline int alloc_fresh_gigantic_page(struct hstate *h,
 					nodemask_t *nodes_allowed) { return 0; }
 #endif
@@ -1073,7 +1073,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
 	put_page(page); /* free it into the hugepage allocator */
 }
 
-static void prep_compound_gigantic_page(struct page *page, unsigned long order)
+static void prep_compound_gigantic_page(struct page *page, unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -1640,7 +1640,8 @@ found:
 	return 1;
 }
 
-static void __init prep_compound_huge_page(struct page *page, int order)
+static void __init prep_compound_huge_page(struct page *page,
+		unsigned int order)
 {
 	if (unlikely(order > (MAX_ORDER - 1)))
 		prep_compound_gigantic_page(page, order);
@@ -2351,7 +2352,7 @@ static int __init hugetlb_init(void)
 module_init(hugetlb_init);
 
 /* Should be called on processing a hugepagesz=... option */
-void __init hugetlb_add_hstate(unsigned order)
+void __init hugetlb_add_hstate(unsigned int order)
 {
 	struct hstate *h;
 	unsigned long i;
mm/internal.h +2 -2
index 89e21a07080a..9a9fc497593f 100644
@@ -157,7 +157,7 @@ __find_buddy_index(unsigned long page_idx, unsigned int order)
 extern int __isolate_free_page(struct page *page, unsigned int order);
 extern void __free_pages_bootmem(struct page *page, unsigned long pfn,
 					unsigned int order);
-extern void prep_compound_page(struct page *page, unsigned long order);
+extern void prep_compound_page(struct page *page, unsigned int order);
 #ifdef CONFIG_MEMORY_FAILURE
 extern bool is_free_buddy_page(struct page *page);
 #endif
@@ -214,7 +214,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
  * page cannot be allocated or merged in parallel. Alternatively, it must
  * handle invalid values gracefully, and use page_order_unsafe() below.
  */
-static inline unsigned long page_order(struct page *page)
+static inline unsigned int page_order(struct page *page)
 {
 	/* PageBuddy() must be checked by the caller */
 	return page_private(page);
mm/page_alloc.c +15 -12
index 78859d47aaf4..347724850665 100644
@@ -163,7 +163,7 @@ bool pm_suspended_storage(void)
 #endif /* CONFIG_PM_SLEEP */
 
 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
-int pageblock_order __read_mostly;
+unsigned int pageblock_order __read_mostly;
 #endif
 
 static void __free_pages_ok(struct page *page, unsigned int order);
@@ -441,7 +441,7 @@ static void free_compound_page(struct page *page)
 	__free_pages_ok(page, compound_order(page));
 }
 
-void prep_compound_page(struct page *page, unsigned long order)
+void prep_compound_page(struct page *page, unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -641,7 +641,7 @@ static inline void __free_one_page(struct page *page,
 	unsigned long combined_idx;
 	unsigned long uninitialized_var(buddy_idx);
 	struct page *buddy;
-	int max_order = MAX_ORDER;
+	unsigned int max_order = MAX_ORDER;
 
 	VM_BUG_ON(!zone_is_initialized(zone));
 	VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
@@ -1436,7 +1436,7 @@ int move_freepages(struct zone *zone,
 			  int migratetype)
 {
 	struct page *page;
-	unsigned long order;
+	unsigned int order;
 	int pages_moved = 0;
 
 #ifndef CONFIG_HOLES_IN_ZONE
@@ -1550,7 +1550,7 @@ static bool can_steal_fallback(unsigned int order, int start_mt)
 static void steal_suitable_fallback(struct zone *zone, struct page *page,
 							  int start_type)
 {
-	int current_order = page_order(page);
+	unsigned int current_order = page_order(page);
 	int pages;
 
 	/* Take ownership for orders >= pageblock_order */
@@ -2657,7 +2657,7 @@ static DEFINE_RATELIMIT_STATE(nopage_rs,
 		DEFAULT_RATELIMIT_INTERVAL,
 		DEFAULT_RATELIMIT_BURST);
 
-void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
+void warn_alloc_failed(gfp_t gfp_mask, unsigned int order, const char *fmt, ...)
 {
 	unsigned int filter = SHOW_MEM_FILTER_NODES;
 
@@ -2691,7 +2691,7 @@ void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
 		va_end(args);
 	}
 
-	pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
+	pr_warn("%s: page allocation failure: order:%u, mode:0x%x\n",
 		current->comm, order, gfp_mask);
 
 	dump_stack();
@@ -3450,7 +3450,8 @@ void free_kmem_pages(unsigned long addr, unsigned int order)
 	}
 }
 
-static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
+static void *make_alloc_exact(unsigned long addr, unsigned int order,
+		size_t size)
 {
 	if (addr) {
 		unsigned long alloc_end = addr + (PAGE_SIZE << order);
@@ -3502,7 +3503,7 @@ EXPORT_SYMBOL(alloc_pages_exact);
  */
 void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
 {
-	unsigned order = get_order(size);
+	unsigned int order = get_order(size);
 	struct page *p = alloc_pages_node(nid, gfp_mask, order);
 	if (!p)
 		return NULL;
@@ -3804,7 +3805,8 @@ void show_free_areas(unsigned int filter)
 	}
 
 	for_each_populated_zone(zone) {
-		unsigned long nr[MAX_ORDER], flags, order, total = 0;
+		unsigned int order;
+		unsigned long nr[MAX_ORDER], flags, total = 0;
 		unsigned char types[MAX_ORDER];
 
 		if (skip_free_areas_node(filter, zone_to_nid(zone)))
@@ -4153,7 +4155,7 @@ static void build_zonelists(pg_data_t *pgdat)
 	nodemask_t used_mask;
 	int local_node, prev_node;
 	struct zonelist *zonelist;
-	int order = current_zonelist_order;
+	unsigned int order = current_zonelist_order;
 
 	/* initialize zonelists */
 	for (i = 0; i < MAX_ZONELISTS; i++) {
@@ -6818,7 +6820,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,
 		       unsigned migratetype)
 {
 	unsigned long outer_start, outer_end;
-	int ret = 0, order;
+	unsigned int order;
+	int ret = 0;
 
 	struct compact_control cc = {
 		.nr_migratepages = 0,
Signature · 7 lines
2.5.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v3 5/5
#43 lore raw

Re: [PATCHv3 5/5] mm: use 'unsigned int' for page order

Quoted 1 line · Kirill A. Shutemov

Let's try to be consistent about data type of page order.

Looks good to me.

We still have *_control::order but that is not directly related to this patch series.

Quoted 1 line

Thanks!

Quoted 249 lines
---
 include/linux/mm.h |  5 +++--
 mm/hugetlb.c       | 19 ++++++++++---------
 mm/internal.h      |  4 ++--
 mm/page_alloc.c    | 27 +++++++++++++++------------
 4 files changed, 30 insertions(+), 25 deletions(-)
include/linux/mm.h +3 -2
index a4c4b7d07473..a75bbb3f7142 100644
@@ -557,7 +557,7 @@ static inline compound_page_dtor *get_compound_page_dtor(struct page *page)
 	return compound_page_dtors[page[1].compound_dtor];
 }
 
-static inline int compound_order(struct page *page)
+static inline unsigned int compound_order(struct page *page)
 {
 	if (!PageHead(page))
 		return 0;
@@ -1718,7 +1718,8 @@ extern void si_meminfo(struct sysinfo * val);
 extern void si_meminfo_node(struct sysinfo *val, int nid);
 
 extern __printf(3, 4)
-void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...);
+void warn_alloc_failed(gfp_t gfp_mask, unsigned int order,
+		const char *fmt, ...);
 
 extern void setup_per_cpu_pageset(void);
 
mm/hugetlb.c +10 -9
index 53c0709fd87b..bf64bfebc473 100644
@@ -817,7 +817,7 @@ static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
 
 #if defined(CONFIG_CMA) && defined(CONFIG_X86_64)
 static void destroy_compound_gigantic_page(struct page *page,
-					unsigned long order)
+					unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -832,7 +832,7 @@ static void destroy_compound_gigantic_page(struct page *page,
 	__ClearPageHead(page);
 }
 
-static void free_gigantic_page(struct page *page, unsigned order)
+static void free_gigantic_page(struct page *page, unsigned int order)
 {
 	free_contig_range(page_to_pfn(page), 1 << order);
 }
@@ -876,7 +876,7 @@ static bool zone_spans_last_pfn(const struct zone *zone,
 	return zone_spans_pfn(zone, last_pfn);
 }
 
-static struct page *alloc_gigantic_page(int nid, unsigned order)
+static struct page *alloc_gigantic_page(int nid, unsigned int order)
 {
 	unsigned long nr_pages = 1 << order;
 	unsigned long ret, pfn, flags;
@@ -912,7 +912,7 @@ static struct page *alloc_gigantic_page(int nid, unsigned order)
 }
 
 static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
-static void prep_compound_gigantic_page(struct page *page, unsigned long order);
+static void prep_compound_gigantic_page(struct page *page, unsigned int order);
 
 static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid)
 {
@@ -945,9 +945,9 @@ static int alloc_fresh_gigantic_page(struct hstate *h,
 static inline bool gigantic_page_supported(void) { return true; }
 #else
 static inline bool gigantic_page_supported(void) { return false; }
-static inline void free_gigantic_page(struct page *page, unsigned order) { }
+static inline void free_gigantic_page(struct page *page, unsigned int order) { }
 static inline void destroy_compound_gigantic_page(struct page *page,
-						unsigned long order) { }
+						unsigned int order) { }
 static inline int alloc_fresh_gigantic_page(struct hstate *h,
 					nodemask_t *nodes_allowed) { return 0; }
 #endif
@@ -1073,7 +1073,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
 	put_page(page); /* free it into the hugepage allocator */
 }
 
-static void prep_compound_gigantic_page(struct page *page, unsigned long order)
+static void prep_compound_gigantic_page(struct page *page, unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -1640,7 +1640,8 @@ found:
 	return 1;
 }
 
-static void __init prep_compound_huge_page(struct page *page, int order)
+static void __init prep_compound_huge_page(struct page *page,
+		unsigned int order)
 {
 	if (unlikely(order > (MAX_ORDER - 1)))
 		prep_compound_gigantic_page(page, order);
@@ -2351,7 +2352,7 @@ static int __init hugetlb_init(void)
 module_init(hugetlb_init);
 
 /* Should be called on processing a hugepagesz=... option */
-void __init hugetlb_add_hstate(unsigned order)
+void __init hugetlb_add_hstate(unsigned int order)
 {
 	struct hstate *h;
 	unsigned long i;
mm/internal.h +2 -2
index 89e21a07080a..9a9fc497593f 100644
@@ -157,7 +157,7 @@ __find_buddy_index(unsigned long page_idx, unsigned int order)
 extern int __isolate_free_page(struct page *page, unsigned int order);
 extern void __free_pages_bootmem(struct page *page, unsigned long pfn,
 					unsigned int order);
-extern void prep_compound_page(struct page *page, unsigned long order);
+extern void prep_compound_page(struct page *page, unsigned int order);
 #ifdef CONFIG_MEMORY_FAILURE
 extern bool is_free_buddy_page(struct page *page);
 #endif
@@ -214,7 +214,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
  * page cannot be allocated or merged in parallel. Alternatively, it must
  * handle invalid values gracefully, and use page_order_unsafe() below.
  */
-static inline unsigned long page_order(struct page *page)
+static inline unsigned int page_order(struct page *page)
 {
 	/* PageBuddy() must be checked by the caller */
 	return page_private(page);
mm/page_alloc.c +15 -12
index 78859d47aaf4..347724850665 100644
@@ -163,7 +163,7 @@ bool pm_suspended_storage(void)
 #endif /* CONFIG_PM_SLEEP */
 
 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
-int pageblock_order __read_mostly;
+unsigned int pageblock_order __read_mostly;
 #endif
 
 static void __free_pages_ok(struct page *page, unsigned int order);
@@ -441,7 +441,7 @@ static void free_compound_page(struct page *page)
 	__free_pages_ok(page, compound_order(page));
 }
 
-void prep_compound_page(struct page *page, unsigned long order)
+void prep_compound_page(struct page *page, unsigned int order)
 {
 	int i;
 	int nr_pages = 1 << order;
@@ -641,7 +641,7 @@ static inline void __free_one_page(struct page *page,
 	unsigned long combined_idx;
 	unsigned long uninitialized_var(buddy_idx);
 	struct page *buddy;
-	int max_order = MAX_ORDER;
+	unsigned int max_order = MAX_ORDER;
 
 	VM_BUG_ON(!zone_is_initialized(zone));
 	VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
@@ -1436,7 +1436,7 @@ int move_freepages(struct zone *zone,
 			  int migratetype)
 {
 	struct page *page;
-	unsigned long order;
+	unsigned int order;
 	int pages_moved = 0;
 
 #ifndef CONFIG_HOLES_IN_ZONE
@@ -1550,7 +1550,7 @@ static bool can_steal_fallback(unsigned int order, int start_mt)
 static void steal_suitable_fallback(struct zone *zone, struct page *page,
 							  int start_type)
 {
-	int current_order = page_order(page);
+	unsigned int current_order = page_order(page);
 	int pages;
 
 	/* Take ownership for orders >= pageblock_order */
@@ -2657,7 +2657,7 @@ static DEFINE_RATELIMIT_STATE(nopage_rs,
 		DEFAULT_RATELIMIT_INTERVAL,
 		DEFAULT_RATELIMIT_BURST);
 
-void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
+void warn_alloc_failed(gfp_t gfp_mask, unsigned int order, const char *fmt, ...)
 {
 	unsigned int filter = SHOW_MEM_FILTER_NODES;
 
@@ -2691,7 +2691,7 @@ void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
 		va_end(args);
 	}
 
-	pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
+	pr_warn("%s: page allocation failure: order:%u, mode:0x%x\n",
 		current->comm, order, gfp_mask);
 
 	dump_stack();
@@ -3450,7 +3450,8 @@ void free_kmem_pages(unsigned long addr, unsigned int order)
 	}
 }
 
-static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
+static void *make_alloc_exact(unsigned long addr, unsigned int order,
+		size_t size)
 {
 	if (addr) {
 		unsigned long alloc_end = addr + (PAGE_SIZE << order);
@@ -3502,7 +3503,7 @@ EXPORT_SYMBOL(alloc_pages_exact);
  */
 void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
 {
-	unsigned order = get_order(size);
+	unsigned int order = get_order(size);
 	struct page *p = alloc_pages_node(nid, gfp_mask, order);
 	if (!p)
 		return NULL;
@@ -3804,7 +3805,8 @@ void show_free_areas(unsigned int filter)
 	}
 
 	for_each_populated_zone(zone) {
-		unsigned long nr[MAX_ORDER], flags, order, total = 0;
+		unsigned int order;
+		unsigned long nr[MAX_ORDER], flags, total = 0;
 		unsigned char types[MAX_ORDER];
 
 		if (skip_free_areas_node(filter, zone_to_nid(zone)))
@@ -4153,7 +4155,7 @@ static void build_zonelists(pg_data_t *pgdat)
 	nodemask_t used_mask;
 	int local_node, prev_node;
 	struct zonelist *zonelist;
-	int order = current_zonelist_order;
+	unsigned int order = current_zonelist_order;
 
 	/* initialize zonelists */
 	for (i = 0; i < MAX_ZONELISTS; i++) {
@@ -6818,7 +6820,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,
 		       unsigned migratetype)
 {
 	unsigned long outer_start, outer_end;
-	int ret = 0, order;
+	unsigned int order;
+	int ret = 0;
 
 	struct compact_control cc = {
 		.nr_migratepages = 0,
Signature · 1 line
2.5.0
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#44 lore raw
In reply to Kirill A. Shutemov
Quoted 15 lines · Kirill A. Shutemov

Here's my attempt on fixing recently discovered race in compound_head(). It should make compound_head() reliable in all contexts.

The patchset is against Linus' tree. Let me know if it need to be rebased onto different baseline.

It's expected to have conflicts with my page-flags patchset and probably should be applied before it.

v3:
- Fix build without hugetlb;
- Drop page->first_page;
- Update comment for free_compound_page();
- Use 'unsigned int' for page order;

Quoted 6 lines

v2: Per Hugh's suggestion page->compound_head is moved into third double
word. This way we can avoid memory overhead which v1 had in some cases.

This place in struct page is rather overloaded. More testing is required to make sure we don't collide with anyone.

Andrew, can we have the patchset applied, if nobody has objections?

It applies cleanly into your patchstack just before my page-flags patchset.

As expected, it causes few conflicts with patches:

page-flags-introduce-page-flags-policies-wrt-compound-pages.patch mm-sanitize-page-mapping-for-tail-pages.patch include-linux-page-flagsh-rename-macros-to-avoid-collisions.patch

Updated patches with solved conflicts are attached.

Let me know if I need to do anything else about this.

Hugh, does it address your worry wrt page-flags?

Before you've mentioned races of whether the head page still agrees with the tail. I don't think it's an issue: you can get this kind of race only in very special environments like pfn scanner where you anyway need to re-validate the page after stabilizing it.

Bloat from my page-flags is also reduced substantially. Size of your page_is_locked() example in allnoconfig case reduced from 32 to 17 bytes. With the patchset it look this way:

00003070 <page_is_locked>:

    3070:	8b 50 14             	mov    0x14(%eax),%edx
    3073:	f6 c2 01             	test   $0x1,%dl
    3076:	8d 4a ff             	lea    -0x1(%edx),%ecx
    3079:	0f 45 c1             	cmovne %ecx,%eax
    307c:	8b 00                	mov    (%eax),%eax
    307e:	24 01                	and    $0x1,%al
    3080:	c3                   	ret    
Signature · 1 line
 Kirill A. Shutemov
maintainer
#45 lore raw
Quoted 16 lines · On Thu, 20 Aug 2015 15:31:07 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 15 lines · Kirill A. Shutemov

Here's my attempt on fixing recently discovered race in compound_head(). It should make compound_head() reliable in all contexts.

The patchset is against Linus' tree. Let me know if it need to be rebased onto different baseline.

It's expected to have conflicts with my page-flags patchset and probably should be applied before it.

v3:
- Fix build without hugetlb;
- Drop page->first_page;
- Update comment for free_compound_page();
- Use 'unsigned int' for page order;

Quoted 8 lines
Quoted 6 lines

v2: Per Hugh's suggestion page->compound_head is moved into third double
word. This way we can avoid memory overhead which v1 had in some cases.

This place in struct page is rather overloaded. More testing is required to make sure we don't collide with anyone.

Andrew, can we have the patchset applied, if nobody has objections?

I've been hoping to hear from Hugh and I wasn't planning on processing these before the 4.2 release.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#46 lore raw
Quoted 22 lines · Andrew Morton
Quoted 20 lines · On Thu, 20 Aug 2015 15:31:07 +0300 "Kirill A. Shutemov" <[email protected]>…
Quoted 19 lines · Kirill A. Shutemov

Here's my attempt on fixing recently discovered race in compound_head(). It should make compound_head() reliable in all contexts.

The patchset is against Linus' tree. Let me know if it need to be rebased onto different baseline.

It's expected to have conflicts with my page-flags patchset and probably should be applied before it.

v3:
- Fix build without hugetlb;
- Drop page->first_page;
- Update comment for free_compound_page();
- Use 'unsigned int' for page order;

v2: Per Hugh's suggestion page->compound_head is moved into third double
word. This way we can avoid memory overhead which v1 had in some cases.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

This place in struct page is rather overloaded. More testing is required to make sure we don't collide with anyone.

Andrew, can we have the patchset applied, if nobody has objections?

I've been hoping to hear from Hugh and I wasn't planning on processing these before the 4.2 release.

I think this patchset is very good, in a variety of different ways.

Fixes a tricky race, deletes more code than it adds, shrinks kernel text, deletes tricky functions relying on barriers, frees up a page flag bit, removes a discrepancy between configs, is really neat in how PageTail is necessarily false on all lru and lru-candidate pages, probably more. Good job.

Yes, I did think the compound destructor enum stuff over-engineered, and would have preferred just direct calls to free_compound_page() or free_huge_page() myself. But when I tried to make a patch on top to do that, even when I left PageHuge out-of-line (which had certainly not been my intention), it still generated more kernel text than Kirill's enum version (maybe his "- 1" in compound_head works better in some places than masking out 3, I didn't study); so let's forget about that.

I've not actually run and tested with it, but I shall be pleased when it gets in to mmotm, and will do so then.

As to whether it answers my doubts about his patch-flags patchset already in mmotm (not your question here, Andrew, but Kirill's in another of these mails): I'd say that it greatly reduces my doubts, but does not entirely set me at ease with the bloat.

This set here gives us a compound_head() that is safe to tuck inside PageFlags ops in that set there: that doesn't worry me any more. And the bloat is reduced enough that I don't think it should be allowed to block Kirill's progress.

But I can't shake off the idea that someone somewhere (0day perf results? Mel on an __spree?) is going to need to shave away some of these hidden and rarely needed compound_head() calls one day.

Take __activate_page() in mm/swap.c as an example, something that begins with a bold PageLRU && !PageActive && !PageUnevictable. That function contains six sequences of the form
mov 0x20(%rdi),%rax; test $0x1,%al; je over_next; sub $0x1,%rax.

Five of which I expect could be avoided if we just did a compound_head() conversion on entry. I suppose any branch predictor will do a fine job with the last five: am I just too old-fashioned to be thinking we should (have the ability to) eliminate them completely?

I'm not saying that we need to convert __activate_page, or anything else, at this time; but I do think we shall want diet versions of at least the simple PageFlags tests themselves (we should already be sparing with the atomic ones), and need to establish convention now for what the diet versions of PageFlags will be called.

Would __PageFlag be good enough? Could we say that __SetPageFlag and __ClearPageFlag omit the compound_head() - we already have to think carefully when applying those?

Hugh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#47 lore raw
Quoted 8 lines · Hugh Dickins

Yes, I did think the compound destructor enum stuff over-engineered, and would have preferred just direct calls to free_compound_page() or free_huge_page() myself. But when I tried to make a patch on top to do that, even when I left PageHuge out-of-line (which had certainly not been my intention), it still generated more kernel text than Kirill's enum version (maybe his "- 1" in compound_head works better in some places than masking out 3, I didn't study); so let's forget about that.

I had my agenda on ->compound_dtor: my refcounting patchset introduces one more compound destructor. I wanted to avoid hardcoding them here.

Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>