2006-02-13 15:39:54

by Michael S. Tsirkin

[permalink] [raw]
Subject: madvise MADV_DONTFORK/MADV_DOFORK

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.

Signed-off-by: Michael S. Tsirkin <[email protected]>

Index: linux-2.6.16-rc2/kernel/fork.c
===================================================================
--- linux-2.6.16-rc2.orig/kernel/fork.c 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/kernel/fork.c 2006-02-12 20:48:37.000000000 +0200
@@ -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,
Index: linux-2.6.16-rc2/mm/mmap.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/mmap.c 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/mm/mmap.c 2006-02-12 20:48:37.000000000 +0200
@@ -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;
}
Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-12 20:48:37.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/linux/mm.h
===================================================================
--- linux-2.6.16-rc2.orig/include/linux/mm.h 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/include/linux/mm.h 2006-02-12 20:52:11.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-12 20:52:21.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-12 20:55:07.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-12 20:54:31.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-12 20:54:27.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-12 20:55:44.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-12 20:54:54.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-10 03:43:19.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-12 20:55:38.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-12 20:55:00.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-10 03:43:18.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-12 20:55:24.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-10 03:43:18.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-12 20:55:31.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-12 20:55:11.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-12 20:55:04.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-12 20:54:43.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-12 20:55:14.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-12 20:54:47.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-12 20:55:20.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-12 20:54:51.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-12 20:54:35.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-12 20:54:39.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-10 03:43:13.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-12 20:54:19.000000000 +0200
@@ -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


--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies


2006-02-13 18:54:42

by Hugh Dickins

[permalink] [raw]
Subject: Re: madvise MADV_DONTFORK/MADV_DOFORK

On Mon, 13 Feb 2006, Michael S. Tsirkin wrote:

> 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.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>

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:

> 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

2006-02-13 19:00:48

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: madvise MADV_DONTFORK/MADV_DOFORK

Quoting r. Hugh Dickins <[email protected]>:
> > 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.
> >
> > Signed-off-by: Michael S. Tsirkin <[email protected]>
>
> 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?

...

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

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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 19:04:13

by Roland Dreier

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

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

2006-02-13 19:05:55

by Linus Torvalds

[permalink] [raw]
Subject: Re: madvise MADV_DONTFORK/MADV_DOFORK



On Mon, 13 Feb 2006, Michael S. Tsirkin wrote:
>
> 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.
>
> Signed-off-by: Michael S. Tsirkin <[email protected]>
>
> - 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

2006-02-13 19:16:37

by Roland Dreier

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

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:

> 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

2006-02-13 19:19:24

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: madvise MADV_DONTFORK/MADV_DOFORK

Quoting Linus Torvalds <[email protected]>:
> >
> > 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.
> >
> > Signed-off-by: Michael S. Tsirkin <[email protected]>
> >
> > - 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.

When this was last discussed, Hugh Dickins said:
> 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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 19:34:58

by Linus Torvalds

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



On Mon, 13 Feb 2006, Roland Dreier wrote:
>
> 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).

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

> As Hugh said in his suggestion for a better changelog entry:
>
> > 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

2006-02-13 19:50:01

by Hugh Dickins

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

On Mon, 13 Feb 2006, Linus Torvalds wrote:
>
> 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.

> 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

2006-02-13 20:04:25

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: Re: madvise MADV_DONTFORK/MADV_DOFORK

Quoting Linus Torvalds <[email protected]>:
> 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:
> 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.

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 21:07:44

by Michael S. Tsirkin

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

Quoting Hugh Dickins <[email protected]>:
> 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.

Signed-off-by: Michael S. Tsirkin <[email protected]>

Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-14 01:34:20.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 21:56:47

by Hugh Dickins

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

On Mon, 13 Feb 2006, Michael S. Tsirkin wrote:
>
> 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

2006-02-13 22:08:27

by Michael S. Tsirkin

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

Quoting r. Hugh Dickins <[email protected]>:
> Subject: Re: [openib-general] Re: madvise MADV_DONTFORK/MADV_DOFORK
>
> On Mon, 13 Feb 2006, Michael S. Tsirkin wrote:
> >
> > 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?

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



--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 22:28:16

by Linus Torvalds

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



On Mon, 13 Feb 2006, Hugh Dickins wrote:
>
> 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

2006-02-13 22:54:19

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: Re: madvise MADV_DONTFORK/MADV_DOFORK

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.

Signed-off-by: Michael S. Tsirkin <[email protected]>

Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-14 03:20:33.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 22:54:15

by Hugh Dickins

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

On Tue, 14 Feb 2006, Michael S. Tsirkin wrote:
> Quoting r. Hugh Dickins <[email protected]>:
>
> > 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."

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

2006-02-13 22:59:39

by Michael S. Tsirkin

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Michael S. Tsirkin <[email protected]>

Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-14 03:40:22.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 23:00:40

by Hugh Dickins

[permalink] [raw]
Subject: Re: Re: madvise MADV_DONTFORK/MADV_DOFORK

