Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp147837ybb; Fri, 27 Mar 2020 18:14:15 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtar8o0bua742H01uBeKrn8YVRX/Eh8ePPwkc1WoGrU7EbKr/eC60s3xJSfwEBEAc5RhFwF X-Received: by 2002:a9d:18f:: with SMTP id e15mr1084341ote.42.1585358055649; Fri, 27 Mar 2020 18:14:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585358055; cv=none; d=google.com; s=arc-20160816; b=XDw7jLtBQT0mTTWwzyp3hzi9yAIQREAQ96XkXygKzH+9WUtdBjJ/HFfgm6nw6WN4zj oSK6T9kfjj74apd62o9PUNMVRba6pE7kk/KIa/bjK6ZCL2NRYhpPp0346z+I1SqSge+O tm4kBOWMQN1Ah0GhNDKKkhYqTArY3OHe1Bs9LBOFBZcFVWoprtIfkTB7o8Wgdm21de5i HF+TEqFE+zloGXjvvfjDnHNufWOPeNMIPCbMHQdj9eB49X9WNrzG77y3XeCR0TgSwLGW aVOHdi0giCDLJL0TI4BQ3M6JV0UmFBgjqOD792EbBhJn0HLHKDOBov8Pn6dVVFhN7AGy FBcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=VNo3FndSyfgarKuL3MCerw7v/TlJBnMEpF3pRFegZd8=; b=PPFGHB+e6wTvSCFx9REzS4rci6r3iX8hu7JCiYTLOt2uwn3WmQEe+59Bek4z6a/Z+I CFwhLh2sx70b5GpW5bonDKjjrwF+S+BGTq4rrHgqEkfLTuAfxb4kP//bxBQeoQW/JLLP vrAWWkjwsU/sEO2tbwp6wffDduEh/3VQGd/aeKxZ/ZKlpOQDDRF35xNvBQsNSCo4viYy 02K/4YJIoZ9q0jwEtQlReqA6BuyD956w4+6Ugp/Ku6UIb5IZE+HkNk/XoMQvmHb+go0u G9BpIIidxIqI0DKnvCGmBzwPLG069UYApxtpX2EXEOBTJv73p31CRBz6MKoVM71yjrop tdDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tWtvDPul; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 64si3345614otn.173.2020.03.27.18.14.02; Fri, 27 Mar 2020 18:14:15 -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=pass header.i=@gmail.com header.s=20161025 header.b=tWtvDPul; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726330AbgC1BMn (ORCPT + 99 others); Fri, 27 Mar 2020 21:12:43 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:39831 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726134AbgC1BMn (ORCPT ); Fri, 27 Mar 2020 21:12:43 -0400 Received: by mail-ed1-f68.google.com with SMTP id a43so13594977edf.6 for ; Fri, 27 Mar 2020 18:12:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VNo3FndSyfgarKuL3MCerw7v/TlJBnMEpF3pRFegZd8=; b=tWtvDPulZTijBQTjNmE7bkdsRPhUcHte5hyy3c8P2W4w2wTvIPMLBmpbrc7qq3zQz/ n8mKnFt7gwbUSKWrjiPX5hzOCXlquyHcU4imjvsNXSmXfx4N+pCb3AA1BwpyRT7WtRFB Qabnk6WQjtGOrkOjawTXG9AgidQFq3lp8Qmf1oVNzexRSnOg4PiTKv0Cx5DoDQ5vcMVd lGg9MYX+G+h+CTyzVzBXQ7MYvk3G9gzmSgG/alDuTA1jk49StTfbGYZ6eYLVrMbYO8iV VElIrplPuphAxfofFuo5VPPV840b0TtPzCrdoeu87k2p3mChJpbv0cwTBR11IsPvpIUt tlMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VNo3FndSyfgarKuL3MCerw7v/TlJBnMEpF3pRFegZd8=; b=KOCFsuT7LrIifU5QTUQNoIVME7dQHq1VfCtlOuzXu24cb8xNALNvRCojdidusUjDc/ N728Gq1qlsNcWfzAAuVyFZmyVT/gQU82z9KYddqcJs8pib9TUemNfOLGCC6261msDRIM usZslkiEpz4SegVV3BO5LtVXge0j7IYAmD56C2iRrFLkrn3pIDNrFV3xZzWL0heV/B+H fVecCYcdETXmxBIl5HKcyv1ktdhXJ9tohcxPE9qppeQU10CSb9odEPfw5TLCcCSyHTXO opGRiOXVFDsgeNcXj0PwD7SEBmqGEbevO1sSCLn/s19L1CXRGnkx5paDxHViDBeMetM1 itAA== X-Gm-Message-State: ANhLgQ169Zt2xZpsR3uJk9/g2rY1tfALsQyasGgUmccotMeP+W85lR+C WXkcsWpKWSr4AcQpyC04KXEISx5jRhOWV5rp1Kk= X-Received: by 2002:a50:c948:: with SMTP id p8mr1883047edh.200.1585357960413; Fri, 27 Mar 2020 18:12:40 -0700 (PDT) MIME-Version: 1.0 References: <20200327170601.18563-1-kirill.shutemov@linux.intel.com> <20200327170601.18563-6-kirill.shutemov@linux.intel.com> <20200328004034.jhzpqlv4riid27mh@box> In-Reply-To: <20200328004034.jhzpqlv4riid27mh@box> From: Yang Shi Date: Fri, 27 Mar 2020 18:12:28 -0700 Message-ID: Subject: Re: [PATCH 5/7] khugepaged: Allow to collapse PTE-mapped compound pages To: "Kirill A. Shutemov" Cc: Andrew Morton , Andrea Arcangeli , Linux MM , Linux Kernel Mailing List , "Kirill A. Shutemov" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 27, 2020 at 5:40 PM Kirill A. Shutemov wrote: > > On Fri, Mar 27, 2020 at 01:45:55PM -0700, Yang Shi wrote: > > On Fri, Mar 27, 2020 at 10:06 AM Kirill A. Shutemov > > wrote: > > > > > > We can collapse PTE-mapped compound pages. We only need to avoid > > > handling them more than once: lock/unlock page only once if it's present > > > in the PMD range multiple times as it handled on compound level. The > > > same goes for LRU isolation and putpack. > > > > > > Signed-off-by: Kirill A. Shutemov > > > --- > > > mm/khugepaged.c | 41 +++++++++++++++++++++++++++++++---------- > > > 1 file changed, 31 insertions(+), 10 deletions(-) > > > > > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > > > index b47edfe57f7b..c8c2c463095c 100644 > > > --- a/mm/khugepaged.c > > > +++ b/mm/khugepaged.c > > > @@ -515,6 +515,17 @@ void __khugepaged_exit(struct mm_struct *mm) > > > > > > static void release_pte_page(struct page *page) > > > { > > > + /* > > > + * We need to unlock and put compound page on LRU only once. > > > + * The rest of the pages have to be locked and not on LRU here. > > > + */ > > > + VM_BUG_ON_PAGE(!PageCompound(page) && > > > + (!PageLocked(page) && PageLRU(page)), page); > > > + > > > + if (!PageLocked(page)) > > > + return; > > > + > > > + page = compound_head(page); > > > dec_node_page_state(page, NR_ISOLATED_ANON + page_is_file_cache(page)); > > > unlock_page(page); > > > putback_lru_page(page); > > > > BTW, wouldn't this unlock the whole THP and put it back to LRU? > > It is the intention. Yes, understood. Considering the below case: Subpages 0, 1, 2, 3 are PTE mapped. Once subpage 0 is copied release_pte_page() would be called then the whole THP would be unlocked and putback to lru, then the loop would iterate to subpage 1, 2 and 3, but the page is not locked and on lru already. Is it intentional? > > > Then we may copy the following PTE mapped pages with page unlocked and > > on LRU. I don't see critical problem, just the pages might be on and off > > LRU by others, i.e. vmscan, compaction, migration, etc. But no one could > > take the page away since try_to_unmap() would fail, but not very > > productive. > > > > > > > @@ -537,6 +548,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > > > pte_t *_pte; > > > int none_or_zero = 0, result = 0, referenced = 0; > > > bool writable = false; > > > + LIST_HEAD(compound_pagelist); > > > > > > for (_pte = pte; _pte < pte+HPAGE_PMD_NR; > > > _pte++, address += PAGE_SIZE) { > > > @@ -561,13 +573,23 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > > > goto out; > > > } > > > > > > - /* TODO: teach khugepaged to collapse THP mapped with pte */ > > > + VM_BUG_ON_PAGE(!PageAnon(page), page); > > > + > > > if (PageCompound(page)) { > > > - result = SCAN_PAGE_COMPOUND; > > > - goto out; > > > - } > > > + struct page *p; > > > + page = compound_head(page); > > > > > > - VM_BUG_ON_PAGE(!PageAnon(page), page); > > > + /* > > > + * Check if we have dealt with the compount page > > > + * already > > > + */ > > > + list_for_each_entry(p, &compound_pagelist, lru) { > > > + if (page == p) > > > + break; > > > + } > > > + if (page == p) > > > + continue; > > > + } > > > > > > /* > > > * We can do it before isolate_lru_page because the > > > @@ -640,6 +662,9 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > > > page_is_young(page) || PageReferenced(page) || > > > mmu_notifier_test_young(vma->vm_mm, address)) > > > referenced++; > > > + > > > + if (PageCompound(page)) > > > + list_add_tail(&page->lru, &compound_pagelist); > > > } > > > if (likely(writable)) { > > > if (likely(referenced)) { > > > @@ -1185,11 +1210,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, > > > goto out_unmap; > > > } > > > > > > - /* TODO: teach khugepaged to collapse THP mapped with pte */ > > > - if (PageCompound(page)) { > > > - result = SCAN_PAGE_COMPOUND; > > > - goto out_unmap; > > > - } > > > + page = compound_head(page); > > > > > > /* > > > * Record which node the original page is from and save this > > > -- > > > 2.26.0 > > > > > > > > -- > Kirill A. Shutemov