]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mm: make page ref count overflow check tighter and more explicit
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 11 Apr 2019 17:06:20 +0000 (10:06 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 14 Apr 2019 17:00:04 +0000 (10:00 -0700)
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 <willy@infradead.org>
Cc: Jann Horn <jannh@google.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/mm.h

index 80bb6408fe73a8744c733362abf5446b237156ab..541d99b86aea9efe8df91ed29eae76e9e6e23918 100644 (file)
@@ -965,6 +965,10 @@ static inline bool is_pci_p2pdma_page(const struct page *page)
 }
 #endif /* CONFIG_DEV_PAGEMAP_OPS */
 
+/* 127: arbitrary random number, small enough to assemble well */
+#define page_ref_zero_or_close_to_overflow(page) \
+       ((unsigned int) page_ref_count(page) + 127u <= 127u)
+
 static inline void get_page(struct page *page)
 {
        page = compound_head(page);
@@ -972,7 +976,7 @@ static inline void get_page(struct page *page)
         * Getting a normal page or the head of a compound page
         * requires to already have an elevated page->_refcount.
         */
-       VM_BUG_ON_PAGE(page_ref_count(page) <= 0, page);
+       VM_BUG_ON_PAGE(page_ref_zero_or_close_to_overflow(page), page);
        page_ref_inc(page);
 }