Skip to content

Commit 28b2ee2

Browse files
Rik van Rieltorvalds
authored andcommitted
access_process_vm device memory infrastructure
In order to be able to debug things like the X server and programs using the PPC Cell SPUs, the debugger needs to be able to access device memory through ptrace and /proc/pid/mem. This patch: Add the generic_access_phys access function and put the hooks in place to allow access_process_vm to access device or PPC Cell SPU memory. [riel@redhat.com: Add documentation for the vm_ops->access function] Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: Benjamin Herrensmidt <benh@kernel.crashing.org> Cc: Dave Airlie <airlied@linux.ie> Cc: Hugh Dickins <hugh@veritas.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Arnd Bergmann <arnd@arndb.de> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0d71d10 commit 28b2ee2

8 files changed

Lines changed: 144 additions & 18 deletions

File tree

‎Documentation/filesystems/Locking‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,15 @@ prototypes:
510510
void (*close)(struct vm_area_struct*);
511511
int (*fault)(struct vm_area_struct*, struct vm_fault *);
512512
int (*page_mkwrite)(struct vm_area_struct *, struct page *);
513+
int (*access)(struct vm_area_struct *, unsigned long, void*, int, int);
513514

514515
locking rules:
515516
BKL mmap_sem PageLocked(page)
516517
open: no yes
517518
close: no yes
518519
fault: no yes
519520
page_mkwrite: no yes no
521+
access: no yes
520522

521523
->page_mkwrite() is called when a previously read-only page is
522524
about to become writeable. The file system is responsible for
@@ -525,6 +527,11 @@ taking to lock out truncate, the page range should be verified to be
525527
within i_size. The page mapping should also be checked that it is not
526528
NULL.
527529

530+
->access() is called when get_user_pages() fails in
531+
acces_process_vm(), typically used to debug a process through
532+
/proc/pid/mem or ptrace. This function is needed only for
533+
VM_IO | VM_PFNMAP VMAs.
534+
528535
================================================================================
529536
Dubious stuff
530537

‎arch/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ config KRETPROBES
3131
def_bool y
3232
depends on KPROBES && HAVE_KRETPROBES
3333

34+
config HAVE_IOREMAP_PROT
35+
def_bool n
36+
3437
config HAVE_KPROBES
3538
def_bool n
3639

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ config X86
2121
select HAVE_UNSTABLE_SCHED_CLOCK
2222
select HAVE_IDE
2323
select HAVE_OPROFILE
24+
select HAVE_IOREMAP_PROT
2425
select HAVE_KPROBES
2526
select HAVE_KRETPROBES
2627
select HAVE_DYNAMIC_FTRACE

‎arch/x86/mm/ioremap.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ static void __iomem *ioremap_default(resource_size_t phys_addr,
330330
return (void __iomem *)ret;
331331
}
332332

333+
void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
334+
unsigned long prot_val)
335+
{
336+
return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
337+
__builtin_return_address(0));
338+
}
339+
EXPORT_SYMBOL(ioremap_prot);
340+
333341
/**
334342
* iounmap - Free a IO remapping
335343
* @addr: virtual address from ioremap_*

‎include/asm-x86/io_32.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ static inline void *phys_to_virt(unsigned long address)
110110
*/
111111
extern void __iomem *ioremap_nocache(resource_size_t offset, unsigned long size);
112112
extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size);
113+
extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size,
114+
unsigned long prot_val);
113115

114116
/*
115117
* The default ioremap() behavior is non-cached:

‎include/asm-x86/io_64.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ extern void early_iounmap(void *addr, unsigned long size);
175175
*/
176176
extern void __iomem *ioremap_nocache(resource_size_t offset, unsigned long size);
177177
extern void __iomem *ioremap_cache(resource_size_t offset, unsigned long size);
178+
extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size,
179+
unsigned long prot_val);
178180

179181
/*
180182
* The default ioremap() behavior is non-cached:

‎include/linux/mm.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ struct vm_operations_struct {
170170
/* notification that a previously read-only page is about to become
171171
* writable, if an error is returned it will cause a SIGBUS */
172172
int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
173+
174+
/* called by access_process_vm when get_user_pages() fails, typically
175+
* for use by special VMAs that can switch between memory and hardware
176+
*/
177+
int (*access)(struct vm_area_struct *vma, unsigned long addr,
178+
void *buf, int len, int write);
173179
#ifdef CONFIG_NUMA
174180
/*
175181
* set_policy() op must add a reference to any non-NULL @new mempolicy
@@ -771,6 +777,8 @@ int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
771777
struct vm_area_struct *vma);
772778
void unmap_mapping_range(struct address_space *mapping,
773779
loff_t const holebegin, loff_t const holelen, int even_cows);
780+
int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
781+
void *buf, int len, int write);
774782

775783
static inline void unmap_shared_mapping_range(struct address_space *mapping,
776784
loff_t const holebegin, loff_t const holelen)

‎mm/memory.c‎

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,86 @@ int in_gate_area_no_task(unsigned long addr)
27512751

27522752
#endif /* __HAVE_ARCH_GATE_AREA */
27532753

