From patchwork Wed Aug 19 11:01:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Song Bao Hua (Barry Song)" X-Patchwork-Id: 11723521 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4758614F6 for ; Wed, 19 Aug 2020 11:07:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1A57E207BB for ; Wed, 19 Aug 2020 11:07:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1A57E207BB Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 16DCD6B0070; Wed, 19 Aug 2020 07:07:14 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0D1526B0072; Wed, 19 Aug 2020 07:07:14 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8AF26B0073; Wed, 19 Aug 2020 07:07:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0083.hostedemail.com [216.40.44.83]) by kanga.kvack.org (Postfix) with ESMTP id CC7E36B0070 for ; Wed, 19 Aug 2020 07:07:13 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 83F4A824934B for ; Wed, 19 Aug 2020 11:07:13 +0000 (UTC) X-FDA: 77167041546.02.game31_031841627027 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 55F3510097AA0 for ; Wed, 19 Aug 2020 11:07:13 +0000 (UTC) X-Spam-Summary: 1,0,0,4ba28dd0596fdd0a,d41d8cd98f00b204,song.bao.hua@hisilicon.com,,RULES_HIT:41:69:152:355:379:541:582:800:960:973:988:989:1152:1260:1261:1277:1311:1313:1314:1345:1437:1515:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2198:2199:2393:2553:2559:2562:3138:3139:3140:3141:3142:3355:3865:3866:3867:3870:3871:3872:3874:4321:4605:5007:6261:6742:7903:8660:10004:10400:11026:11473:11658:11914:12043:12291:12296:12297:12438:12555:12683:12895:12986:13148:13230:13894:14096:14097:14181:14394:14659:14721:21080:21212:21251:21451:21627:21795:21939:21990:30051:30054:30064:30090,0,RBL:45.249.212.191:@hisilicon.com:.lbl8.mailshell.net-62.14.2.100 64.201.201.201;04y8bz9bphrpeaqx7b4miewdhrh57yc4oropnqgirio3wdoutdudij5zax39r6j.kxhdaechgddmydgzi5a7s937efqnz8jkxn8suhiyek1sjncy9f99jwbzmw7mbui.e-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: game31_031841627027 X-Filterd-Recvd-Size: 5205 Received: from huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Wed, 19 Aug 2020 11:07:12 +0000 (UTC) Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 3B91A56D07EEA3C7EE08; Wed, 19 Aug 2020 19:04:01 +0800 (CST) Received: from SWX921481.china.huawei.com (10.126.200.153) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.487.0; Wed, 19 Aug 2020 19:03:49 +0800 From: Barry Song To: , , CC: , Barry Song , "John Hubbard" , Jan Kara , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , "Matthew Wilcox (Oracle)" , Al Viro , "Christoph Hellwig" , Dan Williams , "Dave Chinner" , Jason Gunthorpe , "Jonathan Corbet" , Michal Hocko , Mike Kravetz , Shuah Khan , Vlastimil Babka Subject: [PATCH] mm/gup: don't permit users to call get_user_pages with FOLL_LONGTERM Date: Wed, 19 Aug 2020 23:01:00 +1200 Message-ID: <20200819110100.23504-1-song.bao.hua@hisilicon.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.126.200.153] X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 55F3510097AA0 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 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; /*