Skip to contentSkip to conversation

MMU Notifiers V7

PATCH In review Christoph Lameter 12 participants 90 messages Feb 15, 2008 – Mar 5, 2008

Subsystems (12)
lore
Share & tools
lore
#0 lore raw

cover MMU Notifiers V7

This is a patchset implementing MMU notifier callbacks based on Andrea's earlier work. These are needed if Linux pages are referenced from something else than tracked by the rmaps of the kernel (an external MMU). MMU notifiers allow us to get rid of the page pinning for RDMA and various other purposes. It gets rid of the broken use of mlock for page pinning and avoids having to lock pages by increasing the refcount.
(mlock really does *not* pin pages....)

More information on the rationale and the technical details can be found in the first patch and the README provided by that patch in Documentation/mmu_notifiers.

The known immediate users are

KVM
- Establishes a refcount to the page via get_user_pages().
- External references are called spte.
- Has page tables to track pages whose refcount was elevated but no reverse maps.

GRU
- Simple additional hardware TLB (possibly covering multiple instances of Linux)
- Needs TLB shootdown when the VM unmaps pages.
- Determines page address via follow_page (from interrupt context) but can fall back to get_user_pages().
- No page reference possible since no page status is kept..

XPmem
- Allows use of a processes memory by remote instances of Linux.
- Provides its own reverse mappings to track remote pte.
- Established refcounts on the exported pages.
- Must sleep in order to wait for remote acks of ptes that are being cleared.

Andrea's mmu_notifier #4 -> RFC V1

- Merge subsystem rmap based with Linux rmap based approach
- Move Linux rmap based notifiers out of macro
- Try to account for what locks are held while the notifiers are called.
- Develop a patch sequence that separates out the different types of hooks so that we can review their use.
- Avoid adding include to linux/mm_types.h
- Integrate RCU logic suggested by Peter.

V1->V2:
- Improve RCU support
- Use mmap_sem for mmu_notifier register / unregister
- Drop invalidate_page from COW, mm/fremap.c and mm/rmap.c since we already have invalidate_range() callbacks there.
- Clean compile for !MMU_NOTIFIER
- Isolate filemap_xip strangeness into its own diff
- Pass a the flag to invalidate_range to indicate if a spinlock is held.
- Add invalidate_all()

V2->V3:
- Further RCU fixes
- Fixes from Andrea to fixup aging and move invalidate_range() in do_wp_page and sys_remap_file_pages() after the pte clearing.

V3->V4:
- Drop locking and synchronize_rcu() on ->release since we know on release that we are the only executing thread. This is also true for invalidate_all() so we could drop off the mmu_notifier there early. Use hlist_del_init instead of hlist_del_rcu.
- Do the invalidation as begin/end pairs with the requirement that the driver holds off new references in between.
- Fixup filemap_xip.c
- Figure out a potential way in which XPmem can deal with locks that are held.
- Robin's patches to make the mmu_notifier logic manage the PageRmapExported bit.
- Strip cc list down a bit.
- Drop Peters new rcu list macro
- Add description to the core patch

V4->V5:
- Provide missing callouts for mremap.
- Provide missing callouts for copy_page_range.
- Reduce mm_struct space to zero if !MMU_NOTIFIER by #ifdeffing out structure contents.
- Get rid of the invalidate_all() callback by moving ->release in place of invalidate_all.
- Require holding mmap_sem on register/unregister instead of acquiring it ourselves. In some contexts where we want to register/unregister we are already holding mmap_sem.
- Split out the rmap support patch so that there is no need to apply all patches for KVM and GRU.

V5->V6:
- Provide missing range callouts for mprotect
- Fix do_wp_page control path sequencing
- Clarify locking conventions
- GRU and XPmem confirmed to work with this patchset.
- Provide skeleton code for GRU/KVM type callback and for XPmem type.
- Rework documentation and put it into Documentation/mmu_notifier.

V6->V7:
- Code our own page table traversal in the skeletons so that we can perform the insertion of a remote pte under pte lock.
- Discuss page pinning by increasing page refcount

#1 lore raw

PATCH 1/6 mmu_notifier: Core code

MMU notifiers are used for hardware and software that establishes external references to pages managed by the Linux kernel. These are page table entriews or tlb entries or something else that allows hardware (such as DMA engines, scatter gather devices, networking, sharing of address spaces across operating system boundaries) and software (Virtualization solutions such as KVM, Xen etc) to access memory managed by the Linux kernel.

The MMU notifier will notify the device driver that subscribes to such a notifier that the VM is going to do something with the memory mapped by that device. The device must then drop references for the indicated memory area. The references may be reestablished later.

The notification scheme is much better than the current schemes of avoiding the danger of the VM removing pages that are externally mapped. We currently either mlock pages used for RDMA, XPmem etc in memory or increase the refcount to pin the pages. Increasing the refcount makes it impossible for the VM to reclaim the page.

Mlock causes problems with reclaim and may lead to OOM if too many pages are pinned in memory. It is also incorrect in terms what the POSIX specificies for what role mlock should play. Mlock does *not* pin pages in memory. Mlock just means do not allow the page to be moved to swap.

Linux can move pages in memory (for example through the page migration mechanism). These pages can be moved even if they are mlocked(!!!!). The current approach of page pinning in use by RDMA etc is conceptually broken but there are currently no other easy solutions.

The alternate of increasing the page count to pin pages is also not that enticing since there will be continual attempts to reclaim or migrate these pages.

The solution here allows us to finally fix this issue by requiring such devices to subscribe to a notification chain that will allow them to work without pinning. The VM gains control of its memory again and the memory that has external references can be managed like regular memory.

This patch: Core portion

---
 Documentation/mmu_notifier/README |  105 ++++++++++++++++++++++
 include/linux/mm_types.h          |    7 +
 include/linux/mmu_notifier.h      |  180 ++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                     |    2 
 mm/Kconfig                        |    4 
 mm/Makefile                       |    1 
 mm/mmap.c                         |    2 
 mm/mmu_notifier.c                 |   76 ++++++++++++++++
 8 files changed, 377 insertions(+)
Documentation/mmu_notifier/README new file +105 -0
Index: linux-2.6/Documentation/mmu_notifier/README
===================================================================
@@ -0,0 +1,105 @@
+Linux MMU Notifiers
+-------------------
+
+MMU notifiers are used for hardware and software that establishes
+external references to pages managed by the Linux kernel. These are
+page table entriews or tlb entries or something else that allows
+hardware (such as DMA engines, scatter gather devices, networking,
+sharing of address spaces across operating system boundaries) and
+software (Virtualization solutions such as KVM, Xen etc) to
+access memory managed by the Linux kernel.
+
+The MMU notifier will notify the device driver that subscribes to such
+a notifier that the VM is going to do something with the memory
+mapped by that device. The device must then drop references for the
+indicated memory area. The references may be reestablished later.
+
+The notification scheme is much better than the current schemes of
+dealing with the danger of the VM removing pages.
+We currently mlock pages used for RDMA, XPmem etc in memory or
+increase the refcount of the pages.
+
+Both cause problems with reclaim and may lead to OOM if too many
+pages are pinned in memory. Mlock is also incorrect in terms of the POSIX
+specification of the role of mlock. Mlock does *not* pin pages in
+memory. It just does not allow the page to be moved to swap.
+The page refcount is used to track current users of a page struct.
+Artificially inflating the refcount means that the VM cannot track
+down all references to a page. It will not be able to reclaim or
+move a page. However, the core code will try again and again because
+the assumption is that an elevated refcount is a temporary situation.
+
+Linux can move pages in memory (for example through the page migration
+mechanism). These pages can be moved even if they are mlocked(!!!!).
+So the current approach in use by RDMA etc etc is conceptually broken
+but there are currently no other easy solutions.
+
+The solution here allows us to finally fix this issue by requiring
+such devices to subscribe to a notification chain that will allow
+them to work without pinning.
+
+The notifier chains provide two callback mechanisms. The
+first one is required for any device that establishes external mappings.
+The second (rmap) mechanism is required if a device needs to be
+able to sleep when invalidating references. Sleeping may be necessary
+if we are mapping across a network or to different Linux instances
+in the same address space.
+
+mmu_notifier mechanism (for KVM/GRU etc)
+----------------------------------------
+Callbacks are registered with an mm_struct from a device driver using
+mmu_notifier_register(). When the VM removes pages (or changes
+permissions on pages etc) then callbacks are triggered.
+
+The invalidation function for a single page (*invalidate_page)
+is called with spinlocks (in particular the pte lock) held. This allow
+for an easy implementation of external ptes that are on the local system.
+
+The invalidation mechanism for a range (*invalidate_range_begin/end*) is
+called most of the time without any locks held. It is only called with
+locks held for file backed mappings that are truncated. A flag indicates
+in which mode we are. A driver can use that mechanism to f.e.
+delay the freeing of the pages during truncate until no locks are held.
+
+Pages must be marked dirty if dirty bits are found to be set in
+the external ptes during unmap.
+
+The *release* method is called when a Linux process exits. It is run before
+the pages and mappings of a process are torn down and gives the device driver
+a chance to zap all the external mappings in one go.
+
+An example for a code that can be used to build a notifier mechanism into
+a device driver can be found in the file
+Documentation/mmu_notifier/skeleton.c
+
+mmu_rmap_notifier mechanism (XPMEM etc)
+---------------------------------------
+The mmu_rmap_notifier allows the device driver to implement their own rmap
+and allows the device driver to sleep during page eviction. This is necessary
+for complex drivers that f.e. allow the sharing of memory between processes
+running on different Linux instances (typically over a network or in a
+partitioned NUMA system).
+
+The mmu_rmap_notifier adds another invalidate_page() callout that is called
+*before* the Linux rmaps are walked. At that point only the page lock is
+held. The invalidate_page() function must walk the driver rmaps and evict
+all the references to the page.
+
+There is no process information available before the rmaps are consulted.
+The notifier mechanism can therefore not be attached to an mm_struct. Instead
+it is a global callback list. Having to perform a callback for each and every
+page that is reclaimed would be inefficient. Therefore we add an additional
+page flag: PageRmapExternal(). Only pages that are marked with this bit can
+be exported and the rmap callbacks will only be performed for pages marked
+that way.
+
+The required additional Page flag is only availabe in 64 bit mode and
+therefore the mmu_rmap_notifier portion is not available on 32 bit platforms.
+
+An example of code to build a mmu_notifier mechanism with rmap capabilty
+can be found in Documentation/mmu_notifier/skeleton_rmap.c
+
+February 9, 2008,
+	Christoph Lameter <[email protected]
+
+Index: linux-2.6/include/linux/mm_types.h
include/linux/mm_types.h +7 -0
Index: linux-2.6/include/linux/mm_types.h
===================================================================
@@ -159,6 +159,12 @@ struct vm_area_struct {
 #endif
 };
 
+struct mmu_notifier_head {
+#ifdef CONFIG_MMU_NOTIFIER
+	struct hlist_head head;
+#endif
+};
+
 struct mm_struct {
 	struct vm_area_struct * mmap;		/* list of VMAs */
 	struct rb_root mm_rb;
@@ -228,6 +234,7 @@ struct mm_struct {
 #ifdef CONFIG_CGROUP_MEM_CONT
 	struct mem_cgroup *mem_cgroup;
 #endif
+	struct mmu_notifier_head mmu_notifier; /* MMU notifier list */
 };
 
 #endif /* _LINUX_MM_TYPES_H */
include/linux/mmu_notifier.h new file +180 -0

180 lines not on this page · raw message

mm/Kconfig +4 -0
Index: linux-2.6/mm/Kconfig
===================================================================
@@ -193,3 +193,7 @@ config NR_QUICK
 config VIRT_TO_BUS
 	def_bool y
 	depends on !ARCH_NO_VIRT_TO_BUS
+
+config MMU_NOTIFIER
+	def_bool y
+	bool "MMU notifier, for paging KVM/RDMA"
mm/Makefile +1 -0
Index: linux-2.6/mm/Makefile
===================================================================
@@ -33,4 +33,5 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_SMP) += allocpercpu.o
 obj-$(CONFIG_QUICKLIST) += quicklist.o
 obj-$(CONFIG_CGROUP_MEM_CONT) += memcontrol.o
+obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o
 
mm/mmu_notifier.c new file +76 -0

76 lines not on this page · raw message

kernel/fork.c +2 -0
Index: linux-2.6/kernel/fork.c
===================================================================
@@ -53,6 +53,7 @@
 #include <linux/tty.h>
 #include <linux/proc_fs.h>
 #include <linux/blkdev.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -362,6 +363,7 @@ static struct mm_struct * mm_init(struct
 
 	if (likely(!mm_alloc_pgd(mm))) {
 		mm->def_flags = 0;
+		mmu_notifier_head_init(&mm->mmu_notifier);
 		return mm;
 	}
 
mm/mmap.c +2 -0
Index: linux-2.6/mm/mmap.c
===================================================================
@@ -26,6 +26,7 @@
 #include <linux/mount.h>
 #include <linux/mempolicy.h>
 #include <linux/rmap.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -2037,6 +2038,7 @@ void exit_mmap(struct mm_struct *mm)
 	unsigned long end;
 
 	/* mm's last user has gone, and its about to be pulled down */
+	mmu_notifier_release(mm);
 	arch_exit_mmap(mm);
 
 	lru_add_drain();
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#14 lore raw

PATCH 2/6 mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

In two cases we use invalidate_range_begin/end to invalidate single pages because the pair allows holding off new references (idea by Robin Holt).

do_wp_page(): We hold off new references while we update the pte.

xip_unmap: We are not taking the PageLock so we cannot
use the invalidate_page mmu_rmap_notifier. invalidate_range_begin/end stands in.

---
 mm/filemap_xip.c |    5 +++++
 mm/fremap.c      |    3 +++
 mm/hugetlb.c     |    3 +++
 mm/memory.c      |   35 +++++++++++++++++++++++++++++------
 mm/mmap.c        |    2 ++
 mm/mprotect.c    |    3 +++
 mm/mremap.c      |    7 ++++++-
 7 files changed, 51 insertions(+), 7 deletions(-)
mm/fremap.c +3 -0
Index: linux-2.6/mm/fremap.c
===================================================================
@@ -15,6 +15,7 @@
 #include <linux/rmap.h>
 #include <linux/module.h>
 #include <linux/syscalls.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/mmu_context.h>
 #include <asm/cacheflush.h>
@@ -214,7 +215,9 @@ asmlinkage long sys_remap_file_pages(uns
 		spin_unlock(&mapping->i_mmap_lock);
 	}
 
+	mmu_notifier(invalidate_range_begin, mm, start, start + size, 0);
 	err = populate_range(mm, vma, start, size, pgoff);
