Skip to content

Commit 15759cb

Browse files
kvaneeshmpe
authored andcommitted
powerpc/perf/callchain: Use __get_user_pages_fast in read_user_stack_slow
read_user_stack_slow is called with interrupts soft disabled and it copies contents from the page which we find mapped to a specific address. To convert userspace address to pfn, the kernel now uses lockless page table walk. The kernel needs to make sure the pfn value read remains stable and is not released and reused for another process while the contents are read from the page. This can only be achieved by holding a page reference. One of the first approaches I tried was to check the pte value after the kernel copies the contents from the page. But as shown below we can still get it wrong CPU0 CPU1 pte = READ_ONCE(*ptep); pte_clear(pte); put_page(page); page = alloc_page(); memcpy(page_address(page), "secret password", nr); memcpy(buf, kaddr + offset, nb); put_page(page); handle_mm_fault() page = alloc_page(); set_pte(pte, page); if (pte_val(pte) != pte_val(*ptep)) Hence switch to __get_user_pages_fast. Signed-off-by: Aneesh Kumar K.V <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0da81b6 commit 15759cb

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

arch/powerpc/perf/callchain_64.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,25 @@
2626
*/
2727
int read_user_stack_slow(void __user *ptr, void *buf, int nb)
2828
{
29-
int ret = -EFAULT;
30-
pgd_t *pgdir;
31-
pte_t *ptep, pte;
32-
unsigned int shift;
29+
3330
unsigned long addr = (unsigned long) ptr;
3431
unsigned long offset;
35-
unsigned long pfn, flags;
32+
struct page *page;
33+
int nrpages;
3634
void *kaddr;
3735

38-
pgdir = current->mm->pgd;
39-
if (!pgdir)
40-
return -EFAULT;
36+
nrpages = __get_user_pages_fast(addr, 1, 1, &page);
37+
if (nrpages == 1) {
38+
kaddr = page_address(page);
39+
40+
/* align address to page boundary */
41+
offset = addr & ~PAGE_MASK;
4142

42-
local_irq_save(flags);
43-
ptep = find_current_mm_pte(pgdir, addr, NULL, &shift);
44-
if (!ptep)
45-
goto err_out;
46-
if (!shift)
47-
shift = PAGE_SHIFT;
48-
49-
/* align address to page boundary */
50-
offset = addr & ((1UL << shift) - 1);
51-
52-
pte = READ_ONCE(*ptep);
53-
if (!pte_present(pte) || !pte_user(pte))
54-
goto err_out;
55-
pfn = pte_pfn(pte);
56-
if (!page_is_ram(pfn))
57-
goto err_out;
58-
59-
/* no highmem to worry about here */
60-
kaddr = pfn_to_kaddr(pfn);
61-
memcpy(buf, kaddr + offset, nb);
62-
ret = 0;
63-
err_out:
64-
local_irq_restore(flags);
65-
return ret;
43+
memcpy(buf, kaddr + offset, nb);
44+
put_page(page);
45+
return 0;
46+
}
47+
return -EFAULT;
6648
}
6749

6850
static int read_user_stack_64(unsigned long __user *ptr, unsigned long *ret)

0 commit comments

Comments
 (0)