Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1087905ybi; Fri, 14 Jun 2019 08:25:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqxJcBiqoJ5rXIeKR9f0Nf7+PB2S4+GeuXB0KtclZMtPMge1q1BGV/yMWW/ZSKHMZhv6aBVm X-Received: by 2002:a63:81c6:: with SMTP id t189mr35718690pgd.293.1560525920735; Fri, 14 Jun 2019 08:25:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560525920; cv=none; d=google.com; s=arc-20160816; b=JbFpZ8NQzPdFtySZPwX+wsbmrZjm7RE59566AYDk3dX6omwhgIQdqOGHto9mO1BjST SIZsikCpazrB6yTgO6SWMZyjyjFc4I3zXjZ5PqZQXqjxWCuEiroox4BVUd2IY4uPPIBB bG5X2cZ4iIYq0BjjjukHqSfnnHjmZkMILbkOnURe1le9FrAkcQg/sqdj8Od0p+dZiIc2 IoIrE9VOh1yYftANtN5gJhXw1Ih4iIAi7+QPbSMSGelogvmrejDMMPWSAZyJnlUk14mZ 4p8C94SwU4oDK18/n805swIoWA0cJaRIzruya+qsPdP+DG2oQHKVAOi7nTxuVzuA6RxC U68w== 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=b5mc7IOtZTIYzG3UEP4sVSXC9GRywSomfDBGG2VeoWI=; b=rsA7vhDiIHKUdCrDqdoqK4C6ZnCcTnzw3z1WhzDp5KGwjZ8uzajCT852UjBu261kq7 9LNpCEdQcpAO5k1p5BXH/TYN4L51xTOshht/QyTaYSwE3XRYeJLccfTZtZK6sGUy3ghX j9Bd9Y93hFZKycD6nGaGg4lHvLKg/QmozT3aOySZj2uroIaHKs561PlB4LBS6T7XsOtJ RP5mARc7jswCsiv9TVPp7MrHYiQV07cxx8eWNjdqrj8ynVroFu42OoJtxCIkv1se5e4n KsHPSxMm2ombjBIZYTVlhg+SO4hvFODCo92dMhtQ5mjqsckFTp2+rnU/jp7t7y8hMoP1 1EYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=KnTCmViR; 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 e6si2672131pfe.111.2019.06.14.08.25.04; Fri, 14 Jun 2019 08:25:20 -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=KnTCmViR; 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 S1726490AbfFNPYb (ORCPT + 99 others); Fri, 14 Jun 2019 11:24:31 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:34837 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725923AbfFNPYa (ORCPT ); Fri, 14 Jun 2019 11:24:30 -0400 Received: by mail-io1-f67.google.com with SMTP id m24so6670743ioo.2 for ; Fri, 14 Jun 2019 08:24:29 -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=b5mc7IOtZTIYzG3UEP4sVSXC9GRywSomfDBGG2VeoWI=; b=KnTCmViRin1UgLkXIhzoE2OPV9TlwVESbaXCiEIMM2WNU5h4E9lpY0vF0ycwvoRD/Q Rrwmm35gXN74dCUWB8WvT8bjZ/1ewsxImQA28kH0+htgddgjTH8fyQm+OcR4qiFHOVKC q+d4bgfBXRRCy0RHBkrGVnD0FIBi8h0vKCMXs+gRBz0RPt+yhpG0JRTbh/iVVJSVHc3q N15E8ikRs9oCaIAvlmeDXsA9ylpFj7nVeN/GfY0XCm3Xy+ikuCzkpanl008D2KaUZ7Kr USKoXTAsmfArudlWKCx4S/cMNM3n3zPAhznScDp76DDGGOtaTpKvWnayZ6NHG9WOXRJU Bpag== 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=b5mc7IOtZTIYzG3UEP4sVSXC9GRywSomfDBGG2VeoWI=; b=B902XFx98wOQ8PIqZV5QmZ/nOOY1mjvJrqF8LNp2CZCmvPDVrZ8OwIKO+5hyxSdlM7 oX0AZjrh3Lif6XiXgm8pQmcpklOcuhCqnkRxxIr+aZtUln3RajOd71YTOVKDJ+zqOHf2 v2GOCwFV0GyMtOq3d4GshuUvds5ja5/yS5kyakwIbHHV9/3JYT5okX+/WZg2cHAye0BU 659yVhn65/OoyATsXFyMg0fR0wPxGMfX5AYTg9rjcnS+F/SqltKQwE/wWUzAsU+xaiFm QutL4r0uJ7ghWUmIBXP85nhA1lnlznXUlp6DwL15WMcwW1/Vvjrsh6Mzwv2WbNndDBI8 ec9Q== X-Gm-Message-State: APjAAAUU7/32iagibW06dUuXA8YMnVDFcBgoOIJlAorVqVqaEYNqO0kJ QpToEjzxEadjNDu6Sz4E484ZEld+xsCaLuUetw== X-Received: by 2002:a6b:4107:: with SMTP id n7mr3534566ioa.12.1560525869521; Fri, 14 Jun 2019 08:24:29 -0700 (PDT) MIME-Version: 1.0 References: <1560422702-11403-1-git-send-email-kernelfans@gmail.com> <1560422702-11403-3-git-send-email-kernelfans@gmail.com> <20190613213915.GE32404@iweiny-DESK2.sc.intel.com> In-Reply-To: <20190613213915.GE32404@iweiny-DESK2.sc.intel.com> From: Pingfan Liu Date: Fri, 14 Jun 2019 23:24:18 +0800 Message-ID: Subject: Re: [PATCHv4 2/3] mm/gup: fix omission of check on FOLL_LONGTERM in gup fast path To: Ira Weiny Cc: linux-mm@kvack.org, Andrew Morton , Mike Rapoport , Dan Williams , Matthew Wilcox , John Hubbard , "Aneesh Kumar K.V" , Keith Busch , Christoph Hellwig , Shuah Khan , LKML , mike.kravetz@oracle.com, David Rientjes 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 Cc Mike, David, who is an expert of hugetlb and thp On Fri, Jun 14, 2019 at 5:37 AM Ira Weiny wrote: > > On Thu, Jun 13, 2019 at 06:45:01PM +0800, Pingfan Liu wrote: > > FOLL_LONGTERM suggests a pin which is going to be given to hardware and > > can't move. It would truncate CMA permanently and should be excluded. > > > > FOLL_LONGTERM has already been checked in the slow path, but not checked in > > the fast path, which means a possible leak of CMA page to longterm pinned > > requirement through this crack. > > > > Place a check in gup_pte_range() in the fast path. > > > > Signed-off-by: Pingfan Liu > > Cc: Ira Weiny > > Cc: Andrew Morton > > Cc: Mike Rapoport > > Cc: Dan Williams > > Cc: Matthew Wilcox > > Cc: John Hubbard > > Cc: "Aneesh Kumar K.V" > > Cc: Keith Busch > > Cc: Christoph Hellwig > > Cc: Shuah Khan > > Cc: linux-kernel@vger.kernel.org > > --- > > mm/gup.c | 26 ++++++++++++++++++++++++++ > > 1 file changed, 26 insertions(+) > > > > diff --git a/mm/gup.c b/mm/gup.c > > index 766ae54..de1b03f 100644 > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -1757,6 +1757,14 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > > VM_BUG_ON(!pfn_valid(pte_pfn(pte))); > > page = pte_page(pte); > > > > + /* > > + * FOLL_LONGTERM suggests a pin given to hardware. Prevent it > > + * from truncating CMA area > > + */ > > + if (unlikely(flags & FOLL_LONGTERM) && > > + is_migrate_cma_page(page)) > > + goto pte_unmap; > > + > > head = try_get_compound_head(page, 1); > > if (!head) > > goto pte_unmap; > > @@ -1900,6 +1908,12 @@ static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr, > > refs++; > > } while (addr += PAGE_SIZE, addr != end); > > > > + if (unlikely(flags & FOLL_LONGTERM) && > > + is_migrate_cma_page(page)) { > > + *nr -= refs; > > + return 0; > > + } > > + > > Why can't we place this check before the while loop and skip subtracting the > page count? Yes, that will be better. > > Can is_migrate_cma_page() operate on any "subpage" of a compound page? For gigantic page, __alloc_gigantic_page() allocate from MIGRATE_MOVABLE pageblock. For page order < MAX_ORDER, pages are allocated from either free_list[MIGRATE_MOVABLE] or free_list[MIGRATE_CMA]. So all subpage have the same migrate type. Thanks, Pingfan > > Here this calls is_magrate_cma_page() on the tail page of the compound page. > > I'm not an expert on compound pages nor cma handling so is this ok? > > It seems like you need to call is_migrate_cma_page() on each page within the > while loop? > > > head = try_get_compound_head(pmd_page(orig), refs); > > if (!head) { > > *nr -= refs; > > @@ -1941,6 +1955,12 @@ static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr, > > refs++; > > } while (addr += PAGE_SIZE, addr != end); > > > > + if (unlikely(flags & FOLL_LONGTERM) && > > + is_migrate_cma_page(page)) { > > + *nr -= refs; > > + return 0; > > + } > > + > > Same comment here. > > > head = try_get_compound_head(pud_page(orig), refs); > > if (!head) { > > *nr -= refs; > > @@ -1978,6 +1998,12 @@ static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr, > > refs++; > > } while (addr += PAGE_SIZE, addr != end); > > > > + if (unlikely(flags & FOLL_LONGTERM) && > > + is_migrate_cma_page(page)) { > > + *nr -= refs; > > + return 0; > > + } > > + > > And here. > > Ira > > > head = try_get_compound_head(pgd_page(orig), refs); > > if (!head) { > > *nr -= refs; > > -- > > 2.7.5 > >