+	mmu_notifier(invalidate_range_end, mm, start, start + size, 0);
 	if (!err && !(flags & MAP_NONBLOCK)) {
 		if (unlikely(has_write_lock)) {
 			downgrade_write(&mm->mmap_sem);
mm/memory.c +29 -6
Index: linux-2.6/mm/memory.c
===================================================================
@@ -51,6 +51,7 @@
 #include <linux/init.h>
 #include <linux/writeback.h>
 #include <linux/memcontrol.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/pgalloc.h>
 #include <asm/uaccess.h>
@@ -611,6 +612,9 @@ int copy_page_range(struct mm_struct *ds
 	if (is_vm_hugetlb_page(vma))
 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
 
+	if (is_cow_mapping(vma->vm_flags))
+		mmu_notifier(invalidate_range_begin, src_mm, addr, end, 0);
+
 	dst_pgd = pgd_offset(dst_mm, addr);
 	src_pgd = pgd_offset(src_mm, addr);
 	do {
@@ -621,6 +625,11 @@ int copy_page_range(struct mm_struct *ds
 						vma, addr, next))
 			return -ENOMEM;
 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
+
+	if (is_cow_mapping(vma->vm_flags))
+		mmu_notifier(invalidate_range_end, src_mm,
+						vma->vm_start, end, 0);
+
 	return 0;
 }
 
@@ -893,13 +902,16 @@ unsigned long zap_page_range(struct vm_a
 	struct mmu_gather *tlb;
 	unsigned long end = address + size;
 	unsigned long nr_accounted = 0;
+	int atomic = details ? (details->i_mmap_lock != 0) : 0;
 
 	lru_add_drain();
 	tlb = tlb_gather_mmu(mm, 0);
 	update_hiwater_rss(mm);
+	mmu_notifier(invalidate_range_begin, mm, address, end, atomic);
 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
 	if (tlb)
 		tlb_finish_mmu(tlb, address, end);
+	mmu_notifier(invalidate_range_end, mm, address, end, atomic);
 	return end;
 }
 
@@ -1339,7 +1351,7 @@ int remap_pfn_range(struct vm_area_struc
 {
 	pgd_t *pgd;
 	unsigned long next;
-	unsigned long end = addr + PAGE_ALIGN(size);
+	unsigned long start = addr, end = addr + PAGE_ALIGN(size);
 	struct mm_struct *mm = vma->vm_mm;
 	int err;
 
@@ -1373,6 +1385,7 @@ int remap_pfn_range(struct vm_area_struc
 	pfn -= addr >> PAGE_SHIFT;
 	pgd = pgd_offset(mm, addr);
 	flush_cache_range(vma, addr, end);
+	mmu_notifier(invalidate_range_begin, mm, start, end, 0);
 	do {
 		next = pgd_addr_end(addr, end);
 		err = remap_pud_range(mm, pgd, addr, next,
@@ -1380,6 +1393,7 @@ int remap_pfn_range(struct vm_area_struc
 		if (err)
 			break;
 	} while (pgd++, addr = next, addr != end);
+	mmu_notifier(invalidate_range_end, mm, start, end, 0);
 	return err;
 }
 EXPORT_SYMBOL(remap_pfn_range);
@@ -1463,10 +1477,11 @@ int apply_to_page_range(struct mm_struct
 {
 	pgd_t *pgd;
 	unsigned long next;
-	unsigned long end = addr + size;
+	unsigned long start = addr, end = addr + size;
 	int err;
 
 	BUG_ON(addr >= end);
+	mmu_notifier(invalidate_range_begin, mm, start, end, 0);
 	pgd = pgd_offset(mm, addr);
 	do {
 		next = pgd_addr_end(addr, end);
@@ -1474,6 +1489,7 @@ int apply_to_page_range(struct mm_struct
 		if (err)
 			break;
 	} while (pgd++, addr = next, addr != end);
+	mmu_notifier(invalidate_range_end, mm, start, end, 0);
 	return err;
 }
 EXPORT_SYMBOL_GPL(apply_to_page_range);
@@ -1614,8 +1630,10 @@ static int do_wp_page(struct mm_struct *
 			page_table = pte_offset_map_lock(mm, pmd, address,
 							 &ptl);
 			page_cache_release(old_page);
-			if (!pte_same(*page_table, orig_pte))
-				goto unlock;
+			if (!pte_same(*page_table, orig_pte)) {
+				pte_unmap_unlock(page_table, ptl);
+				goto check_dirty;
+			}
 
 			page_mkwrite = 1;
 		}
@@ -1631,7 +1649,8 @@ static int do_wp_page(struct mm_struct *
 		if (ptep_set_access_flags(vma, address, page_table, entry,1))
 			update_mmu_cache(vma, address, entry);
 		ret |= VM_FAULT_WRITE;
-		goto unlock;
+		pte_unmap_unlock(page_table, ptl);
+		goto check_dirty;
 	}
 
 	/*
@@ -1653,6 +1672,8 @@ gotten:
 	if (mem_cgroup_charge(new_page, mm, GFP_KERNEL))
 		goto oom_free_new;
 
+	mmu_notifier(invalidate_range_begin, mm, address,
+				address + PAGE_SIZE, 0);
 	/*
 	 * Re-check the pte - we dropped the lock
 	 */
@@ -1691,8 +1712,10 @@ gotten:
 		page_cache_release(new_page);
 	if (old_page)
 		page_cache_release(old_page);
-unlock:
 	pte_unmap_unlock(page_table, ptl);
+	mmu_notifier(invalidate_range_end, mm,
+				address, address + PAGE_SIZE, 0);
+check_dirty:
 	if (dirty_page) {
 		if (vma->vm_file)
 			file_update_time(vma->vm_file);
mm/mmap.c +2 -0
Index: linux-2.6/mm/mmap.c
===================================================================
@@ -1748,11 +1748,13 @@ static void unmap_region(struct mm_struc
 	lru_add_drain();
 	tlb = tlb_gather_mmu(mm, 0);
 	update_hiwater_rss(mm);
+	mmu_notifier(invalidate_range_begin, mm, start, end, 0);
 	unmap_vmas(&tlb, vma, start, end, &nr_accounted, NULL);
 	vm_unacct_memory(nr_accounted);
 	free_pgtables(&tlb, vma, prev? prev->vm_end: FIRST_USER_ADDRESS,
 				 next? next->vm_start: 0);
 	tlb_finish_mmu(tlb, start, end);
+	mmu_notifier(invalidate_range_end, mm, start, end, 0);
 }
 
 /*
mm/hugetlb.c +3 -0
Index: linux-2.6/mm/hugetlb.c
===================================================================
@@ -14,6 +14,7 @@
 #include <linux/mempolicy.h>
 #include <linux/cpuset.h>
 #include <linux/mutex.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/page.h>
 #include <asm/pgtable.h>
@@ -755,6 +756,7 @@ void __unmap_hugepage_range(struct vm_ar
 	BUG_ON(start & ~HPAGE_MASK);
 	BUG_ON(end & ~HPAGE_MASK);
 
+	mmu_notifier(invalidate_range_begin, mm, start, end, 1);
 	spin_lock(&mm->page_table_lock);
 	for (address = start; address < end; address += HPAGE_SIZE) {
 		ptep = huge_pte_offset(mm, address);
@@ -775,6 +777,7 @@ void __unmap_hugepage_range(struct vm_ar
 	}
 	spin_unlock(&mm->page_table_lock);
 	flush_tlb_range(vma, start, end);
+	mmu_notifier(invalidate_range_end, mm, start, end, 1);
 	list_for_each_entry_safe(page, tmp, &page_list, lru) {
 		list_del(&page->lru);
 		put_page(page);
mm/filemap_xip.c +5 -0
Index: linux-2.6/mm/filemap_xip.c
===================================================================
@@ -13,6 +13,7 @@
 #include <linux/module.h>
 #include <linux/uio.h>
 #include <linux/rmap.h>
+#include <linux/mmu_notifier.h>
 #include <linux/sched.h>
 #include <asm/tlbflush.h>
 
@@ -190,6 +191,8 @@ __xip_unmap (struct address_space * mapp
 		address = vma->vm_start +
 			((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
 		BUG_ON(address < vma->vm_start || address >= vma->vm_end);
+		mmu_notifier(invalidate_range_begin, mm, address,
+					address + PAGE_SIZE, 1);
 		pte = page_check_address(page, mm, address, &ptl);
 		if (pte) {
 			/* Nuke the page table entry. */
@@ -201,6 +204,8 @@ __xip_unmap (struct address_space * mapp
 			pte_unmap_unlock(pte, ptl);
 			page_cache_release(page);
 		}
+		mmu_notifier(invalidate_range_end, mm,
+				address, address + PAGE_SIZE, 1);
 	}
 	spin_unlock(&mapping->i_mmap_lock);
 }
mm/mremap.c +6 -1
Index: linux-2.6/mm/mremap.c
===================================================================
@@ -18,6 +18,7 @@
 #include <linux/highmem.h>
 #include <linux/security.h>
 #include <linux/syscalls.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/uaccess.h>
 #include <asm/cacheflush.h>
@@ -124,12 +125,15 @@ unsigned long move_page_tables(struct vm
 		unsigned long old_addr, struct vm_area_struct *new_vma,
 		unsigned long new_addr, unsigned long len)
 {
-	unsigned long extent, next, old_end;
+	unsigned long extent, next, old_start, old_end;
 	pmd_t *old_pmd, *new_pmd;
 
+	old_start = old_addr;
 	old_end = old_addr + len;
 	flush_cache_range(vma, old_addr, old_end);
 
+	mmu_notifier(invalidate_range_begin, vma->vm_mm,
+					old_addr, old_end, 0);
 	for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
 		cond_resched();
 		next = (old_addr + PMD_SIZE) & PMD_MASK;
@@ -150,6 +154,7 @@ unsigned long move_page_tables(struct vm
 		move_ptes(vma, old_pmd, old_addr, old_addr + extent,
 				new_vma, new_pmd, new_addr);
 	}
+	mmu_notifier(invalidate_range_end, vma->vm_mm, old_start, old_end, 0);
 
 	return len + old_addr - old_end;	/* how much done */
 }
mm/mprotect.c +3 -0
Index: linux-2.6/mm/mprotect.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/syscalls.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
+#include <linux/mmu_notifier.h>
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
 #include <asm/cacheflush.h>
@@ -198,10 +199,12 @@ success:
 		dirty_accountable = 1;
 	}
 
+	mmu_notifier(invalidate_range_begin, mm, start, end, 0);
 	if (is_vm_hugetlb_page(vma))
 		hugetlb_change_protection(vma, start, end, vma->vm_page_prot);
 	else
 		change_protection(vma, start, end, vma->vm_page_prot, dirty_accountable);
+	mmu_notifier(invalidate_range_end, mm, start, end, 0);
 	vm_stat_account(mm, oldflags, vma->vm_file, -nrpages);
 	vm_stat_account(mm, newflags, vma->vm_file, nrpages);
 	return 0;
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#55 lore raw

PATCH 3/6 mmu_notifier: invalidate_page callbacks

In reply to Christoph Lameter

Two callbacks to remove individual pages as done in rmap code

invalidate_page()

Called from the inner loop of rmap walks to invalidate pages.

	age_page()

Called for the determination of the page referenced status.

If we do not care about page referenced status then an age_page callback may be be omitted. PageLock and pte lock are held when either of the functions is called.

---
 mm/rmap.c |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
mm/rmap.c +10 -3
Index: linux-2.6/mm/rmap.c
===================================================================
@@ -49,6 +49,7 @@
 #include <linux/module.h>
 #include <linux/kallsyms.h>
 #include <linux/memcontrol.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/tlbflush.h>
 
@@ -287,7 +288,8 @@ static int page_referenced_one(struct pa
 	if (vma->vm_flags & VM_LOCKED) {
 		referenced++;
 		*mapcount = 1;	/* break early from loop */
-	} else if (ptep_clear_flush_young(vma, address, pte))
+	} else if (ptep_clear_flush_young(vma, address, pte) |
+		   mmu_notifier_age_page(mm, address))
 		referenced++;
 
 	/* Pretend the page is referenced if the task has the
@@ -455,6 +457,7 @@ static int page_mkclean_one(struct page 
 
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		entry = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);
 		entry = pte_wrprotect(entry);
 		entry = pte_mkclean(entry);
 		set_pte_at(mm, address, pte, entry);
@@ -712,7 +715,8 @@ static int try_to_unmap_one(struct page 
 	 * skipped over this mm) then we should reactivate it.
 	 */
 	if (!migration && ((vma->vm_flags & VM_LOCKED) ||
-			(ptep_clear_flush_young(vma, address, pte)))) {
+			(ptep_clear_flush_young(vma, address, pte) |
+				mmu_notifier_age_page(mm, address)))) {
 		ret = SWAP_FAIL;
 		goto out_unmap;
 	}
@@ -720,6 +724,7 @@ static int try_to_unmap_one(struct page 
 	/* Nuke the page table entry. */
 	flush_cache_page(vma, address, page_to_pfn(page));
 	pteval = ptep_clear_flush(vma, address, pte);
+	mmu_notifier(invalidate_page, mm, address);
 
 	/* Move the dirty bit to the physical page now the pte is gone. */
 	if (pte_dirty(pteval))
@@ -844,12 +849,14 @@ static void try_to_unmap_cluster(unsigne
 		page = vm_normal_page(vma, address, *pte);
 		BUG_ON(!page || PageAnon(page));
 
-		if (ptep_clear_flush_young(vma, address, pte))
+		if (ptep_clear_flush_young(vma, address, pte) |
+		    mmu_notifier_age_page(mm, address))
 			continue;
 
 		/* Nuke the page table entry. */
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		pteval = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);
 
 		/* If nonlinear, store the file page offset in the pte. */
 		if (page->index != linear_page_index(vma, address))
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#63 lore raw

PATCH 4/6 mmu_notifier: Skeleton driver for a simple mmu_notifier

In reply to Christoph Lameter

This is example code for a simple device driver interface to unmap pages that were externally mapped.

Locking is simple through a single lock that is used to protect the device drivers data structures as well as a counter that tracks the active invalidates on a single address space.

The invalidation of extern ptes must be possible with code that does not require sleeping. The lock is taken for all driver operations on the mmu that the driver manages. Locking could be made more sophisticated but I think this is going to be okay for most uses.

---
 Documentation/mmu_notifier/skeleton.c |  267 ++++++++++++++++++++++++++++++++++
 1 file changed, 267 insertions(+)
Documentation/mmu_notifier/skeleton.c new file +267 -0
Index: linux-2.6/Documentation/mmu_notifier/skeleton.c
===================================================================
@@ -0,0 +1,267 @@
+#include <linux/mm.h>
+#include <linux/mmu_notifier.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/pagemap.h>
+
+/*
+ * Skeleton for an mmu notifier without rmap callbacks and no need to slepp
+ * during invalidate_page().
+ *
+ * (C) 2008 Silicon Graphics, Inc.
+ * 		Christoph Lameter <[email protected]>
+ *
+ * Note that the locking is fairly basic. One can add various optimizations
+ * here and there. There is a single lock for an address space which should be
+ * satisfactory for most cases. If not then the lock can be split like the
+ * pte_lock in Linux. It is most likely best to place the locks in the
+ * page table structure or into whatever the external mmu uses to
+ * track the mappings.
+ */
+
+struct my_mmu {
+	/* MMU notifier specific fields */
+	struct mmu_notifier notifier;
+	spinlock_t lock;	/* Protects counter and invidual zaps */
+	int invalidates;	/* Number of active range_invalidates */
+};
+
+/*
+ * Called with m->lock held
+ */
+static void my_mmu_insert_page(struct my_mmu *m,
+		unsigned long address, unsigned long pfn)
+{
+	/* Must be provided */
+	printk(KERN_INFO "insert page %p address=%lx pfn=%ld\n",
+							m, address, pfn);
+}
+
+/*
+ * Called with m->lock held (optional but usually required to
+ * protect data structures of the driver).
+ */
+static void my_mmu_zap_page(struct my_mmu *m, unsigned long address)
+{
+	/* Must be provided */
+	printk(KERN_INFO "zap page %p address=%lx\n", m, address);
+}
+
+/*
+ * Called with m->lock held
+ */
+static void my_mmu_zap_range(struct my_mmu *m,
+	unsigned long start, unsigned long end, int atomic)
+{
+	/* Must be provided */
+	printk(KERN_INFO "zap range %p address=%lx-%lx atomic=%d\n",
+						m, start, end, atomic);
+}
+
+/*
+ * Zap an individual page.
+ *
+ * Serialization with establishment of a new external pte occurs
+ * through the pte lock. The m->lock is taken to serialize access
+ * to the driver private data. If the driver does not need this
+ * serialization then the lock can be omitted.
+ */
+static void my_mmu_invalidate_page(struct mmu_notifier *mn,
+	struct mm_struct *mm, unsigned long address)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	spin_lock(&m->lock);
+	my_mmu_zap_page(m, address);
+	spin_unlock(&m->lock);
+}
+
+/*
+ * Increment and decrement of the number of range invalidates
+ */
+static inline void inc_active(struct my_mmu *m)
+{
+	spin_lock(&m->lock);
+	m->invalidates++;
+	spin_unlock(&m->lock);
+}
+
+static inline void dec_active(struct my_mmu *m)
+{
+	spin_lock(&m->lock);
+	m->invalidates--;
+	spin_unlock(&m->lock);
+}
+
+static void my_mmu_invalidate_range_begin(struct mmu_notifier *mn,
+	struct mm_struct *mm, unsigned long start, unsigned long end,
+	int atomic)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	inc_active(m);	/* Holds off new references */
+	my_mmu_zap_range(m, start, end, atomic);
+}
+
+static void my_mmu_invalidate_range_end(struct mmu_notifier *mn,
+	struct mm_struct *mm, unsigned long start, unsigned long end,
+	int atomic)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	dec_active(m);		/* Enables new references */
+}
+
+/*
+ * Populate a page.
+ *
+ * A return value of-EAGAIN means please retry this operation.
+ *
+ * Acquisition of mmap_sem can be omitted if the caller already holds
+ * the semaphore.
+ */
+struct page *my_mmu_populate_page(struct my_mmu *m,
+	struct vm_area_struct *vma,
+	unsigned long address, int atomic, int write)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct page *page = ERR_PTR(-EAGAIN);
+	int err;
+	int done = 0;
+	pgd_t *pgd;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *ptep, pte;
+	spinlock_t *ptl;
+
+	/* No need to do anything if a range invalidate is running */
+	if (m->invalidates)
+		return ERR_PTR(-EAGAIN);
+
+	if (atomic) {
+		if (!down_read_trylock(&mm->mmap_sem))
+			return ERR_PTR(-EAGAIN);
+	} else
+		down_read(&mm->mmap_sem);
+
+	do {
+		page = ERR_PTR(-EAGAIN);
+
+		if (m->invalidates)
+			break;
+
+		pgd = pgd_offset(mm, address);
+		if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+			goto check;
+
+		pud = pud_offset(pgd, address);
+		if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+			goto check;
+
+		pmd = pmd_offset(pud, address);
+		if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+			goto check;
+
+		ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
+		if (!ptep)
+			goto check;
+
+		pte = *ptep;
+		if (!pte_present(pte))
+			goto pte_unlock;
+		if (write && !pte_write(pte))
+			goto pte_unlock;
+
+		page = vm_normal_page(vma, address, pte);
+		if (page) {
+			done = 1;
+			/*
+			 * The m->lock is held to ensure that the count of
+			 * current invalidates stays constant.
+			 * invalidate_page() is held off by the pte lock.
+			 */
+			spin_lock(&m->lock);
+
+			if (!m->invalidates)
+				my_mmu_insert_page(m, address, page_to_pfn(page));
+			else
+				page = ERR_PTR(-EAGAIN);
+
+			spin_unlock(&m->lock);
+		}
+pte_unlock:
+		pte_unmap_unlock(ptep, ptl);
+check:
+
+		if (done || atomic)
+			break;
+
+		/*
+		 * Need to run the page fault handler to get the pte entry
+		 * setup right.
+		 */
+		err = get_user_pages(current, vma->vm_mm, address, 1,
+					write, 1, NULL, NULL);
+
+		if (err < 0) {
+			page = ERR_PTR(err);
+			break;
+		}
+
+	} while (!done);
+
+	up_read(&vma->vm_mm->mmap_sem);
+	return page;
+}
+
+/*
+ * All other threads accessing this mm_struct must have terminated by now.
+ */
+static void my_mmu_release(struct mmu_notifier *mn, struct mm_struct *mm)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	my_mmu_zap_range(m, 0, TASK_SIZE, 0);
+	kfree(m);
+	printk(KERN_INFO "MMU Notifier detaching\n");
+}
+
+static struct mmu_notifier_ops my_mmu_ops = {
+	my_mmu_release,
+	NULL,			/* No aging function */
+	my_mmu_invalidate_page,
+	my_mmu_invalidate_range_begin,
+	my_mmu_invalidate_range_end
+};
+
+/*
+ * This function must be called to activate callbacks from a process
+ */
+int my_mmu_attach_to_process(struct mm_struct *mm)
+{
+	struct my_mmu *m = kzalloc(sizeof(struct my_mmu), GFP_KERNEL);
+
+	if (!m)
+		return -ENOMEM;
+
+	m->notifier.ops = &my_mmu_ops;
+	spin_lock_init(&m->lock);
+
+	/*
+	 * mmap_sem handling can be omitted if it is guaranteed that
+	 * the context from which my_mmu_attach_to_process is called
+	 * is already holding a writelock on mmap_sem.
+	 */
+	down_write(&mm->mmap_sem);
+	mmu_notifier_register(&m->notifier, mm);
+	up_write(&mm->mmap_sem);
+
+	/*
+	 * RCU sync is expensive but necessary if we need to guarantee
+	 * that multiple threads running on other cpus have seen the
+	 * notifier changes.
+	 */
+	synchronize_rcu();
+	return 0;
+}
+
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#64 lore raw

PATCH 5/6 mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Christoph Lameter

These special additional callbacks are required because XPmem (and likely other mechanisms) do use their own rmap (multiple processes on a series of remote Linux instances may be accessing the memory of a process). F.e. XPmem may have to send out notifications to remote Linux instances and receive confirmation before a page can be freed.

So we handle this like an additional Linux reverse map that is walked after the existing rmaps have been walked. We leave the walking to the driver that is then able to use something else than a spinlock to walk its reverse maps. So we can actually call the driver without holding spinlocks while we hold the Pagelock.

However, we cannot determine the mm_struct that a page belongs to at that point. The mm_struct can only be determined from the rmaps by the device driver.

We add another pageflag (PageExternalRmap) that is set if a page has been remotely mapped (f.e. by a process from another Linux instance). We can then only perform the callbacks for pages that are actually in remote use.

Rmap notifiers need an extra page bit and are only available on 64 bit platforms. This functionality is not available on 32 bit!

A notifier that uses the reverse maps callbacks does not need to provide the invalidate_page() method that is called when locks are held.

---
 include/linux/mmu_notifier.h |   65 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/page-flags.h   |   11 +++++++
 mm/mmu_notifier.c            |   34 ++++++++++++++++++++++
 mm/rmap.c                    |    9 +++++
 4 files changed, 119 insertions(+)
include/linux/page-flags.h +11 -0
Index: linux-2.6/include/linux/page-flags.h
===================================================================
@@ -105,6 +105,7 @@
  * 64 bit  |           FIELDS             | ??????         FLAGS         |
  *         63                            32                              0
  */
+#define PG_external_rmap	30	/* Page has external rmap */
 #define PG_uncached		31	/* Page has been mapped as uncached */
 #endif
 
@@ -296,6 +297,16 @@ static inline void __ClearPageTail(struc
 #define SetPageUncached(page)	set_bit(PG_uncached, &(page)->flags)
 #define ClearPageUncached(page)	clear_bit(PG_uncached, &(page)->flags)
 
+#if defined(CONFIG_MMU_NOTIFIER) && defined(CONFIG_64BIT)
+#define PageExternalRmap(page)	test_bit(PG_external_rmap, &(page)->flags)
+#define SetPageExternalRmap(page) set_bit(PG_external_rmap, &(page)->flags)
+#define ClearPageExternalRmap(page) clear_bit(PG_external_rmap, \
+							&(page)->flags)
+#else
+#define ClearPageExternalRmap(page) do {} while (0)
+#define PageExternalRmap(page)	0
+#endif
+
 struct page;	/* forward declaration */
 
 extern void cancel_dirty_page(struct page *page, unsigned int account_size);
include/linux/mmu_notifier.h +65 -0

95 lines not on this page · raw message

mm/mmu_notifier.c +34 -0
Index: linux-2.6/mm/mmu_notifier.c
===================================================================
@@ -74,3 +74,37 @@ void mmu_notifier_unregister(struct mmu_
 }
 EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
 
+#ifdef CONFIG_64BIT
+static DEFINE_SPINLOCK(mmu_notifier_list_lock);
+HLIST_HEAD(mmu_rmap_notifier_list);
+
+void mmu_rmap_notifier_register(struct mmu_rmap_notifier *mrn)
+{
+	spin_lock(&mmu_notifier_list_lock);
+	hlist_add_head_rcu(&mrn->hlist, &mmu_rmap_notifier_list);
+	spin_unlock(&mmu_notifier_list_lock);
+}
+EXPORT_SYMBOL(mmu_rmap_notifier_register);
+
+void mmu_rmap_notifier_unregister(struct mmu_rmap_notifier *mrn)
+{
+	spin_lock(&mmu_notifier_list_lock);
+	hlist_del_rcu(&mrn->hlist);
+	spin_unlock(&mmu_notifier_list_lock);
+}
+EXPORT_SYMBOL(mmu_rmap_notifier_unregister);
+
+/*
+ * Export a page.
+ *
+ * Pagelock must be held.
+ * Must be called before a page is put on an external rmap.
+ */
+void mmu_rmap_export_page(struct page *page)
+{
+	BUG_ON(!PageLocked(page));
+	SetPageExternalRmap(page);
+}
+EXPORT_SYMBOL(mmu_rmap_export_page);
+
+#endif
mm/rmap.c +9 -0
Index: linux-2.6/mm/rmap.c
===================================================================
@@ -497,6 +497,10 @@ int page_mkclean(struct page *page)
 		struct address_space *mapping = page_mapping(page);
 		if (mapping) {
 			ret = page_mkclean_file(mapping, page);
+			if (unlikely(PageExternalRmap(page))) {
+				mmu_rmap_notifier(invalidate_page, page);
+				ClearPageExternalRmap(page);
+			}
 			if (page_test_dirty(page)) {
 				page_clear_dirty(page);
 				ret = 1;
@@ -1013,6 +1017,11 @@ int try_to_unmap(struct page *page, int 
 	else
 		ret = try_to_unmap_file(page, migration);
 
+	if (unlikely(PageExternalRmap(page))) {
+		mmu_rmap_notifier(invalidate_page, page);
+		ClearPageExternalRmap(page);
+	}
+
 	if (!page_mapped(page))
 		ret = SWAP_SUCCESS;
 	return ret;
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#84 lore raw

PATCH 6/6 mmu_rmap_notifier: Skeleton for complex driver that uses its own rmaps

In reply to Christoph Lameter

The skeleton for the rmap notifier leaves the invalidate_page method of the mmu_notifier empty and hooks a new invalidate_page callback into the global chain for mmu_rmap_notifiers.

There are seveal simplifications in here to avoid making this too complex. The reverse maps need to consit of references to vma f.e.

---
 Documentation/mmu_notifier/skeleton_rmap.c |  311 +++++++++++++++++++++++++++++
 1 file changed, 311 insertions(+)
Documentation/mmu_notifier/skeleton_rmap.c new file +311 -0
Index: linux-2.6/Documentation/mmu_notifier/skeleton_rmap.c
===================================================================
@@ -0,0 +1,311 @@
+#include <linux/mm.h>
+#include <linux/mmu_notifier.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/pagemap.h>
+
+/*
+ * Skeleton for an mmu notifier with rmap callbacks and sleeping during
+ * invalidate_page.
+ *
+ * (C) 2008 Silicon Graphics, Inc.
+ * 		Christoph Lameter <[email protected]>
+ *
+ * Note that the locking is fairly basic. One can add various optimizations
+ * here and there. There is a single lock for an address space which should be
+ * satisfactory for most cases. If not then the lock can be split like the
+ * pte_lock in Linux. It is most likely best to place the locks in the
+ * page table structure or into whatever the external mmu uses to
+ * track the mappings.
+ */
+
+struct my_mmu {
+	/* MMU notifier specific fields */
+	struct mmu_notifier notifier;
+	spinlock_t lock;	/* Protects counter and invidual zaps */
+	int invalidates;	/* Number of active range_invalidate */
+
+       /* Rmap support */
+       struct list_head list;	/* rmap list of my_mmu structs */
+       unsigned long base;
+};
+
+/*
+ * Called with m->lock held
+ */
+static void my_mmu_insert_page(struct my_mmu *m,
+		unsigned long address, unsigned long pfn)
+{
+	/* Must be provided */
+	printk(KERN_INFO "insert page %p address=%lx pfn=%ld\n",
+							m, address, pfn);
+}
+
+/*
+ * Called with m->lock held
+ */
+static void my_mmu_zap_range(struct my_mmu *m,
+	unsigned long start, unsigned long end, int atomic)
+{
+	/* Must be provided */
+	printk(KERN_INFO "zap range %p address=%lx-%lx atomic=%d\n",
+						m, start, end, atomic);
+}
+
+/*
+ * Called with m->lock held (optional but usually required to
+ * protect data structures of the driver).
+ */
+static void my_mmu_zap_page(struct my_mmu *m, unsigned long address)
+{
+	/* Must be provided */
+	printk(KERN_INFO "zap page %p address=%lx\n", m, address);
+}
+
+/*
+ * Increment and decrement of the number of range invalidates
+ */
+static inline void inc_active(struct my_mmu *m)
+{
+	spin_lock(&m->lock);
+	m->invalidates++;
+	spin_unlock(&m->lock);
+}
+
+static inline void dec_active(struct my_mmu *m)
+{
+	spin_lock(&m->lock);
+	m->invalidates--;
+	spin_unlock(&m->lock);
+}
+
+static void my_mmu_invalidate_range_begin(struct mmu_notifier *mn,
+	struct mm_struct *mm, unsigned long start, unsigned long end,
+	int atomic)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	inc_active(m);	/* Holds off new references */
+	my_mmu_zap_range(m, start, end, atomic);
+}
+
+static void my_mmu_invalidate_range_end(struct mmu_notifier *mn,
+	struct mm_struct *mm, unsigned long start, unsigned long end,
+	int atomic)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	dec_active(m);	/* Enables new references */
+}
+
+/*
+ * Populate a page.
+ *
+ * A return value of-EAGAIN means please retry this operation.
+ *
+ * Acuisition of mmap_sem can be omitted if the caller already holds
+ * the semaphore.
+ */
+struct page *my_mmu_populate_page(struct my_mmu *m,
+	struct vm_area_struct *vma,
+	unsigned long address, int write)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	struct page *page;
+	int err;
+	int done = 0;
+	pgd_t *pgd;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *ptep, pte;
+	spinlock_t *ptl;
+
+	/* No need to do anything if a range invalidate is running */
+	if (m->invalidates)
+		return ERR_PTR(-EAGAIN);
+
+	down_read(&mm->mmap_sem);
+	do {
+		page = ERR_PTR(-EAGAIN);
+
+		if (m->invalidates)
+			break;
+
+		pgd = pgd_offset(mm, address);
+		if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
+			goto check;
+
+		pud = pud_offset(pgd, address);
+		if (pud_none(*pud) || unlikely(pud_bad(*pud)))
+			goto check;
+
+		pmd = pmd_offset(pud, address);
+		if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
+			goto check;
+
+		ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
+		if (!ptep)
+			goto check;
+
+		pte = *ptep;
+		if (!pte_present(pte))
+			goto pte_unlock;
+		if (write && !pte_write(pte))
+			goto pte_unlock;
+
+		page = vm_normal_page(vma, address, pte);
+		if (page) {
+			done = 1;
+			/*
+			 * The m->lock is held to ensure that the count of
+			 * current invalidates stays constant.
+			 * invalidate_page() is held off by the pte lock.
+			 */
+			spin_lock(&m->lock);
+
+			if (!m->invalidates)
+				my_mmu_insert_page(m, address, page_to_pfn(page));
+			else
+				page = ERR_PTR(-EAGAIN);
+
+			spin_unlock(&m->lock);
+		}
+pte_unlock:
+		pte_unmap_unlock(ptep, ptl);
+check:
+
+		if (done)
+			break;
+
+		/*
+		 * Need to run the page fault handler to get the pte entry
+		 * setup right.
+		 */
+		err = get_user_pages(current, vma->vm_mm, address, 1,
+					write, 1, NULL, NULL);
+
+		if (err < 0) {
+			page = ERR_PTR(err);
+			break;
+		}
+
+	} while (!done);
+
+	up_read(&vma->vm_mm->mmap_sem);
+	return page;
+}
+
+/*
+ * All other threads accessing this mm_struct must have terminated by now.
+ */
+static void my_mmu_release(struct mmu_notifier *mn, struct mm_struct *mm)
+{
+	struct my_mmu *m = container_of(mn, struct my_mmu, notifier);
+
+	my_mmu_zap_range(m, 0, TASK_SIZE, 0);
+	/* No concurrent processes thus no worries about RCU */
+	list_del(&m->list);
+	kfree(m);
+	printk(KERN_INFO "MMU Notifier terminating\n");
+}
+
+static struct mmu_notifier_ops my_mmu_ops = {
+	my_mmu_release,
+	NULL,		/* No aging function */
+	NULL,		/* No atomic invalidate_page function */
+	my_mmu_invalidate_range_begin,
+	my_mmu_invalidate_range_end
+};
+
+/* Rmap specific fields */
+static LIST_HEAD(my_mmu_list);
+static struct rw_semaphore listlock;
+
+/*
+ * This function must be called to activate callbacks from a process
+ */
+int my_mmu_attach_to_process(struct mm_struct *mm)
+{
+	struct my_mmu *m = kzalloc(sizeof(struct my_mmu), GFP_KERNEL);
+
+	if (!m)
+		return -ENOMEM;
+
+	m->notifier.ops = &my_mmu_ops;
+	spin_lock_init(&m->lock);
+
+	/*
+	 * mmap_sem handling can be omitted if it is guaranteed that
+	 * the context from which my_mmu_attach_to_process is called
+	 * is already holding a writelock on mmap_sem.
+	 */
+	down_write(&mm->mmap_sem);
+	mmu_notifier_register(&m->notifier, mm);
+	up_write(&mm->mmap_sem);
+	down_write(&listlock);
+	list_add(&m->list, &my_mmu_list);
+	up_write(&listlock);
+
+	/*
+	 * RCU sync is expensive but necessary if we need to guarantee
+	 * that multiple threads running on other cpus have seen the
+	 * notifier changes.
+	 */
+	synchronize_rcu();
+	return 0;
+}
+
+
+static void my_sleeping_invalidate_page(struct my_mmu *m, unsigned long address)
+{
+	/* Must be provided */
+	spin_lock(&m->lock);	/* Only taken to ensure mmu data integrity */
+	my_mmu_zap_page(m, address);
+	spin_unlock(&m->lock);
+	printk(KERN_INFO "Sleeping invalidate_page %p address=%lx\n",
+                                                               m, address);
+}
+
+static unsigned long my_mmu_find_addr(struct my_mmu *m, struct page *page)
+{
+	/* Determine the address of a page in a mmu segment */
+	return -EFAULT;
+}
+
+/*
+ * A reference must be held on the page passed and the page passed
+ * must be locked. No spinlocks are held. invalidate_page() is held
+ * off by us holding the page lock.
+ */
+static void my_mmu_rmap_invalidate_page(struct mmu_rmap_notifier *mrn,
+							struct page *page)
+{
+	struct my_mmu *m;
+
+	BUG_ON(!PageLocked(page));
+	down_read(&listlock);
+	list_for_each_entry(m, &my_mmu_list, list) {
+		unsigned long address = my_mmu_find_addr(m, page);
+
+		if (address != -EFAULT)
+			my_sleeping_invalidate_page(m, address);
+	}
+	up_read(&listlock);
+}
+
+static struct mmu_rmap_notifier_ops my_mmu_rmap_ops = {
+	.invalidate_page = my_mmu_rmap_invalidate_page
+};
+
+static struct mmu_rmap_notifier my_mmu_rmap_notifier = {
+	.ops = &my_mmu_rmap_ops
+};
+
+static int __init my_mmu_init(void)
+{
+	mmu_rmap_notifier_register(&my_mmu_rmap_notifier);
+	return 0;
+}
+
+late_initcall(my_mmu_init);
+
Signature · 4 lines
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
maintainer
#2 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Christoph Lameter
Quoted 33 lines · On Thu, 14 Feb 2008 22:49:00 -0800 Christoph Lameter <[email protected]> wrote:

MMU notifiers are used for hardware and software that establishes external references to pages managed by the Linux kernel. These are page table entriews or tlb entries or something else that allows hardware (such as DMA engines, scatter gather devices, networking, sharing of address spaces across operating system boundaries) and software (Virtualization solutions such as KVM, Xen etc) to access memory managed by the Linux kernel.

The MMU notifier will notify the device driver that subscribes to such a notifier that the VM is going to do something with the memory mapped by that device. The device must then drop references for the indicated memory area. The references may be reestablished later.

The notification scheme is much better than the current schemes of avoiding the danger of the VM removing pages that are externally mapped. We currently either mlock pages used for RDMA, XPmem etc in memory or increase the refcount to pin the pages. Increasing the refcount makes it impossible for the VM to reclaim the page.

Mlock causes problems with reclaim and may lead to OOM if too many pages are pinned in memory. It is also incorrect in terms what the POSIX specificies for what role mlock should play. Mlock does *not* pin pages in memory. Mlock just means do not allow the page to be moved to swap.

Linux can move pages in memory (for example through the page migration mechanism). These pages can be moved even if they are mlocked(!!!!). The current approach of page pinning in use by RDMA etc is conceptually broken but there are currently no other easy solutions.

The alternate of increasing the page count to pin pages is also not that enticing since there will be continual attempts to reclaim or migrate these pages.

Quoted 8 lines

The solution here allows us to finally fix this issue by requiring such devices to subscribe to a notification chain that will allow them to work without pinning. The VM gains control of its memory again and the memory that has external references can be managed like regular memory.

This patch: Core portion

What is the status of getting infiniband to use this facility?

How important is this feature to KVM?

To xpmem?

Which other potential clients have been identified and how important it it to those?

Quoted 41 lines

Index: linux-2.6/Documentation/mmu_notifier/README
===================================================================

Documentation/mmu_notifier/README new file +36 -0
@@ -0,0 +1,105 @@
+Linux MMU Notifiers
+-------------------
+
+MMU notifiers are used for hardware and software that establishes
+external references to pages managed by the Linux kernel. These are
+page table entriews or tlb entries or something else that allows
+hardware (such as DMA engines, scatter gather devices, networking,
+sharing of address spaces across operating system boundaries) and
+software (Virtualization solutions such as KVM, Xen etc) to
+access memory managed by the Linux kernel.
+
+The MMU notifier will notify the device driver that subscribes to such
+a notifier that the VM is going to do something with the memory
+mapped by that device. The device must then drop references for the
+indicated memory area. The references may be reestablished later.
+
+The notification scheme is much better than the current schemes of
+dealing with the danger of the VM removing pages.
+We currently mlock pages used for RDMA, XPmem etc in memory or
+increase the refcount of the pages.
+
+Both cause problems with reclaim and may lead to OOM if too many
+pages are pinned in memory. Mlock is also incorrect in terms of the POSIX
+specification of the role of mlock. Mlock does *not* pin pages in
+memory. It just does not allow the page to be moved to swap.
+The page refcount is used to track current users of a page struct.
+Artificially inflating the refcount means that the VM cannot track
+down all references to a page. It will not be able to reclaim or
+move a page. However, the core code will try again and again because
+the assumption is that an elevated refcount is a temporary situation.
+
+Linux can move pages in memory (for example through the page migration
+mechanism). These pages can be moved even if they are mlocked(!!!!).
+So the current approach in use by RDMA etc etc is conceptually broken
+but there are currently no other easy solutions.
+

Documentation/mmu_notifier/README line 46

Quoted 10 lines
Documentation/mmu_notifier/README new file +10 -0
+The solution here allows us to finally fix this issue by requiring
+such devices to subscribe to a notification chain that will allow
+them to work without pinning.
+
+The notifier chains provide two callback mechanisms. The
+first one is required for any device that establishes external mappings.
+The second (rmap) mechanism is required if a device needs to be
+able to sleep when invalidating references. Sleeping may be necessary
+if we are mapping across a network or to different Linux instances
+in the same address space.

I'd have thought that a major reason for sleeping would be to wait for IO to complete. Worth mentioning here?

Documentation/mmu_notifier/README line 54

Quoted 7 lines
Documentation/mmu_notifier/README new file +7 -0
+mmu_notifier mechanism (for KVM/GRU etc)
+----------------------------------------
+Callbacks are registered with an mm_struct from a device driver using
+mmu_notifier_register(). When the VM removes pages (or changes
+permissions on pages etc) then callbacks are triggered.
+
+The invalidation function for a single page (*invalidate_page)

We already have an invalidatepage. Ho hum.

Documentation/mmu_notifier/README line 56

Quoted 3 lines
Documentation/mmu_notifier/README new file +2 -0
+is called with spinlocks (in particular the pte lock) held. This allow
+for an easy implementation of external ptes that are on the local system.

Why is that "easy"? I's have thought that it would only be easy if the driver happened to be using those same locks for its own purposes. Otherwise it is "awkward"?

Documentation/mmu_notifier/README line 62

Quoted 5 lines
Documentation/mmu_notifier/README new file +5 -0
+The invalidation mechanism for a range (*invalidate_range_begin/end*) is
+called most of the time without any locks held. It is only called with
+locks held for file backed mappings that are truncated. A flag indicates
+in which mode we are. A driver can use that mechanism to f.e.
+delay the freeing of the pages during truncate until no locks are held.

That sucks big time. What do we need to do to make get the callback functions called in non-atomic context?

Documentation/mmu_notifier/README line 65

Quoted 2 lines
Documentation/mmu_notifier/README new file +2 -0
+Pages must be marked dirty if dirty bits are found to be set in
+the external ptes during unmap.

That sentence is too vague. Define "marked dirty"?

Documentation/mmu_notifier/README line 67

Quoted 1 line
Documentation/mmu_notifier/README new file +1 -0
+The *release* method is called when a Linux process exits. It is run before

We'd conventionally use a notation such as "->release()" here, rather than the asterisks.

Documentation/mmu_notifier/README line 69

Quoted 2 lines
Documentation/mmu_notifier/README new file +2 -0
+the pages and mappings of a process are torn down and gives the device driver
+a chance to zap all the external mappings in one go.

I assume what you mean here is that ->release() is called during exit() when the final reference to an mm is being dropped.

Documentation/mmu_notifier/README line 73

Quoted 3 lines
Documentation/mmu_notifier/README new file +3 -0
+An example for a code that can be used to build a notifier mechanism into
+a device driver can be found in the file
+Documentation/mmu_notifier/skeleton.c

Should that be in samples/?

Documentation/mmu_notifier/README line 77

Quoted 3 lines
Documentation/mmu_notifier/README new file +3 -0
+mmu_rmap_notifier mechanism (XPMEM etc)
+---------------------------------------
+The mmu_rmap_notifier allows the device driver to implement their own rmap

s/their/its/

Documentation/mmu_notifier/README line 86

Quoted 9 lines
Documentation/mmu_notifier/README new file +9 -0
+and allows the device driver to sleep during page eviction. This is necessary
+for complex drivers that f.e. allow the sharing of memory between processes
+running on different Linux instances (typically over a network or in a
+partitioned NUMA system).
+
+The mmu_rmap_notifier adds another invalidate_page() callout that is called
+*before* the Linux rmaps are walked. At that point only the page lock is
+held. The invalidate_page() function must walk the driver rmaps and evict
+all the references to the page.

What happens if it cannot do so?

Documentation/mmu_notifier/README line 88

Quoted 1 line
Documentation/mmu_notifier/README new file +1 -0
+There is no process information available before the rmaps are consulted.

Not sure what that sentence means. I guess "available to the core VM"?

Documentation/mmu_notifier/README

Quoted 4 lines
Documentation/mmu_notifier/README new file +4 -0
+The notifier mechanism can therefore not be attached to an mm_struct. Instead
+it is a global callback list. Having to perform a callback for each and every
+page that is reclaimed would be inefficient. Therefore we add an additional
+page flag: PageRmapExternal().

How many page flags are left?

Is this feature important enough to justfy consumption of another one?

Documentation/mmu_notifier/README line 94

Quoted 3 lines

Only pages that are marked with this bit can

Documentation/mmu_notifier/README new file +2 -0
+be exported and the rmap callbacks will only be performed for pages marked
+that way.

"exported": new term, unclear what it means.

Documentation/mmu_notifier/README line 97

Quoted 2 lines
Documentation/mmu_notifier/README new file +2 -0
+The required additional Page flag is only availabe in 64 bit mode and
+therefore the mmu_rmap_notifier portion is not available on 32 bit platforms.

whoa. Is that good? You just made your feature unavailable on the great majority of Linux systems.

Quoted 37 lines
Documentation/mmu_notifier/README new file +7 -0
+An example of code to build a mmu_notifier mechanism with rmap capabilty
+can be found in Documentation/mmu_notifier/skeleton_rmap.c
+
+February 9, 2008,
+	Christoph Lameter <[email protected]
+
+Index: linux-2.6/include/linux/mm_types.h

Index: linux-2.6/include/linux/mm_types.h
===================================================================

include/linux/mm_types.h +7 -0
@@ -159,6 +159,12 @@ struct vm_area_struct {
 #endif
 };
 
+struct mmu_notifier_head {
+#ifdef CONFIG_MMU_NOTIFIER
+	struct hlist_head head;
+#endif
+};
+
 struct mm_struct {
 	struct vm_area_struct * mmap;		/* list of VMAs */
 	struct rb_root mm_rb;
@@ -228,6 +234,7 @@ struct mm_struct {
 #ifdef CONFIG_CGROUP_MEM_CONT
 	struct mem_cgroup *mem_cgroup;
 #endif
+	struct mmu_notifier_head mmu_notifier; /* MMU notifier list */
 };
 
 #endif /* _LINUX_MM_TYPES_H */

Index: linux-2.6/include/linux/mmu_notifier.h
===================================================================

include/linux/mmu_notifier.h line 5

Quoted 5 lines
include/linux/mmu_notifier.h new file +5 -0
@@ -0,0 +1,180 @@
+#ifndef _LINUX_MMU_NOTIFIER_H
+#define _LINUX_MMU_NOTIFIER_H
+
+/*
+ * MMU motifier

typo

include/linux/mmu_notifier.h line 10

Quoted 4 lines
include/linux/mmu_notifier.h new file +4 -0
+ * Notifier functions for hardware and software that establishes external
+ * references to pages of a Linux system. The notifier calls ensure that
+ * external mappings are removed when the Linux VM removes memory ranges
+ * or individual pages from a process.

So the callee cannot fail. hm. If it can't block, it's likely screwed in that case. In other cases it might be screwed anyway. I suspect we'll need to be able to handle callee failure.

include/linux/mmu_notifier.h line 17

Quoted 6 lines
include/linux/mmu_notifier.h new file +6 -0
+ * These fall into two classes:
+ *
+ * 1. mmu_notifier
+ *
+ * 	These are callbacks registered with an mm_struct. If pages are
+ * 	removed from an address space then callbacks are performed.

"to be removed", I guess. It's called before the page is actually removed?

include/linux/mmu_notifier.h line 20

Quoted 2 lines
include/linux/mmu_notifier.h new file +2 -0
+ * 	Spinlocks must be held in order to walk reverse maps. The
+ * 	invalidate_page() callbacks are performed with spinlocks held.

hm, yes, problem. Permitting callee failure might be good enough.

include/linux/mmu_notifier.h line 24

Quoted 3 lines
include/linux/mmu_notifier.h new file +3 -0
+ * 	The invalidate_range_start/end callbacks can be performed in contexts
+ * 	where sleeping is allowed or in atomic contexts. A flag is passed
+ * 	to indicate an atomic context.

We generally would prefer separate callbacks, rather than a unified callback with a mode flag.

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+ *	Pages must be marked dirty if dirty bits are found to be set in
+ *	the external ptes.
+ */
+
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/rcupdate.h>
+#include <linux/mm_types.h>
+
+struct mmu_notifier_ops;

include/linux/mmu_notifier.h line 45

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+
+struct mmu_notifier {
+	struct hlist_node hlist;
+	const struct mmu_notifier_ops *ops;
+};
+
+struct mmu_notifier_ops {
+	/*
+	 * The release notifier is called when no other execution threads
+	 * are left. Synchronization is not necessary.

"and the mm is about to be destroyed"?

include/linux/mmu_notifier.h line 55

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+	 */
+	void (*release)(struct mmu_notifier *mn,
+			struct mm_struct *mm);
+
+	/*
+	 * age_page is called from contexts where the pte_lock is held
+	 */
+	int (*age_page)(struct mmu_notifier *mn,
+			struct mm_struct *mm,
+			unsigned long address);

This wasn't documented.

include/linux/mmu_notifier.h

Quoted 12 lines
include/linux/mmu_notifier.h new file +12 -0
+	/*
+	 * invalidate_page is called from contexts where the pte_lock is held.
+	 */
+	void (*invalidate_page)(struct mmu_notifier *mn,
+				struct mm_struct *mm,
+				unsigned long address);
+
+	/*
+	 * invalidate_range_begin() and invalidate_range_end() must be paired.
+	 *
+	 * Multiple invalidate_range_begin/ends may be nested or called
+	 * concurrently.

Under what circumstances would they be nested?

Quoted 1 line

That is legit. However, no new external references

references to what?

include/linux/mmu_notifier.h line 70

Quoted 2 lines
include/linux/mmu_notifier.h new file +2 -0
+	 * may be established as long as any invalidate_xxx is running or
+	 * any invalidate_range_begin() and has not been completed through a

stray "and".

include/linux/mmu_notifier.h line 76

Quoted 6 lines
include/linux/mmu_notifier.h new file +6 -0
+	 * corresponding call to invalidate_range_end().
+	 *
+	 * Locking within the notifier needs to serialize events correspondingly.
+	 *
+	 * invalidate_range_begin() must clear all references in the range
+	 * and stop the establishment of new references.

and stop the establishment of new references within the range, I assume?

If so, that's putting a heck of a lot of complexity into the driver, isn't it? It needs to temporarily remember an arbitrarily large number of regions in this mm against which references may not be taken?

include/linux/mmu_notifier.h line 78

Quoted 1 line
include/linux/mmu_notifier.h new file +1 -0
+	 * invalidate_range_end() reenables the establishment of references.

within the range?

Quoted 33 lines
include/linux/mmu_notifier.h new file +33 -0
+	 * atomic indicates that the function is called in an atomic context.
+	 * We can sleep if atomic == 0.
+	 *
+	 * invalidate_range_begin() must remove all external references.
+	 * There will be no retries as with invalidate_page().
+	 */
+	void (*invalidate_range_begin)(struct mmu_notifier *mn,
+				 struct mm_struct *mm,
+				 unsigned long start, unsigned long end,
+				 int atomic);
+
+	void (*invalidate_range_end)(struct mmu_notifier *mn,
+				 struct mm_struct *mm,
+				 unsigned long start, unsigned long end,
+				 int atomic);
+};
+
+#ifdef CONFIG_MMU_NOTIFIER
+
+/*
+ * Must hold the mmap_sem for write.
+ *
+ * RCU is used to traverse the list. A quiescent period needs to pass
+ * before the notifier is guaranteed to be visible to all threads
+ */
+extern void mmu_notifier_register(struct mmu_notifier *mn,
+				  struct mm_struct *mm);
+
+/*
+ * Must hold mmap_sem for write.
+ *
+ * A quiescent period needs to pass before the mmu_notifier structure
+ * can be released. mmu_notifier_release() will wait for a quiescent period

include/linux/mmu_notifier.h line 122

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+ * after calling the ->release callback. So it is safe to call
+ * mmu_notifier_unregister from the ->release function.
+ */
+extern void mmu_notifier_unregister(struct mmu_notifier *mn,
+				    struct mm_struct *mm);
+
+
+extern void mmu_notifier_release(struct mm_struct *mm);
+extern int mmu_notifier_age_page(struct mm_struct *mm,
+				 unsigned long address);

There's the mysterious age_page again.

Quoted 12 lines
include/linux/mmu_notifier.h new file +12 -0
+static inline void mmu_notifier_head_init(struct mmu_notifier_head *mnh)
+{
+	INIT_HLIST_HEAD(&mnh->head);
+}
+
+#define mmu_notifier(function, mm, args...)				\
+	do {								\
+		struct mmu_notifier *__mn;				\
+		struct hlist_node *__n;					\
+									\
+		if (unlikely(!hlist_empty(&(mm)->mmu_notifier.head))) { \
+			rcu_read_lock();				\

include/linux/mmu_notifier.h line 145

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+			hlist_for_each_entry_rcu(__mn, __n,		\
+					     &(mm)->mmu_notifier.head,	\
+					     hlist)			\
+				if (__mn->ops->function)		\
+					__mn->ops->function(__mn,	\
+							    mm,		\
+							    args);	\
+			rcu_read_unlock();				\
+		}							\
+	} while (0)

The macro references its args more than once. Anyone who does

mmu_notifier(function, some_function_which_has_side_effects())

will get a surprise. Use temporaries.

Quoted 7 lines
include/linux/mmu_notifier.h new file +7 -0
+#else /* CONFIG_MMU_NOTIFIER */
+
+/*
+ * Notifiers that use the parameters that they were passed so that the
+ * compiler does not complain about unused variables but does proper
+ * parameter checks even if !CONFIG_MMU_NOTIFIER.
+ * Macros generate no code.

include/linux/mmu_notifier.h line 163

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+ */
+#define mmu_notifier(function, mm, args...)				\
+	do {								\
+		if (0) {						\
+			struct mmu_notifier *__mn;			\
+									\
+			__mn = (struct mmu_notifier *)(0x00ff);		\
+			__mn->ops->function(__mn, mm, args);		\
+		};							\
+	} while (0)

That's a bit weird. Can't we do the old

	(void)function;
	(void)mm;

trick? Or make it a staic inline function?

Quoted 21 lines
include/linux/mmu_notifier.h new file +16 -0
+static inline void mmu_notifier_register(struct mmu_notifier *mn,
+						struct mm_struct *mm) {}
+static inline void mmu_notifier_unregister(struct mmu_notifier *mn,
+						struct mm_struct *mm) {}
+static inline void mmu_notifier_release(struct mm_struct *mm) {}
+static inline int mmu_notifier_age_page(struct mm_struct *mm,
+				unsigned long address)
+{
+	return 0;
+}
+
+static inline void mmu_notifier_head_init(struct mmu_notifier_head *mmh) {}
+
+#endif /* CONFIG_MMU_NOTIFIER */
+
+#endif /* _LINUX_MMU_NOTIFIER_H */

Index: linux-2.6/mm/Kconfig
===================================================================

mm/Kconfig config NR_QUICK · line 199

Quoted 7 lines
mm/Kconfig +4 -0
@@ -193,3 +193,7 @@ config NR_QUICK
 config VIRT_TO_BUS
 	def_bool y
 	depends on !ARCH_NO_VIRT_TO_BUS
+
+config MMU_NOTIFIER
+	def_bool y
+	bool "MMU notifier, for paging KVM/RDMA"

Why is this not selectable? The help seems a bit brief.

Does this cause 32-bit systems to drag in a bunch of code they're not allowed to ever use?

Quoted 35 lines

Index: linux-2.6/mm/Makefile
===================================================================

mm/Makefile +1 -0
@@ -33,4 +33,5 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_SMP) += allocpercpu.o
 obj-$(CONFIG_QUICKLIST) += quicklist.o
 obj-$(CONFIG_CGROUP_MEM_CONT) += memcontrol.o
+obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o
 

Index: linux-2.6/mm/mmu_notifier.c
===================================================================

mm/mmu_notifier.c new file +20 -0
@@ -0,0 +1,76 @@
+/*
+ *  linux/mm/mmu_notifier.c
+ *
+ *  Copyright (C) 2008  Qumranet, Inc.
+ *  Copyright (C) 2008  SGI
+ *  		Christoph Lameter <[email protected]>
+ *
+ *  This work is licensed under the terms of the GNU GPL, version 2. See
+ *  the COPYING file in the top-level directory.
+ */
+
+#include <linux/module.h>
+#include <linux/mm.h>
+#include <linux/mmu_notifier.h>
+
+/*
+ * No synchronization. This function can only be called when only a single
+ * process remains that performs teardown.
+ */
+void mmu_notifier_release(struct mm_struct *mm)

mm/mmu_notifier.c line 30

Quoted 10 lines
mm/mmu_notifier.c new file +10 -0
+{
+	struct mmu_notifier *mn;
+	struct hlist_node *n, *t;
+
+	if (unlikely(!hlist_empty(&mm->mmu_notifier.head))) {
+		hlist_for_each_entry_safe(mn, n, t,
+					  &mm->mmu_notifier.head, hlist) {
+			hlist_del_init(&mn->hlist);
+			if (mn->ops->release)
+				mn->ops->release(mn, mm);

We do this a lot, but back in the old days people didn't like optional callbacks which can be NULL. If we expect that mmu_notifier_ops.release is usually implemented, the just unconditionally call it and require that all clients implement it. Perhaps provide an exported-to-modules stuv in core kernel for clients which didn't want to implement ->release().

Quoted 17 lines
mm/mmu_notifier.c new file +17 -0
+		}
+	}
+}
+
+/*
+ * If no young bitflag is supported by the hardware, ->age_page can
+ * unmap the address and return 1 or 0 depending if the mapping previously
+ * existed or not.
+ */
+int mmu_notifier_age_page(struct mm_struct *mm, unsigned long address)
+{
+	struct mmu_notifier *mn;
+	struct hlist_node *n;
+	int young = 0;
+
+	if (unlikely(!hlist_empty(&mm->mmu_notifier.head))) {
+		rcu_read_lock();

mm/mmu_notifier.c line 57

Quoted 10 lines
mm/mmu_notifier.c new file +10 -0
+		hlist_for_each_entry_rcu(mn, n,
+					  &mm->mmu_notifier.head, hlist) {
+			if (mn->ops->age_page)
+				young |= mn->ops->age_page(mn, mm, address);
+		}
+		rcu_read_unlock();
+	}
+
+	return young;
+}

should the rcu_read_lock() cover the hlist_empty() test?

This function looks like it was tossed in at the last minute. It's mysterious, undocumented, poorly commented, poorly named. A better name would be one which has some correlation with the return value.

Because anyone who looks at some code which does

	if (mmu_notifier_age_page(mm, address))
		...

has to go and reverse-engineer the implementation of mmu_notifier_age_page() to work out under which circumstances the "..." will be executed. But this should be apparent just from reading the callee implementation.

This function *really* does need some documentation. What does it *mean* when the ->age_page() from some of the notifiers returned "1" and the ->age_page() from some other notifiers returned zero? Dunno.

maintainer
#15 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 2 lines · On Thu, 14 Feb 2008 22:49:01 -0800 Christoph Lameter <[email protected]> wrote:

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

hm. Do they? Why? If I'm in the process of zero-copy writing a hunk of memory out to hardware then do I care if someone write-protects the ptes?

Spose so, but some fleshing-out of the various scenarios here would clarify things.

Quoted 3 lines

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

This is so bad.

I supposed in the restricted couple of cases which you're focussed on it works OK. But is it generally suitable? What if IO is in progress? What if other cluster nodes need to be talked to? Does it suit RDMA?

Quoted 3 lines

In two cases we use invalidate_range_begin/end to invalidate single pages because the pair allows holding off new references (idea by Robin Holt).

Assuming that there is a missing "within the range" in this description, I assume that all clients will just throw up theior hands in horror and will disallow all references to all parts of the mm.

Of course, to do that they will need to take a sleeping lock to prevent other threads from establishing new references. whoops.

Quoted 5 lines

do_wp_page(): We hold off new references while we update the pte.

xip_unmap: We are not taking the PageLock so we cannot
use the invalidate_page mmu_rmap_notifier. invalidate_range_begin/end stands in.

What does "stands in" mean?

Quoted 27 lines
---
 mm/filemap_xip.c |    5 +++++
 mm/fremap.c      |    3 +++
 mm/hugetlb.c     |    3 +++
 mm/memory.c      |   35 +++++++++++++++++++++++++++++------
 mm/mmap.c        |    2 ++
 mm/mprotect.c    |    3 +++
 mm/mremap.c      |    7 ++++++-
 7 files changed, 51 insertions(+), 7 deletions(-)

Index: linux-2.6/mm/fremap.c
===================================================================

mm/fremap.c +1 -0
@@ -15,6 +15,7 @@
 #include <linux/rmap.h>
 #include <linux/module.h>
 #include <linux/syscalls.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/mmu_context.h>
 #include <asm/cacheflush.h>

mm/fremap.c asmlinkage long sys_remap_file_pages(uns · line 220

Quoted 6 lines
mm/fremap.c +2 -0
@@ -214,7 +215,9 @@ asmlinkage long sys_remap_file_pages(uns
 		spin_unlock(&mapping->i_mmap_lock);
 	}
 
+	mmu_notifier(invalidate_range_begin, mm, start, start + size, 0);
 	err = populate_range(mm, vma, start, size, pgoff);
+	mmu_notifier(invalidate_range_end, mm, start, start + size, 0);

To avoid off-by-one confusion the changelogs, documentation and comments should be very careful to tell the reader whether the range includes the byte at start+size. I don't thik that was done?

maintainer
#56 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Christoph Lameter
Quoted 6 lines · On Thu, 14 Feb 2008 22:49:02 -0800 Christoph Lameter <[email protected]> wrote:

Two callbacks to remove individual pages as done in rmap code

invalidate_page()

Called from the inner loop of rmap walks to invalidate pages.

Quoted 7 lines
	age_page()

Called for the determination of the page referenced status.

If we do not care about page referenced status then an age_page callback may be be omitted. PageLock and pte lock are held when either of the functions is called.

The age_page mystery shallows.

It would be useful to have some rationale somewhere in the patchset for the existence of this callback.

mm/rmap.c static int page_referenced_one(struct pa · line 293

Quoted 10 lines

#include <asm/tlbflush.h>

mm/rmap.c +2 -1
@@ -287,7 +288,8 @@ static int page_referenced_one(struct pa
 	if (vma->vm_flags & VM_LOCKED) {
 		referenced++;
 		*mapcount = 1;	/* break early from loop */
-	} else if (ptep_clear_flush_young(vma, address, pte))
+	} else if (ptep_clear_flush_young(vma, address, pte) |
+		   mmu_notifier_age_page(mm, address))
 		referenced++;

The "|" is obviously deliberate. But no explanation is provided telling us why we still call the callback if ptep_clear_flush_young() said the page was recently referenced. People who read your code will want to understand this.

mm/rmap.c static int page_mkclean_one(struct page · line 460

Quoted 6 lines
mm/rmap.c +1 -0
 	/* Pretend the page is referenced if the task has the
@@ -455,6 +457,7 @@ static int page_mkclean_one(struct page 
 
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		entry = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);

I just don't see how ths can be done if the callee has another thread in the middle of establishing IO against this region of memory. ->invalidate_page() _has_ to be able to block. Confused.

maintainer
#65 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Christoph Lameter
Quoted 21 lines · On Thu, 14 Feb 2008 22:49:04 -0800 Christoph Lameter <[email protected]> wrote:

These special additional callbacks are required because XPmem (and likely other mechanisms) do use their own rmap (multiple processes on a series of remote Linux instances may be accessing the memory of a process). F.e. XPmem may have to send out notifications to remote Linux instances and receive confirmation before a page can be freed.

So we handle this like an additional Linux reverse map that is walked after the existing rmaps have been walked. We leave the walking to the driver that is then able to use something else than a spinlock to walk its reverse maps. So we can actually call the driver without holding spinlocks while we hold the Pagelock.

However, we cannot determine the mm_struct that a page belongs to at that point. The mm_struct can only be determined from the rmaps by the device driver.

We add another pageflag (PageExternalRmap) that is set if a page has been remotely mapped (f.e. by a process from another Linux instance). We can then only perform the callbacks for pages that are actually in remote use.

Quoted 6 lines

Rmap notifiers need an extra page bit and are only available on 64 bit platforms. This functionality is not available on 32 bit!

A notifier that uses the reverse maps callbacks does not need to provide the invalidate_page() method that is called when locks are held.

hrm.

include/linux/mmu_notifier.h static inline void mmu_notifier_head_ini · line 198

Quoted 13 lines
include/linux/mmu_notifier.h +13 -0
+#define mmu_rmap_notifier(function, args...)				\
+	do {								\
+		struct mmu_rmap_notifier *__mrn;			\
+		struct hlist_node *__n;					\
+									\
+		rcu_read_lock();					\
+		hlist_for_each_entry_rcu(__mrn, __n,			\
+				&mmu_rmap_notifier_list, hlist)		\
+			if (__mrn->ops->function)			\
+				__mrn->ops->function(__mrn, args);	\
+		rcu_read_unlock();					\
+	} while (0);
+

buggy macro: use locals.

include/linux/mmu_notifier.h static inline void mmu_notifier_head_ini · line 226

Quoted 10 lines
include/linux/mmu_notifier.h +10 -0
+#define mmu_rmap_notifier(function, args...)				\
+	do {								\
+		if (0) {						\
+			struct mmu_rmap_notifier *__mrn;		\
+									\
+			__mrn = (struct mmu_rmap_notifier *)(0x00ff);	\
+			__mrn->ops->function(__mrn, args);		\
+		}							\
+	} while (0);
+

Same observation as in the other patch.

Quoted 29 lines

===================================================================

mm/mmu_notifier.c +21 -0
@@ -74,3 +74,37 @@ void mmu_notifier_unregister(struct mmu_
 }
 EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
 
+#ifdef CONFIG_64BIT
+static DEFINE_SPINLOCK(mmu_notifier_list_lock);
+HLIST_HEAD(mmu_rmap_notifier_list);
+
+void mmu_rmap_notifier_register(struct mmu_rmap_notifier *mrn)
+{
+	spin_lock(&mmu_notifier_list_lock);
+	hlist_add_head_rcu(&mrn->hlist, &mmu_rmap_notifier_list);
+	spin_unlock(&mmu_notifier_list_lock);
+}
+EXPORT_SYMBOL(mmu_rmap_notifier_register);
+
+void mmu_rmap_notifier_unregister(struct mmu_rmap_notifier *mrn)
+{
+	spin_lock(&mmu_notifier_list_lock);
+	hlist_del_rcu(&mrn->hlist);
+	spin_unlock(&mmu_notifier_list_lock);
+}
+EXPORT_SYMBOL(mmu_rmap_notifier_unregister);
 
+/*
+ * Export a page.

mm/mmu_notifier.c void mmu_notifier_unregister(struct mmu_ · line 108

Quoted 10 lines
mm/mmu_notifier.c +10 -0
+ *
+ * Pagelock must be held.
+ * Must be called before a page is put on an external rmap.
+ */
+void mmu_rmap_export_page(struct page *page)
+{
+	BUG_ON(!PageLocked(page));
+	SetPageExternalRmap(page);
+}
+EXPORT_SYMBOL(mmu_rmap_export_page);

The other patch used EXPORT_SYMBOL_GPL.

#3 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

How important is this feature to KVM?

Very. kvm pins pages that are referenced by the guest; a 64-bit guest will easily pin its entire memory with the kernel map. So this is critical for guest swapping to actually work.

Other nice features like page migration are also enabled by this patch.

Signature · 7 lines
Any sufficiently difficult bug is indistinguishable from a feature.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
maintainer
#4 lore raw

Re: [patch 1/6] mmu_notifier: Core code

Quoted 5 lines · On Sat, 16 Feb 2008 10:45:50 +0200 Avi Kivity <[email protected]> wrote:
Quoted 2 lines · Andrew Morton

How important is this feature to KVM?

Very. kvm pins pages that are referenced by the guest;

hm. Why does it do that?

Quoted 5 lines

a 64-bit guest
will easily pin its entire memory with the kernel map.

So this is
critical for guest swapping to actually work.

Curious. If KVM can release guest pages at the request of this notifier so that they can be swapped out, why can't it release them by default, and allow swapping to proceed?

Quoted 3 lines

Other nice features like page migration are also enabled by this patch.

We already have page migration. Do you mean page-migration-when-using-kvm?

#5 lore raw

Re: [patch 1/6] mmu_notifier: Core code

Quoted 6 lines · Andrew Morton
Quoted 2 lines

Very. kvm pins pages that are referenced by the guest;

hm. Why does it do that?

It was deemed best not to allow the guest to write to a page that has been swapped out and assigned to an unrelated host process.

One way to view the kvm shadow page tables is as hardware dma descriptors. kvm pins pages for the same reason that drivers pin pages that are being dma'ed. It's also the reason why mmu notifiers are useful for such a wide range of dma capable hardware.

Quoted 5 lines
Quoted 3 lines

a 64-bit guest
will easily pin its entire memory with the kernel map.

Quoted 9 lines
Quoted 3 lines

So this is
critical for guest swapping to actually work.

Curious. If KVM can release guest pages at the request of this notifier so that they can be swapped out, why can't it release them by default, and allow swapping to proceed?

If kvm releases a page, it must also zap any shadow ptes pointing at the page and flush the tlb. If you do that for all of memory you can't reference any of it.

Releasing a page has costs, both at the time of the release and when the guest eventually refers to the page again.

Quoted 6 lines
Quoted 3 lines

Other nice features like page migration are also enabled by this patch.

We already have page migration. Do you mean page-migration-when-using-kvm?

Yes, I'm obviously writing from a kvm-centric point of view. This is an important feature, as the virtualization future seems to be NUMA hosts (2- or 4- way, 4 cores per socket) running moderately sized guests. The ability to load-balance guests among the NUMA nodes is important for performance.

(btw, I'm also looking forward to memory defragmentation. large pages are important for virtualization workloads and mmu notifiers are again critical to getting it to work while running kvm).

Signature · 7 lines
Any sufficiently difficult bug is indistinguishable from a feature.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#6 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrew Morton
Quoted 9 lines · Andrew Morton

What is the status of getting infiniband to use this facility?

How important is this feature to KVM?

To xpmem?

Which other potential clients have been identified and how important it it to those?

As I said when Andrea posted the first patch series, I used something very similar for non-RDMA-based HPC about 4 years ago. I haven't had time yet to look in depth and try the latest proposed API but my feeling is that it looks good.

Brice

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#85 lore raw

PATCH 1/1 KVM swapping with MMU Notifiers V7

In reply to Christoph Lameter

Those below two patches enable KVM to swap the guest physical memory through Christoph's V7.

There's one last _purely_theoretical_ race condition I figured out and that I'm wondering how to best fix. The race condition worst case is that a few guest physical pages could remain pinned by sptes. The race can materialize if the linux pte is zapped after get_user_pages returns but before the page is mapped by the spte and tracked by rmap. The invalidate_ calls can also likely be optimized further but it's not a fast path so it's not urgent.

arch/x86/kvm/Kconfig +1 -0
index 41962e7..e1287ab 100644
@@ -21,6 +21,7 @@ config KVM
 	tristate "Kernel-based Virtual Machine (KVM) support"
 	depends on HAVE_KVM && EXPERIMENTAL
 	select PREEMPT_NOTIFIERS
+	select MMU_NOTIFIER
 	select ANON_INODES
 	---help---
 	  Support hosting fully virtualized guest machines using hardware
arch/x86/kvm/mmu.c +104 -0

110 lines not on this page · raw message

arch/x86/kvm/x86.c +43 -0

55 lines not on this page · raw message

include/asm-x86/kvm_host.h +5 -0
index da61255..11976c8 100644
@@ -13,6 +13,7 @@
 
 #include <linux/types.h>
 #include <linux/mm.h>
+#include <linux/mmu_notifier.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
@@ -287,6 +288,8 @@ struct kvm_arch{
 	int round_robin_prev_vcpu;
 	unsigned int tss_addr;
 	struct page *apic_access_page;
+
+	struct mmu_notifier mmu_notifier;
 };
 
 struct kvm_vm_stat {
@@ -404,6 +407,8 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu);
 int kvm_mmu_setup(struct kvm_vcpu *vcpu);
 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
 
+void kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
+int kvm_age_hva(struct kvm *kvm, unsigned long hva);
 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
 void kvm_mmu_zap_all(struct kvm *kvm);

This allows to browse the memslots with only the mmu_lock hold and it should be applied along the above patch:

arch/x86/kvm/x86.c +14 -7
index 0c910c7..80b719d 100644
@@ -3245,16 +3245,23 @@ int kvm_arch_set_memory_region(struct kvm *kvm,
 	 */
 	if (!user_alloc) {
 		if (npages && !old.rmap) {
+			unsigned long userspace_addr;
+
 			down_write(&current->mm->mmap_sem);
-			memslot->userspace_addr = do_mmap(NULL, 0,
-						     npages * PAGE_SIZE,
-						     PROT_READ | PROT_WRITE,
-						     MAP_SHARED | MAP_ANONYMOUS,
-						     0);
+			userspace_addr = do_mmap(NULL, 0,
+						 npages * PAGE_SIZE,
+						 PROT_READ | PROT_WRITE,
+						 MAP_SHARED | MAP_ANONYMOUS,
+						 0);
 			up_write(&current->mm->mmap_sem);
 
-			if (IS_ERR((void *)memslot->userspace_addr))
-				return PTR_ERR((void *)memslot->userspace_addr);
+			if (IS_ERR((void *)userspace_addr))
+				return PTR_ERR((void *)userspace_addr);
+
+			/* set userspace_addr atomically for kvm_hva_to_rmapp */
+			spin_lock(&kvm->mmu_lock);
+			memslot->userspace_addr = userspace_addr;
+			spin_unlock(&kvm->mmu_lock);
 		} else {
 			if (!old.user_alloc && old.rmap) {
 				int ret;
virt/kvm/kvm_main.c +13 -1
index cf6df51..743c5c5 100644
@@ -299,7 +299,15 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.rmap, 0, npages * sizeof(*new.rmap));
 
 		new.user_alloc = user_alloc;
-		new.userspace_addr = mem->userspace_addr;
+		/*
+		 * hva_to_rmmap() serialzies with the mmu_lock and to be
+		 * safe it has to ignore memslots with !user_alloc &&
+		 * !userspace_addr.
+		 */
+		if (user_alloc)
+			new.userspace_addr = mem->userspace_addr;
+		else
+			new.userspace_addr = 0;
 	}
 
 	/* Allocate page dirty bitmap if needed */
@@ -312,14 +320,18 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.dirty_bitmap, 0, dirty_bytes);
 	}
 
+	spin_lock(&kvm->mmu_lock);
 	if (mem->slot >= kvm->nmemslots)
 		kvm->nmemslots = mem->slot + 1;
 
 	*memslot = new;
+	spin_unlock(&kvm->mmu_lock);
 
 	r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
 	if (r) {
+		spin_lock(&kvm->mmu_lock);
 		*memslot = old;
+		spin_unlock(&kvm->mmu_lock);
 		goto out_free;
 	}
 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

maintainer
#7 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Brice Goglin
Quoted 7 lines · On Sat, 16 Feb 2008 11:41:35 +0100 Brice Goglin <[email protected]> wrote:
Quoted 6 lines · Andrew Morton

What is the status of getting infiniband to use this facility?

How important is this feature to KVM?

To xpmem?

Quoted 9 lines
Quoted 3 lines

Which other potential clients have been identified and how important it it to those?

As I said when Andrea posted the first patch series, I used something very similar for non-RDMA-based HPC about 4 years ago. I haven't had time yet to look in depth and try the latest proposed API but my feeling is that it looks good.

"looks good" maybe. But it's in the details where I fear this will come unstuck. The likelihood that some callbacks really will want to be able to block in places where this interface doesn't permit that - either to wait for IO to complete or to wait for other threads to clear critical regions.

Quoted 1 line

From that POV it doesn't look like a sufficiently general and useful

design. Looks like it was grafted onto the current VM implementation in a way which just about suits two particular clients if they try hard enough.

Which is all perfectly understandable - it would be hard to rework core MM to be able to make this interface more general. But I do think it's half-baked and there is a decent risk that future (or present) code which _could_ use something like this won't be able to use this one, and will continue to futz with mlock, page-pinning, etc.

Not that I know what the fix to that is..

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#57 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Andrew Morton
Quoted 4 lines · Andrew Morton

The "|" is obviously deliberate. But no explanation is provided telling us why we still call the callback if ptep_clear_flush_young() said the page was recently referenced. People who read your code will want to understand this.

This is to clear the young bit in every pte and spte to such physical page before backing off because any young bit was on. So if any young bit will be on in the next scan, we're guaranteed the page has been touched recently and not ages before (otherwise it would take a worst case N rounds of the lru before the page can be freed, where N is the number of pte or sptes pointing to the page).

Quoted 3 lines

I just don't see how ths can be done if the callee has another thread in the middle of establishing IO against this region of memory. ->invalidate_page() _has_ to be able to block. Confused.

invalidate_page marking the spte invalid and flushing the asid/tlb doesn't need to block the same way ptep_clear_flush doesn't need to block for the main linux pte. Infact before invalidate_page and ptep_clear_flush can touch anything at all, they've to take their own spinlocks (mmu_lock for the former, and PT lock for the latter).

The only sleeping trouble is for networked driven message passing, where they want to schedule while they wait the message to arrive or it'd hang the whole cpu to spin for so long.

sptes are cpu-clocked entities like ptes so scheduling there is by far not necessary because there's zero delay in invalidating them and flushing their tlbs. GRU is similar. Because we boost the reference count of the pages for every spte mapping, only implementing invalidate_range_end is enough, but I need to figure out the get_user_pages->rmap_add window too and because get_user_pages can schedule, and if I want to add a critical section around it to avoid calling get_user_pages twice during the kvm page fault, a mutex would be the only way (it sure can't be a spinlock). But a mutex can't be taken by invalidate_page to stop it. So that leaves me with the idea of adding a get_user_pages variant that returns the page locked. So instead of calling get_user_pages a second time after rmap_add returns, I will only need to call unlock_page which should be faster than a follow_page. And setting the PG_lock before dropping the PT lock in follow_page, should be fast enough too.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

maintainer
#86 lore raw

Re: [PATCH] KVM swapping with MMU Notifiers V7

In reply to Andrea Arcangeli
Quoted 4 lines · On Sat, 16 Feb 2008 11:48:27 +0100 Andrea Arcangeli <[email protected]> wrote:
arch/x86/kvm/x86.c +4 -0
+void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
+					   struct mm_struct *mm,
+					   unsigned long start, unsigned long end,
+					   int lock)

arch/x86/kvm/x86.c void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) · line 3226

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+{
+	for (; start < end; start += PAGE_SIZE)
+		kvm_mmu_notifier_invalidate_page(mn, mm, start);
+}
+
+static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
+	.invalidate_page	= kvm_mmu_notifier_invalidate_page,
+	.age_page		= kvm_mmu_notifier_age_page,
+	.invalidate_range_end	= kvm_mmu_notifier_invalidate_range_end,
+};

So this doesn't implement ->invalidate_range_start().

By what means does it prevent new mappings from being established in the range after core mm has tried to call ->invalidate_rande_start()? mmap_sem, I assume?

arch/x86/kvm/x86.c int kvm_arch_set_memory_region(struct kvm *kvm, · line 3264

Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+			/* set userspace_addr atomically for kvm_hva_to_rmapp */
+			spin_lock(&kvm->mmu_lock);
+			memslot->userspace_addr = userspace_addr;
+			spin_unlock(&kvm->mmu_lock);

are you sure? kvm_unmap_hva() and kvm_age_hva() read ->userspace_addr a single time and it doesn't immediately look like there's a need to take the lock here?

#88 lore raw

Re: [PATCH] KVM swapping with MMU Notifiers V7

In reply to Andrea Arcangeli
Quoted 10 lines · Andrea Arcangeli

Those below two patches enable KVM to swap the guest physical memory through Christoph's V7.

There's one last _purely_theoretical_ race condition I figured out and that I'm wondering how to best fix. The race condition worst case is that a few guest physical pages could remain pinned by sptes. The race can materialize if the linux pte is zapped after get_user_pages returns but before the page is mapped by the spte and tracked by rmap. The invalidate_ calls can also likely be optimized further but it's not a fast path so it's not urgent.

I am doing this in xpmem with a stack-based structure in the function calling get_user_pages. That structure describes the start and end address of the range we are doing the get_user_pages on. If an invalidate_range_begin comes in while we are off to the kernel doing the get_user_pages, the invalidate_range_begin marks that structure indicating an invalidate came in. When the get_user_pages gets the structures relocked, it checks that flag (really a generation counter) and if it is set, retries the get_user_pages. After 3 retries, it returns -EAGAIN and the fault is started over from the remote side.

Thanks,
Robin

#9 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrew Morton
Quoted 1 line · Andrew Morton

What is the status of getting infiniband to use this facility?

Well we are talking about this it seems.

Quoted 2 lines

How important is this feature to KVM?

Andrea can answer this.

Quoted 1 line

To xpmem?

Without this feature we are stuck with page pinning by increasing refcounts which leads to endless lru scanning and other misbehavior. Also applications that use XPmem will not be able to swap or be able to use things like remap.

Quoted 2 lines

Which other potential clients have been identified and how important it it to those?

It is likely important to various DMA engines, framebuffers devices etc etc. Seems to be a generally useful feature.

Quoted 9 lines

Documentation/mmu_notifier/README line 46

Quoted 6 lines
Documentation/mmu_notifier/README new file +6 -0
+The notifier chains provide two callback mechanisms. The
+first one is required for any device that establishes external mappings.
+The second (rmap) mechanism is required if a device needs to be
+able to sleep when invalidating references. Sleeping may be necessary
+if we are mapping across a network or to different Linux instances
+in the same address space.

I'd have thought that a major reason for sleeping would be to wait for IO to complete. Worth mentioning here?

Right.

Quoted 3 lines

Why is that "easy"? I's have thought that it would only be easy if the driver happened to be using those same locks for its own purposes. Otherwise it is "awkward"?

Its relatively easy because it is tied directly to a process and can use external tlb shootdown / external page table clearing directly. The other method requires an rmap in the device driver where it can lookup the processes that are mapping the page.

Quoted 8 lines

Documentation/mmu_notifier/README line 62

Quoted 5 lines
Documentation/mmu_notifier/README new file +5 -0
+The invalidation mechanism for a range (*invalidate_range_begin/end*) is
+called most of the time without any locks held. It is only called with
+locks held for file backed mappings that are truncated. A flag indicates
+in which mode we are. A driver can use that mechanism to f.e.
+delay the freeing of the pages during truncate until no locks are held.

That sucks big time. What do we need to do to make get the callback functions called in non-atomic context?

We would have to drop the inode_mmap_lock. Could be done with some minor work.

Quoted 4 lines

Documentation/mmu_notifier/README line 65

Quoted 2 lines
Documentation/mmu_notifier/README new file +2 -0
+Pages must be marked dirty if dirty bits are found to be set in
+the external ptes during unmap.

That sentence is too vague. Define "marked dirty"?

Call set_page_dirty().

Quoted 4 lines
Quoted 1 line

+The *release* method is called when a Linux process exits. It is run before

We'd conventionally use a notation such as "->release()" here, rather than the asterisks.

Ok.

Quoted 6 lines

Documentation/mmu_notifier/README line 69

Quoted 3 lines
Documentation/mmu_notifier/README new file +2 -0
+the pages and mappings of a process are torn down and gives the device driver
+a chance to zap all the external mappings in one go.

I assume what you mean here is that ->release() is called during exit() when the final reference to an mm is being dropped.

Right.

Quoted 5 lines

Documentation/mmu_notifier/README line 73

Quoted 3 lines
Documentation/mmu_notifier/README new file +3 -0
+An example for a code that can be used to build a notifier mechanism into
+a device driver can be found in the file
+Documentation/mmu_notifier/skeleton.c

Should that be in samples/?

Oh. We have that?

Quoted 6 lines

Documentation/mmu_notifier/README line 86

Quoted 4 lines
Documentation/mmu_notifier/README new file +4 -0
+The mmu_rmap_notifier adds another invalidate_page() callout that is called
+*before* the Linux rmaps are walked. At that point only the page lock is
+held. The invalidate_page() function must walk the driver rmaps and evict
+all the references to the page.

What happens if it cannot do so?

The page is not reclaimed if we were called from try_to_unmap(). From page_mkclean() we must always evict the page to switch off the write protect bit.

Quoted 3 lines
Quoted 1 line

+There is no process information available before the rmaps are consulted.

Not sure what that sentence means. I guess "available to the core VM"?

At that point we only have the page. We do not know which processes map the page. In order to find out we need to take a spinlock.

Quoted 6 lines

Documentation/mmu_notifier/README

Quoted 4 lines
Documentation/mmu_notifier/README new file +4 -0
+The notifier mechanism can therefore not be attached to an mm_struct. Instead
+it is a global callback list. Having to perform a callback for each and every
+page that is reclaimed would be inefficient. Therefore we add an additional
+page flag: PageRmapExternal().

How many page flags are left?

30 or so. Its only available on 64bit.

Quoted 7 lines

Is this feature important enough to justfy consumption of another one?

Documentation/mmu_notifier/README line 94

Quoted 3 lines

Only pages that are marked with this bit can

Documentation/mmu_notifier/README new file +2 -0
+be exported and the rmap callbacks will only be performed for pages marked
+that way.

"exported": new term, unclear what it means.

Something external to the kernel references the page.

Quoted 5 lines

Documentation/mmu_notifier/README line 97

Quoted 2 lines
Documentation/mmu_notifier/README new file +2 -0
+The required additional Page flag is only availabe in 64 bit mode and
+therefore the mmu_rmap_notifier portion is not available on 32 bit platforms.

whoa. Is that good? You just made your feature unavailable on the great majority of Linux systems.

rmaps are usually used by complex drivers that are typically used in large systems.

Quoted 8 lines
Quoted 4 lines

+ * Notifier functions for hardware and software that establishes external
+ * references to pages of a Linux system. The notifier calls ensure that
+ * external mappings are removed when the Linux VM removes memory ranges
+ * or individual pages from a process.

So the callee cannot fail. hm. If it can't block, it's likely screwed in that case. In other cases it might be screwed anyway. I suspect we'll need to be able to handle callee failure.

Probably.

Quoted 9 lines
Quoted 7 lines

+ * These fall into two classes:
+ *
+ * 1. mmu_notifier
+ *
+ * These are callbacks registered with an mm_struct. If pages are
+ * removed from an address space then callbacks are performed.

"to be removed", I guess. It's called before the page is actually removed?

Its called after the pte was cleared while holding the pte lock.

Quoted 6 lines
Quoted 3 lines

+ * The invalidate_range_start/end callbacks can be performed in contexts
+ * where sleeping is allowed or in atomic contexts. A flag is passed
+ * to indicate an atomic context.

We generally would prefer separate callbacks, rather than a unified callback with a mode flag.

We could drop the inode_mmap_lock when doing truncate. That would make this work but its a kind of invasive thing for the VM.

Quoted 6 lines

include/linux/mmu_notifier.h line 45

Quoted 4 lines
include/linux/mmu_notifier.h new file +4 -0
+struct mmu_notifier_ops {
+	/*
+	 * The release notifier is called when no other execution threads
+	 * are left. Synchronization is not necessary.

"and the mm is about to be destroyed"?

Right.

Quoted 7 lines

include/linux/mmu_notifier.h

Quoted 5 lines
include/linux/mmu_notifier.h new file +5 -0
+	/*
+	 * invalidate_range_begin() and invalidate_range_end() must be paired.
+	 *
+	 * Multiple invalidate_range_begin/ends may be nested or called
+	 * concurrently.

Under what circumstances would they be nested?

Hmmmm.. Right they cannot be nested. Multiple processors can have invalidates() concurrently in progress.

Quoted 3 lines
Quoted 1 line

That is legit. However, no new external references

references to what?

To the ranges that are in the process of being invalidated.

Quoted 4 lines

include/linux/mmu_notifier.h line 76

Quoted 2 lines
include/linux/mmu_notifier.h new file +2 -0
+	 * invalidate_range_begin() must clear all references in the range
+	 * and stop the establishment of new references.

and stop the establishment of new references within the range, I assume?

Right.

Quoted 3 lines

If so, that's putting a heck of a lot of complexity into the driver, isn't it? It needs to temporarily remember an arbitrarily large number of regions in this mm against which references may not be taken?

That is one implementation (XPmem does that). The other is to simply stop all references when any invalidate_range is in progress (KVM and GRU do that).

Quoted 3 lines

include/linux/mmu_notifier.h line 78

Quoted 1 line
include/linux/mmu_notifier.h new file +1 -0
+	 * invalidate_range_end() reenables the establishment of references.

within the range?

Right.

Quoted 5 lines

include/linux/mmu_notifier.h line 122

Quoted 3 lines
include/linux/mmu_notifier.h new file +3 -0
+extern void mmu_notifier_release(struct mm_struct *mm);
+extern int mmu_notifier_age_page(struct mm_struct *mm,
+				 unsigned long address);

There's the mysterious age_page again.

Andrea put this in to check the reference status of a page. It functions like the accessed bit.

Quoted 25 lines
Quoted 12 lines
include/linux/mmu_notifier.h new file +12 -0
+static inline void mmu_notifier_head_init(struct mmu_notifier_head *mnh)
+{
+	INIT_HLIST_HEAD(&mnh->head);
+}
+
+#define mmu_notifier(function, mm, args...)				\
+	do {								\
+		struct mmu_notifier *__mn;				\
+		struct hlist_node *__n;					\
+									\
+		if (unlikely(!hlist_empty(&(mm)->mmu_notifier.head))) { \
+			rcu_read_lock();				\

include/linux/mmu_notifier.h line 145

Quoted 10 lines
include/linux/mmu_notifier.h new file +10 -0
+			hlist_for_each_entry_rcu(__mn, __n,		\
+					     &(mm)->mmu_notifier.head,	\
+					     hlist)			\
+				if (__mn->ops->function)		\
+					__mn->ops->function(__mn,	\
+							    mm,		\
+							    args);	\
+			rcu_read_unlock();				\
+		}							\
+	} while (0)

The macro references its args more than once. Anyone who does

Quoted 3 lines

mmu_notifier(function, some_function_which_has_side_effects())

will get a surprise. Use temporaries.

Ok.

Quoted 12 lines

include/linux/mmu_notifier.h line 163

Quoted 9 lines
include/linux/mmu_notifier.h new file +9 -0
+#define mmu_notifier(function, mm, args...)				\
+	do {								\
+		if (0) {						\
+			struct mmu_notifier *__mn;			\
+									\
+			__mn = (struct mmu_notifier *)(0x00ff);		\
+			__mn->ops->function(__mn, mm, args);		\
+		};							\
+	} while (0)

That's a bit weird. Can't we do the old

Quoted 4 lines
	(void)function;
	(void)mm;

trick? Or make it a staic inline function?

Static inline wont allow the checking of the parameters.

(void) may be a good thing here.

Quoted 8 lines

mm/Kconfig config NR_QUICK · line 199

Quoted 3 lines
mm/Kconfig +3 -0
+config MMU_NOTIFIER
+	def_bool y
+	bool "MMU notifier, for paging KVM/RDMA"

Why is this not selectable? The help seems a bit brief.

Does this cause 32-bit systems to drag in a bunch of code they're not allowed to ever use?

I have selected it a number of times. We could make that a bit longer right.

Quoted 4 lines
Quoted 4 lines
mm/mmu_notifier.c new file +4 -0
+	if (unlikely(!hlist_empty(&mm->mmu_notifier.head))) {
+		hlist_for_each_entry_safe(mn, n, t,
+					  &mm->mmu_notifier.head, hlist) {
+			hlist_del_init(&mn->hlist);
Quoted 8 lines

mm/mmu_notifier.c line 30

Quoted 2 lines
mm/mmu_notifier.c new file +2 -0
+			if (mn->ops->release)
+				mn->ops->release(mn, mm);

We do this a lot, but back in the old days people didn't like optional callbacks which can be NULL. If we expect that mmu_notifier_ops.release is usually implemented, the just unconditionally call it and require that all clients implement it. Perhaps provide an exported-to-modules stuv in core kernel for clients which didn't want to implement ->release().

Ok.

Quoted 29 lines
Quoted 7 lines
mm/mmu_notifier.c new file +7 -0
+{
+	struct mmu_notifier *mn;
+	struct hlist_node *n;
+	int young = 0;
+
+	if (unlikely(!hlist_empty(&mm->mmu_notifier.head))) {
+		rcu_read_lock();

mm/mmu_notifier.c line 57

Quoted 10 lines
mm/mmu_notifier.c new file +10 -0
+		hlist_for_each_entry_rcu(mn, n,
+					  &mm->mmu_notifier.head, hlist) {
+			if (mn->ops->age_page)
+				young |= mn->ops->age_page(mn, mm, address);
+		}
+		rcu_read_unlock();
+	}
+
+	return young;
+}

should the rcu_read_lock() cover the hlist_empty() test?

This function looks like it was tossed in at the last minute. It's mysterious, undocumented, poorly commented, poorly named. A better name would be one which has some correlation with the return value.

Because anyone who looks at some code which does

	if (mmu_notifier_age_page(mm, address))
		...
Quoted 8 lines

has to go and reverse-engineer the implementation of mmu_notifier_age_page() to work out under which circumstances the "..." will be executed. But this should be apparent just from reading the callee implementation.

This function *really* does need some documentation. What does it *mean* when the ->age_page() from some of the notifiers returned "1" and the ->age_page() from some other notifiers returned zero? Dunno.

Andrea: Could you provide some more detail here?

#58 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Andrew Morton
Quoted 5 lines · Andrew Morton
Quoted 5 lines
mm/rmap.c +0 -1
@@ -287,7 +288,8 @@ static int page_referenced_one(struct pa
 	if (vma->vm_flags & VM_LOCKED) {
 		referenced++;
 		*mapcount = 1;	/* break early from loop */
-	} else if (ptep_clear_flush_young(vma, address, pte))
Quoted 8 lines

mm/rmap.c static int page_referenced_one(struct pa · line 293

Quoted 3 lines
mm/rmap.c +2 -0
+	} else if (ptep_clear_flush_young(vma, address, pte) |
+		   mmu_notifier_age_page(mm, address))
 		referenced++;

The "|" is obviously deliberate. But no explanation is provided telling us why we still call the callback if ptep_clear_flush_young() said the page was recently referenced. People who read your code will want to understand this.

Andrea?

Quoted 7 lines

mm/rmap.c static int page_mkclean_one(struct page · line 460

Quoted 3 lines
mm/rmap.c +1 -0
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		entry = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);

I just don't see how ths can be done if the callee has another thread in the middle of establishing IO against this region of memory. ->invalidate_page() _has_ to be able to block. Confused.

The page lock is held and that holds off I/O?

#16 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrew Morton
Quoted 10 lines · Andrew Morton
Quoted 2 lines · On Thu, 14 Feb 2008 22:49:01 -0800 Christoph Lameter <[email protected]> wrote:

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

hm. Do they? Why? If I'm in the process of zero-copy writing a hunk of memory out to hardware then do I care if someone write-protects the ptes?

Spose so, but some fleshing-out of the various scenarios here would clarify things.

You care f.e. if the VM needs to writeprotect a memory range and a write occurs. In that case the VM needs to be proper write processing and write through an external pte would cause memory corruption.

Quoted 5 lines
Quoted 3 lines

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

This is so bad.

Ok so I can twidlle around with the inode_mmap_lock to drop it while this is called?

Quoted 7 lines
Quoted 3 lines

In two cases we use invalidate_range_begin/end to invalidate single pages because the pair allows holding off new references (idea by Robin Holt).

Assuming that there is a missing "within the range" in this description, I assume that all clients will just throw up theior hands in horror and will disallow all references to all parts of the mm.

Right. Missing within the range. We only need to disallow creating new ptes right? Why disallow references?

Quoted 5 lines
Quoted 3 lines

xip_unmap: We are not taking the PageLock so we cannot
use the invalidate_page mmu_rmap_notifier. invalidate_range_begin/end stands in.

What does "stands in" mean?

Use a range begin / end to invalidate a page.

Quoted 7 lines

mm/fremap.c asmlinkage long sys_remap_file_pages(uns · line 220

Quoted 3 lines
mm/fremap.c +2 -0
+	mmu_notifier(invalidate_range_begin, mm, start, start + size, 0);
 	err = populate_range(mm, vma, start, size, pgoff);
+	mmu_notifier(invalidate_range_end, mm, start, start + size, 0);

To avoid off-by-one confusion the changelogs, documentation and comments should be very careful to tell the reader whether the range includes the byte at start+size. I don't thik that was done?

No it was not. I assumed that the convention is always start - (end - 1) and the byte at end is not affected by the operation.

#66 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Andrew Morton
Quoted 7 lines · Andrew Morton
Quoted 7 lines
include/linux/mmu_notifier.h +7 -0
+#define mmu_rmap_notifier(function, args...)				\
+	do {								\
+		struct mmu_rmap_notifier *__mrn;			\
+		struct hlist_node *__n;					\
+									\
+		rcu_read_lock();					\
+		hlist_for_each_entry_rcu(__mrn, __n,			\
Quoted 8 lines

include/linux/mmu_notifier.h static inline void mmu_notifier_head_ini · line 198

Quoted 6 lines
include/linux/mmu_notifier.h +6 -0
+				&mmu_rmap_notifier_list, hlist)		\
+			if (__mrn->ops->function)			\
+				__mrn->ops->function(__mrn, args);	\
+		rcu_read_unlock();					\
+	} while (0);
+

buggy macro: use locals.

Ok. Same as the non rmap version.

Quoted 3 lines
Quoted 1 line

+EXPORT_SYMBOL(mmu_rmap_export_page);

The other patch used EXPORT_SYMBOL_GPL.

Ok will make that consistent.

#8 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrew Morton
Quoted 4 lines · Andrew Morton

"looks good" maybe. But it's in the details where I fear this will come unstuck. The likelihood that some callbacks really will want to be able to block in places where this interface doesn't permit that - either to wait for IO to complete or to wait for other threads to clear critical regions.

We can get the invalidate_range to always be called without spinlocks if we deal with the case of the inode_mmap_lock being held in truncate case.

If you always want to be able to sleep then we could drop the invalidate_page() that is called while pte locks held and require the use of a device driver rmap?

Quoted 3 lines
Quoted 1 line

From that POV it doesn't look like a sufficiently general and useful

design. Looks like it was grafted onto the current VM implementation in a way which just about suits two particular clients if they try hard enough.

You missed KVM. We did the best we could being as least invasive as possible.

Quoted 7 lines

Which is all perfectly understandable - it would be hard to rework core MM to be able to make this interface more general. But I do think it's half-baked and there is a decent risk that future (or present) code which _could_ use something like this won't be able to use this one, and will continue to futz with mlock, page-pinning, etc.

Not that I know what the fix to that is..

You do not see a chance of this being okay if we adopt the two measures that I mentioned above?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#59 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Christoph Lameter
Quoted 18 lines · Christoph Lameter
Quoted 5 lines · Andrew Morton
Quoted 5 lines
mm/rmap.c +0 -1
@@ -287,7 +288,8 @@ static int page_referenced_one(struct pa
 	if (vma->vm_flags & VM_LOCKED) {
 		referenced++;
 		*mapcount = 1;	/* break early from loop */
-	} else if (ptep_clear_flush_young(vma, address, pte))
Quoted 9 lines

mm/rmap.c static int page_referenced_one(struct pa · line 294

Quoted 4 lines
mm/rmap.c +2 -0
+	} else if (ptep_clear_flush_young(vma, address, pte) |
+		   mmu_notifier_age_page(mm, address))
 		referenced++;
 

The "|" is obviously deliberate. But no explanation is provided telling us why we still call the callback if ptep_clear_flush_young() said the page was recently referenced. People who read your code will want to understand this.

Quoted 3 lines

Andrea?

I'm not Andrea, but the way I read it, ptep_clear_flush_young() and ->age_page() each have two effects: check whether the page has been referenced and clear the referenced bit. || would retain the semantics of the check but lose the clearing. | does the right thing.

Signature · 1 line
Any sufficiently difficult bug is indistinguishable from a feature.
#10 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Christoph Lameter
Quoted 5 lines · Christoph Lameter
Quoted 1 line · Andrew Morton

What is the status of getting infiniband to use this facility?

Well we are talking about this it seems.

It seems the IB folks think allowing RDMA over virtual memory is not interesting, their argument seem to be that RDMA is only interesting on RAM (and they seem not interested in allowing RDMA over a ram+swap backed _virtual_ memory allocation). They've just to decide if ram+swap allocation for RDMA is useful or not.

Quoted 3 lines
Quoted 1 line

How important is this feature to KVM?

Andrea can answer this.

I think I already did in separate email.

Quoted 2 lines
Quoted 2 lines

That sucks big time. What do we need to do to make get the callback functions called in non-atomic context?

I sure agree given I also asked to drop the lock param and enforce the invalidate_range_* to always be called in non atomic context.

Quoted 2 lines

We would have to drop the inode_mmap_lock. Could be done with some minor work.

The invalidate may be deferred after releasing the lock, the lock may not have to be dropped to cleanup the API (and make xpmem life easier).

Quoted 3 lines

That is one implementation (XPmem does that). The other is to simply stop all references when any invalidate_range is in progress (KVM and GRU do that).

KVM doesn't stop new references. It doesn't need to because it holds a reference on the page (GRU doesn't). KVM can invalidate the spte and flush the tlb only after the linux pte has been cleared and after the page has been released by the VM (because the page doesn't go in the freelist and it remains pinned for a little while, until the spte is dropped too inside invalidate_range_end). GRU has to invalidate _before_ the linux pte is cleared so it has to stop new references from being established in the invalidate_range_start/end critical section.

Quoted 2 lines

Andrea put this in to check the reference status of a page. It functions like the accessed bit.

In short each pte can have some spte associated to it. So whenever we do a ptep_clear_flush protected by the PT lock, we also have to run invalidate_page that will internally invoke a sort-of sptep_clear_flush protected by a kvm->mmu_lock (equivalent of page_table_lock/PT-lock). sptes just like ptes maps virtual addresses to physical addresses, so you can read/write to RAM either through a pte or through a spte.

Just like it would be insane to have any requirement that ptep_clear_flush has to run in not-atomic context (forcing a conversion of the PT lock to a mutex), it's also weird require the invalidate_page/age_page to run in atomic context.

All troubles start with the xpmem requirements of having to schedule in its equivalent of the sptep_clear_flush because it's not a gigaherz-in-cpu thing but a gigabit thing where the network stack is involved with its own software linux driven skb memory allocations, schedules waiting for network I/O, etc... Imagine ptes allocated in a remote node, no surprise its brings a new set of problems (assuming it can work reliably during oom given its memory requirements in the try_to_unmap path, no page can ever be freed until the skbs have been allocated and sent and allocated again to receive the ack).

Furthermore xpmem doesn't associate any pte to a spte, it associates a page_t to certain remote references, or it would be in trouble with invalidate_page that corresponds to ptep_clear_flush on a virtual address that exists thanks to the anon_vma/i_mmap lock held (and not thanks to the mmap_sem like in all invalidate_range calls).

Christoph's patch is a mix of two entirely separated features. KVM can live with V7 just fine, but it's a lot more than what is needed by KVM.

I don't think that invalidate_page/age_page must be allowed to sleep because invalidate_range also can sleep. You've to just ask yourself if the VM locks shall remain spinlocks, for the VM own good (not for the mmu notifiers good). It'd be bad to make the VM underperform with mutex protecting tiny critical sections to please some mmu notifier user. But if they're spinlocks, then clearly invalidate_page/age_page based on virtual addresses can't sleep or the virtual address wouldn't make sense anymore by the time the spinlock is released.

Quoted 14 lines
Quoted 14 lines

This function looks like it was tossed in at the last minute. It's mysterious, undocumented, poorly commented, poorly named. A better name would be one which has some correlation with the return value.

Because anyone who looks at some code which does

	if (mmu_notifier_age_page(mm, address))
		...

has to go and reverse-engineer the implementation of mmu_notifier_age_page() to work out under which circumstances the "..." will be executed. But this should be apparent just from reading the callee implementation.

Quoted 5 lines
Quoted 3 lines

This function *really* does need some documentation. What does it *mean* when the ->age_page() from some of the notifiers returned "1" and the ->age_page() from some other notifiers returned zero? Dunno.

Andrea: Could you provide some more detail here?

age_page is simply the ptep_clear_flush_young equivalent for sptes. It's meant to provide aging to the pages mapped by secondary mmus. Its return value is the same one of ptep_clear_flush_young but it represents the sptes associated with the pte, ptep_clear_flush_young instead only takes care of the pte itself.

For KVM the below would be all that is needed, the fact invalidate_range can sleep and invalidate_page/age_page can't, is because their users are very different. With my approach the mmu notifiers callback are always protected by the PT lock (just like ptep_clear_flush and the other pte+tlb manglings) and they're called after the pte is cleared and before the VM reference on the page has been dropped. That makes it safe for GRU too, so for my initial approach _none_ of the callbacks was allowed to sleep, and that was a feature that allows GRU not to block its tlb miss interrupt with any further locking (the PT-lock taken by follow_page automatically serialized the GRU interrupt against the MMU notifiers and the linux page fault). For KVM the invalidate_pages of my patch is converted to invalidate_range_end because it doesn't matter for KVM if it's called after the PT lock has been dropped. In the try_to_unmap case invalidate_page is called by atomic context in Christoph's patch too, because a virtual address and in turn a pte and in turn certain sptes, can only exist thanks to the spinlocks taken by the VM. Changing the VM to make mmu notifiers sleepable in the try_to_unmap path sounds bad to me, especially given not even xpmem needs this.

You can see how everything looks simpler and more symmetric by assuming the secondary mmu-references are established and dropped like ptes, like in the KVM case where infact sptes are a pure cpu thing exact like the ptes. XPMEM adds the requirement that sptes are infact remote entities that are mangled by a message passing protocol over the network, it's the same as ptep_clear_flush being required to schedule and send skbs to be successful and allowing try_to_unmap to do its work. Same problem. No wonder patch gets more complicated then.

include/asm-generic/pgtable.h +2 -0
@@ -46,6 +46,7 @@
 	__young = ptep_test_and_clear_young(__vma, __address, __ptep);	\
 	if (__young)							\
 		flush_tlb_page(__vma, __address);			\
+	__young |= mmu_notifier_age_page((__vma)->vm_mm, __address);	\
 	__young;							\
 })
 #endif
@@ -86,6 +87,7 @@ do {									\
 	pte_t __pte;							\
 	__pte = ptep_get_and_clear((__vma)->vm_mm, __address, __ptep);	\
 	flush_tlb_page(__vma, __address);				\
+	mmu_notifier(invalidate_page, (__vma)->vm_mm, __address);	\
 	__pte;								\
 })
 #endif
include/asm-s390/pgtable.h +1 -0
@@ -712,6 +712,7 @@ static inline pte_t ptep_clear_flush(str
 {
 	pte_t pte = *ptep;
 	ptep_invalidate(address, ptep);
+	mmu_notifier(invalidate_page, vma->vm_mm, address);
 	return pte;
 }
 
include/linux/mm_types.h +3 -0
@@ -10,6 +10,7 @@
 #include <linux/rbtree.h>
 #include <linux/rwsem.h>
 #include <linux/completion.h>
+#include <linux/mmu_notifier.h>
 #include <asm/page.h>
 #include <asm/mmu.h>
 
@@ -219,6 +220,8 @@ struct mm_struct {
 	/* aio bits */
 	rwlock_t		ioctx_list_lock;
 	struct kioctx		*ioctx_list;
+
+	struct mmu_notifier_head mmu_notifier; /* MMU notifier list */
 };
 
 #endif /* _LINUX_MM_TYPES_H */
include/linux/mmu_notifier.h new file +132 -0

132 lines not on this page · raw message

kernel/fork.c +1 -0
@@ -360,6 +360,7 @@ static struct mm_struct * mm_init(struct
 
 	if (likely(!mm_alloc_pgd(mm))) {
 		mm->def_flags = 0;
+		mmu_notifier_head_init(&mm->mmu_notifier);
 		return mm;
 	}
 	free_mm(mm);
mm/Kconfig +4 -0
@@ -193,3 +193,7 @@ config VIRT_TO_BUS
 config VIRT_TO_BUS
 	def_bool y
 	depends on !ARCH_NO_VIRT_TO_BUS
+
+config MMU_NOTIFIER
+	def_bool y
+	bool "MMU notifier, for paging KVM/RDMA"
mm/Makefile +1 -0
@@ -30,4 +30,5 @@ obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_MIGRATION) += migrate.o
 obj-$(CONFIG_SMP) += allocpercpu.o
 obj-$(CONFIG_QUICKLIST) += quicklist.o
+obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o
 
mm/hugetlb.c +1 -0
@@ -756,6 +756,7 @@ void __unmap_hugepage_range(struct vm_ar
 		if (pte_none(pte))
 			continue;
 
+		mmu_notifier(invalidate_page, mm, address);
 		page = pte_page(pte);
 		if (pte_dirty(pte))
 			set_page_dirty(page);
mm/memory.c +7 -0
@@ -494,6 +494,7 @@ static int copy_pte_range(struct mm_stru
 	spinlock_t *src_ptl, *dst_ptl;
 	int progress = 0;
 	int rss[2];
+	unsigned long start;
 
 again:
 	rss[1] = rss[0] = 0;
@@ -505,6 +506,7 @@ again:
 	spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
 	arch_enter_lazy_mmu_mode();
 
+	start = addr;
 	do {
 		/*
 		 * We are holding two locks at this point - either of them
@@ -525,6 +527,8 @@ again:
 	} while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
 
 	arch_leave_lazy_mmu_mode();
+	if (is_cow_mapping(vma->vm_flags))
+		mmu_notifier(invalidate_pages, vma->vm_mm, start, addr);
 	spin_unlock(src_ptl);
 	pte_unmap_nested(src_pte - 1);
 	add_mm_rss(dst_mm, rss[0], rss[1]);
@@ -660,6 +664,7 @@ static unsigned long zap_pte_range(struc
 			}
 			ptent = ptep_get_and_clear_full(mm, addr, pte,
 							tlb->fullmm);
+			mmu_notifier(invalidate_page, mm, addr);
 			tlb_remove_tlb_entry(tlb, pte, addr);
 			if (unlikely(!page))
 				continue;
@@ -1248,6 +1253,7 @@ static int remap_pte_range(struct mm_str
 {
 	pte_t *pte;
 	spinlock_t *ptl;
+	unsigned long start = addr;
 
 	pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
 	if (!pte)
@@ -1259,6 +1265,7 @@ static int remap_pte_range(struct mm_str
 		pfn++;
 	} while (pte++, addr += PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
+	mmu_notifier(invalidate_pages, mm, start, addr);
 	pte_unmap_unlock(pte - 1, ptl);
 	return 0;
 }
mm/mmap.c +1 -0
@@ -2044,6 +2044,7 @@ void exit_mmap(struct mm_struct *mm)
 	vm_unacct_memory(nr_accounted);
 	free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, 0);
 	tlb_finish_mmu(tlb, 0, end);
+	mmu_notifier_release(mm);
 
 	/*
 	 * Walk the list again, actually closing and freeing it,
mm/mmu_notifier.c new file +73 -0

73 lines not on this page · raw message

mm/mprotect.c +2 -0
@@ -32,6 +32,7 @@ static void change_pte_range(struct mm_s
 {
 	pte_t *pte, oldpte;
 	spinlock_t *ptl;
+	unsigned long start = addr;
 
 	pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
 	arch_enter_lazy_mmu_mode();
@@ -71,6 +72,7 @@ static void change_pte_range(struct mm_s
 
 	} while (pte++, addr += PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
+	mmu_notifier(invalidate_pages, mm, start, addr);
 	pte_unmap_unlock(pte - 1, ptl);
 }
 
#12 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrew Morton
Quoted 2 lines · Andrew Morton

Which other potential clients have been identified and how important it it to those?

The powerpc ehea utilizes its own mmu. Not sure about the importance to the driver. (But will investigate :)

++doug

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#11 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Andrea Arcangeli
Quoted 5 lines · Andrea Arcangeli
Quoted 4 lines · Christoph Lameter
Quoted 1 line · Andrew Morton

What is the status of getting infiniband to use this facility?

Quoted 7 lines
Quoted 1 line

Well we are talking about this it seems.

It seems the IB folks think allowing RDMA over virtual memory is not interesting, their argument seem to be that RDMA is only interesting on RAM (and they seem not interested in allowing RDMA over a ram+swap backed _virtual_ memory allocation). They've just to decide if ram+swap allocation for RDMA is useful or not.

I don't think that is a completely fair characterization. It would be more fair to say that the changes required to their library/user api would be too significant to allow an adaptation to any scheme which allowed removal of physical memory below a virtual mapping.

I agree with the IB folks when they say it is impossible with their current scheme. The fact that any consumer of their endpoint identifier can use any identifier without notifying the kernel prior to its use certainly makes any implementation under any scheme impossible.

I guess we could possibly make things work for IB if we did some heavy work. Let's assume, instead of passing around the physical endpoint identifiers, they passed around a handle. In order for any IB endpoint to commuicate, it would need to request the kernel translate a handle into an endpoint identifier. In order for the kernel to put a TLB entry into the processes address space allowing the process access to the _CARD_, it would need to ensure all the current endpoint identifiers for this process were "active" meaning we have verified with the other endpoint that all pages are faulted and TLB/PFN information is in the owning card's TLB/PFN tables. Once all of a processes endoints are "active" we would drop in the PFN for the adapter into the pages tables. Any time pages are being revoked from under an active handle, we would shoot-down the IB adapter card TLB entries for all the remote users of this handle and quiesce the cards state to ensure transfers are either complete or terminated. When their are no active transfers, we would respond back to the owner and they could complete the source process page table cleaning. Any time all of the pages for a handle can not be mapped from virtual to physical, the remote process would be SIGBUS'd instead of having it IB adapter TLB installed.

This is essentially how XPMEM does it except we have the benefit of working on individual pages.

Again, not knowing what I am talking about, but under the assumption that MPI IB use is contained to a library, I would hope the changes could be contained under the MPI-to-IB library interface and would not need any changes at the MPI-user library interface.

We do keep track of the virtual address ranges within a handle that are being used. I assume the IB folks will find that helpful as well. Otherwise, I think they could make things operate this way. XPMEM has the advantage of not needing to have virtual-to-physical at all times, but otherwise it is essentially the same.

Thanks,
Robin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#62 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Andrew Morton
Quoted 1 line · Andrew Morton

On Thu, 14 Feb 2008 22:49:02 -0800 Christoph Lameter <[email protected]>

Quoted 8 lines · wrote:
Quoted 8 lines

Two callbacks to remove individual pages as done in rmap code

invalidate_page()

Called from the inner loop of rmap walks to invalidate pages.

	age_page()
Quoted 7 lines
Quoted 5 lines

Called for the determination of the page referenced status.

If we do not care about page referenced status then an age_page callback may be be omitted. PageLock and pte lock are held when either of the functions is called.

The age_page mystery shallows.

BTW. can this callback be called mmu_notifier_clear_flush_young? To match the core VM.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#87 lore raw

Re: [PATCH] KVM swapping with MMU Notifiers V7

In reply to Andrew Morton
Quoted 18 lines · Andrew Morton
Quoted 4 lines · On Sat, 16 Feb 2008 11:48:27 +0100 Andrea Arcangeli <[email protected]> wrote:
arch/x86/kvm/x86.c +4 -0
+void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
+					   struct mm_struct *mm,
+					   unsigned long start, unsigned long end,
+					   int lock)

arch/x86/kvm/x86.c void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) · line 3226

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+{
+	for (; start < end; start += PAGE_SIZE)
+		kvm_mmu_notifier_invalidate_page(mn, mm, start);
+}
+
+static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
+	.invalidate_page	= kvm_mmu_notifier_invalidate_page,
+	.age_page		= kvm_mmu_notifier_age_page,
+	.invalidate_range_end	= kvm_mmu_notifier_invalidate_range_end,
+};

So this doesn't implement ->invalidate_range_start().

Correct. range_start is needed by subsystems that don't pin the pages (so they've to drop the secondary mmu mappings on the physical page before the page is released by the linux VM).

Quoted 3 lines

By what means does it prevent new mappings from being established in the range after core mm has tried to call ->invalidate_rande_start()? mmap_sem, I assume?

No, populate range only takes the mmap_sem in read mode and the kvm page fault also is of course taking it only in read mode.

What makes it safe, is that invalidate_range_end is called _after_ the linux pte is clear. The kvm page fault, if it triggers, it will call into get_user_pages again to re-establish the linux pte _before_ establishing the spte.

It's the same reason why it's safe to flush the tlb after clearing the linux pte. sptes are like a secondary tlb.

Quoted 8 lines

arch/x86/kvm/x86.c int kvm_arch_set_memory_region(struct kvm *kvm, · line 3264

Quoted 4 lines
arch/x86/kvm/x86.c +4 -0
+			/* set userspace_addr atomically for kvm_hva_to_rmapp */
+			spin_lock(&kvm->mmu_lock);
+			memslot->userspace_addr = userspace_addr;
+			spin_unlock(&kvm->mmu_lock);

are you sure? kvm_unmap_hva() and kvm_age_hva() read ->userspace_addr a single time and it doesn't immediately look like there's a need to take the lock here?

gcc will always write it with a movq but this is to be C-specs-compliant and because this is by far not a performance critical path I thought it was simpler than some other atomic move in a single insn.

#89 lore raw

Re: [PATCH] KVM swapping with MMU Notifiers V7

In reply to Robin Holt
Quoted 9 lines · Robin Holt

I am doing this in xpmem with a stack-based structure in the function calling get_user_pages. That structure describes the start and end address of the range we are doing the get_user_pages on. If an invalidate_range_begin comes in while we are off to the kernel doing the get_user_pages, the invalidate_range_begin marks that structure indicating an invalidate came in. When the get_user_pages gets the structures relocked, it checks that flag (really a generation counter) and if it is set, retries the get_user_pages. After 3 retries, it returns -EAGAIN and the fault is started over from the remote side.

A seqlock sounds a good optimization for the non-swapping fast path, a per-VM-guest seqlock number can allow us to know when we need to worry to call get_user_pages a second time, but won't be really a retry like in 99% of seqlock usages for the reader side, but just a second get_user_pages to trigger a minor fault. Then if the page is different in the second run, we'll really retry (so not in function of the seqlock but in function of the get_user_pages page array), and there's no risk of livelocks because get_user_pages returning a different page won't be the common case. The seqlock should be increased first before the invalidate and a second time once the invalidate is over.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#13 lore raw

Re: [patch 1/6] mmu_notifier: Core code

In reply to Christoph Lameter

It seems that we've come up with two reasonable cases where it makes sense to use these notifiers for InfiniBand/RDMA:

First, the ability to safely to DMA to/from userspace memory with the memory regions mlock()ed but the pages not pinned. In this case the notifiers here would seem to suit us well:

include/linux/mmu_notifier.h line 94

Quoted 9 lines
include/linux/mmu_notifier.h new file +9 -0
+	void (*invalidate_range_begin)(struct mmu_notifier *mn,
+				 struct mm_struct *mm,
+				 unsigned long start, unsigned long end,
+				 int atomic);
+
+	void (*invalidate_range_end)(struct mmu_notifier *mn,
+				 struct mm_struct *mm,
+				 unsigned long start, unsigned long end,
+				 int atomic);

If I understand correctly, the IB stack would have to get the hardware driver to shoot down translation entries and suspend access to the region when an invalidate_range_begin notifier is called, and wait for the invalidate_range_end notifier to repopulate the adapter translation tables. This will probably work OK as long as the interval between the invalidate_range_begin and invalidate_range_end calls is not "too long."

Also, using this effectively requires us to figure out how we want to mlock() regions that are going to be used for RDMA. We could require userspace to do it, but it's not clear to me that we're safe in the case where userspace decides not to... what happens if some pages get swapped out after the invalidate_range_begin notifier?

The second case where some form of notifiers are useful is for userspace to know when a memory registration is still valid, ie Pete Wyckoff's work:

    http://www.osc.edu/~pw/papers/wyckoff-memreg-ccgrid05.pdf
    http://www.osc.edu/~pw/dreg/

however these MMU notifiers seem orthogonal to that: the registration cache is concerned with address spaces, not page mapping, and hence the existing vma operations seem to be a better fit.

- R.

#60 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Christoph Lameter
Quoted 11 lines · Christoph Lameter
Quoted 7 lines · Andrew Morton

mm/rmap.c static int page_mkclean_one(struct page · line 460

Quoted 3 lines
mm/rmap.c +1 -0
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		entry = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);

I just don't see how ths can be done if the callee has another thread in the middle of establishing IO against this region of memory. ->invalidate_page() _has_ to be able to block. Confused.

The page lock is held and that holds off I/O?

I think the actual answer is that "it doesn't matter".

ptes are not exactly the entity via which IO gets established, so all we really care about here is that after the callback finishes, we will not get any more reads or writes to the page via the external mapping.

As far as holding off local IO goes, that is the job of the core VM. (And no, page lock does not necessarily hold it off FYI -- it can be writeback IO or even IO directly via buffers).

Holding off IO via the external references I guess is a job for the notifier driver.

#17 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 11 lines · Christoph Lameter

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

In two cases we use invalidate_range_begin/end to invalidate single pages because the pair allows holding off new references (idea by Robin Holt).

Quoted 5 lines

do_wp_page(): We hold off new references while we update the pte.

xip_unmap: We are not taking the PageLock so we cannot
use the invalidate_page mmu_rmap_notifier. invalidate_range_begin/end stands in.

This whole thing would be much better if you didn't rely on the page lock at all, but either a) used the same locking as Linux does for its ptes/tlbs, or b) have some locking that is private to the mmu notifier code. Then there is not all this new stuff that has to be understood in the core VM.

Also, why do you have to "invalidate" ranges when switching to a _more_ permissive state? This stuff should basically be the same as (a subset of) the TLB flushing API AFAIKS. Anything more is a pretty big burden to put in the core VM.

See my alternative patch I posted -- I can't see why it won't work just like a TLB.

As far as sleeping inside callbacks goes... I think there are big problems with the patch (the sleeping patch and the external rmap patch). I don't think it is workable in its current state. Either we have to make some big changes to the core VM, or we have to turn some locks into sleeping locks to do it properly AFAIKS. Neither one is good.

But anyway, I don't really think the two approaches (Andrea's notifiers vs sleeping/xrmap) should be tangled up too much. I think Andrea's can possibly be quite unintrusive and useful very soon.

#61 lore raw

Re: [patch 3/6] mmu_notifier: invalidate_page callbacks

In reply to Nick Piggin
Quoted 11 lines · Nick Piggin
Quoted 10 lines · Christoph Lameter
Quoted 7 lines · Andrew Morton

mm/rmap.c static int page_mkclean_one(struct page · line 460

Quoted 3 lines
mm/rmap.c +1 -0
 		flush_cache_page(vma, address, pte_pfn(*pte));
 		entry = ptep_clear_flush(vma, address, pte);
+		mmu_notifier(invalidate_page, mm, address);

I just don't see how ths can be done if the callee has another thread in the middle of establishing IO against this region of memory. ->invalidate_page() _has_ to be able to block. Confused.

Quoted 3 lines
Quoted 1 line

The page lock is held and that holds off I/O?

I think the actual answer is that "it doesn't matter".

Agreed. The PG_lock itself taken when invalidate_page is called, is used to serialized the VM against the VM, not the VM against I/O.

#18 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Nick Piggin
Quoted 6 lines · Nick Piggin

As far as sleeping inside callbacks goes... I think there are big problems with the patch (the sleeping patch and the external rmap patch). I don't think it is workable in its current state. Either we have to make some big changes to the core VM, or we have to turn some locks into sleeping locks to do it properly AFAIKS. Neither one is good.

Agreed.

The thing is quite simple, the moment we support xpmem the complexity in the mmu notifier patch start and there are hacks, duplicated functionality through the same xpmem callbacks etc... GRU can already be 100% supported (infact simpler and safer) with my patch.

Quoted 4 lines

But anyway, I don't really think the two approaches (Andrea's notifiers vs sleeping/xrmap) should be tangled up too much. I think Andrea's can possibly be quite unintrusive and useful very soon.

Yes, that's why I kept maintaining my patch and I posted the last revision to Andrew. I use pte/tlb locking of the core VM, it's unintrusive and obviously safe. Furthermore it can be extended with Christoph's stuff in a 100% backwards compatible fashion later if needed.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#21 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 6 lines · Christoph Lameter

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

You can't sleep inside rcu_read_lock()!

I must say that for a patch that is up to v8 or whatever and is posted twice a week to such a big cc list, it is kind of slack to not even test it and expect other people to review it.

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver writer, then for the sanity of the VM developers (I don't want to have to understand xpmem or infiniband in order to understand how the VM works).

Quoted 87 lines

In two cases we use invalidate_range_begin/end to invalidate single pages because the pair allows holding off new references (idea by Robin Holt).

do_wp_page(): We hold off new references while we update the pte.

xip_unmap: We are not taking the PageLock so we cannot
use the invalidate_page mmu_rmap_notifier. invalidate_range_begin/end stands in.

---
 mm/filemap_xip.c |    5 +++++
 mm/fremap.c      |    3 +++
 mm/hugetlb.c     |    3 +++
 mm/memory.c      |   35 +++++++++++++++++++++++++++++------
 mm/mmap.c        |    2 ++
 mm/mprotect.c    |    3 +++
 mm/mremap.c      |    7 ++++++-
 7 files changed, 51 insertions(+), 7 deletions(-)

Index: linux-2.6/mm/fremap.c
===================================================================

mm/fremap.c +3 -0
@@ -15,6 +15,7 @@
 #include <linux/rmap.h>
 #include <linux/module.h>
 #include <linux/syscalls.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/mmu_context.h>
 #include <asm/cacheflush.h>
@@ -214,7 +215,9 @@ asmlinkage long sys_remap_file_pages(uns
 		spin_unlock(&mapping->i_mmap_lock);
 	}
 
+	mmu_notifier(invalidate_range_begin, mm, start, start + size, 0);
 	err = populate_range(mm, vma, start, size, pgoff);
+	mmu_notifier(invalidate_range_end, mm, start, start + size, 0);
 	if (!err && !(flags & MAP_NONBLOCK)) {
 		if (unlikely(has_write_lock)) {
 			downgrade_write(&mm->mmap_sem);

Index: linux-2.6/mm/memory.c
===================================================================

mm/memory.c +10 -0
@@ -51,6 +51,7 @@
 #include <linux/init.h>
 #include <linux/writeback.h>
 #include <linux/memcontrol.h>
+#include <linux/mmu_notifier.h>
 
 #include <asm/pgalloc.h>
 #include <asm/uaccess.h>
@@ -611,6 +612,9 @@ int copy_page_range(struct mm_struct *ds
 	if (is_vm_hugetlb_page(vma))
 		return copy_hugetlb_page_range(dst_mm, src_mm, vma);
 
+	if (is_cow_mapping(vma->vm_flags))
+		mmu_notifier(invalidate_range_begin, src_mm, addr, end, 0);
+
 	dst_pgd = pgd_offset(dst_mm, addr);
 	src_pgd = pgd_offset(src_mm, addr);
 	do {
@@ -621,6 +625,11 @@ int copy_page_range(struct mm_struct *ds
 						vma, addr, next))
 			return -ENOMEM;
 	} while (dst_pgd++, src_pgd++, addr = next, addr != end);
+
+	if (is_cow_mapping(vma->vm_flags))
+		mmu_notifier(invalidate_range_end, src_mm,
+						vma->vm_start, end, 0);
+
 	return 0;
 }
 
@@ -893,13 +902,16 @@ unsigned long zap_page_range(struct vm_a
 	struct mmu_gather *tlb;
 	unsigned long end = address + size;
 	unsigned long nr_accounted = 0;
+	int atomic = details ? (details->i_mmap_lock != 0) : 0;
 
 	lru_add_drain();

mm/memory.c unsigned long zap_page_range(struct vm_a · line 917

Quoted 10 lines
mm/memory.c +2 -0
 	tlb = tlb_gather_mmu(mm, 0);
 	update_hiwater_rss(mm);
+	mmu_notifier(invalidate_range_begin, mm, address, end, atomic);
 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
 	if (tlb)
 		tlb_finish_mmu(tlb, address, end);
+	mmu_notifier(invalidate_range_end, mm, address, end, atomic);
 	return end;
 }
 

Where do you invalidate for munmap()?

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

#67 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Christoph Lameter
Quoted 11 lines · Christoph Lameter

These special additional callbacks are required because XPmem (and likely other mechanisms) do use their own rmap (multiple processes on a series of remote Linux instances may be accessing the memory of a process). F.e. XPmem may have to send out notifications to remote Linux instances and receive confirmation before a page can be freed.

So we handle this like an additional Linux reverse map that is walked after the existing rmaps have been walked. We leave the walking to the driver that is then able to use something else than a spinlock to walk its reverse maps. So we can actually call the driver without holding spinlocks while we hold the Pagelock.

I don't know how this is supposed to solve anything. The sleeping problem happens I guess mostly in truncate. And all you are doing is putting these rmap callbacks in page_mkclean and try_to_unmap.

Quoted 9 lines

However, we cannot determine the mm_struct that a page belongs to at that point. The mm_struct can only be determined from the rmaps by the device driver.

We add another pageflag (PageExternalRmap) that is set if a page has been remotely mapped (f.e. by a process from another Linux instance). We can then only perform the callbacks for pages that are actually in remote use.

Quoted 5 lines

Rmap notifiers need an extra page bit and are only available on 64 bit platforms. This functionality is not available on 32 bit!

A notifier that uses the reverse maps callbacks does not need to provide the invalidate_page() method that is called when locks are held.

That doesn't seem right. To start with, the new callbacks aren't even called in the places where invalidate_page isn't allowed to sleep.

The problem is unmap_mapping_range, right? And unmap_mapping_range must walk the rmaps with the mmap lock held, which is why it can't sleep. And it can't hold any mmap_sem so it cannot prevent address space modifications of the processes in question between the time you unmap them from the linux ptes with unmap_mapping_range, and the time that you unmap them from your driver.

So in the meantime, you could have eg. a fault come in and set up a new page for one of the processes, and that page might even get exported via the same external driver. And now you have a totally inconsistent view.

Preventing new mappings from being set up until the old mapping is completely flushed is basically what we need to ensure for any sane TLB as far as I can tell. To do that, you'll need to make the mmap lock sleep, and either take mmap_sem inside it (which is a deadlock condition at the moment), or make ptl sleep as well. These are simply the locks we use to prevent that from happening, so I can't see how you can possibly hope to have a coherent TLB without invalidating inside those locks.

#22 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Nick Piggin
Quoted 5 lines · Nick Piggin

You can't sleep inside rcu_read_lock()!

I must say that for a patch that is up to v8 or whatever and is posted twice a week to such a big cc list, it is kind of slack to not even test it and expect other people to review it.

Well, xpmem requirements are complex. As as side effect of the simplicity of my approach, my patch is 100% safe since #v1. Now it also works for GRU and it cluster invalidates.

Quoted 4 lines

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver

I've a fully working scenario for my patch, infact I didn't post the mmu notifier patch until I got KVM to swap 100% reliably to be sure I would post something that works well. mmu notifiers are already used in KVM for:

1) 100% reliable and efficient swapping of guest physical memory
2) copy-on-writes of writeprotect faults after ksm page sharing of guest physical memory
3) ballooning using madvise to give the guest memory back to the host

My implementation is the most handy because it requires zero changes to the ksm code too (no explicit mmu notifier calls after ptep_clear_flush) and it's also 100% safe (no mess with schedules over rcu_read_lock), no "atomic" parameters, and it doesn't open a window where sptes have a view on older pages and linux pte has view on newer pages (this can happen with remap_file_pages with my KVM swapping patch to use V8 Christoph's patch).

Quoted 4 lines

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

I also asked exactly this, glad you reasked this too.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#23 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 34 lines · Andrea Arcangeli
Quoted 5 lines · Nick Piggin

You can't sleep inside rcu_read_lock()!

I must say that for a patch that is up to v8 or whatever and is posted twice a week to such a big cc list, it is kind of slack to not even test it and expect other people to review it.

Well, xpmem requirements are complex. As as side effect of the simplicity of my approach, my patch is 100% safe since #v1. Now it also works for GRU and it cluster invalidates.

Quoted 4 lines

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver

I've a fully working scenario for my patch, infact I didn't post the mmu notifier patch until I got KVM to swap 100% reliably to be sure I would post something that works well. mmu notifiers are already used in KVM for:

1) 100% reliable and efficient swapping of guest physical memory
2) copy-on-writes of writeprotect faults after ksm page sharing of guest physical memory
3) ballooning using madvise to give the guest memory back to the host

My implementation is the most handy because it requires zero changes to the ksm code too (no explicit mmu notifier calls after ptep_clear_flush) and it's also 100% safe (no mess with schedules over rcu_read_lock), no "atomic" parameters, and it doesn't open a window where sptes have a view on older pages and linux pte has view on newer pages (this can happen with remap_file_pages with my KVM swapping patch to use V8 Christoph's patch).

Quoted 6 lines
Quoted 4 lines

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

I also asked exactly this, glad you reasked this too.

Currently, we BUG_ON having a PFN in our tables and not being able to sleep. These are mappings which MPT has never supported in the past and XPMEM was already not allowing page faults for VMAs which are not anonymous so it should never happen. If the file-backed operations can ever get changed to allow for sleeping and a customer has a need for it, we would need to change XPMEM to allow those types of faults to succeed.

Thanks,
Robin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#24 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 8 lines · Robin Holt
Quoted 7 lines · Andrea Arcangeli
Quoted 4 lines · Nick Piggin

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

Quoted 8 lines
Quoted 1 line

I also asked exactly this, glad you reasked this too.

Currently, we BUG_ON having a PFN in our tables and not being able to sleep. These are mappings which MPT has never supported in the past and XPMEM was already not allowing page faults for VMAs which are not anonymous so it should never happen. If the file-backed operations can ever get changed to allow for sleeping and a customer has a need for it, we would need to change XPMEM to allow those types of faults to succeed.

Do you really want to be able to swap, or are you just interested in keeping track of unmaps / prot changes?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#68 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Nick Piggin
Quoted 49 lines · Nick Piggin
Quoted 11 lines · Christoph Lameter

These special additional callbacks are required because XPmem (and likely other mechanisms) do use their own rmap (multiple processes on a series of remote Linux instances may be accessing the memory of a process). F.e. XPmem may have to send out notifications to remote Linux instances and receive confirmation before a page can be freed.

So we handle this like an additional Linux reverse map that is walked after the existing rmaps have been walked. We leave the walking to the driver that is then able to use something else than a spinlock to walk its reverse maps. So we can actually call the driver without holding spinlocks while we hold the Pagelock.

I don't know how this is supposed to solve anything. The sleeping problem happens I guess mostly in truncate. And all you are doing is putting these rmap callbacks in page_mkclean and try_to_unmap.

Quoted 9 lines

However, we cannot determine the mm_struct that a page belongs to at that point. The mm_struct can only be determined from the rmaps by the device driver.

We add another pageflag (PageExternalRmap) that is set if a page has been remotely mapped (f.e. by a process from another Linux instance). We can then only perform the callbacks for pages that are actually in remote use.

Quoted 5 lines

Rmap notifiers need an extra page bit and are only available on 64 bit platforms. This functionality is not available on 32 bit!

A notifier that uses the reverse maps callbacks does not need to provide the invalidate_page() method that is called when locks are held.

That doesn't seem right. To start with, the new callbacks aren't even called in the places where invalidate_page isn't allowed to sleep.

The problem is unmap_mapping_range, right? And unmap_mapping_range must walk the rmaps with the mmap lock held, which is why it can't sleep. And it can't hold any mmap_sem so it cannot prevent address space modifications of the processes in question between the time you unmap them from the linux ptes with unmap_mapping_range, and the time that you unmap them from your driver.

So in the meantime, you could have eg. a fault come in and set up a new page for one of the processes, and that page might even get exported via the same external driver. And now you have a totally inconsistent view.

Quoted 8 lines

Preventing new mappings from being set up until the old mapping is completely flushed is basically what we need to ensure for any sane TLB as far as I can tell. To do that, you'll need to make the mmap lock sleep, and either take mmap_sem inside it (which is a deadlock condition at the moment), or make ptl sleep as well. These are simply the locks we use to prevent that from happening, so I can't see how you can possibly hope to have a coherent TLB without invalidating inside those locks.

All of that is correct. For XPMEM, we do not currently allow file backed mapping pages from being exported so we should never reach this condition. It has been an issue since day 1. We have operated with that assumption for 6 years and have not had issues with that assumption. The user of xpmem is MPT and it controls the communication buffers so it is reasonable to expect this type of behavior.

Thanks,
Robin

#25 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Nick Piggin
Quoted 18 lines · Nick Piggin
Quoted 8 lines · Robin Holt
Quoted 7 lines · Andrea Arcangeli
Quoted 4 lines · Nick Piggin

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

Quoted 8 lines
Quoted 1 line

I also asked exactly this, glad you reasked this too.

Currently, we BUG_ON having a PFN in our tables and not being able to sleep. These are mappings which MPT has never supported in the past and XPMEM was already not allowing page faults for VMAs which are not anonymous so it should never happen. If the file-backed operations can ever get changed to allow for sleeping and a customer has a need for it, we would need to change XPMEM to allow those types of faults to succeed.

Quoted 2 lines

Do you really want to be able to swap, or are you just interested in keeping track of unmaps / prot changes?

I would rather not swap, but we do have one customer that would like swapout to work for certain circumstances. Additionally, we have many customers that would rather that their system not die under I/O termination.

Thanks,
Robin

#69 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Robin Holt
Quoted 6 lines · Robin Holt

For XPMEM, we do not currently allow file backed
mapping pages from being exported so we should never reach this condition. It has been an issue since day 1. We have operated with that assumption for 6 years and have not had issues with that assumption. The user of xpmem is MPT and it controls the communication buffers so it is reasonable to expect this type of behavior.

OK, that makes things simpler.

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

I guess you may also want an "munmap/mprotect" callback, which we don't have in the kernel right now... but at least you could prototype it easily by having an ioctl to be called before munmapping or mprotecting (eg. the ioctl could prevent new TLB setup for the region, and shoot down existing ones).

This is actually going to be much faster for you if you use any threaded applications, because you will be able to do all the shootdown round trips outside mmap_sem, and so you will be able to have other threads faulting and even mmap()ing / munmaping at the same time as the shootdown is happening.

I guess there is some catch...

#70 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 8 lines · Nick Piggin
Quoted 6 lines · Robin Holt

For XPMEM, we do not currently allow file backed
mapping pages from being exported so we should never reach this condition. It has been an issue since day 1. We have operated with that assumption for 6 years and have not had issues with that assumption. The user of xpmem is MPT and it controls the communication buffers so it is reasonable to expect this type of behavior.

Quoted 5 lines

OK, that makes things simpler.

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

Because we need to have heap and stack available as well. MPT does not control all the communication buffer areas. I haven't checked, but this is the same problem that IB will have. I believe they are actually allowing any memory region be accessible, but I am not sure of that.

Thanks,
Robin

#71 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 11 lines · Robin Holt
Quoted 10 lines · Nick Piggin
Quoted 6 lines · Robin Holt

For XPMEM, we do not currently allow file backed
mapping pages from being exported so we should never reach this condition. It has been an issue since day 1. We have operated with that assumption for 6 years and have not had issues with that assumption. The user of xpmem is MPT and it controls the communication buffers so it is reasonable to expect this type of behavior.

OK, that makes things simpler.

Quoted 8 lines
Quoted 3 lines

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

Because we need to have heap and stack available as well. MPT does not control all the communication buffer areas. I haven't checked, but this is the same problem that IB will have. I believe they are actually allowing any memory region be accessible, but I am not sure of that.

I should have read my work email first. I had gotten an email from one of our MPT developers saying they would love it if they could share file backed memory areas as well as it would help them with their MPI-IO functions which currently need to do multiple copy steps. Not sure how high of a priority I am going to be able to make that.

Thanks,
Robin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#72 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Robin Holt
Quoted 11 lines · Robin Holt
Quoted 10 lines · Nick Piggin
Quoted 6 lines · Robin Holt

For XPMEM, we do not currently allow file backed
mapping pages from being exported so we should never reach this condition. It has been an issue since day 1. We have operated with that assumption for 6 years and have not had issues with that assumption. The user of xpmem is MPT and it controls the communication buffers so it is reasonable to expect this type of behavior.

OK, that makes things simpler.

Quoted 8 lines
Quoted 3 lines

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

Because we need to have heap and stack available as well. MPT does not control all the communication buffer areas. I haven't checked, but this is the same problem that IB will have. I believe they are actually allowing any memory region be accessible, but I am not sure of that.

Then you should create a driver that the user program can register and unregister regions of their memory with. The driver can do a get_user_pages to get the pages, and then you'd just need to set up some kind of mapping so that userspace can unmap pages / won't leak memory (and an exit_mm notifier I guess).

Because you don't need to swap, you don't need coherency, and you are in control of the areas, then this seems like the best choice. It would allow you to use heap, stack, file-backed, anything.

#73 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 9 lines · Nick Piggin
Quoted 8 lines
Quoted 3 lines

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

Because we need to have heap and stack available as well. MPT does not control all the communication buffer areas. I haven't checked, but this is the same problem that IB will have. I believe they are actually allowing any memory region be accessible, but I am not sure of that.

Quoted 5 lines

Then you should create a driver that the user program can register and unregister regions of their memory with. The driver can do a get_user_pages to get the pages, and then you'd just need to set up some kind of mapping so that userspace can unmap pages / won't leak memory (and an exit_mm notifier I guess).

OK. You need to explain this better to me. How would this driver supposedly work? What we have is an MPI library. It gets invoked at process load time to establish its rank-to-rank communication regions. It then turns control over to the processes main(). That is allowed to run until it hits the

	MPI_Init(&argc, &argv);

The process is then totally under the users control until:

	MPI_Send(intmessage, m_size, MPI_INT, my_rank+half, tag, MPI_COMM_WORLD);
	MPI_Recv(intmessage, m_size, MPI_INT, my_rank+half,tag, MPI_COMM_WORLD, &status);

That is it. That is all our allowed interaction with the users process. Are you saying at the time of the MPI_Send, we should:

	down_write(&current->mm->mmap_sem);
	Find all the VMAs that describe this region and record their

vm_ops structure.
Find all currently inserted page table information. Create new VMAs that describe the same regions as before. Insert our special fault handler which merely calls their old
fault handler and then exports the page then returns the page to the kernel.
Take an extra reference count on the page for each possible
remote rank we are exporting this to.

That doesn't seem too unreasonable, except when you compare it to how the driver currently works. Remember, this is done from a library which has no insight into what the user has done to its own virtual address space. As a result, each MPI_Send() would result in a system call (or we would need to have a set of callouts for changes to a processes VMAs) which would be a significant increase in communication overhead.

Maybe I am missing what you intend to do, but what we need is a means of tracking one processes virtual address space changes so other processes can do direct memory accesses without the need for a system call on each communication event.

Quoted 3 lines

Because you don't need to swap, you don't need coherency, and you are in control of the areas, then this seems like the best choice. It would allow you to use heap, stack, file-backed, anything.

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec, we would still need to support the existing specs. As a result, the user can change their virtual address space as they need and still expect communications be cheap.

Thanks,
Robin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#74 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 22 lines · Robin Holt
Quoted 10 lines · Nick Piggin
Quoted 9 lines
Quoted 3 lines

So why can't you export a device from your xpmem driver, which can be mmap()ed to give out "anonymous" memory pages to be used for these communication buffers?

Because we need to have heap and stack available as well. MPT does not control all the communication buffer areas. I haven't checked, but this is the same problem that IB will have. I believe they are actually allowing any memory region be accessible, but I am not sure of that.

Quoted 5 lines

Then you should create a driver that the user program can register and unregister regions of their memory with. The driver can do a get_user_pages to get the pages, and then you'd just need to set up some kind of mapping so that userspace can unmap pages / won't leak memory (and an exit_mm notifier I guess).

OK. You need to explain this better to me. How would this driver supposedly work? What we have is an MPI library. It gets invoked at process load time to establish its rank-to-rank communication regions. It then turns control over to the processes main(). That is allowed to run until it hits the

Quoted 8 lines
	MPI_Init(&argc, &argv);

The process is then totally under the users control until:

	MPI_Send(intmessage, m_size, MPI_INT, my_rank+half, tag, MPI_COMM_WORLD);
	MPI_Recv(intmessage, m_size, MPI_INT, my_rank+half,tag, MPI_COMM_WORLD,

&status);

That is it. That is all our allowed interaction with the users process.

OK, when you said something along the lines of "the MPT library has control of the comm buffer", then I assumed it was an area of virtual memory which is set up as part of initialization, rather than during runtime. I guess I jumped to conclusions.

Quoted 11 lines

That doesn't seem too unreasonable, except when you compare it to how the driver currently works. Remember, this is done from a library which has no insight into what the user has done to its own virtual address space. As a result, each MPI_Send() would result in a system call (or we would need to have a set of callouts for changes to a processes VMAs) which would be a significant increase in communication overhead.

Maybe I am missing what you intend to do, but what we need is a means of tracking one processes virtual address space changes so other processes can do direct memory accesses without the need for a system call on each communication event.

Yeah it's tricky. BTW. what is the performance difference between having a system call or no?

Quoted 8 lines
Quoted 3 lines

Because you don't need to swap, you don't need coherency, and you are in control of the areas, then this seems like the best choice. It would allow you to use heap, stack, file-backed, anything.

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Can you change the spec? Are you working on it?

Quoted 3 lines

we would still need to support the existing specs. As a result, the user can change their virtual address space as they need and still expect communications be cheap.

That's true. How has it been supported up to now? Are you using these kind of notifiers in patched kernels?

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#75 lore raw

Re: [ofa-general] Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 6 lines · Nick Piggin
Quoted 4 lines

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Can you change the spec?

Not really. It will break all existing codes. MPI-2 provides a call for memory allocation (and it's beneficial to use this call for some interconnects), but many (most?) applications are still written for MPI-1 and those that are written for MPI-2 mostly uses the old habit of allocating memory by malloc(), or even use stack or BSS memory for communication buffer purposes.

--
Gleb.

#76 lore raw

Re: [ofa-general] Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 9 lines · Gleb Natapov
Quoted 6 lines · Nick Piggin
Quoted 4 lines

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Can you change the spec?

Not really. It will break all existing codes.

I meant as in eg. submit changes to MPI-3

Quoted 6 lines

MPI-2 provides a call for
memory allocation (and it's beneficial to use this call for some interconnects), but many (most?) applications are still written for MPI-1 and those that are written for MPI-2 mostly uses the old habit of allocating memory by malloc(), or even use stack or BSS memory for communication buffer purposes.

OK, so MPI-2 already has some way to do that... I'm not saying that we can now completely dismiss the idea of using notifiers for this, but it is just a good data point to know.

Thanks,
Nick

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#77 lore raw

Re: [ofa-general] Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 7 lines · Nick Piggin
Quoted 6 lines · Gleb Natapov
Quoted 5 lines · Nick Piggin
Quoted 4 lines

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Can you change the spec?

Not really. It will break all existing codes.

I meant as in eg. submit changes to MPI-3

MPI spec tries to be backward compatible. And MPI-2 spec is 10 years old, but MPI-1 is still in a wider use. HPC is moving fast in terms of HW technology, but slow in terms of SW. Fortran is still hot there :)

--
Gleb.

#78 lore raw

Re: [ofa-general] Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Can you change the spec?

Not really. It will break all existing codes.

I meant as in eg. submit changes to MPI-3

MPI spec tries to be backward compatible. And MPI-2 spec is 10 years old, but MPI-1 is still in a wider use. HPC is moving fast in terms of HW technology, but slow in terms of SW. Fortran is still hot there :)

Agreed.
many many people dislike incompatible specification change.

We should accept real world spec.

- kosaki

#79 lore raw

Re: [ofa-general] Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Nick Piggin
Quoted 21 lines · Nick Piggin
Quoted 9 lines · Gleb Natapov
Quoted 6 lines · Nick Piggin
Quoted 4 lines

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Can you change the spec?

Not really. It will break all existing codes.

I meant as in eg. submit changes to MPI-3

Quoted 6 lines

MPI-2 provides a call for
memory allocation (and it's beneficial to use this call for some interconnects), but many (most?) applications are still written for MPI-1 and those that are written for MPI-2 mostly uses the old habit of allocating memory by malloc(), or even use stack or BSS memory for communication buffer purposes.

Quoted 3 lines

OK, so MPI-2 already has some way to do that... I'm not saying that we can now completely dismiss the idea of using notifiers for this, but it is just a good data point to know.

It is in MPI-2, but MPI-2 does not prohibit communication from regions not allocated by the MPI call.

Thanks,
Robin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#80 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Nick Piggin
Quoted 6 lines
Quoted 1 line

That is it. That is all our allowed interaction with the users process.

OK, when you said something along the lines of "the MPT library has control of the comm buffer", then I assumed it was an area of virtual memory which is set up as part of initialization, rather than during runtime. I guess I jumped to conclusions.

There are six regions the MPT library typically makes. The most basic one is a fixed size. It describes the MPT internal buffers, the stack, the heap, the application text, and finally the entire address space. That last region is seldom used. MPT only has control over the first two.

Quoted 7 lines
Quoted 7 lines

That doesn't seem too unreasonable, except when you compare it to how the driver currently works. Remember, this is done from a library which has no insight into what the user has done to its own virtual address space. As a result, each MPI_Send() would result in a system call (or we would need to have a set of callouts for changes to a processes VMAs) which would be a significant increase in communication overhead.

Quoted 7 lines
Quoted 4 lines

Maybe I am missing what you intend to do, but what we need is a means of tracking one processes virtual address space changes so other processes can do direct memory accesses without the need for a system call on each communication event.

Yeah it's tricky. BTW. what is the performance difference between having a system call or no?

The system call takes many microseconds and still requires the same latency of the communication. Without it, our latency is usually below two microseconds.

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Because you don't need to swap, you don't need coherency, and you are in control of the areas, then this seems like the best choice. It would allow you to use heap, stack, file-backed, anything.

You are missing one point here. The MPI specifications that have been out there for decades do not require the process use a library for allocating the buffer. I realize that is a horrible shortcoming, but that is the world we live in. Even if we could change that spec,

Can you change the spec? Are you working on it?

Even if we changed the spec, the old specs will continue to be supported. I personally am not involved. Not sure if anybody else is working this issue.

Quoted 6 lines
Quoted 3 lines

we would still need to support the existing specs. As a result, the user can change their virtual address space as they need and still expect communications be cheap.

That's true. How has it been supported up to now? Are you using these kind of notifiers in patched kernels?

At fault time, we check to see if it is an anon or mspec vma. We pin the page an insert them. The remote OS then losses synchronicity with the owning processes page tables. If an unmap, madvise, etc occurs the page tables are updated without regard to our references. Fork or exit (fork is caught using an LD_PRELOAD library) cause the user pages to be recalled from the remote side and put_page returns them to the kernel. We have documented that this loss of synchronicity is due to their action and not supported. Essentially, we rely upon the application being well behaved. To this point, that has remainded true.

Thanks,
Robin

#19 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 4 lines · Andrea Arcangeli

Yes, that's why I kept maintaining my patch and I posted the last revision to Andrew. I use pte/tlb locking of the core VM, it's unintrusive and obviously safe. Furthermore it can be extended with Christoph's stuff in a 100% backwards compatible fashion later if needed.

How would that work? You rely on the pte locking. Thus calls are all in an atomic context. I think we need a general scheme that allows sleeping when references are invalidates. Even the GRU has performance issues when using the KVM patch.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#28 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Nick Piggin
Quoted 9 lines · Nick Piggin
Quoted 6 lines · Christoph Lameter

The invalidation of address ranges in a mm_struct needs to be performed when pages are removed or permissions etc change.

If invalidate_range_begin() is called with locks held then we pass a flag into invalidate_range() to indicate that no sleeping is possible. Locks are only held for truncate and huge pages.

You can't sleep inside rcu_read_lock()!

Could you be specific? This refers to page migration? Hmmm... Guess we would need to inc the refcount there instead?

Quoted 3 lines

I must say that for a patch that is up to v8 or whatever and is posted twice a week to such a big cc list, it is kind of slack to not even test it and expect other people to review it.

It was tested with the GRU and XPmem. Andrea also reported success.

Quoted 7 lines

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver writer, then for the sanity of the VM developers (I don't want to have to understand xpmem or infiniband in order to understand how the VM works).

There are 3 different drivers that can already use it but the code is complex and not easy to review. Skeletons are easy to allow people to get started with it.

Quoted 5 lines
Quoted 5 lines
mm/memory.c +1 -0
 	lru_add_drain();
 	tlb = tlb_gather_mmu(mm, 0);
 	update_hiwater_rss(mm);
+	mmu_notifier(invalidate_range_begin, mm, address, end, atomic);
 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
Quoted 8 lines

mm/memory.c unsigned long zap_page_range(struct vm_a · line 916

Quoted 6 lines
mm/memory.c +1 -0
 	if (tlb)
 		tlb_finish_mmu(tlb, address, end);
+	mmu_notifier(invalidate_range_end, mm, address, end, atomic);
 	return end;
 }

Where do you invalidate for munmap()?

zap_page_range() called from unmap_vmas().

Quoted 4 lines

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

That can be done in a variety of ways:

1. Change VM locking

2. Not handle file backed mappings (XPmem could work mostly in such a config)

3. Keep the refcount elevated until pages are freed in another execution context.

#26 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 3 lines · Andrea Arcangeli

Well, xpmem requirements are complex. As as side effect of the simplicity of my approach, my patch is 100% safe since #v1. Now it also works for GRU and it cluster invalidates.

The patch has to satisfy RDMA, XPMEM, GRU and KVM. I keep hearing that we have a KVM only solution that works 100% (which makes me just switch ignore the rest of the argument because 100% solutions usually do not exist).

Quoted 4 lines

rcu_read_lock), no "atomic" parameters, and it doesn't open a window where sptes have a view on older pages and linux pte has view on newer pages (this can happen with remap_file_pages with my KVM swapping patch to use V8 Christoph's patch).

Ok so you are now getting away from keeping the refcount elevated? That was your design decision....

Quoted 6 lines
Quoted 4 lines

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

I also asked exactly this, glad you reasked this too.

It would have helped if you would have repeated my answers that you had already gotten before. You knew I was on vacation....

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#29 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 9 lines
Quoted 8 lines

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver writer, then for the sanity of the VM developers (I don't want to have to understand xpmem or infiniband in order to understand how the VM works).

Quoted 3 lines

There are 3 different drivers that can already use it but the code is complex and not easy to review. Skeletons are easy to allow people to get started with it.

I posted the full GRU driver late last week. It is a lot of code & somewhat difficult to understand w/o access to full chip specs (sorry). The code is fairly well commented & the parts related to TLB management should be understandable.

#81 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Nick Piggin
Quoted 3 lines · Nick Piggin

I don't know how this is supposed to solve anything. The sleeping problem happens I guess mostly in truncate. And all you are doing is putting these rmap callbacks in page_mkclean and try_to_unmap.

truncate is handled by the range invalidates. This is special code to deal with the unnap/clean of an individual page.

Quoted 7 lines

That doesn't seem right. To start with, the new callbacks aren't even called in the places where invalidate_page isn't allowed to sleep.

The problem is unmap_mapping_range, right? And unmap_mapping_range must walk the rmaps with the mmap lock held, which is why it can't sleep. And it can't hold any mmap_sem so it cannot prevent address

Nope. unmap_mapping_range is already handled by the range callbacks.

Quoted 4 lines

So in the meantime, you could have eg. a fault come in and set up a new page for one of the processes, and that page might even get exported via the same external driver. And now you have a totally inconsistent view.

The situation that you are imagining has already been dealt with by the earlier patches. This is only to allow sleeping while unmapping individual pages.

#20 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 1 line · Christoph Lameter

How would that work? You rely on the pte locking. Thus calls are all in an

I don't rely on the pte locking in #v7, exactly to satisfy GRU (so far purely theoretical) performance complains.

Quoted 1 line

atomic context. I think we need a general scheme that allows sleeping when

Calls are still in atomic context until we change the i_mmap_lock to a mutex under a CONFIG_XPMEM, or unless we boost mm_users, drop the lock and restart the loop at every different mm. In any case those changes should be under CONFIG_XPMEM IMHO given desktop users definitely don't need this (regular non-blocking mmu notifiers in my patch are all what a desktop user need as far as I can tell).

Quoted 2 lines

references are invalidates. Even the GRU has performance issues when using the KVM patch.

GRU will perform the same with #v7 or V8.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#30 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 2 lines · Christoph Lameter

Could you be specific? This refers to page migration? Hmmm... Guess we would need to inc the refcount there instead?

Argh. No its the callback list scanning. Yuck. No one noticed.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#31 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 1 line · Christoph Lameter

Could you be specific? This refers to page migration? Hmmm... Guess we

If the reader schedule, the synchronize_rcu will return in the other cpu and the objects in the list will be freed and overwritten, and when the task is scheduled back in, it'll follow dangling pointers... You can't use RCU if you want any of your invalidate methods to schedule. Otherwise it's like having zero locking.

Quoted 2 lines

2. Not handle file backed mappings (XPmem could work mostly in such a config)

IMHO that fits under your definition of "hacking something in now and then having to modify it later".

Quoted 2 lines

3. Keep the refcount elevated until pages are freed in another execution context.

Page refcount is not enough (the mmu_notifier_release will run in another cpu the moment after i_mmap_lock is unlocked) but mm_users may prevent us to change the i_mmap_lock to a mutex, but it'll slowdown truncate as it'll have to drop the lock and restart the radix tree walk every time so a change like this better fits as a separate CONFIG_XPMEM IMHO.

#32 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 9 lines · Andrea Arcangeli
Quoted 2 lines

3. Keep the refcount elevated until pages are freed in another execution context.

Page refcount is not enough (the mmu_notifier_release will run in another cpu the moment after i_mmap_lock is unlocked) but mm_users may prevent us to change the i_mmap_lock to a mutex, but it'll slowdown truncate as it'll have to drop the lock and restart the radix tree walk every time so a change like this better fits as a separate CONFIG_XPMEM IMHO.

Erm. This would also be needed by RDMA etc.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#27 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 10 lines · Christoph Lameter
Quoted 3 lines · Andrea Arcangeli

Well, xpmem requirements are complex. As as side effect of the simplicity of my approach, my patch is 100% safe since #v1. Now it also works for GRU and it cluster invalidates.

The patch has to satisfy RDMA, XPMEM, GRU and KVM. I keep hearing that we have a KVM only solution that works 100% (which makes me just switch ignore the rest of the argument because 100% solutions usually do not exist).

I only said 100% safe, I didn't imply anything other than it won't crash the kernel ;).

#v6 and #v7 only leaves XPMEM out AFIK, and that can be supported later with a CONFIG_XPMEM that purely changes some VM locking. #v7 also provides maximum performance to GRU.

Quoted 7 lines
Quoted 4 lines

rcu_read_lock), no "atomic" parameters, and it doesn't open a window where sptes have a view on older pages and linux pte has view on newer pages (this can happen with remap_file_pages with my KVM swapping patch to use V8 Christoph's patch).

Ok so you are now getting away from keeping the refcount elevated? That was your design decision....

No, I'm not getting away from it. If I would get away from it, I would be forced to implement invalidate_range_begin. However even if I don't get away from it, the fact I only implement invalidate_range_end, and that's called after the PT lock is dropped, opens a little window with lost coherency (which may not be detectable by userland anyway). But this little window is fine for KVM and it doesn't impose any security risk. But clearly proving the locking safe becomes a bit more complex in #v7 than in #v6.

Quoted 2 lines

It would have helped if you would have repeated my answers that you had already gotten before. You knew I was on vacation....

I didn't remember the BUG_ON crystal clear sorry, but not sure why you think it was your call, this was a lowlevel XPMEM question and Robin promptly answered/reminded about it infact.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#82 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Christoph Lameter
Quoted 1 line · Christoph Lameter

Nope. unmap_mapping_range is already handled by the range callbacks.

But they're called with atomic=1 on anything but anonymous memory. I understood Andrew asked to remove the atomic param and to allow sleeping for all kind of vmas. I also understood certain XPMEM customers asked to use XPMEM on something more than anonymous memory.

Quoted 1 line

The situation that you are imagining has already been dealt with [..]

I guess there's some misunderstanding, I think Nick was referring to the above problem.

#33 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 1 line · Christoph Lameter

Erm. This would also be needed by RDMA etc.

The only RDMA I know is Quadrics, and Quadrics apparently doesn't need to schedule inside the invalidate methods AFIK, so I doubt the above is true. It'd be interesting to know if IB is like Quadrics and it also doesn't require blocking to invalidate certain remote mappings.

#83 lore raw

Re: [patch 5/6] mmu_notifier: Support for drivers with revers maps (f.e. for XPmem)

In reply to Andrea Arcangeli
Quoted 7 lines · Andrea Arcangeli
Quoted 1 line · Christoph Lameter

Nope. unmap_mapping_range is already handled by the range callbacks.

But they're called with atomic=1 on anything but anonymous memory. I understood Andrew asked to remove the atomic param and to allow sleeping for all kind of vmas. I also understood certain XPMEM customers asked to use XPMEM on something more than anonymous memory.

Yes but the patch that is discussed here does not handle that situation.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#34 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 7 lines · Andrea Arcangeli
Quoted 1 line · Christoph Lameter

Erm. This would also be needed by RDMA etc.

The only RDMA I know is Quadrics, and Quadrics apparently doesn't need to schedule inside the invalidate methods AFIK, so I doubt the above is true. It'd be interesting to know if IB is like Quadrics and it also doesn't require blocking to invalidate certain remote mappings.

RDMA works across a network and I would assume that it needs confirmation that a connection has been torn down before pages can be unmapped.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#35 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 2 lines · Christoph Lameter

RDMA works across a network and I would assume that it needs confirmation that a connection has been torn down before pages can be unmapped.

Depends on the latency of the network, for example with page pinning it can even try to reduce the wait time, by tearing down the mapping in range_begin and spin waiting the ack only later in range_end.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#49 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 7 lines · Andrea Arcangeli
Quoted 1 line · Christoph Lameter

Erm. This would also be needed by RDMA etc.

The only RDMA I know is Quadrics, and Quadrics apparently doesn't need to schedule inside the invalidate methods AFIK, so I doubt the above is true. It'd be interesting to know if IB is like Quadrics and it also doesn't require blocking to invalidate certain remote mappings.

We got an answer from the IB guys already. They do not track which of their handles are being used by remote processes so neither approach will work for their purposes with the exception of straight unmaps. In that case, they could use the callout to remove TLB information and rely on the lack of page table information to kill the users process. Without changes to their library spec, I don't believe anything further is possible. If they did change their library spec, I believe they could get things to work the same way that XPMEM has gotten things to work, where a message is sent to the remote side for TLB clearing and that will require sleeping.

Thanks,
Robin

#36 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 7 lines · Andrea Arcangeli
Quoted 2 lines · Christoph Lameter

RDMA works across a network and I would assume that it needs confirmation that a connection has been torn down before pages can be unmapped.

Depends on the latency of the network, for example with page pinning it can even try to reduce the wait time, by tearing down the mapping in range_begin and spin waiting the ack only later in range_end.

What about invalidate_page()?

#37 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 1 line · Christoph Lameter

What about invalidate_page()?

That would just spin waiting an ack (just like the smp-tlb-flushing invalidates in numa already does).

Thinking more about this, we could also parallelize it with an invalidate_page_before/end. If it takes 1usec to flush remotely, scheduling would be overkill, but spending 1usec in a while loop isn't nice if we can parallelize that 1usec with the ipi-tlb-flush. Not sure if it makes sense... it certainly would be quick to add it (especially thanks to _notify ;).

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#38 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 5 lines · Andrea Arcangeli
Quoted 1 line · Christoph Lameter

What about invalidate_page()?

That would just spin waiting an ack (just like the smp-tlb-flushing invalidates in numa already does).

And thus the device driver may stop receiving data on a UP system? It will never get the ack.

Quoted 6 lines

Thinking more about this, we could also parallelize it with an invalidate_page_before/end. If it takes 1usec to flush remotely, scheduling would be overkill, but spending 1usec in a while loop isn't nice if we can parallelize that 1usec with the ipi-tlb-flush. Not sure if it makes sense... it certainly would be quick to add it (especially thanks to _notify ;).

invalidate_page_before/end could be realized as an invalidate_range_begin/end on a page sized range?

#39 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 2 lines · Christoph Lameter

And thus the device driver may stop receiving data on a UP system? It will never get the ack.

Not sure to follow, sorry.

My idea was:

post the invalidate in the mmio region of the device smp_call_function()
while (mmio device wait-bitflag is on);

Instead of the current:

smp_call_function()
post the invalidate in the mmio region of the device while (mmio device wait-bitflag is on);

To decrease the wait loop time.

Quoted 2 lines

invalidate_page_before/end could be realized as an invalidate_range_begin/end on a page sized range?

If we go this route, once you add support to xpmem, you'll have to make the anon_vma lock a mutex too, that would be fine with me though. The main reason invalidate_page exists, is to allow you to leave it as non-sleep-capable even after you make invalidate_range sleep capable, and to implement the mmu_rmap_notifiers sleep capable in all the paths that invalidate_page would be called. That was the strategy you had in your patch. I'll try to drop invalidate_page. I wonder if then you won't need the mmu_rmap_notifiers anymore.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#40 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 11 lines · Andrea Arcangeli
Quoted 2 lines · Christoph Lameter

And thus the device driver may stop receiving data on a UP system? It will never get the ack.

Not sure to follow, sorry.

My idea was:

post the invalidate in the mmio region of the device smp_call_function()
while (mmio device wait-bitflag is on);

So the device driver on UP can only operate through interrupts? If you are hogging the only cpu then driver operations may not be possible.

Quoted 11 lines
Quoted 2 lines

invalidate_page_before/end could be realized as an invalidate_range_begin/end on a page sized range?

If we go this route, once you add support to xpmem, you'll have to make the anon_vma lock a mutex too, that would be fine with me though. The main reason invalidate_page exists, is to allow you to leave it as non-sleep-capable even after you make invalidate_range sleep capable, and to implement the mmu_rmap_notifiers sleep capable in all the paths that invalidate_page would be called. That was the strategy you had in your patch. I'll try to drop invalidate_page. I wonder if then you won't need the mmu_rmap_notifiers anymore.

I am mainly concerned with making the mmu notifier a generally useful feature for multiple users. Xpmem is one example of a different user. It should be considered as one example of a different type of callback user. It is not the gold standard that you make it to be. RDMA is another and there are likely scores of others (DMA engines etc) once it becomes clear that such a feature is available. In general the mmu notifier will allows us to fix the problems caused by memory pinning and mlock by various devices and other mechanisms that need to directly access memory.

And yes I would like to get rid of the mmu_rmap_notifiers altogether. It would be much cleaner with just one mmu_notifier that can sleep in all functions.

#41 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 6 lines · Christoph Lameter
Quoted 3 lines

post the invalidate in the mmio region of the device smp_call_function()
while (mmio device wait-bitflag is on);

So the device driver on UP can only operate through interrupts? If you are hogging the only cpu then driver operations may not be possible.

There was no irq involved in the above pseudocode, the irq if something would run in the remote system. Still irqs can run fine during the while loop like they run fine on top of smp_call_function. The send-irq and the following spin-on-a-bitflag works exactly as smp_call_function except this isn't a numa-CPU to invalidate.

Quoted 3 lines

And yes I would like to get rid of the mmu_rmap_notifiers altogether. It would be much cleaner with just one mmu_notifier that can sleep in all functions.

Agreed. I just thought xpmem needed an invalidate-by-page, but I'm glad if xpmem can go in sync with the KVM/GRU/DRI model in this regard.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#42 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 3 lines · Andrea Arcangeli

Agreed. I just thought xpmem needed an invalidate-by-page, but I'm glad if xpmem can go in sync with the KVM/GRU/DRI model in this regard.

That means we need both the anon_vma locks and the i_mmap_lock to become semaphores. I think semaphores are better than mutexes. Rik and Lee saw some performance improvements because list can be traversed in parallel when the anon_vma lock is switched to be a rw lock.

Sounds like we get to a conceptually clean version here?

#43 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 4 lines · Christoph Lameter

That means we need both the anon_vma locks and the i_mmap_lock to become semaphores. I think semaphores are better than mutexes. Rik and Lee saw some performance improvements because list can be traversed in parallel when the anon_vma lock is switched to be a rw lock.

The improvement was with a rw spinlock IIRC, so I don't see how it's related to this.

Perhaps the rwlock spinlock can be changed to a rw semaphore without measurable overscheduling in the fast path. However theoretically speaking the rw_lock spinlock is more efficient than a rw semaphore in case of a little contention during the page fault fast path because the critical section is just a list_add so it'd be overkill to schedule while waiting. That's why currently it's a spinlock (or rw spinlock).

Quoted 1 line

Sounds like we get to a conceptually clean version here?

I don't have a strong opinion if it should become a semaphore unconditionally or only with a CONFIG_XPMEM=y. But keep in mind preempt-rt runs quite a bit slower, or we could rip spinlocks out of the kernel in the first place ;)

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#44 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 4 lines · Andrea Arcangeli

I don't have a strong opinion if it should become a semaphore unconditionally or only with a CONFIG_XPMEM=y. But keep in mind preempt-rt runs quite a bit slower, or we could rip spinlocks out of the kernel in the first place ;)

D you just skip comments of people on the mmu_notifier? It took me to remind you about Andrew's comments to note those. And I just responded on the XPmem issue in the morning.

Again for the gazillionth time: There will be no CONFIG_XPMEM because the functionality needs to be generic and not XPMEM specific.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#45 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Andrea Arcangeli
Quoted 8 lines · Andrea Arcangeli
Quoted 4 lines · Christoph Lameter

That means we need both the anon_vma locks and the i_mmap_lock to become semaphores. I think semaphores are better than mutexes. Rik and Lee saw some performance improvements because list can be traversed in parallel when the anon_vma lock is switched to be a rw lock.

The improvement was with a rw spinlock IIRC, so I don't see how it's related to this.

AFAICT The rw semaphore fastpath is similar in performance to a rw spinlock.

Quoted 2 lines

Perhaps the rwlock spinlock can be changed to a rw semaphore without measurable overscheduling in the fast path. However theoretically

Overscheduling? You mean overhead?

Quoted 5 lines

speaking the rw_lock spinlock is more efficient than a rw semaphore in case of a little contention during the page fault fast path because the critical section is just a list_add so it'd be overkill to schedule while waiting. That's why currently it's a spinlock (or rw spinlock).

On the other hand a semaphore puts the process to sleep and may actually improve performance because there is less time spend in a busy loop. Other processes may do something useful and we stay off the contended cacheline reducing traffic on the interconnect.

Quoted 2 lines

preempt-rt runs quite a bit slower, or we could rip spinlocks out of the kernel in the first place ;)

