Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp729688ybb; Fri, 10 Apr 2020 08:58:16 -0700 (PDT) X-Google-Smtp-Source: APiQypL8lY/ZDfvFdqujUtNA3tnv0LOcBduz6kNeBsaOxtoVVSzdAfgf3E6Hl1Xm/SyqphV+y+sb X-Received: by 2002:a0c:9068:: with SMTP id o95mr5868056qvo.101.1586534296877; Fri, 10 Apr 2020 08:58:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586534296; cv=none; d=google.com; s=arc-20160816; b=vnJpLdr752qFBIsBnS7nDNv8w/VzXK1aHCiXsgLtRBr8u24SyPsQFE4YbR7zwQ95ux h8RcF+ek3Mc5fpUygorq93RaJKmf9g/dSIedwLwD7lc0H6PD96wVsXTUaKlnXgqqWfPZ GgPZh5Ax/wJ6CWhVbNJLKWydZsUhzfQc5z4tcFuLiLOkUEUNzf9shwa9R8zPHqlv8ZoW UQbEHLMfMaXNvuMCcXq93RN0vEREAzZ4BVPDTGN20VlhNKDLVjM01zdWwUzGurNOo7D1 CHavTfLxiLeRBax6jr5WGiO1cvvLEut8oz61bXUJylpaPcfLc/ZWhcNcyRHmVU+dKRg5 e2CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=Wu3QFPy2u02irBlE9rfNz4x/NqhgeVrVQTYnGJ11NYo=; b=VFNwK5woTNZtNmVRXjCHAc5Z6xOD4gkvx9H847aUCSaStTBLyI93IAR4V/LZCuR8+a HutqKubui7x0ryaSIO/3/mqgRirRlB+aHLwdzSOKAWRRwmvi8ylavLewVM/zsXOJK7GA cKebnmNis2oEpUDTVGvdqyIIkGdTO8qXmn/SbsGMKzdeGeTf7dtG65FZwI43Jk73JRXO 5I3B0MMi1FOKKw74jCSPxM35jf3mZFNx0uoAnuxCrWiGf9yqw7Zqyl6/ldmUJxoziTxJ 3XqfX8wM3jQ6hD2c74626vU+vY/vRhOOGG4Q2y0OVI1/nZ90gZaC+tlTOjEtitQ43dhR MoDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=uS8y2cKR; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i19si1256116qvm.184.2020.04.10.08.58.02; Fri, 10 Apr 2020 08:58:16 -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=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=uS8y2cKR; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726651AbgDJPzt (ORCPT + 99 others); Fri, 10 Apr 2020 11:55:49 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:33756 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726080AbgDJPzs (ORCPT ); Fri, 10 Apr 2020 11:55:48 -0400 Received: by mail-lf1-f68.google.com with SMTP id h6so1677886lfc.0 for ; Fri, 10 Apr 2020 08:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Wu3QFPy2u02irBlE9rfNz4x/NqhgeVrVQTYnGJ11NYo=; b=uS8y2cKR1FfXqahoaPTcszOGRTwbPpTELgbZQoR3u9B7Lvyac8wFhWrptev/dAJMiw 53OCXKiazqb+yZob4y0e9GoKGvdfP3GL5wTS4Rux8cvLKFEKoMcO0JdQuf8Zc3DWCZDQ ruu1L/FSPpqTT4+TjcyWnaZYAKvyEkJtjyRvEh47at78e20FWiFn8GB56b3IuZ4aAjIF BDj2L/LTNuCbjJAXxPH4O6B60kmKca6zwASadExwJTNZeN/YqcUSyR+hlUPp3e0JMVYn rd1qvhjvh75W7MIHhdXR7Lf4JKrRs5RgsnR4bF6NZslbOIbULzx8/+CLEPG4982Oqknn O50A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Wu3QFPy2u02irBlE9rfNz4x/NqhgeVrVQTYnGJ11NYo=; b=GWVy4ik4C+yUvmOUy5NquOUQ2rA7j4hMU0OzxLd+3g5iv4EezKA+1pyH2XbT47VK2s sgJEvAESwxU3E2Ly7cUMKqBnV2teEspGxz4n8o+8UYdTd18jVENtbfmEvg8jW3pdsUpQ N9FbQI3XVm8hpk8rFSGqK9jdQG9Sa3dpfJJ07aXAazqn2TXjforMIBp6+WGIQDPGI+AK m2H+SN+WOBhxdbI//9u+bWlmuuivH+lZRb60wazblEtJ94LqfCckRg3n5vOtwX/wVHa1 GE18Kbn8/1vJWVzgZWEv5Ibmi2JfEJILnXD4/IZjaVnXxTTdWz0P5MyJwZZkYU9LrTsQ Y9Nw== X-Gm-Message-State: AGi0PuZL+7Huz35UonmTa4l+awHFvWZVtJvKK5SCjdcqRmHOvYTInUOk vjpKsKSEl27Cs2JCF9Y4mDwhoQ== X-Received: by 2002:ac2:5611:: with SMTP id v17mr2990420lfd.137.1586534144915; Fri, 10 Apr 2020 08:55:44 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id c22sm1285409ljh.66.2020.04.10.08.55.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2020 08:55:44 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id 4C005102110; Fri, 10 Apr 2020 18:55:43 +0300 (+03) Date: Fri, 10 Apr 2020 18:55:43 +0300 From: "Kirill A. Shutemov" To: John Hubbard Cc: akpm@linux-foundation.org, Andrea Arcangeli , Zi Yan , Yang Shi , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: Re: [PATCHv2 5/8] khugepaged: Allow to callapse a page shared across fork Message-ID: <20200410155543.i66uz6pbynfvkhak@box> References: <20200403112928.19742-1-kirill.shutemov@linux.intel.com> <20200403112928.19742-6-kirill.shutemov@linux.intel.com> <5a57635b-ed75-8f09-6f0c-5623f557fc55@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5a57635b-ed75-8f09-6f0c-5623f557fc55@nvidia.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 06, 2020 at 02:30:07PM -0700, John Hubbard wrote: > On 4/3/20 4:29 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). > > Hi Kirill, > > s/callapse/collapse/ in the Subject line. > > The commit message should mention that you're also removing a > VM_BUG_ON_PAGE(). > > > > > Signed-off-by: Kirill A. Shutemov > > --- > > mm/khugepaged.c | 25 ++++++++++++++----------- > > 1 file changed, 14 insertions(+), 11 deletions(-) > > > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > > index 57ff287caf6b..1e7e6543ebca 100644 > > --- a/mm/khugepaged.c > > +++ b/mm/khugepaged.c > > @@ -581,11 +581,18 @@ 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 > > + * additinal pin on the page. > > > I'd recommend this wording instead, for the last two lines: > > * from the page table tree. Therefore, this page will not > * normally receive any additional pins. I guess I'm not clear enough. The point is that the page cannot get any new pins from this process. It can get new pin from other process after the check. But it is fine because if the page is mapped multiple times it has to be write-protected (CoW after fork()) and we can rely that page's content will not change under us. Does it make sense? Wording suggestions are welcome. > > + * > > + * 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 (total_mapcount(page) + PageSwapCache(page) != > > + page_count(page)) { > > > I think it's time to put that logic ( "does this page have any extra references") > into a small function. It's already duplicated once below. And the documentation is > duplicated as well. Fair enough. But comments have to stay where they are. Because the context is different. The first time we check speculatively, before the page table is unlinked from the page table tree and this check is inherintly racy. Unlike the second one. > I took a quick peek at this patch because, after adding pin_user_pages*() APIs earlier > to complement get_user_pages*(), I had a moment of doubt here: what if I'd done it in > a way that required additional logic here? Fortunately, that's not the case: all > pin_user_pages() calls on huge pages take a "primary/real" refcount, in addition > to scribbling into the compound_pincount_ptr() area. whew. :) > > > > > unlock_page(page); > > result = SCAN_PAGE_COUNT; > > goto out; > > @@ -672,7 +679,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 > > @@ -1206,12 +1212,9 @@ 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 (total_mapcount(page) + PageSwapCache(page) != > > + page_count(page)) {> result = SCAN_PAGE_COUNT; > > goto out_unmap; > > } > > > > > > thanks, > -- > John Hubbard > NVIDIA -- Kirill A. Shutemov