Skip to contentSkip to conversation

mm: madvise: MADV_POPULATE for quick pre-faulting

RFC PATCH In review Dave Hansen 3 participants 10 messages Jun 27, 2013 – Jul 15, 2013

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

RFC PATCH 1/1 mm: madvise: MADV_POPULATE for quick pre-faulting

I've been doing some testing involving large amounts of page cache. It's quite painful to get hundreds of GB of page cache mapped in, especially when I am trying to do it in parallel threads. This is true even when the page cache is already allocated and I only need to map it in. The test:

1. take 160 16MB files
2. clone 160 threads, mmap the 16MB files, and either a. walk through the file touching each page b. run MADV_POPULATE on the file
3. MADV_DONTNEED on the mmap()'d area

160 threads/processes:
faulting | MADV_POPULATE

  Threads:       698 |        102239 (146x speedup)
Proceeses:    154247 |        297518 (1.9x speedup)

single threaded:

	    faulting | MADV_POPULATE
                1908 |          3710 (1.9x speedup)

To fix the thread suckage, this patch just walks the VMAs and maps all the pages in. Since it does a bunch of them in one go, it amortizes the cost of acquiring the mmap_sem across all of those pages.

FAQ:

Why do threads suck so much?

Bouncing the mmap_sem cacheline around, plus anything else that we write to during a fault. We do one page, move the cachelines to another CPU, do one more page, etc...

Does MADV_DONTNEED work for this?

No. It brings the pages in to the page cache, but does not map them the way it is implemented at the moment. I guess we'd be within our rights to make it behave like MADV_POPULATE if we want though.

---

 linux.git-davehans/include/uapi/asm-generic/mman-common.h |    1 
 linux.git-davehans/mm/madvise.c                           |   40 +++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
include/uapi/asm-generic/mman-common.h renamed from include/uapi/asm-generic/mman-common.h~madv_populate +1 -0
diff -puN include/uapi/asm-generic/mman-common.h~madv_populate include/uapi/asm-generic/mman-common.h
@@ -51,6 +51,7 @@
 #define MADV_DONTDUMP   16		/* Explicity exclude from the core dump,
 					   overrides the coredump filter bits */
 #define MADV_DODUMP	17		/* Clear the MADV_NODUMP flag */
+#define MADV_POPULATE	18		/* Fill in mapping like faults would */
 
 /* compatibility flags */
 #define MAP_FILE	0
mm/madvise.c renamed from mm/madvise.c~madv_populate +39 -1
diff -puN mm/madvise.c~madv_populate mm/madvise.c
@@ -19,6 +19,7 @@
 #include <linux/blkdev.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
+#include "internal.h"
 
 /*
  * Any behaviour which results in changes to the vma->vm_flags needs to
@@ -31,6 +32,7 @@ static int madvise_need_mmap_write(int b
 	case MADV_REMOVE:
 	case MADV_WILLNEED:
 	case MADV_DONTNEED:
+	case MADV_POPULATE:
 		return 0;
 	default:
 		/* be safe, default to 1. list exceptions explicitly */
@@ -252,6 +254,39 @@ static long madvise_willneed(struct vm_a
 }
 
 /*
+ * Do not just populate the page cache (WILLNEED), also map the pages.
+ */
+static long madvise_populate(struct vm_area_struct * vma,
+			     struct vm_area_struct ** prev,
+			     unsigned long start, unsigned long end)
+{
+	struct file *file = vma->vm_file;
+	int locked = 1;
+	int ret;
+
+	if (file && file->f_mapping->a_ops->get_xip_mem) {
+		/* no bad return value, but ignore advice */
+		return 0;
+	}
+
+	ret = __mlock_vma_pages_range(vma, start, end, &locked);
+	/*
+	 * Make sure that out down_read() matches (read vs.
+	 * write) what we did in sys_madvise.
+	 */
+	BUG_ON(madvise_need_mmap_write(MADV_POPULATE));
+	if (!locked) {
+		down_read(&current->mm->mmap_sem);
+		/* tell sys_madvise we drop mmap_sem: */
+		*prev = NULL;
+	} else {
+		*prev = vma;
+	}
+
+	return ret;
+}
+
+/*
  * Application no longer needs these pages.  If the pages are dirty,
  * it's OK to just throw them away.  The app will be more careful about
  * data it wants to keep.  Be sure to free swap resources too.  The
@@ -378,6 +413,8 @@ madvise_vma(struct vm_area_struct *vma,
 		return madvise_remove(vma, prev, start, end);
 	case MADV_WILLNEED:
 		return madvise_willneed(vma, prev, start, end);
+	case MADV_POPULATE:
+		return madvise_populate(vma, prev, start, end);
 	case MADV_DONTNEED:
 		return madvise_dontneed(vma, prev, start, end);
 	default:
@@ -407,6 +444,7 @@ madvise_behavior_valid(int behavior)
 #endif
 	case MADV_DONTDUMP:
 	case MADV_DODUMP:
+	case MADV_POPULATE:
 		return 1;
 
 	default:
@@ -536,7 +574,7 @@ SYSCALL_DEFINE3(madvise, unsigned long,
 			goto out;
 		if (prev)
 			vma = prev->vm_next;
-		else	/* madvise_remove dropped mmap_sem */
+		else	/* madvise_remove/populate dropped mmap_sem */
 			vma = find_vma(current->mm, start);
 	}
 out:

_

--
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>

#1 lore raw

Hi Dave,

Quoted 11 lines · Dave Hansen

I've been doing some testing involving large amounts of page cache. It's quite painful to get hundreds of GB of page cache mapped in, especially when I am trying to do it in parallel threads. This is true even when the page cache is already allocated and I only need to map it in. The test:

1. take 160 16MB files
2. clone 160 threads, mmap the 16MB files, and either a. walk through the file touching each page

Why not change MAP_POPULATE flag in mmap(2)? Now it is only for private mappings. But maybe we could let it support shared mapping.

Regards,
- Zheng

Quoted 139 lines

b. run MADV_POPULATE on the file
3. MADV_DONTNEED on the mmap()'d area

160 threads/processes:
faulting | MADV_POPULATE

  Threads:       698 |        102239 (146x speedup)
Proceeses:    154247 |        297518 (1.9x speedup)

single threaded:

	    faulting | MADV_POPULATE
                1908 |          3710 (1.9x speedup)

To fix the thread suckage, this patch just walks the VMAs and maps all the pages in. Since it does a bunch of them in one go, it amortizes the cost of acquiring the mmap_sem across all of those pages.

FAQ:

Why do threads suck so much?

Bouncing the mmap_sem cacheline around, plus anything else that we write to during a fault. We do one page, move the cachelines to another CPU, do one more page, etc...

Does MADV_DONTNEED work for this?

No. It brings the pages in to the page cache, but does not map them the way it is implemented at the moment. I guess we'd be within our rights to make it behave like MADV_POPULATE if we want though.

---

 linux.git-davehans/include/uapi/asm-generic/mman-common.h |    1 
 linux.git-davehans/mm/madvise.c                           |   40 +++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)

diff -puN include/uapi/asm-generic/mman-common.h~madv_populate include/uapi/asm-generic/mman-common.h

include/uapi/asm-generic/mman-common.h renamed from include/uapi/asm-generic/mman-common.h~madv_populate +1 -0
@@ -51,6 +51,7 @@
 #define MADV_DONTDUMP   16		/* Explicity exclude from the core dump,
 					   overrides the coredump filter bits */
 #define MADV_DODUMP	17		/* Clear the MADV_NODUMP flag */
+#define MADV_POPULATE	18		/* Fill in mapping like faults would */
 
 /* compatibility flags */
 #define MAP_FILE	0

diff -puN mm/madvise.c~madv_populate mm/madvise.c

