Skip to contentSkip to conversation

madvise MADV_DONTFORK/MADV_DOFORK

PATCH New Michael S. Tsirkin 8 participants 30 messages Feb 13, 2006 – Feb 15, 2006

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

OK, I guess its time to start the push for merging this patch. Probably not 2.6.16 material, but it would be nice to get this say into -mm to make it easier to test this. Tested on x86_64 only.

Please Cc me directly with comments, I'm not on the list.

---

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

kernel/fork.c +1 -1
Index: linux-2.6.16-rc2/kernel/fork.c
===================================================================
@@ -210,7 +210,7 @@ static inline int dup_mmap(struct mm_str
 	for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
 		struct file *file;
 
-		if (mpnt->vm_flags & VM_DONTCOPY) {
+		if (mpnt->vm_flags & (VM_DONTCOPY | VM_DONTFORK)) {
 			long pages = vma_pages(mpnt);
 			mm->total_vm -= pages;
 			vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
mm/mmap.c +1 -1
Index: linux-2.6.16-rc2/mm/mmap.c
===================================================================
@@ -847,7 +847,7 @@ void vm_stat_account(struct mm_struct *m
 
 #ifdef CONFIG_HUGETLB
 	if (flags & VM_HUGETLB) {
-		if (!(flags & VM_DONTCOPY))
+		if (!(flags & (VM_DONTCOPY|VM_DONTFORK)))
 			mm->shared_vm += pages;
 		return;
 	}
mm/madvise.c +13 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTFORK;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTFORK;
 		break;
 	}
 
@@ -180,6 +187,8 @@ madvise_vma(struct vm_area_struct *vma, 
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
+	case MADV_DONTFORK:
+	case MADV_DOFORK:
 		error = madvise_behavior(vma, prev, start, end, behavior);
 		break;
 	case MADV_REMOVE:
include/linux/mm.h +1 -0
Index: linux-2.6.16-rc2/include/linux/mm.h
===================================================================
@@ -166,6 +166,7 @@ extern unsigned int kobjsize(const void 
 #define VM_NONLINEAR	0x00800000	/* Is non-linear (remap_file_pages) */
 #define VM_MAPPED_COPY	0x01000000	/* T if mapped copy of data (nommu mmap) */
 #define VM_INSERTPAGE	0x02000000	/* The vma has had "vm_insert_page()" done on it */
+#define VM_DONTFORK	0x04000000      /* App wants to avoid inheriting the vma on fork */
 
 #ifndef VM_STACK_DEFAULT_FLAGS		/* arch can override this */
 #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#1 lore raw
Quoted 4 lines · Michael S. Tsirkin

OK, I guess its time to start the push for merging this patch. Probably not 2.6.16 material, but it would be nice to get this say into -mm to make it easier to test this. Tested on x86_64 only.

Quoted 8 lines

Please Cc me directly with comments, I'm not on the list.

---

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

Looks good to me, Michael (but Gleb's eye has always proved better than mine). Just a couple of adjustments I'd ask before you send to Andrew:

1. Please just drop your mm/mmap.c vm_stat_account() mod:

mm/mmap.c void vm_stat_account(struct mm_struct *m · line 851

Quoted 4 lines
mm/mmap.c +1 -1
 	if (flags & VM_HUGETLB) {
-		if (!(flags & VM_DONTCOPY))
+		if (!(flags & (VM_DONTCOPY|VM_DONTFORK)))
 			mm->shared_vm += pages;

Conscientious of you to include that, but (a) if it's right, then you'd need to be fiddling shared_vm up and down whenever madvise changes VM_DONTFORK, and none us much want to get into that; and (b) I cannot for the life of me work out what that VM_HUGETLB VM_DONTCOPY block is about in the first place - I can't even find any instance of VM_HUGETLB with VM_DONTCOPY to judge it by. Luckily, wli CC'ed is the expert on both hugetlb and vm_stat_account - I hope he'll just tell us that block is wrong and should be deleted (which he or I could do as an unrelated patch). Perhaps it was an inappropriate hack to prevent some count going negative, from the days when we forgot to correct total_vm in the VM_DONTCOPY case in dup_mmap.

2. Your two-line changeset comment should be expanded: mention Infiniband, mention get_user_pages, explain how frustrating it is for the carefully pinned page to be orphaned from its user address space by a stray Copy- On-Write, if the process happens to fork meanwhile; and how VM_DONTFORK can be used to secure areas against that possibility. Mention how it could also be useful to an application, wanting to speed up its forks by cutting large areas out of consideration. Some of that information will be useful to Michael Kerrisk when he updates the madvise man page.

Explain that MADV_DONTFORK should be reversible, hence MADV_DOFORK; but should not be reversible on areas a driver has so marked, hence VM_DONTFORK distinct from VM_DONTCOPY.

Thanks,
Hugh

#2 lore raw

Quoting r. Hugh Dickins <[email protected]>:

Quoted 7 lines
Quoted 4 lines

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

Looks good to me, Michael (but Gleb's eye has always proved better than mine). Just a couple of adjustments I'd ask before you send to Andrew:

Gleb has acked this to me in a private mail.
Right, Gleb?

...

Quoted 2 lines

2. Your two-line changeset comment should be expanded:

OK, thanks, I'll work on an appropriate description.

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
Acked-by
#3 lore raw
Quoted 4 lines · Michael S. Tsirkin

Quoting r. Hugh Dickins <[email protected]>:

Quoted 3 lines
Quoted 3 lines

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Looks good to me, Michael (but Gleb's eye has always proved better than mine). Just a couple of adjustments I'd ask before you send to Andrew:

Gleb has acked this to me in a private mail.
Right, Gleb?

Sory to be late :)

Yes the patch is looking good.

--
Gleb.

#4 lore raw
In reply to Michael S. Tsirkin

One question, which I'm too lazy to read the source to answer: what does an old (pre-MADV_DONTFORK) kernel do with an application that tries to set MADV_DONTFORK?

I'm wondering what portable applications will have to do to handle the case of a new application running on an old kernel.

Thanks,
Roland

#5 lore raw
In reply to Michael S. Tsirkin

kernel/fork.c static inline int dup_mmap(struct mm_str · line 213

Quoted 8 lines · Michael S. Tsirkin

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

kernel/fork.c +1 -1
-		if (mpnt->vm_flags & VM_DONTCOPY) {
+		if (mpnt->vm_flags & (VM_DONTCOPY | VM_DONTFORK)) {

Why?

That VM_DONTCOPY _is_ DONTFORK.

Don't add a new useless DONTFORK that doesn't have any value.

Linus

#6 lore raw

Linus> Why?

Linus> That VM_DONTCOPY _is_ DONTFORK.

Linus> Don't add a new useless DONTFORK that doesn't have any Linus> value.

VM_DONTCOPY is hardly used in the kernel, so the semantics aren't very precisely defined. But the idea is that a driver setting VM_DONTCOPY probably has a good reason for doing it, and we don't want userspace to be able to erase that flag through madvise().

As Hugh said in his suggestion for a better changelog entry:

Quoted 3 lines

Explain that MADV_DONTFORK should be reversible, hence MADV_DOFORK; but should not be reversible on areas a driver has so marked, hence VM_DONTFORK distinct from VM_DONTCOPY.

Perhaps we don't care for now, and we should wait and add VM_KERNEL_DONTCOPY later if we really need it. I honestly don't know.

- Roland

#7 lore raw
Quoted 3 lines · Roland Dreier

VM_DONTCOPY is hardly used in the kernel, so the semantics aren't very precisely defined.

Now, I agree - it's a strange bit, and was initially just done "because we can and it seems to be a conceptually valid notion", so it's not used a lot.

That said, the semantics shouldn't be all that unexpected:

	#define VM_DONTCOPY  0x00020000		/* Do not copy this vma on fork */

and the usage ends up matching that (except for some really strange issue with hugepage counting, which just looks wrong, but never mind).

Quoted 3 lines

But the idea is that a driver setting VM_DONTCOPY
probably has a good reason for doing it, and we don't want userspace to be able to erase that flag through madvise().

Well, I can't actually see any case where a driver could validly do something that confuses the VM enough that clearing that bit could cause new problems.

Put another way: if that is true, then we have bigger issues, and should fix those problems instead.

So at most we might have _applications_ that depend on the fork not causing a copy-on-write thing (due to the old and broken private mapping of ioremapped areas behaviour), but if that's true, then it would have to be the driver itself that does the MADV_DOFORK thing, so..

Quoted 8 lines

As Hugh said in his suggestion for a better changelog entry:

Quoted 3 lines

Explain that MADV_DONTFORK should be reversible, hence MADV_DOFORK; but should not be reversible on areas a driver has so marked, hence VM_DONTFORK distinct from VM_DONTCOPY.

Perhaps we don't care for now, and we should wait and add VM_KERNEL_DONTCOPY later if we really need it. I honestly don't know.

I can see where Hugh is coming from, but I think it's adding cruft very much for a "be very careful" reason.

I would suggest that if you wanted to be very careful, you'd simply disallow changing - or perhaps just clearing - that DONTCOPY flag on special regions (ie ones that have been marked with VM_IO or VM_RESERVED).

Linus

#8 lore raw
Quoted 3 lines · Linus Torvalds

and the usage ends up matching that (except for some really strange issue with hugepage counting, which just looks wrong, but never mind).

Yes, I cc'ed wli on my reply to Michael,
we're hoping he'll just say delete that block.

Quoted 6 lines

I can see where Hugh is coming from, but I think it's adding cruft very much for a "be very careful" reason.

I would suggest that if you wanted to be very careful, you'd simply disallow changing - or perhaps just clearing - that DONTCOPY flag on special regions (ie ones that have been marked with VM_IO or VM_RESERVED).

Fair enough, disallow clearing on VM_IO (VM_RESERVED is on its way out, does little more than perpetuate a few accounting anomalies I think). So no new VM_DONTFORK flag, stick with VM_DONTCOPY: that's fine with me.

Hugh

#9 lore raw

Quoting Hugh Dickins <[email protected]>:

Quoted 3 lines

Fair enough, disallow clearing on VM_IO (VM_RESERVED is on its way out, does little more than perpetuate a few accounting anomalies I think). So no new VM_DONTFORK flag, stick with VM_DONTCOPY: that's fine with me.

Like this then?

---

Currently, copy-on-write may change the physical address of a page even if the user requested that the page is pinned in memory (either by mlock or by get_user_pages). This happens if the process forks meanwhile, and the parent writes to that page. As a result, the page is orphaned: in case of get_user_pages, the application will never see any data hardware DMA's into this page after the COW. In case of mlock'd memory, the parent is not getting the realtime/security benefits of mlock.

In particular, this affects the Infiniband modules which do DMA from and into user pages all the time.

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages. Could also be useful to an application wanting to speed up its forks by cutting large areas out of consideration.

mm/madvise.c +17 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTCOPY;
 		break;
 	}
 
@@ -177,6 +184,12 @@ madvise_vma(struct vm_area_struct *vma, 
 	long error;
 
 	switch (behavior) {
+	case MADV_DONTFORK:
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			break;
+		}
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#10 lore raw
Quoted 2 lines · Michael S. Tsirkin

Like this then?

Almost. I would still prefer madvise_vma to allow MADV_DONTFORK on a VM_IO vma, even though it must prohibit MADV_DOFORK there. But if Linus disagrees, of course ignore me.

Comments much better, thanks. I didn't get your point about mlock'd memory, but I'm content to believe you're thinking of an issue that hasn't occurred to me.

Hugh

#11 lore raw

Quoting r. Hugh Dickins <[email protected]>:

Quoted 9 lines

Subject: Re: [openib-general] Re: madvise MADV_DONTFORK/MADV_DOFORK

Quoted 2 lines · Michael S. Tsirkin

Like this then?

Almost. I would still prefer madvise_vma to allow MADV_DONTFORK on a VM_IO vma, even though it must prohibit MADV_DOFORK there. But if Linus disagrees, of course ignore me.

I'm not sure about this point. Linus?

Quoted 3 lines

Comments much better, thanks. I didn't get your point about mlock'd memory, but I'm content to believe you're thinking of an issue that hasn't occurred to me.

I'm referring to the follwing, from man mlock(2):

"Cryptographic security software often handles critical bytes like passwords or secret keys as data structures. As a result of paging, these secrets could be transfered onto a persistent swap store medium, where they might be accessible to the enemy long after the security software has erased the secrets in RAM and terminated."

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#12 lore raw
Quoted 6 lines · Michael S. Tsirkin

Quoting r. Hugh Dickins <[email protected]>:

Quoted 3 lines

Comments much better, thanks. I didn't get your point about mlock'd memory, but I'm content to believe you're thinking of an issue that hasn't occurred to me.

Quoted 7 lines

I'm referring to the follwing, from man mlock(2):

"Cryptographic security software often handles critical bytes like passwords or secret keys as data structures. As a result of paging, these secrets could be transfered onto a persistent swap store medium, where they might be accessible to the enemy long after the security software has erased the secrets in RAM and terminated."

Ah, I get it, thanks: once parent and child have distinct pages, the child's is not locked in memory and might go out to swap. Yes, a valid point, and a relevant use for MADV_DONTFORK.

Hugh

#13 lore raw

PATCH 1/1 madvise MADV_DONTFORK/MADV_DOFORK

Here's the final version of MADV_DONTFORK/MADV_DOFORK patch. Hugh, I gather you'll forward this to Andrew, correct?

---

Currently, copy-on-write may change the physical address of a page even if the user requested that the page is pinned in memory (either by mlock or by get_user_pages). This happens if the process forks meanwhile, and the parent writes to that page. As a result, the page is orphaned: in case of get_user_pages, the application will never see any data hardware DMAs into this page after the COW. In case of mlock'd memory, the parent is not getting the real-time/security benefits of mlock.

In particular, this affects the Infiniband modules which do DMA from and into user pages all the time.

This patch adds madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages. Could also be useful to an application wanting to speed up its forks by cutting large areas out of consideration.

mm/madvise.c +17 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTCOPY;
 		break;
 	}
 
@@ -177,6 +184,12 @@ madvise_vma(struct vm_area_struct *vma, 
 	long error;
 
 	switch (behavior) {
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			break;
+		}
+	case MADV_DONTFORK:
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#14 lore raw

PATCH 1/1 madvise MADV_DONTFORK/MADV_DOFORK

Quoted 2 lines · Michael S. Tsirkin

Here's the final version of MADV_DONTFORK/MADV_DOFORK patch. Hugh, I gather you'll forward this to Andrew, correct?

Oh, if you like, here we go - but it would have been perfectly okay for you to send it to him directly yourself. (And he's probably been watching and already taken it anyway.) It seems to me that it's sufficiently harmless that it could still be 2.6.16 material, but let's leave Linus and Andrew to decide on that.

Hugh

---
From: Michael S. Tsirkin <[email protected]>

Currently, copy-on-write may change the physical address of a page even if the user requested that the page is pinned in memory (either by mlock or by get_user_pages). This happens if the process forks meanwhile, and the parent writes to that page. As a result, the page is orphaned: in case of get_user_pages, the application will never see any data hardware DMAs into this page after the COW. In case of mlock'd memory, the parent is not getting the real-time/security benefits of mlock.

In particular, this affects the Infiniband modules which do DMA from and into user pages all the time.

This patch adds madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages. Could also be useful to an application wanting to speed up its forks by cutting large areas out of consideration.

mm/madvise.c +17 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTCOPY;
 		break;
 	}
 
@@ -177,6 +184,12 @@ madvise_vma(struct vm_area_struct *vma, 
 	long error;
 
 	switch (behavior) {
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			break;
+		}
+	case MADV_DONTFORK:
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#15 lore raw
In reply to Hugh Dickins
Quoted 4 lines · Hugh Dickins

Almost. I would still prefer madvise_vma to allow MADV_DONTFORK on a VM_IO vma, even though it must prohibit MADV_DOFORK there. But if Linus disagrees, of course ignore me.

No, I agree. Quite frankly, I'd be willing to allow even the other way around, because I don't see how the VM could screw up, but prohibiting DOFORK is clearly the safer thing to do.

Linus

#16 lore raw

Here's the final version then.

Currently, copy-on-write may change the physical address of a page even if the user requested that the page is pinned in memory (either by mlock or by get_user_pages). This happens if the process forks meanwhile, and the parent writes to that page. As a result, the page is orphaned: in case of get_user_pages, the application will never see any data hardware DMAs into this page after the COW.

In particular, this affects the Infiniband modules which do DMA from and into user pages all the time.

This patch adds madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages. Could also be useful to an application wanting to speed up its forks by cutting large areas out of consideration.

mm/madvise.c +17 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTCOPY;
 		break;
 	}
 
@@ -177,6 +184,12 @@ madvise_vma(struct vm_area_struct *vma, 
 	long error;
 
 	switch (behavior) {
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			break;
+		}
+	case MADV_DONTFORK:
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#18 lore raw

PATCH 1/1 madvise MADV_DONTFORK/MADV_DOFORK

Hello, Andrew!
Please consider the following for inclusion in -mm (and hopefully mainline in 2.6.17). The patch below is against 2.6.16-rc2. Tested on x86_64.

---

Currently, copy-on-write may change the physical address of a page even if the user requested that the page is pinned in memory (either by mlock or by get_user_pages). This happens if the process forks meanwhile, and the parent writes to that page. As a result, the page is orphaned: in case of get_user_pages, the application will never see any data hardware DMA's into this page after the COW. In case of mlock'd memory, the parent is not getting the realtime/security benefits of mlock.

In particular, this affects the Infiniband modules which do DMA from and into user pages all the time.

This patch adds madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages. Could also be useful to an application wanting to speed up its forks by cutting large areas out of consideration.

mm/madvise.c +17 -4
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
@@ -22,16 +22,23 @@ static long madvise_behavior(struct vm_a
 	struct mm_struct * mm = vma->vm_mm;
 	int error = 0;
 	pgoff_t pgoff;
-	int new_flags = vma->vm_flags & ~VM_READHINTMASK;
+	int new_flags = vma->vm_flags;
 
 	switch (behavior) {
+	case MADV_NORMAL:
+		new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ;
+		break;
 	case MADV_SEQUENTIAL:
-		new_flags |= VM_SEQ_READ;
+		new_flags = (new_flags & ~VM_RAND_READ) | VM_SEQ_READ;
 		break;
 	case MADV_RANDOM:
-		new_flags |= VM_RAND_READ;
+		new_flags = (new_flags & ~VM_SEQ_READ) | VM_RAND_READ;
 		break;
-	default:
+	case MADV_DONTFORK:
+		new_flags |= VM_DONTCOPY;
+		break;
+	case MADV_DOFORK:
+		new_flags &= ~VM_DONTCOPY;
 		break;
 	}
 
@@ -177,6 +184,12 @@ madvise_vma(struct vm_area_struct *vma, 
 	long error;
 
 	switch (behavior) {
+	case MADV_DOFORK:
+		if (vma->vm_flags & VM_IO) {
+			error = -EINVAL;
+			break;
+		}
+	case MADV_DONTFORK:
 	case MADV_NORMAL:
 	case MADV_SEQUENTIAL:
 	case MADV_RANDOM:
include/asm-x86_64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
@@ -45,6 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-alpha/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
@@ -43,6 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
@@ -73,6 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-mips/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
@@ -66,6 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
@@ -33,6 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
@@ -49,6 +49,8 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
@@ -44,6 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
@@ -55,6 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
@@ -38,6 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -0
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
@@ -36,6 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#19 lore raw

include/asm-x86_64/mman.h line 42

Quoted 11 lines · Michael S. Tsirkin

Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================

include/asm-x86_64/mman.h +2 -0
@@ -37,6 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
+#define MADV_DONTFORK	0x30		/* dont inherit across fork */
+#define MADV_DOFORK	0x31		/* do inherit across fork */
 

May I ask, what is the rationale for ignoring the apparent conventions of all architectures? For example parisc, you appear to even go contrary to the comment.

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#20 lore raw

Nick> May I ask, what is the rationale for ignoring the apparent Nick> conventions of all architectures? For example parisc, you Nick> appear to even go contrary to the comment.

Looking through include/asm-*/mman.h, I have to agree. The parisc example seemly especially bad, as (in addition to being in the reserved range as Nick notes) the DONTFORK/DOFORK values are stuck in a block with the page size values instead of the previous block where they seem more sensible. However, in other files like the alpha version, where the rest of the values are in decimal, the hex defines look rather jarring.

Michael, what led you to choose 0x30 and 0x31 for the two new values? It does seem that keeping them uniform across architectures is a reasonable thing to do, but as far as I can tell the values 9 and 10 are unused on all architectures, and have the added merit of not falling in the parisc reserved range.

Do we still have a chance to change this?

- R.

#21 lore raw
Quoted 2 lines · Roland Dreier

Do we still have a chance to change this?

yes, please do.

#22 lore raw

PATCH 1/1 Fix up MADV_DONTFORK/MADV_DOFORK definitions

Andrew> yes, please do.

OK, here's a patch that changes them to 9 and 10. I would hold off sending this to Linus until Michael has a chance to speak up, in case there's a reason I don't know for choosing 0x30 and 0x31.

- R.

The recently added MADV_DONTFORK and MADV_DOFORK values were defined to be 0x30 and 0x31 respectively. This leaves a strange gap from the older values, and ends up putting the values in the range of values that parisc reserves for page size specification. Also, the macros were always defined using hex, which looks somewhat strange when an architecture defines all the other values in decimal.

Change MADV_DONTFORK and MADV_DOFORK to be 9 and 10 respectively. These values are unused on all architectures and safely outside of the parisc reserved range. Define the values in decimal or hex to match the surrounding style for each architecture. While we're touching all this, change the comments from "dont inherit" to "don't inherit."

include/asm-alpha/mman.h +2 -2
index a21515c..0831a7c 100644
@@ -43,8 +43,8 @@
 #define	MADV_SPACEAVAIL	5		/* ensure resources are available */
 #define MADV_DONTNEED	6		/* don't need these pages */
 #define MADV_REMOVE	7		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	9		/* don't inherit across fork */
+#define MADV_DOFORK	10		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm/mman.h +2 -2
index 693ed85..9a87604 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-arm26/mman.h +2 -2
index 2096c50..83240c8 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-cris/mman.h +2 -2
index deddfb2..536bb02 100644
@@ -38,8 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-frv/mman.h +2 -2
index d3bca30..7f96e5f 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-h8300/mman.h +2 -2
index ac0346f..e03cbd8 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-i386/mman.h +2 -2
index ab2339a..2c740e2 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-ia64/mman.h +2 -2
index 357ebb7..a4b8dc1 100644
@@ -44,8 +44,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m32r/mman.h +2 -2
index 6b02fe3..68c28c5 100644
@@ -38,8 +38,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-m68k/mman.h +2 -2
index efd12bc..dd98f77 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-mips/mman.h +2 -2
index 6d01e26..018eb2e 100644
@@ -66,8 +66,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
include/asm-parisc/mman.h +2 -2
index a381cf5..e231d7a 100644
@@ -39,6 +39,8 @@
 #define MADV_VPS_PURGE  6               /* Purge pages from VM page cache */
 #define MADV_VPS_INHERIT 7              /* Inherit parents page size */
 #define MADV_REMOVE     8		/* remove these pages & resources */
+#define MADV_DONTFORK	9		/* don't inherit across fork */
+#define MADV_DOFORK	10		/* do inherit across fork */
 
 /* The range 12-64 is reserved for page size specification. */
 #define MADV_4K_PAGES   12              /* Use 4K pages  */
@@ -49,8 +51,6 @@
 #define MADV_4M_PAGES   22              /* Use 4 Megabyte pages */
 #define MADV_16M_PAGES  24              /* Use 16 Megabyte pages */
 #define MADV_64M_PAGES  26              /* Use 64 Megabyte pages */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-powerpc/mman.h +2 -2
index fcff25d..e81aa80 100644
@@ -45,8 +45,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-s390/mman.h +2 -2
index d41ca14..d9a5387 100644
@@ -44,8 +44,8 @@
 #define MADV_WILLNEED  0x3              /* pre-fault pages */
 #define MADV_DONTNEED  0x4              /* discard these pages */
 #define MADV_REMOVE    0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK  0x9		/* don't inherit across fork */
+#define MADV_DOFORK    0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sh/mman.h +2 -2
index 0e08d05..0e3efb1 100644
@@ -36,8 +36,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc/mman.h +2 -2
index 4a298b2..d1618d8 100644
@@ -55,8 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-sparc64/mman.h +2 -2
index d705ec9..cabec87 100644
@@ -55,8 +55,8 @@
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_FREE	0x5		/* (Solaris) contents can be freed */
 #define MADV_REMOVE	0x6		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-v850/mman.h +2 -2
index 7b851c3..d652235 100644
@@ -33,8 +33,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-x86_64/mman.h +2 -2
index b699a38..9333709 100644
@@ -37,8 +37,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON	MAP_ANONYMOUS
include/asm-xtensa/mman.h +2 -2
index e2d7afb..12f93a7 100644
@@ -73,8 +73,8 @@
 #define MADV_WILLNEED	0x3		/* pre-fault pages */
 #define MADV_DONTNEED	0x4		/* discard these pages */
 #define MADV_REMOVE	0x5		/* remove these pages & resources */
-#define MADV_DONTFORK	0x30		/* dont inherit across fork */
-#define MADV_DOFORK	0x31		/* do inherit across fork */
+#define MADV_DONTFORK	0x9		/* don't inherit across fork */
+#define MADV_DOFORK	0xa		/* do inherit across fork */
 
 /* compatibility flags */
 #define MAP_ANON       MAP_ANONYMOUS
#23 lore raw

Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

Quoted 6 lines · Roland Dreier

Andrew> yes, please do.

OK, here's a patch that changes them to 9 and 10. I would hold off sending this to Linus until Michael has a chance to speak up, in case there's a reason I don't know for choosing 0x30 and 0x31.

Here
http://marc.theaimsgroup.com/?l=linux-kernel&m=113162971606408&w=2
at the end there is a reasoning. So I think 9 and 10 will do too. By the way Nick was on CC list back than and haven't raised any concerns :)

--
Gleb.

#24 lore raw

Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

Quoted 4 lines · Gleb Natapov
Quoted 2 lines · Roland Dreier

Andrew> yes, please do.

Quoted 8 lines
Quoted 4 lines

OK, here's a patch that changes them to 9 and 10. I would hold off sending this to Linus until Michael has a chance to speak up, in case there's a reason I don't know for choosing 0x30 and 0x31.

Here
http://marc.theaimsgroup.com/?l=linux-kernel&m=113162971606408&w=2
at the end there is a reasoning.

Well it may make userspace portability slightly easier for this one case (exactly how, I'm not so sure because each architecture has their own MADV_ defines anyway). I rather think this should be left up to arch maintainers' numbering schemes, but...

Quoted 1 line

So I think 9 and 10 will do too.

s/too// ?

0x30 and 0x31 broke parisc's numbering scheme.

Quoted 2 lines

By the way Nick was on CC list back than and haven't raised any concerns :)

I probably would have assumed it had gone past arch maintainers and so wouldn't have given it a second thought: I don't know a great deal about the issues here. I just now happened to see the parisc comment.

But no harm done this time.

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com 
#25 lore raw

Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

In reply to Roland Dreier
Quoted 1 line · Roland Dreier

Change MADV_DONTFORK and MADV_DOFORK to be 9 and 10 respectively.

I wonder if we can convert the alpha-asm defines to hex and to move the flags which are common to all architectures to a arch-independend include file?

Gruss
Bernd

author Acked-by on v1 1/1
#26 lore raw

Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

In reply to Roland Dreier

Please apply.

Quoting Roland Dreier <[email protected]>:

Quoted 7 lines

Change MADV_DONTFORK and MADV_DOFORK to be 9 and 10 respectively. These values are unused on all architectures and safely outside of the parisc reserved range. Define the values in decimal or hex to match the surrounding style for each architecture. While we're touching all this, change the comments from "dont inherit" to "don't inherit."

Thanks,

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#27 lore raw
In reply to Roland Dreier

Quoting r. Roland Dreier <[email protected]>:

Quoted 5 lines

Michael, what led you to choose 0x30 and 0x31 for the two new values? It does seem that keeping them uniform across architectures is a reasonable thing to do, but as far as I can tell the values 9 and 10 are unused on all architectures, and have the added merit of not falling in the parisc reserved range.

No particular reason - I just selected values away from the rest of pack. Lets go ahead and change them.

Quoted 1 line

Do we still have a chance to change this?

So, any value consistent across architectures will do.

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#28 lore raw
In reply to Linus Torvalds

Quoting Linus Torvalds <[email protected]>:

Quoted 3 lines

I would suggest that if you wanted to be very careful, you'd simply disallow changing - or perhaps just clearing - that DONTCOPY flag on special regions (ie ones that have been marked with VM_IO or VM_RESERVED).

Right, this was already proposed here
http://lkml.org/lkml/2005/11/3/81
and I site:

Quoted 4 lines

You're then saying that a process cannot set VM_DONTCOPY on a VM_IO area to prevent the first child getting the area, but clear it after so the next child does get a copy of the area. I think it'd be wrong (surprising) to limit the functionality in that way.

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies
#29 lore raw
In reply to Linus Torvalds

Quoting Linus Torvalds <[email protected]>:

Quoted 14 lines

kernel/fork.c static inline int dup_mmap(struct mm_str · line 213

Quoted 8 lines

Add madvise options to control whether memory range is inherited across fork. Useful e.g. for when hardware is doing DMA from/into these pages.

kernel/fork.c +1 -1
-		if (mpnt->vm_flags & VM_DONTCOPY) {
+		if (mpnt->vm_flags & (VM_DONTCOPY | VM_DONTFORK)) {

Why?

That VM_DONTCOPY _is_ DONTFORK.

Don't add a new useless DONTFORK that doesn't have any value.

Quoted 4 lines · Hugh Dickins

If a driver sets VM_DONTCOPY, it's likely to be because the driver knows it'll cause some nastiness (memory corruption, memory leak, lockup...) if it were copied. The memory belongs to the driver, it's letting the process have a window on it. I don't think we should now let the process overrule it.

Here's a pointer to the relevant discussion:
http://lkml.org/lkml/2005/11/3/112

Signature · 2 lines
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies