Skip to contentSkip to conversation

+ mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch added to -mm tree

PATCH [email protected] 8 participants 19 messages Feb 25, 2014 – Feb 27, 2014 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

Subject: + mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch added to -mm tree
To: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
From: [email protected]
Date: Tue, 25 Feb 2014 15:53:13 -0800

The patch titled

     Subject: mm: revert "thp: make MADV_HUGEPAGE check for mm->def_flags"

has been added to the -mm tree. Its filename is
mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch

This patch should soon appear at
http://ozlabs.org/~akpm/mmots/broken-out/mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch
and later at
http://ozlabs.org/~akpm/mmotm/broken-out/mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/SubmitChecklist when testing your code ***

The -mm tree is included into linux-next and is updated there every 3-4 working days

------------------------------------------------------
From: Alex Thorlton <[email protected]>
Subject: mm: revert "thp: make MADV_HUGEPAGE check for mm->def_flags"

The main motivation behind this patch is to provide a way to disable THP for jobs where the code cannot be modified, and using a malloc hook with madvise is not an option (i.e. statically allocated data). This patch allows us to do just that, without affecting other jobs running on the system.

We need to do this sort of thing for jobs where THP hurts performance, due to the possibility of increased remote memory accesses that can be created by situations such as the following:

When you touch 1 byte of an untouched, contiguous 2MB chunk, a THP will be handed out, and the THP will be stuck on whatever node the chunk was originally referenced from. If many remote nodes need to do work on that same chunk, they'll be making remote accesses.

With THP disabled, 4K pages can be handed out to separate nodes as they're needed, greatly reducing the amount of remote accesses to memory.

This patch is based on some of my work combined with some suggestions/patches given by Oleg Nesterov. The main goal here is to add a prctl switch to allow us to disable to THP on a per mm_struct basis.

Here's a bit of test data with the new patch in place...

First with the flag unset:

# perf stat -a ./prctl_wrapper_mmv3 0 ./thp_pthread -C 0 -m 0 -c 512 -b 256g                  
Setting thp_disabled for this task...
thp_disable: 0
Set thp_disabled state to 0
Process pid = 18027

PF/
MAX MIN TOTCPU/ TOT_PF/ TOT_PF/ WSEC/

TYPE:               CPUS       WALL       WALL        SYS     USER     TOTCPU       CPU     WALL_SEC   SYS_SEC       CPU   NODES
 512      1.120      0.060      0.000    0.110      0.110     0.000    28571428864 -9223372036854775808  55803572      23

Performance counter stats for './prctl_wrapper_mmv3_hack 0 ./thp_pthread -C 0 -m 0 -c 512 -b 256g':

  273719072.841402 task-clock                #  641.026 CPUs utilized           [100.00%]
         1,008,986 context-switches          #    0.000 M/sec                   [100.00%]
             7,717 CPU-migrations            #    0.000 M/sec                   [100.00%]
         1,698,932 page-faults               #    0.000 M/sec
355,222,544,890,379 cycles                    #    1.298 GHz                     [100.00%]
536,445,412,234,588 stalled-cycles-frontend   #  151.02% frontend cycles idle    [100.00%]
409,110,531,310,223 stalled-cycles-backend    #  115.17% backend  cycles idle    [100.00%]
148,286,797,266,411 instructions              #    0.42  insns per cycle
                                             #    3.62  stalled cycles per insn [100.00%]
27,061,793,159,503 branches                  #   98.867 M/sec                   [100.00%]
     1,188,655,196 branch-misses             #    0.00% of all branches

     427.001706337 seconds time elapsed

Now with the flag set:

# perf stat -a ./prctl_wrapper_mmv3 1 ./thp_pthread -C 0 -m 0 -c 512 -b 256g
Setting thp_disabled for this task...
thp_disable: 1
Set thp_disabled state to 1
Process pid = 144957

PF/
MAX MIN TOTCPU/ TOT_PF/ TOT_PF/ WSEC/

TYPE:               CPUS       WALL       WALL        SYS     USER     TOTCPU       CPU     WALL_SEC   SYS_SEC       CPU   NODES
 512      0.620      0.260      0.250    0.320      0.570     0.001    51612901376 128000000000 100806448      23

