From patchwork Sun Mar 22 11:32:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pingfan Liu X-Patchwork-Id: 11451639 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 1903E1668 for ; Sun, 22 Mar 2020 11:34:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D9935206C3 for ; Sun, 22 Mar 2020 11:34:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B09uNYE3" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D9935206C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0D0AC6B0006; Sun, 22 Mar 2020 07:34:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 081826B0007; Sun, 22 Mar 2020 07:34:42 -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 ED88B6B0008; Sun, 22 Mar 2020 07:34:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0144.hostedemail.com [216.40.44.144]) by kanga.kvack.org (Postfix) with ESMTP id D5D546B0006 for ; Sun, 22 Mar 2020 07:34:41 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9F507181AEF09 for ; Sun, 22 Mar 2020 11:34:41 +0000 (UTC) X-FDA: 76622790762.20.park84_8040f73885d11 X-Spam-Summary: 2,0,0,023d887529c044fa,d41d8cd98f00b204,kernelfans@gmail.com,,RULES_HIT:41:69:355:379:541:800:960:973:988:989:1260:1345:1359:1431:1437:1535:1542:1711:1730:1747:1777:1792:2393:2559:2562:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3871:4117:4321:4605:5007:6119:6261:6653:6742:7514:7901:7903:9413:10004:11026:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12895:13846:14096:14181:14394:14687:14721:21080:21444:21451:21627:21666:21990:30012:30045:30054:30064,0,RBL:209.85.216.65:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.50.0.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: park84_8040f73885d11 X-Filterd-Recvd-Size: 6088 Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by imf19.hostedemail.com (Postfix) with ESMTP for ; Sun, 22 Mar 2020 11:34:41 +0000 (UTC) Received: by mail-pj1-f65.google.com with SMTP id g9so608235pjp.0 for ; Sun, 22 Mar 2020 04:34:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xWoNlFcF04oMTTvMbHBq5MIT/9eTXh290wY6htrAIuI=; b=B09uNYE3YLv9AUjIpSkhK9RLo1vpui/8HJtBFrQRwJytPlHEfd+gH9dEuhggII+Iew FtfkGoevnyc3b/xFGOIfHowiEK/PkSuEylcdNlvBkgjwwpWKnFiP09E/H43f9M0/HmCE 2E92oKmD/v5FmM+jfbsdvdbDp7d8gTGtpD+oxRHvWBfCS4re2l8TkuDksKLaPz4XelZ8 zbL00C3XSyROMjSTUPc1c3yzfGzxZCbsuldi58WuwreDsJljc44YZrqCpN2fVqSpKHcV 8FXS3M7hHYvdqoWec6ZiVyVCULlpZR36AIp4DFnq3++gCRVtu4ALtVbn+enIGEQlUbV2 XS4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=xWoNlFcF04oMTTvMbHBq5MIT/9eTXh290wY6htrAIuI=; b=QuuZlnXBB6ZyMlqZxkWOvGEzFlQmkjLbogPBDgNrEFZQX+CQsU/m7OL7ZslK4MD12U oVesvjZCQyaz4qPfH/VRj/iLKcVC8Y4YxiYefN+5yUZpuaLeHffYOZuwByCaBzcFb2Z8 e+G7/EEy2F4vbE9M+3FX6PEc1Hm/tZnqUr91y3QInnXdbA+Db7KiEexx9YO4aSj1colf HCzY9X3MIV9p/IjvO5Mt34zUfLIDqrRuBpX56F/4YLNtZ7xYZdVg9Yn0/R4/0fmskHPk mgw9iuLYNk0i4uUOM6EcE7F09utxPcAy6yl3nDD+xl47uakIC8mmkOV/SxzKLWUghsuZ f9Wg== X-Gm-Message-State: ANhLgQ1lKEaHYLIY8F+hnZMtaWf3G2BJYGZniyxyG4lrvC4WlFObG8Iv Xnvx4HwXv9aQn+0ptpaZMtJSUeA= X-Google-Smtp-Source: ADFU+vuBbHVBMZ9Vibcd72WJpS7tH/vUS4fAO297iYtCc/GotCgFfnIIeUnW/cf7GDw8qlaRmAyAbg== X-Received: by 2002:a17:902:6acc:: with SMTP id i12mr16839795plt.87.1584876880017; Sun, 22 Mar 2020 04:34:40 -0700 (PDT) Received: from mylaptop.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id i23sm10474445pfq.157.2020.03.22.04.34.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Mar 2020 04:34:39 -0700 (PDT) From: Pingfan Liu To: linux-mm@kvack.org Cc: Pingfan Liu , Ira Weiny , Andrew Morton , Mike Rapoport , Dan Williams , Matthew Wilcox , "Aneesh Kumar K.V" , Christoph Hellwig , Shuah Khan , Jason Gunthorpe , linux-kernel@vger.kernel.org Subject: [PATCHv8 1/2] mm/gup: rename nr as nr_pinned in get_user_pages_fast() Date: Sun, 22 Mar 2020 19:32:12 +0800 Message-Id: <1584876733-17405-2-git-send-email-kernelfans@gmail.com> X-Mailer: git-send-email 2.7.5 In-Reply-To: <1584876733-17405-1-git-send-email-kernelfans@gmail.com> References: <1584876733-17405-1-git-send-email-kernelfans@gmail.com> 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: To better reflect the held state of pages and make code self-explaining, rename nr as nr_pinned. Signed-off-by: Pingfan Liu Reviewed-by: John Hubbard Cc: Ira Weiny Cc: Andrew Morton Cc: Mike Rapoport Cc: Dan Williams Cc: Matthew Wilcox Cc: "Aneesh Kumar K.V" Cc: Christoph Hellwig Cc: Shuah Khan Cc: Jason Gunthorpe To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/gup.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) -- 2.7.5 diff --git a/mm/gup.c b/mm/gup.c index e8aaa40..02a95b1 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2665,7 +2665,7 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write, { unsigned long len, end; unsigned long flags; - int nr = 0; + int nr_pinned = 0; /* * Internally (within mm/gup.c), gup fast variants must set FOLL_GET, * because gup fast is always a "pin with a +1 page refcount" request. @@ -2699,11 +2699,11 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write, if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && gup_fast_permitted(start, end)) { local_irq_save(flags); - gup_pgd_range(start, end, gup_flags, pages, &nr); + gup_pgd_range(start, end, gup_flags, pages, &nr_pinned); local_irq_restore(flags); } - return nr; + return nr_pinned; } EXPORT_SYMBOL_GPL(__get_user_pages_fast); @@ -2735,7 +2735,7 @@ static int internal_get_user_pages_fast(unsigned long start, int nr_pages, struct page **pages) { unsigned long addr, len, end; - int nr = 0, ret = 0; + int nr_pinned = 0, ret = 0; if (WARN_ON_ONCE(gup_flags & ~(FOLL_WRITE | FOLL_LONGTERM | FOLL_FORCE | FOLL_PIN | FOLL_GET))) @@ -2754,25 +2754,25 @@ static int internal_get_user_pages_fast(unsigned long start, int nr_pages, if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && gup_fast_permitted(start, end)) { local_irq_disable(); - gup_pgd_range(addr, end, gup_flags, pages, &nr); + gup_pgd_range(addr, end, gup_flags, pages, &nr_pinned); local_irq_enable(); - ret = nr; + ret = nr_pinned; } - if (nr < nr_pages) { + if (nr_pinned < nr_pages) { /* Try to get the remaining pages with get_user_pages */ - start += nr << PAGE_SHIFT; - pages += nr; + start += nr_pinned << PAGE_SHIFT; + pages += nr_pinned; - ret = __gup_longterm_unlocked(start, nr_pages - nr, + ret = __gup_longterm_unlocked(start, nr_pages - nr_pinned, gup_flags, pages); /* Have to be a bit careful with return values */ - if (nr > 0) { + if (nr_pinned > 0) { if (ret < 0) - ret = nr; + ret = nr_pinned; else - ret += nr; + ret += nr_pinned; } } From patchwork Sun Mar 22 11:32:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pingfan Liu X-Patchwork-Id: 11451641 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 538DE1667 for ; Sun, 22 Mar 2020 11:34:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1F80120719 for ; Sun, 22 Mar 2020 11:34:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZCgbAm/i" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1F80120719 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4EF4E6B0007; Sun, 22 Mar 2020 07:34:48 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 49FC06B0008; Sun, 22 Mar 2020 07:34:48 -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 38ECE6B000A; Sun, 22 Mar 2020 07:34:48 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0021.hostedemail.com [216.40.44.21]) by kanga.kvack.org (Postfix) with ESMTP id 232A36B0007 for ; Sun, 22 Mar 2020 07:34:48 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 051A4180AD815 for ; Sun, 22 Mar 2020 11:34:48 +0000 (UTC) X-FDA: 76622791056.18.trip22_81295d8fe2835 X-Spam-Summary: 2,0,0,86593e5f2f82132d,d41d8cd98f00b204,kernelfans@gmail.com,,RULES_HIT:41:355:379:541:800:960:966:973:988:989:1260:1345:1359:1431:1437:1535:1542:1711:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2731:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3872:4250:4321:4385:5007:6261:6653:6742:7514:9413:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:13846:14096:14181:14394:14687:14721:21080:21251:21444:21451:21627:21666:21990:30054:30064:30070,0,RBL:209.85.215.195:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.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: trip22_81295d8fe2835 X-Filterd-Recvd-Size: 5355 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Sun, 22 Mar 2020 11:34:47 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id a32so5645244pga.4 for ; Sun, 22 Mar 2020 04:34:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/1SM3867y5SxnAk5W+nCWfh5uFmnZCup8fknvMevl8I=; b=ZCgbAm/iPixhzfGPOch7scTLnnbNH0Ce/QnnMs3JENoqXF8vBILnIV1dIn7EILxnTk /WOGRkxPYmqz+eb7gUU0jx0g3JN8FGy49bJdoGQBqCSMcXdfjO3i04D/V9LGkeJCqUZo HR4ISjeBbOCKuX9PYVLMInFsw7kMt4iiHEy/MMyi5n3uOlYSsTlnEhnhkI2p6iyLpeD1 k6zkgSjySq8VK3DAd2P7+Q+HwhzrhwqE1eDX1Unf32kwsA++x/a9IizdDHpyMBje1kc1 cv5Xhub/coe6nHFJOa7R2yacoJlYTd3Ac/BbvT7UZbPZ/FSPEUEZv3nxM0ZffXwy6APt UeSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/1SM3867y5SxnAk5W+nCWfh5uFmnZCup8fknvMevl8I=; b=MNezWXjCkMe7zPdk0vhDzEyYAgQorZyFMFgzRw5KfAHtJDfwm437ZAd+tH4pexz+8b UWU5WGGBrypP7YOmZaP6+1D/VwM9IkwPcrolJXjvO2XUTyO1ceHcAyLvGBjxVwG0CwYk L3HaddbzZjWrRlikDbbVA44q+Cl2aRtX/zmxZknUdhflJr7zP99D1MiYRfMvioOB8xe5 bvtlLiDBsPYGRf8p6j4UD+3PzibgAD/n4w/0jIXlGe/OLUEO93JG0HbsrPjkW61FN/if YIfIckJcPwh9L7/OQuBHM2B5PUY4jhgHoRNfmUBwPiRj3oEg0JtLfrIrkUfuGsLoTLxU qoOg== X-Gm-Message-State: ANhLgQ1QrrvUP5h2l8t3FbGRWzx9cgr92ddsVQB+YvB96pMyClLBdnEy LfH0bIL0RHSpBJ2Z7QhMsJKb39Y= X-Google-Smtp-Source: ADFU+vt2Orqn/hOvqJdCrhDZmh7HfFtpy6+z9jOFDkLUvT9xMsriKqGJktpP2gjGP7sprCZLp7doWQ== X-Received: by 2002:aa7:8bda:: with SMTP id s26mr18467747pfd.142.1584876886209; Sun, 22 Mar 2020 04:34:46 -0700 (PDT) Received: from mylaptop.redhat.com ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id i23sm10474445pfq.157.2020.03.22.04.34.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Mar 2020 04:34:45 -0700 (PDT) From: Pingfan Liu To: linux-mm@kvack.org Cc: Pingfan Liu , Ira Weiny , Andrew Morton , Mike Rapoport , Dan Williams , Matthew Wilcox , John Hubbard , "Aneesh Kumar K.V" , Christoph Hellwig , Shuah Khan , Jason Gunthorpe , linux-kernel@vger.kernel.org Subject: [PATCHv8 2/2] mm/gup: fix omission of check on FOLL_LONGTERM in gup fast path Date: Sun, 22 Mar 2020 19:32:13 +0800 Message-Id: <1584876733-17405-3-git-send-email-kernelfans@gmail.com> X-Mailer: git-send-email 2.7.5 In-Reply-To: <1584876733-17405-1-git-send-email-kernelfans@gmail.com> References: <1584876733-17405-1-git-send-email-kernelfans@gmail.com> 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: FOLL_LONGTERM is a special case of FOLL_PIN. It suggests a pin which is going to be given to hardware and can't move. It would truncate CMA permanently and should be excluded. In gup slow path, where __gup_longterm_locked->check_and_migrate_cma_pages() handles FOLL_LONGTERM, but in fast path, there lacks such a check, which means a possible leak of CMA page to longterm pinned. Place a check in try_grab_compound_head() in the fast path to fix the leak, and if FOLL_LONGTERM happens on CMA, it will fall back to slow path to migrate the page. Some note about the check: Huge page's subpages have the same migrate type due to either allocation from a free_list[] or alloc_contig_range() with param MIGRATE_MOVABLE. So it is enough to check on a single subpage by is_migrate_cma_page(subpage) Signed-off-by: Pingfan Liu Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Cc: Ira Weiny Cc: Andrew Morton Cc: Mike Rapoport Cc: Dan Williams Cc: Matthew Wilcox Cc: John Hubbard Cc: "Aneesh Kumar K.V" Cc: Christoph Hellwig Cc: Shuah Khan Cc: Jason Gunthorpe To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Reviewed-by: John Hubbard --- mm/gup.c | 8 ++++++++ 1 file changed, 8 insertions(+) -- 2.7.5 diff --git a/mm/gup.c b/mm/gup.c index 02a95b1..3fe75c4 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -89,6 +89,14 @@ static __maybe_unused struct page *try_grab_compound_head(struct page *page, int orig_refs = refs; /* + * Can't do FOLL_LONGTERM + FOLL_PIN with CMA in the gup fast + * path, so fail and let the caller fall back to the slow path. + */ + if (unlikely(flags & FOLL_LONGTERM) && + is_migrate_cma_page(page)) + return NULL; + + /* * When pinning a compound page of order > 1 (which is what * hpage_pincount_available() checks for), use an exact count to * track it, via hpage_pincount_add/_sub().