Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1250372

[PATCH 3/5] mm: clear PG_dirty to mark page freeable

From Minchan Kim <minchan@kernel.org>
Newsgroups linux.kernel
Subject [PATCH 3/5] mm: clear PG_dirty to mark page freeable
Date 2015-10-19 08:30 +0200
Message-ID <qlc7f-4wB-13@gated-at.bofh.it> (permalink)
References <qlc7f-4wB-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Basically, MADV_FREE relies on dirty bit in page table entry
to decide whether VM allows to discard the page or not.
IOW, if page table entry includes marked dirty bit, VM shouldn't
discard the page.

However, as a example, if swap-in by read fault happens,
page table entry doesn't have dirty bit so MADV_FREE could discard
the page wrongly.

For avoiding the problem, MADV_FREE did more checks with PageDirty
and PageSwapCache. It worked out because swapped-in page lives on
swap cache and since it is evicted from the swap cache, the page has
PG_dirty flag. So both page flags check effectively prevent
wrong discarding by MADV_FREE.

However, a problem in above logic is that swapped-in page has
PG_dirty still after they are removed from swap cache so VM cannot
consider the page as freeable any more even if madvise_free is
called in future.

Look at below example for detail.

    ptr = malloc();
    memset(ptr);
    ..
    ..
    .. heavy memory pressure so all of pages are swapped out
    ..
    ..
    var = *ptr; -> a page swapped-in and could be removed from
                   swapcache. Then, page table doesn't mark
                   dirty bit and page descriptor includes PG_dirty
    ..
    ..
    madvise_free(ptr); -> It doesn't clear PG_dirty of the page.
    ..
    ..
    ..
    .. heavy memory pressure again.
    .. In this time, VM cannot discard the page because the page
    .. has *PG_dirty*

To solve the problem, this patch clears PG_dirty if only the page
is owned exclusively by current process when madvise is called
because PG_dirty represents ptes's dirtiness in several processes
so we could clear it only if we own it exclusively.

Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Minchan Kim <minchan@kernel.org>
---
 mm/madvise.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index fdfb14a78c60..5db546431285 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -312,11 +312,19 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
 		if (!page)
 			continue;
 
-		if (PageSwapCache(page)) {
+		if (PageSwapCache(page) || PageDirty(page)) {
 			if (!trylock_page(page))
 				continue;
+			/*
+			 * If page is shared with others, we couldn't clear
+			 * PG_dirty of the page.
+			 */
+			if (page_count(page) != 1 + !!PageSwapCache(page)) {
+				unlock_page(page);
+				continue;
+			}
 
-			if (!try_to_free_swap(page)) {
+			if (PageSwapCache(page) && !try_to_free_swap(page)) {
 				unlock_page(page);
 				continue;
 			}
-- 
1.9.1

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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/5] MADV_FREE refactoring and fix KSM page Minchan Kim <minchan@kernel.org> - 2015-10-19 08:30 +0200
  [PATCH 2/5] mm: skip huge zero page in MADV_FREE Minchan Kim <minchan@kernel.org> - 2015-10-19 08:30 +0200
  [PATCH 1/5] mm: MADV_FREE trivial clean up Minchan Kim <minchan@kernel.org> - 2015-10-19 08:30 +0200
  [PATCH 5/5] mm: mark stable page dirty in KSM Minchan Kim <minchan@kernel.org> - 2015-10-19 08:30 +0200
    Re: [PATCH 5/5] mm: mark stable page dirty in KSM Hugh Dickins <hughd@google.com> - 2015-10-27 03:30 +0100
      Re: [PATCH 5/5] mm: mark stable page dirty in KSM Minchan Kim <minchan@kernel.org> - 2015-10-27 08:00 +0100
  [PATCH 3/5] mm: clear PG_dirty to mark page freeable Minchan Kim <minchan@kernel.org> - 2015-10-19 08:30 +0200
    Re: [PATCH 3/5] mm: clear PG_dirty to mark page freeable Hugh Dickins <hughd@google.com> - 2015-10-27 02:30 +0100
      Re: [PATCH 3/5] mm: clear PG_dirty to mark page freeable Minchan Kim <minchan@kernel.org> - 2015-10-27 08:00 +0100
  [PATCH 4/5] mm: simplify reclaim path for MADV_FREE Minchan Kim <minchan@kernel.org> - 2015-10-19 08:40 +0200
    Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE Hugh Dickins <hughd@google.com> - 2015-10-27 03:10 +0100
      Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE yalin wang <yalin.wang2010@gmail.com> - 2015-10-27 04:50 +0100
        Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE Minchan Kim <minchan@kernel.org> - 2015-10-27 08:10 +0100
          Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE yalin wang <yalin.wang2010@gmail.com> - 2015-10-27 08:40 +0100
            Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE Minchan Kim <minchan@kernel.org> - 2015-10-27 09:20 +0100
              Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE yalin wang <yalin.wang2010@gmail.com> - 2015-10-27 10:00 +0100
                Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE yalin wang <yalin.wang2010@gmail.com> - 2015-10-28 05:10 +0100
      Re: [PATCH 4/5] mm: simplify reclaim path for MADV_FREE Minchan Kim <minchan@kernel.org> - 2015-10-27 08:00 +0100
  Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page Minchan Kim <minchan@kernel.org> - 2015-10-19 12:00 +0200
    Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page Minchan Kim <minchan@kernel.org> - 2015-10-20 09:30 +0200
    Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page Minchan Kim <minchan@kernel.org> - 2015-10-20 09:30 +0200
      Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page Andrew Morton <akpm@linux-foundation.org> - 2015-10-20 23:40 +0200
        Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page "Kirill A. Shutemov" <kirill@shutemov.name> - 2015-10-21 00:50 +0200
          Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page Minchan Kim <minchan@kernel.org> - 2015-10-21 07:20 +0200
            Re: [PATCH 0/5] MADV_FREE refactoring and fix KSM page "Kirill A. Shutemov" <kirill@shutemov.name> - 2015-10-21 10:00 +0200

csiph-web