stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Peter Feiner <pfeiner@google.com>,
	"Kirill A. Shutemov" <kirill@shutemov.name>,
	Cyrill Gorcunov <gorcunov@openvz.org>,
	Pavel Emelyanov <xemul@parallels.com>,
	Jamie Liu <jamieliu@google.com>, Hugh Dickins <hughd@google.com>,
	Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH 3.14 41/42] mm: softdirty: unmapped addresses between VMAs are clean
Date: Sun, 26 Apr 2015 16:02:00 +0200	[thread overview]
Message-ID: <20150426134249.947614911@linuxfoundation.org> (raw)
In-Reply-To: <20150426134248.160161895@linuxfoundation.org>

3.14-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Peter Feiner <pfeiner@google.com>

commit 81d0fa623c5b8dbd5279d9713094b0f9b0a00fb4 upstream.

If a /proc/pid/pagemap read spans a [VMA, an unmapped region, then a
VM_SOFTDIRTY VMA], the virtual pages in the unmapped region are reported
as softdirty.  Here's a program to demonstrate the bug:

int main() {
	const uint64_t PAGEMAP_SOFTDIRTY = 1ul << 55;
	uint64_t pme[3];
	int fd = open("/proc/self/pagemap", O_RDONLY);;
	char *m = mmap(NULL, 3 * getpagesize(), PROT_READ,
	               MAP_ANONYMOUS | MAP_SHARED, -1, 0);
	munmap(m + getpagesize(), getpagesize());
	pread(fd, pme, 24, (unsigned long) m / getpagesize() * 8);
	assert(pme[0] & PAGEMAP_SOFTDIRTY);    /* passes */
	assert(!(pme[1] & PAGEMAP_SOFTDIRTY)); /* fails */
	assert(pme[2] & PAGEMAP_SOFTDIRTY);    /* passes */
	return 0;
}

(Note that all pages in new VMAs are softdirty until cleared).

Tested:
	Used the program given above. I'm going to include this code in
	a selftest in the future.

[n-horiguchi@ah.jp.nec.com: prevent pagemap_pte_range() from overrunning]
Signed-off-by: Peter Feiner <pfeiner@google.com>
Cc: "Kirill A. Shutemov" <kirill@shutemov.name>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Pavel Emelyanov <xemul@parallels.com>
Cc: Jamie Liu <jamieliu@google.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 fs/proc/task_mmu.c |   59 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 20 deletions(-)