The question is why that is the case and it seesm that there are issues with interrupt on/off that are important here and particularly significant with the SLAB allocator (significant hacks there to deal with that issue). The fastpath that we have in the works for SLUB may address a large part of that issue because it no longer relies on disabling interrupts.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#46 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 8 lines · Christoph Lameter
Quoted 6 lines · Andrea Arcangeli
Quoted 4 lines · Christoph Lameter

That means we need both the anon_vma locks and the i_mmap_lock to become semaphores. I think semaphores are better than mutexes. Rik and Lee saw some performance improvements because list can be traversed in parallel when the anon_vma lock is switched to be a rw lock.

Quoted 5 lines
Quoted 2 lines

The improvement was with a rw spinlock IIRC, so I don't see how it's related to this.

AFAICT The rw semaphore fastpath is similar in performance to a rw spinlock.

read side is taken in the slow path.

write side is taken in the fast path.

pagefault is fast path, VM during swapping is slow path.

Quoted 4 lines
Quoted 2 lines

Perhaps the rwlock spinlock can be changed to a rw semaphore without measurable overscheduling in the fast path. However theoretically

Overscheduling? You mean overhead?

The only possible overhead that a rw semaphore could ever generate vs a rw lock is overscheduling.

Quoted 10 lines
Quoted 5 lines

