From nobody Tue Nov 26 14:22:47 2024 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2042.outbound.protection.outlook.com [40.107.93.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5A5646444 for ; Fri, 18 Oct 2024 01:17:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.42 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729214245; cv=fail; b=F/AwVKz+cOGHNjdKwIw6Zh0YPqNk7vAl1N1Pj+bd1xRuy3tpur+2YTA3nswqQsXU4p+P6KdBTcCT98mdWOTtxfQBbImK5e8EtRMS752oOIS4DTOtoS3An3g7m3V5MCH3isC1lcLmNesN6MIBEaHoVu3UNf99a/68NC7MKR19rqc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729214245; c=relaxed/simple; bh=UYssHrNGAq9KXgJ2QPhrf0wKcdlxH9FyLsmiECT69sM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=dSD/twrOTjuHh45rNQ86nzxyHjalF/sNB6JhYGxr5JnVYfMkJa5gG1DqrZ+0ekCzOs2TzCdaFzkJF/xDkTObvW13HpFOucXGKU5ld8EuEehVZH6cjZC/ulT0UGvKlBmg1bnbgFiT5ux/jXbZ+XmuNbTFqF2mZ4+qRq/tpUJRudM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Q1rSnmyV; arc=fail smtp.client-ip=40.107.93.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Q1rSnmyV" 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SJ0PR13CA0044.namprd13.prod.outlook.com (2603:10b6:a03:2c2::19) To SJ0PR12MB5469.namprd12.prod.outlook.com (2603:10b6:a03:37f::16) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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: =?us-ascii?Q?u+xgi/RPVQrRtaIYaIrdsoyHkUKIF8OlSoZ703MVWIIyhQ7Po0mORhreDwOL?= =?us-ascii?Q?xNwpyIKdbsg2PmmEN+Rx+odB9Fj0b5pt/xrpW/jcOvKvoaPHEbbC4oB73XoB?= =?us-ascii?Q?tPt9QcRFiH2H6A0ElxXlKsIfVbKL9UiGRSJtZUNUMZeDZ9CbWQhTFRQvxsT8?= =?us-ascii?Q?Ii4XLChs38vVdvscJpVU2IYtTEmQ2w1XJ2PbND/fdqKMG7UaGLxIbfWyvBqw?= =?us-ascii?Q?iSYle4VnmvI6FrTfv9dDyEPJrurBLprrgl8Vj7HbJZuZrjC4v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IVb9wo5AOmWi0CRLLfrjLnbFUEAWUrLfBggbJ/bYUgnyeKN?= =?us-ascii?Q?X1Lvg1d3tgSReHkPqvIgGqFUEW1d24lxDgBV1YQhZETlidvtZ4ZcOUGjWw0z?= =?us-ascii?Q?6wAehLJe/FmPcODOHtk/xMScpFYPNiXkBUJBr0K8hRViQXJH9CYz6hl0t5EC?= =?us-ascii?Q?dZpLDKtxwIUnbm+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: =?us-ascii?Q?QBqpOBTS+8Iw2ubqb9DxjwjU8/g0iXBwNaHwAc8J5i3uY+HQvZyNrbiTCFg6?= =?us-ascii?Q?NFVLDfqnBQ81bNy7s5qyKIptGfzp90B//DxXlLTHlnOYvcsPMhWyp4ZscF6L?= =?us-ascii?Q?Luy7AykWeL81Kcwn7bpupHwULvl3C/hQYxsApA/pz2Vm2myeqtiXKch1XjNS?= =?us-ascii?Q?lVUXnZkjNwcemhEEyi5je0w+y2sqRXDUB12/e0zlL96lFgGgZPrfVLztaGIl?= =?us-ascii?Q?Ep7R99Un+cNwLmxuR2vJqBsq/N37LUfCkCFob33yo3i0hcnzXqkDDjRrMzEc?= =?us-ascii?Q?eGkwqlemujbaLu3L/pqans8JkP2g7YWdfp+nGHjtT+W6MJ3pSYvvaoAGRMKW?= =?us-ascii?Q?ArxSvwXFm2Wulbt5FyVlO8nrLKhwPvhONNjNoHcRvaegLyVjOPHov8LeVqJl?= =?us-ascii?Q?UCHY9ZRc5BbOR4vXezt3kkh5GipvoClNw0uKHpLYqG9CddjYI2iibc0Bl103?= =?us-ascii?Q?FyaC5fFLaKdawlPmPC4VqwBL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Q?36/sfCuu3t6KmQKLatdkm3Qa7yYlJAo3IKsPvXPMAiMmpHtMH7N6M2I3PS03?= =?us-ascii?Q?WKbmOpatLbYxuA0KJkTV+1TDNMGFbVqbbuRIVv8K8fm8j9SsIXA+ZokbzNMO?= =?us-ascii?Q?g/uL1fHwEMRaqqNQkaop7SgRQjZsHyFyh+vDOKIbCbIF8mJOzIuApROe6Kij?= =?us-ascii?Q?f3E5yrxMd5nvISyhYkiRBXPC3xaKIqoScMQpVJOw?= 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 Content-Type: text/plain; charset="utf-8" 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 memf= d 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 st= art, loff_t end, ret =3D check_and_migrate_movable_folios(nr_folios, folios); } while (ret =3D=3D -EAGAIN); =20 - 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); --=20 2.47.0