Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7499244ybi; Wed, 5 Jun 2019 19:22:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxgPRWFRgmxk5cINH79S4uEq3bSpv7LTCqdJ/W0zYwnyIYFlcKDKWThleb1pM8gbSgH2R73 X-Received: by 2002:a17:902:e6:: with SMTP id a93mr5348583pla.175.1559787760568; Wed, 05 Jun 2019 19:22:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559787760; cv=none; d=google.com; s=arc-20160816; b=zidvuxyC5rxlTr80SYw7yaUu4+z538yUILqkPP8M/r78RnJsOHMF9qzWMcvuugrCwM x8zR/NhxXclahe14xK/JLmS66Wn1ARK2PAfhA84/3UkV2H3QvYeePjtTPxQQo195fHsK 1/qXMyoVCYjTBphOvaUuypVH8AO6D7RMQZAECmPsHaYNWnsuu/5HEsbAdKTItOOIVeOL T/DL2eGUqTBb0Bs6AnOc/r81WWm8rH5zQisYA2OszngF2axYcWFbrTWR8m9+9lWrSnEW 4f3bGBT8b1+B++yIlqxl4SXFdIWM6zjqkFCqzx6Scl3Xs/gN2a0EyD47CSPZCVm/n18W wllQ== 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=DTWH24yA/1C8wqtT9jqxaOXK4VdWlElUekSQr6wCUNk=; b=HlOWtQuGoKV+8BwtAFhJDccvB/Ouu0Ygu2270HfbKmDWf59+XvbE+eXV3FBriMACyo ++ZM5/pdWNL9Mq7PnzuVRLaoei7Qj9+mc3IgJd9RPz/PEUSV2VhicP1TPlIP9Q2Bv5qj evWDp/Jt9pdz5cDMOjv5eH3MEKZaYb0069Jo2v3pnee1DLfkW09GObd5/Bhd8SBnEfqA LYwtuKyKpY5yoFLZIG0vUT2vSHr7u/R+PGOpPqoLs7Odm3qVC6UdQvnVMjGGwiyLnNA0 ODrxl/oluVrlSxWET7d8meG43yz2Mll+dcCvVq8e7FZV6W9/HLQy1c9+A1ZJ81ppmCvf 7Agg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kmgXYItR; 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 m15si575073pgl.300.2019.06.05.19.22.23; Wed, 05 Jun 2019 19:22:40 -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=kmgXYItR; 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 S1726697AbfFFCTu (ORCPT + 99 others); Wed, 5 Jun 2019 22:19:50 -0400 Received: from mail-io1-f67.google.com ([209.85.166.67]:40534 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726589AbfFFCTu (ORCPT ); Wed, 5 Jun 2019 22:19:50 -0400 Received: by mail-io1-f67.google.com with SMTP id n5so99669ioc.7 for ; Wed, 05 Jun 2019 19:19:50 -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=DTWH24yA/1C8wqtT9jqxaOXK4VdWlElUekSQr6wCUNk=; b=kmgXYItRTWz7KU83m4f4kQdWt8EQlqNIcHOHwnh10wNa68tCz1p6bokonrDgngqEr+ FRMLMaIy/lj5RAYS2Z0jJjnURXbh17DPBk341t6COKHH3QlsLfEvtoqXP1NllVRwEicd 1SsU9xldKIqlRNVu1j1kw49OJgqIolMCZuxAzit5j6NdewERw7I9rjzHfEFc8Lk8ntwd XI1GCa7jtok3tNC+8zLVW7bL2tYjpPP1Md3foyuun58BQ32SuG+j0DtX8LC5sVbYHAdL p4VPKF4pCtFpj9AQ8swn3mpHQ2Ka0oQpGEeWUHC4RfvzPbvsg7Hioj3HcfIul55kE4sU XWTg== 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=DTWH24yA/1C8wqtT9jqxaOXK4VdWlElUekSQr6wCUNk=; b=Kkzi/XNcAWHBjNZzVXWHbZQRdBzGG/2NYSNu/ODdMZMD0jQxnsoC5N1RAhTMnItYa2 +1NO4nZ2BZyPuMZ6PYcQfnJ0Btc+dFheWleZWqDW2VDJ6QqHaH2oNcVY1KcLmc0uvI7R 3o8jt9sTVRsk2QZ1pBQ2CVrfBpxYf7x+wus+NsPbxz5iM609WhUf8XJsdzOiWPHChqVF OUOO8skJZfp6Dfi/uY8hMjxfaU7JbQCwrupH7wGCjkODeko9x4F/LiHu5MT85CZ/xMK6 wG3RQuq7RCFxQWTv5xy1ZY+2DHuw04LT6gjNcBrYA/PSwx0bBw3k1rZYZ29BPI36Xa8R 2ZjQ== X-Gm-Message-State: APjAAAXCe3bh6VVmNod0MfAFk833+2aEHyuPwq+pmLTamPAcAs/RlAqM 2CPpnOQa94QA6Od58wVVyQwt3FCylJa/KeEJGw== X-Received: by 2002:a6b:5106:: with SMTP id f6mr15331128iob.15.1559787589673; Wed, 05 Jun 2019 19:19:49 -0700 (PDT) MIME-Version: 1.0 References: <1559725820-26138-1-git-send-email-kernelfans@gmail.com> <20190605144912.f0059d4bd13c563ddb37877e@linux-foundation.org> In-Reply-To: <20190605144912.f0059d4bd13c563ddb37877e@linux-foundation.org> From: Pingfan Liu Date: Thu, 6 Jun 2019 10:19:38 +0800 Message-ID: Subject: Re: [PATCHv3 1/2] mm/gup: fix omission of check on FOLL_LONGTERM in get_user_pages_fast() To: Andrew Morton Cc: linux-mm@kvack.org, Ira Weiny , Mike Rapoport , Dan Williams , Matthew Wilcox , John Hubbard , "Aneesh Kumar K.V" , Keith Busch , Christoph Hellwig , LKML 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 Thu, Jun 6, 2019 at 5:49 AM Andrew Morton wrote: > > On Wed, 5 Jun 2019 17:10:19 +0800 Pingfan Liu wrote: > > > As for FOLL_LONGTERM, it is checked in the slow path > > __gup_longterm_unlocked(). But it is 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 the fast path. > > I'm not actually seeing a description (in either the existing code or > this changelog or patch) an explanation of *why* we wish to exclude CMA > pages from longterm pinning. > What about a short description like this: 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. > > --- a/mm/gup.c > > +++ b/mm/gup.c > > @@ -2196,6 +2196,26 @@ static int __gup_longterm_unlocked(unsigned long start, int nr_pages, > > return ret; > > } > > > > +#ifdef CONFIG_CMA > > +static inline int reject_cma_pages(int nr_pinned, struct page **pages) > > +{ > > + int i; > > + > > + for (i = 0; i < nr_pinned; i++) > > + if (is_migrate_cma_page(pages[i])) { > > + put_user_pages(pages + i, nr_pinned - i); > > + return i; > > + } > > + > > + return nr_pinned; > > +} > > There's no point in inlining this. OK, will drop it in V4. > > The code seems inefficient. If it encounters a single CMA page it can > end up discarding a possibly significant number of non-CMA pages. I The trick is the page is not be discarded, in fact, they are still be referrenced by pte. We just leave the slow path to pick up the non-CMA pages again. > guess that doesn't matter much, as get_user_pages(FOLL_LONGTERM) is > rare. But could we avoid this (and the second pass across pages[]) by > checking for a CMA page within gup_pte_range()? It will spread the same logic to hugetlb pte and normal pte. And no improvement in performance due to slow path. So I think maybe it is not worth. > > > +#else > > +static inline int reject_cma_pages(int nr_pinned, struct page **pages) > > +{ > > + return nr_pinned; > > +} > > +#endif > > + > > /** > > * get_user_pages_fast() - pin user pages in memory > > * @start: starting user address > > @@ -2236,6 +2256,9 @@ int get_user_pages_fast(unsigned long start, int nr_pages, > > ret = nr; > > } > > > > + if (unlikely(gup_flags & FOLL_LONGTERM) && nr) > > + nr = reject_cma_pages(nr, pages); > > + > > This would be a suitable place to add a comment explaining why we're > doing this... Would add one comment "FOLL_LONGTERM suggests a pin given to hardware and rarely returned." Thanks for your kind review. Regards, Pingfan