Skip to content

Commit def52ac

Browse files
rientjesgregkh
authored andcommitted
mm: close PageTail race
commit 668f9ab upstream. Commit bf6bddf ("mm: introduce compaction and migration for ballooned pages") introduces page_count(page) into memory compaction which dereferences page->first_page if PageTail(page). This results in a very rare NULL pointer dereference on the aforementioned page_count(page). Indeed, anything that does compound_head(), including page_count() is susceptible to racing with prep_compound_page() and seeing a NULL or dangling page->first_page pointer. This patch uses Andrea's implementation of compound_trans_head() that deals with such a race and makes it the default compound_head() implementation. This includes a read memory barrier that ensures that if PageTail(head) is true that we return a head page that is neither NULL nor dangling. The patch then adds a store memory barrier to prep_compound_page() to ensure page->first_page is set. This is the safest way to ensure we see the head page that we are expecting, PageTail(page) is already in the unlikely() path and the memory barriers are unfortunately required. Hugetlbfs is the exception, we don't enforce a store memory barrier during init since no race is possible. Signed-off-by: David Rientjes <rientjes@google.com> Cc: Holger Kiehl <Holger.Kiehl@dwd.de> Cc: Christoph Lameter <cl@linux.com> Cc: Rafael Aquini <aquini@redhat.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Michal Hocko <mhocko@suse.cz> Cc: Mel Gorman <mgorman@suse.de> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Rik van Riel <riel@redhat.com> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d113edc commit def52ac

10 files changed

Lines changed: 25 additions & 31 deletions

File tree

