Skip to content

Commit

Permalink
mm, dump_page: do not crash with bad compound_mapcount()
Browse files Browse the repository at this point in the history
If a compound page is being split while dump_page() is being run on that
page, we can end up calling compound_mapcount() on a page that is no
longer compound.  This leads to a crash (already seen at least once in the
field), due to the VM_BUG_ON_PAGE() assertion inside compound_mapcount().

(The above is from Matthew Wilcox's analysis of Qian Cai's bug report.)

A similar problem is possible, via compound_pincount() instead of
compound_mapcount().

In order to avoid this kind of crash, make dump_page() slightly more
robust, by providing a pair of simpler routines that don't contain
assertions: head_mapcount() and head_pincount().

For debug tools, we don't want to go *too* far in this direction, but this
is a simple small fix, and the crash has already been seen, so it's a good
trade-off.

Reported-by: Qian Cai <[email protected]>
Suggested-by: Matthew Wilcox <[email protected]>
Signed-off-by: John Hubbard <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Acked-by: Vlastimil Babka <[email protected]>
Cc: Kirill A. Shutemov <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: William Kucharski <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
johnhubbard authored and torvalds committed Aug 7, 2020
1 parent 54a7515 commit 6dc5ea1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 12 additions & 2 deletions include/linux/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ static inline void *kvcalloc(size_t n, size_t size, gfp_t flags)
extern void kvfree(const void *addr);
extern void kvfree_sensitive(const void *addr, size_t len);

static inline int head_mapcount(struct page *head)
{
return atomic_read(compound_mapcount_ptr(head)) + 1;
}

/*
* Mapcount of compound page as a whole, does not include mapped sub-pages.
*
Expand All @@ -788,7 +793,7 @@ static inline int compound_mapcount(struct page *page)
{
VM_BUG_ON_PAGE(!PageCompound(page), page);
page = compound_head(page);
return atomic_read(compound_mapcount_ptr(page)) + 1;
return head_mapcount(page);
}

/*
Expand Down Expand Up @@ -901,11 +906,16 @@ static inline bool hpage_pincount_available(struct page *page)
return PageCompound(page) && compound_order(page) > 1;
}

static inline int head_pincount(struct page *head)
{
return atomic_read(compound_pincount_ptr(head));
}

static inline int compound_pincount(struct page *page)
{
VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
page = compound_head(page);
return atomic_read(compound_pincount_ptr(page));
return head_pincount(page);
}

static inline void set_compound_order(struct page *page, unsigned int order)
Expand Down
6 changes: 3 additions & 3 deletions mm/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ void __dump_page(struct page *page, const char *reason)
if (hpage_pincount_available(page)) {
pr_warn("head:%p order:%u compound_mapcount:%d compound_pincount:%d\n",
head, compound_order(head),
compound_mapcount(head),
compound_pincount(head));
head_mapcount(head),
head_pincount(head));
} else {
pr_warn("head:%p order:%u compound_mapcount:%d\n",
head, compound_order(head),
compound_mapcount(head));
head_mapcount(head));
}
}
if (PageKsm(page))
Expand Down

0 comments on commit 6dc5ea1

Please sign in to comment.