proc: fix oops on invalid /proc/<pid>/maps access, CVE-2011-1020
[linux-flexiantxendom0-natty.git] / fs / proc / task_mmu.c
1 #include <linux/mm.h>
2 #include <linux/hugetlb.h>
3 #include <linux/mount.h>
4 #include <linux/seq_file.h>
5 #include <linux/highmem.h>
6 #include <linux/ptrace.h>
7 #include <linux/slab.h>
8 #include <linux/pagemap.h>
9 #include <linux/mempolicy.h>
10 #include <linux/swap.h>
11 #include <linux/swapops.h>
12
13 #include <asm/elf.h>
14 #include <asm/uaccess.h>
15 #include <asm/tlbflush.h>
16 #include "internal.h"
17
18 void task_mem(struct seq_file *m, struct mm_struct *mm)
19 {
20         unsigned long data, text, lib, swap;
21         unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
22
23         /*
24          * Note: to minimize their overhead, mm maintains hiwater_vm and
25          * hiwater_rss only when about to *lower* total_vm or rss.  Any
26          * collector of these hiwater stats must therefore get total_vm
27          * and rss too, which will usually be the higher.  Barriers? not
28          * worth the effort, such snapshots can always be inconsistent.
29          */
30         hiwater_vm = total_vm = mm->total_vm;
31         if (hiwater_vm < mm->hiwater_vm)
32                 hiwater_vm = mm->hiwater_vm;
33         hiwater_rss = total_rss = get_mm_rss(mm);
34         if (hiwater_rss < mm->hiwater_rss)
35                 hiwater_rss = mm->hiwater_rss;
36
37         data = mm->total_vm - mm->shared_vm - mm->stack_vm;
38         text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
39         lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
40         swap = get_mm_counter(mm, MM_SWAPENTS);
41         seq_printf(m,
42                 "VmPeak:\t%8lu kB\n"
43                 "VmSize:\t%8lu kB\n"
44                 "VmLck:\t%8lu kB\n"
45                 "VmHWM:\t%8lu kB\n"
46                 "VmRSS:\t%8lu kB\n"
47                 "VmData:\t%8lu kB\n"
48                 "VmStk:\t%8lu kB\n"
49                 "VmExe:\t%8lu kB\n"
50                 "VmLib:\t%8lu kB\n"
51                 "VmPTE:\t%8lu kB\n"
52                 "VmSwap:\t%8lu kB\n",
53                 hiwater_vm << (PAGE_SHIFT-10),
54                 (total_vm - mm->reserved_vm) << (PAGE_SHIFT-10),
55                 mm->locked_vm << (PAGE_SHIFT-10),
56                 hiwater_rss << (PAGE_SHIFT-10),
57                 total_rss << (PAGE_SHIFT-10),
58                 data << (PAGE_SHIFT-10),
59                 mm->stack_vm << (PAGE_SHIFT-10), text, lib,
60                 (PTRS_PER_PTE*sizeof(pte_t)*mm->nr_ptes) >> 10,
61                 swap << (PAGE_SHIFT-10));
62 }
63
64 unsigned long task_vsize(struct mm_struct *mm)
65 {
66         return PAGE_SIZE * mm->total_vm;
67 }
68
69 unsigned long task_statm(struct mm_struct *mm,
70                          unsigned long *shared, unsigned long *text,
71                          unsigned long *data, unsigned long *resident)
72 {
73         *shared = get_mm_counter(mm, MM_FILEPAGES);
74         *text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
75                                                                 >> PAGE_SHIFT;
76         *data = mm->total_vm - mm->shared_vm;
77         *resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
78         return mm->total_vm;
79 }
80
81 static void pad_len_spaces(struct seq_file *m, int len)
82 {
83         len = 25 + sizeof(void*) * 6 - len;
84         if (len < 1)
85                 len = 1;
86         seq_printf(m, "%*c", len, ' ');
87 }
88
89 static void vma_stop(struct proc_maps_private *priv, struct vm_area_struct *vma)
90 {
91         if (vma && vma != priv->tail_vma) {
92                 struct mm_struct *mm = vma->vm_mm;
93                 up_read(&mm->mmap_sem);
94                 mmput(mm);
95         }
96 }
97
98 static void *m_start(struct seq_file *m, loff_t *pos)
99 {
100         struct proc_maps_private *priv = m->private;
101         unsigned long last_addr = m->version;
102         struct mm_struct *mm;
103         struct vm_area_struct *vma, *tail_vma = NULL;
104         loff_t l = *pos;
105
106         /* Clear the per syscall fields in priv */
107         priv->task = NULL;
108         priv->tail_vma = NULL;
109
110         /*
111          * We remember last_addr rather than next_addr to hit with
112          * mmap_cache most of the time. We have zero last_addr at
113          * the beginning and also after lseek. We will have -1 last_addr
114          * after the end of the vmas.
115          */
116
117         if (last_addr == -1UL)
118                 return NULL;
119
120         priv->task = get_pid_task(priv->pid, PIDTYPE_PID);
121         if (!priv->task)
122                 return ERR_PTR(-ESRCH);
123
124         mm = mm_for_maps(priv->task);
125         if (!mm || IS_ERR(mm))
126                 return mm;
127         down_read(&mm->mmap_sem);
128
129         tail_vma = get_gate_vma(priv->task);
130         priv->tail_vma = tail_vma;
131
132         /* Start with last addr hint */
133         vma = find_vma(mm, last_addr);
134         if (last_addr && vma) {
135                 vma = vma->vm_next;
136                 goto out;
137         }
138
139         /*
140          * Check the vma index is within the range and do
141          * sequential scan until m_index.
142          */
143         vma = NULL;
144         if ((unsigned long)l < mm->map_count) {
145                 vma = mm->mmap;
146                 while (l-- && vma)
147                         vma = vma->vm_next;
148                 goto out;
149         }
150
151         if (l != mm->map_count)
152                 tail_vma = NULL; /* After gate vma */
153
154 out:
155         if (vma)
156                 return vma;
157
158         /* End of vmas has been reached */
159         m->version = (tail_vma != NULL)? 0: -1UL;
160         up_read(&mm->mmap_sem);
161         mmput(mm);
162         return tail_vma;
163 }
164
165 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
166 {
167         struct proc_maps_private *priv = m->private;
168         struct vm_area_struct *vma = v;
169         struct vm_area_struct *tail_vma = priv->tail_vma;
170
171         (*pos)++;
172         if (vma && (vma != tail_vma) && vma->vm_next)
173                 return vma->vm_next;
174         vma_stop(priv, vma);
175         return (vma != tail_vma)? tail_vma: NULL;
176 }
177
178 static void m_stop(struct seq_file *m, void *v)
179 {
180         struct proc_maps_private *priv = m->private;
181         struct vm_area_struct *vma = v;
182
183         if (!IS_ERR(vma))
184                 vma_stop(priv, vma);
185         if (priv->task)
186                 put_task_struct(priv->task);
187 }
188
189 static int do_maps_open(struct inode *inode, struct file *file,
190                         const struct seq_operations *ops)
191 {
192         struct proc_maps_private *priv;
193         int ret = -ENOMEM;
194         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
195         if (priv) {
196                 priv->pid = proc_pid(inode);
197                 ret = seq_open(file, ops);
198                 if (!ret) {
199                         struct seq_file *m = file->private_data;
200                         m->private = priv;
201                 } else {
202                         kfree(priv);
203                 }
204         }
205         return ret;
206 }
207
208 static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
209 {
210         struct mm_struct *mm = vma->vm_mm;
211         struct file *file = vma->vm_file;
212         int flags = vma->vm_flags;
213         unsigned long ino = 0;
214         unsigned long long pgoff = 0;
215         unsigned long start, end;
216         dev_t dev = 0;
217         int len;
218
219         if (file) {
220                 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
221                 dev = inode->i_sb->s_dev;
222                 ino = inode->i_ino;
223                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
224         }
225
226         /* We don't show the stack guard page in /proc/maps */
227         start = vma->vm_start;
228         if (stack_guard_page_start(vma, start))
229                 start += PAGE_SIZE;
230         end = vma->vm_end;
231         if (stack_guard_page_end(vma, end))
232                 end -= PAGE_SIZE;
233
234         seq_printf(m, "%08lx-%08lx %c%c%c%c %08llx %02x:%02x %lu %n",
235                         start,
236                         end,
237                         flags & VM_READ ? 'r' : '-',
238                         flags & VM_WRITE ? 'w' : '-',
239                         flags & VM_EXEC ? 'x' : '-',
240                         flags & VM_MAYSHARE ? 's' : 'p',
241                         pgoff,
242                         MAJOR(dev), MINOR(dev), ino, &len);
243
244         /*
245          * Print the dentry name for named mappings, and a
246          * special [heap] marker for the heap:
247          */
248         if (file) {
249                 pad_len_spaces(m, len);
250                 seq_path(m, &file->f_path, "\n");
251         } else {
252                 const char *name = arch_vma_name(vma);
253                 if (!name) {
254                         if (mm) {
255                                 if (vma->vm_start <= mm->brk &&
256                                                 vma->vm_end >= mm->start_brk) {
257                                         name = "[heap]";
258                                 } else if (vma->vm_start <= mm->start_stack &&
259                                            vma->vm_end >= mm->start_stack) {
260                                         name = "[stack]";
261                                 }
262                         } else {
263                                 name = "[vdso]";
264                         }
265                 }
266                 if (name) {
267                         pad_len_spaces(m, len);
268                         seq_puts(m, name);
269                 }
270         }
271         seq_putc(m, '\n');
272 }
273
274 static int show_map(struct seq_file *m, void *v)
275 {
276         struct vm_area_struct *vma = v;
277         struct proc_maps_private *priv = m->private;
278         struct task_struct *task = priv->task;
279
280         show_map_vma(m, vma);
281
282         if (m->count < m->size)  /* vma is copied successfully */
283                 m->version = (vma != get_gate_vma(task))? vma->vm_start: 0;
284         return 0;
285 }
286
287 static const struct seq_operations proc_pid_maps_op = {
288         .start  = m_start,
289         .next   = m_next,
290         .stop   = m_stop,
291         .show   = show_map
292 };
293
294 static int maps_open(struct inode *inode, struct file *file)
295 {
296         return do_maps_open(inode, file, &proc_pid_maps_op);
297 }
298
299 const struct file_operations proc_maps_operations = {
300         .open           = maps_open,
301         .read           = seq_read,
302         .llseek         = seq_lseek,
303         .release        = seq_release_private,
304 };
305
306 /*
307  * Proportional Set Size(PSS): my share of RSS.
308  *
309  * PSS of a process is the count of pages it has in memory, where each
310  * page is divided by the number of processes sharing it.  So if a
311  * process has 1000 pages all to itself, and 1000 shared with one other
312  * process, its PSS will be 1500.
313  *
314  * To keep (accumulated) division errors low, we adopt a 64bit
315  * fixed-point pss counter to minimize division errors. So (pss >>
316  * PSS_SHIFT) would be the real byte count.
317  *
318  * A shift of 12 before division means (assuming 4K page size):
319  *      - 1M 3-user-pages add up to 8KB errors;
320  *      - supports mapcount up to 2^24, or 16M;
321  *      - supports PSS up to 2^52 bytes, or 4PB.
322  */
323 #define PSS_SHIFT 12
324
325 #ifdef CONFIG_PROC_PAGE_MONITOR
326 struct mem_size_stats {
327         struct vm_area_struct *vma;
328         unsigned long resident;
329         unsigned long shared_clean;
330         unsigned long shared_dirty;
331         unsigned long private_clean;
332         unsigned long private_dirty;
333         unsigned long referenced;
334         unsigned long anonymous;
335         unsigned long swap;
336         u64 pss;
337 };
338
339 static int smaps_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
340                            struct mm_walk *walk)
341 {
342         struct mem_size_stats *mss = walk->private;
343         struct vm_area_struct *vma = mss->vma;
344         pte_t *pte, ptent;
345         spinlock_t *ptl;
346         struct page *page;
347         int mapcount;
348
349         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
350         for (; addr != end; pte++, addr += PAGE_SIZE) {
351                 ptent = *pte;
352
353                 if (is_swap_pte(ptent)) {
354                         mss->swap += PAGE_SIZE;
355                         continue;
356                 }
357
358                 if (!pte_present(ptent))
359                         continue;
360
361                 page = vm_normal_page(vma, addr, ptent);
362                 if (!page)
363                         continue;
364
365                 if (PageAnon(page))
366                         mss->anonymous += PAGE_SIZE;
367
368                 mss->resident += PAGE_SIZE;
369                 /* Accumulate the size in pages that have been accessed. */
370                 if (pte_young(ptent) || PageReferenced(page))
371                         mss->referenced += PAGE_SIZE;
372                 mapcount = page_mapcount(page);
373                 if (mapcount >= 2) {
374                         if (pte_dirty(ptent) || PageDirty(page))
375                                 mss->shared_dirty += PAGE_SIZE;
376                         else
377                                 mss->shared_clean += PAGE_SIZE;
378                         mss->pss += (PAGE_SIZE << PSS_SHIFT) / mapcount;
379                 } else {
380                         if (pte_dirty(ptent) || PageDirty(page))
381                                 mss->private_dirty += PAGE_SIZE;
382                         else
383                                 mss->private_clean += PAGE_SIZE;
384                         mss->pss += (PAGE_SIZE << PSS_SHIFT);
385                 }
386         }
387         pte_unmap_unlock(pte - 1, ptl);
388         cond_resched();
389         return 0;
390 }
391
392 static int show_smap(struct seq_file *m, void *v)
393 {
394         struct proc_maps_private *priv = m->private;
395         struct task_struct *task = priv->task;
396         struct vm_area_struct *vma = v;
397         struct mem_size_stats mss;
398         struct mm_walk smaps_walk = {
399                 .pmd_entry = smaps_pte_range,
400                 .mm = vma->vm_mm,
401                 .private = &mss,
402         };
403
404         memset(&mss, 0, sizeof mss);
405         mss.vma = vma;
406         /* mmap_sem is held in m_start */
407         if (vma->vm_mm && !is_vm_hugetlb_page(vma))
408                 walk_page_range(vma->vm_start, vma->vm_end, &smaps_walk);
409
410         show_map_vma(m, vma);
411
412         seq_printf(m,
413                    "Size:           %8lu kB\n"
414                    "Rss:            %8lu kB\n"
415                    "Pss:            %8lu kB\n"
416                    "Shared_Clean:   %8lu kB\n"
417                    "Shared_Dirty:   %8lu kB\n"
418                    "Private_Clean:  %8lu kB\n"
419                    "Private_Dirty:  %8lu kB\n"
420                    "Referenced:     %8lu kB\n"
421                    "Anonymous:      %8lu kB\n"
422                    "Swap:           %8lu kB\n"
423                    "KernelPageSize: %8lu kB\n"
424                    "MMUPageSize:    %8lu kB\n"
425                    "Locked:         %8lu kB\n",
426                    (vma->vm_end - vma->vm_start) >> 10,
427                    mss.resident >> 10,
428                    (unsigned long)(mss.pss >> (10 + PSS_SHIFT)),
429                    mss.shared_clean  >> 10,
430                    mss.shared_dirty  >> 10,
431                    mss.private_clean >> 10,
432                    mss.private_dirty >> 10,
433                    mss.referenced >> 10,
434                    mss.anonymous >> 10,
435                    mss.swap >> 10,
436                    vma_kernel_pagesize(vma) >> 10,
437                    vma_mmu_pagesize(vma) >> 10,
438                    (vma->vm_flags & VM_LOCKED) ?
439                         (unsigned long)(mss.pss >> (10 + PSS_SHIFT)) : 0);
440
441         if (m->count < m->size)  /* vma is copied successfully */
442                 m->version = (vma != get_gate_vma(task)) ? vma->vm_start : 0;
443         return 0;
444 }
445
446 static const struct seq_operations proc_pid_smaps_op = {
447         .start  = m_start,
448         .next   = m_next,
449         .stop   = m_stop,
450         .show   = show_smap
451 };
452
453 static int smaps_open(struct inode *inode, struct file *file)
454 {
455         return do_maps_open(inode, file, &proc_pid_smaps_op);
456 }
457
458 const struct file_operations proc_smaps_operations = {
459         .open           = smaps_open,
460         .read           = seq_read,
461         .llseek         = seq_lseek,
462         .release        = seq_release_private,
463 };
464
465 static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
466                                 unsigned long end, struct mm_walk *walk)
467 {
468         struct vm_area_struct *vma = walk->private;
469         pte_t *pte, ptent;
470         spinlock_t *ptl;
471         struct page *page;
472
473         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
474         for (; addr != end; pte++, addr += PAGE_SIZE) {
475                 ptent = *pte;
476                 if (!pte_present(ptent))
477                         continue;
478
479                 page = vm_normal_page(vma, addr, ptent);
480                 if (!page)
481                         continue;
482
483                 /* Clear accessed and referenced bits. */
484                 ptep_test_and_clear_young(vma, addr, pte);
485                 ClearPageReferenced(page);
486         }
487         pte_unmap_unlock(pte - 1, ptl);
488         cond_resched();
489         return 0;
490 }
491
492 #define CLEAR_REFS_ALL 1
493 #define CLEAR_REFS_ANON 2
494 #define CLEAR_REFS_MAPPED 3
495
496 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
497                                 size_t count, loff_t *ppos)
498 {
499         struct task_struct *task;
500         char buffer[PROC_NUMBUF];
501         struct mm_struct *mm;
502         struct vm_area_struct *vma;
503         long type;
504
505         memset(buffer, 0, sizeof(buffer));
506         if (count > sizeof(buffer) - 1)
507                 count = sizeof(buffer) - 1;
508         if (copy_from_user(buffer, buf, count))
509                 return -EFAULT;
510         if (strict_strtol(strstrip(buffer), 10, &type))
511                 return -EINVAL;
512         if (type < CLEAR_REFS_ALL || type > CLEAR_REFS_MAPPED)
513                 return -EINVAL;
514         task = get_proc_task(file->f_path.dentry->d_inode);
515         if (!task)
516                 return -ESRCH;
517         mm = get_task_mm(task);
518         if (mm) {
519                 struct mm_walk clear_refs_walk = {
520                         .pmd_entry = clear_refs_pte_range,
521                         .mm = mm,
522                 };
523                 down_read(&mm->mmap_sem);
524                 for (vma = mm->mmap; vma; vma = vma->vm_next) {
525                         clear_refs_walk.private = vma;
526                         if (is_vm_hugetlb_page(vma))
527                                 continue;
528                         /*
529                          * Writing 1 to /proc/pid/clear_refs affects all pages.
530                          *
531                          * Writing 2 to /proc/pid/clear_refs only affects
532                          * Anonymous pages.
533                          *
534                          * Writing 3 to /proc/pid/clear_refs only affects file
535                          * mapped pages.
536                          */
537                         if (type == CLEAR_REFS_ANON && vma->vm_file)
538                                 continue;
539                         if (type == CLEAR_REFS_MAPPED && !vma->vm_file)
540                                 continue;
541                         walk_page_range(vma->vm_start, vma->vm_end,
542                                         &clear_refs_walk);
543                 }
544                 flush_tlb_mm(mm);
545                 up_read(&mm->mmap_sem);
546                 mmput(mm);
547         }
548         put_task_struct(task);
549
550         return count;
551 }
552
553 const struct file_operations proc_clear_refs_operations = {
554         .write          = clear_refs_write,
555         .llseek         = noop_llseek,
556 };
557
558 struct pagemapread {
559         int pos, len;
560         u64 *buffer;
561 };
562
563 #define PM_ENTRY_BYTES      sizeof(u64)
564 #define PM_STATUS_BITS      3
565 #define PM_STATUS_OFFSET    (64 - PM_STATUS_BITS)
566 #define PM_STATUS_MASK      (((1LL << PM_STATUS_BITS) - 1) << PM_STATUS_OFFSET)
567 #define PM_STATUS(nr)       (((nr) << PM_STATUS_OFFSET) & PM_STATUS_MASK)
568 #define PM_PSHIFT_BITS      6
569 #define PM_PSHIFT_OFFSET    (PM_STATUS_OFFSET - PM_PSHIFT_BITS)
570 #define PM_PSHIFT_MASK      (((1LL << PM_PSHIFT_BITS) - 1) << PM_PSHIFT_OFFSET)
571 #define PM_PSHIFT(x)        (((u64) (x) << PM_PSHIFT_OFFSET) & PM_PSHIFT_MASK)
572 #define PM_PFRAME_MASK      ((1LL << PM_PSHIFT_OFFSET) - 1)
573 #define PM_PFRAME(x)        ((x) & PM_PFRAME_MASK)
574
575 #define PM_PRESENT          PM_STATUS(4LL)
576 #define PM_SWAP             PM_STATUS(2LL)
577 #define PM_NOT_PRESENT      PM_PSHIFT(PAGE_SHIFT)
578 #define PM_END_OF_BUFFER    1
579
580 static int add_to_pagemap(unsigned long addr, u64 pfn,
581                           struct pagemapread *pm)
582 {
583         pm->buffer[pm->pos++] = pfn;
584         if (pm->pos >= pm->len)
585                 return PM_END_OF_BUFFER;
586         return 0;
587 }
588
589 static int pagemap_pte_hole(unsigned long start, unsigned long end,
590                                 struct mm_walk *walk)
591 {
592         struct pagemapread *pm = walk->private;
593         unsigned long addr;
594         int err = 0;
595         for (addr = start; addr < end; addr += PAGE_SIZE) {
596                 err = add_to_pagemap(addr, PM_NOT_PRESENT, pm);
597                 if (err)
598                         break;
599         }
600         return err;
601 }
602
603 static u64 swap_pte_to_pagemap_entry(pte_t pte)
604 {
605         swp_entry_t e = pte_to_swp_entry(pte);
606         return swp_type(e) | (swp_offset(e) << MAX_SWAPFILES_SHIFT);
607 }
608
609 static u64 pte_to_pagemap_entry(pte_t pte)
610 {
611         u64 pme = 0;
612         if (is_swap_pte(pte))
613                 pme = PM_PFRAME(swap_pte_to_pagemap_entry(pte))
614                         | PM_PSHIFT(PAGE_SHIFT) | PM_SWAP;
615         else if (pte_present(pte))
616                 pme = PM_PFRAME(pte_pfn(pte))
617                         | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
618         return pme;
619 }
620
621 static int pagemap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
622                              struct mm_walk *walk)
623 {
624         struct vm_area_struct *vma;
625         struct pagemapread *pm = walk->private;
626         pte_t *pte;
627         int err = 0;
628
629         /* find the first VMA at or above 'addr' */
630         vma = find_vma(walk->mm, addr);
631         for (; addr != end; addr += PAGE_SIZE) {
632                 u64 pfn = PM_NOT_PRESENT;
633
634                 /* check to see if we've left 'vma' behind
635                  * and need a new, higher one */
636                 if (vma && (addr >= vma->vm_end))
637                         vma = find_vma(walk->mm, addr);
638
639                 /* check that 'vma' actually covers this address,
640                  * and that it isn't a huge page vma */
641                 if (vma && (vma->vm_start <= addr) &&
642                     !is_vm_hugetlb_page(vma)) {
643                         pte = pte_offset_map(pmd, addr);
644                         pfn = pte_to_pagemap_entry(*pte);
645                         /* unmap before userspace copy */
646                         pte_unmap(pte);
647                 }
648                 err = add_to_pagemap(addr, pfn, pm);
649                 if (err)
650                         return err;
651         }
652
653         cond_resched();
654
655         return err;
656 }
657
658 #ifdef CONFIG_HUGETLB_PAGE
659 static u64 huge_pte_to_pagemap_entry(pte_t pte, int offset)
660 {
661         u64 pme = 0;
662         if (pte_present(pte))
663                 pme = PM_PFRAME(pte_pfn(pte) + offset)
664                         | PM_PSHIFT(PAGE_SHIFT) | PM_PRESENT;
665         return pme;
666 }
667
668 /* This function walks within one hugetlb entry in the single call */
669 static int pagemap_hugetlb_range(pte_t *pte, unsigned long hmask,
670                                  unsigned long addr, unsigned long end,
671                                  struct mm_walk *walk)
672 {
673         struct pagemapread *pm = walk->private;
674         int err = 0;
675         u64 pfn;
676
677         for (; addr != end; addr += PAGE_SIZE) {
678                 int offset = (addr & ~hmask) >> PAGE_SHIFT;
679                 pfn = huge_pte_to_pagemap_entry(*pte, offset);
680                 err = add_to_pagemap(addr, pfn, pm);
681                 if (err)
682                         return err;
683         }
684
685         cond_resched();
686
687         return err;
688 }
689 #endif /* HUGETLB_PAGE */
690
691 /*
692  * /proc/pid/pagemap - an array mapping virtual pages to pfns
693  *
694  * For each page in the address space, this file contains one 64-bit entry
695  * consisting of the following:
696  *
697  * Bits 0-55  page frame number (PFN) if present
698  * Bits 0-4   swap type if swapped
699  * Bits 5-55  swap offset if swapped
700  * Bits 55-60 page shift (page size = 1<<page shift)
701  * Bit  61    reserved for future use
702  * Bit  62    page swapped
703  * Bit  63    page present
704  *
705  * If the page is not present but in swap, then the PFN contains an
706  * encoding of the swap file number and the page's offset into the
707  * swap. Unmapped pages return a null PFN. This allows determining
708  * precisely which pages are mapped (or in swap) and comparing mapped
709  * pages between processes.
710  *
711  * Efficient users of this interface will use /proc/pid/maps to
712  * determine which areas of memory are actually mapped and llseek to
713  * skip over unmapped regions.
714  */
715 #define PAGEMAP_WALK_SIZE       (PMD_SIZE)
716 #define PAGEMAP_WALK_MASK       (PMD_MASK)
717 static ssize_t pagemap_read(struct file *file, char __user *buf,
718                             size_t count, loff_t *ppos)
719 {
720         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
721         struct mm_struct *mm;
722         struct pagemapread pm;
723         int ret = -ESRCH;
724         struct mm_walk pagemap_walk = {};
725         unsigned long src;
726         unsigned long svpfn;
727         unsigned long start_vaddr;
728         unsigned long end_vaddr;
729         int copied = 0;
730
731         if (!task)
732                 goto out;
733
734         mm = mm_for_maps(task);
735         ret = PTR_ERR(mm);
736         if (!mm || IS_ERR(mm))
737                 goto out_task;
738
739         ret = -EINVAL;
740         /* file position must be aligned */
741         if ((*ppos % PM_ENTRY_BYTES) || (count % PM_ENTRY_BYTES))
742                 goto out_task;
743
744         ret = 0;
745
746         if (!count)
747                 goto out_task;
748
749         pm.len = PM_ENTRY_BYTES * (PAGEMAP_WALK_SIZE >> PAGE_SHIFT);
750         pm.buffer = kmalloc(pm.len, GFP_TEMPORARY);
751         ret = -ENOMEM;
752         if (!pm.buffer)
753                 goto out_mm;
754
755         pagemap_walk.pmd_entry = pagemap_pte_range;
756         pagemap_walk.pte_hole = pagemap_pte_hole;
757 #ifdef CONFIG_HUGETLB_PAGE
758         pagemap_walk.hugetlb_entry = pagemap_hugetlb_range;
759 #endif
760         pagemap_walk.mm = mm;
761         pagemap_walk.private = &pm;
762
763         src = *ppos;
764         svpfn = src / PM_ENTRY_BYTES;
765         start_vaddr = svpfn << PAGE_SHIFT;
766         end_vaddr = TASK_SIZE_OF(task);
767
768         /* watch out for wraparound */
769         if (svpfn > TASK_SIZE_OF(task) >> PAGE_SHIFT)
770                 start_vaddr = end_vaddr;
771
772         /*
773          * The odds are that this will stop walking way
774          * before end_vaddr, because the length of the
775          * user buffer is tracked in "pm", and the walk
776          * will stop when we hit the end of the buffer.
777          */
778         ret = 0;
779         while (count && (start_vaddr < end_vaddr)) {
780                 int len;
781                 unsigned long end;
782
783                 pm.pos = 0;
784                 end = (start_vaddr + PAGEMAP_WALK_SIZE) & PAGEMAP_WALK_MASK;
785                 /* overflow ? */
786                 if (end < start_vaddr || end > end_vaddr)
787                         end = end_vaddr;
788                 down_read(&mm->mmap_sem);
789                 ret = walk_page_range(start_vaddr, end, &pagemap_walk);
790                 up_read(&mm->mmap_sem);
791                 start_vaddr = end;
792
793                 len = min(count, PM_ENTRY_BYTES * pm.pos);
794                 if (copy_to_user(buf, pm.buffer, len)) {
795                         ret = -EFAULT;
796                         goto out_free;
797                 }
798                 copied += len;
799                 buf += len;
800                 count -= len;
801         }
802         *ppos += copied;
803         if (!ret || ret == PM_END_OF_BUFFER)
804                 ret = copied;
805
806 out_free:
807         kfree(pm.buffer);
808 out_mm:
809         mmput(mm);
810 out_task:
811         put_task_struct(task);
812 out:
813         return ret;
814 }
815
816 const struct file_operations proc_pagemap_operations = {
817         .llseek         = mem_lseek, /* borrow this */
818         .read           = pagemap_read,
819 };
820 #endif /* CONFIG_PROC_PAGE_MONITOR */
821
822 #ifdef CONFIG_NUMA
823 extern int show_numa_map(struct seq_file *m, void *v);
824
825 static const struct seq_operations proc_pid_numa_maps_op = {
826         .start  = m_start,
827         .next   = m_next,
828         .stop   = m_stop,
829         .show   = show_numa_map,
830 };
831
832 static int numa_maps_open(struct inode *inode, struct file *file)
833 {
834         return do_maps_open(inode, file, &proc_pid_numa_maps_op);
835 }
836
837 const struct file_operations proc_numa_maps_operations = {
838         .open           = numa_maps_open,
839         .read           = seq_read,
840         .llseek         = seq_lseek,
841         .release        = seq_release_private,
842 };
843 #endif