‎drivers/block/aoe/aoecmd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ bio_pageinc(struct bio *bio)
899899
* but this has never been seen here.
900900
*/
901901
if (unlikely(PageCompound(page)))
902-
if (compound_trans_head(page) != page) {
902+
if (compound_head(page) != page) {
903903
pr_crit("page tail used for block I/O\n");
904904
BUG();
905905
}

‎drivers/vfio/vfio_iommu_type1.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ static bool is_invalid_reserved_pfn(unsigned long pfn)
138138
if (pfn_valid(pfn)) {
139139
bool reserved;
140140
struct page *tail = pfn_to_page(pfn);
141-
struct page *head = compound_trans_head(tail);
141+
struct page *head = compound_head(tail);
142142
reserved = !!(PageReserved(head));
143143
if (head != tail) {
144144
/*
145145
* "head" is not a dangling pointer
146-
* (compound_trans_head takes care of that)
146+
* (compound_head takes care of that)
147147
* but the hugepage may have been split
148148
* from under us (and we may not hold a
149149
* reference count on the head page so it can

‎fs/proc/page.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ u64 stable_page_flags(struct page *page)
121121
* just checks PG_head/PG_tail, so we need to check PageLRU to make
122122
* sure a given page is a thp, not a non-huge compound page.
123123
*/
124-
else if (PageTransCompound(page) && PageLRU(compound_trans_head(page)))
124+
else if (PageTransCompound(page) && PageLRU(compound_head(page)))
125125
u |= 1 << KPF_THP;
126126

127127
/*

‎include/linux/huge_mm.h‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,6 @@ static inline int hpage_nr_pages(struct page *page)
159159
return HPAGE_PMD_NR;
160160
return 1;
161161
}
162-
static inline struct page *compound_trans_head(struct page *page)
163-
{
164-
if (PageTail(page)) {
165-
struct page *head;
166-
head = page->first_page;
167-
smp_rmb();
168-
/*
169-
* head may be a dangling pointer.
170-
* __split_huge_page_refcount clears PageTail before
171-
* overwriting first_page, so if PageTail is still
172-
* there it means the head pointer isn't dangling.
173-
*/
174-
if (PageTail(page))
175-
return head;
176-
}
177-
return page;
178-
}
179162

180163
extern int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
181164
unsigned long addr, pmd_t pmd, pmd_t *pmdp);
@@ -205,7 +188,6 @@ static inline int split_huge_page(struct page *page)
205188
do { } while (0)
206189
#define split_huge_page_pmd_mm(__mm, __address, __pmd) \
207190
do { } while (0)
208-
#define compound_trans_head(page) compound_head(page)
209191
static inline int hugepage_madvise(struct vm_area_struct *vma,
210192
unsigned long *vm_flags, int advice)
211193
{

‎include/linux/mm.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,18 @@ static inline void compound_unlock_irqrestore(struct page *page,
361361

362362
static inline struct page *compound_head(struct page *page)
363363
{
364-
if (unlikely(PageTail(page)))
365-
return page->first_page;
364+
if (unlikely(PageTail(page))) {
365+
struct page *head = page->first_page;
366+
367+
/*
368+
* page->first_page may be a dangling pointer to an old
369+
* compound page, so recheck that it is still a tail
370+
* page before returning.
371+
*/
372+
smp_rmb();
373+
if (likely(PageTail(page)))
374+
return head;
375+
}
366376
return page;
367377
}
368378

‎mm/ksm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ static void break_cow(struct rmap_item *rmap_item)
444444
static struct page *page_trans_compound_anon(struct page *page)
445445
{
446446
if (PageTransCompound(page)) {
447-
struct page *head = compound_trans_head(page);
447+
struct page *head = compound_head(page);
448448
/*
449449
* head may actually be splitted and freed from under
450450
* us but it's ok here.

‎mm/memory-failure.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ int soft_offline_page(struct page *page, int flags)
15441544
{
15451545
int ret;
15461546
unsigned long pfn = page_to_pfn(page);
1547-
struct page *hpage = compound_trans_head(page);
1547+
struct page *hpage = compound_head(page);
15481548

15491549
if (PageHWPoison(page)) {
15501550
pr_info("soft offline: %#lx page already poisoned\n", pfn);

‎mm/page_alloc.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,11 @@ void prep_compound_page(struct page *page, unsigned long order)
360360
__SetPageHead(page);
361361
for (i = 1; i < nr_pages; i++) {
362362
struct page *p = page + i;
363-
__SetPageTail(p);
364363
set_page_count(p, 0);
365364
p->first_page = page;
365+
/* Make sure p->first_page is always valid for PageTail() */
366+
smp_wmb();
367+
__SetPageTail(p);
366368
}
367369
}
368370

‎mm/swap.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void put_compound_page(struct page *page)
8181
{
8282
if (unlikely(PageTail(page))) {
8383
/* __split_huge_page_refcount can run under us */
84-
struct page *page_head = compound_trans_head(page);
84+
struct page *page_head = compound_head(page);
8585

8686
if (likely(page != page_head &&
8787
get_page_unless_zero(page_head))) {
@@ -219,7 +219,7 @@ bool __get_page_tail(struct page *page)
219219
*/
220220
unsigned long flags;
221221
bool got = false;
222-
struct page *page_head = compound_trans_head(page);
222+
struct page *page_head = compound_head(page);
223223

224224
if (likely(page != page_head && get_page_unless_zero(page_head))) {
225225
/* Ref to put_compound_page() comment. */

‎virt/kvm/kvm_main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ bool kvm_is_mmio_pfn(pfn_t pfn)
105105
if (pfn_valid(pfn)) {
106106
int reserved;
107107
struct page *tail = pfn_to_page(pfn);
108-
struct page *head = compound_trans_head(tail);
108+
struct page *head = compound_head(tail);
109109
reserved = PageReserved(head);
110110
if (head != tail) {
111111
/*
112112
* "head" is not a dangling pointer
113-
* (compound_trans_head takes care of that)
113+
* (compound_head takes care of that)
114114
* but the hugepage may have been splitted
115115
* from under us (and we may not hold a
116116
* reference count on the head page so it can

0 commit comments

Comments
 (0)