--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -995,7 +995,6 @@ static int pagemap_pte_range(pmd_t *pmd,
 	spinlock_t *ptl;
 	pte_t *pte;
 	int err = 0;
-	pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
 
 	/* find the first VMA at or above 'addr' */
 	vma = find_vma(walk->mm, addr);
@@ -1009,6 +1008,7 @@ static int pagemap_pte_range(pmd_t *pmd,
 
 		for (; addr != end; addr += PAGE_SIZE) {
 			unsigned long offset;
+			pagemap_entry_t pme;
 
 			offset = (addr & ~PAGEMAP_WALK_MASK) >>
 					PAGE_SHIFT;
@@ -1023,32 +1023,51 @@ static int pagemap_pte_range(pmd_t *pmd,
 
 	if (pmd_trans_unstable(pmd))
 		return 0;
-	for (; addr != end; addr += PAGE_SIZE) {
-		int flags2;
 
-		/* check to see if we've left 'vma' behind
-		 * and need a new, higher one */
-		if (vma && (addr >= vma->vm_end)) {
-			vma = find_vma(walk->mm, addr);
-			if (vma && (vma->vm_flags & VM_SOFTDIRTY))
-				flags2 = __PM_SOFT_DIRTY;
-			else
-				flags2 = 0;
-			pme = make_pme(PM_NOT_PRESENT(pm->v2) | PM_STATUS2(pm->v2, flags2));
+	while (1) {
+		/* End of address space hole, which we mark as non-present. */
+		unsigned long hole_end;
+
+		if (vma)
+			hole_end = min(end, vma->vm_start);
+		else
+			hole_end = end;
+
+		for (; addr < hole_end; addr += PAGE_SIZE) {
+			pagemap_entry_t pme = make_pme(PM_NOT_PRESENT(pm->v2));
+
+			err = add_to_pagemap(addr, &pme, pm);
+			if (err)
+				return err;
 		}
 
-		/* check that 'vma' actually covers this address,
-		 * and that it isn't a huge page vma */
-		if (vma && (vma->vm_start <= addr) &&
-		    !is_vm_hugetlb_page(vma)) {
+		if (!vma || vma->vm_start >= end)
+			break;
+		/*
+		 * We can't possibly be in a hugetlb VMA. In general,
+		 * for a mm_walk with a pmd_entry and a hugetlb_entry,
+		 * the pmd_entry can only be called on addresses in a
+		 * hugetlb if the walk starts in a non-hugetlb VMA and
+		 * spans a hugepage VMA. Since pagemap_read walks are
+		 * PMD-sized and PMD-aligned, this will never be true.
+		 */
+		BUG_ON(is_vm_hugetlb_page(vma));
+
+		/* Addresses in the VMA. */
+		for (; addr < min(end, vma->vm_end); addr += PAGE_SIZE) {
+			pagemap_entry_t pme;
 			pte = pte_offset_map(pmd, addr);
 			pte_to_pagemap_entry(&pme, pm, vma, addr, *pte);
-			/* unmap before userspace copy */
 			pte_unmap(pte);
+			err = add_to_pagemap(addr, &pme, pm);
+			if (err)
+				return err;
 		}
-		err = add_to_pagemap(addr, &pme, pm);
-		if (err)
-			return err;
+
+		if (addr == end)
+			break;
+
+		vma = find_vma(walk->mm, addr);
 	}
 
 	cond_resched();



  parent reply	other threads:[~2015-04-26 14:02 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-26 14:01 [PATCH 3.14 00/42] 3.14.40-stable review Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 01/42] tcp: prevent fetching dst twice in early demux code Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 02/42] net/mlx4_en: Call register_netdevice in the proper location Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 03/42] ipv6: Dont reduce hop limit for an interface Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 04/42] tcp: fix FRTO undo on cumulative ACK of SACKed range Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 05/42] ipv6: protect skb->sk accesses from recursive dereference inside the stack Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 06/42] tcp: tcp_make_synack() should clear skb->tstamp Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 07/42] 8139cp: Call dev_kfree_skby_any instead of kfree_skb Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 08/42] 8139too: Call dev_kfree_skby_any instead of dev_kfree_skb Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 09/42] r8169: " Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 10/42] bonding: Call dev_kfree_skby_any instead of kfree_skb Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 11/42] bnx2: Call dev_kfree_skby_any instead of dev_kfree_skb Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 12/42] tg3: " Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 13/42] ixgb: " Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 14/42] mlx4: " Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 15/42] benet: Call dev_kfree_skby_any instead of kfree_skb Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 16/42] gianfar: Carefully free skbs in functions called by netpoll Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 17/42] Bluetooth: Enable Atheros 0cf3:311e for firmware upload Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 18/42] Bluetooth: Add firmware update for Atheros 0cf3:311f Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 19/42] Bluetooth: btusb: Add IMC Networks (Broadcom based) Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 20/42] Bluetooth: sort the list of IDs in the source code Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 21/42] Bluetooth: append new supported device to the list [0b05:17d0] Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 22/42] Bluetooth: Add support for Intel bootloader devices Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 23/42] Bluetooth: Ignore isochronous endpoints for Intel USB bootloader Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 24/42] Bluetooth: Add support for Acer [13D3:3432] Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 25/42] Bluetooth: Add support for Broadcom device of Asus Z97-DELUXE motherboard Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 26/42] Add a new PID/VID 0227/0930 for AR3012 Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 27/42] Bluetooth: Add support for Acer [0489:e078] Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 28/42] Bluetooth: ath3k: Add support of MCI 13d3:3408 bt device Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 29/42] Bluetooth: Add USB device 04ca:3010 as Atheros AR3012 Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 30/42] mm: hwpoison: drop lru_add_drain_all() in __soft_offline_page() Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 31/42] netfilter: conntrack: disable generic tracking for known protocols Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 32/42] KVM: x86: SYSENTER emulation is broken Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 33/42] move d_rcu from overlapping d_child to overlapping d_alias Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 34/42] sched: declare pid_alive as inline Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 35/42] vm: add VM_FAULT_SIGSEGV handling support Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 36/42] vm: make stack guard page errors return VM_FAULT_SIGSEGV rather than SIGBUS Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 37/42] ARM: 8108/1: mm: Introduce {pte,pmd}_isset and {pte,pmd}_isclear Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 38/42] ARM: 8109/1: mm: Modify pte_write and pmd_write logic for LPAE Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 39/42] x86: mm: move mmap_sem unlock from mm_fault_error() to caller Greg Kroah-Hartman
2015-04-26 14:01 ` [PATCH 3.14 40/42] sb_edac: avoid INTERNAL ERROR message in EDAC with unspecified channel Greg Kroah-Hartman
2015-04-26 14:02 ` Greg Kroah-Hartman [this message]
2015-04-26 14:02 ` [PATCH 3.14 42/42] proc/pagemap: walk page tables under pte lock Greg Kroah-Hartman
2015-04-26 20:03 ` [PATCH 3.14 00/42] 3.14.40-stable review Guenter Roeck
2015-04-27 17:19 ` Shuah Khan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150426134249.947614911@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=gorcunov@openvz.org \
    --cc=hughd@google.com \
    --cc=jamieliu@google.com \
    --cc=kirill@shutemov.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=n-horiguchi@ah.jp.nec.com \
    --cc=pfeiner@google.com \
    --cc=stable@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=xemul@parallels.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).