aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <jeremy@goop.org>2007-05-06 17:48:54 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-07 15:12:51 -0400
commitaee16b3cee2746880e40945a9b5bff4f309cfbc4 (patch)
tree887faaebf5562dc1fac5e090140da04dc7e2a174 /mm
parenteb3a1e1145ca8f12372c7c96aa0702d86a9002a9 (diff)
Add apply_to_page_range() which applies a function to a pte range
Add a new mm function apply_to_page_range() which applies a given function to every pte in a given virtual address range in a given mm structure. This is a generic alternative to cut-and-pasting the Linux idiomatic pagetable walking code in every place that a sequence of PTEs must be accessed. Although this interface is intended to be useful in a wide range of situations, it is currently used specifically by several Xen subsystems, for example: to ensure that pagetables have been allocated for a virtual address range, and to construct batched special pagetable update requests to map I/O memory (in ioremap()). [akpm@linux-foundation.org: fix warning, unpleasantly] Signed-off-by: Ian Pratt <ian.pratt@xensource.com> Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk> Signed-off-by: Chris Wright <chrisw@sous-sol.org> Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com> Cc: Christoph Lameter <clameter@sgi.com> Cc: Matt Mackall <mpm@waste.org> Acked-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/memory.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/mm/memory.c b/mm/memory.c
index e7066e71dfa3..044feb7e7134 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1448,6 +1448,100 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1448} 1448}
1449EXPORT_SYMBOL(remap_pfn_range); 1449EXPORT_SYMBOL(remap_pfn_range);
1450 1450
1451static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
1452 unsigned long addr, unsigned long end,
1453 pte_fn_t fn, void *data)
1454{
1455 pte_t *pte;
1456 int err;
1457 struct page *pmd_page;
1458 spinlock_t *ptl = ptl; /* Suppress gcc warning */
1459
1460 pte = (mm == &init_mm) ?
1461 pte_alloc_kernel(pmd, addr) :
1462 pte_alloc_map_lock(mm, pmd, addr, &ptl);
1463 if (!pte)
1464 return -ENOMEM;
1465
1466 BUG_ON(pmd_huge(*pmd));
1467
1468 pmd_page = pmd_page(*pmd);
1469
1470 do {
1471 err = fn(pte, pmd_page, addr, data);
1472 if (err)
1473 break;
1474 } while (pte++, addr += PAGE_SIZE, addr != end);
1475
1476 if (mm != &init_mm)
1477 pte_unmap_unlock(pte-1, ptl);
1478 return err;
1479}
1480
1481static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
1482 unsigned long addr, unsigned long end,
1483 pte_fn_t fn, void *data)
1484{
1485 pmd_t *pmd;
1486 unsigned long next;
1487 int err;
1488
1489 pmd = pmd_alloc(mm, pud, addr);
1490 if (!pmd)
1491 return -ENOMEM;
1492 do {
1493 next = pmd_addr_end(addr, end);
1494 err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
1495 if (err)
1496 break;
1497 } while (pmd++, addr = next, addr != end);
1498 return err;
1499}
1500
1501static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
1502 unsigned long addr, unsigned long end,
1503 pte_fn_t fn, void *data)
1504{
1505 pud_t *pud;
1506 unsigned long next;
1507 int err;
1508
1509 pud = pud_alloc(mm, pgd, addr);
1510 if (!pud)
1511 return -ENOMEM;
1512 do {
1513 next = pud_addr_end(addr, end);
1514 err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
1515 if (err)
1516 break;
1517 } while (pud++, addr = next, addr != end);
1518 return err;
1519}
1520
1521/*
1522 * Scan a region of virtual memory, filling in page tables as necessary
1523 * and calling a provided function on each leaf page table.
1524 */
1525int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
1526 unsigned long size, pte_fn_t fn, void *data)
1527{
1528 pgd_t *pgd;
1529 unsigned long next;
1530 unsigned long end = addr + size;
1531 int err;
1532
1533 BUG_ON(addr >= end);
1534 pgd = pgd_offset(mm, addr);
1535 do {
1536 next = pgd_addr_end(addr, end);
1537 err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
1538 if (err)
1539 break;
1540 } while (pgd++, addr = next, addr != end);
1541 return err;
1542}
1543EXPORT_SYMBOL_GPL(apply_to_page_range);
1544
1451/* 1545/*
1452 * handle_pte_fault chooses page fault handler according to an entry 1546 * handle_pte_fault chooses page fault handler according to an entry
1453 * which was read non-atomically. Before making any commitment, on 1547 * which was read non-atomically. Before making any commitment, on