Skip to content

Commit 6dc5ea1

Browse files
johnhubbardtorvalds
authored andcommitted
mm, dump_page: do not crash with bad compound_mapcount()
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 <cai@lca.pw> Suggested-by: Matthew Wilcox <willy@infradead.org> Signed-off-by: John Hubbard <jhubbard@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Vlastimil Babka <vbabka@suse.cz> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Mike Rapoport <rppt@linux.ibm.com> Cc: William Kucharski <william.kucharski@oracle.com> Link: http://lkml.kernel.org/r/20200804214807.169256-1-jhubbard@nvidia.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 54a7515 commit 6dc5ea1

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎include/linux/mm.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,11 @@ static inline void *kvcalloc(size_t n, size_t size, gfp_t flags)
779779
extern void kvfree(const void *addr);
780780
extern void kvfree_sensitive(const void *addr, size_t len);
781781

782+
static inline int head_mapcount(struct page *head)
783+
{
784+
return atomic_read(compound_mapcount_ptr(head)) + 1;
785+
}
786+
782787
/*
783788
* Mapcount of compound page as a whole, does not include mapped sub-pages.
784789
*
@@ -788,7 +793,7 @@ static inline int compound_mapcount(struct page *page)
788793
{
789794
VM_BUG_ON_PAGE(!PageCompound(page), page);
790795
page = compound_head(page);
791-
return atomic_read(compound_mapcount_ptr(page)) + 1;
796+
return head_mapcount(page);
792797
}
793798

794799
/*
@@ -901,11 +906,16 @@ static inline bool hpage_pincount_available(struct page *page)
901906
return PageCompound(page) && compound_order(page) > 1;
902907
}
903908

909+
static inline int head_pincount(struct page *head)
910+
{
911+
return atomic_read(compound_pincount_ptr(head));
912+
}
913+
904914
static inline int compound_pincount(struct page *page)
905915
{
906916
VM_BUG_ON_PAGE(!hpage_pincount_available(page), page);
907917
page = compound_head(page);
908-
return atomic_read(compound_pincount_ptr(page));
918+
return head_pincount(page);
909919
}
910920

911921
static inline void set_compound_order(struct page *page, unsigned int order)

‎mm/debug.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ void __dump_page(struct page *page, const char *reason)
102102
if (hpage_pincount_available(page)) {
103103
pr_warn("head:%p order:%u compound_mapcount:%d compound_pincount:%d\n",
104104
head, compound_order(head),
105-
compound_mapcount(head),
106-
compound_pincount(head));
105+
head_mapcount(head),
106+
head_pincount(head));
107107
} else {
108108
pr_warn("head:%p order:%u compound_mapcount:%d\n",
109109
head, compound_order(head),
110-
compound_mapcount(head));
110+
head_mapcount(head));
111111
}
112112
}
113113
if (PageKsm(page))

0 commit comments

Comments
 (0)