mm/madvise.c renamed from mm/madvise.c~madv_populate +39 -1
@@ -19,6 +19,7 @@
 #include <linux/blkdev.h>
 #include <linux/swap.h>
 #include <linux/swapops.h>
+#include "internal.h"
 
 /*
  * Any behaviour which results in changes to the vma->vm_flags needs to
@@ -31,6 +32,7 @@ static int madvise_need_mmap_write(int b
 	case MADV_REMOVE:
 	case MADV_WILLNEED:
 	case MADV_DONTNEED:
+	case MADV_POPULATE:
 		return 0;
 	default:
 		/* be safe, default to 1. list exceptions explicitly */
@@ -252,6 +254,39 @@ static long madvise_willneed(struct vm_a
 }
 
 /*
+ * Do not just populate the page cache (WILLNEED), also map the pages.
+ */
+static long madvise_populate(struct vm_area_struct * vma,
+			     struct vm_area_struct ** prev,
+			     unsigned long start, unsigned long end)
+{
+	struct file *file = vma->vm_file;
+	int locked = 1;
+	int ret;
+
+	if (file && file->f_mapping->a_ops->get_xip_mem) {
+		/* no bad return value, but ignore advice */
+		return 0;
+	}
+
+	ret = __mlock_vma_pages_range(vma, start, end, &locked);
+	/*
+	 * Make sure that out down_read() matches (read vs.
+	 * write) what we did in sys_madvise.
+	 */
+	BUG_ON(madvise_need_mmap_write(MADV_POPULATE));
+	if (!locked) {
+		down_read(&current->mm->mmap_sem);
+		/* tell sys_madvise we drop mmap_sem: */
+		*prev = NULL;
+	} else {
+		*prev = vma;
+	}
+
+	return ret;
+}
+
+/*
  * Application no longer needs these pages.  If the pages are dirty,
  * it's OK to just throw them away.  The app will be more careful about
  * data it wants to keep.  Be sure to free swap resources too.  The
@@ -378,6 +413,8 @@ madvise_vma(struct vm_area_struct *vma,
 		return madvise_remove(vma, prev, start, end);
 	case MADV_WILLNEED:
 		return madvise_willneed(vma, prev, start, end);
+	case MADV_POPULATE:
+		return madvise_populate(vma, prev, start, end);
 	case MADV_DONTNEED:
 		return madvise_dontneed(vma, prev, start, end);
 	default:
@@ -407,6 +444,7 @@ madvise_behavior_valid(int behavior)
 #endif
 	case MADV_DONTDUMP:
 	case MADV_DODUMP:
+	case MADV_POPULATE:
 		return 1;
 
 	default:
@@ -536,7 +574,7 @@ SYSCALL_DEFINE3(madvise, unsigned long,
 			goto out;
 		if (prev)
 			vma = prev->vm_next;
-		else	/* madvise_remove dropped mmap_sem */
+		else	/* madvise_remove/populate dropped mmap_sem */
 			vma = find_vma(current->mm, start);
 	}
 out:

_

Quoted 5 lines

--
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>

--
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>

#2 lore raw
Quoted 7 lines · Zheng Liu
Quoted 7 lines

I've been doing some testing involving large amounts of page cache. It's quite painful to get hundreds of GB of page cache mapped in, especially when I am trying to do it in parallel threads. This is true even when the page cache is already allocated and I only need to map it in. The test:

Quoted 6 lines
Quoted 3 lines

1. take 160 16MB files
2. clone 160 threads, mmap the 16MB files, and either a. walk through the file touching each page

Why not change MAP_POPULATE flag in mmap(2)? Now it is only for private mappings. But maybe we could let it support shared mapping.

Adding that support to mmap() will certainly _help_ some folks. But, anything that mmap()s something is taking mmap_sem for write. That means that threaded apps doing mmap()/munmap() frequently are _not_ scalable.

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

--
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>

#3 lore raw
Quoted 15 lines · Dave Hansen
Quoted 7 lines · Zheng Liu
Quoted 7 lines