Performance counter stats for './prctl_wrapper_mmv3_hack 1 ./thp_pthread -C 0 -m 0 -c 512 -b 256g':

  138789390.540183 task-clock                #  641.959 CPUs utilized           [100.00%]
           534,205 context-switches          #    0.000 M/sec                   [100.00%]
             4,595 CPU-migrations            #    0.000 M/sec                   [100.00%]
        63,133,119 page-faults               #    0.000 M/sec
147,977,747,269,768 cycles                    #    1.066 GHz                     [100.00%]
200,524,196,493,108 stalled-cycles-frontend   #  135.51% frontend cycles idle    [100.00%]
105,175,163,716,388 stalled-cycles-backend    #   71.07% backend  cycles idle    [100.00%]
180,916,213,503,160 instructions              #    1.22  insns per cycle
                                             #    1.11  stalled cycles per insn [100.00%]
26,999,511,005,868 branches                  #  194.536 M/sec                   [100.00%]
       714,066,351 branch-misses             #    0.00% of all branches

     216.196778807 seconds time elapsed

As with previous versions of the patch, We're getting about a 2x performance increase here. Here's a link to the test case I used, along with the little wrapper to activate the flag:

http://oss.sgi.com/projects/memtests/thp_pthread_mmprctlv3.tar.gz

This patch (of 3):

Revert 8e72033f2a489b6c98 and add in code to fix up any issues caused by the revert.

The revert is necessary because hugepage_madvise would return -EINVAL when VM_NOHUGEPAGE is set, which will break subsequent chunks of this patch set.

Here's a snip of an e-mail from Gerald detailing the original purpose of this code, and providing justification for the revert:

<snip>
The intent of 8e72033f2a48 was to guard against any future programming errors that may result in an madvice(MADV_HUGEPAGE) on guest mappings, which would crash the kernel.

Martin suggested adding the bit to arch/s390/mm/pgtable.c, if 8e72033f2a48 was to be reverted, because that check will also prevent a kernel crash in the case described above, it will now send a SIGSEGV instead.

This would now also allow to do the madvise on other parts, if needed, so it is a more flexible approach. One could also say that it would have been better to do it this way right from the beginning... </snip>

Cc Gerald Schaefer <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andrea Arcangeli <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Alexander Viro <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc David Rientjes <[email protected]>

---

 arch/s390/mm/pgtable.c |    3 +++
 mm/huge_memory.c       |    4 ----
 2 files changed, 3 insertions(+), 4 deletions(-)
