Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3521473ybi; Sun, 14 Jul 2019 16:14:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqzbk8flDjfAqw81tu9dxSrUQTdObZxk/pJaovTJuMFxRJhPZjeeXq7Hq1RdhW/Uh3XzgC16 X-Received: by 2002:a17:90a:ab0d:: with SMTP id m13mr24712937pjq.84.1563146096359; Sun, 14 Jul 2019 16:14:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563146096; cv=none; d=google.com; s=arc-20160816; b=dKCJ26I2YV2QROi+oy1/UGeK+xQjput3IKxEINf/PKNljgmr9uU9ELvDDTr5LxAHYL UvWZiCarrsE1Irb9RQgG9R1MU0i4InQ1RXQTHbPaCAeUjFaQoo3/gC2rStH41Yg5L8Dw GtljReWaNF8c2zq3usnEYHrm8r7lG9XdzOK8V8EuhjU3zFVfZ39aIatthrujQ2mogW1t F/QK+AEOHLtCakS0GWsOXWML2QXe+g3WejBZOuSY5QKbq6YN3w2oS/0e/6w7yJHpby3N Nfb0I/rN3YrZ+9A1gWYISVTZYfya9gNKSnyYRu2FaIe2ORRNqtgVWNTJzX3WxEVHnt1x z9qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=QprZyTVrRGOU97hs8P2pLED7gdEo6Bn7w3dBtOXj1PI=; b=yyMsuLSgBcJ/2dnyfdiaZls2zwuS1GmbdjbdUND6IWaSvekx/ayWH33UPNHQS/LmKb /0EjpAn81BVsfHOpmB/L8KzoRzemOLAY+Yc9kkw3/83krLcPapc59nPv8GB4IWxSur47 sEc9l5HilQCNJIR+iPak9QbCd/nDSxblLcqEjqGTRA37tQ0Pc2SN5eu0W+TbAJnhnPPx 5RJinkEDL/6fSyo1rTDWAWAB3K1/BGutW5qh1Q1y6m+Ijkqh4bi64xn+hCEZj2cf4VWx 4mFko280JpUZW/iBmYNNeVwub7aFU73zav+OopLOfCGRx2a3E5ylI4gTl7lTf6aQOt9F vmrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=A+C44U4G; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p1si13391962plq.286.2019.07.14.16.14.27; Sun, 14 Jul 2019 16:14:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=A+C44U4G; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728898AbfGNXLy (ORCPT + 99 others); Sun, 14 Jul 2019 19:11:54 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:45871 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728754AbfGNXLx (ORCPT ); Sun, 14 Jul 2019 19:11:53 -0400 Received: by mail-pl1-f193.google.com with SMTP id y8so7328277plr.12; Sun, 14 Jul 2019 16:11:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=QprZyTVrRGOU97hs8P2pLED7gdEo6Bn7w3dBtOXj1PI=; b=A+C44U4GHiHGQET3tLkpB31RD4wI6fK5Vo1rnT8BcbP+YePUM/RwDLOppKtpT48bQh dxrFo9S5oW64hvrbSIZ7HjmyHRxW4LE5TY+bqLOjfjjd0c9MauojzUsXK30QjZhshJhK tS4TpbLjnw0AgnEkAf90FdNebDnuacYZaEn2NZ1tdnfHCuXQkOLG9g3H8BBR50IW0Dyk aDP1nW3xQeCzrAtrSJBTRl3Tu99vC2S7j0uFKH6weRrUWJlmz9HOjg7szu/TB8C0PdZz OjtB+Q365C6wqGTtJ3ZmP4hkT5SfsLd5dljivGfZYl9nOkuE+znhjyUEOU4aans/SIlZ KkGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=QprZyTVrRGOU97hs8P2pLED7gdEo6Bn7w3dBtOXj1PI=; b=eRraLSE05Z4hcVqXWOfEsADlj2beRybZYHUAEM0OdazHQH9+dTd7Dag52sxv/m2XXV s6HfGt0Due8uPV00HVIT/miJ2ZVZ2eU9JjXuzt9oGPjEJg2a7x+iWNKg3YKzvQtcSOn6 /oBcZEMm08rOEqobhFY3tqswa43BqWZKerKOxlsvqMGd/ilW8E7TGXadwqra8wI+e247 ERrK2qs9oi7L0FMpK3HP7//a5lQON69SnAXFdx3A9aYDpdPJExeIckCQz/GBPJa+Wp45 QPTVH4uCY5ftMsDbkN9c/QMr8jdDl11gtWxWGf71voNUi5d/wxl+otUYb96c0mO/Qhhi wUmw== X-Gm-Message-State: APjAAAXjnfQqWRAH3aIhUJXf90jViG1GWKVg6xWI6mTrqx0Am1IsOwz3 2Jd8jcrznVubvSlabebGjDo= X-Received: by 2002:a17:902:1e6:: with SMTP id b93mr24790653plb.295.1563145911990; Sun, 14 Jul 2019 16:11:51 -0700 (PDT) Received: from google.com ([2401:fa00:d:0:98f1:8b3d:1f37:3e8]) by smtp.gmail.com with ESMTPSA id s6sm22459338pfs.122.2019.07.14.16.11.46 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sun, 14 Jul 2019 16:11:50 -0700 (PDT) Date: Mon, 15 Jul 2019 08:11:44 +0900 From: Minchan Kim To: Johannes Weiner Cc: Andrew Morton , linux-mm , LKML , linux-api@vger.kernel.org, Michal Hocko , Tim Murray , Joel Fernandes , Suren Baghdasaryan , Daniel Colascione , Shakeel Butt , Sonny Rao , oleksandr@redhat.com, hdanton@sina.com, lizeb@google.com, Dave Hansen , "Kirill A . Shutemov" Subject: Re: [PATCH v4 4/4] mm: introduce MADV_PAGEOUT Message-ID: <20190714231144.GB128252@google.com> References: <20190711012528.176050-1-minchan@kernel.org> <20190711012528.176050-5-minchan@kernel.org> <20190711184223.GD20341@cmpxchg.org> <20190712051828.GA128252@google.com> <20190712135809.GB31107@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190712135809.GB31107@cmpxchg.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 12, 2019 at 09:58:09AM -0400, Johannes Weiner wrote: > On Fri, Jul 12, 2019 at 02:18:28PM +0900, Minchan Kim wrote: > > Hi Johannes, > > > > On Thu, Jul 11, 2019 at 02:42:23PM -0400, Johannes Weiner wrote: > > > On Thu, Jul 11, 2019 at 10:25:28AM +0900, Minchan Kim wrote: > > > > @@ -480,6 +482,198 @@ static long madvise_cold(struct vm_area_struct *vma, > > > > return 0; > > > > } > > > > > > > > +static int madvise_pageout_pte_range(pmd_t *pmd, unsigned long addr, > > > > + unsigned long end, struct mm_walk *walk) > > > > +{ > > > > + struct mmu_gather *tlb = walk->private; > > > > + struct mm_struct *mm = tlb->mm; > > > > + struct vm_area_struct *vma = walk->vma; > > > > + pte_t *orig_pte, *pte, ptent; > > > > + spinlock_t *ptl; > > > > + LIST_HEAD(page_list); > > > > + struct page *page; > > > > + unsigned long next; > > > > + > > > > + if (fatal_signal_pending(current)) > > > > + return -EINTR; > > > > + > > > > + next = pmd_addr_end(addr, end); > > > > + if (pmd_trans_huge(*pmd)) { > > > > + pmd_t orig_pmd; > > > > + > > > > + tlb_change_page_size(tlb, HPAGE_PMD_SIZE); > > > > + ptl = pmd_trans_huge_lock(pmd, vma); > > > > + if (!ptl) > > > > + return 0; > > > > + > > > > + orig_pmd = *pmd; > > > > + if (is_huge_zero_pmd(orig_pmd)) > > > > + goto huge_unlock; > > > > + > > > > + if (unlikely(!pmd_present(orig_pmd))) { > > > > + VM_BUG_ON(thp_migration_supported() && > > > > + !is_pmd_migration_entry(orig_pmd)); > > > > + goto huge_unlock; > > > > + } > > > > + > > > > + page = pmd_page(orig_pmd); > > > > + if (next - addr != HPAGE_PMD_SIZE) { > > > > + int err; > > > > + > > > > + if (page_mapcount(page) != 1) > > > > + goto huge_unlock; > > > > + get_page(page); > > > > + spin_unlock(ptl); > > > > + lock_page(page); > > > > + err = split_huge_page(page); > > > > + unlock_page(page); > > > > + put_page(page); > > > > + if (!err) > > > > + goto regular_page; > > > > + return 0; > > > > + } > > > > + > > > > + if (isolate_lru_page(page)) > > > > + goto huge_unlock; > > > > + > > > > + if (pmd_young(orig_pmd)) { > > > > + pmdp_invalidate(vma, addr, pmd); > > > > + orig_pmd = pmd_mkold(orig_pmd); > > > > + > > > > + set_pmd_at(mm, addr, pmd, orig_pmd); > > > > + tlb_remove_tlb_entry(tlb, pmd, addr); > > > > + } > > > > + > > > > + ClearPageReferenced(page); > > > > + test_and_clear_page_young(page); > > > > + list_add(&page->lru, &page_list); > > > > +huge_unlock: > > > > + spin_unlock(ptl); > > > > + reclaim_pages(&page_list); > > > > + return 0; > > > > + } > > > > + > > > > + if (pmd_trans_unstable(pmd)) > > > > + return 0; > > > > +regular_page: > > > > + tlb_change_page_size(tlb, PAGE_SIZE); > > > > + orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); > > > > + flush_tlb_batched_pending(mm); > > > > + arch_enter_lazy_mmu_mode(); > > > > + for (; addr < end; pte++, addr += PAGE_SIZE) { > > > > + ptent = *pte; > > > > + if (!pte_present(ptent)) > > > > + continue; > > > > + > > > > + page = vm_normal_page(vma, addr, ptent); > > > > + if (!page) > > > > + continue; > > > > + > > > > + /* > > > > + * creating a THP page is expensive so split it only if we > > > > + * are sure it's worth. Split it if we are only owner. > > > > + */ > > > > + if (PageTransCompound(page)) { > > > > + if (page_mapcount(page) != 1) > > > > + break; > > > > + get_page(page); > > > > + if (!trylock_page(page)) { > > > > + put_page(page); > > > > + break; > > > > + } > > > > + pte_unmap_unlock(orig_pte, ptl); > > > > + if (split_huge_page(page)) { > > > > + unlock_page(page); > > > > + put_page(page); > > > > + pte_offset_map_lock(mm, pmd, addr, &ptl); > > > > + break; > > > > + } > > > > + unlock_page(page); > > > > + put_page(page); > > > > + pte = pte_offset_map_lock(mm, pmd, addr, &ptl); > > > > + pte--; > > > > + addr -= PAGE_SIZE; > > > > + continue; > > > > + } > > > > + > > > > + VM_BUG_ON_PAGE(PageTransCompound(page), page); > > > > + > > > > + if (isolate_lru_page(page)) > > > > + continue; > > > > + > > > > + if (pte_young(ptent)) { > > > > + ptent = ptep_get_and_clear_full(mm, addr, pte, > > > > + tlb->fullmm); > > > > + ptent = pte_mkold(ptent); > > > > + set_pte_at(mm, addr, pte, ptent); > > > > + tlb_remove_tlb_entry(tlb, pte, addr); > > > > + } > > > > + ClearPageReferenced(page); > > > > + test_and_clear_page_young(page); > > > > + list_add(&page->lru, &page_list); > > > > + } > > > > + > > > > + arch_leave_lazy_mmu_mode(); > > > > + pte_unmap_unlock(orig_pte, ptl); > > > > + reclaim_pages(&page_list); > > > > + cond_resched(); > > > > + > > > > + return 0; > > > > +} > > > > > > I know you have briefly talked about code sharing already. > > > > > > While I agree that sharing with MADV_FREE is maybe a stretch, I > > > applied these patches and compared the pageout and the cold page table > > > functions, and they are line for line the same EXCEPT for 2-3 lines at > > > the very end, where one reclaims and the other deactivates. It would > > > be good to share here, it shouldn't be hard or result in fragile code. > > > > Fair enough if we leave MADV_FREE. > > > > > > > > Something like int madvise_cold_or_pageout_range(..., bool pageout)? > > > > How about this? > > > > From 41592f23e876ec21e49dc3c76dc89538e2bb16be Mon Sep 17 00:00:00 2001 > > From: Minchan Kim > > Date: Fri, 12 Jul 2019 14:05:36 +0900 > > Subject: [PATCH] mm: factor out common parts between MADV_COLD and > > MADV_PAGEOUT > > > > There are many common parts between MADV_COLD and MADV_PAGEOUT. > > This patch factor them out to save code duplication. > > > > Signed-off-by: Minchan Kim > > This looks much better, thanks! > > > @@ -423,6 +445,12 @@ static int madvise_cold_pte_range(pmd_t *pmd, unsigned long addr, > > > > VM_BUG_ON_PAGE(PageTransCompound(page), page); > > > > + if (pageout) { > > + if (isolate_lru_page(page)) > > + continue; > > + list_add(&page->lru, &page_list); > > + } > > + > > if (pte_young(ptent)) { > > ptent = ptep_get_and_clear_full(mm, addr, pte, > > tlb->fullmm); > > One thought on the ordering here. > > When LRU isolation fails, it would still make sense to clear the young > bit: we cannot reclaim the page as we wanted to, but the user still > provided a clear hint that the page is cold and she won't be touching > it for a while. MADV_PAGEOUT is basically MADV_COLD + try_to_reclaim. > So IMO isolation should go to the end next to deactivate_page(). Sure, I will modify MADV_PAGEOUT patch instead of refactoring one. Thanks for the review, Johannes!