I've been doing some testing involving large amounts of page cache. It's quite painful to get hundreds of GB of page cache mapped in, especially when I am trying to do it in parallel threads. This is true even when the page cache is already allocated and I only need to map it in. The test:

Quoted 6 lines
Quoted 3 lines

1. take 160 16MB files
2. clone 160 threads, mmap the 16MB files, and either a. walk through the file touching each page

Why not change MAP_POPULATE flag in mmap(2)? Now it is only for private mappings. But maybe we could let it support shared mapping.

Quoted 7 lines

Adding that support to mmap() will certainly _help_ some folks. But, anything that mmap()s something is taking mmap_sem for write. That means that threaded apps doing mmap()/munmap() frequently are _not_ scalable.

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

I look at the code, and it seems that we will handle MAP_POPULATE flag after we release mmap_sem locking in vm_mmap_pgoff():

                down_write(&mm->mmap_sem);
                ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
                                    &populate);
                up_write(&mm->mmap_sem);
                if (populate)
                        mm_populate(ret, populate);

Am I missing something?

Regards,
- Zheng

--
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>

#4 lore raw
Quoted 5 lines · Zheng Liu
Quoted 2 lines
Quoted 2 lines

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

I look at the code, and it seems that we will handle MAP_POPULATE flag after we release mmap_sem locking in vm_mmap_pgoff():

Quoted 8 lines
                down_write(&mm->mmap_sem);
                ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
                                    &populate);
                up_write(&mm->mmap_sem);
                if (populate)
                        mm_populate(ret, populate);

Am I missing something?

I went and did my same test using mmap(MAP_POPULATE)/munmap() pair versus using MADV_POPULATE in 160 threads in parallel.

MADV_POPULATE was about 10x faster in the threaded configuration.

With MADV_POPULATE, the biggest cost is shipping the mmap_sem cacheline around so that we can write the reader count update in to it. With mmap(), there is a lot of _contention_ on that lock which is much, much more expensive than simply bouncing a cacheline around.

--
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>

#5 lore raw
Quoted 18 lines · Dave Hansen
Quoted 5 lines · Zheng Liu
Quoted 2 lines
Quoted 2 lines

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

I look at the code, and it seems that we will handle MAP_POPULATE flag after we release mmap_sem locking in vm_mmap_pgoff():

Quoted 8 lines
                down_write(&mm->mmap_sem);
                ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
                                    &populate);
                up_write(&mm->mmap_sem);
                if (populate)
                        mm_populate(ret, populate);

Am I missing something?

I went and did my same test using mmap(MAP_POPULATE)/munmap() pair versus using MADV_POPULATE in 160 threads in parallel.

Quoted 6 lines

MADV_POPULATE was about 10x faster in the threaded configuration.

With MADV_POPULATE, the biggest cost is shipping the mmap_sem cacheline around so that we can write the reader count update in to it. With mmap(), there is a lot of _contention_ on that lock which is much, much more expensive than simply bouncing a cacheline around.

Thanks for your explanation.

FWIW, it would be great if we can let MAP_POPULATE flag support shared mappings because in our product system there has a lot of applications that uses mmap(2) and then pre-faults this mapping. Currently these applications need to pre-fault the mapping manually.

Regards,
- Zheng

--
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>

#6 lore raw
Quoted 4 lines · Zheng Liu

FWIW, it would be great if we can let MAP_POPULATE flag support shared mappings because in our product system there has a lot of applications that uses mmap(2) and then pre-faults this mapping. Currently these applications need to pre-fault the mapping manually.

Are you sure it doesn't? From a cursory look at the code, it looked to me like it would populate anonymous and file-backed, but I didn't double-check experimentally.

--
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>

#7 lore raw
Quoted 9 lines · Dave Hansen
Quoted 4 lines · Zheng Liu

