Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp748582ybz; Wed, 15 Apr 2020 18:06:28 -0700 (PDT) X-Google-Smtp-Source: APiQypLb22CagXbD/jHxUN+CqDXOk3f81MVIHz6lD8TPD8fmMOkTVO9sdk3JD4iwq8yPEuAWRBY3 X-Received: by 2002:a17:906:7486:: with SMTP id e6mr7732846ejl.181.1586999188017; Wed, 15 Apr 2020 18:06:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586999188; cv=none; d=google.com; s=arc-20160816; b=JN1DPYw3V3rA5I+T4Qg7ZNT6JNwujQIA5UiVzQzqHZbCrDh9kmngS1htlabAIRxNAE 2nYxhEhBG9o+TKl7xZsxMtWnypFLtPuq4z52Js5s3/NcvaH+L0P4JnKc3NBufxaQq+JU 5nDxLdnhGoj3Zo4iC/yQ/NvDDyMIv8Cz8iQgy9ZeRjqTO2rkTztvylrZQvwO+WPcVn1a WDPs5PlcJyJlbDlldQCFxMUU5udouSZT5VuzMe5oWrGb6t5OYhDIQpZfmDN/R4zY9bxO GWRt5NPDmm+jB0ie6KA26JShOcQ253fg3qXadQfrrb/sL6LJyP1MV/4vRNZyjOgs2qRD UKcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=JS3W9Hw/68zB7dIcXNg1ab1Vd26AL1WyhkXsRTMX6eY=; b=OfLEYPLZUun7jqnb9Ir2hu1/SkJAEq1dFREdEQV+4p0KRfpFuK1hk1ZRNTqf9xPYhH DJJsm6G5Z88MEcnCOf6hRZ+SROvdKflVElRJhkmCi3cmNsaqhwZTDjLc9noYQr9WgG12 JMeCEWVJ4fsdqj2BjPxpHXeNzRPxtpf1SXfDq7crdncbj7vmMJazDcXRG7lbbfarFf+l DWzpo9qEaGkeCXIbPuyAidyUN3rML7nN++joZ6N2v2h7kpFZDfvvlNDbNV9x9pW5JOwQ 3okNskhys577UhhwAfMf+pNKNeHPB5Cszy5SFLsDIyzmbXV8VpifxkkUheKtGLY3fRzF /CHA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p17si6700502ejd.260.2020.04.15.18.06.03; Wed, 15 Apr 2020 18:06:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2442082AbgDOUj0 (ORCPT + 99 others); Wed, 15 Apr 2020 16:39:26 -0400 Received: from out30-131.freemail.mail.aliyun.com ([115.124.30.131]:45002 "EHLO out30-131.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438172AbgDOUjY (ORCPT ); Wed, 15 Apr 2020 16:39:24 -0400 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e07488;MF=yang.shi@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0TvduEle_1586983159; Received: from US-143344MP.local(mailfrom:yang.shi@linux.alibaba.com fp:SMTPD_---0TvduEle_1586983159) by smtp.aliyun-inc.com(127.0.0.1); Thu, 16 Apr 2020 04:39:21 +0800 Subject: Re: [PATCHv3, RESEND 5/8] khugepaged: Allow to collapse a page shared across fork To: "Kirill A. Shutemov" , akpm@linux-foundation.org, Andrea Arcangeli Cc: Zi Yan , Ralph Campbell , John Hubbard , William Kucharski , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20200413125220.663-1-kirill.shutemov@linux.intel.com> <20200413125220.663-6-kirill.shutemov@linux.intel.com> From: Yang Shi Message-ID: Date: Wed, 15 Apr 2020 13:39:16 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <20200413125220.663-6-kirill.shutemov@linux.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/13/20 5:52 AM, Kirill A. Shutemov wrote: > The page can be included into collapse as long as it doesn't have extra > pins (from GUP or otherwise). > > Logic to check the refcound is moved to a separate function. s/refcound/refcount > Note that the function is ready to deal with compound pages. It's > preparation for the following patch. > > VM_BUG_ON_PAGE() was removed from __collapse_huge_page_copy() as the > invariant it checks is no longer valid: the source can be mapped > multiple times now. > > Signed-off-by: Kirill A. Shutemov > --- > mm/khugepaged.c | 41 ++++++++++++++++++++++++++++++----------- > 1 file changed, 30 insertions(+), 11 deletions(-) Just a minor typo problem. Acked-by: Yang Shi > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index e3e41c2768d8..f9864644c3b7 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -529,6 +529,24 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte) > } > } > > +static bool is_refcount_suitable(struct page *page) > +{ > + int expected_refcount, refcount; > + > + refcount = page_count(page); > + expected_refcount = total_mapcount(page); > + if (PageSwapCache(page)) > + expected_refcount += compound_nr(page); > + > + if (IS_ENABLED(CONFIG_DEBUG_VM) && expected_refcount > refcount) { > + pr_err("expected_refcount: %d, refcount: %d\n", > + expected_refcount, refcount); > + dump_page(page, "Unexpected refcount"); > + } > + > + return page_count(page) == expected_refcount; > +} > + > static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > unsigned long address, > pte_t *pte) > @@ -581,11 +599,17 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, > } > > /* > - * cannot use mapcount: can't collapse if there's a gup pin. > - * The page must only be referenced by the scanned process > - * and page swap cache. > + * Check if the page has any GUP (or other external) pins. > + * > + * The page table that maps the page has been already unlinked > + * from the page table tree and this process cannot get > + * an additinal pin on the page. > + * > + * New pins can come later if the page is shared across fork, > + * but not for the this process. It is fine. The other process > + * cannot write to the page, only trigger CoW. > */ > - if (page_count(page) != 1 + PageSwapCache(page)) { > + if (!is_refcount_suitable(page)) { > unlock_page(page); > result = SCAN_PAGE_COUNT; > goto out; > @@ -672,7 +696,6 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page, > } else { > src_page = pte_page(pteval); > copy_user_highpage(page, src_page, address, vma); > - VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page); > release_pte_page(src_page); > /* > * ptl mostly unnecessary, but preempt has to > @@ -1201,12 +1224,8 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, > goto out_unmap; > } > > - /* > - * cannot use mapcount: can't collapse if there's a gup pin. > - * The page must only be referenced by the scanned process > - * and page swap cache. > - */ > - if (page_count(page) != 1 + PageSwapCache(page)) { > + /* Check if the page has any GUP (or other external) pins */ > + if (!is_refcount_suitable(page)) { > result = SCAN_PAGE_COUNT; > goto out_unmap; > }