arch/s390/mm/pgtable.c renamed from arch/s390/mm/pgtable.c~mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags +3 -0
diff -puN arch/s390/mm/pgtable.c~mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags arch/s390/mm/pgtable.c
@@ -504,6 +504,9 @@ static int gmap_connect_pgtable(unsigned
 	if (!pmd_present(*pmd) &&
 	    __pte_alloc(mm, vma, pmd, vmaddr))
 		return -ENOMEM;
+	/* large pmds cannot yet be handled */
+	if (pmd_large(*pmd))
+		return -EFAULT;
 	/* pmd now points to a valid segment table entry. */
 	rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
 	if (!rmap)
mm/huge_memory.c renamed from mm/huge_memory.c~mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags +0 -1
diff -puN mm/huge_memory.c~mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags mm/huge_memory.c
@@ -1891,8 +1891,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 19 lines · Christian Borntraeger
Quoted 7 lines · On 26/02/14 00:53, [email protected] wrote:

Subject: + mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch added to -mm tree
To: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
From: [email protected]
Date: Tue, 25 Feb 2014 15:53:13 -0800

The patch titled

Quoted 8 lines
     Subject: mm: revert "thp: make MADV_HUGEPAGE check for mm->def_flags"

has been added to the -mm tree. Its filename is
mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch

This patch should soon appear at
http://ozlabs.org/~akpm/mmots/broken-out/mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch
and later at
http://ozlabs.org/~akpm/mmotm/broken-out/mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch

Quoted 5 lines

NAK.

Since 2012 qemu does call "qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);" for all kvm pages. (commit ad0b5321f1f797274603ebbe20108b0750baee94 Call MADV_HUGEPAGE for guest RAM allocations) so this breaks any recent kvm guest on s390.

Well, I can't really discuss the changes in arch/s390.

But perhaps qemu can be changed to avoid MADV_HUGEPAGE on s390 ? Otherwise I'd suggest the change below.

Oleg.

mm/huge_memory.c +9 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+		if (vma->vm_mm->def_flags & VM_NOHUGEPAGE)
 			return -EINVAL;
+#endif
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#3 lore raw

On 26/02/14 15:50, Oleg Nesterov wrote:
[...]

Quoted 30 lines
Quoted 5 lines

NAK.

Since 2012 qemu does call "qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);" for all kvm pages. (commit ad0b5321f1f797274603ebbe20108b0750baee94 Call MADV_HUGEPAGE for guest RAM allocations) so this breaks any recent kvm guest on s390.

Well, I can't really discuss the changes in arch/s390.

But perhaps qemu can be changed to avoid MADV_HUGEPAGE on s390 ? Otherwise I'd suggest the change below.

Oleg.

mm/huge_memory.c +0 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1987

Quoted 10 lines
mm/huge_memory.c +9 -0
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+		if (vma->vm_mm->def_flags & VM_NOHUGEPAGE)
 			return -EINVAL;
+#endif
+

Ifdefs are ugly but might be the only way of not breaking existing userspace. If we come up with a solution for THP in KVM host processes on s390, we can then remove that wart. We could even limit that hack to KVM only processes to retain Alex' prctl capability by checking mm_has_pgste (defined in arch/s390/include/asm/pgtable.h should be included via linux/mm.h)

Quoted 10 lines
mm/huge_memory.c +9 -0
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+		if ((vma->vm_mm->def_flags & VM_NOHUGEPAGE) && mm_has_pgste(vma->vm_mm))
 			return -EINVAL;
+#endif
+
#4 lore raw
Quoted 37 lines · Christian Borntraeger

On 26/02/14 15:50, Oleg Nesterov wrote:
[...]

Quoted 34 lines
Quoted 5 lines

NAK.

Since 2012 qemu does call "qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE);" for all kvm pages. (commit ad0b5321f1f797274603ebbe20108b0750baee94 Call MADV_HUGEPAGE for guest RAM allocations) so this breaks any recent kvm guest on s390.

Well, I can't really discuss the changes in arch/s390.

But perhaps qemu can be changed to avoid MADV_HUGEPAGE on s390 ? Otherwise I'd suggest the change below.

Oleg.

mm/huge_memory.c +4 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
Quoted 8 lines

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1987

Quoted 6 lines
mm/huge_memory.c +5 -0
+ */
+#ifdef CONFIG_S390
+		if (vma->vm_mm->def_flags & VM_NOHUGEPAGE)
 			return -EINVAL;
+#endif
+

Ifdefs are ugly

IS_ENABLED(CONFIG_S390) should help with this.

Signature · 1 line
 Kirill A. Shutemov
#5 lore raw
In reply to Christian Borntraeger
Quoted 29 lines · Christian Borntraeger
Quoted 27 lines · Oleg Nesterov

But perhaps qemu can be changed to avoid MADV_HUGEPAGE on s390 ? Otherwise I'd suggest the change below.

Oleg.

mm/huge_memory.c +4 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
Quoted 8 lines

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1987

Quoted 6 lines
mm/huge_memory.c +5 -0
+ */
+#ifdef CONFIG_S390
+		if (vma->vm_mm->def_flags & VM_NOHUGEPAGE)
 			return -EINVAL;
+#endif
+

Ifdefs are ugly but might be the only way of not breaking existing userspace.

Yes, agreed. but note that this code is already s390-specific, nobody else puts VM_NOHUGEPAGE into ->def_flags (until this series of course).

Quoted 3 lines

If we come up with a solution for THP in KVM host processes on s390, we can then remove that wart. We could even limit that hack to KVM only processes to retain Alex' prctl capability by checking mm_has_pgste

Yes, yes, I looked at mm->context.has_pgste too ;) But I wasn't sure we can rely on it. Thanks.

Quoted 7 lines

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru

Quoted 7 lines
mm/huge_memory.c +6 -0
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+		if ((vma->vm_mm->def_flags & VM_NOHUGEPAGE) && mm_has_pgste(vma->vm_mm))
 			return -EINVAL;

Hmm... but why do we need to check VM_NOHUGEPAGE then? Can't the change below work?

Oleg.

arch/s390/mm/pgtable.c +0 -1
@@ -1084,7 +1084,6 @@ static inline void thp_split_mm(struct m
 		vma->vm_flags &= ~VM_HUGEPAGE;
 		vma->vm_flags |= VM_NOHUGEPAGE;
 	}
-	mm->def_flags |= VM_NOHUGEPAGE;
 }
 #else
 static inline void thp_split_mm(struct mm_struct *mm)
mm/huge_memory.c +5 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,12 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+#ifdef CONFIG_S390
+		if (mm_has_pgste(vma->vm_mm))
 			return -EINVAL;
+#endif
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#6 lore raw
Quoted 49 lines · On Wed, 26 Feb 2014 16:31:44 +0100 Oleg Nesterov <[email protected]> wrote:
Quoted 29 lines · Christian Borntraeger
Quoted 27 lines · Oleg Nesterov

But perhaps qemu can be changed to avoid MADV_HUGEPAGE on s390 ? Otherwise I'd suggest the change below.

Oleg.

mm/huge_memory.c +4 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
Quoted 8 lines

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1983

Quoted 6 lines
mm/huge_memory.c +5 -0
+ */
+#ifdef CONFIG_S390
+		if (vma->vm_mm->def_flags & VM_NOHUGEPAGE)
 			return -EINVAL;
+#endif
+

Ifdefs are ugly but might be the only way of not breaking existing userspace.

Yes, agreed. but note that this code is already s390-specific, nobody else puts VM_NOHUGEPAGE into ->def_flags (until this series of course).

Quoted 3 lines

If we come up with a solution for THP in KVM host processes on s390, we can then remove that wart. We could even limit that hack to KVM only processes to retain Alex' prctl capability by checking mm_has_pgste

Yes, yes, I looked at mm->context.has_pgste too ;) But I wasn't sure we can rely on it. Thanks.

Quoted 10 lines
Quoted 7 lines

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru

Quoted 7 lines
mm/huge_memory.c +6 -0
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+		if ((vma->vm_mm->def_flags & VM_NOHUGEPAGE) && mm_has_pgste(vma->vm_mm))
 			return -EINVAL;

Hmm... but why do we need to check VM_NOHUGEPAGE then? Can't the change below work?

Yes, good point, that should do the trick.

Quoted 39 lines

Oleg.

arch/s390/mm/pgtable.c +0 -1
@@ -1084,7 +1084,6 @@ static inline void thp_split_mm(struct m
 		vma->vm_flags &= ~VM_HUGEPAGE;
 		vma->vm_flags |= VM_NOHUGEPAGE;
 	}
-	mm->def_flags |= VM_NOHUGEPAGE;
 }
 #else
 static inline void thp_split_mm(struct mm_struct *mm)
mm/huge_memory.c +5 -3
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*
@@ -1977,8 +1975,12 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+#ifdef CONFIG_S390
+		if (mm_has_pgste(vma->vm_mm))
 			return -EINVAL;
+#endif
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#7 lore raw
In reply to Oleg Nesterov
Quoted 22 lines · Oleg Nesterov
arch/s390/mm/pgtable.c +0 -1
@@ -1084,7 +1084,6 @@ static inline void thp_split_mm(struct m
 		vma->vm_flags &= ~VM_HUGEPAGE;
 		vma->vm_flags |= VM_NOHUGEPAGE;
 	}
-	mm->def_flags |= VM_NOHUGEPAGE;
 }
 #else
 static inline void thp_split_mm(struct mm_struct *mm)
