Skip to content

Commit f958d7b

Browse files
committed
mm: make page ref count overflow check tighter and more explicit
We have a VM_BUG_ON() to check that the page reference count doesn't underflow (or get close to overflow) by checking the sign of the count. That's all fine, but we actually want to allow people to use a "get page ref unless it's already very high" helper function, and we want that one to use the sign of the page ref (without triggering this VM_BUG_ON). Change the VM_BUG_ON to only check for small underflows (or _very_ close to overflowing), and ignore overflows which have strayed into negative territory. Acked-by: Matthew Wilcox <[email protected]> Cc: Jann Horn <[email protected]> Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 1c163f4 commit f958d7b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

include/linux/mm.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,14 +965,18 @@ static inline bool is_pci_p2pdma_page(const struct page *page)
965965
}
966966
#endif /* CONFIG_DEV_PAGEMAP_OPS */
967967

968+
/* 127: arbitrary random number, small enough to assemble well */
969+
#define page_ref_zero_or_close_to_overflow(page) \
970+
((unsigned int) page_ref_count(page) + 127u <= 127u)
971+
968972
static inline void get_page(struct page *page)
969973
{
970974
page = compound_head(page);
971975
/*
972976
* Getting a normal page or the head of a compound page
973977
* requires to already have an elevated page->_refcount.
974978
*/
975-
VM_BUG_ON_PAGE(page_ref_count(page) <= 0, page);
979+
VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
976980
page_ref_inc(page);
977981
}
978982

0 commit comments

Comments
 (0)