On Tue, 14 Feb 2006, Michael S. Tsirkin wrote:
> Here's the final version then.

Acked-by: Hugh Dickins <[email protected]>

2006-02-13 23:33:56

by Michael S. Tsirkin

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Michael S. Tsirkin <[email protected]>
Acked-by: Hugh Dickins <[email protected]>

Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-14 03:40:22.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-13 23:43:47

by Hugh Dickins

[permalink] [raw]
Subject: [PATCH] madvise MADV_DONTFORK/MADV_DOFORK

On Tue, 14 Feb 2006, Michael S. Tsirkin wrote:
> 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.

Signed-off-by: Michael S. Tsirkin <[email protected]>
Signed-off-by: Hugh Dickins <[email protected]>

Index: linux-2.6.16-rc2/mm/madvise.c
===================================================================
--- linux-2.6.16-rc2.orig/mm/madvise.c 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/mm/madvise.c 2006-02-14 03:40:22.000000000 +0200
@@ -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:
Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-powerpc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-powerpc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-powerpc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-cris/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-cris/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-cris/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm26/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm26/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm26/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-alpha/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-alpha/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-alpha/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m68k/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m68k/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m68k/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-xtensa/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-xtensa/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-xtensa/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-mips/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-mips/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-mips/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-v850/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-v850/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-v850/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-s390/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-s390/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-s390/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-parisc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-parisc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-parisc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-i386/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-i386/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-i386/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sh/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sh/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sh/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-ia64/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-ia64/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-ia64/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-sparc/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-sparc/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-sparc/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-m32r/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-m32r/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-m32r/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-frv/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-frv/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-frv/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-h8300/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-h8300/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-h8300/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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
Index: linux-2.6.16-rc2/include/asm-arm/mman.h
===================================================================
--- linux-2.6.16-rc2.orig/include/asm-arm/mman.h 2006-02-14 01:22:27.000000000 +0200
+++ linux-2.6.16-rc2/include/asm-arm/mman.h 2006-02-14 01:24:57.000000000 +0200
@@ -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

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-14 06:52:45

by Gleb Natapov

[permalink] [raw]
Subject: Re: madvise MADV_DONTFORK/MADV_DOFORK

On Mon, Feb 13, 2006 at 09:02:06PM +0200, Michael S. Tsirkin wrote:
> Quoting r. Hugh Dickins <[email protected]>:
> > > 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.
> > >
> > > Signed-off-by: Michael S. Tsirkin <[email protected]>
> >
> > 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.

Acked-by: Gleb Natapov <[email protected]>

--
Gleb.

2006-02-15 04:37:57

by Nick Piggin

[permalink] [raw]
Subject: Re: [PATCH] madvise MADV_DONTFORK/MADV_DOFORK

Michael S. Tsirkin wrote:

> Index: linux-2.6.16-rc2/include/asm-x86_64/mman.h
> ===================================================================
> --- linux-2.6.16-rc2.orig/include/asm-x86_64/mman.h 2006-02-14 01:22:27.000000000 +0200
> +++ linux-2.6.16-rc2/include/asm-x86_64/mman.h 2006-02-14 01:24:57.000000000 +0200
> @@ -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.

--
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com

2006-02-15 06:09:38

by Roland Dreier

[permalink] [raw]
Subject: Re: [PATCH] madvise MADV_DONTFORK/MADV_DOFORK

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.

2006-02-15 06:18:16

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] madvise MADV_DONTFORK/MADV_DOFORK

Roland Dreier <[email protected]> wrote:
>
> Do we still have a chance to change this?

yes, please do.

2006-02-15 06:34:54

by Roland Dreier

[permalink] [raw]
Subject: [PATCH] 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."

Signed-off-by: Roland Dreier <[email protected]>

