Skip to content

Commit 668f9ab

Browse files
rientjestorvalds
authored andcommitted
mm: close PageTail race
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> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent aa15aa0 commit 668f9ab

9 files changed

Lines changed: 25 additions & 55 deletions

File tree

‎drivers/block/aoe/aoecmd.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ bio_pageinc(struct bio *bio)
874874
/* Non-zero page count for non-head members of
875875
* compound pages is no longer allowed by the kernel.
876876
*/
877-
page = compound_trans_head(bv.bv_page);
877+
page = compound_head(bv.bv_page);
878878
atomic_inc(&page->_count);
879879
}
880880
}
@@ -887,7 +887,7 @@ bio_pagedec(struct bio *bio)
887887
struct bvec_iter iter;
888888

889889
bio_for_each_segment(bv, bio, iter) {
890-
page = compound_trans_head(bv.bv_page);
890+
page = compound_head(bv.bv_page);
891891
atomic_dec(&page->_count);
892892
}
893893
}

‎drivers/vfio/vfio_iommu_type1.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ static bool is_invalid_reserved_pfn(unsigned long pfn)
186186
if (pfn_valid(pfn)) {
187187
bool reserved;
188188
struct page *tail = pfn_to_page(pfn);
189-
struct page *head = compound_trans_head(tail);
189+
struct page *head = compound_head(tail);
190190
reserved = !!(PageReserved(head));
191191
if (head != tail) {
192192
/*
193193
* "head" is not a dangling pointer
194-
* (compound_trans_head takes care of that)
194+
* (compound_head takes care of that)
195195
* but the hugepage may have been split
196196
* from under us (and we may not hold a
197197
* reference count on the head page so it can

‎fs/proc/page.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ u64 stable_page_flags(struct page *page)
121121
* just checks PG_head/PG_tail, so we need to check PageLRU/PageAnon
122122
* to make sure a given page is a thp, not a non-huge compound page.
123123
*/
124-
else if (PageTransCompound(page) &&
125-
(PageLRU(compound_trans_head(page)) ||
126-
PageAnon(compound_trans_head(page))))
124+
else if (PageTransCompound(page) && (PageLRU(compound_head(page)) ||
125+
PageAnon(compound_head(page))))
127126
u |= 1 << KPF_THP;
128127

129128
/*

‎include/linux/huge_mm.h‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -157,46 +157,6 @@ static inline int hpage_nr_pages(struct page *page)
157157
return HPAGE_PMD_NR;
158158
return 1;
159159
}
160-
/*
161-
* compound_trans_head() should be used instead of compound_head(),
162-
* whenever the "page" passed as parameter could be the tail of a
163-
* transparent hugepage that could be undergoing a
164-
* __split_huge_page_refcount(). The page structure layout often
165-
* changes across releases and it makes extensive use of unions. So if
166-
* the page structure layout will change in a way that
167-
* page->first_page gets clobbered by __split_huge_page_refcount, the
168-
* implementation making use of smp_rmb() will be required.
169-
*
170-
* Currently we define compound_trans_head as compound_head, because
171-
* page->private is in the same union with page->first_page, and
172-
* page->private isn't clobbered. However this also means we're
173-
* currently leaving dirt into the page->private field of anonymous
174-
* pages resulting from a THP split, instead of setting page->private
175-
* to zero like for every other page that has PG_private not set. But
176-
* anonymous pages don't use page->private so this is not a problem.
177-
*/
178-
#if 0
179-
/* This will be needed if page->private will be clobbered in split_huge_page */
180-
static inline struct page *compound_trans_head(struct page *page)
181-
{
182-
if (PageTail(page)) {
183-
struct page *head;
184-
head = page->first_page;
185-
smp_rmb();
186-
/*
187-
* head may be a dangling pointer.
188-
* __split_huge_page_refcount clears PageTail before
189-
* overwriting first_page, so if PageTail is still
190-
* there it means the head pointer isn't dangling.
191-
*/
192-
if (PageTail(page))
193-
return head;
194-
}
195-
return page;
196-
}
197-
#else
198-
#define compound_trans_head(page) compound_head(page)
199-
#endif
200160

201161
extern int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
202162
unsigned long addr, pmd_t pmd, pmd_t *pmdp);
@@ -226,7 +186,6 @@ static inline int split_huge_page(struct page *page)
226186
do { } while (0)
227187
#define split_huge_page_pmd_mm(__mm, __address, __pmd) \
228188
do { } while (0)
229-
#define compound_trans_head(page) compound_head(page)
230189
static inline int hugepage_madvise(struct vm_area_struct *vma,
231190
unsigned long *vm_flags, int advice)
232191
{

‎include/linux/mm.h‎

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

400400
static inline struct page *compound_head(struct page *page)
401401
{
402-
if (unlikely(PageTail(page)))
403-
return page->first_page;
402+
if (unlikely(PageTail(page))) {
403+
struct page *head = page->first_page;
404+
405+
/*
406+
* page->first_page may be a dangling pointer to an old
407+
* compound page, so recheck that it is still a tail
408+
* page before returning.
409+
*/
410+
smp_rmb();
411+
if (likely(PageTail(page)))
412+
return head;
413+
}
404414
return page;
405415
}
406416

‎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
@@ -1651,7 +1651,7 @@ int soft_offline_page(struct page *page, int flags)
16511651
{
16521652
int ret;
16531653
unsigned long pfn = page_to_pfn(page);
1654-
struct page *hpage = compound_trans_head(page);
1654+
struct page *hpage = compound_head(page);
16551655

16561656
if (PageHWPoison(page)) {
16571657
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
@@ -369,9 +369,11 @@ void prep_compound_page(struct page *page, unsigned long order)
369369
__SetPageHead(page);
370370
for (i = 1; i < nr_pages; i++) {
371371
struct page *p = page + i;
372-
__SetPageTail(p);
373372
set_page_count(p, 0);
374373
p->first_page = page;
374+
/* Make sure p->first_page is always valid for PageTail() */
375+
smp_wmb();
376+
__SetPageTail(p);
375377
}
376378
}
377379

‎mm/swap.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void put_compound_page(struct page *page)
9898
}
9999

100100
/* __split_huge_page_refcount can run under us */
101-
page_head = compound_trans_head(page);
101+
page_head = compound_head(page);
102102

103103
/*
104104
* THP can not break up slab pages so avoid taking
@@ -253,7 +253,7 @@ bool __get_page_tail(struct page *page)
253253
*/
254254
unsigned long flags;
255255
bool got;
256-
struct page *page_head = compound_trans_head(page);
256+
struct page *page_head = compound_head(page);
257257

258258
/* Ref to put_compound_page() comment. */
259259
if (!__compound_tail_refcounted(page_head)) {

0 commit comments

Comments
 (0)