|
@@ -153,7 +153,10 @@ retry:
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (flags & FOLL_GET) {
|
|
if (flags & FOLL_GET) {
|
|
|
- get_page(page);
|
|
|
|
|
|
|
+ if (unlikely(!try_get_page(page))) {
|
|
|
|
|
+ page = ERR_PTR(-ENOMEM);
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/* drop the pgmap reference now that we hold the page */
|
|
/* drop the pgmap reference now that we hold the page */
|
|
|
if (pgmap) {
|
|
if (pgmap) {
|
|
@@ -296,7 +299,10 @@ retry_locked:
|
|
|
if (pmd_trans_unstable(pmd))
|
|
if (pmd_trans_unstable(pmd))
|
|
|
ret = -EBUSY;
|
|
ret = -EBUSY;
|
|
|
} else {
|
|
} else {
|
|
|
- get_page(page);
|
|
|
|
|
|
|
+ if (unlikely(!try_get_page(page))) {
|
|
|
|
|
+ spin_unlock(ptl);
|
|
|
|
|
+ return ERR_PTR(-ENOMEM);
|
|
|
|
|
+ }
|
|
|
spin_unlock(ptl);
|
|
spin_unlock(ptl);
|
|
|
lock_page(page);
|
|
lock_page(page);
|
|
|
ret = split_huge_page(page);
|
|
ret = split_huge_page(page);
|
|
@@ -480,7 +486,10 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,
|
|
|
if (is_device_public_page(*page))
|
|
if (is_device_public_page(*page))
|
|
|
goto unmap;
|
|
goto unmap;
|
|
|
}
|
|
}
|
|
|
- get_page(*page);
|
|
|
|
|
|
|
+ if (unlikely(!try_get_page(*page))) {
|
|
|
|
|
+ ret = -ENOMEM;
|
|
|
|
|
+ goto unmap;
|
|
|
|
|
+ }
|
|
|
out:
|
|
out:
|
|
|
ret = 0;
|
|
ret = 0;
|
|
|
unmap:
|
|
unmap:
|
|
@@ -1368,6 +1377,20 @@ static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+ * Return the compund head page with ref appropriately incremented,
|
|
|
|
|
+ * or NULL if that failed.
|
|
|
|
|
+ */
|
|
|
|
|
+static inline struct page *try_get_compound_head(struct page *page, int refs)
|
|
|
|
|
+{
|
|
|
|
|
+ struct page *head = compound_head(page);
|
|
|
|
|
+ if (WARN_ON_ONCE(page_ref_count(head) < 0))
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ if (unlikely(!page_cache_add_speculative(head, refs)))
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ return head;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
|
|
#ifdef CONFIG_ARCH_HAS_PTE_SPECIAL
|
|
|
static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
|
|
static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
|
|
|
int write, struct page **pages, int *nr)
|
|
int write, struct page **pages, int *nr)
|
|
@@ -1402,9 +1425,9 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
|
|
|
|
|
|
|
|
VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
|
|
VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
|
|
|
page = pte_page(pte);
|
|
page = pte_page(pte);
|
|
|
- head = compound_head(page);
|
|
|
|
|
|
|
|
|
|
- if (!page_cache_get_speculative(head))
|
|
|
|
|
|
|
+ head = try_get_compound_head(page, 1);
|
|
|
|
|
+ if (!head)
|
|
|
goto pte_unmap;
|
|
goto pte_unmap;
|
|
|
|
|
|
|
|
if (unlikely(pte_val(pte) != pte_val(*ptep))) {
|
|
if (unlikely(pte_val(pte) != pte_val(*ptep))) {
|
|
@@ -1543,8 +1566,8 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
|
|
|
refs++;
|
|
refs++;
|
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
|
|
|
|
|
|
- head = compound_head(pmd_page(orig));
|
|
|
|
|
- if (!page_cache_add_speculative(head, refs)) {
|
|
|
|
|
|
|
+ head = try_get_compound_head(pmd_page(orig), refs);
|
|
|
|
|
+ if (!head) {
|
|
|
*nr -= refs;
|
|
*nr -= refs;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -1581,8 +1604,8 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
|
|
|
refs++;
|
|
refs++;
|
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
|
|
|
|
|
|
- head = compound_head(pud_page(orig));
|
|
|
|
|
- if (!page_cache_add_speculative(head, refs)) {
|
|
|
|
|
|
|
+ head = try_get_compound_head(pud_page(orig), refs);
|
|
|
|
|
+ if (!head) {
|
|
|
*nr -= refs;
|
|
*nr -= refs;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -1618,8 +1641,8 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
|
|
|
refs++;
|
|
refs++;
|
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
} while (addr += PAGE_SIZE, addr != end);
|
|
|
|
|
|
|
|
- head = compound_head(pgd_page(orig));
|
|
|
|
|
- if (!page_cache_add_speculative(head, refs)) {
|
|
|
|
|
|
|
+ head = try_get_compound_head(pgd_page(orig), refs);
|
|
|
|
|
+ if (!head) {
|
|
|
*nr -= refs;
|
|
*nr -= refs;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|