mm/huge_memory.c +0 -2
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
 		     unsigned long *vm_flags, int advice)
 {
-	struct mm_struct *mm = vma->vm_mm;
-
 	switch (advice) {
 	case MADV_HUGEPAGE:
 		/*

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1979

Quoted 6 lines
mm/huge_memory.c +2 -1
@@ -1977,8 +1975,12 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+#ifdef CONFIG_S390

Do we want a comment here, explaining why s390 is special again?

Quoted 8 lines
mm/huge_memory.c +3 -0
+		if (mm_has_pgste(vma->vm_mm))
 			return -EINVAL;
+#endif
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#8 lore raw
Quoted 2 lines · Peter Zijlstra

On Wed, Feb 26, 2014 at 04:31:44PM +0100, Oleg Nesterov wrote:
Do we want a comment here, explaining why s390 is special again?

Here's what I've got, with everybody's suggestions spun together:

mm/huge_memory.c +9 -3
index 82166bf..7f01491 100644
@@ -1968,8 +1968,6 @@ out:
 int hugepage_madvise(struct vm_area_struct *vma,
                     unsigned long *vm_flags, int advice)
 {
-       struct mm_struct *mm = vma->vm_mm;
-
        switch (advice) {
        case MADV_HUGEPAGE:
                /*
@@ -1977,8 +1975,16 @@ int hugepage_madvise(struct vm_area_struct *vma,
                 */
                if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
                        return -EINVAL;
-               if (mm->def_flags & VM_NOHUGEPAGE)
+
+/*
+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */
+#ifdef CONFIG_S390
+               if (mm_has_pgste(vma->vm_mm))
                        return -EINVAL;
+#endif
+
                *vm_flags &= ~VM_NOHUGEPAGE;
                *vm_flags |= VM_HUGEPAGE;
                /*

I've compiled and tested and it works ok on my machines (I don't have an s390 to test on though :). Is everybody okay with this solution?

BTW, Kirill, I looked at using IS_ENABLED to clean up the ifdef, but it won't work here, since mm_has_pgste isn't defined unless CONFIG_S390 is defined. i.e.: This line:

if (IS_ENABLED(CONFIG_S390) && mm_has_pgste(vma->vm_mm))

won't compile.

#9 lore raw

Review

Quoted 8 lines · Alex Thorlton

+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */

(quoted diff) +3 -0
+#ifdef CONFIG_S390
+               if (mm_has_pgste(vma->vm_mm))
                        return -EINVAL;
+#endif

The comment is not really right...

And personally I think that

@@ -504,6 +504,9 @@ static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
if (!pmd_present(*pmd) &&
__pte_alloc(mm, vma, pmd, vmaddr))
return -ENOMEM;
+ /* large pmds cannot yet be handled */
+ if (pmd_large(*pmd))
+ return -EFAULT;

change still makes sense, so that we can simply revert this s390- specific hack in hugepage_madvise().

I'd suggest the patch below on top of your changes, but I won't argue.

It would be nice to also change thp_split_mm() to not not play with mm->def_flags, but I am not sure if we can do this.

Oleg.
---

Subject: [PATCH] s390: make sure MADV_HUGEPAGE fails after s390_enable_sie()

As Christian pointed out, the recent 'Revert "thp: make MADV_HUGEPAGE check for mm->def_flags"' breaks qemu, it does QEMU_MADV_HUGEPAGE for all kvm pages but this doesn't work after s390_enable_sie/thp_split_mm.

mm/huge_memory.c +11 -0
index a4310a5..0e08d92 100644
@@ -1970,11 +1970,22 @@ int hugepage_madvise(struct vm_area_struct *vma,
 {
 	switch (advice) {
 	case MADV_HUGEPAGE:
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif
 		/*
 		 * Be somewhat over-protective like KSM for now!
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#10 lore raw
Quoted 22 lines · On Wed, 26 Feb 2014 19:06:03 +0100 Oleg Nesterov <[email protected]> wrote:

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma,

Quoted 8 lines · Alex Thorlton

+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */

mm/huge_memory.c +3 -0
+#ifdef CONFIG_S390
+               if (mm_has_pgste(vma->vm_mm))
                        return -EINVAL;
+#endif

The comment is not really right...

And personally I think that

@@ -504,6 +504,9 @@ static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
if (!pmd_present(*pmd) &&
__pte_alloc(mm, vma, pmd, vmaddr))
return -ENOMEM;
+ /* large pmds cannot yet be handled */
+ if (pmd_large(*pmd))
+ return -EFAULT;

Quoted 2 lines

change still makes sense, so that we can simply revert this s390- specific hack in hugepage_madvise().

Yes, agreed.

Quoted 5 lines

I'd suggest the patch below on top of your changes, but I won't argue.

It would be nice to also change thp_split_mm() to not not play with mm->def_flags, but I am not sure if we can do this.

Hmm, I'm also wondering about this. Basically, we only need VM_NOHUGEPAGE in vma->vm_flags, which is done for all existing vmas in thp_split_mm(). But if there should be new vmas created afterwards, it would still be necessary to also have VM_NOHUGEPAGE in mm->def_flags, because the vm_flags for new vmas will be set via OR of mm->def_flags, e.g. in do_brk() and do_mmap_pgoff().

I guess the question is if new vmas can be created for the qemu/kvm host process?

Anyway, this would then have to be a separate patch, to keep the "revertability" of this hack.

Quoted 42 lines

Oleg.
---

Subject: [PATCH] s390: make sure MADV_HUGEPAGE fails after s390_enable_sie()

As Christian pointed out, the recent 'Revert "thp: make MADV_HUGEPAGE check for mm->def_flags"' breaks qemu, it does QEMU_MADV_HUGEPAGE for all kvm pages but this doesn't work after s390_enable_sie/thp_split_mm.

mm/huge_memory.c +11 -0
index a4310a5..0e08d92 100644
@@ -1970,11 +1970,22 @@ int hugepage_madvise(struct vm_area_struct *vma,
 {
 	switch (advice) {
 	case MADV_HUGEPAGE:
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif
 		/*
 		 * Be somewhat over-protective like KSM for now!
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
+
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
#11 lore raw
Quoted 7 lines · Gerald Schaefer
Quoted 2 lines · On Wed, 26 Feb 2014 19:06:03 +0100 Oleg Nesterov <[email protected]> wrote:

It would be nice to also change thp_split_mm() to not not play with mm->def_flags, but I am not sure if we can do this.

Quoted 6 lines

Hmm, I'm also wondering about this. Basically, we only need VM_NOHUGEPAGE in vma->vm_flags, which is done for all existing vmas in thp_split_mm(). But if there should be new vmas created afterwards, it would still be necessary to also have VM_NOHUGEPAGE in mm->def_flags, because the vm_flags for new vmas will be set via OR of mm->def_flags, e.g. in do_brk() and do_mmap_pgoff().

Yes, exactly, this was my concern.

And while I know nothing about s390, it seems to me that huge pages should be forbidden for any vma if ->has_pgste was set.

Quoted 2 lines

Anyway, this would then have to be a separate patch, to keep the "revertability" of this hack.

Agreed. Thanks!

Oleg.

Tested-by Reviewed-by
#12 lore raw
In reply to Oleg Nesterov
Quoted 22 lines · Oleg Nesterov

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma,

Quoted 8 lines · Alex Thorlton

+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */

mm/huge_memory.c +3 -0
+#ifdef CONFIG_S390
+               if (mm_has_pgste(vma->vm_mm))
                        return -EINVAL;
+#endif

The comment is not really right...

And personally I think that

@@ -504,6 +504,9 @@ static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
if (!pmd_present(*pmd) &&
__pte_alloc(mm, vma, pmd, vmaddr))
return -ENOMEM;
+ /* large pmds cannot yet be handled */
+ if (pmd_large(*pmd))
+ return -EFAULT;

Quoted 2 lines

change still makes sense, so that we can simply revert this s390- specific hack in hugepage_madvise().

Yes, it still makes sense to cover existing THPs here.

Quoted 23 lines

I'd suggest the patch below on top of your changes, but I won't argue.

It would be nice to also change thp_split_mm() to not not play with mm->def_flags, but I am not sure if we can do this.

Oleg.
---

Subject: [PATCH] s390: make sure MADV_HUGEPAGE fails after s390_enable_sie()

As Christian pointed out, the recent 'Revert "thp: make MADV_HUGEPAGE check for mm->def_flags"' breaks qemu, it does QEMU_MADV_HUGEPAGE for all kvm pages but this doesn't work after s390_enable_sie/thp_split_mm.

mm/huge_memory.c +0 -0
index a4310a5..0e08d92 100644

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1978

Quoted 9 lines
mm/huge_memory.c +6 -0
@@ -1970,11 +1970,22 @@ int hugepage_madvise(struct vm_area_struct *vma,
 {
 	switch (advice) {
 	case MADV_HUGEPAGE:
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.

I prefer:
* until kvm/s390 can handle large pages in the host.

Otherwise qemu has to be changed again, if we get THP working for kvm.

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1988

Quoted 10 lines
mm/huge_memory.c +5 -0
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif
 		/*
 		 * Be somewhat over-protective like KSM for now!
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
+

Unrelated white space?

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1991

Quoted 4 lines
mm/huge_memory.c +0 -0
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*

With the comment and white space change:

Thanks for the quick patch

#13 lore raw
Quoted 23 lines · Christian Borntraeger
Quoted 22 lines · Oleg Nesterov

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma,

Quoted 8 lines · Alex Thorlton

+ * MADV_HUGEPAGE after PRCTL_THP_DISABLE is broken on s390 because
+ * qemu blindly does madvise(MADV_HUGEPAGE) after s390_enable_sie().
+ */

mm/huge_memory.c +3 -0
+#ifdef CONFIG_S390
+               if (mm_has_pgste(vma->vm_mm))
                        return -EINVAL;
+#endif

The comment is not really right...

And personally I think that

@@ -504,6 +504,9 @@ static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
if (!pmd_present(*pmd) &&
__pte_alloc(mm, vma, pmd, vmaddr))
return -ENOMEM;
+ /* large pmds cannot yet be handled */
+ if (pmd_large(*pmd))
+ return -EFAULT;

Quoted 4 lines
Quoted 2 lines

change still makes sense, so that we can simply revert this s390- specific hack in hugepage_madvise().

Yes, it still makes sense to cover existing THPs here.

Yes, it does. I just snipped the chunk of the original patch that I actually changed in my last e-mail. I didn't intend to actually remove the above portion in the final patch - sorry for the confusion!

Quoted 3 lines
Quoted 3 lines

I'd suggest the patch below on top of your changes, but I won't argue.

I like this approach, and your updated comment as well. This should probably go in as [PATCH 2/4] in my series. Do I need to spin a v5 with this new patch included in the series, or will Andrew pull this in?

Quoted 56 lines
Quoted 22 lines

It would be nice to also change thp_split_mm() to not not play with mm->def_flags, but I am not sure if we can do this.

Oleg.
---

Subject: [PATCH] s390: make sure MADV_HUGEPAGE fails after s390_enable_sie()

As Christian pointed out, the recent 'Revert "thp: make MADV_HUGEPAGE check for mm->def_flags"' breaks qemu, it does QEMU_MADV_HUGEPAGE for all kvm pages but this doesn't work after s390_enable_sie/thp_split_mm.

mm/huge_memory.c +0 -0
index a4310a5..0e08d92 100644

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1978

Quoted 9 lines
mm/huge_memory.c +6 -0
@@ -1970,11 +1970,22 @@ int hugepage_madvise(struct vm_area_struct *vma,
 {
 	switch (advice) {
 	case MADV_HUGEPAGE:
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.

I prefer:
* until kvm/s390 can handle large pages in the host.

Otherwise qemu has to be changed again, if we get THP working for kvm.

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1988

Quoted 10 lines
mm/huge_memory.c +5 -0
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif
 		/*
 		 * Be somewhat over-protective like KSM for now!
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
+

Unrelated white space?

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1991

Quoted 4 lines
mm/huge_memory.c +0 -0
 		*vm_flags &= ~VM_NOHUGEPAGE;
 		*vm_flags |= VM_HUGEPAGE;
 		/*
Quoted 6 lines

With the comment and white space change:

Thanks for the quick patch

Yes, thank you all for the quick responses, and for looking over these patches!

author maintainer
#14 lore raw
Quoted 7 lines · On Wed, 26 Feb 2014 13:39:17 -0600 Alex Thorlton <[email protected]> wrote:
Quoted 3 lines
Quoted 3 lines

I'd suggest the patch below on top of your changes, but I won't argue.

I like this approach, and your updated comment as well. This should probably go in as [PATCH 2/4] in my series. Do I need to spin a v5 with this new patch included in the series, or will Andrew pull this in?

Paolo's comments on Oleg's patch remain unaddressed, so please take a look at that and send out the final version?

An incremental patch would be preferred, please. I'll later fold that into mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch to avoid any bisection holes.

#15 lore raw
Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Wed, 26 Feb 2014 13:39:17 -0600 Alex Thorlton <[email protected]> wrote:
Quoted 2 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

I'd suggest the patch below on top of your changes, but I won't argue.

I like this approach, and your updated comment as well. This should probably go in as [PATCH 2/4] in my series. Do I need to spin a v5 with this new patch included in the series, or will Andrew pull this in?

Paolo's comments on Oleg's patch remain unaddressed, so please take a look at that and send out the final version?

Got it. I'll get that to you tonight/tomorrow morning.

Quoted 3 lines

An incremental patch would be preferred, please. I'll later fold that into mm-revert-thp-make-madv_hugepage-check-for-mm-def_flags.patch to avoid any bisection holes.

Understood. I'll keep them separate.

Thanks, Andrew!

- Alex

#16 lore raw
Quoted 7 lines · Alex Thorlton
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Wed, 26 Feb 2014 13:39:17 -0600 Alex Thorlton <[email protected]> wrote:
Quoted 3 lines
Quoted 3 lines

I'd suggest the patch below on top of your changes, but I won't argue.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I like this approach, and your updated comment as well. This should probably go in as [PATCH 2/4] in my series. Do I need to spin a v5 with this new patch included in the series, or will Andrew pull this in?

Paolo's comments on Oleg's patch remain unaddressed, so please take a look at that and send out the final version?

Got it. I'll get that to you tonight/tomorrow morning.

Just kicked out another version of the patch that should cover all comments/suggestions. Let me know if you need anything else from me!

- Alex

#17 lore raw
In reply to Oleg Nesterov

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1982

Quoted 11 lines
mm/huge_memory.c +10 -0
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif

The comment is not quite true. QEMU doesn't expect that the madvise fails. It simply expects that the madvise doesn't cause SIGSEGVs or later malfunctioning, because (quoting tha man page) madvise "does not influence the semantics of the application".

There's nothing to fix in QEMU, in fact I believe this should return 0 rather than -EINVAL. It's perfectly legal for the kernel to ignore an madvise, and this is what should happen here.

Paolo

#18 lore raw
Quoted 13 lines · Paolo Bonzini

mm/huge_memory.c int hugepage_madvise(struct vm_area_struct *vma, · line 1982

Quoted 11 lines
mm/huge_memory.c +10 -0
+#ifdef CONFIG_S390
+		/*
+		 * MADV_HUGEPAGE is broken after s390_enable_sie(), qemu
+		 * blindly does madvise(MADV_HUGEPAGE) for for all kvm pages
+		 * and expects it must fail on s390. Avoid a possible SIGSEGV
+		 * until qemu is changed.
+		 */
+		if (mm_has_pgste(vma->vm_mm))
+			return -EINVAL;
+#endif

The comment is not quite true. QEMU doesn't expect that the madvise fails.

Yes, sorry. I didn't mean "it expects -EINVAL".

Quoted 3 lines

It simply expects that the madvise doesn't cause SIGSEGVs or later malfunctioning, because (quoting tha man page) madvise "does not influence the semantics of the application".

Yes, I understand. But currently this means "MADV_HUGEPAGE should not actually work", this is what I tried to say.

Quoted 1 line

There's nothing to fix in QEMU,

I was going to argue, but this is probably true too.

In short: I agree with any comment ;)

Oleg.

#19 lore raw
In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra

mm/huge_memory.c int hugepage_madvise(struct vm_area_stru · line 1979

Quoted 8 lines · Oleg Nesterov
mm/huge_memory.c +2 -1
 		/*
@@ -1977,8 +1975,12 @@ int hugepage_madvise(struct vm_area_stru
 		 */
 		if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
 			return -EINVAL;
-		if (mm->def_flags & VM_NOHUGEPAGE)
+
+#ifdef CONFIG_S390

Do we want a comment here, explaining why s390 is special again?

Yes, yes, sure. Especially because this is (hopefully) the temporary hack.

Oleg.