Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp643149pxa; Wed, 19 Aug 2020 10:48:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqwgIcZ/lQ8IJ1nhGL+VlrR1CLsIxWiKovLnCxJibBS0Ca4SQPZa/myHkX0Fv6/9UfQ9jA X-Received: by 2002:a17:906:328d:: with SMTP id 13mr27140229ejw.71.1597859300984; Wed, 19 Aug 2020 10:48:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597859300; cv=none; d=google.com; s=arc-20160816; b=hYe29sCst9VLcnN2Dy5xLIvLRnjoBMecDJiG6lk+W2AHmAhDqhOqtXz+hAs3a6kniP 2N6DFGm4uHVG3Ilc4mqPD1cnHujZzRojHGy4GZ9AstkXHR+l3K527UC1vjSYOWSrHcl+ I7E7Uu/By1Jmqq/cacbeWJ7SrfX5aLmooYDPM661D3uq8nRhkvvds5VFtaMP/h23T/X3 3pkwY6wlmRmsr8ADrIE1lLh8fIXbzMJayPl30p3HyFKtkwKMZr7gzzC5Ze3ORhoUY9nD MAeGdYBdlRGvf+BCuhpwONTYEYv9Fn0Kq2ERVixadVxWXhUC+4MXE+LC/BP6kDW+dagK xyig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:ironport-sdr :ironport-sdr; bh=DhnE/AkV/e7/zqSFCuG/Y3kjOM7SQUYoyN1Bo0VJb4s=; b=q6Fy8xciCnzMeAdlYxiOWqJYbB7Jo6fQ92Db1lW4tv9n8WOi9UqoMF+wDh+ZkF/0V/ yPtBXnQemchotD0qPkNMYMZWtG2ukBKYVJg3R1lQVYNrlTDcasYpRIbGK8V4t/kq4l8d yONBIS0OKyBzcGruT1phUSM2IAhxZzrXpucp6OfcZY5eqs70RnyRzC36B09iiSvRDKjj IzVUX+kNmWPAMt382k0pO2iXhDzd9gB/So6cL26k4QDrqbCkjB2xVnwf2KbIvS3m4L0E oejBmRda6OhCOJUXmuVbAY2Q8FvtOYqZMJbGpXqqU+HX5lztspnmkkPvdc6BTOPe5/pp kWtA== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p22si15878897eds.169.2020.08.19.10.47.56; Wed, 19 Aug 2020 10:48:20 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726698AbgHSRo4 (ORCPT + 99 others); Wed, 19 Aug 2020 13:44:56 -0400 Received: from mga02.intel.com ([134.134.136.20]:39258 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725804AbgHSRoy (ORCPT ); Wed, 19 Aug 2020 13:44:54 -0400 IronPort-SDR: gqJnFc+apa+36RHMbFsCMg/QYWjtcBONZlB4fTbz5TpUWLH5wAHGEOMUr8Jog8Bs4gXLAAWFcl DLEo723cOH5A== X-IronPort-AV: E=McAfee;i="6000,8403,9718"; a="142984162" X-IronPort-AV: E=Sophos;i="5.76,332,1592895600"; d="scan'208";a="142984162" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2020 10:44:53 -0700 IronPort-SDR: lvWAKSOz/gYMlM8RHyPflbusPtjx/GMfDwuX4T8/btt5FTlvvXnv7z4AlyTB542e7y+X1acUpf Aee+KmKw5f4Q== X-IronPort-AV: E=Sophos;i="5.76,332,1592895600"; d="scan'208";a="472320725" Received: from iweiny-desk2.sc.intel.com (HELO localhost) ([10.3.52.147]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Aug 2020 10:44:53 -0700 Date: Wed, 19 Aug 2020 10:44:53 -0700 From: Ira Weiny To: Barry Song Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linuxarm@huawei.com, John Hubbard , Jan Kara , =?iso-8859-1?B?Suly9G1l?= Glisse , "Matthew Wilcox (Oracle)" , Al Viro , Christoph Hellwig , Dan Williams , Dave Chinner , Jason Gunthorpe , Jonathan Corbet , Michal Hocko , Mike Kravetz , Shuah Khan , Vlastimil Babka Subject: Re: [PATCH] mm/gup: don't permit users to call get_user_pages with FOLL_LONGTERM Message-ID: <20200819174452.GA104589@iweiny-DESK2.sc.intel.com> References: <20200819110100.23504-1-song.bao.hua@hisilicon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200819110100.23504-1-song.bao.hua@hisilicon.com> User-Agent: Mutt/1.11.1 (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 19, 2020 at 11:01:00PM +1200, Barry Song wrote: > gug prohibits users from calling get_user_pages() with FOLL_PIN. But it > allows users to call get_user_pages() with FOLL_LONGTERM only. It seems > insensible. > > since FOLL_LONGTERM is a stricter case of FOLL_PIN, we should prohibit > users from calling get_user_pages() with FOLL_LONGTERM while not with > FOLL_PIN. > > mm/gup_benchmark.c used to be the only user who did this improperly. > But it has been fixed by moving to use pin_user_pages(). > > Cc: John Hubbard > Cc: Jan Kara > Cc: J?r?me Glisse > Cc: "Matthew Wilcox (Oracle)" > Cc: Al Viro > Cc: Christoph Hellwig > Cc: Dan Williams > Cc: Dave Chinner > Cc: Jason Gunthorpe > Cc: Jonathan Corbet > Cc: Michal Hocko > Cc: Mike Kravetz > Cc: Shuah Khan > Cc: Vlastimil Babka > Signed-off-by: Barry Song Seems reasonable to me. Reviewed-by: Ira Weiny > --- > mm/gup.c | 37 ++++++++++++++++++++++--------------- > 1 file changed, 22 insertions(+), 15 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index ae096ea7583f..4da669f79566 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1789,6 +1789,25 @@ static long __get_user_pages_remote(struct mm_struct *mm, > gup_flags | FOLL_TOUCH | FOLL_REMOTE); > } > > +static bool is_valid_gup_flags(unsigned int gup_flags) > +{ > + /* > + * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, > + * never directly by the caller, so enforce that with an assertion: > + */ > + if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) > + return false; > + /* > + * FOLL_PIN is a prerequisite to FOLL_LONGTERM. Another way of saying > + * that is, FOLL_LONGTERM is a specific case, more restrictive case of > + * FOLL_PIN. > + */ > + if (WARN_ON_ONCE(gup_flags & FOLL_LONGTERM)) > + return false; > + > + return true; > +} > + > /** > * get_user_pages_remote() - pin user pages in memory > * @mm: mm_struct of target mm > @@ -1854,11 +1873,7 @@ long get_user_pages_remote(struct mm_struct *mm, > unsigned int gup_flags, struct page **pages, > struct vm_area_struct **vmas, int *locked) > { > - /* > - * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, > - * never directly by the caller, so enforce that with an assertion: > - */ > - if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) > + if (!is_valid_gup_flags(gup_flags)) > return -EINVAL; > > return __get_user_pages_remote(mm, start, nr_pages, gup_flags, > @@ -1904,11 +1919,7 @@ long get_user_pages(unsigned long start, unsigned long nr_pages, > unsigned int gup_flags, struct page **pages, > struct vm_area_struct **vmas) > { > - /* > - * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, > - * never directly by the caller, so enforce that with an assertion: > - */ > - if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) > + if (!is_valid_gup_flags(gup_flags)) > return -EINVAL; > > return __gup_longterm_locked(current->mm, start, nr_pages, > @@ -2810,11 +2821,7 @@ EXPORT_SYMBOL_GPL(get_user_pages_fast_only); > int get_user_pages_fast(unsigned long start, int nr_pages, > unsigned int gup_flags, struct page **pages) > { > - /* > - * FOLL_PIN must only be set internally by the pin_user_pages*() APIs, > - * never directly by the caller, so enforce that: > - */ > - if (WARN_ON_ONCE(gup_flags & FOLL_PIN)) > + if (!is_valid_gup_flags(gup_flags)) > return -EINVAL; > > /* > -- > 2.27.0 > > >