speaking the rw_lock spinlock is more efficient than a rw semaphore in case of a little contention during the page fault fast path because the critical section is just a list_add so it'd be overkill to schedule while waiting. That's why currently it's a spinlock (or rw spinlock).

On the other hand a semaphore puts the process to sleep and may actually improve performance because there is less time spend in a busy loop. Other processes may do something useful and we stay off the contended cacheline reducing traffic on the interconnect.

Yes, that's the positive side, the negative side is that you'll put the task in uninterruptible sleep and call schedule() and require a wakeup, because a list_add taking <1usec is running in the other cpu. No other downside. But that's the only reason it's a spinlock right now, infact there can't be any other reason.

#47 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 4 lines · Andrea Arcangeli
Quoted 2 lines

AFAICT The rw semaphore fastpath is similar in performance to a rw spinlock.

read side is taken in the slow path.

Slowpath meaning VM slowpath or lock slow path? Its seems that the rwsem read side path is pretty efficient:

static inline void __down_read(struct rw_semaphore *sem) {
__asm__ __volatile__(
"# beginning down_read\n\t"

LOCK_PREFIX     "  incl      (%%eax)\n\t" /* adds 0x00000001, returns the old value */
                "  jns        1f\n"
                "  call call_rwsem_down_read_failed\n"
                "1:\n\t"
                "# ending down_read\n\t"
                : "+m" (sem->count)
                : "a" (sem)
                : "memory", "cc");
}
Quoted 4 lines

write side is taken in the fast path.

pagefault is fast path, VM during swapping is slow path.

Not sure what you are saying here. A pagefault should be considered as a fast path and swapping is not performance critical?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Perhaps the rwlock spinlock can be changed to a rw semaphore without measurable overscheduling in the fast path. However theoretically

Overscheduling? You mean overhead?

The only possible overhead that a rw semaphore could ever generate vs a rw lock is overscheduling.

Ok too many calls to schedule() because the slow path (of the semaphore) is taken?

Quoted 10 lines
Quoted 4 lines

On the other hand a semaphore puts the process to sleep and may actually improve performance because there is less time spend in a busy loop. Other processes may do something useful and we stay off the contended cacheline reducing traffic on the interconnect.

Yes, that's the positive side, the negative side is that you'll put the task in uninterruptible sleep and call schedule() and require a wakeup, because a list_add taking <1usec is running in the other cpu. No other downside. But that's the only reason it's a spinlock right now, infact there can't be any other reason.

But that is only happening for the contended case. Certainly a spinlock is better for 2p system but the more processors content for the lock (and the longer the hold off is, typical for the processors with 4p or 8p or more) the better a semaphore will work.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#48 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 8 lines · Christoph Lameter
Quoted 4 lines · Andrea Arcangeli
Quoted 2 lines

