Skip to contentSkip to conversation

oprofile: add check_user_page_readable()

PATCH In review Greg Banks 2 participants 5 messages Nov 9, 2004

Subsystems (2)
lore
Share & tools
lore

Greg Banks

author
#0 lore raw

PATCH 1/11 oprofile: add check_user_page_readable()

Signature · 3 lines
-- 
Greg Banks, R&D Software Engineer, SGI Australian Software Group.
I don't speak for SGI.

Add check_user_page_readable() for kernel modules which need to follow user space addresses but can't use get_user().

---

 include/linux/mm.h |    1 +
 mm/memory.c        |   20 ++++++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
include/linux/mm.h +1 -0
Index: linux/include/linux/mm.h
===================================================================
@@ -789,6 +789,7 @@ extern struct page * vmalloc_to_page(voi
 extern unsigned long vmalloc_to_pfn(void *addr);
 extern struct page * follow_page(struct mm_struct *mm, unsigned long address,
 		int write);
+extern int check_user_page_readable(struct mm_struct *mm, unsigned long address);
 int remap_pfn_range(struct vm_area_struct *, unsigned long,
 		unsigned long, unsigned long, pgprot_t);
 
mm/memory.c +18 -2
Index: linux/mm/memory.c
===================================================================
@@ -746,8 +746,8 @@ void zap_page_range(struct vm_area_struc
  * Do a quick page-table lookup for a single page.
  * mm->page_table_lock must be held.
  */
-struct page *
-follow_page(struct mm_struct *mm, unsigned long address, int write) 
+static struct page *
+__follow_page(struct mm_struct *mm, unsigned long address, int read, int write)
 {
 	pml4_t *pml4;
 	pgd_t *pgd;
@@ -790,6 +790,8 @@ follow_page(struct mm_struct *mm, unsign
 	if (pte_present(pte)) {
 		if (write && !pte_write(pte))
 			goto out;
+		if (read && !pte_read(pte))
+			goto out;
 		pfn = pte_pfn(pte);
 		if (pfn_valid(pfn)) {
 			page = pfn_to_page(pfn);
@@ -804,6 +806,20 @@ out:
 	return NULL;
 }
 
+struct page *
+follow_page(struct mm_struct *mm, unsigned long address, int write) 
+{
+	return __follow_page(mm, address, /*read*/0, write);
+}
+
+int
+check_user_page_readable(struct mm_struct *mm, unsigned long address)
+{
+	return __follow_page(mm, address, /*read*/1, /*write*/0) != NULL;
+}
+
+EXPORT_SYMBOL(check_user_page_readable);
+
 /* 
  * Given a physical address, is there a useful struct page pointing to
  * it?  This may become more complex in the future if we start dealing
#1 lore raw
Quoted 3 lines · Greg Banks

Add check_user_page_readable() for kernel modules which need
to follow user space addresses but can't use get_user().

Strange. What is the usage pattern for this? And why is that usage pattern not racy in the presence of paging activity?

Did you consider use_mm(), in conjunction with get_user()?

author
#2 lore raw
Quoted 6 lines · Andrew Morton
Quoted 3 lines · Greg Banks

Add check_user_page_readable() for kernel modules which need
to follow user space addresses but can't use get_user().

Strange. What is the usage pattern for this?

The i386 callgraph code attempts to follow user stacks, from an interrupt (perfmon, NMI, or timer) where get_user() is explicitly disallowed by Documentation/DocBook/kernel-locking.tmpl. AFAICS from the ia64 and i386 page fault handlers get_user should "just work" and return -EFAULT if the page isn't resident or readable, but the doc says...

Currently this is only an issue for i386. The ia64 code doesn't even try to look at user stacks (shudder).

Quoted 2 lines

And why is that usage
pattern not racy in the presence of paging activity?

The i386 backtracer takes the &current->mm->page_table_lock, and just drops out of the trace early if a page isn't resident. It doesn't expect or try to page in. After all this is only statistical sampling not write() data.

Quoted 2 lines

Did you consider use_mm(), in conjunction with get_user()?

No, but glancing at use_mm() the comment says

* (Note: this routine is intended to be called only
* from a kernel thread context)

Greg.

Signature · 2 lines
Greg Banks, R&D Software Engineer, SGI Australian Software Group.
I don't speak for SGI.
#3 lore raw
Quoted 11 lines · Greg Banks
Quoted 6 lines · Andrew Morton
Quoted 3 lines · Greg Banks

Add check_user_page_readable() for kernel modules which need
to follow user space addresses but can't use get_user().

Strange. What is the usage pattern for this?

The i386 callgraph code attempts to follow user stacks, from an interrupt (perfmon, NMI, or timer)

Yikes.

Quoted 6 lines

where get_user() is
explicitly disallowed by Documentation/DocBook/kernel-locking.tmpl. AFAICS from the ia64 and i386 page fault handlers get_user should "just work" and return -EFAULT if the page isn't resident or readable, but the doc says...

Quoted 7 lines

Currently this is only an issue for i386. The ia64 code doesn't even try to look at user stacks (shudder).

Quoted 2 lines

And why is that usage
pattern not racy in the presence of paging activity?

The i386 backtracer takes the &current->mm->page_table_lock,

But that cannot be taken from interrupt context. A trylock would be OK I guess.

Quoted 6 lines

and
just drops out of the trace early if a page isn't resident. It doesn't expect or try to page in. After all this is only statistical sampling not write() data.

Quoted 1 line
Quoted 6 lines
Quoted 1 line

Did you consider use_mm(), in conjunction with get_user()?

No, but glancing at use_mm() the comment says

* (Note: this routine is intended to be called only
* from a kernel thread context)

It could probably be made to work from a normal process, but not from interrupt context.

I guess I should apply the patches and take a closer look.

author
#4 lore raw
Quoted 4 lines · Andrew Morton
Quoted 2 lines

The i386 callgraph code attempts to follow user stacks, from an interrupt (perfmon, NMI, or timer)

Yikes.

There are a number of problems with this, for example modern libcs built with -fomit-frame-pointer limit it's usefulness. But when it does get meaningful traces it's really quite useful.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

And why is that usage
pattern not racy in the presence of paging activity?

The i386 backtracer takes the &current->mm->page_table_lock,

But that cannot be taken from interrupt context. A trylock would be OK I guess.

The code reads:

#ifdef CONFIG_SMP
        if (!spin_trylock(&current->mm->page_table_lock))
                return;

#endif

i.e. it tries to get the lock and abandons the trace if it can't.

Greg.

Signature · 2 lines
Greg Banks, R&D Software Engineer, SGI Australian Software Group.
I don't speak for SGI.