FWIW, it would be great if we can let MAP_POPULATE flag support shared mappings because in our product system there has a lot of applications that uses mmap(2) and then pre-faults this mapping. Currently these applications need to pre-fault the mapping manually.

Are you sure it doesn't? From a cursory look at the code, it looked to me like it would populate anonymous and file-backed, but I didn't double-check experimentally.

Thanks for pointing it out. I write a program to test this issue, and it seems to me that it can populate a shared mapping. But in manpage it describes as below:

MAP_POPULATE (since Linux 2.5.46)
Populate (prefault) page tables for a mapping. For a file mapping, this causes read-ahead on the file. Later accesses to the mapping will not be blocked by page faults. MAP_POPULATE is only supported for private mappings since Linux 2.6.23.

This page is part of release 3.24 of the Linux man-pages project. I am not sure whether it has been updated or not.

Regards,
- Zheng

--
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>

#8 lore raw
In reply to Zheng Liu
Quoted 24 lines · Zheng Liu
Quoted 17 lines · Dave Hansen
Quoted 5 lines · Zheng Liu
Quoted 2 lines
Quoted 2 lines

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

I look at the code, and it seems that we will handle MAP_POPULATE flag after we release mmap_sem locking in vm_mmap_pgoff():

Quoted 8 lines
                 down_write(&mm->mmap_sem);
                 ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
                                     &populate);
                 up_write(&mm->mmap_sem);
                 if (populate)
                         mm_populate(ret, populate);

Am I missing something?

I went and did my same test using mmap(MAP_POPULATE)/munmap() pair versus using MADV_POPULATE in 160 threads in parallel.

Quoted 6 lines

MADV_POPULATE was about 10x faster in the threaded configuration.

With MADV_POPULATE, the biggest cost is shipping the mmap_sem cacheline around so that we can write the reader count update in to it. With mmap(), there is a lot of _contention_ on that lock which is much, much more expensive than simply bouncing a cacheline around.

Quoted 6 lines

Thanks for your explanation.

FWIW, it would be great if we can let MAP_POPULATE flag support shared mappings because in our product system there has a lot of applications that uses mmap(2) and then pre-faults this mapping. Currently these applications need to pre-fault the mapping manually.

How do you pre-fault the mapping manually in your product system? By walking through the file touching each page?

Quoted 8 lines

Regards,
- Zheng
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
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>

#9 lore raw
Quoted 27 lines · Sam Ben
Quoted 26 lines · Zheng Liu
Quoted 17 lines · Dave Hansen
Quoted 5 lines · Zheng Liu
Quoted 2 lines
Quoted 2 lines

IOW, a process needing to do a bunch of MAP_POPULATEs isn't parallelizable, but one using this mechanism would be.

I look at the code, and it seems that we will handle MAP_POPULATE flag after we release mmap_sem locking in vm_mmap_pgoff():

Quoted 8 lines
                down_write(&mm->mmap_sem);
                ret = do_mmap_pgoff(file, addr, len, prot, flag, pgoff,
                                    &populate);
                up_write(&mm->mmap_sem);
                if (populate)
                        mm_populate(ret, populate);

Am I missing something?

I went and did my same test using mmap(MAP_POPULATE)/munmap() pair versus using MADV_POPULATE in 160 threads in parallel.

Quoted 6 lines

MADV_POPULATE was about 10x faster in the threaded configuration.

With MADV_POPULATE, the biggest cost is shipping the mmap_sem cacheline around so that we can write the reader count update in to it. With mmap(), there is a lot of _contention_ on that lock which is much, much more expensive than simply bouncing a cacheline around.

Thanks for your explanation.

Quoted 7 lines
Quoted 4 lines

FWIW, it would be great if we can let MAP_POPULATE flag support shared mappings because in our product system there has a lot of applications that uses mmap(2) and then pre-faults this mapping. Currently these applications need to pre-fault the mapping manually.

How do you pre-fault the mapping manually in your product system? By walking through the file touching each page?

Yes, in our product system most applications do like this.

Regards,
- Zheng

--
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>