diff --git a/include/asm-alpha/mman.h b/include/asm-alpha/mman.h
index a21515c..0831a7c 100644
--- a/include/asm-alpha/mman.h
+++ b/include/asm-alpha/mman.h
@@ -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
diff --git a/include/asm-arm/mman.h b/include/asm-arm/mman.h
index 693ed85..9a87604 100644
--- a/include/asm-arm/mman.h
+++ b/include/asm-arm/mman.h
@@ -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
diff --git a/include/asm-arm26/mman.h b/include/asm-arm26/mman.h
index 2096c50..83240c8 100644
--- a/include/asm-arm26/mman.h
+++ b/include/asm-arm26/mman.h
@@ -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
diff --git a/include/asm-cris/mman.h b/include/asm-cris/mman.h
index deddfb2..536bb02 100644
--- a/include/asm-cris/mman.h
+++ b/include/asm-cris/mman.h
@@ -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
diff --git a/include/asm-frv/mman.h b/include/asm-frv/mman.h
index d3bca30..7f96e5f 100644
--- a/include/asm-frv/mman.h
+++ b/include/asm-frv/mman.h
@@ -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
diff --git a/include/asm-h8300/mman.h b/include/asm-h8300/mman.h
index ac0346f..e03cbd8 100644
--- a/include/asm-h8300/mman.h
+++ b/include/asm-h8300/mman.h
@@ -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
diff --git a/include/asm-i386/mman.h b/include/asm-i386/mman.h
index ab2339a..2c740e2 100644
--- a/include/asm-i386/mman.h
+++ b/include/asm-i386/mman.h
@@ -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
diff --git a/include/asm-ia64/mman.h b/include/asm-ia64/mman.h
index 357ebb7..a4b8dc1 100644
--- a/include/asm-ia64/mman.h
+++ b/include/asm-ia64/mman.h
@@ -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
diff --git a/include/asm-m32r/mman.h b/include/asm-m32r/mman.h
index 6b02fe3..68c28c5 100644
--- a/include/asm-m32r/mman.h
+++ b/include/asm-m32r/mman.h
@@ -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
diff --git a/include/asm-m68k/mman.h b/include/asm-m68k/mman.h
index efd12bc..dd98f77 100644
--- a/include/asm-m68k/mman.h
+++ b/include/asm-m68k/mman.h
@@ -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
diff --git a/include/asm-mips/mman.h b/include/asm-mips/mman.h
index 6d01e26..018eb2e 100644
--- a/include/asm-mips/mman.h
+++ b/include/asm-mips/mman.h
@@ -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
diff --git a/include/asm-parisc/mman.h b/include/asm-parisc/mman.h
index a381cf5..e231d7a 100644
--- a/include/asm-parisc/mman.h
+++ b/include/asm-parisc/mman.h
@@ -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
diff --git a/include/asm-powerpc/mman.h b/include/asm-powerpc/mman.h
index fcff25d..e81aa80 100644
--- a/include/asm-powerpc/mman.h
+++ b/include/asm-powerpc/mman.h
@@ -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
diff --git a/include/asm-s390/mman.h b/include/asm-s390/mman.h
index d41ca14..d9a5387 100644
--- a/include/asm-s390/mman.h
+++ b/include/asm-s390/mman.h
@@ -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
diff --git a/include/asm-sh/mman.h b/include/asm-sh/mman.h
index 0e08d05..0e3efb1 100644
--- a/include/asm-sh/mman.h
+++ b/include/asm-sh/mman.h
@@ -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
diff --git a/include/asm-sparc/mman.h b/include/asm-sparc/mman.h
index 4a298b2..d1618d8 100644
--- a/include/asm-sparc/mman.h
+++ b/include/asm-sparc/mman.h
@@ -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
diff --git a/include/asm-sparc64/mman.h b/include/asm-sparc64/mman.h
index d705ec9..cabec87 100644
--- a/include/asm-sparc64/mman.h
+++ b/include/asm-sparc64/mman.h
@@ -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
diff --git a/include/asm-v850/mman.h b/include/asm-v850/mman.h
index 7b851c3..d652235 100644
--- a/include/asm-v850/mman.h
+++ b/include/asm-v850/mman.h
@@ -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
diff --git a/include/asm-x86_64/mman.h b/include/asm-x86_64/mman.h
index b699a38..9333709 100644
--- a/include/asm-x86_64/mman.h
+++ b/include/asm-x86_64/mman.h
@@ -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
diff --git a/include/asm-xtensa/mman.h b/include/asm-xtensa/mman.h
index e2d7afb..12f93a7 100644
--- a/include/asm-xtensa/mman.h
+++ b/include/asm-xtensa/mman.h
@@ -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

2006-02-15 06:49:36

by Gleb Natapov

[permalink] [raw]
Subject: Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

On Tue, Feb 14, 2006 at 10:34:48PM -0800, Roland Dreier wrote:
> 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.

2006-02-15 07:31:13

by be-news06

[permalink] [raw]
Subject: Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

Roland Dreier <[email protected]> wrote:
> 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

2006-02-15 08:06:22

by Nick Piggin

[permalink] [raw]
Subject: Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

Gleb Natapov wrote:
> On Tue, Feb 14, 2006 at 10:34:48PM -0800, Roland Dreier wrote:
>
>> 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.

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

> So I think 9 and 10 will do too.

s/too// ?

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

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

--
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com

2006-02-15 08:12:01

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH] madvise MADV_DONTFORK/MADV_DOFORK

Quoting r. Roland Dreier <[email protected]>:
> 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.

> Do we still have a chance to change this?
So, any value consistent across architectures will do.

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies

2006-02-15 08:17:33

by Michael S. Tsirkin

[permalink] [raw]
Subject: Re: [PATCH] Fix up MADV_DONTFORK/MADV_DOFORK definitions

Please apply.

Quoting Roland Dreier <[email protected]>:
> 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."
>
> Signed-off-by: Roland Dreier <[email protected]>

Acked-by: Michael S. Tsirkin <[email protected]>
Thanks,

--
Michael S. Tsirkin
Staff Engineer, Mellanox Technologies