AFAICT The rw semaphore fastpath is similar in performance to a rw spinlock.

read side is taken in the slow path.

Slowpath meaning VM slowpath or lock slow path? Its seems that the rwsem

With slow path I meant the VM. Sorry if that was confusing given locks also have fast paths (no contention) and slow paths (contention).

Quoted 1 line

read side path is pretty efficient:

Yes. The assembly doesn't worry me at all.

Quoted 4 lines
Quoted 1 line

pagefault is fast path, VM during swapping is slow path.

Not sure what you are saying here. A pagefault should be considered as a fast path and swapping is not performance critical?

Yes, swapping is I/O bound and it rarely becomes CPU hog in the common case.

There are corner case workloads (including OOM) where swapping can become cpu bound (that's also where rwlock helps). But certainly the speed of fork() and a page fault, is critical for _everyone_, not just a few workloads and setups.

Quoted 2 lines

Ok too many calls to schedule() because the slow path (of the semaphore) is taken?

Yes, that's the only possible worry when converting a spinlock to mutex.

Quoted 4 lines

But that is only happening for the contended case. Certainly a spinlock is better for 2p system but the more processors content for the lock (and the longer the hold off is, typical for the processors with 4p or 8p or more) the better a semaphore will work.

Sure. That's also why the PT lock switches for >4way compiles. Config option helps to keep the VM optimal for everyone. Here it is possible it won't be necessary but I can't be sure given both i_mmap_lock and anon-vma lock are used in some many places. Some TPC comparison would be nice before making a default switch IMHO.

#50 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

In reply to Christoph Lameter
Quoted 11 lines · Christoph Lameter
Quoted 9 lines · Nick Piggin

On Friday 15 February 2008 17:49, Christoph Lameter wrote:

Also, what we are going to need here are not skeleton drivers that just do all the *easy* bits (of registering their callbacks), but actual fully working examples that do everything that any real driver will need to do. If not for the sanity of the driver writer, then for the sanity of the VM developers (I don't want to have to understand xpmem or infiniband in order to understand how the VM works).

Quoted 3 lines

There are 3 different drivers that can already use it but the code is complex and not easy to review. Skeletons are easy to allow people to get started with it.

Your skeleton is just registering notifiers and saying

/* you fill the hard part in */

If somebody needs a skeleton in order just to register the notifiers, then almost by definition they are unqualified to write the hard part ;)

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
mm/memory.c +1 -0
 	lru_add_drain();
 	tlb = tlb_gather_mmu(mm, 0);
 	update_hiwater_rss(mm);
+	mmu_notifier(invalidate_range_begin, mm, address, end, atomic);
Quoted 8 lines

mm/memory.c unsigned long zap_page_range(struct vm_a · line 916

Quoted 6 lines
mm/memory.c +1 -0
 	end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
 	if (tlb)
 		tlb_finish_mmu(tlb, address, end);
+	mmu_notifier(invalidate_range_end, mm, address, end, atomic);
 	return end;
 }

Where do you invalidate for munmap()?

Quoted 1 line

zap_page_range() called from unmap_vmas().

But it is not allowed to sleep. Where do you call the sleepable one from?

Quoted 7 lines
Quoted 4 lines

Also, how to you resolve the case where you are not allowed to sleep? I would have thought either you have to handle it, in which case nobody needs to sleep; or you can't handle it, in which case the code is broken.

That can be done in a variety of ways:

Quoted 7 lines

1. Change VM locking

2. Not handle file backed mappings (XPmem could work mostly in such a config)

3. Keep the refcount elevated until pages are freed in another execution context.

OK, there are ways to solve it or hack around it. But this is exactly why I think the implementations should be kept seperate. Andrea's notifiers are coherent, work on all types of mappings, and will hopefully match closely the regular TLB invalidation sequence in the Linux VM (at the moment it is quite close, but I hope to make it a bit closer) so that it requires almost no changes to the mm.

All the other things to try to make it sleep are either hacking holes in it (eg by removing coherency). So I don't think it is reasonable to require that any patch handle all cases. I actually think Andrea's patch is quite nice and simple itself, wheras I am against the patches that you posted.

What about a completely different approach... XPmem runs over NUMAlink, right? Why not provide some non-sleeping way to basically IPI remote nodes over the NUMAlink where they can process the invalidation? If you intra-node cache coherency has to run over this link anyway, then presumably it is capable.

Or another idea, why don't you LD_PRELOAD in the MPT library to also intercept munmap, mprotect, mremap etc as well as just fork()? That would give you similarly "good enough" coherency as the mmu notifier patches except that you can't swap (which Robin said was not a big problem).

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#51 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 7 lines · Nick Piggin

Your skeleton is just registering notifiers and saying

/* you fill the hard part in */

If somebody needs a skeleton in order just to register the notifiers, then almost by definition they are unqualified to write the hard part ;)

