Skip to content

Commit 4716023

Browse files
gthelenPeter Zijlstra
authored andcommitted
perf/core: Avoid put_page() when GUP fails
PEBS PERF_SAMPLE_PHYS_ADDR events use perf_virt_to_phys() to convert PMU sampled virtual addresses to physical using get_user_page_fast_only() and page_to_phys(). Some get_user_page_fast_only() error cases return false, indicating no page reference, but still initialize the output page pointer with an unreferenced page. In these error cases perf_virt_to_phys() calls put_page(). This causes page reference count underflow, which can lead to unintentional page sharing. Fix perf_virt_to_phys() to only put_page() if get_user_page_fast_only() returns a referenced page. Fixes: fc7ce9c ("perf/core, x86: Add PERF_SAMPLE_PHYS_ADDR") Signed-off-by: Greg Thelen <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 5863702 commit 4716023

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

kernel/events/core.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7154,7 +7154,6 @@ void perf_output_sample(struct perf_output_handle *handle,
71547154
static u64 perf_virt_to_phys(u64 virt)
71557155
{
71567156
u64 phys_addr = 0;
7157-
struct page *p = NULL;
71587157

71597158
if (!virt)
71607159
return 0;
@@ -7173,14 +7172,15 @@ static u64 perf_virt_to_phys(u64 virt)
71737172
* If failed, leave phys_addr as 0.
71747173
*/
71757174
if (current->mm != NULL) {
7175+
struct page *p;
7176+
71767177
pagefault_disable();
7177-
if (get_user_page_fast_only(virt, 0, &p))
7178+
if (get_user_page_fast_only(virt, 0, &p)) {
71787179
phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
7180+
put_page(p);
7181+
}
71797182
pagefault_enable();
71807183
}
7181-
7182-
if (p)
7183-
put_page(p);
71847184
}
71857185

71867186
return phys_addr;

0 commit comments

Comments
 (0)