From patchwork Fri Oct 18 01:17:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Hubbard X-Patchwork-Id: 13840949 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B98B4D3C53E for ; Fri, 18 Oct 2024 01:17:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 062E66B0082; Thu, 17 Oct 2024 21:17:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2E3D6B0083; Thu, 17 Oct 2024 21:17:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA7A46B0085; Thu, 17 Oct 2024 21:17:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B80626B0082 for ; Thu, 17 Oct 2024 21:17:22 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3DA751A0473 for ; Fri, 18 Oct 2024 01:17:01 +0000 (UTC) X-FDA: 82684959378.09.B383017 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2042.outbound.protection.outlook.com [40.107.243.42]) by imf24.hostedemail.com (Postfix) with ESMTP id 09847180003 for ; Fri, 18 Oct 2024 01:17:16 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="IdL/PoOW"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf24.hostedemail.com: domain of jhubbard@nvidia.com designates 40.107.243.42 as permitted sender) smtp.mailfrom=jhubbard@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729214093; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CZAVy+a1Uit8MnAlbuW5dKC2vHWFJUruiB8FoQzkxfA=; b=6CeVuKqI44NZY4+7KNak5fJHDgv6WRI2VmSvk+xhMvEUN5eZ83Bbsyhfpyqi2/wldmI6as Sh2QnTFi+yiJBb95yrV7e3AWBcbHy+M9hFQYCBm85cPlHS/ZODH8T7gkrIiQ2ZSXW1uXZ1 giiH7E51DPQHeq1N46/AvsEYpD+BilA= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1729214093; a=rsa-sha256; cv=pass; b=ieTMsDQDXzY5cf0Z4Rbks6sfW39l1Qub/rYY1t0JbUfzmQlk375YvnDY4tqm8V2aDbqfwc Fbjk0c5k/RXGPMnX85u4W1Se+EM8l0rPyZ/y6YsjcrozMsa8H1kqNqNi0pe6tL2s7eZZz0 cLIZpS0xszeXvLhdqnESRVzNXTx+9Do= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="IdL/PoOW"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf24.hostedemail.com: domain of jhubbard@nvidia.com designates 40.107.243.42 as permitted sender) smtp.mailfrom=jhubbard@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YVnOtkV4HcVoNH0Uz5hJqRnqKqkH07NRMfflYxM601vzTDEvL3KlS1MfkaaqhOJt/hKMWgaexDyUc3jvtDuur0pJrFExGiUZX4Y/Mjsg8kix+22vS0CYzz91A/06wRp7ATZlGsKYSJ0b13b0KPBOKGJkSl9SqtyPBE2cPiQlDUQrFDhXYrHsZ+eQWzkbx03UzXfHqIIPgrdZlo2gr/cQAheSRHSkt8q4pjVKQ+yHwT9QvbnNMfMM9GtoGdQlUbWcBc3EPIbG/vPEhf4lCGsEDpy2R0ho0Chd2t7xr4SGy06SH2WONr5n5i5NutPihEkPEph6JGDYxjk3ZtM8gK7rhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CZAVy+a1Uit8MnAlbuW5dKC2vHWFJUruiB8FoQzkxfA=; b=EsBDGcgrPDl1Xwm1eS3TJ9tQ//YP5XqKk22tzMrYPD7rQt72RDICGgt4AaTYtNRWAXmy/2XqUT4GtPW8eUQYyF5OaWC6g075iOcrWp8isA9JP0VnpAXnLU1jazhPWQD09Rc0Cc50T61xM2GJ4jikgq2lR5TcD/NzGtQTTMhnFNvHsBr0ekwJM1QLLpnOUNdb9Qwd3hj3tWnk0Dd1GsztBmI6mZ6QY7Iqo9ocHbZX0Qz6kO/gs1rQ2z5bRkKBDA+q53nRkJAB3jUaozUpcjkvSdDoB+A8AYDf3TD9I6rXfWMGCqYVExLK3AMqR7ieuZRu5XJHToL3Qit3tljjqP+hdw== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=CZAVy+a1Uit8MnAlbuW5dKC2vHWFJUruiB8FoQzkxfA=; b=IdL/PoOWrFONiU12U/U4+AnSFp6zWr33r78ZUCYcLd29+4QXX8Su7bImHNPGTohR2cAX3KGjGbRpSj0TKAzAyY6J8CwuRP58ja3sWHPEg/mqCjk+u/s4odqfIxbxQSu8H43pFIq4j+1H/nUxP7CFDaAjKqRtCrFHwPr4H8scHjNtRJUHr61QxiEa+q78nZEU6+WFJ+pYWBKKWCXLrrvwE0i5qoyIZD+yx6Wr8srEDmtHXzdc+idjfysXS/e/6jocDU34jTNn2+1OvJ8wiWAiGvtX469l0YZOt0lsU/+JjNpv2fRmsS9Q+Lbh2mI/35Wds7EZzGob14rUKXjKAv0ItQ== Received: from SJ0PR12MB5469.namprd12.prod.outlook.com (2603:10b6:a03:37f::16) by DM4PR12MB5913.namprd12.prod.outlook.com (2603:10b6:8:66::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.19; Fri, 18 Oct 2024 01:17:13 +0000 Received: from SJ0PR12MB5469.namprd12.prod.outlook.com ([fe80::ff21:d180:55f2:d0c0]) by SJ0PR12MB5469.namprd12.prod.outlook.com ([fe80::ff21:d180:55f2:d0c0%6]) with mapi id 15.20.8069.019; Fri, 18 Oct 2024 01:17:13 +0000 From: John Hubbard To: Andrew Morton Cc: LKML , linux-mm@kvack.org, John Hubbard , Alistair Popple , David Hildenbrand , Shigeru Yoshida , Jason Gunthorpe , Minchan Kim , Pasha Tatashin Subject: [PATCH v2 1/2] mm/gup: stop leaking pinned pages in low memory conditions Date: Thu, 17 Oct 2024 18:17:10 -0700 Message-ID: <20241018011711.183642-2-jhubbard@nvidia.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241018011711.183642-1-jhubbard@nvidia.com> References: <20241018011711.183642-1-jhubbard@nvidia.com> X-NVConfidentiality: public X-ClientProxiedBy: BY5PR04CA0001.namprd04.prod.outlook.com (2603:10b6:a03:1d0::11) To SJ0PR12MB5469.namprd12.prod.outlook.com (2603:10b6:a03:37f::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR12MB5469:EE_|DM4PR12MB5913:EE_ X-MS-Office365-Filtering-Correlation-Id: 6e0850f8-9cf0-42de-2c5a-08dcef129905 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|366016; X-Microsoft-Antispam-Message-Info: pPFEmPgG2RAz/8NCQYsLiycVECysgyTlQhNpE49rIAalEFv2owswsK1r+zOl0c8K00KPQy+Ewesb1cEFraamW+RxKhyK3IyfG3dxomz0VOS/s5xNPlZMjeZDROqnmr69T1Z1uGlczE8/qBzLL41tJOhEmRRXZHSdpCtW7yfjGGVcGSiUGyrGkgEAUn21N5gy5+T9St6d2PVltOMuDM0UIFQHhLBfjH3HmiemTJQbQA4WYO9WqM5lMrs5lZUgCNS94T/SsVdQFlAfPVCd/238LGJ7q6QYDSMls1hB4ZsSKAFLEsJolUyyUFu28FL/u4bZyaVZRrNLB+zAtdljuY6uz17IsMOJwuZwCTQmA+kZOSybSfXMjXJFG7kyFqhlBP9Z7e9yGuR2XkZkT5nje2UXfAKqy4oecBowMPDSSOltNfz7zMMKRdDg+3NfDvr4pyvLd0zt3Ue1IUCEOiCeck8+5K3eqL1/AnpF+LqOBZB/cMcYxbOiPTyNYT15uuggFc4GOqEdu4BcUAohzpNFQSRj0DLa+m+TyxoOI/JxL1vR/RHo5JoM9tZZ0ZW6pPvBhQ25zUr8/i3fTPBV1i84PCKKaa/l9unWp/bI7rkUd2N8KdTvPxQsHFaCKAgabnRjZlYPPwP70Enmsu+cLTYq0VGiTicNVKn94KNVxF62/JFkFxLaprObT5M6TKx3c9W2Qmw04HYDz4DiCla4rAzP5DA1HzaKqIrRFeJC19v5BqBjQlB6ajzMfaAGvKZJRy8iJaemCoFGxZ3Gnd1R/Q4wDb9ll8a42iKfblCmr/Q25TLKBmaFlZeWo6caGZcJ8gt/HbSi9Tp5Fvm4fgT/Z4Q20b4QJ/3PD5UqzdpZzSTaeM5C13v1XPndJYRfb/yESW/pshzXwYaH3Zqp1MDUYi+/mjabfUEyhZZbdVj9rkQNnQZvH4Iw+C0OV0jfZERTtnodoItrZBE4AKo4NdS/Wt/AwMTrIp+ctNQrN2LNB8swLy7TmnhxDkSb+nziwYxxNQPzf6XyQc6HuvIEHn49gttTZVuhTsXtMD3MUCSYRXZoz9mhLcelf0Jn6DpuVmfkyhiT8sPyeymQAtKWN2vkh3o7Noxp9EUgeQ6ocejuN/RevzwZ78kV90CaNjoe8BtNQvnJPTk5JqD2smODwYEThCzeC5O9fZpId7mOk+aFR9XMg1DwKNbifJhMEpfnO/DFdO13hwiskfGSZpmdMpQmfgi20yxlO8wkK4bz3GjXtdKBgcfM3cqv8eO9aJ6US5JZUne9Pxnm4Vf4DNIlNVS0bRONyoW3kUVmCPZV7nTNC6qH1flMezNM0qOfteYbfS82BK2G405X X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR12MB5469.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9jJWpWzpA7rDXA/fOmvk1XX/qVUgrhYJDblnsk7cmOiNg5+jVXJFomBAOEAsA3fTOtywPIJnEzBWz9tf4GPICbTDtvJnOO1v4W34U4YSCC/x0VYQ+JWp24wmcc/uAjMvdj3LxtR+kk4L1S+E1OuPbV1qHNzeCAV8dlzVRH4cFCxY3853UOhRgI8Gg5nP5RIZdLCa2DRwen6KGPXStOrCHXcJsxoMMMHxORCFwztjrqHKl56A/kZNBibQ+ggGsymEXLq8DYuvW204jb84KYMBl1xPUyLUYdDXpRDlhSRUIOTFhopdvq485u/P+VWzLT5py3rQSwhu7H0q/mFimFlw9I4Pz5b67cu5rutDSVO+OTMmODi2XuBzYXFXLvsYCjMo2JS32oNYY87xeRtLOJ8Tc6eL3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51jtcnWdN3+cTdHZaWlHkDn7U/BMWTbX/KifwmKYxNwmvj0YmoiGhnUsuzH0zx0u2qZ94XJxKOnu7FzWCn8X9XBQMxsDJJHYKS7mMZtMSL0vuEduGtnbZP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6e0850f8-9cf0-42de-2c5a-08dcef129905 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR12MB5469.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2024 01:17:13.6209 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: dee9T8jH4KjqyfmUEDkkLZSVj/Srvpg/sXI7XUTeL/S7F+nwcKzZOishQQG2MQSuQMXdgKjQGFI7rmO7GXulcg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5913 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 09847180003 X-Stat-Signature: bwbpcujapdouq4ohx745xh7bwnqf4qhs X-HE-Tag: 1729214236-796009 X-HE-Meta: U2FsdGVkX18vclaPYEVn33TvJW7erVBHrnTOx0DxenjI7l4BeCfK4KAP5NnZToeuIwqGWBi7JDG/4AhnSELV5SRcsuljpT34xN7Wbcd1QxhODBM520DBhSRPUayhVL8Deozh84SUK+q9DCjPdxNMAcxIE00a7Ct0+EHCgvHgpO/nY1dAc4dqkOOJE5pmwCebjakumme9yEVJA0CqPX5Kbqr2NjsnPdRYC3XOz4WUqVIAJhjoHdkSod/byO699RT5Y0l1Xse/Vr8tR2H2OspsHj+sx5GyEZuYS0+TVYHHEqD4vRxfqP2NyVhVv1VPPmgCcdD3gsZhp1lluRclsB4zUsCpKxTvw9bn4mrntLyRt06ZaJDIc/uCbKDQXvrTBH1ARGTqDtnP1bhZYYv5l8wtcAxFUkLR6SZt8UdhPwanSaP/2UZ5Efu+HxkwCP+u/SZMeOwilyLtEdR3Yl+WmSoGz/4RCBPKxvIWY7bmINpDKVxcmtlC15WagZmypvPh9mWimFlFkxfsucAYb/YCcvfMmpzKKX6yJjgMQVRa7QRolqGFgwZbQnIt864R2CSzVcNRuoYqO4ik810/d2UstZUYejaW0K6/XezKVq3i+ughQkBjA2LemrxJt51ZpYFxxi7KdOpciVNTq3KBARQ4w3sA0yIHykP82VLe9UuDR2kPkgLWtXOYNIhzd6drSOkqP2l3r6+zrvauDC1/sKD8NqOZLUUz143xZyn+kCJT8yr/TZsMoS4q89OiRRscT9b5TVQFAcvU3zPegIae/5eYnXyxU7GbOQY0kOmcQL1aExI7YI262J8+Gmgz0NnWsF0kPPSCbqvgMzv1LD8xkutYr3Rsz88XLjzoARVe7fymOTfF3O3G0BekZyv/vfg0ASkYroz06S/6p+/XnJX99UWjymUCdVJfNEIBP1SZ+0PwvcA34fJ7Jjo3M/ACF1jfAjbPKsklxc+dT5sNJi5XYo0AvQI 10vOQlz7 fG83AHABGYHdnxM/mw2YrT/1f7AY+73Bbchl+dRy9cAMn6nUcDteAz3ymwXP/8YT16h0yfQs2A1X9Y9EXrFaSPHSfKzYAocL/C9aJmAtSSqVWTjw8mp0X7YU+Bi1hboFeHpaoOpCY2zAOU58T7t2HnusMU8hts42unbwve/mKvKtIT07qIN+v8YsMZ0QETV2Cb63SbgA6dzGKG0iII2IE2l6yW74QBnaFhrZrH3UTPUypDIjJ6KHRHegZYsPtuLtMBC2oaIvk8TmqxD3qPRGC5kHSIrJfbPFdM61LpyZ8dcB3erMDHGra7B2/UOroCAJTnNu/FkocHDT6UnBb60+uumzG5g97cFijdoF/uMtwUtThjgokqT32i4TmkfwmswWQAkD+4c0T9bCbcpcIuO0ZHbQSUA== 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: List-Subscribe: List-Unsubscribe: If a driver tries to call any of the pin_user_pages*(FOLL_LONGTERM) family of functions, and requests "too many" pages, then the call will erroneously leave pages pinned. This is visible in user space as an actual memory leak. Repro is trivial: just make enough pin_user_pages(FOLL_LONGTERM) calls to exhaust memory. The root cause of the problem is this sequence, within __gup_longterm_locked(): __get_user_pages_locked() rc = check_and_migrate_movable_pages() ...which gets retried in a loop. The loop error handling is incomplete, clearly due to a somewhat unusual and complicated tri-state error API. But anyway, if -ENOMEM, or in fact, any unexpected error is returned from check_and_migrate_movable_pages(), then __gup_longterm_locked() happily returns the error, while leaving the pages pinned. In the failed case, which is an app that requests (via a device driver) 30720000000 bytes to be pinned, and then exits, I see this: $ grep foll /proc/vmstat nr_foll_pin_acquired 7502048 nr_foll_pin_released 2048 And after applying this patch, it returns to balanced pins: $ grep foll /proc/vmstat nr_foll_pin_acquired 7502048 nr_foll_pin_released 7502048 Fix this by unpinning the pages that __get_user_pages_locked() has pinned, in such error cases. Fixes: 24a95998e9ba ("mm/gup.c: simplify and fix check_and_migrate_movable_pages() return codes") Reviewed-by: Alistair Popple Acked-by: David Hildenbrand Cc: Shigeru Yoshida Cc: Jason Gunthorpe Cc: Minchan Kim Cc: Pasha Tatashin Signed-off-by: John Hubbard --- mm/gup.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mm/gup.c b/mm/gup.c index a82890b46a36..233c284e8e66 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2492,6 +2492,17 @@ static long __gup_longterm_locked(struct mm_struct *mm, /* FOLL_LONGTERM implies FOLL_PIN */ rc = check_and_migrate_movable_pages(nr_pinned_pages, pages); + + /* + * The __get_user_pages_locked() call happens before we know if + * it's possible to successfully complete the whole operation. + * To compensate for this, if we get an unexpected error (such + * as -ENOMEM) then we must unpin everything, before erroring + * out. + */ + if (rc != -EAGAIN && rc != 0) + unpin_user_pages(pages, nr_pinned_pages); + } while (rc == -EAGAIN); memalloc_pin_restore(flags); return rc ? rc : nr_pinned_pages; From patchwork Fri Oct 18 01:17:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Hubbard X-Patchwork-Id: 13840950 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97EADD3C53E for ; Fri, 18 Oct 2024 01:17:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 348F16B0085; Thu, 17 Oct 2024 21:17:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D2426B0088; Thu, 17 Oct 2024 21:17:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 124C96B0089; Thu, 17 Oct 2024 21:17:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E89706B0085 for ; Thu, 17 Oct 2024 21:17:35 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 75D0C14043D for ; Fri, 18 Oct 2024 01:17:23 +0000 (UTC) X-FDA: 82684960218.15.9904030 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2057.outbound.protection.outlook.com [40.107.243.57]) by imf03.hostedemail.com (Postfix) with ESMTP id 8337520008 for ; Fri, 18 Oct 2024 01:17:27 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Q1rSnmyV; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf03.hostedemail.com: domain of jhubbard@nvidia.com designates 40.107.243.57 as permitted sender) smtp.mailfrom=jhubbard@nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1729214145; a=rsa-sha256; cv=pass; b=ZoNnd02271D/ZvlQmn9M57dohUY/s9gFPACIIoydMcgk5rqN9Uk1+fVFKAwvCIZduF61pS 5rLldBxjLgLbI5Cv1VkL2T+ZV1+2gRX/h/252OwO51YrmBeFMGCMW3WCWStSf7SFa8MWrO p+Pb6AsCUQHF2mSW45v9Zg4uz0SSHyE= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Q1rSnmyV; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf03.hostedemail.com: domain of jhubbard@nvidia.com designates 40.107.243.57 as permitted sender) smtp.mailfrom=jhubbard@nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729214145; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Md1MjpdHuilWPTTJzdr6SW7W/BrD9InA2SMVrB7H2Hw=; b=MO7otKdymsHr7N8FhNcUChiyFUZEoN2wX5oTL4Ad3LoiQ03U7Bd4Y/DMU9zu9iluQEKq6g z8zrbqf44nP1btkc72etSGR895DtELNObuyP7EZgAJSRYfEuqUG6vEGzoW474R2F+LjKQM KabKnWsmT+Wnyj+EPxYzwTigGV+GfWE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=drWu645H172MaJUNq/CikRaRJF/MCvBF8S/MwV2AUoK2vgaW3PywTInEO0BjgncHWFt/h2ZDYwwjJNHlDQbw5WUiVV3nShznGcN/Jmv1EB1dx9WW3vq81s2dNFD5ORfuyIiQwYFzfNB9TgTMSuSDiSdAb3+hE4CeJ44oJgkaYt72EjXBOYGl/5oORKT9BOiEUdZOPDHFUhZnwDv2/w2NzKw4aM+lr5kif3ZCJPCKJ/3UfaMdyR1J6N0IVISE4vz1wyvf69uIFuJDorGModjTdfN4NFLVmSVzP56RB+K+63kwvcXsYvqJrIyQIDXUybCfAwUHhmkB+zrMfjhjTDMMnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Md1MjpdHuilWPTTJzdr6SW7W/BrD9InA2SMVrB7H2Hw=; b=nyuj3jsPRWTH1iBsserpUGOn/Hfam2FDTMu8KxWEKAwLudN8RZ3TvLzavmbNAcau888C5oqciJW+twxiioh/lALf+IUpHexmjR+JTRcU/iterACrSuBQVS5z/VLzcf2oFOz2P98ZuFyOl58qcbJWsEnX/PMLScTkPx9WHgsG1h/dtgZR316VYEXNf14fnRljg7MMDf3zglq3clVQ6WZ0givjCcCGGn2ZMcSjAw8nsZ9yvk7WnKy+Rw+y7cF3OWd001BVXc6VCl398ZbybVRpYya0X4ETfSaLAFM7z0YzR0hOoCOKjTICieUZofE3QtF/MltP5KCUauhiHsB5OZB0Bw== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Md1MjpdHuilWPTTJzdr6SW7W/BrD9InA2SMVrB7H2Hw=; b=Q1rSnmyVPk4jIRnjeeBzwxpAadgT3gBg4nNn8veCg6CiBl1bANhExvbHQQu+CaLXMwaGhBTf12OORM6Fzfexpy2WqnvcC5+UrIlC9wCRZPT+aa8sEEkhK2zDA2XhNDOs0RYowgg1GkaOuZBa+Q2G+LCR7K9APKW4v5aX3OXHpa2TGgYwPBORkHMdpytStSNpbuMa06egs8aOiSmkgoIKEm7iuUocDmUpJuIZLsyP2LFM3juiBZBdpVjYUBCSk2TC6RwTNaJ3XNU4caVYdyYMJSn4DjzyrSd3eHY5lgAkge11TEcUBZ5mbpMnAj6q/uC5uWiJTepeZWWeZ3UafeGDUQ== Received: from SJ0PR12MB5469.namprd12.prod.outlook.com (2603:10b6:a03:37f::16) by DM4PR12MB5913.namprd12.prod.outlook.com (2603:10b6:8:66::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.19; Fri, 18 Oct 2024 01:17:14 +0000 Received: from SJ0PR12MB5469.namprd12.prod.outlook.com ([fe80::ff21:d180:55f2:d0c0]) by SJ0PR12MB5469.namprd12.prod.outlook.com ([fe80::ff21:d180:55f2:d0c0%6]) with mapi id 15.20.8069.019; Fri, 18 Oct 2024 01:17:14 +0000 From: John Hubbard To: Andrew Morton Cc: LKML , linux-mm@kvack.org, John Hubbard , David Hildenbrand , Alistair Popple , Vivek Kasireddy , Jason Gunthorpe , Christoph Hellwig , Dave Airlie , Gerd Hoffmann , "Matthew Wilcox (Oracle)" , Daniel Vetter , Hugh Dickins , Peter Xu , Dongwon Kim , Junxiao Chang , Arnd Bergmann , Christoph Hellwig , Mike Kravetz , Oscar Salvador Subject: [PATCH v2 2/2] mm/gup: memfd: stop leaking pinned pages in low memory conditions Date: Thu, 17 Oct 2024 18:17:11 -0700 Message-ID: <20241018011711.183642-3-jhubbard@nvidia.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241018011711.183642-1-jhubbard@nvidia.com> References: <20241018011711.183642-1-jhubbard@nvidia.com> X-NVConfidentiality: public X-ClientProxiedBy: SJ0PR13CA0044.namprd13.prod.outlook.com (2603:10b6:a03:2c2::19) To SJ0PR12MB5469.namprd12.prod.outlook.com (2603:10b6:a03:37f::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR12MB5469:EE_|DM4PR12MB5913:EE_ X-MS-Office365-Filtering-Correlation-Id: 9b67244d-bbee-471b-ac17-08dcef129963 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|7416014|366016; X-Microsoft-Antispam-Message-Info: u+xgi/RPVQrRtaIYaIrdsoyHkUKIF8OlSoZ703MVWIIyhQ7Po0mORhreDwOLxNwpyIKdbsg2PmmEN+Rx+odB9Fj0b5pt/xrpW/jcOvKvoaPHEbbC4oB73XoBtPt9QcRFiH2H6A0ElxXlKsIfVbKL9UiGRSJtZUNUMZeDZ9CbWQhTFRQvxsT8Ii4XLChs38vVdvscJpVU2IYtTEmQ2w1XJ2PbND/fdqKMG7UaGLxIbfWyvBqwiSYle4VnmvI6FrTfv9dDyEPJrurBLprrgl8Vj7HbJZuZrjC4v+CyWlDgu6Xfoaad4t/qfzUtwSL/j9f/r52is/staT1vDAaTLNVeO8zUIdWV8n22L2fhowrIwOJ0I+q7UTR7vjuWKejYdSmCyILuzaOZi+WqzKXZQWGuJOptRxZ8+DP4YZuDJkeEDYq95ceIFAz5i9mSUHV4aDf+5jimJ6zIjh8Du4u5WVNOdFfAEQqj7nr0MJiJnCqLd5Ond7ESGRk+k4CKgOVO74Wa+GuJ3HlgtF6VlTPneyXlyuG/5/71ORaHUXxiiQW1V5EkZ2+QEeAUFVUK1EgsEgZ9lsEs9qljHIhtWm6VTBBeUOUIzNYBpp1sNq85j4dGg222rFNe1HP8jP2DWbBY+EtlS9wltr9I3Zc2h2JvBYUHc85X6R82Wu7YxxxmptxIJtUUdDgF0In0zyW+LfaNGOpBXQhRY0jVyrAHy3L1Mdyg+uyFYkUm2NRqP27oCsXlzahCM/hWGmDKOK8+2X2xbO/3Hh1AFoLPWYIqFAp0cYVaD/MZMXpdQbOAQI/cQy7lM0uAfsyen5u4GbSMlFqaQ2XUUzthhFKIxgdex8akmL9ocW0wpgBxgy191NRdUClQMy2KSThPIcVjJKQ2LEfSPwneO0gYDHAIm2Wm70WG3n/E8pO4gEFttJMeb25M5QQfTQKNJKTmYZKjOCleo3B9xwwDhqTxWfztFjjJhKk+jHd3x1R8NFzTmRTWWPe+abvyLEH9/s8fqHuoqovtI8gW+lp9nn7/9Lf/pPBujOfN0lGlrZ1zLu4diDbYBgS+d2cVjKWsktNAGoasdOK1xbbfM4yyKHJ7NG6n5IVNmRMsoNtLW+poYJifHLkblxSSuo0v2tNADAlssZhhr195PvDQf2I6CIVb9wo5AOmWi0CRLLfrjLnbFUEAWUrLfBggbJ/bYUgnyeKNX1Lvg1d3tgSReHkPqvIgGqFUEW1d24lxDgBV1YQhZETlidvtZ4ZcOUGjWw0z6wAehLJe/FmPcODOHtk/xMScpFYPNiXkBUJBr0K8hRViQXJH9CYz6hl0t5ECdZpLDKtxwIUnbm+CdEgu686l X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR12MB5469.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(376014)(7416014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QBqpOBTS+8Iw2ubqb9DxjwjU8/g0iXBwNaHwAc8J5i3uY+HQvZyNrbiTCFg6NFVLDfqnBQ81bNy7s5qyKIptGfzp90B//DxXlLTHlnOYvcsPMhWyp4ZscF6LLuy7AykWeL81Kcwn7bpupHwULvl3C/hQYxsApA/pz2Vm2myeqtiXKch1XjNSlVUXnZkjNwcemhEEyi5je0w+y2sqRXDUB12/e0zlL96lFgGgZPrfVLztaGIlEp7R99Un+cNwLmxuR2vJqBsq/N37LUf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tEmg0RBWBT8CE41cOM15wrwGv36/sfCuu3t6KmQKLatdkm3Qa7yYlJAo3IKsPvXPMAiMmpHtMH7N6M2I3PS03WKbmOpatLbYxuA0KJkTV+1TDNMGFbVqbbuRIVv8K8fm8j9SsIXA+ZokbzNMOg/uL1fHwEMRaqqNQkaop7SgRQjZsHyFyh+vDOKIbCbIF8mJOzIuApROe6Kijf3E5yrxMd5nvISyhYkiRBXPC3xaKIqoScMQpVJOw X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9b67244d-bbee-471b-ac17-08dcef129963 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR12MB5469.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2024 01:17:14.2502 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 5yqFJFAWNIz6A5S5htlIkGZ5NSI3BAed7Utd+y1nVtlniNqXCMAlHpVp9SKA2qgCzgtAtj7aON94A8M1f9t+hA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5913 X-Rspamd-Queue-Id: 8337520008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 3fte88epdoseogt9rskywdqd4r9tktj4 X-HE-Tag: 1729214247-246738 X-HE-Meta: U2FsdGVkX19BC2wb7lucfthYRudmzfY6An9inPa2XJKoZVd8wnbssImEcJirUgvLtIBVbK1FhL979MXFe6MsneRUqanXf9N8HR3yNxYMzbr5/yVsRcnN4TiBWx7gQWULKJAuOivqVmXx1X0pA1LWDnVuCMX1LfvtdNl7qjHvbAM7w1rfyNDc24ctXykUoyd3dQEY2qdu4zrj56lZVUQmnH/eQuEVg+dhh5NhMOwVeSVtJvPnFy3dgzkadKav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Kr+p9FJwoQdL6yk3AlndgJivpnPTyHmV6GMvNwFlGtMdaTxR/ilBm/ZkTEsP/8PTOdiRKVfGUAGkBko7i/BOguhhrt6091whiSQ94ccPx8EOGXJ83QEDOjLjg 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: List-Subscribe: List-Unsubscribe: If check_and_migrate_movable_pages() fails, typically with -ENOMEM, then memfd_pin_folios() will leave pages pinned. Those are leaked forever, and are visible to user space as a memory leak. Fix this by unpinning the folios that try_grab_folio(FOLL_PIN) has pinned, in such error cases. Fixes: 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios") Suggested-by: David Hildenbrand Cc: Alistair Popple Cc: Vivek Kasireddy Cc: Jason Gunthorpe Cc: Christoph Hellwig Cc: Dave Airlie Cc: Gerd Hoffmann Cc: Matthew Wilcox (Oracle) Cc: Daniel Vetter Cc: Hugh Dickins Cc: Peter Xu Cc: Dongwon Kim Cc: Junxiao Chang Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Mike Kravetz Cc: Oscar Salvador Signed-off-by: John Hubbard --- mm/gup.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 233c284e8e66..dc4906243b97 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -3719,12 +3719,10 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end, ret = check_and_migrate_movable_folios(nr_folios, folios); } while (ret == -EAGAIN); - memalloc_pin_restore(flags); - return ret ? ret : nr_folios; err: memalloc_pin_restore(flags); - unpin_folios(folios, nr_folios); - - return ret; + if (ret) + unpin_folios(folios, nr_folios); + return ret ? ret : nr_folios; } EXPORT_SYMBOL_GPL(memfd_pin_folios);