From patchwork Mon Nov 2 18:19:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 11874865 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 3D1C0921 for ; Mon, 2 Nov 2020 18:20:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CFFD52224E for ; Mon, 2 Nov 2020 18:20:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="G1IpGZBw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CFFD52224E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nvidia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0AAE86B0068; Mon, 2 Nov 2020 13:20:08 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0837B6B006C; Mon, 2 Nov 2020 13:20:08 -0500 (EST) 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 F02116B006E; Mon, 2 Nov 2020 13:20:07 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0220.hostedemail.com [216.40.44.220]) by kanga.kvack.org (Postfix) with ESMTP id C41C66B0068 for ; Mon, 2 Nov 2020 13:20:07 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6CD46181AC9CC for ; Mon, 2 Nov 2020 18:20:07 +0000 (UTC) X-FDA: 77440292454.30.fuel78_3f0b0c8272b2 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 367DC180B3C8B for ; Mon, 2 Nov 2020 18:20:07 +0000 (UTC) X-Spam-Summary: 1,0,0,3cae0cf88505caa8,d41d8cd98f00b204,jgg@nvidia.com,,RULES_HIT:41:355:379:800:960:967:973:988:989:1260:1277:1311:1313:1314:1345:1431:1437:1513:1515:1516:1518:1521:1535:1542:1711:1730:1747:1777:1792:2393:2525:2559:2563:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4250:4321:4362:5007:6261:6653:7875:7901:9010:9025:10004:10400:11026:11473:11658:11914:12043:12114:12296:12297:12438:12519:12521:12555:12679:12895:13095:13161:13221:13229:13846:14181:14394:14721:21080:21220:21251:21433:21444:21451:21627:21990:30045:30054:30064:30070,0,RBL:216.228.121.143:@nvidia.com:.lbl8.mailshell.net-64.201.201.201 62.22.0.100;04yg6rp4ry19sqkdqd9wobgby6werocn5c6q1xe4unsbcmdma58cop5j9nncrmw.ubd9hnga49x9qu9156an7ob3yktkut4h5atng1pzdqrqfa3ekmtsfhms78krwkw.r-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0 ,MSF:not X-HE-Tag: fuel78_3f0b0c8272b2 X-Filterd-Recvd-Size: 6487 Received: from hqnvemgate24.nvidia.com (hqnvemgate24.nvidia.com [216.228.121.143]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Mon, 2 Nov 2020 18:20:05 +0000 (UTC) Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Mon, 02 Nov 2020 10:20:07 -0800 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL111.nvidia.com (172.20.187.18) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Mon, 2 Nov 2020 18:20:01 +0000 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (104.47.59.177) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Mon, 2 Nov 2020 18:20:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BYQLSvyFU99Y8xAcLIp3m7WUMaB1NF75kZxzMe79VtbngFXfCDp/uYNAcwK9cJu8j+uQV862TUfBbc8e6u/DtNEN/MHg6bag3xfiNW7Y+NDagkMF/onFIFAuPWCU4ExQGG6F5+VXB/nnspy/jhXkFIiEQoUrqM48pC1914R6Cdm/VEOG78FAffxZkJzcH1lGtUcaypIwmMxJb3ZSKsMGp1toX10HRcFnyj+CRGmjORVTTprNU9SmgyRJmMC1S4Q64C5qus+nOB4XftRdEy5/qBOsjVdvAEpNEvDwxixo2zazTE8ouYNbdkQAdRjz9RJU8dcuMqYWnr9v3UwFe3LvZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HhgU8jmRqYQPtfSzEI+ym8YojsY6Q7J2dVcQcnoeUps=; b=M+Xk6iWYu5vQSi+p12sNSgZHhCMlCky4cTzba0HtU5SMSW4eXLOIMWN1QJXFzR08rBt+MLWgtmB5fEyr6jZL7/XedDjRP+ELmC1vDDdSztNMwDM7Abi1vW2vrmipuL5UuQMhHvjjxbdWEarZCMJjvlP9cSkVALNYXN1uzuqYaomNkX/3zlHODUpsyHCJmwDZVgkwzNNXAo+lgnEOw/DPw1g3Kyo0X2+wTWX81YiB2ruIejhkNPYKQ1lPDneDQf/0F5kE36p6yWxRknvebAFTsBwzJ6AVe0XIKw7AUAzWkP0pDCEptIIEMZTcf8vqff/nin9dPwykWi6vLNDgprUbyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none Received: from DM6PR12MB3834.namprd12.prod.outlook.com (2603:10b6:5:14a::12) by DM6PR12MB4451.namprd12.prod.outlook.com (2603:10b6:5:2ab::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18; Mon, 2 Nov 2020 18:20:00 +0000 Received: from DM6PR12MB3834.namprd12.prod.outlook.com ([fe80::cdbe:f274:ad65:9a78]) by DM6PR12MB3834.namprd12.prod.outlook.com ([fe80::cdbe:f274:ad65:9a78%7]) with mapi id 15.20.3499.030; Mon, 2 Nov 2020 18:20:00 +0000 From: Jason Gunthorpe To: Andrew Morton , CC: Aneesh Kumar K.V , Dan Williams , Ira Weiny , John Hubbard Subject: [PATCH rc v2] mm/gup: use unpin_user_pages() in __gup_longterm_locked() Date: Mon, 2 Nov 2020 14:19:59 -0400 Message-ID: <0-v2-3ae7d9d162e2+2a7-gup_cma_fix_jgg@nvidia.com> X-ClientProxiedBy: BL0PR03CA0015.namprd03.prod.outlook.com (2603:10b6:208:2d::28) To DM6PR12MB3834.namprd12.prod.outlook.com (2603:10b6:5:14a::12) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from mlx.ziepe.ca (156.34.48.30) by BL0PR03CA0015.namprd03.prod.outlook.com (2603:10b6:208:2d::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3499.18 via Frontend Transport; Mon, 2 Nov 2020 18:20:00 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kZeQx-00FQft-4s; Mon, 02 Nov 2020 14:19:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1604341207; bh=0eRlbMhZvk42Pw0Cvl+o3SrxF4a5QVINUO0LWnW3fE8=; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:From:To: CC:Subject:Date:Message-ID:Content-Transfer-Encoding:Content-Type: X-ClientProxiedBy:MIME-Version: X-MS-Exchange-MessageSentRepresentingType; b=G1IpGZBwjJt55pN3hU79aIR44smNyamD5vgCN6HTsqcBpq8HRhVjsdq/bkFRsihAD kz3zKOkrAOWxosUKbBKy8/j8XzucYcIqMiE2LJ9Qm6+V2FAQCvk4mO0VOT9Iu/4Wls Nx/H6HJn2KUYMHbAmZQFk36E/Q0ZWD4keBkCvtFRN/1loQNURFFca5uoTzGt5kJyyM Cp2mc6+z9QmAy5OXv6p8r5w4QgbimsgtPbUWm+0iLkMTdNUw41gip/OgNIiehqCWJ5 iF4Txwwv/mdsMrmKzr4y3MckNFZhhGOu0sxkkc+kQ8ukeJnLrViGkMX1VnbL2onLpi xrj4Ae7OCfAcA== 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: When FOLL_PIN is passed to __get_user_pages() the page list must be put back using unpin_user_pages() otherwise the page pin reference persists in a corrupted state. There are two places in the unwind of __gup_longterm_locked() that put the pages back without checking. Normally on error this function would return the partial page list making this the caller's responsibility, but in these two cases the caller is not allowed to see these pages at all. Cc: Cc: Aneesh Kumar K.V Fixes: 3faa52c03f44 ("mm/gup: track FOLL_PIN pages") Reported-by: Ira Weiny Signed-off-by: Jason Gunthorpe Reviewed-by: Ira Weiny Reviewed-by: John Hubbard --- mm/gup.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) v2: - Catch the DAX related case as well (Ira) v1: https://lore.kernel.org/r/0-v1-976effcd4468+d4-gup_cma_fix_jgg@nvidia.com Andrew, this version with a modified commit message and extra hunk replaces: mm-gup-use-unpin_user_pages-in-check_and_migrate_cma_pages.patch Thanks, Jason diff --git a/mm/gup.c b/mm/gup.c index 102877ed77a4b4..98eb8e6d2609c3 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1647,8 +1647,11 @@ static long check_and_migrate_cma_pages(struct mm_struct *mm, /* * drop the above get_user_pages reference. */ - for (i = 0; i < nr_pages; i++) - put_page(pages[i]); + if (gup_flags & FOLL_PIN) + unpin_user_pages(pages, nr_pages); + else + for (i = 0; i < nr_pages; i++) + put_page(pages[i]); if (migrate_pages(&cma_page_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, MR_CONTIG_RANGE)) { @@ -1728,8 +1731,11 @@ static long __gup_longterm_locked(struct mm_struct *mm, goto out; if (check_dax_vmas(vmas_tmp, rc)) { - for (i = 0; i < rc; i++) - put_page(pages[i]); + if (gup_flags & FOLL_PIN) + unpin_user_pages(pages, rc); + else + for (i = 0; i < rc; i++) + put_page(pages[i]); rc = -EOPNOTSUPP; goto out; }