Its also providing a locking scheme.

Quoted 6 lines

OK, there are ways to solve it or hack around it. But this is exactly why I think the implementations should be kept seperate. Andrea's notifiers are coherent, work on all types of mappings, and will hopefully match closely the regular TLB invalidation sequence in the Linux VM (at the moment it is quite close, but I hope to make it a bit closer) so that it requires almost no changes to the mm.

Then put it into the arch code for TLB invalidation. Paravirt ops gives good examples on how to do that.

Quoted 5 lines

What about a completely different approach... XPmem runs over NUMAlink, right? Why not provide some non-sleeping way to basically IPI remote nodes over the NUMAlink where they can process the invalidation? If you intra-node cache coherency has to run over this link anyway, then presumably it is capable.

There is another Linux instance at the remote end that first has to remove its own ptes. Also would not work for Inifiniband and other solutions. All the approaches that require evictions in an atomic context are limiting the approach and do not allow the generic functionality that we want in order to not add alternate APIs for this.

Quoted 5 lines

Or another idea, why don't you LD_PRELOAD in the MPT library to also intercept munmap, mprotect, mremap etc as well as just fork()? That would give you similarly "good enough" coherency as the mmu notifier patches except that you can't swap (which Robin said was not a big problem).

The good enough solution right now is to pin pages by elevating refcounts.

