diff options
author | Kirill A. Shutemov <kirill.shutemov@linux.intel.com> | 2014-06-04 19:08:10 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-06-04 19:54:04 -0400 |
commit | 4bbd4c776a63a063546552de42f6a535395f6d9e (patch) | |
tree | 2a722c3bde3f3dabf85030b391b44c2cb3972df2 /mm/memory.c | |
parent | f4527c90868d8fa175c68ccf216cf9b67a7d8a1a (diff) |
mm: move get_user_pages()-related code to separate file
mm/memory.c is overloaded: over 4k lines. get_user_pages() code is
pretty much self-contained let's move it to separate file.
No other changes made.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/memory.c')
-rw-r--r-- | mm/memory.c | 641 |
1 files changed, 0 insertions, 641 deletions
diff --git a/mm/memory.c b/mm/memory.c index 0897830011f3..7049d394fa07 100644 --- a/mm/memory.c +++ b/mm/memory.c | |||
@@ -698,11 +698,6 @@ static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr, | |||
698 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); | 698 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
699 | } | 699 | } |
700 | 700 | ||
701 | static inline bool is_cow_mapping(vm_flags_t flags) | ||
702 | { | ||
703 | return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | ||
704 | } | ||
705 | |||
706 | /* | 701 | /* |
707 | * vm_normal_page -- This function gets the "struct page" associated with a pte. | 702 | * vm_normal_page -- This function gets the "struct page" associated with a pte. |
708 | * | 703 | * |
@@ -1458,642 +1453,6 @@ int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, | |||
1458 | } | 1453 | } |
1459 | EXPORT_SYMBOL_GPL(zap_vma_ptes); | 1454 | EXPORT_SYMBOL_GPL(zap_vma_ptes); |
1460 | 1455 | ||
1461 | /** | ||
1462 | * follow_page_mask - look up a page descriptor from a user-virtual address | ||
1463 | * @vma: vm_area_struct mapping @address | ||
1464 | * @address: virtual address to look up | ||
1465 | * @flags: flags modifying lookup behaviour | ||
1466 | * @page_mask: on output, *page_mask is set according to the size of the page | ||
1467 | * | ||
1468 | * @flags can have FOLL_ flags set, defined in <linux/mm.h> | ||
1469 | * | ||
1470 | * Returns the mapped (struct page *), %NULL if no mapping exists, or | ||
1471 | * an error pointer if there is a mapping to something not represented | ||
1472 | * by a page descriptor (see also vm_normal_page()). | ||
1473 | */ | ||
1474 | struct page *follow_page_mask(struct vm_area_struct *vma, | ||
1475 | unsigned long address, unsigned int flags, | ||
1476 | unsigned int *page_mask) | ||
1477 | { | ||
1478 | pgd_t *pgd; | ||
1479 | pud_t *pud; | ||
1480 | pmd_t *pmd; | ||
1481 | pte_t *ptep, pte; | ||
1482 | spinlock_t *ptl; | ||
1483 | struct page *page; | ||
1484 | struct mm_struct *mm = vma->vm_mm; | ||
1485 | |||
1486 | *page_mask = 0; | ||
1487 | |||
1488 | page = follow_huge_addr(mm, address, flags & FOLL_WRITE); | ||
1489 | if (!IS_ERR(page)) { | ||
1490 | BUG_ON(flags & FOLL_GET); | ||
1491 | goto out; | ||
1492 | } | ||
1493 | |||
1494 | page = NULL; | ||
1495 | pgd = pgd_offset(mm, address); | ||
1496 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) | ||
1497 | goto no_page_table; | ||
1498 | |||
1499 | pud = pud_offset(pgd, address); | ||
1500 | if (pud_none(*pud)) | ||
1501 | goto no_page_table; | ||
1502 | if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) { | ||
1503 | if (flags & FOLL_GET) | ||
1504 | goto out; | ||
1505 | page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE); | ||
1506 | goto out; | ||
1507 | } | ||
1508 | if (unlikely(pud_bad(*pud))) | ||
1509 | goto no_page_table; | ||
1510 | |||
1511 | pmd = pmd_offset(pud, address); | ||
1512 | if (pmd_none(*pmd)) | ||
1513 | goto no_page_table; | ||
1514 | if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) { | ||
1515 | page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE); | ||
1516 | if (flags & FOLL_GET) { | ||
1517 | /* | ||
1518 | * Refcount on tail pages are not well-defined and | ||
1519 | * shouldn't be taken. The caller should handle a NULL | ||
1520 | * return when trying to follow tail pages. | ||
1521 | */ | ||
1522 | if (PageHead(page)) | ||
1523 | get_page(page); | ||
1524 | else { | ||
1525 | page = NULL; | ||
1526 | goto out; | ||
1527 | } | ||
1528 | } | ||
1529 | goto out; | ||
1530 | } | ||
1531 | if ((flags & FOLL_NUMA) && pmd_numa(*pmd)) | ||
1532 | goto no_page_table; | ||
1533 | if (pmd_trans_huge(*pmd)) { | ||
1534 | if (flags & FOLL_SPLIT) { | ||
1535 | split_huge_page_pmd(vma, address, pmd); | ||
1536 | goto split_fallthrough; | ||
1537 | } | ||
1538 | ptl = pmd_lock(mm, pmd); | ||
1539 | if (likely(pmd_trans_huge(*pmd))) { | ||
1540 | if (unlikely(pmd_trans_splitting(*pmd))) { | ||
1541 | spin_unlock(ptl); | ||
1542 | wait_split_huge_page(vma->anon_vma, pmd); | ||
1543 | } else { | ||
1544 | page = follow_trans_huge_pmd(vma, address, | ||
1545 | pmd, flags); | ||
1546 | spin_unlock(ptl); | ||
1547 | *page_mask = HPAGE_PMD_NR - 1; | ||
1548 | goto out; | ||
1549 | } | ||
1550 | } else | ||
1551 | spin_unlock(ptl); | ||
1552 | /* fall through */ | ||
1553 | } | ||
1554 | split_fallthrough: | ||
1555 | if (unlikely(pmd_bad(*pmd))) | ||
1556 | goto no_page_table; | ||
1557 | |||
1558 | ptep = pte_offset_map_lock(mm, pmd, address, &ptl); | ||
1559 | |||
1560 | pte = *ptep; | ||
1561 | if (!pte_present(pte)) { | ||
1562 | swp_entry_t entry; | ||
1563 | /* | ||
1564 | * KSM's break_ksm() relies upon recognizing a ksm page | ||
1565 | * even while it is being migrated, so for that case we | ||
1566 | * need migration_entry_wait(). | ||
1567 | */ | ||
1568 | if (likely(!(flags & FOLL_MIGRATION))) | ||
1569 | goto no_page; | ||
1570 | if (pte_none(pte) || pte_file(pte)) | ||
1571 | goto no_page; | ||
1572 | entry = pte_to_swp_entry(pte); | ||
1573 | if (!is_migration_entry(entry)) | ||
1574 | goto no_page; | ||
1575 | pte_unmap_unlock(ptep, ptl); | ||
1576 | migration_entry_wait(mm, pmd, address); | ||
1577 | goto split_fallthrough; | ||
1578 | } | ||
1579 | if ((flags & FOLL_NUMA) && pte_numa(pte)) | ||
1580 | goto no_page; | ||
1581 | if ((flags & FOLL_WRITE) && !pte_write(pte)) | ||
1582 | goto unlock; | ||
1583 | |||
1584 | page = vm_normal_page(vma, address, pte); | ||
1585 | if (unlikely(!page)) { | ||
1586 | if ((flags & FOLL_DUMP) || | ||
1587 | !is_zero_pfn(pte_pfn(pte))) | ||
1588 | goto bad_page; | ||
1589 | page = pte_page(pte); | ||
1590 | } | ||
1591 | |||
1592 | if (flags & FOLL_GET) | ||
1593 | get_page_foll(page); | ||
1594 | if (flags & FOLL_TOUCH) { | ||
1595 | if ((flags & FOLL_WRITE) && | ||
1596 | !pte_dirty(pte) && !PageDirty(page)) | ||
1597 | set_page_dirty(page); | ||
1598 | /* | ||
1599 | * pte_mkyoung() would be more correct here, but atomic care | ||
1600 | * is needed to avoid losing the dirty bit: it is easier to use | ||
1601 | * mark_page_accessed(). | ||
1602 | */ | ||
1603 | mark_page_accessed(page); | ||
1604 | } | ||
1605 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { | ||
1606 | /* | ||
1607 | * The preliminary mapping check is mainly to avoid the | ||
1608 | * pointless overhead of lock_page on the ZERO_PAGE | ||
1609 | * which might bounce very badly if there is contention. | ||
1610 | * | ||
1611 | * If the page is already locked, we don't need to | ||
1612 | * handle it now - vmscan will handle it later if and | ||
1613 | * when it attempts to reclaim the page. | ||
1614 | */ | ||
1615 | if (page->mapping && trylock_page(page)) { | ||
1616 | lru_add_drain(); /* push cached pages to LRU */ | ||
1617 | /* | ||
1618 | * Because we lock page here, and migration is | ||
1619 | * blocked by the pte's page reference, and we | ||
1620 | * know the page is still mapped, we don't even | ||
1621 | * need to check for file-cache page truncation. | ||
1622 | */ | ||
1623 | mlock_vma_page(page); | ||
1624 | unlock_page(page); | ||
1625 | } | ||
1626 | } | ||
1627 | unlock: | ||
1628 | pte_unmap_unlock(ptep, ptl); | ||
1629 | out: | ||
1630 | return page; | ||
1631 | |||
1632 | bad_page: | ||
1633 | pte_unmap_unlock(ptep, ptl); | ||
1634 | return ERR_PTR(-EFAULT); | ||
1635 | |||
1636 | no_page: | ||
1637 | pte_unmap_unlock(ptep, ptl); | ||
1638 | if (!pte_none(pte)) | ||
1639 | return page; | ||
1640 | |||
1641 | no_page_table: | ||
1642 | /* | ||
1643 | * When core dumping an enormous anonymous area that nobody | ||
1644 | * has touched so far, we don't want to allocate unnecessary pages or | ||
1645 | * page tables. Return error instead of NULL to skip handle_mm_fault, | ||
1646 | * then get_dump_page() will return NULL to leave a hole in the dump. | ||
1647 | * But we can only make this optimization where a hole would surely | ||
1648 | * be zero-filled if handle_mm_fault() actually did handle it. | ||
1649 | */ | ||
1650 | if ((flags & FOLL_DUMP) && | ||
1651 | (!vma->vm_ops || !vma->vm_ops->fault)) | ||
1652 | return ERR_PTR(-EFAULT); | ||
1653 | return page; | ||
1654 | } | ||
1655 | |||
1656 | static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr) | ||
1657 | { | ||
1658 | return stack_guard_page_start(vma, addr) || | ||
1659 | stack_guard_page_end(vma, addr+PAGE_SIZE); | ||
1660 | } | ||
1661 | |||
1662 | /** | ||
1663 | * __get_user_pages() - pin user pages in memory | ||
1664 | * @tsk: task_struct of target task | ||
1665 | * @mm: mm_struct of target mm | ||
1666 | * @start: starting user address | ||
1667 | * @nr_pages: number of pages from start to pin | ||
1668 | * @gup_flags: flags modifying pin behaviour | ||
1669 | * @pages: array that receives pointers to the pages pinned. | ||
1670 | * Should be at least nr_pages long. Or NULL, if caller | ||
1671 | * only intends to ensure the pages are faulted in. | ||
1672 | * @vmas: array of pointers to vmas corresponding to each page. | ||
1673 | * Or NULL if the caller does not require them. | ||
1674 | * @nonblocking: whether waiting for disk IO or mmap_sem contention | ||
1675 | * | ||
1676 | * Returns number of pages pinned. This may be fewer than the number | ||
1677 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | ||
1678 | * were pinned, returns -errno. Each page returned must be released | ||
1679 | * with a put_page() call when it is finished with. vmas will only | ||
1680 | * remain valid while mmap_sem is held. | ||
1681 | * | ||
1682 | * Must be called with mmap_sem held for read or write. | ||
1683 | * | ||
1684 | * __get_user_pages walks a process's page tables and takes a reference to | ||
1685 | * each struct page that each user address corresponds to at a given | ||
1686 | * instant. That is, it takes the page that would be accessed if a user | ||
1687 | * thread accesses the given user virtual address at that instant. | ||
1688 | * | ||
1689 | * This does not guarantee that the page exists in the user mappings when | ||
1690 | * __get_user_pages returns, and there may even be a completely different | ||
1691 | * page there in some cases (eg. if mmapped pagecache has been invalidated | ||
1692 | * and subsequently re faulted). However it does guarantee that the page | ||
1693 | * won't be freed completely. And mostly callers simply care that the page | ||
1694 | * contains data that was valid *at some point in time*. Typically, an IO | ||
1695 | * or similar operation cannot guarantee anything stronger anyway because | ||
1696 | * locks can't be held over the syscall boundary. | ||
1697 | * | ||
1698 | * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If | ||
1699 | * the page is written to, set_page_dirty (or set_page_dirty_lock, as | ||
1700 | * appropriate) must be called after the page is finished with, and | ||
1701 | * before put_page is called. | ||
1702 | * | ||
1703 | * If @nonblocking != NULL, __get_user_pages will not wait for disk IO | ||
1704 | * or mmap_sem contention, and if waiting is needed to pin all pages, | ||
1705 | * *@nonblocking will be set to 0. | ||
1706 | * | ||
1707 | * In most cases, get_user_pages or get_user_pages_fast should be used | ||
1708 | * instead of __get_user_pages. __get_user_pages should be used only if | ||
1709 | * you need some special @gup_flags. | ||
1710 | */ | ||
1711 | long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | ||
1712 | unsigned long start, unsigned long nr_pages, | ||
1713 | unsigned int gup_flags, struct page **pages, | ||
1714 | struct vm_area_struct **vmas, int *nonblocking) | ||
1715 | { | ||
1716 | long i; | ||
1717 | unsigned long vm_flags; | ||
1718 | unsigned int page_mask; | ||
1719 | |||
1720 | if (!nr_pages) | ||
1721 | return 0; | ||
1722 | |||
1723 | VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET)); | ||
1724 | |||
1725 | /* | ||
1726 | * If FOLL_FORCE is set then do not force a full fault as the hinting | ||
1727 | * fault information is unrelated to the reference behaviour of a task | ||
1728 | * using the address space | ||
1729 | */ | ||
1730 | if (!(gup_flags & FOLL_FORCE)) | ||
1731 | gup_flags |= FOLL_NUMA; | ||
1732 | |||
1733 | i = 0; | ||
1734 | |||
1735 | do { | ||
1736 | struct vm_area_struct *vma; | ||
1737 | |||
1738 | vma = find_extend_vma(mm, start); | ||
1739 | if (!vma && in_gate_area(mm, start)) { | ||
1740 | unsigned long pg = start & PAGE_MASK; | ||
1741 | pgd_t *pgd; | ||
1742 | pud_t *pud; | ||
1743 | pmd_t *pmd; | ||
1744 | pte_t *pte; | ||
1745 | |||
1746 | /* user gate pages are read-only */ | ||
1747 | if (gup_flags & FOLL_WRITE) | ||
1748 | goto efault; | ||
1749 | if (pg > TASK_SIZE) | ||
1750 | pgd = pgd_offset_k(pg); | ||
1751 | else | ||
1752 | pgd = pgd_offset_gate(mm, pg); | ||
1753 | BUG_ON(pgd_none(*pgd)); | ||
1754 | pud = pud_offset(pgd, pg); | ||
1755 | BUG_ON(pud_none(*pud)); | ||
1756 | pmd = pmd_offset(pud, pg); | ||
1757 | if (pmd_none(*pmd)) | ||
1758 | goto efault; | ||
1759 | VM_BUG_ON(pmd_trans_huge(*pmd)); | ||
1760 | pte = pte_offset_map(pmd, pg); | ||
1761 | if (pte_none(*pte)) { | ||
1762 | pte_unmap(pte); | ||
1763 | goto efault; | ||
1764 | } | ||
1765 | vma = get_gate_vma(mm); | ||
1766 | if (pages) { | ||
1767 | struct page *page; | ||
1768 | |||
1769 | page = vm_normal_page(vma, start, *pte); | ||
1770 | if (!page) { | ||
1771 | if (!(gup_flags & FOLL_DUMP) && | ||
1772 | is_zero_pfn(pte_pfn(*pte))) | ||
1773 | page = pte_page(*pte); | ||
1774 | else { | ||
1775 | pte_unmap(pte); | ||
1776 | goto efault; | ||
1777 | } | ||
1778 | } | ||
1779 | pages[i] = page; | ||
1780 | get_page(page); | ||
1781 | } | ||
1782 | pte_unmap(pte); | ||
1783 | page_mask = 0; | ||
1784 | goto next_page; | ||
1785 | } | ||
1786 | |||
1787 | if (!vma) | ||
1788 | goto efault; | ||
1789 | vm_flags = vma->vm_flags; | ||
1790 | if (vm_flags & (VM_IO | VM_PFNMAP)) | ||
1791 | goto efault; | ||
1792 | |||
1793 | if (gup_flags & FOLL_WRITE) { | ||
1794 | if (!(vm_flags & VM_WRITE)) { | ||
1795 | if (!(gup_flags & FOLL_FORCE)) | ||
1796 | goto efault; | ||
1797 | /* | ||
1798 | * We used to let the write,force case do COW | ||
1799 | * in a VM_MAYWRITE VM_SHARED !VM_WRITE vma, so | ||
1800 | * ptrace could set a breakpoint in a read-only | ||
1801 | * mapping of an executable, without corrupting | ||
1802 | * the file (yet only when that file had been | ||
1803 | * opened for writing!). Anon pages in shared | ||
1804 | * mappings are surprising: now just reject it. | ||
1805 | */ | ||
1806 | if (!is_cow_mapping(vm_flags)) { | ||
1807 | WARN_ON_ONCE(vm_flags & VM_MAYWRITE); | ||
1808 | goto efault; | ||
1809 | } | ||
1810 | } | ||
1811 | } else { | ||
1812 | if (!(vm_flags & VM_READ)) { | ||
1813 | if (!(gup_flags & FOLL_FORCE)) | ||
1814 | goto efault; | ||
1815 | /* | ||
1816 | * Is there actually any vma we can reach here | ||
1817 | * which does not have VM_MAYREAD set? | ||
1818 | */ | ||
1819 | if (!(vm_flags & VM_MAYREAD)) | ||
1820 | goto efault; | ||
1821 | } | ||
1822 | } | ||
1823 | |||
1824 | if (is_vm_hugetlb_page(vma)) { | ||
1825 | i = follow_hugetlb_page(mm, vma, pages, vmas, | ||
1826 | &start, &nr_pages, i, gup_flags); | ||
1827 | continue; | ||
1828 | } | ||
1829 | |||
1830 | do { | ||
1831 | struct page *page; | ||
1832 | unsigned int foll_flags = gup_flags; | ||
1833 | unsigned int page_increm; | ||
1834 | |||
1835 | /* | ||
1836 | * If we have a pending SIGKILL, don't keep faulting | ||
1837 | * pages and potentially allocating memory. | ||
1838 | */ | ||
1839 | if (unlikely(fatal_signal_pending(current))) | ||
1840 | return i ? i : -ERESTARTSYS; | ||
1841 | |||
1842 | cond_resched(); | ||
1843 | while (!(page = follow_page_mask(vma, start, | ||
1844 | foll_flags, &page_mask))) { | ||
1845 | int ret; | ||
1846 | unsigned int fault_flags = 0; | ||
1847 | |||
1848 | /* For mlock, just skip the stack guard page. */ | ||
1849 | if (foll_flags & FOLL_MLOCK) { | ||
1850 | if (stack_guard_page(vma, start)) | ||
1851 | goto next_page; | ||
1852 | } | ||
1853 | if (foll_flags & FOLL_WRITE) | ||
1854 | fault_flags |= FAULT_FLAG_WRITE; | ||
1855 | if (nonblocking) | ||
1856 | fault_flags |= FAULT_FLAG_ALLOW_RETRY; | ||
1857 | if (foll_flags & FOLL_NOWAIT) | ||
1858 | fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT); | ||
1859 | |||
1860 | ret = handle_mm_fault(mm, vma, start, | ||
1861 | fault_flags); | ||
1862 | |||
1863 | if (ret & VM_FAULT_ERROR) { | ||
1864 | if (ret & VM_FAULT_OOM) | ||
1865 | return i ? i : -ENOMEM; | ||
1866 | if (ret & (VM_FAULT_HWPOISON | | ||
1867 | VM_FAULT_HWPOISON_LARGE)) { | ||
1868 | if (i) | ||
1869 | return i; | ||
1870 | else if (gup_flags & FOLL_HWPOISON) | ||
1871 | return -EHWPOISON; | ||
1872 | else | ||
1873 | return -EFAULT; | ||
1874 | } | ||
1875 | if (ret & VM_FAULT_SIGBUS) | ||
1876 | goto efault; | ||
1877 | BUG(); | ||
1878 | } | ||
1879 | |||
1880 | if (tsk) { | ||
1881 | if (ret & VM_FAULT_MAJOR) | ||
1882 | tsk->maj_flt++; | ||
1883 | else | ||
1884 | tsk->min_flt++; | ||
1885 | } | ||
1886 | |||
1887 | if (ret & VM_FAULT_RETRY) { | ||
1888 | if (nonblocking) | ||
1889 | *nonblocking = 0; | ||
1890 | return i; | ||
1891 | } | ||
1892 | |||
1893 | /* | ||
1894 | * The VM_FAULT_WRITE bit tells us that | ||
1895 | * do_wp_page has broken COW when necessary, | ||
1896 | * even if maybe_mkwrite decided not to set | ||
1897 | * pte_write. We can thus safely do subsequent | ||
1898 | * page lookups as if they were reads. But only | ||
1899 | * do so when looping for pte_write is futile: | ||
1900 | * in some cases userspace may also be wanting | ||
1901 | * to write to the gotten user page, which a | ||
1902 | * read fault here might prevent (a readonly | ||
1903 | * page might get reCOWed by userspace write). | ||
1904 | */ | ||
1905 | if ((ret & VM_FAULT_WRITE) && | ||
1906 | !(vma->vm_flags & VM_WRITE)) | ||
1907 | foll_flags &= ~FOLL_WRITE; | ||
1908 | |||
1909 | cond_resched(); | ||
1910 | } | ||
1911 | if (IS_ERR(page)) | ||
1912 | return i ? i : PTR_ERR(page); | ||
1913 | if (pages) { | ||
1914 | pages[i] = page; | ||
1915 | |||
1916 | flush_anon_page(vma, page, start); | ||
1917 | flush_dcache_page(page); | ||
1918 | page_mask = 0; | ||
1919 | } | ||
1920 | next_page: | ||
1921 | if (vmas) { | ||
1922 | vmas[i] = vma; | ||
1923 | page_mask = 0; | ||
1924 | } | ||
1925 | page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask); | ||
1926 | if (page_increm > nr_pages) | ||
1927 | page_increm = nr_pages; | ||
1928 | i += page_increm; | ||
1929 | start += page_increm * PAGE_SIZE; | ||
1930 | nr_pages -= page_increm; | ||
1931 | } while (nr_pages && start < vma->vm_end); | ||
1932 | } while (nr_pages); | ||
1933 | return i; | ||
1934 | efault: | ||
1935 | return i ? : -EFAULT; | ||
1936 | } | ||
1937 | EXPORT_SYMBOL(__get_user_pages); | ||
1938 | |||
1939 | /* | ||
1940 | * fixup_user_fault() - manually resolve a user page fault | ||
1941 | * @tsk: the task_struct to use for page fault accounting, or | ||
1942 | * NULL if faults are not to be recorded. | ||
1943 | * @mm: mm_struct of target mm | ||
1944 | * @address: user address | ||
1945 | * @fault_flags:flags to pass down to handle_mm_fault() | ||
1946 | * | ||
1947 | * This is meant to be called in the specific scenario where for locking reasons | ||
1948 | * we try to access user memory in atomic context (within a pagefault_disable() | ||
1949 | * section), this returns -EFAULT, and we want to resolve the user fault before | ||
1950 | * trying again. | ||
1951 | * | ||
1952 | * Typically this is meant to be used by the futex code. | ||
1953 | * | ||
1954 | * The main difference with get_user_pages() is that this function will | ||
1955 | * unconditionally call handle_mm_fault() which will in turn perform all the | ||
1956 | * necessary SW fixup of the dirty and young bits in the PTE, while | ||
1957 | * handle_mm_fault() only guarantees to update these in the struct page. | ||
1958 | * | ||
1959 | * This is important for some architectures where those bits also gate the | ||
1960 | * access permission to the page because they are maintained in software. On | ||
1961 | * such architectures, gup() will not be enough to make a subsequent access | ||
1962 | * succeed. | ||
1963 | * | ||
1964 | * This should be called with the mm_sem held for read. | ||
1965 | */ | ||
1966 | int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm, | ||
1967 | unsigned long address, unsigned int fault_flags) | ||
1968 | { | ||
1969 | struct vm_area_struct *vma; | ||
1970 | vm_flags_t vm_flags; | ||
1971 | int ret; | ||
1972 | |||
1973 | vma = find_extend_vma(mm, address); | ||
1974 | if (!vma || address < vma->vm_start) | ||
1975 | return -EFAULT; | ||
1976 | |||
1977 | vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ; | ||
1978 | if (!(vm_flags & vma->vm_flags)) | ||
1979 | return -EFAULT; | ||
1980 | |||
1981 | ret = handle_mm_fault(mm, vma, address, fault_flags); | ||
1982 | if (ret & VM_FAULT_ERROR) { | ||
1983 | if (ret & VM_FAULT_OOM) | ||
1984 | return -ENOMEM; | ||
1985 | if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) | ||
1986 | return -EHWPOISON; | ||
1987 | if (ret & VM_FAULT_SIGBUS) | ||
1988 | return -EFAULT; | ||
1989 | BUG(); | ||
1990 | } | ||
1991 | if (tsk) { | ||
1992 | if (ret & VM_FAULT_MAJOR) | ||
1993 | tsk->maj_flt++; | ||
1994 | else | ||
1995 | tsk->min_flt++; | ||
1996 | } | ||
1997 | return 0; | ||
1998 | } | ||
1999 | |||
2000 | /* | ||
2001 | * get_user_pages() - pin user pages in memory | ||
2002 | * @tsk: the task_struct to use for page fault accounting, or | ||
2003 | * NULL if faults are not to be recorded. | ||
2004 | * @mm: mm_struct of target mm | ||
2005 | * @start: starting user address | ||
2006 | * @nr_pages: number of pages from start to pin | ||
2007 | * @write: whether pages will be written to by the caller | ||
2008 | * @force: whether to force access even when user mapping is currently | ||
2009 | * protected (but never forces write access to shared mapping). | ||
2010 | * @pages: array that receives pointers to the pages pinned. | ||
2011 | * Should be at least nr_pages long. Or NULL, if caller | ||
2012 | * only intends to ensure the pages are faulted in. | ||
2013 | * @vmas: array of pointers to vmas corresponding to each page. | ||
2014 | * Or NULL if the caller does not require them. | ||
2015 | * | ||
2016 | * Returns number of pages pinned. This may be fewer than the number | ||
2017 | * requested. If nr_pages is 0 or negative, returns 0. If no pages | ||
2018 | * were pinned, returns -errno. Each page returned must be released | ||
2019 | * with a put_page() call when it is finished with. vmas will only | ||
2020 | * remain valid while mmap_sem is held. | ||
2021 | * | ||
2022 | * Must be called with mmap_sem held for read or write. | ||
2023 | * | ||
2024 | * get_user_pages walks a process's page tables and takes a reference to | ||
2025 | * each struct page that each user address corresponds to at a given | ||
2026 | * instant. That is, it takes the page that would be accessed if a user | ||
2027 | * thread accesses the given user virtual address at that instant. | ||
2028 | * | ||
2029 | * This does not guarantee that the page exists in the user mappings when | ||
2030 | * get_user_pages returns, and there may even be a completely different | ||
2031 | * page there in some cases (eg. if mmapped pagecache has been invalidated | ||
2032 | * and subsequently re faulted). However it does guarantee that the page | ||
2033 | * won't be freed completely. And mostly callers simply care that the page | ||
2034 | * contains data that was valid *at some point in time*. Typically, an IO | ||
2035 | * or similar operation cannot guarantee anything stronger anyway because | ||
2036 | * locks can't be held over the syscall boundary. | ||
2037 | * | ||
2038 | * If write=0, the page must not be written to. If the page is written to, | ||
2039 | * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called | ||
2040 | * after the page is finished with, and before put_page is called. | ||
2041 | * | ||
2042 | * get_user_pages is typically used for fewer-copy IO operations, to get a | ||
2043 | * handle on the memory by some means other than accesses via the user virtual | ||
2044 | * addresses. The pages may be submitted for DMA to devices or accessed via | ||
2045 | * their kernel linear mapping (via the kmap APIs). Care should be taken to | ||
2046 | * use the correct cache flushing APIs. | ||
2047 | * | ||
2048 | * See also get_user_pages_fast, for performance critical applications. | ||
2049 | */ | ||
2050 | long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, | ||
2051 | unsigned long start, unsigned long nr_pages, int write, | ||
2052 | int force, struct page **pages, struct vm_area_struct **vmas) | ||
2053 | { | ||
2054 | int flags = FOLL_TOUCH; | ||
2055 | |||
2056 | if (pages) | ||
2057 | flags |= FOLL_GET; | ||
2058 | if (write) | ||
2059 | flags |= FOLL_WRITE; | ||
2060 | if (force) | ||
2061 | flags |= FOLL_FORCE; | ||
2062 | |||
2063 | return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas, | ||
2064 | NULL); | ||
2065 | } | ||
2066 | EXPORT_SYMBOL(get_user_pages); | ||
2067 | |||
2068 | /** | ||
2069 | * get_dump_page() - pin user page in memory while writing it to core dump | ||
2070 | * @addr: user address | ||
2071 | * | ||
2072 | * Returns struct page pointer of user page pinned for dump, | ||
2073 | * to be freed afterwards by page_cache_release() or put_page(). | ||
2074 | * | ||
2075 | * Returns NULL on any kind of failure - a hole must then be inserted into | ||
2076 | * the corefile, to preserve alignment with its headers; and also returns | ||
2077 | * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - | ||
2078 | * allowing a hole to be left in the corefile to save diskspace. | ||
2079 | * | ||
2080 | * Called without mmap_sem, but after all other threads have been killed. | ||
2081 | */ | ||
2082 | #ifdef CONFIG_ELF_CORE | ||
2083 | struct page *get_dump_page(unsigned long addr) | ||
2084 | { | ||
2085 | struct vm_area_struct *vma; | ||
2086 | struct page *page; | ||
2087 | |||
2088 | if (__get_user_pages(current, current->mm, addr, 1, | ||
2089 | FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma, | ||
2090 | NULL) < 1) | ||
2091 | return NULL; | ||
2092 | flush_cache_page(vma, addr, page_to_pfn(page)); | ||
2093 | return page; | ||
2094 | } | ||
2095 | #endif /* CONFIG_ELF_CORE */ | ||
2096 | |||
2097 | pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, | 1456 | pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr, |
2098 | spinlock_t **ptl) | 1457 | spinlock_t **ptl) |
2099 | { | 1458 | { |