2754+
#ifdef CONFIG_HAVE_IOREMAP_PROT
2755+
static resource_size_t follow_phys(struct vm_area_struct *vma,
2756+
unsigned long address, unsigned int flags,
2757+
unsigned long *prot)
2758+
{
2759+
pgd_t *pgd;
2760+
pud_t *pud;
2761+
pmd_t *pmd;
2762+
pte_t *ptep, pte;
2763+
spinlock_t *ptl;
2764+
resource_size_t phys_addr = 0;
2765+
struct mm_struct *mm = vma->vm_mm;
2766+
2767+
VM_BUG_ON(!(vma->vm_flags & (VM_IO | VM_PFNMAP)));
2768+
2769+
pgd = pgd_offset(mm, address);
2770+
if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
2771+
goto no_page_table;
2772+
2773+
pud = pud_offset(pgd, address);
2774+
if (pud_none(*pud) || unlikely(pud_bad(*pud)))
2775+
goto no_page_table;
2776+
2777+
pmd = pmd_offset(pud, address);
2778+
if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
2779+
goto no_page_table;
2780+
2781+
/* We cannot handle huge page PFN maps. Luckily they don't exist. */
2782+
if (pmd_huge(*pmd))
2783+
goto no_page_table;
2784+
2785+
ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
2786+
if (!ptep)
2787+
goto out;
2788+
2789+
pte = *ptep;
2790+
if (!pte_present(pte))
2791+
goto unlock;
2792+
if ((flags & FOLL_WRITE) && !pte_write(pte))
2793+
goto unlock;
2794+
phys_addr = pte_pfn(pte);
2795+
phys_addr <<= PAGE_SHIFT; /* Shift here to avoid overflow on PAE */
2796+
2797+
*prot = pgprot_val(pte_pgprot(pte));
2798+
2799+
unlock:
2800+
pte_unmap_unlock(ptep, ptl);
2801+
out:
2802+
return phys_addr;
2803+
no_page_table:
2804+
return 0;
2805+
}
2806+
2807+
int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
2808+
void *buf, int len, int write)
2809+
{
2810+
resource_size_t phys_addr;
2811+
unsigned long prot = 0;
2812+
void *maddr;
2813+
int offset = addr & (PAGE_SIZE-1);
2814+
2815+
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
2816+
return -EINVAL;
2817+
2818+
phys_addr = follow_phys(vma, addr, write, &prot);
2819+
2820+
if (!phys_addr)
2821+
return -EINVAL;
2822+
2823+
maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
2824+
if (write)
2825+
memcpy_toio(maddr + offset, buf, len);
2826+
else
2827+
memcpy_fromio(buf, maddr + offset, len);
2828+
iounmap(maddr);
2829+
2830+
return len;
2831+
}
2832+
#endif
2833+
27542834
/*
27552835
* Access another process' address space.
27562836
* Source/target buffer must be kernel space,
@@ -2760,7 +2840,6 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in
27602840
{
27612841
struct mm_struct *mm;
27622842
struct vm_area_struct *vma;
2763-
struct page *page;
27642843
void *old_buf = buf;
27652844

27662845
mm = get_task_mm(tsk);
@@ -2772,28 +2851,44 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in
27722851
while (len) {
27732852
int bytes, ret, offset;
27742853
void *maddr;
2854+
struct page *page = NULL;
27752855

27762856
ret = get_user_pages(tsk, mm, addr, 1,
27772857
write, 1, &page, &vma);
2778-
if (ret <= 0)
2779-
break;
2780-
2781-
bytes = len;
2782-
offset = addr & (PAGE_SIZE-1);
2783-
if (bytes > PAGE_SIZE-offset)
2784-
bytes = PAGE_SIZE-offset;
2785-
2786-
maddr = kmap(page);
2787-
if (write) {
2788-
copy_to_user_page(vma, page, addr,
2789-
maddr + offset, buf, bytes);
2790-
set_page_dirty_lock(page);
2858+
if (ret <= 0) {
2859+
/*
2860+
* Check if this is a VM_IO | VM_PFNMAP VMA, which
2861+
* we can access using slightly different code.
2862+
*/
2863+
#ifdef CONFIG_HAVE_IOREMAP_PROT
2864+
vma = find_vma(mm, addr);
2865+
if (!vma)
2866+
break;
2867+
if (vma->vm_ops && vma->vm_ops->access)
2868+
ret = vma->vm_ops->access(vma, addr, buf,
2869+
len, write);
2870+
if (ret <= 0)
2871+
#endif
2872+
break;
2873+
bytes = ret;
27912874
} else {
2792-
copy_from_user_page(vma, page, addr,
2793-
buf, maddr + offset, bytes);
2875+
bytes = len;
2876+
offset = addr & (PAGE_SIZE-1);
2877+
if (bytes > PAGE_SIZE-offset)
2878+
bytes = PAGE_SIZE-offset;
2879+
2880+
maddr = kmap(page);
2881+
if (write) {
2882+
copy_to_user_page(vma, page, addr,
2883+
maddr + offset, buf, bytes);
2884+
set_page_dirty_lock(page);
2885+
} else {
2886+
copy_from_user_page(vma, page, addr,
2887+
buf, maddr + offset, bytes);
2888+
}
2889+
kunmap(page);
2890+
page_cache_release(page);
27942891
}
2795-
kunmap(page);
2796-
page_cache_release(page);
27972892
len -= bytes;
27982893
buf += bytes;
27992894
addr += bytes;

0 commit comments

Comments
 (0)