#52 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 10 lines · Christoph Lameter
Quoted 7 lines · Nick Piggin

Your skeleton is just registering notifiers and saying

/* you fill the hard part in */

If somebody needs a skeleton in order just to register the notifiers, then almost by definition they are unqualified to write the hard part ;)

Its also providing a locking scheme.

Not the full locking scheme. If you have a look at the real code required to do it, it is non trivial.

Quoted 9 lines
Quoted 6 lines

OK, there are ways to solve it or hack around it. But this is exactly why I think the implementations should be kept seperate. Andrea's notifiers are coherent, work on all types of mappings, and will hopefully match closely the regular TLB invalidation sequence in the Linux VM (at the moment it is quite close, but I hope to make it a bit closer) so that it requires almost no changes to the mm.

Then put it into the arch code for TLB invalidation. Paravirt ops gives good examples on how to do that.

Put what into arch code?

Quoted 8 lines
Quoted 5 lines

What about a completely different approach... XPmem runs over NUMAlink, right? Why not provide some non-sleeping way to basically IPI remote nodes over the NUMAlink where they can process the invalidation? If you intra-node cache coherency has to run over this link anyway, then presumably it is capable.

There is another Linux instance at the remote end that first has to remove its own ptes.

Yeah, what's the problem?

Quoted 2 lines

Also would not work for Inifiniband and other
solutions.

infiniband doesn't want it. Other solutions is just handwaving, because if we don't know what the other soloutions are, then we can't make any sort of informed choices.

Quoted 3 lines

All the approaches that require evictions in an atomic context are limiting the approach and do not allow the generic functionality that we want in order to not add alternate APIs for this.

The only generic way to do this that I have seen (and the only proposed way that doesn't add alternate APIs for that matter) is turning VM locks into sleeping locks. In which case, Andrea's notifiers will work just fine (except for relatively minor details like rcu list scanning).

So I don't see what you're arguing for. There is no requirement that we support sleeping notifiers in the same patch as non-sleeping ones. Considering the simplicity of the non-sleeping notifiers and the problems with sleeping ones, I think it is pretty clear that they are different beasts (unless VM locking is changed).

Quoted 8 lines
Quoted 5 lines

Or another idea, why don't you LD_PRELOAD in the MPT library to also intercept munmap, mprotect, mremap etc as well as just fork()? That would give you similarly "good enough" coherency as the mmu notifier patches except that you can't swap (which Robin said was not a big problem).

The good enough solution right now is to pin pages by elevating refcounts.

Which kind of leads to the question of why do you need any further kernel patches if that is good enough?

#53 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 4 lines · Nick Piggin
Quoted 2 lines

Then put it into the arch code for TLB invalidation. Paravirt ops gives good examples on how to do that.

Put what into arch code?

The mmu notifier code.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines

What about a completely different approach... XPmem runs over NUMAlink, right? Why not provide some non-sleeping way to basically IPI remote nodes over the NUMAlink where they can process the invalidation? If you intra-node cache coherency has to run over this link anyway, then presumably it is capable.

There is another Linux instance at the remote end that first has to remove its own ptes.

Yeah, what's the problem?

The remote end has to invalidate the page which involves locking etc.

Quoted 6 lines
Quoted 2 lines

Also would not work for Inifiniband and other
solutions.

infiniband doesn't want it. Other solutions is just handwaving, because if we don't know what the other soloutions are, then we can't make any sort of informed choices.

We need a solution in general to avoid the pinning problems. Infiniband has those too.

Quoted 8 lines
Quoted 3 lines

All the approaches that require evictions in an atomic context are limiting the approach and do not allow the generic functionality that we want in order to not add alternate APIs for this.

The only generic way to do this that I have seen (and the only proposed way that doesn't add alternate APIs for that matter) is turning VM locks into sleeping locks. In which case, Andrea's notifiers will work just fine (except for relatively minor details like rcu list scanning).

No they wont. As you pointed out the callback need RCU locking.

Quoted 5 lines
Quoted 2 lines

The good enough solution right now is to pin pages by elevating refcounts.

Which kind of leads to the question of why do you need any further kernel patches if that is good enough?

Well its good enough with severe problems during reclaim, livelocks etc. One could improve on that scheme through Rik's work trying to add a new page flag that mark pinned pages and then keep them off the LRUs and limiting their number. Having pinned page would limit the ability to reclaim by the VM and make page migration, memory unplug etc impossible. It is better to have notifier scheme that allows to tell a device driver to free up the memory it has mapped.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

#54 lore raw

Re: [patch 2/6] mmu_notifier: Callbacks to invalidate address ranges

Quoted 7 lines · Christoph Lameter
Quoted 4 lines · Nick Piggin
Quoted 2 lines

Then put it into the arch code for TLB invalidation. Paravirt ops gives good examples on how to do that.

Put what into arch code?

The mmu notifier code.

It isn't arch specific.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

What about a completely different approach... XPmem runs over NUMAlink, right? Why not provide some non-sleeping way to basically IPI remote nodes over the NUMAlink where they can process the invalidation? If you intra-node cache coherency has to run over this link anyway, then presumably it is capable.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

There is another Linux instance at the remote end that first has to remove its own ptes.

Yeah, what's the problem?

The remote end has to invalidate the page which involves locking etc.

I don't see what the problem is.

Quoted 14 lines
Quoted 6 lines
Quoted 2 lines

Also would not work for Inifiniband and other
solutions.

infiniband doesn't want it. Other solutions is just handwaving, because if we don't know what the other soloutions are, then we can't make any sort of informed choices.

We need a solution in general to avoid the pinning problems. Infiniband has those too.

Quoted 4 lines
Quoted 3 lines

All the approaches that require evictions in an atomic context are limiting the approach and do not allow the generic functionality that we want in order to not add alternate APIs for this.

Quoted 6 lines
Quoted 4 lines

The only generic way to do this that I have seen (and the only proposed way that doesn't add alternate APIs for that matter) is turning VM locks into sleeping locks. In which case, Andrea's notifiers will work just fine (except for relatively minor details like rcu list scanning).

No they wont. As you pointed out the callback need RCU locking.

That can be fixed easily.

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines

The good enough solution right now is to pin pages by elevating refcounts.

Which kind of leads to the question of why do you need any further kernel patches if that is good enough?

Well its good enough with severe problems during reclaim, livelocks etc. One could improve on that scheme through Rik's work trying to add a new page flag that mark pinned pages and then keep them off the LRUs and limiting their number. Having pinned page would limit the ability to reclaim by the VM and make page migration, memory unplug etc impossible.

Well not impossible. You could have a callback to invalidate the remote TLB and drop the pin on a given page.

Quoted 2 lines

It is better to have notifier scheme that allows to tell a device driver to free up the memory it has mapped.

Yeah, it would be nice for those people with clusters of Altixes. Doesn't mean it has to go upstream, though.