From patchwork Tue Jan 24 05:42:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113482 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 B20FDC38142 for ; Tue, 24 Jan 2023 05:43:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEACE6B0072; Tue, 24 Jan 2023 00:43:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E99046B0074; Tue, 24 Jan 2023 00:43:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CED9C6B0075; Tue, 24 Jan 2023 00:43:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BC7D16B0072 for ; Tue, 24 Jan 2023 00:43:44 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 943BE803FF for ; Tue, 24 Jan 2023 05:43:44 +0000 (UTC) X-FDA: 80388600768.26.181A6D7 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2088.outbound.protection.outlook.com [40.107.94.88]) by imf11.hostedemail.com (Postfix) with ESMTP id C009A40009 for ; Tue, 24 Jan 2023 05:43:40 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=bQbX4+VE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.88 as permitted sender) smtp.mailfrom=apopple@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=1674539020; 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=9MtXqt5AI6xt4ICbluiJvN2UYWO1rvf+ghlewsHqu9o=; b=sE+eL/XDU4ugrKr6DWMSG5OwoCmhBqWRvbTeZRdyvg3GRD/LJvP7mtFLZGllrohG6IwnHy 0JTZOPn5zKpBS8/5NQx25v+WjDR4yzfeDNsIZmba+23O4xpalnwXb1VOMbM6jQwLxZ8gRC 9V6UkUc09wg3yXeNN1C+NK5KvQL3Cdo= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=bQbX4+VE; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.88 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539020; a=rsa-sha256; cv=pass; b=Z4GuzLuK3i1O2V341zT5JhQZSRZOwYhAy3mwywsIpGErEpwEVoEdOU9Ch6+nlOYjGC9ET1 TDv4jC5GC2TNawf6sXj2aOQ+mWItb5SagN+KJ46mICWoZchlYl0ID4aBgNHklzy5VOjqih 2POxUG+6PuTqBv0HXzfly5K+xnSHyM8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kFiXQfbvBkmWql4u3YiSkcsVXxx4fdxDzFh+cfG0F7YG8RKF2myht+SFIZWPiJ90I+y1/CVQqAgI0vhDNpGIJz0XjEoBz37W/5V8S7cisFLFCvVaqLctGASNfBvYQvgM6t2zZmoUQNV9mmMg8MkP5VXod+lriaP92oQcMXTyqj6OsV8yyxcnC3Phl4VT+c+APnMGU4Uq+SPs/v/KQ+O550bl1MhAmXjlUFPbDMFi0GJW0ZRoc2d7VpmIa3N08a8bztR4bmhdkD3kh9siSvj+Sm5EKd0HARzz46HJoEUBQ5G8J2JwaILF0SURY0EpY42U1VgVoQmSYxIvTIF3DjFHJw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9MtXqt5AI6xt4ICbluiJvN2UYWO1rvf+ghlewsHqu9o=; b=Lqn6Uk7KNBKGCHzoBnigDNo1RiNUI8Hb/hygwqIRZayF1fZm0PeeQoBCWA9nipmIFQ9O6sDUYWZCryEJ8xlpLY6HMZYu+Fmi1cMAqM3Jlg6vvGhq30PYRoqrEtycC3aRavYUAMnlMhFnDUBpc4tyvtadjwPTU0KA5nUGvEj1RjMV0KryOFZFiVcQKdGtcUibr5k9n1TRN1pn0f8V6kJOcFGWueZMv3skF2iCKaPbkGb0MMRXa4hMMHja14TyZGSzd70Bp2QtBQRqIEI7cNFX859fy9XvJkeSMPzklr8FUU+nm8AkIfFemdZj3DX8ag4ZAujP5wCUtmEMJ/8PURug0Q== 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=9MtXqt5AI6xt4ICbluiJvN2UYWO1rvf+ghlewsHqu9o=; b=bQbX4+VEqdrv0T+n3h6V6HKNVTnrJLoLgKshMGWPDjRq+Be3ItoR2iOEfJU8l91nO1ej+Qx7LNgn6gES/pIh9BOzUDP6NL0vmAHEyYxWI9g6rU+TN3EXswdvJL+ZIGEElHreUTXi4GDqPmJXkp/rNTr051kR+aUGHxWFhc1uIw3Oqc81W7WWfX30n5aTOYTvtM2Biu2z7mj1uEVUIn4TnlUlldMEU2ywLWoUl2asaa0LzZg4b2zfIbStdb35n4e9D/vlclBBG8r9NfLa5kYRgze837+mJQO3/Qptnk6Yt8oBp6DBreUwxtxoJS4KcAXc26StmTMiZT3dBMoU8H+/2A== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by MW3PR12MB4540.namprd12.prod.outlook.com (2603:10b6:303:52::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:43:38 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:43:38 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , linuxppc-dev@lists.ozlabs.org, linux-fpga@vger.kernel.org, linux-rdma@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, netdev@vger.kernel.org, io-uring@vger.kernel.org, bpf@vger.kernel.org, rds-devel@oss.oracle.com, linux-kselftest@vger.kernel.org Subject: [RFC PATCH 01/19] mm: Introduce vm_account Date: Tue, 24 Jan 2023 16:42:30 +1100 Message-Id: <748338ffe4c42d86669923159fe0426808ecb04d.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYCP282CA0018.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::30) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW3PR12MB4540:EE_ X-MS-Office365-Filtering-Correlation-Id: 949f3254-124b-4c17-6368-08dafdcdf107 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iMmh0azBbDX7+rL+BE5BEHNjFZdAqJn47L6YKbnxs3D7TsqxrUTQx2RSCUk3jZThDL42O5RGyuwH8zpvTaaLcOekSn4NVxKfSwSxRDATckJ/zWVIPqmuFSlgCVOonXKwtxpTwN1omoNXUf9FGHoaM9MxMnZ2w15IS/57C4DkJPJAlPOxG4ND1JVCtSVxP5K5MV6xFqVsVvrgy56/sKfAKBQdvPbVKI/omxH9zkik6NzSSXsY0fzHd1QTbpYr8sVHLVVCDgb7VA3AdOZUSIEaesfXlSVYkw3EchIllny8q8jD3x0bOdk7FzQVwHxfcXEVeeWJxJLdHoLJJRZs2hHdCT+sImwmAY9nSEE2OyMPhMChDjSCXO7tjAfKmy3I6TxDv6oa/TbXajFwGAbtXr7yTAav86G8TJ7anCZX5Rg8wLUQZM5NTm+tL7CsdosPro8EUMDw3Y37M+N5P5/Tva6u2OvPFeIcqtJwjyYAN1HbpxEVsaK0AkiZI0y5DXwfJow2IaIkLv0djeJUZRxfWusx8xSjt9EGndEsQuEoVc5uGYhZbIs9OgHm9KHFlSm6z8iQf+O+kNdXH+N7nzD+JBy/zgYzeNgm8ao//zRWixl67VmgGTUH/03nhbyX+5GhLzyrA6+f/T6I+iHfINFXU8Hpmw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(376002)(136003)(396003)(39860400002)(451199015)(36756003)(86362001)(5660300002)(38100700002)(2906002)(4326008)(8936002)(7416002)(83380400001)(41300700001)(66476007)(478600001)(6486002)(66556008)(6506007)(6512007)(8676002)(26005)(186003)(316002)(66946007)(2616005)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: j5U5QqlRpm/Gik3sp+z+oKUI1HHnjDZEA4OFtF3hAjU3mL9XzunDP3p9FVmzdc7AYMqirmc+EwX01nM+UL6Va9Qt8dzQr/HKEZPZdyPwz1AZF8tS8qY9AV1pI442Z0Df88RUPMt+VWzZkND6qan6r06X/eAu39uUHw/eVW6jQRbgjSw0SV5MCYWUklPKU2UaJ/5RHBoNnUTtZFrtigudjmyEdIek0jtqqIOp5QsDafeV1vUkPoPcElpHS8v8SOnMklNkkoBYkshtlC9CaGS2qb/DjLMz7hvvxz7KNCtJ4ySVqxEiaV+GM5UK8w3UZRftuefpBb/dek3LIF8RgY3vjiTTnjNNE4LuLLKT8jVg8VsQ/QV3NWyRV6lHrhuVPOjjjVckgokD1HVffBtuVxFGG9Gq7UBCWTOF8O7MM+ayDF6MVDmKddQmxdIWpgy5tKhwPV+y0IZT+LnY8w/DkkvKUK+5I0q6gsqKi14EDz7V7tpIto4QUwYasZ7e1ZCVcbMW+dVTPL8d7h/mZ23Ue3UenIO3VzjS5L88Yq4eNLIdcOWE8B4znTMJu570YAx4Qmxz80zQIUYp23iBgByjNfITPJ3Pd/hVafLEcYXQ17b/SyDimpsDTD8ZC4zPjO5yxiy23pKXSgoYkPRBstRPUDsZK6cho2XfYlDq7338t5Wi6nSBx+D8/0ETEvAFHmxit2H3wqktgA4YYzD43azOK4EMt7f5cuthU5krTGsKnTYQ3+/Lk5vaN6+gIJTXHvauTOdYpHQNBMU6k+B2TGyIIMR86LrtFu2pdyjwmIV945IvAA7HmwvN7jsKjaewpK/tmzBgabRSPg0Q3WZLYbyPSYwqIqUOx8yxB21fNXe6eA3hzFrorABOkoZS1jOGmgo1MCV61ulFEMkwSMrtE1zoPOPcnbB1LFDJWk+TXPBMqewFDcQCvi/Ntiazm8iR6Rg2E5tzomKQ42Dj6L5TGfG5QZjGbf91kV+oI8lWdBxzve+vS2uPQ+KEsLB0DtP7yPfOvIGcyT/nlDXCfDYQF8obLAgsRb36HHqlttTASui5EzGFokjNVp0oj29zBYD4T/LBce33RBhN39V7wgATxPS4Fc4N7vh98uz1EGDLvMRyJe9cu6gdGQdHp4riSS5mqB9MittsgVLeB9TP1R1NFA0RnvOIQdYzxS2zP2PzJ6O9teQYZzFbuxROsaeqg9JxT8fcmCIpmzYJc1NQK0Czh2LALiWJ3S7feot3MQOBwrfJo3FbIGJSYGGWoM3juiDkZ0F9RLR0+T7CwRuada/tWH8cRMO8hGypN7GELE75lheA9jRkafV85GnnMy7uRqRodSp3LKxtlzbYY9wzpV6LrBubYD5ZV0rNorAuBK4v/76yB8mlW3jc+dbAsfVkmcmM00ED40e0zr35seRfbe0iFtT+4qUOfnhWhY6JQGblGbtl8TCYEQuXfNTjisSHSXLJcpkRQa0fkSAFa8Zo8TyMq96w9gNyOivNcOGpdAmHEnmSVeGrFbvR32gtRkftbUeq7R3YF3F73v7vWIB1n+GnUTQuRCz49pDfaKVxzGq7t2I113W3R3C0c+rT1iX7AMaDutlmuub9 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 949f3254-124b-4c17-6368-08dafdcdf107 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:43:38.3387 (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: uIP4ouzbfWVCa0XUJTot9rM9L+PhLKwqPJg7yXDOHet2Jme+y1DQ/VxC+ZGllQMyEfpyeIVZtQ1TVWuxyBQR6g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4540 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C009A40009 X-Stat-Signature: 3ibjbsamiaqkcwujemwkyxzsrcizprts X-Rspam-User: X-HE-Tag: 1674539020-691854 X-HE-Meta: U2FsdGVkX1+ejLuFjCfFuBQMaFL0r6C1812SoCMXzdO0wsyHyKichmamSLZr0zOBc4nftRybEoLxOkuvS2PJQSG9CMkh4kcCd3XHk0X5FK0RUhJYZWtkZUnmJBA2VnChJwrvF1+4Ncmxnc1xcLg98Oy8L9AcRuKLoKmAiR9yamkK29z2JiufCJjKnQpzZbh21sl+6d7kIxiYJCi5fNgaOM1uDTAyu5epuOU6SJ8fxGn7npmb4AZFPsNlXcTChakbv/ts4Sf/FxUJqxox8PriRy0ZlUtwXG9QrYhVzlbCCNuUhlhB8OAZkfbKQcln1uf11QKdXwV1jdLFiWG2aq6esSAXK+8rEIlPnvLOI9Vkicvx449ua/7UNq3b4iPpMI1vn8KL4CoJ6+MUM/MCUJZoXqKwQtG8SiAKlS/Lt2hhbNoYAmlhTvho9iyY51PdNW3czVk/0nhGTmQBT1FcotGjjulAszXqojgB1hhuOBQ0g1ehKrqptGUeov9p7xH+4oC90LlT1S6pZAWKtKCFQhjS7dnUt0c0aL1XCmmIX28qNqLwVdNJtPAdzCTV0YnVT+AZ162g73bjcaKIJTircWAKFS1Xmqu8Tzbg7lzw3sIZ8D3Xk4GZpGmvLFJbIWGc4G70x3xF9I5pvKEmrmwrwVWrp/UWxwUqvE0W5fzVJkSFX3nQbn4MqOVwVg8oupdDLeBsdnTaftT9XoPWPyD2CxXP1V7GwSe/kKnxsk9Qw5qKg4h1hDb4m0KlW6VES7FTrkbuOyj3b9Huje/b6cVUriwhDsA+2SOcDtqW3HP4Wq+IlubwaVauG8xg5S0T8yVzYdXIG858mwuurJyGTTPqylzFf4Q7zUIOeM++LHSgKzUYVE8wjACkRTFW8xwDhBtMfTzwswC7XuxGl78pcOyiErOk+a/KSHKk/eEZGWA60VsLXqerIY+3exKksOdUBjbRnXb9etH6g4toLLI37jLWXpo C4ch1A6H vyWXDN13sWWxY7G0vwa+c4fqMpl/i5R0YJzJTu2Q1PKXnQQuj409+vLzPvSOrrTkpzXR3jsI9dfoZandZtvmXPEwrO7VARPNL9NKcO/erKIzZP1QiXOlWYP6b03Y5av68VHLdsWa8pwI/XibPq6EQCU0t32uiIPxPh1rg86B6MVHmhhlV0MDBCsQtYRx2TCuOb+HOxGNb3qpUGztaVAnlqKSYX7olL3dzaXRoU5e2vbjf7XN8KPnL2SwDzZ6vjtHG86a5IFUAAk6oci5qZU7v84v4gVow2nUBCL2KOqale/BeqBSAihSgIWXEbJ5uokbHNJA7cpJ25TTvzQO0KpI0IJVcQzAhYeMpqV28iM+0qe5E5WnSTi/p0e80yXeeV3gR/wIt9Ui3f8ecgIlSRz10OvbJooc2/qa691PGUqYycz3sLov/rS24DeEmDgH7VwnoYVp1Jj9bQz0v0OhZ55qIqxtYyqopyO/fS3yDz21riS3rSvasJqmeHE7FQ6NOQI2b7qMgIgA9LnWdbbPMODROn7oN6Lo9l0kIW8/SgIlVHgFH6iw= 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: Kernel drivers that pin pages should account these pages against either user->locked_vm or mm->pinned_vm and fail the pinning if RLIMIT_MEMLOCK is exceeded and CAP_IPC_LOCK isn't held. Currently drivers open-code this accounting and use various methods to update the atomic variables and check against the limits leading to various bugs and inconsistencies. To fix this introduce a standard interface for charging pinned and locked memory. As this involves taking references on kernel objects such as mm_struct or user_struct we introduce a new vm_account struct to hold these references. Several helper functions are then introduced to grab references and check limits. As the way these limits are charged and enforced is visible to userspace we need to be careful not to break existing applications by charging to different counters. As a result the vm_account functions support accounting to different counters as required. A future change will extend this to also account against a cgroup for pinned pages. Signed-off-by: Alistair Popple Cc: linux-kernel@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-fpga@vger.kernel.org Cc: linux-rdma@vger.kernel.org Cc: virtualization@lists.linux-foundation.org Cc: kvm@vger.kernel.org Cc: netdev@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: io-uring@vger.kernel.org Cc: linux-mm@kvack.org Cc: bpf@vger.kernel.org Cc: rds-devel@oss.oracle.com Cc: linux-kselftest@vger.kernel.org --- include/linux/mm_types.h | 87 ++++++++++++++++++++++++++++++++++++++++- mm/util.c | 89 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 176 insertions(+) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 9757067..7de2168 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1085,4 +1085,91 @@ enum fault_flag { typedef unsigned int __bitwise zap_flags_t; +/** + * enum vm_account_flags - Determine how pinned/locked memory is accounted. + * @VM_ACCOUNT_TASK: Account pinned memory to mm->pinned_vm. + * @VM_ACCOUNT_BYPASS: Don't enforce rlimit on any charges. + * @VM_ACCOUNT_USER: Accounnt locked memory to user->locked_vm. + * + * Determines which statistic pinned/locked memory is accounted + * against. All limits will be enforced against RLIMIT_MEMLOCK and the + * pins cgroup if CONFIG_CGROUP_PINS is enabled. + * + * New drivers should use VM_ACCOUNT_TASK. VM_ACCOUNT_USER is used by + * pre-existing drivers to maintain existing accounting against + * user->locked_mm rather than mm->pinned_mm. + * + * VM_ACCOUNT_BYPASS may also be specified to bypass rlimit + * checks. Typically this is used to cache CAP_IPC_LOCK from when a + * driver is first initialised. Note that this does not bypass cgroup + * limit checks. + */ +enum vm_account_flags { + VM_ACCOUNT_TASK = 0, + VM_ACCOUNT_BYPASS = 1, + VM_ACCOUNT_USER = 2, +}; + +struct vm_account { + struct task_struct *task; + union { + struct mm_struct *mm; + struct user_struct *user; + } a; + enum vm_account_flags flags; +}; + +/** + * vm_account_init - Initialise a new struct vm_account. + * @vm_account: pointer to uninitialised vm_account. + * @task: task to charge against. + * @user: user to charge against. Must be non-NULL for VM_ACCOUNT_USER. + * @flags: flags to use when charging to vm_account. + * + * Initialise a new uninitialiused struct vm_account. Takes references + * on the task/mm/user/cgroup as required although callers must ensure + * any references passed in remain valid for the duration of this + * call. + */ +void vm_account_init(struct vm_account *vm_account, struct task_struct *task, + struct user_struct *user, enum vm_account_flags flags); +/** + * vm_account_init_current - Initialise a new struct vm_account. + * @vm_account: pointer to uninitialised vm_account. + * + * Helper to initialise a vm_account for the common case of charging + * with VM_ACCOUNT_TASK against current. + */ +void vm_account_init_current(struct vm_account *vm_account); + +/** + * vm_account_release - Initialise a new struct vm_account. + * @vm_account: pointer to initialised vm_account. + * + * Drop any object references obtained by vm_account_init(). The + * vm_account must not be used after calling this unless reinitialised + * with vm_account_init(). + */ +void vm_account_release(struct vm_account *vm_account); + +/** + * vm_account_pinned - Charge pinned or locked memory to the vm_account. + * @vm_account: pointer to an initialised vm_account. + * @npages: number of pages to charge. + * + * Return: 0 on success, -ENOMEM if a limit would be exceeded. + * + * Note: All pages must be explicitly uncharged with + * vm_unaccount_pinned() prior to releasing the vm_account with + * vm_account_release(). + */ +int vm_account_pinned(struct vm_account *vm_account, unsigned long npages); + +/** + * vm_unaccount_pinned - Uncharge pinned or locked memory to the vm_account. + * @vm_account: pointer to an initialised vm_account. + * @npages: number of pages to uncharge. + */ +void vm_unaccount_pinned(struct vm_account *vm_account, unsigned long npages); + #endif /* _LINUX_MM_TYPES_H */ diff --git a/mm/util.c b/mm/util.c index b56c92f..af40b1e 100644 --- a/mm/util.c +++ b/mm/util.c @@ -430,6 +430,95 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) } #endif +void vm_account_init(struct vm_account *vm_account, struct task_struct *task, + struct user_struct *user, enum vm_account_flags flags) +{ + vm_account->task = get_task_struct(task); + + if (flags & VM_ACCOUNT_USER) { + vm_account->a.user = get_uid(user); + } else { + mmgrab(task->mm); + vm_account->a.mm = task->mm; + } + + vm_account->flags = flags; +} +EXPORT_SYMBOL_GPL(vm_account_init); + +void vm_account_init_current(struct vm_account *vm_account) +{ + vm_account_init(vm_account, current, NULL, VM_ACCOUNT_TASK); +} +EXPORT_SYMBOL_GPL(vm_account_init_current); + +void vm_account_release(struct vm_account *vm_account) +{ + put_task_struct(vm_account->task); + if (vm_account->flags & VM_ACCOUNT_USER) + free_uid(vm_account->a.user); + else + mmdrop(vm_account->a.mm); +} +EXPORT_SYMBOL_GPL(vm_account_release); + +/* + * Charge pages with an atomic compare and swap. Returns -ENOMEM on + * failure, 1 on success and 0 for retry. + */ +static int vm_account_cmpxchg(struct vm_account *vm_account, + unsigned long npages, unsigned long lock_limit) +{ + u64 cur_pages, new_pages; + + if (vm_account->flags & VM_ACCOUNT_USER) + cur_pages = atomic_long_read(&vm_account->a.user->locked_vm); + else + cur_pages = atomic64_read(&vm_account->a.mm->pinned_vm); + + new_pages = cur_pages + npages; + if (lock_limit != RLIM_INFINITY && new_pages > lock_limit) + return -ENOMEM; + + if (vm_account->flags & VM_ACCOUNT_USER) { + return atomic_long_cmpxchg(&vm_account->a.user->locked_vm, + cur_pages, new_pages) == cur_pages; + } else { + return atomic64_cmpxchg(&vm_account->a.mm->pinned_vm, + cur_pages, new_pages) == cur_pages; + } +} + +int vm_account_pinned(struct vm_account *vm_account, unsigned long npages) +{ + unsigned long lock_limit = RLIM_INFINITY; + int ret; + + if (!(vm_account->flags & VM_ACCOUNT_BYPASS) && !capable(CAP_IPC_LOCK)) + lock_limit = task_rlimit(vm_account->task, + RLIMIT_MEMLOCK) >> PAGE_SHIFT; + + while (true) { + ret = vm_account_cmpxchg(vm_account, npages, lock_limit); + if (ret > 0) + break; + else if (ret < 0) + return ret; + } + + return 0; +} +EXPORT_SYMBOL_GPL(vm_account_pinned); + +void vm_unaccount_pinned(struct vm_account *vm_account, unsigned long npages) +{ + if (vm_account->flags & VM_ACCOUNT_USER) + atomic_long_sub(npages, &vm_account->a.user->locked_vm); + else + atomic64_sub(npages, &vm_account->a.mm->pinned_vm); +} +EXPORT_SYMBOL_GPL(vm_unaccount_pinned); + /** * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against From patchwork Tue Jan 24 05:42:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113483 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 A9E8EC25B4E for ; Tue, 24 Jan 2023 05:43:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32A016B0074; Tue, 24 Jan 2023 00:43:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D9416B0075; Tue, 24 Jan 2023 00:43:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 153036B0078; Tue, 24 Jan 2023 00:43:55 -0500 (EST) 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 072356B0074 for ; Tue, 24 Jan 2023 00:43:55 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C3AE8C0533 for ; Tue, 24 Jan 2023 05:43:54 +0000 (UTC) X-FDA: 80388601188.07.45C1494 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2086.outbound.protection.outlook.com [40.107.94.86]) by imf11.hostedemail.com (Postfix) with ESMTP id 0931840005 for ; Tue, 24 Jan 2023 05:43:51 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=tLe7CFrS; spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.86 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); 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=1674539032; 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=ZcDnfPrIlRs7Mnv0oLLyOKfuvKifWQZHkSFcnl2A/vA=; b=o/WVJgq9ZMDHOSy001LjUBaChvsFinQ3iJgNv+4WIIniaA5zWoEu93lfM2pv3zgKKua/xc H3PfQyWPSr3hwrnPL7TXqQgWlJaapDu1aMEfB0pFH2i1P2VIvbtF6Jjws73bhvkvHo3rhm oLu7eAbtpO7dkCVws/f+ghvUrMr+Kh0= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=tLe7CFrS; spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.86 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539032; a=rsa-sha256; cv=pass; b=dTXRRVRuEsUPU79RYsa+nE+T1Nc7eZGdy3LRCquyS4d/577IuCSKfwnfTHluPl9cte9WLK wQ8hqHwaWO1P9qU46ixnlCspUhq40hiNFCqRziPPoy4+iaHakJrRO5z7/hPWg79GtFC1Bf n10uHYcol27KDyVTY8TneVnZSqcuAUM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eR0otRmxxyF77ANJ8b50GBHj4vsyT2NfjQPtSoyqrbkZ+M5wQBtlave1j7dhgN/HrUC4m50PVqA6ImAEMpCSbLpZWomXGlUgBhALdLrinnoQ4vy2807/ySYuUkMXJ7Bftsd9i0D+UsKdG4IhsMlUkR+qFa3vI4Z3JWeLB++xRDRgVowxbA0OmrlPW2E6iP5agD6rQ56npJ7hiZtN0hIAoxdHMjjoToYpSI6ZlkdOrL0fSwGF2++A31F7qPVgVHk5vhJZEONw/Nr4BhOYQALzmItBx8fEx6ryFI1TR9Fw9YjLm0mX1KWb2M+w7YP22Fb+lVeg168c9YaQQ1yzg5tjSA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZcDnfPrIlRs7Mnv0oLLyOKfuvKifWQZHkSFcnl2A/vA=; b=CllDKvmaGNEnyqbA7IIvt28n1iIzVMu5iyQ8bk9jpJ6kARDQOMc84ojnIEdMQGKZ3FfrwXdpCawH3ACxC/o+B4R/gIzdjQR2N5SH9kXwnmd9SOhH5RIU2n9HHfxko5WVohB5Yk3hdMINz11e4RiYnMW2ShcyNKCtjEaSyyJiO1lZnYR27Y5O8kcdZqH4eNRj4tWzCwL/MFp/K/gSLeR5dj2UzEh/jlffhgEa9kpolg6VJhkUHFj5kzVotm405cpZHyoGBg07nN9/bjhY0M/svzam8jxF16CrJ/1Nu5XUzZoDjlRsMYmzXwndp9RwBbzMMZhpY+Em5rhAju3JNc2G8w== 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=ZcDnfPrIlRs7Mnv0oLLyOKfuvKifWQZHkSFcnl2A/vA=; b=tLe7CFrSVMWCB8G+nqyccZlJhPmT7dbtlKjpnf3yvgmdXD80GCPvMd/lAxTfZ01VmOzASeYDf+MSRjcmEAGoc1pQNozCLV0YPuQMbLKXn5QIQhGW9U5JMRfuYTWMub1KG+OBPcAFxWnAo9bKNhh029BZSLqrG6XbmENAa1wd7XDUp1+0oKRgD/AJuWxUmOSTsdaxFrr9dyIOoSR69ufqmF9fbi7Zas0Z2DVtqXVXtm08IIhBr1bwc567WSPtNb+osXjZmDStJ6Zx+bnK/zsU9UWANFfO4DKiSbl0boO3DYaUy5AcJRvSMqY8ThT9xLLZSxd0u4D70UCKcwa1e5Hk+w== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by MW3PR12MB4540.namprd12.prod.outlook.com (2603:10b6:303:52::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:43:49 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:43:49 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , "Michael S. Tsirkin" , Jason Wang , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [RFC PATCH 02/19] drivers/vhost: Convert to use vm_account Date: Tue, 24 Jan 2023 16:42:31 +1100 Message-Id: <97a17a6ab7e59be4287a2a94d43bb787300476b4.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0182.ausprd01.prod.outlook.com (2603:10c6:10:52::26) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW3PR12MB4540:EE_ X-MS-Office365-Filtering-Correlation-Id: a9a85e3d-854d-43c7-cc27-08dafdcdf7c7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: abJUjNf4awkT41lafLH+TcN3QopmZ25FPGbBkLfPop7ZB/4n6xB87F5+iUBoBFW8OrANSx0ohukUYsTbDavg9Tduk/7nSAAf3y5F8Z9B/ts7WBCsRe1oyEnJQ/H1thr7ZerdRyXN07Ex847UQUmsCpdIVMLtGDZcvTmZ6TwocH/EKxnybyrCWpe/xEN2VNJjL7E7VeGSIQ1M5treCLZnOEKoqhlNlZ49ocpFkT1A4LldUEuLoBt7eEz6oQByZVGsuSrm1HzQoFwMVMa4E6tTUQ8H8yPhiUVwDliFBss3iQI5EHGXUwT8ndcnCfPZeAgsCu5Nz3i30NpZiAZI8cDUbBzpb2Y0Qllxz7FhrjR5Q6GK3lJwBannxRcQCLJYcXl/u2t5gjO052OhSvf7b+aiLF4rNIVaRM+6+TlMyALsKPmdxGvVMoDzRxOAk+DnZeoE+TOd0oUSzh2b1BhpVc6kaiMxmugeEGy7sw+8xVWN4bfVHmcwD1y6N/GIzSEztYCSfrTd4DU+aprpkXAIE2m7gMzblFu0su80dIg9DU2nZ4LEnIFR9sY4aRWpErXxQKSkwgBW7Xo7GMW341rXbq06DxDTT4UUk7PV5iW1ZpP5sRgdNMGlffwrBk64N8472KaYe8iTJdiLCngfAi+dZzEaYA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(376002)(136003)(396003)(39860400002)(451199015)(36756003)(86362001)(5660300002)(38100700002)(2906002)(4326008)(8936002)(7416002)(83380400001)(41300700001)(66476007)(478600001)(6486002)(66556008)(6506007)(6512007)(8676002)(26005)(186003)(316002)(66946007)(2616005)(54906003)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4BM9nFVTTns4uRS+6HT2HmzGTbfRAHJVsVuI86uynpag9i5ELdLIPavTroPCX6TFDa0Kb092p1Qy9Sv8WyuE6rTacn+Grv7TJ1TJSkbaBkJ8OsOmKiTFSM1Pr1JL5XBfWNziY52Q3zp5vo3yIbgLOwC4Sl8TJxdUP1ZG1zWjizqvW8LagcaZUW3kfCTG3YsGt9OY6Uo4wYreMlTzZR7MAkCpUYis4V6wDEsrQ++Yfc+FhKuBT1Y2vXJOwNc5AC+PIStbdST/gfbIBK8h8dlMLL9TKHwbHLVKbI846uixfemPXDLIxdDVzNggiJE3BacPCz34XCXH0lwGzFZWN9JPu+GaUqZSEZvffpjLukuthwA8sC/f3eXkdUkUVesS9AZyczKUkssJPb3TE/I7rpOEsCRRFfbORd3oBiygPALNaQc6QT9HAqN7oUS0rhCdj3FP1RkWXUAux+wJpAmcGmQ0wQh0TTtSEfwx1CWDD5tUb3ads41sf3nyD3jsMlpgNFIhnfrskSf4wFwzaLvRL7ykj6AtmEqDIJj2C/1ijLtUxooZdjFfHrcWBxI7KHRAjAc+5sZymo5657ySdGlRogv7+DDc+AmJYm4BElyONppJa7/2n464DQ+WviGT9B8Cx1/o0TAkrNiGBkqP/LtY0ZwZyCvBshj/fzeIa846zEYSzt4My9wY6c1ZgSvdShElEovnV5tmgiknXweiRQTR1QwB16aw92nCG/jsiMddsP/vvoxg20XExbNPpcOnOrLivl/1n0jEu4O6rHaX7ueueZE1HHRcd72Mj+yyJdIj68zZ29ze9e8jfsQ/hR8fnPIoV0CDIxRCnsHOy0mCCdt3Cai4d52EDjSPyB/Nifhuqsy54D1UlF41RdUAienT8K3mtEdOVZTUdWA1mJvS/+7xRCBh081u1Tpcb2WbLbSqvTHjLynMxIT9MHdXu32jpxtV9pblDHORTLqNh25IjjRVGsE1/QOW1/SCpg34UZgBwEQtQRD2EiTXHUom1fR9KGkp+NeynVH7nv/0t+uO7T/iULf/9iX8w9mILUPxnXFarCgwi2X7un7xMv3LA0coVq3610mpnBsE/hB/UnHFw+RsgW9AKZD4i29z4MYvE7+CvvtN71zyTCGIQZRd5+IxuHCTpJdcQwRHj+0e2njpDYKugsxAyrj2LyMO1b6Pu2C0z+fISWLhKSoVhYiZNjFpnALisbcq19GVJMk+M31iOaGcntqXigMlobdTDBBIShm7gnnCHsJc5sx54K0my8gjH/39fPoR7bKG/ALu1tYv+WWr/+ApylqFxc8IfIZn9PTXPawnc47SjdkOZN+tVQeGVdutvT2AF21rO6l27auVVYb+Du1tAOJ90TYkvkUforNAIUsBT3QUjvf8hxJ19hExaaRT+lUx3Jq6p9lJU9m7RqOlnpNZfFB3PGCo0uT2uaJhoyG7ed+KPoD68u8uT7x11jdR1zS9lgXAdL/RIHEcgb/dMkc/BMKM8ZoRYkm/mpZHIalV447dscCiQ0Rd/BFH7qe0B8ObzFHAoKRPIb6S3oi8So9A1zwBkBvJvRJB4uPhKTmw9X3yOfhQzB6Q6B30aZPYKrKY X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a9a85e3d-854d-43c7-cc27-08dafdcdf7c7 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:43:49.6973 (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: LLlNevmybuGTYgjGjyYs1bJEs1xvf4XnkOdn9RSNGMI2+3s9LW8Hoccklmql4Rpu4y+wGEJl5QdOGw1CxjbMuQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4540 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 0931840005 X-Stat-Signature: fe1eyb14yxrp4fdnrnrbfie8jir7eiom X-HE-Tag: 1674539031-880269 X-HE-Meta: U2FsdGVkX18oLFp3j1yHqg3NhyHUS/znyloX/MTiuZGgKqlqCpGjOK6wHfyOdpgZklbSutPbFwxMZS1u2Js67fnKMKEmIodkqkChXI7tKE0Ru6XKgpePamoR2bcAgsylIMpOR6w9EBWEzstQZ2nbwu8ZTzK20Q9TRHfy0XyZGNVfSpmkKqGaHTiGsy7voAz8RSgD8XEnBGLL2wJBF3gkDhoJo1uVszypL5SQbckwVZ0ucNeISQFpCnyffzMo43gJjbyXoIpKzQ5+sRckidZR7r8OVqXCFkzNzDYBUEi2hi83o1iSnBzF+NWfr6BjI+uuUq+DyTAM52nH5BdY8/fNLsaUjwH3EjA2F0Q8fvn8WMk4FgfqljemTYGU0NS6neMgGVvbe3ZbP7JZGMSCmMwIVbOhr5AJwJDvpUpsPKeYGKaKh1ZN/BMwqYCoP5oOxUBqibA5vw262n1lktRXs0nza6gfYcOepmPKW795YPxxUuN1EgIlTvVcWDppW+vV2PZPFpZQ3OqMuNxdMYNqUEuNR9vJtd3pb+A9ydeGuRkTdc8b78VgUP6yeELAfER3/IPNgsJAfqaLjtyMz5uxDWGtcOAcRNkY+WPPaGAVHX4oAbBSekIiGfsbIA+URvPaX6dQTVjbbR2geA4hmRQcbgXUB+Rz8RaS0v3qkcwtn1KbXNfsWIw73JcAIYrB5In8FYYnN/GlfZ9QJTdSR+q/vNpgVLE/XNcBUjnftwrJs/7tksdMQKCQV5p0oOnW0QAn5WWg66BOMMc9yuvc6zcjvNko0FAJbZZKFccOk3Io4Ndx+1Ua/oknHHq59CER+N+6DYxuaZkdkiHBMIKY3rU9pFF1sXyXT0hF7A1+IfGu/xxaF640dxl5yGJ0+N+3tMGDssMtpD71of1BDV0GE8Q3LzrofsLsE8XL9shN+bO9CdMLyw4S/+6K5JEi18q6F/Czlafeyn8hrB8+3L2R87drfTx EumxPPT5 Sg6+p9mt236B/NR7ZHgbeKaRqt+YvHOKZ4Z6fUb9hoCjAK1L841pk2oa8TnlIb4vEhO7pp7+gXgej4wcWD1FhVjdX38UyoZE9nn6kNRnXwaJikN3pmLrS6CbHWPXI+XwUMd/8rMVL4M1853YVIcZp1yDfVbtzfqvOG8wwjUZS6tuxi75pTHUvV0XR/HKiWaNWPoF7M1n4bnP8nyUIdPP2m/LtLqeV0quoqpzsaKq+G+DYOAW6/tWiGojFe43rVNvl7ZNXaU4FazxmtVawvl5MHxo/83wR8LgT9Quxrq03jsfYbIbxsglmstSpfhoRu7EuGkdZYEh10i+Bffcpcd1Ohqzsu9T3uZGO1AOBB8Iaxjii+yAIiK045CKKP4tqojwBX+GH+vBdDiy15GkFXl8sHG0XBhV5Z3wLtuuPumtVkQqHUAqrZ227KbIPng== 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: Convert vhost to use the new vm_account structure and associated account_pinned_vm() functions. Signed-off-by: Alistair Popple Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: kvm@vger.kernel.org Cc: virtualization@lists.linux-foundation.org Cc: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/vhost/vdpa.c | 9 +++++---- drivers/vhost/vhost.c | 2 ++ drivers/vhost/vhost.h | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index ec32f78..a31dd53 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -716,7 +716,7 @@ static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, set_page_dirty_lock(page); unpin_user_page(page); } - atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm); + vm_unaccount_pinned(&dev->vm_account, PFN_DOWN(map->size)); vhost_vdpa_general_unmap(v, map, asid); vhost_iotlb_map_free(iotlb, map); } @@ -780,6 +780,10 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, u32 asid = iotlb_to_asid(iotlb); int r = 0; + if (!vdpa->use_va) + if (vm_account_pinned(&dev->vm_account, PFN_DOWN(size))) + return -ENOMEM; + r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1, pa, perm, opaque); if (r) @@ -799,9 +803,6 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, return r; } - if (!vdpa->use_va) - atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm); - return 0; } diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index cbe72bf..5645c26 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -556,6 +556,7 @@ static void vhost_attach_mm(struct vhost_dev *dev) dev->mm = current->mm; mmgrab(dev->mm); } + vm_account_init_current(&dev->vm_account); } static void vhost_detach_mm(struct vhost_dev *dev) @@ -569,6 +570,7 @@ static void vhost_detach_mm(struct vhost_dev *dev) mmdrop(dev->mm); dev->mm = NULL; + vm_account_release(&dev->vm_account); } /* Caller should have device mutex */ diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h index d910910..3a9aed8 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -144,6 +144,7 @@ struct vhost_msg_node { struct vhost_dev { struct mm_struct *mm; struct mutex mutex; + struct vm_account vm_account; struct vhost_virtqueue **vqs; int nvqs; struct eventfd_ctx *log_ctx; From patchwork Tue Jan 24 05:42:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113484 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 B2AD3C25B4E for ; Tue, 24 Jan 2023 05:44:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2173E6B0078; Tue, 24 Jan 2023 00:44:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C7FF6B007B; Tue, 24 Jan 2023 00:44:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 041686B007D; Tue, 24 Jan 2023 00:44:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E924E6B0078 for ; Tue, 24 Jan 2023 00:44:06 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CD84F403F2 for ; Tue, 24 Jan 2023 05:44:06 +0000 (UTC) X-FDA: 80388601692.16.8B3CC11 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2074.outbound.protection.outlook.com [40.107.95.74]) by imf04.hostedemail.com (Postfix) with ESMTP id 1311340016 for ; Tue, 24 Jan 2023 05:44:03 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=LzeyY9O0; spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); 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=1674539044; 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=Lj03mWpjbAOXnJWeiB3fprYyHO3IklPc7NSwtjAmNx4=; b=68Tax8rHaD2t/7o6tk6K+6RHgtwZQO01622DzS7gzL9ASMjr5E+ku65nyw/qT7cjRRam3s R0U15vzDrYj01vzsHSLJyJTTeewIE2BF6mFsdRsZA2COdJAHk3ly9VY/EUOgNybFsWJues gK0eKdlV0lfqbieYSqnakoc3sdt1iJE= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=LzeyY9O0; spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539044; a=rsa-sha256; cv=pass; b=CCH+o3d+EbSHLMgjhbJmhjVHPRzD040RG5yYiqPYU1RvjG2O3MUUS4MM0UlJO+t3pDZoeH PH2OZuBRoFWKbq50IfzcG0c+HKQTfFHDt02w8QXKy89gXsZtZlcQN3Mi5jV8TOrrCgaaEc EwhbJt1od2mOqDGDLle+Fh2wIwCnvJk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JUGDgmtl8dsT/u9CvbmSWFgb7U3oMh5dpC/CL9vTtLijmPBCNyUaKalNxiaDwx+U3gGUQjHslA8lca2fx4GPMSW/ceFgArZtfi5JFMRzp3zKTQ8d//DasO2csAGMKPa7MIoTMKoGqHZT1CjACVCqwJNUNlM7tCTzp6d4R8qn7OdMuTFhS9XVxeJi9ZCixH3Lhn13RIu9Z+bOO1X7g4w8zRoNY0ROmkYbLGNI/dh9TIbBezhyUDOyhi3qrYgghUcBPo8ErIghrdZAJYsq93cuvscKm1pobq/A7DWhs0QKse16kZ2g7oA53DJ0HqlCGZ1FWSsSRonyuUOQyKE+/mbgrg== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Lj03mWpjbAOXnJWeiB3fprYyHO3IklPc7NSwtjAmNx4=; b=MBtJ7D3DkQ+7V4GjhmwEI5eSaKA8rPxVSgHC7hN0bOncNlO/m4QHHH6S0RgcP/SZsgMlIhxP1sWzhY9k1mSGKRFZXqlMMTOIZzrk0pj3G/VhgRNoV3xUobhegPaiLwllNMCUsuWLIuqjunHbs5gP2SYribCvtt42iOGA4TyPjsQHgAELeq9X6pe6j8UBh6v+VmbQj/IYXXGCZNqzftiSdcP+4FroE0+xeIJ5rE/Ir3kEQo9OmSDwcnlIBZ84ipKwPZR9BLWUNmP3O0o59GR1/jI143JrTvITwMIHASbM3xCxoqPZXMG6V4S4C8Vtlottbupdk+OYVl/XRTPxSbxeOQ== 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=Lj03mWpjbAOXnJWeiB3fprYyHO3IklPc7NSwtjAmNx4=; b=LzeyY9O0+J+Ac/U78/4IatA7OJOeOWnXD//j74OXheYmfCQ9eT/hMwCD/3nPYnMZ4zpWMZrcvyMdpoaIT/0vM7OaYlTpkl1JMvO+1I2wrTSusoKtk1COMCzG7LOx7yscRyYRF8XIgMDYs3MyVDTKXlEup8RaYOgF2v8vMHgnrAfd0rzd1UB7Lnm+/MZqpQjdh8vN3h4WnZjuuf6yVvWBXii2iHHvXVmlctRAweN9qN6BvGuiA6gM5huoPUNg8qH+tvekIyb5i4hrAsAlSiTJgtUXxo5fehQU8E0YIgv1KoGQFk2zx4oBYpyAU1KEgOR0h8M0PslFl0ju5WJKj5QzCA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by MW3PR12MB4540.namprd12.prod.outlook.com (2603:10b6:303:52::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:02 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:02 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , "Michael S. Tsirkin" , Jason Wang , virtualization@lists.linux-foundation.org Subject: [RFC PATCH 03/19] drivers/vdpa: Convert vdpa to use the new vm_structure Date: Tue, 24 Jan 2023 16:42:32 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYCP282CA0017.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:80::29) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW3PR12MB4540:EE_ X-MS-Office365-Filtering-Correlation-Id: 24006492-bfad-41e4-3b6f-08dafdcdff55 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Qtu5lNwzDpfFSTZfcYElaRJuqPb+FfsC7U+LCaFWASikDq+74jIAFIeYZ4pF8NehbZANRomB0wbU3i3hm7buJFJwUl2kORfQymkvtAjL0GMSA7114n06rO5J3iAvPKrzMTqzrWMRcer1kwdoU9gQGOpJu5Uz8BfMMAByr3RySGYSl8HSCcs4rprPlDt6YseBCnuYiaKkwd3uFtfjy7eC1nPwn7PTQcMx8AvklFHs6+/oRM+85srmwpNs666NBXk3pQ/3u+fTcGx/E7/nFKvUNPf//0aWh6Z3Sd17+sNlYQUBtBHD5epB74h7E+c+mKQFZ9t1VUkmDOfH+smvC5NuhqfmhckjBWLe6mAUAYa8X4fjCp6g2u8HN4JKZGs2GEqRywsCOsuWOnWeuOuwp6NbkIvRJzdUfVpqx0gR90lnQnmjtxpKrZkd9R3pOs7kmCRb+bHTK5zI1YJ97AEuFO301Bhma40tJ0JeqlbX5XyavRmnFpUx0p6TupohTFh3Lh1+NmmxV05SpjJ57B5CSI0j03bhmvKWmQ4sBUcZrhDxqsAzoWO6Uw+/msVJA3Mbn+5VdkJQvHOTAalIV3YIXa1Oxnh9vIzonfbu5XCY9Mynvfawb4rkfzT7HkzMFJ5avVQT5THg0Dp+6ja8LqMEtEPPgg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(376002)(136003)(396003)(39860400002)(451199015)(36756003)(86362001)(5660300002)(38100700002)(2906002)(4326008)(8936002)(7416002)(83380400001)(41300700001)(66476007)(478600001)(6486002)(66556008)(6506007)(6512007)(8676002)(26005)(186003)(316002)(66946007)(2616005)(54906003)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 09jR9PI1lj6ImPdYgdurnvVWyQmwF8yK6cqvkXMJQcMaFLpkefqMWP0YwvN/bQ7qAPruiAskTOlbhi0yL/aKQkuL4OpWauD9ew09ROBuRrwmfefncraB/ZgLRJXVZYuG4jtuWoWf7CbY5ICB5N/+wRZbMun52SRqnEcH6qffqfRvfZGoOkiDixWc2AHy55Qh1+oFj2GrrSeW/W7BPN5SZh0AUeixCj0TE5W0SC2zLI/u8+CDQ3FZJq4vxjUd20eG7RFt6a/rlQ3yUOxhG2uCCDfnUolx3c/ajFx2f5JaI23NL6cx9cepRhmNblCqC+moPnH+7BD7FQfiZ5Ic4eOGz283VpiRdE/QGZbLeDdT2CyStyI6qHbF2gCVjVXEs0rbrtwXFT2VVZ2RWuwVtb2K2cfWXMFTbJerEJfiz43Tx38pwKawOidmKYGLPMVBjqRdqpWgT4wM3XMrnfayLke9D7rxDkePEp0g1t+eX34+i1Brt52oGYLxW8K1GyJw6dBRIGPMROQHRu2bZkw73AnCK00jVczS2rkf4nWd+qDF5clgBamCCuElN9spORSSwmQxpoytdv5TbjVuboNqw7zeUf/tr8jwEi/zOfbha8IwjPCBM6RG+jC9/P0JAg0JAT8U64DxI1sRFrKNC19GSxLIUiTvwlsSdpurFvO3ZHz4rDb7wyxjzo/FxWIeBG8RVtQ3GiI+n/AUPEbiVlZPxEWXMrMZ7xSz82pm/WLYxSIjJKtyM32/hF+ketZBp/K/JJDDRMlmseI3iPfhJcIw6uex2BttOr1aQYfSK8Ab/+ianeC5ltt3cBlV7J+2WBYTF36XbewYohgNgcn9ZI53HAxSKyP4zPJQGy1qTQVBoyMpVshPhSQ9MhtulEXfP2w3DaVE25riRGUhT4obIxlHnPIMCzLrQprppCJ3v8H0M+QUlo2YJY2r54ChOIBbjh1SDgGn2ogzBhqoVEP1W+0CG4nJzMDprOvVsU8XL7UqX45vn8TmzAty61KPv3Cv3LW77la7RXZ1IHVv3Z2H9LRswzJdk23LjqN9mt/7sviePGHxvR18oluPx9hw7BTaNiPVJIxCaEHk8tbssju1T3/cnkz2mD4XmIznIjTksLsTLrWDTRWiAiF1Tfzwzb5g+yVFbGCMoygp9DDeBM/Bsk3Pm8mYKsI7pGG2TBrHcp9Ny+I2mnZv+5k1+C89ezxXVoEtLFEQ1Yx9/OrXJIniTaQEN3ZTlPB8r5WVhDEXzEa5E1etWBa+EzCuDvLus4CimprWb26Tmy097Npg2SIlK5KhSewS/4lFtu4We+1lhccAxAfPyN86gHgmXik2Lsyc/DaRBcnfqt3vMy0uk59nrdGTwyBBP7f3Tro1WenmemEXHGgtvI89a6pZWEE4gwMmgBPau0HATidbWMRha/6Wz8S7j1/RgDLtvt4LogZnAPJjbnH+4bSxEPYE6KbJFBRyGS5DvIFcU3e5brSll3emglpCE6Oc9UYYGTe4pT+yfP8Riy0LKuJRX+74N/Kc76zrjvKzJ6v3Zz5T5meJaqGwn4wfTyNnjwBApULOiMHqX/yh9yF9L2B8/T8IJqNNbu62pUvuBE4x X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 24006492-bfad-41e4-3b6f-08dafdcdff55 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:02.1963 (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: r4VZpmcdkvheaXLTWXT5dYSDMcC4hIHru8WPmM/kXvTHe/xFHI7/3hanI9zuHmZSP9tKv4v17io7kN11YXB23Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4540 X-Rspamd-Queue-Id: 1311340016 X-Stat-Signature: 6f1c864t4b6sjj3jyiwgpsiawanqqp6c X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674539043-817595 X-HE-Meta: U2FsdGVkX19CHppKkRn2LMxYoe09Er5DxSfGbuGjIWez+I3qKnbgggIKSI2gMtdFL28Pehe1yCIuUcjnGiax+TSjCuQ2h51Z7EIPdnpeuxs4TF1bqZFrvz/qp4BMbdxja4HWl1iu6X5mMXmdSUDWqCwsJO+bwq2K8TdTUpZGV1mhow7FfakQVrxxXflLwuBjJVrwMwu7zwhhpNDIUnyRQHRph8pvBaeO7xmHjh8OH8C7jPL3s2Tkj/S9cvZDeFgfuCv+bucsemt4ozXtdv4HCajag56G05dDV3+oYVsj+yrY8m3vQo0wl3s/injqMo81edcg0zGARJe8LDLL4tWSRaVVcRrx0+v5dyPF6Bq96R5ZzPMnjRetJfiKTfuY1pSQH1UpRgmrt6xiI3gGyfQ0AsRMKrbqVP3ghox0Xb8LkRz8UKOa1WNiH21T5APW99UBwenbUD8tiBIM4GmVsC4OlROcUR/SVjlBzaBHZVrbn+HPTJzPXS21zziEYQ4p9iu1svnmcMfJY7oe+f+CocJIqcpNkSYxF5VCZRo8ZgE2RYUETYmbTn1Ctw0nciqmtBmR9+Wg2J05XytqMVXTeF9AUWtIIZBlhqRqmzEL62tIFGUV4SflpUDvprSByMpT44Sb+9Axnoz9AsHroDFBYm43X++0qoAGbKmpesUKc9e6Ezl3es+UjFa/MP36ziBUBMAZ4oS/+Jo5B7RGxCcfJ3kegbUR8DbmU9S+P1g27Ttjv7yUqipbOOxH3ZeBKwJqMEumX3fTGi2ZN1c+3KZ7aml0DyWXwOpXENfpuP4gAYEYo7IIW3yG7OYmvCIOOciMdUb03l7dPZGoQI2EjAcpupKkg8zqgDDooTC1zUOX3T2XZLA+vn7PRvoPA1Zb+/2mDUzxI0G/HfLmOYdr4i/7IYadPY6NGbPeK3Vkp+hBbht5MveXna+/yFPZtN9u+mv+Sg+jtLAP0faff2h8i6Nxpur id1jlfT0 xffXcMRXNohasjFt3R1u8GLlfZE+5NW29t6U+ZRwwB7CsdYaZWiV8fZIT1tsxN5ScoBNEAOyrvnAIpQ7j7Zgdr0ceGPrblCQQOibySZRzOmz71bIZcnHCs9M5ihsUVv3TNvwABWtii+ewfBmGyrfx6IxZUpDh+dntj7AS5t+/XOAoByajZeY0hu9G2LywvKQndRrwk+N6JDuizJTnn2Q1Ut0s3yqDHLyitMRI6Ski0Va1aMv8VQsrUKRMRY0XaEc9K/C08yg5sMnPqKC9wVsM5h+L+4dg9msDD2j2AhoZE5vNvTGrqWi8oYcPZt3mgEdb5tFL+YwmCT2ArcRXBJMX4gGyO+UDovZkVFB1ffTg76Mv9FCh4amQvvI79jmHDM8zpHeLw63IfCF1nVcLEqrxNlTao0K+jnoml/3CF2xz2pERnx2W/XxVXc82JA== 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: Convert vdpa to use the new vm_structure and associated account_pinned_vm() functions. Signed-off-by: Alistair Popple Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: virtualization@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org --- drivers/vdpa/vdpa_user/vduse_dev.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c index 0c3b486..bd87b58 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -70,7 +70,7 @@ struct vduse_umem { unsigned long iova; unsigned long npages; struct page **pages; - struct mm_struct *mm; + struct vm_account vm_account; }; struct vduse_dev { @@ -950,8 +950,7 @@ static int vduse_dev_dereg_umem(struct vduse_dev *dev, vduse_domain_remove_user_bounce_pages(dev->domain); unpin_user_pages_dirty_lock(dev->umem->pages, dev->umem->npages, true); - atomic64_sub(dev->umem->npages, &dev->umem->mm->pinned_vm); - mmdrop(dev->umem->mm); + vm_unaccount_pinned(&dev->umem->vm_account, dev->umem->npages); vfree(dev->umem->pages); kfree(dev->umem); dev->umem = NULL; @@ -967,7 +966,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, struct page **page_list = NULL; struct vduse_umem *umem = NULL; long pinned = 0; - unsigned long npages, lock_limit; + unsigned long npages; int ret; if (!dev->domain->bounce_map || @@ -990,8 +989,8 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, mmap_read_lock(current->mm); - lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK)); - if (npages + atomic64_read(¤t->mm->pinned_vm) > lock_limit) + vm_account_init_current(&umem->vm_account); + if (vm_account_pinned(&umem->vm_account, npages)) goto out; pinned = pin_user_pages(uaddr, npages, FOLL_LONGTERM | FOLL_WRITE, @@ -1006,22 +1005,21 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, if (ret) goto out; - atomic64_add(npages, ¤t->mm->pinned_vm); - umem->pages = page_list; umem->npages = pinned; umem->iova = iova; - umem->mm = current->mm; - mmgrab(current->mm); dev->umem = umem; out: - if (ret && pinned > 0) + if (ret && pinned > 0) { unpin_user_pages(page_list, pinned); + vm_unaccount_pinned(&umem->vm_account, npages); + } mmap_read_unlock(current->mm); unlock: if (ret) { + vm_account_release(&umem->vm_account); vfree(page_list); kfree(umem); } From patchwork Tue Jan 24 05:42:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113485 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 AC910C25B4E for ; Tue, 24 Jan 2023 05:44:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 264FF6B007D; Tue, 24 Jan 2023 00:44:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 216276B007E; Tue, 24 Jan 2023 00:44:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 090026B0080; Tue, 24 Jan 2023 00:44:21 -0500 (EST) 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 E9C526B007D for ; Tue, 24 Jan 2023 00:44:20 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BBEE114041A for ; Tue, 24 Jan 2023 05:44:20 +0000 (UTC) X-FDA: 80388602280.14.CF39353 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2062.outbound.protection.outlook.com [40.107.95.62]) by imf12.hostedemail.com (Postfix) with ESMTP id 864AE40009 for ; Tue, 24 Jan 2023 05:44:16 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=PUtCVRp5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.62 as permitted sender) smtp.mailfrom=apopple@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=1674539056; 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=QMvEWLA5MngzK2+7cVOKPt4IrdKvnbRxz60Bk/vscac=; b=rR9U2O81ArARjV9PQgM4U6pnwAf7VNAl9aFjLtDvJPJ7hjHZ6W99pY+V9YrrotEdSF1B8F 95yjjnbzhtDNylYjNRKPiCtHfH1hYn+L+nuXwNifO3SSWUxPLavTmo6tYi1sleASKbWpVu 2lToKo/HhaWtyVHLMTWiQoifO3Vksac= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=PUtCVRp5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.62 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539056; a=rsa-sha256; cv=pass; b=4ZG38neUn2GaugBceByK+R8sSWIfDgYa3tgcJ7qBQhEXTYFlHHpfAfCsquTdBNCZjTxAGq BGBY6tMUwn5S9EB+ICrIXajcX9TITvaMc9KwW/D3zcAa2BCXR9MEocFimCbFvlktFrTqTh Ik6pI1kgQxaQ1VFo6qsP6NbAEoY/agU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c3m1K3OEaJE6HDt3yWKerlp6uEtIx3TIbG8xVEw2t6zYQMu8HEK0S2sPJ4a7tnI/LAKuGRik4sgj9euKhW5cxkSurXQugCv5HEugWMGys3d8BU92744bl+quPJnMSWVHLscM1EBAHKduhiCYe4/7vxqewVRZAXKMvr/HvlfdfIujLsyrphScDeFvFiKykUS+mKBpn5cRefLjs0bUiBGCAXGVKGZ7lg5jl/wc8wb5QEhNQv+BZfTrLDkM0uheytr9LyC+11yEGupylNkU0N86ifvvXceEdcUAaFt1YREeYWZVDZ/bpXWNoJMKGVHVU/sBxr+wmCsHqAyN7x7btL/aKQ== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QMvEWLA5MngzK2+7cVOKPt4IrdKvnbRxz60Bk/vscac=; b=ajCNE+NXxcJtcQOvPZ+lVkpEGV9Tc2kVMJCFWKTMGBdJErLvyDI3KRYjqEVzvxN47xeJB7LqHUwC7TJ9yCseWMJsZULMcLEvAtclSPZKelDGhwWkYP6SNvBudN92kuKiIgRCEYTZMzO4cW7oC2EuZ9iByTCbGeFFwvPTu+nIFInwbQMYfUeB68miGS0hZIRn5b5IYvDp13wNfRrVM6N/x8419241MqOjSTzU7Quq8jUfCI7H0k1qEvVl9xwfADqVJ84Iw8ks8Yf2EojPUBGkAl03yyBU89ZsupgANTT0z946mDrTK4gM4z05nKotwn1w4rtKRBA4S/kYjUf6Bq7xOg== 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=QMvEWLA5MngzK2+7cVOKPt4IrdKvnbRxz60Bk/vscac=; b=PUtCVRp5axRVSwNeRP8p3B8IWqlmfZAq9ZQtbWmMDDnZ2q7GEAZU5xSokM6rdIQBZNqti/uqdPQCeQRvoH1G4/G5laKu7HD4KjhFGdFJ9DVHfZLEkcwenX84h7fKafAqd1ZP8wmmfzPFG0FiFsBueaXOnfDZEhN4ESlCr7GfGBtbgs4aToGR6CeAVgVw1w0w+kaDYrog5WWYkDdOaWjAG+58cytY0WUL4/ZEoypTXSLMNKle0MKVlhk4JhpfZmnlTGZJxzSlTBf1HMmgsV1HwbQH9cx/MyvDa8BTdD+BBCdng2Ze6wEpoz0mvfPrvEYYZKdZ4yAkDyvaRDqRkWC9/Q== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by MW3PR12MB4540.namprd12.prod.outlook.com (2603:10b6:303:52::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:14 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:14 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [RFC PATCH 04/19] infiniband/umem: Convert to use vm_account Date: Tue, 24 Jan 2023 16:42:33 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYAPR01CA0047.ausprd01.prod.outlook.com (2603:10c6:1:1::35) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW3PR12MB4540:EE_ X-MS-Office365-Filtering-Correlation-Id: 65e42a4d-43cf-432c-e806-08dafdce06be X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wBYv9oGtcnNGEOsgD3DowtILQGny8oO3+jIWJmhR6fN488+hTxa4W9le8XuLCmP4Z0mVzgB+BvDS3L06m0WRaD8Ly/9z4XALjk9qKDZAb4ZWdqSaQzwHgYikyNWZ9b1WOrXGbDKQpR33NNeBFsIr0PepoQK4V4lw+3hnyKCD5nIC50+dKR5ttWvunPe8cT73RuUgyM7UnLaOPHGv7LWiL0DI753n/eUTi74aNMYAmCdiQf+/XZZJcdISmamrvFU7zYBXSEjU1z82XQ5ldGZS3yDLLfdB7ZmZv/peP8LxUEUJCraue3na5/ARUKkoH/gk14fXhritwuv/0wpoFvk2zPGQfsdMkT1pXCquW7dST6HHnWvWqMkIPQqEAvXFVg4Ky7c8rI8nyzuUtE8/zTxqluUfc4zA24FXidAb0rJAJ1pStZw0lIwZX3qGm9pX62Whe0vcBP1+C1fbnBtMEmssMAs9XcVSFi9TfsegvIMgx+5mtHa3g/2DhUcNRJkKog6T9eu7PlLwL7UlHA9ODTax913cp26bMXE3dpK9nwV5mzR+ynOxMpwnKEXP9c5BHf4JqsHwBxYRpcJaujLsXtgcrwMPdZhy+9DMTirjQ+9pw8pnSZ5ngOfnEcAjvAfHcltG X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(376002)(136003)(396003)(39860400002)(451199015)(36756003)(86362001)(5660300002)(38100700002)(2906002)(4326008)(8936002)(7416002)(83380400001)(41300700001)(66476007)(478600001)(6486002)(66556008)(6506007)(6512007)(8676002)(26005)(186003)(316002)(66946007)(2616005)(54906003)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MT+N3d/4mSlKuRr2m9nmAjRq63EiccwwQQ7lZmvY7j5qqyUP9+N1VbbJCOHbqR8u0/RQPg1so28uvrxGS46AeMFG8saRrrNleX3+BZTlWmMUGP8DQObElSKZpwD2xsKLuMiFtPZK/Ehg6ZQroSgDBF0p5yqufvdTaqKSLAdncnHCSofONqvhIxSmMmtLYGVhc3O3UwxXDHlOvhX3Zd+ngHD1l7DHcyQDklRAE3pV/Hc2CFoBEA57r7A4ZddYzm6BflhsiLx7a0fE4C4jllEOqfJNInIg+Ucm7KF8mloDS9eX3SIyM7wSFa8UX6WpUHNAZ3RNMft+LxRBZQsJuEcSV7DdrdpEKyTfj6oSu1hbGsBOjuh43m5fAgy3TY/UF0v8DJFHfKUh3XW4s3+6ZsnHEuvsxf3r4AUqQa3vyrDkNOnLId1JxX0At5SVkAv3pBZd40+ldQoaFb2AKIp080ElC7cKAQVEMETq89w0s2rF0ZMPl2BviJ6MWXlHCpzXBMR5jqC9f1zcva8R3mUqfJ+cP4UnG6wIHp/ZwNRPXkuc+3M65tstBRn3s/p/JI8k0QLCTKdOIMJm6jTtwotQ9981CxvPFHosOYFr4BBkcJvyVbmCNDzZP6sBHOZN9So1rJG2z1pOUkhst+efbY1l9RSzTWq+4GuOY3rjOljR5emi+8aWVwqw3KTSLpoMTayrZIoGb2NRhqvF2uLWYcSpGPFcYdvI/5fXuVVuGQpZq3K5CBfQnuGdS2rAPHqsP79R/P/lWgLTyXK3bwsaGiutCU/sq9Gn0xkuWfs/k6Fzh7hVi3QkN3m7bk8Z5HXp5qRl42a+3vY5ZYGe1KAMyjBSv21Dyf4P9jd9dX1Xy7av5W2zDdQtbU3rJpjpO/8Fu1yKoSXMDwYYuoeXrdr1u8+XiZhXv6fLc6L6rZ9FDHHFHZqLs9cNDqcA8QcySHQhWanlwPhz0Sl+9EMR/aYGG9Cf5lmlTJR0jrDMIz+bLeOybZFR14vEBj7PqT9VPNqqze7czmM1Br6Zr0PloI79Sq4w7xzTjVqZXwgQA/akL5oUbZfL0gjlsIkGSvs90LUbGU7jQ3IcSzknRl+kn6QzD0BJMtRfgghdxMmF5B4JrSv52nFw2Do5Nrj22gJ0L3ptbHdG3ELzt6c7PK4hcOsw2fGvJ/AyuGtFTVP6TOxZh4OnKKbElCOdw5AvdWezAUYcRVbmeKoDraCFW8vC8y88A8uiHYdFVK3e3jNtXXhWgeZCzuzQtpkW+3PJgPhFYc46VjisdvmrkGcIc7YpaZsQxfECDRoml0rMI2E4PKrMzPMYyrJ7hTJYLwT9nYrCP4e2sBXbMZmmWgdV37sclyQihcxU9MGJ8Xs9OWoE+5KiV2sUEyXA/CFYEKi/Thv7ssN51lqU7EKQuU7Js3wvPgpmJDtEm9fmvgRMsij6XorcCrhSAHTEyPun97fxMsSjIwEXLVXyyJnz6cdyTzwEbo/YeP5091UXu78KVUlKf/Bo1IlKpyNK3lITN08h9vGYpY8OawjlNU4wmAjyiPDC77CcqjULpBUuqmbtWlBsh6wRwGMMlrexQSlgdFiekDAPM4WQWdEKXQRZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 65e42a4d-43cf-432c-e806-08dafdce06be X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:14.7111 (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: 6WdbGYEZ+nXsvkLIvlU/MYyEp4s32vlCTCrm0ONclheAVhWnpmFK5z/ud6LIQAcYJJQegeK+EQZ/gnCveB0deA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4540 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 864AE40009 X-Rspam-User: X-Stat-Signature: 7wos3znzxqnm3ittnixzsimswbf1pibb X-HE-Tag: 1674539056-117763 X-HE-Meta: U2FsdGVkX18WYyGOKcfS+XDK4rvEutkQLDSRe2sT2ITnrGEjWL0g9DuyFnJyFyWG9dlu45uzq8j+jZB1w6gFe3QrtJfOvQWJ4ZSuz4RzofGUD9BhXjxTGf784T5NQRlluX/j2K7fwK0lBPLyTcS47Zzhs07nHJbi0kWSFSFucF9KHAYcLIGTJ8lhRZi0cAgSof7eHCbCYSyhj5v39dlWsjQmvx796ZfCX52UhzBjVIOJ52j6Il8/7ufSA4fvpDp1Exw5qN56+iuK4Vl4G3OBc1wQHiWD5tscRcf5Bi2jdcXH1BN7Lnln88A6SC12xe0JAoKFRb3g3mh1XWeFyTUzF4rvFDRyxvlWDk8HyVtU792J1s/XXDOL5h1VtKZXbN6R5re4JJL8QJbc0ZkovFJ827VYq4xs3Q8UWUq0FrUi6jnS2TVEN6e8m11ZDosdRGijBchNuSrzZwqT5qW6wfz/662zFWsMoPkjDwS1RI4+DIznG2jExUdUD0iEb85+3be42C2+V35vQidhy3NKIs7t/GfKGIokygu4xajxvXGalBmaPqD7PuVFLyEOvOh9Q6qZhT+Yu90sO2U7Uv+ANGjfnKUqBMnCH65SdET7LXAAxBvGRXcG3AIJprE5dVvaG1Q/iV5MGahIObrpWUjBQ/bzgGmsptjmQAj2p6HcCABdpYfRSgO3OfwUT/wOgk9bFPApFwCfRfCZYmr5X1/kF904/MR6Ed5Ow2dnsynyi7q+hE7m92lavplx9pUMDh3MyZyng+vk64Imbm6VqU1O7ARgW2c2mpGyPk2jFs5RWSc7HjsqXGz3YefIhnMywn0N5dETvF5G+Ggr5XOdlYVew2B1Fm6bSTqfPyxCz5VmurU15todqlKV0yrKf4HogOVDNdV4TCmivDkg9EEmiwNbNpwzx8tzOCHVHsShzHwkxn0ta79TR00/BmExMnR9KacTDJml1eJ84GyPk11YuZtKzgW zr7SGb+l gdwxaj+4yC/tQkZR6Q1DO27YWdwKse/l+eSBeSQ3tyqZiLDhz8DGuzuynuANooFIDD78A9dYwNuxROwo7bIbol4UZGn/b+RvEJ9+MHGR8wo1posNiM73wnpIAEsqs01ePoMRhLOlJGnzUKRhKnaqtCWTKGM6wd49vesCz7/SmLPDpOJxkqIGKRoo8/0FmQ80ounZAIXWmY/AxaNcvfF/gR7XCpGL7sFrEB+biCIFHgIoeZojNAOQBiDxJbmkrYrzharbogogZzm+BZ9vX14ovSCn/4C3Lrt0jUB6uslVbqdBDK4IE+kZrvZUADvOx0X2fA8OXubaoAnFOn6h9Vnx7A2IxQcxFACIuN06FGvnP5+1tEKBzPic5RZM/H9yqqieOnxzQCfj92RHLJWFZNzlI60k0xw== 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: Converts the infiniband core umem code to use the vm_account structure so that pinned pages can be charged to the correct cgroup with account_pinned_vm(). Signed-off-by: Alistair Popple Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/core/umem.c | 16 ++++++---------- drivers/infiniband/core/umem_odp.c | 6 ++++++ include/rdma/ib_umem.h | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c index 755a9c5..479b7f0 100644 --- a/drivers/infiniband/core/umem.c +++ b/drivers/infiniband/core/umem.c @@ -149,8 +149,6 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, { struct ib_umem *umem; struct page **page_list; - unsigned long lock_limit; - unsigned long new_pinned; unsigned long cur_base; unsigned long dma_attr = 0; struct mm_struct *mm; @@ -186,6 +184,7 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, umem->writable = ib_access_writable(access); umem->owning_mm = mm = current->mm; mmgrab(mm); + vm_account_init_current(&umem->vm_account); page_list = (struct page **) __get_free_page(GFP_KERNEL); if (!page_list) { @@ -199,11 +198,7 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, goto out; } - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - new_pinned = atomic64_add_return(npages, &mm->pinned_vm); - if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) { - atomic64_sub(npages, &mm->pinned_vm); + if (vm_account_pinned(&umem->vm_account, npages)) { ret = -ENOMEM; goto out; } @@ -248,12 +243,13 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, umem_release: __ib_umem_release(device, umem, 0); - atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm); + vm_unaccount_pinned(&umem->vm_account, ib_umem_num_pages(umem)); out: free_page((unsigned long) page_list); umem_kfree: if (ret) { mmdrop(umem->owning_mm); + vm_account_release(&umem->vm_account); kfree(umem); } return ret ? ERR_PTR(ret) : umem; @@ -275,8 +271,8 @@ void ib_umem_release(struct ib_umem *umem) __ib_umem_release(umem->ibdev, umem, 1); - atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm); - mmdrop(umem->owning_mm); + vm_unaccount_pinned(&umem->vm_account, ib_umem_num_pages(umem)); + vm_account_release(&umem->vm_account); kfree(umem); } EXPORT_SYMBOL(ib_umem_release); diff --git a/drivers/infiniband/core/umem_odp.c b/drivers/infiniband/core/umem_odp.c index e9fa22d..4fbca3e 100644 --- a/drivers/infiniband/core/umem_odp.c +++ b/drivers/infiniband/core/umem_odp.c @@ -130,6 +130,7 @@ struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, umem->ibdev = device; umem->writable = ib_access_writable(access); umem->owning_mm = current->mm; + vm_account_init_current(&umem->vm_account); umem_odp->is_implicit_odp = 1; umem_odp->page_shift = PAGE_SHIFT; @@ -137,6 +138,7 @@ struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, ret = ib_init_umem_odp(umem_odp, NULL); if (ret) { put_pid(umem_odp->tgid); + vm_account_release(&umem->vm_account); kfree(umem_odp); return ERR_PTR(ret); } @@ -179,6 +181,7 @@ ib_umem_odp_alloc_child(struct ib_umem_odp *root, unsigned long addr, umem->address = addr; umem->writable = root->umem.writable; umem->owning_mm = root->umem.owning_mm; + umem->vm_account = root->umem.vm_account; odp_data->page_shift = PAGE_SHIFT; odp_data->notifier.ops = ops; @@ -239,6 +242,7 @@ struct ib_umem_odp *ib_umem_odp_get(struct ib_device *device, umem_odp->umem.address = addr; umem_odp->umem.writable = ib_access_writable(access); umem_odp->umem.owning_mm = current->mm; + vm_account_init_current(&umem_odp->umem.vm_account); umem_odp->notifier.ops = ops; umem_odp->page_shift = PAGE_SHIFT; @@ -255,6 +259,7 @@ struct ib_umem_odp *ib_umem_odp_get(struct ib_device *device, err_put_pid: put_pid(umem_odp->tgid); + vm_account_release(&umem_odp->umem.vm_account); kfree(umem_odp); return ERR_PTR(ret); } @@ -278,6 +283,7 @@ void ib_umem_odp_release(struct ib_umem_odp *umem_odp) kvfree(umem_odp->pfn_list); } put_pid(umem_odp->tgid); + vm_account_release(&umem_odp->umem.vm_account); kfree(umem_odp); } EXPORT_SYMBOL(ib_umem_odp_release); diff --git a/include/rdma/ib_umem.h b/include/rdma/ib_umem.h index 92a673c..de51406 100644 --- a/include/rdma/ib_umem.h +++ b/include/rdma/ib_umem.h @@ -19,6 +19,7 @@ struct dma_buf_attach_ops; struct ib_umem { struct ib_device *ibdev; struct mm_struct *owning_mm; + struct vm_account vm_account; u64 iova; size_t length; unsigned long address; From patchwork Tue Jan 24 05:42:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113486 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 B251DC25B4E for ; Tue, 24 Jan 2023 05:44:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 573096B0071; Tue, 24 Jan 2023 00:44:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5231E6B0080; Tue, 24 Jan 2023 00:44:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39C776B0081; Tue, 24 Jan 2023 00:44:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 260716B0071 for ; Tue, 24 Jan 2023 00:44:32 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 00A058028B for ; Tue, 24 Jan 2023 05:44:31 +0000 (UTC) X-FDA: 80388602742.23.45C84BF Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2058.outbound.protection.outlook.com [40.107.94.58]) by imf14.hostedemail.com (Postfix) with ESMTP id 30042100014 for ; Tue, 24 Jan 2023 05:44:28 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VMqRAsov; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539069; 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=Ys/BSuVjvYg3vlQckN9qotw9N9puGnTTdwB3lsffmG0=; b=4Yj2olNvfrs2e0DByykVaUHe2ioiLMVOD+4AvZRXzlFVTQ0AQsj2GgrZowBNr7e7gHI+aX PX5Z/u2Dkk4mo/Z5tmGMHYNRdwul41zEy875crzct9qbY1dAKrI3rpU1ugsYtstrdvLT3m ANSTSJ4Mxm2BjjCedWmTarx1ykz4v+I= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VMqRAsov; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539069; a=rsa-sha256; cv=pass; b=zn7Yv31E8owozYfgOfzhV+giZMwlDjV0y1ExMyiK0c15Y3eN84HNGSmjGIcjszNkKvZ+Iu tCcA5/JMoGGdYj/dNsd2yzginhfUlGK1xrcDiU+tBIsxFKRrQ6UV/d++AsUVOoTgnPbVef WS96yUsBLJwRXsFF49UrojZ5SSOOdxA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bb3O+BkGV052ySnoKOa7LGiPh2Q84Rl3VxYSPCZyfCMfut4iT4sIDGkl0m0xkmmGCUUutkX30pHcOkY0zi5PeG9T9BxhydURS+/JEjrZy7ZbAS6PfXEwbKW/Ve8xD0hCXjTjuJJkSULQRd0U0exHZyIzl4n4TyEog8fju9JGQkUMxAyeO72CJmdAdQQafabNayo2i/0WHzsg47+q7/nz/pg8wp+Cvn5eWe+HueUP2k3g0d/tPn+Yp3Kc2bXjVCMWyFX8NGwJMPL3TUzWGD+glJjs6yk0n84hoTN1ii8hpSctLyz+a07gPOC9tPHR+jg90szeA7px7KBlhVkx0eSr5Q== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Ys/BSuVjvYg3vlQckN9qotw9N9puGnTTdwB3lsffmG0=; b=XcYRMNMgrw1kZusBBbteylsMkZYnSX4eKvlNjEzTBw3gs/8ojFq7VioI8U+nGpn+aD1qFyHZxW66vVb7jdzzV+ZElUZ7ap6+nWw6A9xlZ0+C8mIf0PJ5yxrJ2loS7SaJ1zIK0f31KT8+oyg3X7GAFTX1zlWhx8bCrftTlxHzWuqkLlq3wH2Pl3CakN/JaG2v3x4tZUjtoopH5xl3AoK+4k70TOMxEJomFgvBYl+s4VhqHUwypzi7HO9lucXbCuDvT0wWnF09x6Rzm6mZ1jLGg0FSaPpGAWPb1ZtJKg7WrDaKaW8/bKRQPbzxj+ezXWClmdHVkCPZ/XZRRdcqvVHEXQ== 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=Ys/BSuVjvYg3vlQckN9qotw9N9puGnTTdwB3lsffmG0=; b=VMqRAsovhuiP1xlHzwhCtlAjrD4l00fCvvvfVy44+q4zWKTW6AUznfapM3qZYyLL7Zo0TAow/blShV90PvoRb/xlIdIEcSWt1wjlefZcGYfPH0PNi0Nim0zEBTM+KQtfCZljuIo/dZej0/O7Lzij2jWwT/I+zJefY+jRSTh6d4LY/bwZvypcYNH2x98rb1c4MLPKTRRvBBYBeeXFeVkE8CAfo9r49jlyUqbQiDI8wo6tgtDIECAIdDXMM+gvgr5drIdK9BDxSZKUgWyhK6xcrjZyT5pIQHWglSQlxWrX4UxkJoj5AeQXqe3CsNNxgwNKwvj+DHbEuJ99Zl0nVNKhwg== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by MW3PR12MB4540.namprd12.prod.outlook.com (2603:10b6:303:52::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:27 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:27 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Bernard Metzler , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [RFC PATCH 05/19] RMDA/siw: Convert to use vm_account Date: Tue, 24 Jan 2023 16:42:34 +1100 Message-Id: <4f8b1d54ab5d6909c46ba5470065f8326f9fcc62.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0005.ausprd01.prod.outlook.com (2603:10c6:10::17) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW3PR12MB4540:EE_ X-MS-Office365-Filtering-Correlation-Id: 90992122-bf85-4cfb-bbac-08dafdce0e2f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xPoVJFoi2Q5dygKTO+tA+wp3wYXP9Odmw4uNetAYU4PsftNIGUxGUSIGMDEW66kbv0L2pSv8IZjHerNBaTqA5BYNpiMVI5B0Hq1KDbrtgEYfIOmjrCbE9o1Gy66CvMgAQWSSnDjRI4vKUhN6AH1ANkK2Gcq6AxxGPJhBjNZ4PWb74q+wDutA68XJ3+qsGGufYgOukDkgeO809WDn5iWQdOMgF6bAi0c7p2Tc18KbyVMSI0xfykAiXcSOM/LhUll1taz4rr5qY6CPR6ZU1Ax6bOiaOyg5lysmjGowykz0T9yVTX884HzXeCOzlLeJG2IgDJgDft/LMh1zMxh8wYhUqmEjIfSjW7Ts+8NYvTuyPwM9aO3BUym3YQVYX/FtBuC7YdHUG9LGX0CAGY0Ak9PH79YJDLqglVlzK/sQaUcEZdAkPBH2i2ML2OcN4TQGnvMzEIt42NY3seep/YvCG8Tz6av5ZRBs5qF75zuUCFW9qMPWaisSprb57iJaalEWYqGDbk98sOkxIJIlgHv6Y3OBlk7LUI1zYj/nUwKHogF5HbJiLDSkAf6sSRnffjlwYOzZrE3CJ4LCsA4mciMOSwHysuMd6cNPB6K9Nje3vwCF3YvK3AuVoCmB2neSiiIYdpvz++SDX2Rl0IN+Cm9SHV8yhg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(376002)(136003)(396003)(39860400002)(451199015)(36756003)(86362001)(5660300002)(38100700002)(2906002)(4326008)(8936002)(7416002)(83380400001)(41300700001)(66476007)(478600001)(6486002)(66556008)(6506007)(6512007)(8676002)(26005)(186003)(316002)(66946007)(2616005)(54906003)(6666004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7INmr83+joiH8mGAyXhiTEKLgagnKOtAuPSs35DKgMe51e2kS33fbUWuaCDtjViRoxUpqVo1pOgkJxQY0rGOIOG5X861wNtgV7tX9idG2Z3nQ/mQV6g8gnuVrJfy0fS36QCEUBVwpvuvVQPIS1suhS1pTgZmoCPFuFlUMnGYd7+ZqpEcCj6JnZTtILSzltnzZySskWf6iLV66xj+NAxsUgLlrrq9fFRXrEgM5OBfGYoWp7GqGtEMVGw9BnwkAA+nChegXAC9jbvpal3V7vEUo/5BRjUt3eaWbXE72P/MFtWMPAR5VnG0B6H/VwTdMSJA1T1uVDSQYUyH7crXfv47Q09NAaWK3n7IE4CsQ09Ek8pw9FG4Pz3LMeeeoSjFtpwWHz301Ya7dafRTqfiLJgIDeJ4A7Js9CAsXWySqOSYSC5jf03idiW/PoZABpMOchr+S41KTQN102Pb5ge8sUZ1eTuoRpfQXhJ5/gT17z+Z+aSxVdlv+4VuURtVdEnINALsORK64gTgRrU5lsGrQ1WNvVw/ajyMAzAqvvkkIoqLhHhwqVAhO+uHsnHCaRreRtD0JvQ3CBsuj1sf7QvEIop9aCaACT5Q3x4H3W0YeRUfrkK7yhSYnk5oyonPDzsPxuCGGSYouLNhRbvm09eQGbfxPUL7HHcQQCP9CfRatdBrxe0XqspdxXGou8Sfo+/AecJ2u0pmnDx0xETm43qT1LJLyb3dsxnmtwJ0YFTe/qFIXSqCoAX1ojr7StL7AGRbiVZu+DRtOGjhBZ8tCA/DVbHoneq1J8FGedSTHTQzzjuhVzZKkDZXtJL86tJyHWZ4klqw0XjGKFjP/+ON514Zd6vpNYwDuqjU+XAUWAeBKbDcMJmQXbVmK79W7S9krG/eXvDGYJGtbCefdiiP4jx+OO9eVzgtTiTgpUBQw8AHPczYgd7Wxcyfx3HfcH73kcgWZtTFF4eVraYb5cioQ40FT/oV+Bbo/fT4aCftSI81r9hUwTLlgmtMlgaOgcfY+qB176mjqhgIv8bfWriwOFawZjJxIY+QSO6Ld883nMuD8f4uFmOTdOPjXRYWSV6Tp031k5ShYEPrbs5kivclLPnES47Dfdxi1JOFIVvN3Qd0jqEIWUb4Q510CywcoWZf3B1IhWN4TjLWv+4ZXKM+4m8xUu7wVkqbVo/oOLX35vQyZojGDBmCXkplz3IRzsvVje7dwCuXFx/7Ani3FB97FW36RzPkm/t4f69CP0djQOcgCr4lBRLbVAe4m0vdCzu85nuaD4gewwowQzpykrJWXIjnmYOP0YvEGie3xyu/SqYVeKxbYLW8bbD9052o1egjKcLyStEhIauRNHSjU5DSianf1ueeNODGOs+Fc7OMLKanJG9d1n8UAlpBgzMZlhqc0caF5A3nOItY2wP65A+X+mfpF8oEF4GcTCxIj5GhntDQrMKiGKyq67ghzWGMjY0cXyPLOm0RuxDnQ6r1DhOXixTPO6iPcXH+sIu5XZKhzpiUPwiFnFeGtqWG91VKBxyG/NLL3yzdMa4Lo6bujdGeta40XrCOQMhXY86m0rhApeqgbDFgjs+taJ9YlGhwGtiRMS7UZGxi X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90992122-bf85-4cfb-bbac-08dafdce0e2f X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:27.2570 (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: GfimHfBEj5y93A4hAuyjqPMkRtt0veLtE4+3AW9Jcl3CCT/FaZIc5uyx7oJJGClA2Xa69XYBm4Y+udtVvo/ePg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4540 X-Rspamd-Queue-Id: 30042100014 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 7rwgponwnkuyg65g8isu9oex1yf8mbhm X-HE-Tag: 1674539068-993496 X-HE-Meta: U2FsdGVkX18vRUj1ninn8q0YUpCtL7e9JMEkgQdEnXYPA3Y/wcl0lR/HLm2QUZe8s/meCochMSQhQ1iUniO+adHVy4Kzo5B9CElhDZaHZCCgjWhdK89KwAq7UDelZEZxWydNG+MQR8XOcErhvU+bxryTEk5l4zRw4HdIus9WJLxgHaZmm0woUeMMHCZpDUm0pDBm9fbSXgxhJQXEUmq8NDaKoJk+3ngiHG5KpavxmSBivJ5PeaXIx3vl63gVJD9mJL9oJpIShAE9SOf/2LfnoSYjdl0L2wJJP9O15Q8io3sPYhmdpQVVkR/tnyRxj/vkvp152XfyDsGGM6qxjxnXMmgoSCYnX4hpxX76tILKV1LOt5Sqnj+0S5TRiF/nlXSooQ2gWF9OyxWOdfX/jHDm5QWkEhnYNDwH8Hcjc4X1E2MGzIAX9g3uh/zVtG1PtvY05fRIl9McK+rbr5SYGxqWMU+5zUVDGFGH92CAlZ92PeCUHC1HPohVaUch1do/7FHbsVNt/YZSaBUkdVO5m0tocFrwmi1pjqXPJlf+qUPf6URd3d4M0ebzPUaNeVripHh9QTJgXQrAqr0itM2+b28fhW7r/Rnc/e9HYFIa+uKnfDg+/EbgxJuavwL138MQbxIVi1kl6Grrb51qIXXlGTr9Wh9f9QwMop6zOb6H8OXZThBC5dz6eLFRzXt3FNkSQz0rKR/qtPK2dlcmvhZp6daC0aHq58c+Gcqa9//zlVg4aA3vtg0gyhNelR0NEAjIAUVOKpPk68ypwOA4iF31LQu670vZD1b6ZsSljucpvbQkhjCfdKbIJzRHjUz1tIVcemNPrCK2yheW6U3HbMKLZ3imIzroYbz2Hg5UdxJPMXy6WfzCKCBWcjQJkW81TVmxuHfyVkqSIzUZr/3CYADxLD71VsdPlM4kn2j7NubSMuqy7zocbOXaCc6lZuZjHhG5/oXBVzPb3tfiByCvvPZO6z5 Yj1fKLZV 5yH+Wk1TQb1ZODWNVkV6UZsMymyFvpj+D95e7BIZfBq0tbyMvbJtHmv+3WKLEIAX0Mo7HKJkx7kzPFb2YnEAgHae+V++dyrOSe05YcIRUhbBdIAzglZ6gwgtfpItMTHAWHeqJzPdWCvyDMTFnqKiQECnLyOXhyx2YicfXI0Ok8JXxdRsIsvXpnXkO07UQSHtuoT7bYwhcxBoeTQzswN7CUfiAQipXSJDXk/QgeDUIIv9aFAG3Hk3AeAPIpiUg1rloHrQgh8GmVDQQny0Fe4Snv+mIUmy+Oi5XJQVd2dJFE97Ysj+y2wUNGkidPnryWEmNjNUW7OBOqCJj1IDUrG9nib3SNO9oKqjTRQUHvCQg8ylAPAkCG0DfjHc7wfIFol+lXaRcbRv7IRGvta6lwVkLQSW5sff96ViRmS75N85EaZ8hdLJf3bS4st/AKg== 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: Convert to using a vm_account structure to account pinned memory to both the mm and the pins cgroup. Signed-off-by: Alistair Popple Cc: Bernard Metzler Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/sw/siw/siw.h | 2 +- drivers/infiniband/sw/siw/siw_mem.c | 20 ++++++-------------- drivers/infiniband/sw/siw/siw_verbs.c | 15 --------------- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/drivers/infiniband/sw/siw/siw.h b/drivers/infiniband/sw/siw/siw.h index 2f3a9cd..0c4a3ec 100644 --- a/drivers/infiniband/sw/siw/siw.h +++ b/drivers/infiniband/sw/siw/siw.h @@ -124,7 +124,7 @@ struct siw_umem { int num_pages; bool writable; u64 fp_addr; /* First page base address */ - struct mm_struct *owning_mm; + struct vm_account vm_account; }; struct siw_pble { diff --git a/drivers/infiniband/sw/siw/siw_mem.c b/drivers/infiniband/sw/siw/siw_mem.c index b2b33dd..9c53fc3 100644 --- a/drivers/infiniband/sw/siw/siw_mem.c +++ b/drivers/infiniband/sw/siw/siw_mem.c @@ -68,7 +68,6 @@ static void siw_free_plist(struct siw_page_chunk *chunk, int num_pages, void siw_umem_release(struct siw_umem *umem, bool dirty) { - struct mm_struct *mm_s = umem->owning_mm; int i, num_pages = umem->num_pages; for (i = 0; num_pages; i++) { @@ -79,9 +78,9 @@ void siw_umem_release(struct siw_umem *umem, bool dirty) kfree(umem->page_chunk[i].plist); num_pages -= to_free; } - atomic64_sub(umem->num_pages, &mm_s->pinned_vm); + vm_unaccount_pinned(&umem->vm_account, umem->num_pages); + vm_account_release(&umem->vm_account); - mmdrop(mm_s); kfree(umem->page_chunk); kfree(umem); } @@ -365,9 +364,7 @@ struct siw_pbl *siw_pbl_alloc(u32 num_buf) struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) { struct siw_umem *umem; - struct mm_struct *mm_s; u64 first_page_va; - unsigned long mlock_limit; unsigned int foll_flags = FOLL_LONGTERM; int num_pages, num_chunks, i, rv = 0; @@ -385,20 +382,16 @@ struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) if (!umem) return ERR_PTR(-ENOMEM); - mm_s = current->mm; - umem->owning_mm = mm_s; umem->writable = writable; - mmgrab(mm_s); + vm_account_init_current(&umem->vm_account); if (writable) foll_flags |= FOLL_WRITE; - mmap_read_lock(mm_s); + mmap_read_lock(current->mm); - mlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if (num_pages + atomic64_read(&mm_s->pinned_vm) > mlock_limit) { + if (vm_account_pinned(&umem->vm_account, num_pages)) { rv = -ENOMEM; goto out_sem_up; } @@ -429,7 +422,6 @@ struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) goto out_sem_up; umem->num_pages += rv; - atomic64_add(rv, &mm_s->pinned_vm); first_page_va += rv * PAGE_SIZE; nents -= rv; got += rv; @@ -437,7 +429,7 @@ struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) num_pages -= got; } out_sem_up: - mmap_read_unlock(mm_s); + mmap_read_unlock(current->mm); if (rv > 0) return umem; diff --git a/drivers/infiniband/sw/siw/siw_verbs.c b/drivers/infiniband/sw/siw/siw_verbs.c index 906fde1..8fab009 100644 --- a/drivers/infiniband/sw/siw/siw_verbs.c +++ b/drivers/infiniband/sw/siw/siw_verbs.c @@ -1321,8 +1321,6 @@ struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len, struct siw_umem *umem = NULL; struct siw_ureq_reg_mr ureq; struct siw_device *sdev = to_siw_dev(pd->device); - - unsigned long mem_limit = rlimit(RLIMIT_MEMLOCK); int rv; siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n", @@ -1338,19 +1336,6 @@ struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len, rv = -EINVAL; goto err_out; } - if (mem_limit != RLIM_INFINITY) { - unsigned long num_pages = - (PAGE_ALIGN(len + (start & ~PAGE_MASK))) >> PAGE_SHIFT; - mem_limit >>= PAGE_SHIFT; - - if (num_pages > mem_limit - current->mm->locked_vm) { - siw_dbg_pd(pd, "pages req %lu, max %lu, lock %lu\n", - num_pages, mem_limit, - current->mm->locked_vm); - rv = -ENOMEM; - goto err_out; - } - } umem = siw_umem_get(start, len, ib_access_writable(rights)); if (IS_ERR(umem)) { rv = PTR_ERR(umem); From patchwork Tue Jan 24 05:42:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113487 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 4A896C25B4E for ; Tue, 24 Jan 2023 05:44:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAF8A6B0081; Tue, 24 Jan 2023 00:44:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D5FB06B0082; Tue, 24 Jan 2023 00:44:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD9686B0083; Tue, 24 Jan 2023 00:44:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AB90C6B0081 for ; Tue, 24 Jan 2023 00:44:45 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8443AC05DD for ; Tue, 24 Jan 2023 05:44:45 +0000 (UTC) X-FDA: 80388603330.25.C683331 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2082.outbound.protection.outlook.com [40.107.243.82]) by imf11.hostedemail.com (Postfix) with ESMTP id CB14F40005 for ; Tue, 24 Jan 2023 05:44:42 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=g6blWCgO; spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.243.82 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539082; a=rsa-sha256; cv=pass; b=10wbi1sHSNNw+ximO8g4+Q5GIXqcjznmA17gaKugwO5sSWdBTMNk/jM+Eo4sMhgWTNmQH2 6eL98+TDJbAdzwqsDOYYaW8Ev0FjsRdezwmaWUExmUMFbopOoGXyPSkVdAtfXzEBVhuIHR XJ6tEfBziqIY490/w2TGWW4v9BUY0Vw= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=g6blWCgO; spf=pass (imf11.hostedemail.com: domain of apopple@nvidia.com designates 40.107.243.82 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539082; 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=xVSTTv9ijFATl+Jf+OCS62NKLjuBKLo0e271Utsbdkw=; b=bmJea+H2nWmeWouBJYZRYPeTae2cs1O4yP3sxMo6uQWIU1j3BNyRMZ7nhJC6IhFfrGhqWU WiIAvl5oPCrEHprvk+Mek7tEWyR/bWk+M7qlY+/RXOddX4V/yZO7c1IWn2lLvB9kfEhQhJ DtYgL/K8YpP51uZ1bOouX9fRfN9UE3g= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lqmD3Rpm28y2RWwRFZGqVCsa7JVofEmG9+7vMC8/3em1LeutbRYHlIiAYitZQVNNXOe7h1fCU229mkbFExwhIuQctBwxIJOQSWAa76VwY9VVm9VSnqxczChzCumh1K7MTK+VIhJeL0vpwjwLPb96t8fTCt7ucFYFgm+fDl50DaFggcY1rUwVevrIKB5PBkf14cOTvYb6vc4II/O8Io7H4ipWGHMt8DJbCyxirkRnq7I2VLJfcixCZPv8tvW6mHo+cz65KNzb0K1A9ST3iLXUKotChGrOXlp0tAso6+o9bFFg6xFv2mSejOIN8OERe3NwV3PXTtVowNQ80jiS88RStA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xVSTTv9ijFATl+Jf+OCS62NKLjuBKLo0e271Utsbdkw=; b=GMhqHZ30CKDgpv63T0QUgee2Wk0qR5+vJh3tsMAoYRyH2M9iidJ/HXwRSje1HHQ+7DBg9g7lS2KujpmMtnTVoz1kkqYQmVl8Tzf/kyLaxiJZEikNJ1da7U+WL820WsESMWxnb2KFfW7P09M1KJNoSQPliylQlzQiQtzOV54+SYEsCMiqWGFQqs3IHzXCiXSSk3o2mQdLokGa2NpNV94GetF9/1YpBk2vSLZN25xnP8bv580SDKirmDd9uy5/z4OTtZ4YWsN2oVqAiDqMiPCkpq5/6RRT3Fhx6sSK3H6zDTy3AX2P25+mzduNqFkjuiPEmnLo9UUpswPqnfdM/POaQA== 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=xVSTTv9ijFATl+Jf+OCS62NKLjuBKLo0e271Utsbdkw=; b=g6blWCgOw4iUL8iy6QPPIj9lyTmJs/dnfznW6R32Pmeru+gzBRXTerouGEqb7Z+3HZTfJnkY5dnd3tbrbOpFNGOlbpnijc87LlwZ5WL5u180Dalp1pgYj652yQ+DAJM5b5npClD9Xx9LD3Kw63lMR/ejDDl+FzkumRXJpgOjgG1klrGzgjNHW5QWAnZSvTqmIsokLExwf8MpAb16PIUQeip8FWzu5VXlJy5wQ8MLo28oBcCt7HsiOvb5phBbNs6/fSWYSmqAkWqj4awByCyxSGE4I01+waUt8CbOZ4ickPz61GsaKXaZqoQjuIV2l4Pl5VC/NDVJ63HGPGfW81C2zg== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by IA0PR12MB7507.namprd12.prod.outlook.com (2603:10b6:208:441::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:40 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:39 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Christian Benvenuti , Nelson Escobar , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [RFC PATCH 06/19] RDMA/usnic: convert to use vm_account Date: Tue, 24 Jan 2023 16:42:35 +1100 Message-Id: <03ed2d166826cd7055810c66a175e20fa2153c52.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYCPR01CA0019.ausprd01.prod.outlook.com (2603:10c6:10:31::31) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|IA0PR12MB7507:EE_ X-MS-Office365-Filtering-Correlation-Id: 58502a66-ecb1-4092-f3a6-08dafdce1583 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MY1gXIJLTFkqkhadHxZi5X+pDcRr+i4Yj0KNEWMYFe/Lz7wyEznPlryhZ7i9earuwU3RgTCbBJbxJZh3Cy9eRkuzcEvZzNRgK07/Q4vbbjnesKtWPEwqQ4gAMhUoOX6JjPrC3s/3MtSd4kMug6WveXXt3z0mZ8+ggxB0e6nejHMWHE+nkJdNhZbtvFInYFnSF5jV/LnJFd92VcYWhhYI86QeKM9A/QaJo33Hs9OyeLHuuch/i0uxmSWhC4vzve4MFjbXXoN+eqah002aiPwTF9XLDsiTL7xKeLQWguHM6rX4o2Kba+yikE4EK4zEQ2PJsFZacW/+2esVbvd24othnTPRm+CHQFAfgTv5Ga7Mz0p8NL1md5hvhgboNSV8bAS6JdHMOZLB90gwjgjr/bgI6PC/YUlYp8AcjIpza1NE/XCWnpDmSHBc5WBFv6EwA90CvGzeUYjF2TF/j3LEIshOE84X/2fMnnSLbdNqvDMZu2pvdUX/RX9TUYQC6c8wSsPyEgHxwkFjK28Y1RUjYInRJ4ygrrHwwHLpmurXMLZTGtlllWkHPec/0TKcTWNE2C7SJLCUUthJHPmKy52Q07lGSXXWf1I92R5LvKA8ThxVMgSba7AomTiyDHxjJUaYcLGJUZhva7MwTus5Yi8y6ET6Pw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(396003)(39860400002)(136003)(376002)(451199015)(2906002)(41300700001)(8936002)(66946007)(66476007)(66556008)(7416002)(5660300002)(54906003)(316002)(6512007)(478600001)(6486002)(6666004)(6506007)(26005)(186003)(2616005)(83380400001)(38100700002)(8676002)(4326008)(36756003)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: IFnCrbaLEJ+YkC6TFtIjvM/ty9nMjoS5C0rEiJzmuS5XONjK6OowLYw45AksHspKnJ1ZOqA0lFuyf3gG8YdVuNPhAKALvuIX/i0lzFlCuptefjn1RBYbeOPusXhqOKclQ0usF85lne57dCXJy/8aRJfkLUWRGkyjF5HdwrzE3Ss1kviZiABnB5KbmoLWuH1nOZbXfqvm4VmkdaEnuIQrNAOK0/a4FtQcmqkm7aJ9tgLsnYgSCobc0PrZM2fO8NMH/5q5C55BMlq0RgXY83D+MPsGrN9YMRVfnwm7pVX7pFpN6EyhJkXlEUNZiU3DztRqfAw8jaH7+y+DPebLE2g/7wjdzWOgvNNPMyyq5QyjQPKuZhh0O0aK4aQKlMVpYHsW/vUoTD8TfAqM1OsRtPF3+YG/ohnsvlPcnnF+wx8KC7Py6MNxscj6s/jSvTmocSP1Rfg3b4Pk1l4R5HMMlIuLwZIRLdBJq+p34fhEKMjEYXvCWYKUS6UMVOL07AtB/IWSGpFKqL8+eWRsmG9Rq/6ZtQlQXcp8ht2xGxJpT2p1NE0qe6OZmCFk1Tb77R3+5hlW02f2pc8GFYAOg9dWzIARFKI9SPXEh3dCsxeRb5sxgA7A/V4uTwXmgGzpJ7b4L/zoCe2/mEqzLeK1Bi9kb5VEo2y310oZ6c8MT6lLEgSnUN+8Crxs6lbBvUyBTeC1r3Nbyow2s2gxlhDl+bc/lMvpvIN3X8h+ZkH18S3ZN0LK0pOt+VIbP2RKOUH4lccTKtjM6z2o1f5Lp16Psf1ZUHWWv+ZfxGHFUJdYerA3M0AQsZWuO3E3Gqz28P00qRvNN4hwIHMaMq/nFsK5B+iBhBqEFj9ootbLXuF0RkDsSMOM4twR9TscwPPQHvnzbakfI9zNAKYj/twplzCDvKVsoRSCgiEyXQHtugfpzlZCIsGOUj64t8cYxhFM9wGsZ6NmEuELsXaR73JEPDOT20ezwon88uXr71jMpnkmubW1/sF5gc6ux/o2Bl8NzR+tIHVtwNE7ZHg/BK1gWU3mGNWS3r2Rd7TqjbcrfA20s6SixSERQkHAqnkcsuXKudbmplCsSQeiPhd3aPWbx41WiPSQ+mV4cctKrmeEChM1hkPYDUYasn4g4yT6+8ahJCYfyE6qJHEtVf5Gpa9iNfUeDy+6jsj/108x2K7d7RnF8oHL4jlj3fYWm5YPGk9MJkM4g/yWx1Eou6r95dCu5hIG5DILuIWyjGSC7eEak64Jfc70rVR4WoN3P8LZiUlUNCGQOAWR/xIdzRuFKGcxtrBaboEF5basRsGwVb3jT9bHEITXFBvt43q3Ive7KJSUZ+Auadt8LxK5wxnA39MVNncdn6VjHHkADO7SdlN3GVfySNQWOf+oLYIfRKYf4ls697sZRVCNStuI5C4Rpe1UKPiaKq3sg8taw07O0ynzjIA5vz+cRdP0rLFqDTI5+9o9oZFIilQ/WKcmnsYk0ejhB29+QrE3bIBkZHs1xN5QeqAu/munHwV8PSHn7bxrx2u1aCSiXYBs8abipnXRdEbLp5UHXpVDtLxn/P/RK0aZx19/vc8xEzKVnXPJx+4q8WyGFkg1rNfl1Fdj X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 58502a66-ecb1-4092-f3a6-08dafdce1583 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:39.4280 (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: imSbUZwV9HUvtiRCcJ3HUSILYWMUenFLV2egkLFBu52lqOJzgY65CbSSEnx1zofFfPtwPTEMLpq73ZrQcRmxzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB7507 X-Rspam-User: X-Rspamd-Queue-Id: CB14F40005 X-Rspamd-Server: rspam01 X-Stat-Signature: mqn4xx3i35qrah9kp6eecapse34c1i74 X-HE-Tag: 1674539082-222915 X-HE-Meta: U2FsdGVkX18I6e5+xEg41aiSKPkl42LybMdf5ojVt8iuSx4x6HNT/7I9mZNAm37onAgzi8T/uCjzQTpYX8OeoGoLUGR7qJyJNsVBKJhrdiu6NSvKjG6pdNp7E66pqzKcIZGKAOJgzZYeZ7qKgu+DnOESfHd4dIx6sWRgdaIegY2uKMjH1uFvHujF+BTlCu24+AbhQuUy9D3iOekFmDnQQrgoZLDa1za34ANaRqpUDno6GDl7MCGcrkyItWNiZ950XffcZPjcT8DG1qd7qGRcIq4v/lHWQLiT3UoK1hOzFWaCp5/5BHZOEQSJUxJx581PS6yQ+Iq0G19evGKwe1HmWVOaGh6LQuVaX4tIqD8BA2whUoIZkgZiFLgi7ax0V58P7ht1mgUtSBERrycs61CTEX5JkhgaRVAHbIDoGTNk2Dp6gxMGw7hK5I6Ss6/Eepzn6fwXI9iUJB8eqYBzbgH1TOfi4iJ++XTIriaIK/vyyHpNmIkvG6mUEzCUM65Q5b8qsBdaK+G8UbFeZQB/AyYE8qM6LNXe/a8h7MEsU5yfDM1X4oQuR6TClAdg02pINrjfpyGk9T0q879TgKLQjBv5qigTI90uyIDTPhEsLd4nLB3UdfPO+9kpqXY8G8H7BTP78ICyALvwyYbDhPA1fEpKL1/LPkCpMU1Acqd7RoJZvbUarzyQI/FJzHNfy+vj3ZFI0gKK+CfdNaceX7sW7O3Km6rcbSajmmcRmsziK12pvUCkur0ZSXQ7xuqZp9S6ZOHvqNLcqrCKZsKXuCtrmVN8RbA3tY2qHfUwvov3NLQTVwrevCAhPqgyKkcfqRioWTeGbNztm7pCtSM06YdGaEgoe7ni+qmBJpgUPctLR+9RsN2oOZZ+PkveXm6KyPMmExBgz/JM7EA90LNNPRjlUXn+Zhj8JQNSoDfqY3+km4gEsUO6eLxLcmYYXi+6GedcPsULgZlWGD1jWvQJj+sf0JT HblWW2Jk HvlNO4DRQvY1l42DEI5kq/Ccb69ZPSBhDQEgGxkkeYL8qpeA8052AifIAcpW3c34tasLpvL160mWHIh8HGIslEP0cjdN3A4CmKvOvAJ8J9OWaRW/QIbKg/GsBaWB5AR0oLGMVrn2o6egCs4TyG3fW/HRwTPbMfbA8waJCKxWVCxaloxvpoRaOAx3BDrtLO4Mcbye5e56zrjgx4voE8t2p0uSeoT2PsJhR0cahNx2auStRhE842azE1JCEjQlXS1jQkDyqDrqc5VFGWB/JdH6riT8eYxReASqvqegQPidw6kqIQHr6giinNgU8PR6XV4B21jbXosVhqYZ+8rPBnTAMe7UGNlREfJNAqygr50VPzD4EcU2xBJct5HzhGS/W0V+fB99lyPP8NrnFnDhVD8Bu+oCAoxQmPSgOO7/ObxYjvCbi31vNoV7o1LI/Xg== 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: Convert to using a vm_account structure to account pinned memory to both the mm and the pins cgroup. Signed-off-by: Alistair Popple Cc: Christian Benvenuti Cc: Nelson Escobar Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/hw/usnic/usnic_uiom.c | 13 +++++-------- drivers/infiniband/hw/usnic/usnic_uiom.h | 1 + 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.c b/drivers/infiniband/hw/usnic/usnic_uiom.c index c301b3b..250276e 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.c +++ b/drivers/infiniband/hw/usnic/usnic_uiom.c @@ -89,8 +89,6 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, struct page **page_list; struct scatterlist *sg; struct usnic_uiom_chunk *chunk; - unsigned long locked; - unsigned long lock_limit; unsigned long cur_base; unsigned long npages; int ret; @@ -123,10 +121,8 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, uiomr->owning_mm = mm = current->mm; mmap_read_lock(mm); - locked = atomic64_add_return(npages, ¤t->mm->pinned_vm); - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) { + vm_account_init_current(&uiomr->vm_account); + if (vm_account_pinned(&uiomr->vm_account, npages)) { ret = -ENOMEM; goto out; } @@ -178,7 +174,8 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, out: if (ret < 0) { usnic_uiom_put_pages(chunk_list, 0); - atomic64_sub(npages, ¤t->mm->pinned_vm); + vm_unaccount_pinned(&uiomr->vm_account, npages); + vm_account_release(&uiomr->vm_account); } else mmgrab(uiomr->owning_mm); @@ -430,7 +427,7 @@ void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr) { __usnic_uiom_reg_release(uiomr->pd, uiomr, 1); - atomic64_sub(usnic_uiom_num_pages(uiomr), &uiomr->owning_mm->pinned_vm); + vm_unaccount_pinned(&uiomr->vm_account, usnic_uiom_num_pages(uiomr)); __usnic_uiom_release_tail(uiomr); } diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.h b/drivers/infiniband/hw/usnic/usnic_uiom.h index 5a9acf9..5c296a7 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.h +++ b/drivers/infiniband/hw/usnic/usnic_uiom.h @@ -72,6 +72,7 @@ struct usnic_uiom_reg { struct list_head chunk_list; struct work_struct work; struct mm_struct *owning_mm; + struct vm_account vm_account; }; struct usnic_uiom_chunk { From patchwork Tue Jan 24 05:42:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113488 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 2228CC38142 for ; Tue, 24 Jan 2023 05:44:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B17026B0083; Tue, 24 Jan 2023 00:44:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AC7A56B0085; Tue, 24 Jan 2023 00:44:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9427A6B0087; Tue, 24 Jan 2023 00:44:54 -0500 (EST) 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 81B836B0083 for ; Tue, 24 Jan 2023 00:44:54 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 58D7C1A0137 for ; Tue, 24 Jan 2023 05:44:54 +0000 (UTC) X-FDA: 80388603708.08.CA3E934 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2057.outbound.protection.outlook.com [40.107.100.57]) by imf15.hostedemail.com (Postfix) with ESMTP id 89F9EA0011 for ; Tue, 24 Jan 2023 05:44:51 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=b5fIBhQy; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.57 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539091; 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=nD8gOnzuByFYMkvEBPVA+p64gDK/jl6+XilYj2DMivY=; b=UDIDv3amnWbINN2bKxP5GxoqEWkLUCcK4neiclPWdHB6ZFR4rw+HCp5SXT1ftjY7lFa3xx DxCyKmmOPCGOU27Km27gM5usnHx8QSu5nq0uKAr1pu9gsyTL6in6r5h520xLDwr0TdKaiB 8X4fsAoAoPSDSwIT3C6oxfjS0PjtLag= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=b5fIBhQy; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.57 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539091; a=rsa-sha256; cv=pass; b=DwhMkCrAgT8ByFr8I5nayPGmXpUoSOHB0Z2AG3qtoAtg6nVW89mZb0eXgncHYPteI1ljUL 80Rodw/AGgUZPaC1dOsatRAhES8+IdQWarUva1OoMSZGYB1dVO13rqXE9C4PO3fJK9dydP KtQTC2zyAurgxXDvTCu3kVK+duBwpjM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iiZ8g149rNqbvxPAbRYCg/mAZswHfCczb7F4VxWx3QN916h8yo1OyIFNubcFuiebxxlPZM3jZdFme5Hptvzbdum2IISpG2UQNxS5JXUEJ8IiF/da5b/6A5lQiAsHeSCbC3imB83IiYj1Sy++q9LMO4Xv99OeARZ185NDxskkAbL7V6I6c1JSMR2K/ZUHAZOfxAR+B9hjUzCKa//PCLTWJj5nCheJ7tsdCkfmG5v4FPMYpowbOBSySEnGCSb+U3cX8zDLq8lCPVUB+2gd88tx6dV01kVqcq3KqrCw0/b0CkrJYIAY92GINbSpeaGxu53PEux8riem99M9BiTolPxZjg== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nD8gOnzuByFYMkvEBPVA+p64gDK/jl6+XilYj2DMivY=; b=h9dsXDx1S2Oy6zSV62LGK/Qtx47oyMZdsj2FlHt3FY8A/sMeGOR6sNa4QKnxQLWC98jCN79lwhNIiutGWugjGqwaBBfJy0xGL9dY2Ar4p3p5wShVs7fuIwkSmHlu0mp0a74mfNG40tia1PPhOAWLPrlIUYvgATsLD/eLLVw7z6AKdzPXPdiasSeX4jwINek5r+oRSdaa/CoLc1tIoNSC1T5fpz9l2tYWvDJVelOC7qEHGstk6wBQArJIw2VtdsNVTVbkn0tMW8ullZz3BrAB2x1N8rx0GrFlX1ibwnB7OXAusWvBmARmkLa2XTJYPjEDE5qBFsmAteJhzEZUAUkfHg== 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=nD8gOnzuByFYMkvEBPVA+p64gDK/jl6+XilYj2DMivY=; b=b5fIBhQy0+x/JFIklkT8yrZxAs2+ODukZzTkXjwMv3lnDsdva7NAqyd3UDoYKDDFjB1Oy1rCR5z/p1MecTvpKZHBmj84dzAr6UkDsWyK5zlt6VFwOLV7Zrk5uHGXcHbGthtN7rRTJ0o/TZU+Z7ngVz88MvdaObLV2gmGOvF8b7KH7lGo2VxZKRyiOqvQXBPHbKM2EcM3YuDgRkYsHntO2kGV59iXlhQJfGpBc/hhgUIimp/7aLKa9oT17yvo0kq1oFB194ukRPNs7loQ8BeWd+MBkEkfEQ1hM9b0zH1LEya/jmmhC4emMA6gIAJyfm6tM1pCtz7Sa/hXJwdkq1mqCg== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:49 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:49 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Subject: [RFC PATCH 07/19] vfio/type1: Charge pinned pages to pinned_vm instead of locked_vm Date: Tue, 24 Jan 2023 16:42:36 +1100 Message-Id: <1235619c5394aa67fe116f89f22fafa370707c90.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY3PR01CA0108.ausprd01.prod.outlook.com (2603:10c6:0:1a::17) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: 08ec6db1-7c20-4aa1-a716-08dafdce1b47 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ckyxsZV3cU8Gxbct3IlUkPyRUJ0jpyA+Tuu+X+Y9XTseJaO60NqaAbQ8kJn4gtsrv8ilwjmeM+26B/9+oL1JUZuJpF4B2o4iD0X8n2dFRokl1NehOU4RAXKuPP3Dqnz9jCdFKst/SpdMXRVBcM8SIr2jbG1qT0ZNUiSf0x7IkEDDyADI/KRBI9oUPKXNRbtsk6atdS+wqAxyKTC+Cl6wRMET9wIhApoHgK0U7KkgpUg+xlqfjMvymg73xkeW6U1qp1t18VJqkLQZE+hS739FlNmxz7s7E2QIu5Az7Ary92eWD9EWhhagNc39No9six39gDZJgGWYbJ9MiTdg0SGf0Ed10+4Wcs6OsHLY0hJsiCvBttxBjRqwZFam1Ol3prLtsl7pwmXhYkKKIfn7eFwWQ3W0OBBUINIbs4J9N0BpN6oycZuHqVFGvQCD4FG4cQJJh6mVjj5xUW2UIHU0mcRU+AALCCyfuMzRgRdzOi3HVd30FABTBAyDcs/qN2MATULMbppVTA+z240Vdd8WlgJwlnmkRZTDzeSlRJDZfev21+OJrR8ARONHomFknXW/9EBLkv2NKnG69yaJRMhK+PHPp/VwykBZDFKFqs4FLY10vUGDPovHsYTfzVg0vEPtDf3acqwpnxgpWiz4QnyDNFKn1g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ugmqBoExZdq773BJjVgPk9diRP2k5+6T+mpslsgdDMF8ZmdjXP64Ou3bZpCFBmbJs/bT0amvZ4vUYu81cnq3wVM3i3Ay3s9rS8VamOUlMHBk6rHdRtY8OHfTcYwtEkZOelPKfbSlTT0tZbSgUzp6bOutrGnZG5HzaxMySBW5F4x84fdBdpXvqq+fudIZY5/H2rzQaRnouk8p2cJ0xmka+XNdzhbtIUfyxD3a+nqReGFDkqyGU6GXETHQaYLmFIyzyfT0QJgofzXG73tAzJTNivS0pGmF4gHXlbFQdJRqqorzutKCmi1P7PPQvAV7R6u/gCqE6dFD4bKGdM/SNudsfn1dPIoGCE63SB9DxzDomntWF9dkaS3L72ydZRwZmQowzheXPY1Uzjv0FAo2Z4lFqNGV3PWqNSj3L4RK3ob6tlTdeliJmPdj/9txaZ0bOW2ZTFehuZee1BEefWRaDiPRJZ2u556Bj4/yJDiUhU/zcYk1MjUDnpJxCe+rLfXRFpmgH275qTIupZZ4BD4vEt7sP7sByxsB7GdCZfIauxeMy3lhKShImiz1Rpcn3wqeFCuLlcRzOwrWEq6u1xS0B+IDiFDEJdqfzBbwODLtiYaPI6XnFb7oWR6kZoqBIr0O67Oqnc7PYh9lT4UiRbnrf4IzmY819OS7I6FAE5g0DLupRyQ5vpU9vWZKsqX9uA/ILap4VnlwnjD8B0zfK3yGOclMzXL2GdLtC57jxH9Rw6FWbbpa6sy1XT0Ccdmy8IQa4KZITDOJjSDSm4a9ShsgibwR3TKWlgHh6p11izQwqj5kiqg0F4dR4OiUec0wtcanRmgBWhoym8Hi6zxkrL4d+B9MNQ0Eg+z9ShcJSLUOUsppFotUcy5F6lVbGIuejIzyjYwEDlGZi8dUam68U/0GMxvSnclj8aWnJPWjNBr2xuxwqksGJN1q38tZar4cufZW2KzQCMcKzuLPKeFOo+DCteyauhluVIfzBMNYeAKAWj2GHedevq6rPen7Gj3OqF8Vomzxfh0qvTaq0bsEZCxtaE+q5UeXNNcXeptWHT2w4kG2ipd2jjbaWISYsDfqXL7+d+CWXMTItyLTFtM63eUOO/kAqcSXmksDtnQLWRwzWqvMYhck/K/57GIWW7g7ixuIY1rMNMRKBdM9S23jmRMHY/Y2cSYu+YDlysV6ilOAzWfei33meOrhIyTS12Uy3Ta62upz1Co+lRB3+pNLiyXsRTtx7Yga19eWy5HkccduTz9Bz6M/Mv6/Tr5UlQbdvps2gQZDl61TV/r/ToHw5oMHO894yo8v71ZUciHM70/qSVuzMcgGnyLfznZVbC/Psx+eB+INSHtNp4viWE6jLJtP60KUYRewW2psg6TzuEWQLtR67y9lqKzLQxcUC9DSXRsY8iRWWHxmjS5FfwBYcYk4/PRbne315BzA1GB80yFOHsgoRLOkHPpbdd6SpCw3m7Yn7dG2rHvEusByBR9ZiCcDLyyAq9hC4trxq3UDyiBaa9flzC57K7YKge5qI/L10TDeKXc6HV/Wjqpc2qHY/UPE74CWLqkxydbn3sb+zHjn7HN4i9j9tCmOHI/RQkraSZ+cEl4l X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 08ec6db1-7c20-4aa1-a716-08dafdce1b47 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:49.0992 (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: Lz20/FDFqpX0w2MfN311iAM1LhjjmmW3WW40dGhXvzR1ci4VG2mk5UGRp8KCYxqn3EbdKQNpSkpivfiMbw0jJw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspamd-Queue-Id: 89F9EA0011 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 9fqq59ubcf99gjoqurun54zn61it9jw4 X-HE-Tag: 1674539091-385510 X-HE-Meta: U2FsdGVkX18C09jglGbiJsnveixnIurqRxQQJs30nXYLaFfRaBlFNg+PRtz++Cl4cQwCQY98iEQNAnCvZft3i8R+0Kll+mSP/b4LDh/sXCDcFvwJPneOBKFaedseLkSvI1n1Zq8LW5fmJ/+5bAbRnCGQ1d3LabbzRlY7yoRLZPZmgCoiljxO4qn3+uTdCXIDWWMCrteviY2KhxaaIwip6fAcbjW4VJdkpq3iUZUj+dafzvXoOVVefTtHp9UJRwnPyQW8/UwkNfC5F852l09dt0lcsU2eyFMDwOVlJ5iXNRDJFUh2rNevkCQvIIeawq/JY1yveNDp3jZ2yErsBLpS20npDRjl0okJFwEtBoxxLxLS4n7u87rTgI04qWkwitElCHK3RKxKsecRVeX/Zj5zW+RA1v4ShRlIz4h149A21cPVrSiHVf56Uve8cNN7z+KScJAWONeWDvrKEMn1ir2yqTa/DPbQedWA6CNZJ08Vw3w77ZNyuWb/IGgKgWbo9+cywEpTMw6u5u6IPppi1xXDxoVhYSvuYmrLrEzEfFyKXyJQlRBazGnXT/OUBSHXciINvBceYCsA84YuPOrDXBKGW3pzsUK4oJeFmtdNEdweawa/CzCmyanwiWaT+9zI+NbuolrOPxzww+twG1kxBSBcJdXJZCS9CvUNwobVI3kgbEtV2wei1ZrFHLnM5wcizGWOG1cjy/2nZUbEUh0fJrnkYShErIVZdYoDhEZ1Ks8ZQ9rkhDnE4hm5KjPKkLK/2ki3cZUN2/9ARhMMApdGHAkvFHbT4fjX4gnLLapszku7T6sI0tUyZsaaz437dXjwelBd1bY6/hh7WlO1jfLmJvxY4t4jnGGp0ZHQPD11ieGHy2E7QSZ85t/7OemKP1WVXqccpKUYPq7EwmKMpL9i9DjGemser2iwy0lwNvAeWu/E7ZQrETJUPm7u0f1d/8cFoXoY5PdzsKA+kfyQit6DaGE q1R5Kbup VDmsL2Cjzs1nmpwBPXNUpXjweBWiHmUfXf3WaLkExSbPmf1ym3/L28DHEgrYDXxcF+LzT5XAvJDgdTjvGGZZmMtG0t5jo0xVJAXYluPsHCRPKEBZferB/Ng+CmQLJtSg31Tqb+K9FmE7OMQyOL+CUQ27YmbLyeLDPDmwasgqMYbItfeJsxxFE7p5ySLUGChld+zv/0K4v/pGxp0Iu40r4XVZyZt2opyp2JAe4n9E3wRXbK0NGUJSWgSTb79X1X1jthHhi7419lXlYmdkaoyRSMozvLsHdEjZCQBf9XIvQxRadlqh6WhjQlpIL9J/VKV73xaPmYf/GA1Q2Bs0mRqKRO4r4O9g0K2GGRvlA2cMwaEQ5yXIAWkGR0Pgggw== 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: This switches the charge to pinned_vm to be consistent with other drivers that pin pages with FOLL_LONGTERM. It also allows the use of the vm_account helper struct which makes a future change to implement cgroup accounting of pinned pages easier to implement as that requires a reference to the cgroup to be maintained. Signed-off-by: Alistair Popple Cc: Alex Williamson Cc: Cornelia Huck Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/vfio/vfio_iommu_type1.c | 59 +++++++++------------------------- 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 23c24fe..828f6c7 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -95,11 +95,11 @@ struct vfio_dma { size_t size; /* Map size (bytes) */ int prot; /* IOMMU_READ/WRITE */ bool iommu_mapped; - bool lock_cap; /* capable(CAP_IPC_LOCK) */ bool vaddr_invalid; struct task_struct *task; struct rb_root pfn_list; /* Ex-user pinned pfn list */ unsigned long *bitmap; + struct vm_account vm_account; }; struct vfio_batch { @@ -412,31 +412,6 @@ static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn) return ret; } -static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async) -{ - struct mm_struct *mm; - int ret; - - if (!npage) - return 0; - - mm = async ? get_task_mm(dma->task) : dma->task->mm; - if (!mm) - return -ESRCH; /* process exited */ - - ret = mmap_write_lock_killable(mm); - if (!ret) { - ret = __account_locked_vm(mm, abs(npage), npage > 0, dma->task, - dma->lock_cap); - mmap_write_unlock(mm); - } - - if (async) - mmput(mm); - - return ret; -} - /* * Some mappings aren't backed by a struct page, for example an mmap'd * MMIO range for our own or another device. These use a different @@ -715,16 +690,8 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, * externally pinned pages are already counted against * the user. */ - if (!rsvd && !vfio_find_vpfn(dma, iova)) { - if (!dma->lock_cap && - mm->locked_vm + lock_acct + 1 > limit) { - pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", - __func__, limit << PAGE_SHIFT); - ret = -ENOMEM; - goto unpin_out; - } + if (!rsvd && !vfio_find_vpfn(dma, iova)) lock_acct++; - } pinned++; npage--; @@ -744,7 +711,11 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, } out: - ret = vfio_lock_acct(dma, lock_acct, false); + if (vm_account_pinned(&dma->vm_account, lock_acct)) { + ret = -ENOMEM; + lock_acct = 0; + pr_warn("%s: RLIMIT_MEMLOCK exceeded\n", __func__); + } unpin_out: if (batch->size == 1 && !batch->offset) { @@ -759,6 +730,7 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, put_pfn(pfn, dma->prot); } vfio_batch_unpin(batch, dma); + vm_unaccount_pinned(&dma->vm_account, lock_acct); return ret; } @@ -782,7 +754,7 @@ static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova, } if (do_accounting) - vfio_lock_acct(dma, locked - unlocked, true); + vm_unaccount_pinned(&dma->vm_account, locked - unlocked); return unlocked; } @@ -805,7 +777,7 @@ static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr, ret = 0; if (do_accounting && !is_invalid_reserved_pfn(*pfn_base)) { - ret = vfio_lock_acct(dma, 1, true); + ret = vm_account_pinned(&dma->vm_account, 1); if (ret) { put_pfn(*pfn_base, dma->prot); if (ret == -ENOMEM) @@ -833,7 +805,7 @@ static int vfio_unpin_page_external(struct vfio_dma *dma, dma_addr_t iova, unlocked = vfio_iova_put_vfio_pfn(dma, vpfn); if (do_accounting) - vfio_lock_acct(dma, -unlocked, true); + vm_unaccount_pinned(&dma->vm_account, unlocked); return unlocked; } @@ -921,7 +893,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data, ret = vfio_add_to_pfn_list(dma, iova, phys_pfn); if (ret) { if (put_pfn(phys_pfn, dma->prot) && do_accounting) - vfio_lock_acct(dma, -1, true); + vm_unaccount_pinned(&dma->vm_account, 1); goto pin_unwind; } @@ -1162,7 +1134,7 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma, } if (do_accounting) { - vfio_lock_acct(dma, -unlocked, true); + vm_unaccount_pinned(&dma->vm_account, unlocked); return 0; } return unlocked; @@ -1674,7 +1646,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, */ get_task_struct(current->group_leader); dma->task = current->group_leader; - dma->lock_cap = capable(CAP_IPC_LOCK); + vm_account_init(&dma->vm_account, dma->task, NULL, VM_ACCOUNT_TASK | + (capable(CAP_IPC_LOCK) ? VM_ACCOUNT_BYPASS : 0)); dma->pfn_list = RB_ROOT; @@ -2398,7 +2371,7 @@ static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu) if (!is_invalid_reserved_pfn(vpfn->pfn)) locked++; } - vfio_lock_acct(dma, locked - unlocked, true); + vm_unaccount_pinned(&dma->vm_account, locked - unlocked); } } From patchwork Tue Jan 24 05:42:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113489 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 D1EA7C54E94 for ; Tue, 24 Jan 2023 05:45:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 76DFA6B0085; Tue, 24 Jan 2023 00:45:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 71E186B0087; Tue, 24 Jan 2023 00:45:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BECD6B0088; Tue, 24 Jan 2023 00:45:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4F2E66B0085 for ; Tue, 24 Jan 2023 00:45:05 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 30FE012038C for ; Tue, 24 Jan 2023 05:45:05 +0000 (UTC) X-FDA: 80388604170.18.90C747B Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2083.outbound.protection.outlook.com [40.107.100.83]) by imf04.hostedemail.com (Postfix) with ESMTP id 601694001A for ; Tue, 24 Jan 2023 05:45:01 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rihIAfNv; spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.83 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539101; a=rsa-sha256; cv=pass; b=u+I7DDtj5gXHf4+/HTyjeeAwwvu2xOdbtYmBblkY/c5Sh8gfmzqD7gQrYXN43DsRgCY6Rh tt1cEBUeuatHK9vrR/tma4bsPhBLmKKJoerYaR5VHyo8uRpICBVNgRmPPEXlHw+d+e5gNb xUqYbJtu5YliXwFp2mZiqxFNKGa1w1E= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rihIAfNv; spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.83 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539101; 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=m+GpPc9JMDnZkAHSZ8sR9hzGtxuRq+aAmVdCeEX8IvI=; b=t/hmvb75zi+3kSKMk8o+M45xEOjypaE9AUGTO2Hk+xHZG/nI1qZtETziJNk/1RO/sDoaSj 0OQ7JywDuBT4F7l1eFrcgMQgGfc7TUSKaywhrWDn6Eb9xtKC1WY6naLzNgAl4FaN79PU2z Bm9jw5lTO/jo6FSisIl6Z8OC258WaBE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dqLqCL4Xrebo0k34TfOtge83s+5RHqcyYC9a7m5kIxlVbXmN8lTtx19cruAMncqvS1Lpy3FqGVl++pBWrmWApNh3GEcus4Oj1+2e2IguM06iTh/a6pZ9b6lJHQAfSRc6wZ1LDr3uCd7TGxiZFWeIBBjjXs+Lj/2OU4m+0ZzfAZecerzNMkjg5liPzAO+fp9/e0DsJvpDz5ycqMOSoyn6cRrLA0TKlJ8DvKvmnulmX0Uaagg3mFYc8qp/2wssf9M2r4r5J5/3O7MuONUiFCvP7p9zXeJZhUiw12STGsgMHqmAe5CHwI1JD6r617/tjIQli55Tw/zqJOmiKZCnGCrAuw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=m+GpPc9JMDnZkAHSZ8sR9hzGtxuRq+aAmVdCeEX8IvI=; b=QtQLv6/70SYVzJSSwOQ6LjW+RlJrp/Vy0PZUq11hFo+zE6mVgUXxSpSM4qVcvoRHQCjb0Ym3NjbwpUnKlKKDBoaSe0i86nG0f8ZtfUyZlSc47F6u8q0o9KTnIOBfZpBosUr4eDSpti9nv8VYgFeMIW3mLfotO+VdFvq0++SG+JQJ47at+ZsjROZkYSjp+wiP1H3KpHlBmr45E80pDceMsYSbZtKwqLjbJ1esytXIGkswb5Sy5OwKurIvTF8ae3Z3G1FboDNOL4/TGnipsJabyGXfzI4uynPMkNIls4wjONZN88iVIFwJSs8cn3h/i3FHhuM3yZh5TTvtWwssfOzp4Q== 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=m+GpPc9JMDnZkAHSZ8sR9hzGtxuRq+aAmVdCeEX8IvI=; b=rihIAfNvyXZqnt+0xpvBWi1FoOEFMDZBokr7LRw/2EeqNaDoznZEaZCIk7alKXs6YLIP/L+OJYqcgHI8LXxHtaUBRQ0X/ChlubjWtH0kgXjiGhMH43KGqLWJLDR7VZAHUaeybPg7hGCMNHM/2oTIihl0KGyhiS0Wo45iP1erH7UQSnbWc+qLdm45iO7QGDKmR+bSBccItXleOP7m4XQO0WeAS8Zz42iQwf2l52k5CTbPIb1JvA4bg80r0DzsFfyx26NILghbAJ/Wme7C9+ki5b47FvjcR0BjH7bY79f3PG3j2m2kII4lrUoBxMJbxh/51rkr6yyPgTh7hrc4bmlLfA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:44:59 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:44:59 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Alex Williamson , Cornelia Huck , Alexey Kardashevskiy , linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org Subject: [RFC PATCH 08/19] vfio/spapr_tce: Convert accounting to pinned_vm Date: Tue, 24 Jan 2023 16:42:37 +1100 Message-Id: <876047347220d490313293a41e261999d1619030.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0107.ausprd01.prod.outlook.com (2603:10c6:10:111::22) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: 9128fe6a-a8d4-4058-34c5-08dafdce2193 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /3+XB99T69Q3lAMRsGO7c+nCkNrThSNDPOchsfank+XgPKEeX2pod1CGpu1nBsYA2/oY+aVhnGTjghijvddHzC6eRWgc5L9mO3Bq2cydfR239wFEHNrIH4mID5rG6skIVsQDVmlpcPIUnD7HA7v0hohWTBXLis9nQFDcgbncGmmVziuuB/Uy3nWLSFN3EJ83koHij3IeMOuT8731WZSomDPa2Yo9CuBk+fH12FoKvpx1wcv1FPLmNWIQw/FLR9D2XlS5EFh0jMpSKiVpcmBKBYWJuHub+xDb4oyF/p5YlfyZDRYnohIHrHd0+pokADQ7535OBM3eZpwGx91ex5k/gyBwdYDECbJGV1KQ8E/TUPcsK5nYkg7cjKkmuJYRxJbf6eqA7r+GCapy+X2AkHheVMQZJ5qQDJ5Hq8QKIttbcEhLSEQ6hXFcUJK2AFW/0vobjLkm20SXQJe1pIXH0q3dbMFOCoAuiIVyTUGVDACoyBuWyiTUgc0tZ6ibgt0ZSxFUCEj/AdTLFyGbQbeSDe1ha4ifuFGtRAZuHpPS1tJK7XYqqhCFT7LP9idsm9xp8tHT/4fa5XICROIdmPcCH8mLbQbgvcyuTubh48VRqDuyCFjMcSiX5CUAQrAD+pnhfBRKtl8VBDIMFYAmo2JjGMZVOA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(15650500001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4+8GMcisTFhM8s8uMV4vH7eZVLAk7S8prHWyO4EaB2XxrDYQ0ffHEaKTFgOw3Jp66KTkCFl1kaAkYl3/GvekfqjNX27Pouf0/59oxHkiBSNxH05LSpZuFLXS6u6DvUMje9VboikGMTC1KfbSxxCJBO+d0Pv9uvd3GfihYL4j0Ah9R5c6n6Khk7JNSR7jvdnkW5klT06wyZ1WVXnLkq2wjnSLxIVWWzaoItCYogQ8mBIis1Fx2mXW6VhN4kZXHxVYhRxV8kjZ+Jh+roJF9wNPeJGvBsiPbAU9vzACDmkS6vMXBML77eScJD45KUYdedyIfyIhGrXZ+fu2rLmIeND1AwrHC3koo+z1UHwsVM4mEFen8Em5aKAh4kEkhHgIcOnTnF/LQ4Ex/dEoI4BDG9Q2PdfrEfOQ7FRku9CZxAYndrK2tVZZgjrKIEzIsucESrhD8uKdz73L/uQdPsOtCstY87zIBdaPgRZdHrisDGHa4aigiNTpsvZJKex1Pn7iDOl/gPfx4S+ZvQasTYKOriWZ3Mi/Y5qiZUQraMddS4E1lmIpQZrfriHk4mLef3FOZ1nCsXUrZlsOSXFbzz1kyDN3bFLG2pZzOE2Lb6eRycyjr4zcBxDgWSE5j/AdgS88XzNbpetEdt0AVRYeVKvDDgeNoPs1uaPt78/1g25jcxXvvWtmLODHpnRIjNlbiBYVXLdYbriO5fZ9thbChOdkMdS2xeZQ3cehFHqnHXeJmcZOhtS1D6RSAyx6vGQMiH8pMZeWSN57ODAU1iLcJs6C/UEtxKgAKrOxTkJ+JtNMeF1+WlJJbAsoA9IqYjOLQNFZgMObMjnR46Xuibqc8i3oGkR22hAuy7gco/bAO0jQfxvh10lUXBlR9Ll270KeD+IZcADIMZz2ocHLGSjSn6uy+NUeQWCmxIz8LK//3NgAr/ZS8FqvDsYIc+QI0IdWxteu88bVb+NoPKB5OrVaxTnVNe0vtEOimlnk+NkansgUaO16mWKKML+lGQ0AgAr6g02XLW4yfRj86EZGBI1MDfiYgiTlwUY5Tjxn3ivuALbrpEyBZ9fJdUbhgNumH5qmwyzFRx6/3z2NHURYMPT/A6SuiYxClN9fOaBnYpHWi9jrsLIcSAgdJBeeYDcfBEqKd7eKWpj9pw6LbAXqqTyd2sMIkE4OwBxSqkWsRtgDIgtl/fidFbWDfQnzKBeo2VtCyDb0Vercxy/0bfq2WQ0qOugbCGX8oZwmO75z19WKF4bWaB1KayiELlf+NBlqnLKGnpm43/E8m1QKfL0DkCFC29jiMFWXd2cH71Kdxsaw4JPO9ycJJUiHe56fhmn2l0rIv/hzF0qdDgMoayud+4wxWGyj4m0DwPBqJcVVMQdxoRtBgp0V+lexZLsU6Vw1i40aQIbj7AP/XnfxIGoCjwGPzzV+Ml2VRtpkT3MZYURezr9DWcG1RTAUAVqhzWxzv3QgynE7xFVGvdNWMt6umLdMSfSILalL9vfTNr06Bo/AvO6dQrHRNutIqhTggrLtdwh0U2nZsFsAT1fenzPnQTo7zp35EiL+NcBF9yddhqXZus0p8TrKxomV1UHvSHqQ7BdOUGmjQFBz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9128fe6a-a8d4-4058-34c5-08dafdce2193 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:44:59.6607 (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: BSiwlseL49wQFH6uMDvamSQHHANtMiAEurRev7BDXlwueAwjb98EZ2RWcN5kz7e7kjSKnyDV64U3gBGuucKB1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Queue-Id: 601694001A X-Rspamd-Server: rspam01 X-Stat-Signature: 3fcedc7kfzigudwcodwariiqzsizk3c5 X-HE-Tag: 1674539101-944947 X-HE-Meta: U2FsdGVkX19AhpPe7jB24pXfo1cWo9ImYNfuZMKAKRMoXSt5P5SwBUDxyETYE9MXRFnQVfa2DNS6plteCcoyWb5+jjU4gLaK6POQQnDK6BJDr00hMV1Pd841UOvIzYM5cFWRV86mxaouFqRKx9ew9g2Fqc7u60eXa1y6afEcuwiEpDevDqeE8prRfeuvIEQO/qBQZ8hHyOWRgLw2hdL92lmO7YM9IZSWD1M86JSiF3Tv+TPtpfC95unuIRGyfee+EdHgDCwGS5AZkLQZbqwzsfmWODCd3pX66ORhLT4hX6tW6dxaFlN9cjw5IOgb8hMWhomYESN21AQt0tKLnTJnqSBkxhYKk/7O+sVM2og8BYKO4IwtY6cJm1NhnLKzfgW6UDWvTVgPu2m6Q/vLn+EGsb26QAlAwT6shkIRRgJmp5emD7JTJICsaJqYBibmriCcaN97ktIDl19lIZ0DuZ37huGKGODhedBn4AzpUKHEbhNWKgiPpVpULwKNtoVQ98EkWEUveK+r7u8DB9N2a87YnYNhy551QHcvsP5tuhbv5Z4sU3ulMgKFQyV4PrtRQW2GGcpestrgJsa6+OnGAuYuUAyOg5Z6F91rDiTs7lmNGKGM7dZ6gmkfijwfYcxjlvAgy0uhXD+BGUr6H//TonYs5GDfeomycnxHGScw/i7ls8su5C5e5HZHwT7hqBi9GWqjYAPF/1kJoC4NEea0O+VIDSiXc+LLN4L8NHLUQwLCqpS4EKSgWkusF/kLgWnyfqV6AO9XnV0Zd5fPpDVYSpV4+CoVbIPu0UivopbQbnWUXXu+8deEEXhZY+xFuncf/GudVy2zC3hob03D4SAYoVjkl0GKnTSnKd+04ZgyecyYuX2hHDgLoc5MBW5Cn6LJEvo8G7AWWHhqqVqMO71RIvBRJyDv06y/ihtMEQr9QuNgGP9A9A6KKyZQGI/rgnBAmZ/3zkw9+HmNtOiL/lyM3+0 KrO5M94F HcvTD9/qrUAAlpdswOHaaLiiJOks7i8/TNlZtJ5fgYT9/COhm7apDcZP106Qo4fNyvxRgDnkjrFutsY0JbJ9e9DBSjzgDVH2nv0YEEToBUsFoxI9vz907KouU2vKHAngl8eN8KVcPLuZhStDgIKkEXpfHmGy1fVjedJYPVkdoGcug9kceWAi79OxjNEc1bSog9hCevt//IfOyHm5A7Vf5j5bWqx/eIDQxV7/JZdWkYhznBbYOWKSunvQyk5U/Ux15e+e9GLHlbv7MwtA5O/pDkRTbkn/q09yYS0c+Budb2fSsAGuMi+ALQxXxT1chAzNNgvIZYvQWQwfjc4BYr3GyY8AFNnBMiTTbPA8SylMLIHisTrh2PKdmTEiQPm4I51+yC0GI5fHMdBF0OyxDVodLGXviDZT3tNDznG20uCGMkq4b1o7Ix5InbXq5h5VJeR0l5CPkOsOpLFXy7oG083hbCGNeECawEi6bee/TOj/F6GcLCJMIHeEqRT6acdIcXFmJvBOMtPHXrOqvojcaIqYHZ9/Y2hNeeHJfhQ+FsYdYgo+ksg68X+a55nvYMNMs+G+Rlea5g3Fms2WBRO0ZDEeN4lYHd93ePENLDXUCQbnm+kRzrakXCVw3kDFKkpiD1L2pgpAlEnzpU7zMzT1CO37pTVIq2Q== 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: Convert from accounting pages against locked_vm to accounting them to pinned_vm. This allows struct vm_account to be used to track the mm_struct used to charge the pages. A future change also uses this to track a cgroup for controlling pinned pages. Signed-off-by: Alistair Popple Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Alex Williamson Cc: Cornelia Huck Cc: Alexey Kardashevskiy Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org --- arch/powerpc/mm/book3s64/iommu_api.c | 29 ++++++++++++++++++----------- drivers/vfio/vfio_iommu_spapr_tce.c | 15 ++++++++++----- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/arch/powerpc/mm/book3s64/iommu_api.c b/arch/powerpc/mm/book3s64/iommu_api.c index 7fcfba1..6c57603 100644 --- a/arch/powerpc/mm/book3s64/iommu_api.c +++ b/arch/powerpc/mm/book3s64/iommu_api.c @@ -30,6 +30,7 @@ struct mm_iommu_table_group_mem_t { unsigned long used; atomic64_t mapped; unsigned int pageshift; + struct vm_account vm_account; u64 ua; /* userspace address */ u64 entries; /* number of entries in hpas/hpages[] */ /* @@ -62,20 +63,24 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua, unsigned int pageshift; unsigned long entry, chunk; - if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) { - ret = account_locked_vm(mm, entries, true); - if (ret) - return ret; - - locked_entries = entries; - } - mem = kzalloc(sizeof(*mem), GFP_KERNEL); if (!mem) { ret = -ENOMEM; goto unlock_exit; } + vm_account_init_current(&mem->vm_account); + if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) { + ret = vm_account_pinned(&mem->vm_account, entries); + if (ret) { + vm_account_release(&mem->vm_account); + kfree(mem); + return ret; + } + + locked_entries = entries; + } + if (dev_hpa != MM_IOMMU_TABLE_INVALID_HPA) { mem->pageshift = __ffs(dev_hpa | (entries << PAGE_SHIFT)); mem->dev_hpa = dev_hpa; @@ -175,10 +180,11 @@ static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua, unpin_user_pages(mem->hpages, pinned); vfree(mem->hpas); - kfree(mem); unlock_exit: - account_locked_vm(mm, locked_entries, false); + vm_unaccount_pinned(&mem->vm_account, locked_entries); + vm_account_release(&mem->vm_account); + kfree(mem); return ret; } @@ -229,6 +235,7 @@ static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem) mm_iommu_unpin(mem); vfree(mem->hpas); + vm_account_release(&mem->vm_account); kfree(mem); } @@ -279,7 +286,7 @@ long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem) unlock_exit: mutex_unlock(&mem_list_mutex); - account_locked_vm(mm, unlock_entries, false); + vm_unaccount_pinned(&mem->vm_account, unlock_entries); return ret; } diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c index 60a50ce..a58281b 100644 --- a/drivers/vfio/vfio_iommu_spapr_tce.c +++ b/drivers/vfio/vfio_iommu_spapr_tce.c @@ -67,6 +67,7 @@ struct tce_container { bool def_window_pending; unsigned long locked_pages; struct mm_struct *mm; + struct vm_account vm_account; struct iommu_table *tables[IOMMU_TABLE_GROUP_MAX_TABLES]; struct list_head group_list; struct list_head prereg_list; @@ -82,6 +83,7 @@ static long tce_iommu_mm_set(struct tce_container *container) BUG_ON(!current->mm); container->mm = current->mm; mmgrab(container->mm); + vm_account_init_current(&container->vm_account); return 0; } @@ -291,7 +293,7 @@ static int tce_iommu_enable(struct tce_container *container) return ret; locked = table_group->tce32_size >> PAGE_SHIFT; - ret = account_locked_vm(container->mm, locked, true); + ret = vm_account_pinned(&container->vm_accounnt, locked); if (ret) return ret; @@ -310,7 +312,7 @@ static void tce_iommu_disable(struct tce_container *container) container->enabled = false; BUG_ON(!container->mm); - account_locked_vm(container->mm, container->locked_pages, false); + vm_account_pinned(&container->vm_account, container->locked_pages); } static void *tce_iommu_open(unsigned long arg) @@ -372,8 +374,10 @@ static void tce_iommu_release(void *iommu_data) WARN_ON(tce_iommu_prereg_free(container, tcemem)); tce_iommu_disable(container); - if (container->mm) + if (container->mm) { mmdrop(container->mm); + vm_account_release(&container->vm_account); + } mutex_destroy(&container->lock); kfree(container); @@ -619,7 +623,8 @@ static long tce_iommu_create_table(struct tce_container *container, if (!table_size) return -EINVAL; - ret = account_locked_vm(container->mm, table_size >> PAGE_SHIFT, true); + ret = vm_account_pinned(&container->vm_account, + table_size >> PAGE_SHIFT); if (ret) return ret; @@ -638,7 +643,7 @@ static void tce_iommu_free_table(struct tce_container *container, unsigned long pages = tbl->it_allocated_size >> PAGE_SHIFT; iommu_tce_table_put(tbl); - account_locked_vm(container->mm, pages, false); + vm_unaccount_pinned(&container->vm_account, pages); } static long tce_iommu_create_window(struct tce_container *container, From patchwork Tue Jan 24 05:42:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113490 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 D580CC38142 for ; Tue, 24 Jan 2023 05:45:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 794626B0083; Tue, 24 Jan 2023 00:45:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 744B66B0087; Tue, 24 Jan 2023 00:45:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BF946B0088; Tue, 24 Jan 2023 00:45:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4DB7A6B0083 for ; Tue, 24 Jan 2023 00:45:22 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 20C9C804DD for ; Tue, 24 Jan 2023 05:45:22 +0000 (UTC) X-FDA: 80388604884.26.BFE9815 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2074.outbound.protection.outlook.com [40.107.93.74]) by imf20.hostedemail.com (Postfix) with ESMTP id 5D0051C0006 for ; Tue, 24 Jan 2023 05:45:19 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=YNhotQ56; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf20.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539119; 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=lky1pwA9ndycQb0tInBuvcdP9GKU+97WFqRHP3U1Daw=; b=49AvyCrVNXy77VCFyJnQjDOJPzOXvpFXvUhUS+BcpsfLyMiK6yr55bGXc2cbtD6HpHZIQc MWXJmXMKo/XY9VXEPhWxEnswtFGPFZwKkXQtBngC8HDMApR88KIj4eUxHjSXKCjwF07pMI OT/oZaN+7Wr2QkJ6Xgz/n8DOTKX+nmY= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=YNhotQ56; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf20.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539119; a=rsa-sha256; cv=pass; b=pcj+D4Xfp6EjF2mW/2CIjqiynaZS+JfmHobE5yYck4Yq+CZnP9Vc+sSteN6pMnGZdy3KoV Vv8VEFVh9RL0dRvS5Rw+eJrY/PwexOVHZ0fquSR+Ak9EGK5kfyjacnG944j5vyHtzLgfIS QF94BqsTjuxfc4qL6S+b8uPPKR8aDFc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Y83Wb++48zcYxpTmTfErEeOUIYe5bfikv+PKytGQVB2mdZC9b21ZPUejt1Dj7I2FVbiJiMvVJwljlzv6/Ri+3qRz6cRzy4mBr4MnRKgGJXpTNqZMn3VACEz1rWVee/W4eXgD0Ag/0YJuBfEltZhhZI2AUiJSkIyc4SCYdW4FgzZfZT28Gwn/pmGGpo2oDadvdXJtApjh6D3ftctHYI/BOx7ltE1UNGe1PPP2GNas3wxiCssoED40kJuykYRP/tGUmDThY9haN8HiG7mg7rirTJpDEpmjnx8MBq6bIz2x/v3a8OPHNY11NS9zwtscxyTwwE8mRzl93eLOi0/JGu5jbQ== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lky1pwA9ndycQb0tInBuvcdP9GKU+97WFqRHP3U1Daw=; b=PyZUxZH0LQqra7Cf3OwBQ9w71iDWf4u4WIdUyamNk+k3HB/j9vc6aqFtgBdXWLfg3aXVBDvDQBMZeyVaJHyDwvLDnPT7vpw4Hsw+QbmLwm4FvyZ8J7p3eAwB8XfivJXY5Xvw2sp9MIo6g578R0QBJWzpAqXvKw2aEkqiFvwOM2E7Cb2sfJb7dB3MI0UnMgGWRK+r+XZ1UPmZ/0Zori4Y8v+Rz8EEVsdUNdVVwvqa4WG7Xviq8M788LIzN3kHJR90OTvZi2Mj3Cx2ZvA3dL/9iEKhhvLIdW9uRj3khjEw4ktxTRaJZR9dEORX3IOfOVfy66ai8NQLVk7Nllgs3xJcVQ== 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=lky1pwA9ndycQb0tInBuvcdP9GKU+97WFqRHP3U1Daw=; b=YNhotQ56/PX/D+NqqAgqfCt2w5ovQb1AwXS86LtQCWt9SQ9hJjxOgH5XWHEUcbH7/dPgcUnBeVIbLEF9YEoXvGDpSuu1PCk/CiYR8ecIfAlhoHH8cRyFiIUQm1Lv+VSsoyA2KAcXgtj13O/2M1hpmFroDu4LJF/CKrJLWsmK2c6BRmZ6tyZOCziW6gfXlVX4M/AAXra2IW9Hsi2tqUlp2wTJo7txDnRWAuHpj/G5txbgXSSrFoxaa2TCUmjKHln2HqgabfcxQ6Ef+11/RE0Nzyv3HX4cTCRJVroIYua/xh4z3xLbW05AWEyCshNINEAOqx8Jb/LhO+4TF64JnIKnzA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:45:17 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:45:17 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Jens Axboe , Pavel Begunkov , io-uring@vger.kernel.org Subject: [RFC PATCH 09/19] io_uring: convert to use vm_account Date: Tue, 24 Jan 2023 16:42:38 +1100 Message-Id: <9f63cf4ab74d6e56e434c1c3d7c98352bb282895.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0107.ausprd01.prod.outlook.com (2603:10c6:10:1::23) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: bc652ba4-ec3c-4172-eccc-08dafdce2c11 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6dnj9Mp9wc5jcvJL2XaxCr1DLMU/lqeIFCLVQZDQy0Db82po2vq/u4nSS1BCKH+hTeCJkZHeO5UnYR2bNAskjkIWocL/8b2vXDYR8vTuhdTMWtrm57cbuSFZPJVdEmS9cbpeK8L3vclGY5dQMcseCNe1C3Ai3khh52sNTC7fZqjHyorSGsy4URIdrPvEbbWVu9OLtw5d7nK/cYYYqkgrG4EUvGDzRztYotJ/EWwJWhw2IaO0ubFKy3DcFZ15chWj/ut+m7ynr+Blbr0faPAwj9OcGtYT4nHWiNG9yFJBJYb0U9WyO8M7V501jCvXmg7CGVg2G35qBibIKO5g4tbbQw+uk/ivc3hjemrW9WmB7nLjC450AO2FvGdec0qIZDmXk5LVx0k9RBmwmk/TG65C+WBB0x9dk6Nbjvy3EcYRGMm/I81M/Zk1eGBRZOjbtb4ew7aBe+eyPbBAvG0BsyxVQJYL8SZE0JLJOnb8hnELfcch81eOqUpr5JV7NymIXSzQVR9A/RAiwmi8/DYZIxKVvYx1Vgv43nYL//9uOuNeB9toc3ztlwcFloH/TF+yadiMOFEw1jBYrQJW7yzoy48cJ4POLRwcmnJRKocVPZHDvtVKsVN3hZwWGgp0ilTlGI0zhcDSRc4E7K8kr6LPyw3XSA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: h50bbO1aOmvYgkgRIOy6pyojVPzHjKZnjlI6lpR5npwBm7T5czn1OrPR1v/4sJDAuT7oPcdm0Hw8cxAVwcMb3qGW5tRnIm27JegVMW5I5FwQbzb6oFd1VX9ah1f68Fl4SVquw81kSnrW5GBhujAYv1QtbcIBzdTB0+PajQ1OLgawGaUItOelEIM2tFRrMflpLtlN2Q0u62Hger/1iEbDb36Qx4MsdqSk0P8etuYtYRmrEqk1FBv+Z+Jqq2shDtFy3zzwdp489VYOPshzDwwHVuobKxbB9D4H3OxE7HwOW4Q1r3BtCWSsWu2t6QmOKbYClbyhbdSyX13la7sLNWE1ka4PlT2jUEIN5YWNNRrnIfXOKVVIsK4t2u0R6P8qCrU1u2QfkETgS5u8Zgtu3XqWhlqZlSRxtWw/+LX83A79sLtegA/jutY4dNpP7sloLEdqmF2ILhElJ48u9FaxRwscLNv593jtXZzAiuE5ettP0DsQt0CtRbDZEfY2oGnHbzUt9ZlWwa1dVJyeROaxErGb3ZlcQsXzDJHx+Q3j94lCH6018TOk38cFuf7YLZt+h+ma0snhtBDlQrctYfpz832jFX5NaXhOXN7BjTHBTRVDbd+cxgi0SoK+eUN1AXgClzLRi1nlDfi9xO7r5kRJz8ION1YS54QEM+LylZgifewNj41rHD4lC3j4Cw+dK7SqcVJuw3APtD78U/UZkq4nrkcUZoKZcikn5Yrs9cCaFqzIWoAlwEKawT3t20Eqdwz8YHYgClYvVcrdXlkjXSNkJ1xKL1lwDygEEwaJe4Vt3WpIEyxa6c66ODFwwSLgs2rjKfxQyinq0DvGellIw2CV3rp/I4vu5x2RdetO0CXFXeUAe9JLgx7ICfYfjeHz908lQR1pp6Do5yL/rUUWQBsdSdWlqajSoCBk8yp3iP3JQOIKnUOUKiVgSjEajILp4EOVIQzYkjt4kg8UprEH4Lw+b2HRpbf6dQvhwVLlyUshxd/eCokyUQ/sRLqER+aOJMiCo06E+U1bv5wFLNOfo7HgImHyfmyQIa1VeuJKzXzsnxUCacoTSV1DQmS+9UWq+Ha5T6/03AoH6uDByJE4KCYPYDX8DJJd+rcI2J6umm5pUGk1DUY4VIhyupt2uGZv+xJS5bCVSN7L6F62apFKe8Rno93VO4dM5XNqGTH6bBzxa5zUN0AuovL22e2XRIavMIlaK2bEq3ko/nIFMtywY3nlk1pc0NcMj5S8dpum2lSv/Cq2Ibooo7HHsURaqTNS77/HqOUiwfpZLszyV3F15e6LKbBNUtaHzF8Vvzvi73F7dgoLh33Rt5gfrAbLrT8DZcu0qyZP5nCxgo1hSacmGHKYtZ0gxw7LBBynitocPJ2625SV1bmCIGBIpIzAjafrqBMaoep8UGSB2Psw3RpSd5xUhvWQROfbJQaAoshw2WZaser6Vj/ebAbkg9sIzs+UelKI3GLZ03pMj4ouRVPX/ZFg+WPmKks3sOLKhacxqn7xTBTGQrF5UFKjPIlmiIka57ZzmDu4mc9bQw+UEC9709vJp/X3zSp1f/LkQFhxvl3JXNRdkzKymfGN8kJbGXQnBdZacFNY X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: bc652ba4-ec3c-4172-eccc-08dafdce2c11 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:45:17.3933 (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: 4VHMQ9Li2qe2ASvzokp/FnjeS86gyvDwJPTIZ9bOyFtj1tVZ7fH8LGUAibhu9xXr4tEoK5tDW0XrytD6CU2S+g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 5D0051C0006 X-Stat-Signature: bsrhd84k1y3tfjg4pb8guppix96c1a1h X-HE-Tag: 1674539119-96506 X-HE-Meta: U2FsdGVkX19aygB/IKLtCC01x0OVlNd+oSO9+dnZdPtCm0Fu6/zkfsOAOjQ0imZOjL2e7k+1SqmXAyoVGW84L3k7DF4ZuCEHgsr1EeknNZcg6f241dsHUgOd9pxO/gkzP0WjUHTTekOZ/ORNwXweV9RwDxzTdmHr2nvmnHWjic0pxaWbdaXTDy0ba2vFaKvUoPXsmvvjiddYVAfUdxqRqCR/pB5wsIqdtPd+UFihkMu1WbtvHt+q429FI8daAGUyJXzTk5ceRAWB/CwZy5/sD5e+rmrYk5kxEGCaCdb6yuS7hoea2jbVNlsswj9TCjUtp4r6RJGFfQePNOxCsKY5b+cSdafYFNzlazmwJAcBrZKypT6gqZaHPMeiz1cx3+Gi5vtBNm1r0PyhYIPJxDrpuVt/5MXINhBzU0/1ntyoZUzL2CZzO++kUtpW5B955+NfTj/tNcAJEbond3tIwNf4vgqBxFKcaHGGA0PSFAOwcjPMk1bd0HftPg9kkeKgRkqDHFDWsnj+8HxQ6g1E1DXz5Em2PH0SMNwUZrhxvWkWIwrx1lZsBQm659zEjKe92yt7p+J+s1NqEBMs3kfUPStaTScWT2OEx3Z43c8h7Ot3F11zn9ngeVoDzPs54vytf8IB7uf/MtKrJ66lLLo7UVPbsMN9WIG2pidS6NP1EpSivRsrSmvCVGDmviMPEwyWTJWRtT1Ufx5U+1Lw2YGgfeOOcxTaT6/kQIAxcNywBlypd+r1dLtLDo6CttoOzh5jwlYTiMfC9JpDVI0oQQh4GAVNiyDilnMmqcu9F1kuDWju4MBWPuarJyq54UXliFXLsSiPeGylLDBcFZZTuKiA3VNvwyoPrkgc91XmbHVVeGXaauFqXYKA7KHk5JSRJMd5PMi/q1vXTswwoNK48PHepxSYVIFI/m/PlCTlPors3hMenrGKKy9eD8GhkTfa2v3duuW6O1Drzat9e3beETFQglS 3Nq4S9qv 3P18/fbGimqPjuzPaufaz2P1opHsWs/MMpcRzG5W1DRG5WKDajj37NWfQuSqG0qS+6lDn6kEUQPfuvsY/TLMb9YDPoNE8ZnxJ0GrzyZ4jOO2t5HXmgt6i9NF5yV70NagqRF+yO57UcgNfXqiBhJspKWLmRUMXE4zDRPkGxdUNUqM4KmnXulnngVcRvg7++31qdy2ZYKsz2GpUiBlxbViyMSAv3oyR/0dIUcA+TU3m2HPNv7FT7STR5f+hj32gyURa4UUYpK1wJmIKBdYvXeO4oE5L8CI15dBws+lG4esFiGkUPfZFg2hU4KNgeA/X/XrwpvPPY3tiv7EbaTq3IqcINJ5ZTbnaqs0M7i55ffCB/V4osrBvw2DcoNolbo5VBzHO2Z3jsSwoCzTtE2rJbdjcUWpTL3BnnxqqanMOzjGugUTCMK4Q8B/ehDcVCGq0DaJfPbdM 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: Convert io_uring to use vm_account instead of directly charging pages against the user/mm. Rather than charge pages to both user->locked_vm and mm->pinned_vm this will only charge pages to user->locked_vm. Signed-off-by: Alistair Popple Cc: Jens Axboe Cc: Pavel Begunkov Cc: io-uring@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- include/linux/io_uring_types.h | 3 +-- io_uring/io_uring.c | 20 +++--------------- io_uring/notif.c | 4 ++-- io_uring/notif.h | 10 +++------ io_uring/rsrc.c | 38 +++-------------------------------- io_uring/rsrc.h | 9 +-------- 6 files changed, 16 insertions(+), 68 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 128a67a..d81aceb 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -343,8 +343,7 @@ struct io_ring_ctx { struct io_wq_hash *hash_map; /* Only used for accounting purposes */ - struct user_struct *user; - struct mm_struct *mm_account; + struct vm_account vm_account; /* ctx exit and cancelation */ struct llist_head fallback_llist; diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 0a4efad..912da4f 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2744,15 +2744,11 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) #endif WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); - if (ctx->mm_account) { - mmdrop(ctx->mm_account); - ctx->mm_account = NULL; - } + vm_account_release(&ctx->vm_account); io_mem_free(ctx->rings); io_mem_free(ctx->sq_sqes); percpu_ref_exit(&ctx->refs); - free_uid(ctx->user); io_req_caches_free(ctx); if (ctx->hash_map) io_wq_put_hash(ctx->hash_map); @@ -3585,8 +3581,9 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, ctx->syscall_iopoll = 1; ctx->compat = in_compat_syscall(); - if (!capable(CAP_IPC_LOCK)) - ctx->user = get_uid(current_user()); + vm_account_init(&ctx->vm_account, current, current_user(), + VM_ACCOUNT_USER | + (capable(CAP_IPC_LOCK) ? VM_ACCOUNT_BYPASS : 0)); /* * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if @@ -3619,15 +3616,6 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, goto err; } - /* - * This is just grabbed for accounting purposes. When a process exits, - * the mm is exited and dropped before the files, hence we need to hang - * on to this mm purely for the purposes of being able to unaccount - * memory (locked/pinned vm). It's not used for anything else. - */ - mmgrab(current->mm); - ctx->mm_account = current->mm; - ret = io_allocate_scq_urings(ctx, p); if (ret) goto err; diff --git a/io_uring/notif.c b/io_uring/notif.c index c4bb793..0f589fa 100644 --- a/io_uring/notif.c +++ b/io_uring/notif.c @@ -17,8 +17,8 @@ static void io_notif_complete_tw_ext(struct io_kiocb *notif, bool *locked) if (nd->zc_report && (nd->zc_copied || !nd->zc_used)) notif->cqe.res |= IORING_NOTIF_USAGE_ZC_COPIED; - if (nd->account_pages && ctx->user) { - __io_unaccount_mem(ctx->user, nd->account_pages); + if (nd->account_pages) { + vm_unaccount_pinned(&ctx->vm_account, nd->account_pages); nd->account_pages = 0; } io_req_task_complete(notif, locked); diff --git a/io_uring/notif.h b/io_uring/notif.h index c88c800..e2cb44a 100644 --- a/io_uring/notif.h +++ b/io_uring/notif.h @@ -43,11 +43,9 @@ static inline int io_notif_account_mem(struct io_kiocb *notif, unsigned len) unsigned nr_pages = (len >> PAGE_SHIFT) + 2; int ret; - if (ctx->user) { - ret = __io_account_mem(ctx->user, nr_pages); - if (ret) - return ret; - nd->account_pages += nr_pages; - } + ret = __io_account_mem(&ctx->vm_account, nr_pages); + if (ret) + return ret; + nd->account_pages += nr_pages; return 0; } diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 18de10c..aa44528 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -42,49 +42,19 @@ void io_rsrc_refs_drop(struct io_ring_ctx *ctx) } } -int __io_account_mem(struct user_struct *user, unsigned long nr_pages) +int __io_account_mem(struct vm_account *vm_account, unsigned long nr_pages) { - unsigned long page_limit, cur_pages, new_pages; - - if (!nr_pages) - return 0; - - /* Don't allow more pages than we can safely lock */ - page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - cur_pages = atomic_long_read(&user->locked_vm); - do { - new_pages = cur_pages + nr_pages; - if (new_pages > page_limit) - return -ENOMEM; - } while (!atomic_long_try_cmpxchg(&user->locked_vm, - &cur_pages, new_pages)); - return 0; + return vm_account_pinned(vm_account, nr_pages); } static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) { - if (ctx->user) - __io_unaccount_mem(ctx->user, nr_pages); - - if (ctx->mm_account) - atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); + vm_unaccount_pinned(&ctx->vm_account, nr_pages); } static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) { - int ret; - - if (ctx->user) { - ret = __io_account_mem(ctx->user, nr_pages); - if (ret) - return ret; - } - - if (ctx->mm_account) - atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); - - return 0; + return vm_account_pinned(&ctx->vm_account, nr_pages); } static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 2b87436..d8833d0 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -167,12 +167,5 @@ static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) int io_files_update(struct io_kiocb *req, unsigned int issue_flags); int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); -int __io_account_mem(struct user_struct *user, unsigned long nr_pages); - -static inline void __io_unaccount_mem(struct user_struct *user, - unsigned long nr_pages) -{ - atomic_long_sub(nr_pages, &user->locked_vm); -} - +int __io_account_mem(struct vm_account *vm_account, unsigned long nr_pages); #endif From patchwork Tue Jan 24 05:42:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113491 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 F06C7C54E94 for ; Tue, 24 Jan 2023 05:45:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9696D6B0087; Tue, 24 Jan 2023 00:45:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 919226B0088; Tue, 24 Jan 2023 00:45:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BA496B0089; Tue, 24 Jan 2023 00:45:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6C0D06B0087 for ; Tue, 24 Jan 2023 00:45:32 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3A3F41C5F08 for ; Tue, 24 Jan 2023 05:45:32 +0000 (UTC) X-FDA: 80388605304.07.579A36D Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2059.outbound.protection.outlook.com [40.107.93.59]) by imf18.hostedemail.com (Postfix) with ESMTP id 68E1C1C0008 for ; Tue, 24 Jan 2023 05:45:29 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=EyOJEsMd; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf18.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.59 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539129; 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=nXKmUiAwwip0H/gynwy5RsgsJUC5caFZfjG6m6383VY=; b=O+nbAkX2ooaRv+gJHCqxG750twJtQb7woIysi3GlUFzYsq3hcA2Ts9jR45ct6HZM1mrP32 miyfE9W9A2EReqYuG0IScLeaAhutGKrkvUfpti2eo8yjo5Y+TlbV2fqaK7mQ9nu0t7c60U z1trFpTWqhNTB/6xq7hLK/ywcU+6yOI= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=EyOJEsMd; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf18.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.59 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539129; a=rsa-sha256; cv=pass; b=QD9XohFbvGVOxreclgEpoxTlmnXHrsQqBZIsxbWXWsYrCVar2irCD4dsvj+XE8UlUr6v6Z UKMZl7XIcdUQGCgbmOOhmTn2+owARL2AY7gw/X+IsVMQCJbEBPwBzwydTpZevYNizbVejj hTNWtkGXd/W+or1e5idVtvtdUstN01I= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SF076JzFXJT5tBCcFPiNclt6XkNHHaTjgJv2GOGCKrQb9KvUdRwi842dnBINijk3BqqjJZtXlyZjsxwkfhkqC/82FP5TE0tdS5sFmJl1EmUTxSHTJJBeUptZ9w+MciD3sDsg45oEP6QPYrhjrVD0ATo4QQ0g/BKU/2JqcvCn9Dgw6L5rm0cfDgzrxiDivD4KrWDvi4+kce9lU8w7TVg/I40abIpceVVFb9mziRC28P9Am7etrYwvc3Sg0Cr7uyYS/9zOeQyIpEUMUbRtQdZdEVhEjxntUcminDwgYsi9oBoxvXDzQyh+HVEOIa1wfU0IMTyeJgXN01xUFmEEUSJWqw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nXKmUiAwwip0H/gynwy5RsgsJUC5caFZfjG6m6383VY=; b=K4vUOpPclHN6c2a3PB7LmoTw+TpMR6xlL14QwH0wiP/fs5WXcHX6ZhS5etqkazcBoeAn8cL0fNIm8c7roKQv5A2dKS53t0TeBFWelk14KohoSjGpurLj+zJ2u/RFQvBV66bmTVBQ1GuVxRX/DMIiW7efPPc7iwbijaiRmSsHMskMKyrqL3vmlm54zANwOoGYgFkrO4hV8FfK7C0b3Ks6vdOgMG6yP4qxxLVJBTdb3SmgbzT/CfsSW35M4+6sIPTHLsnzl8Zt8RmDEQOrVzUnCaGrt/Z0l1EwMakjODkb+6qJCEeLPxLh8z0EhRKFe5X1KL8g7UnR0PC2Rs91+LJ89w== 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=nXKmUiAwwip0H/gynwy5RsgsJUC5caFZfjG6m6383VY=; b=EyOJEsMdC9Pm2dVyZu8t7V04q7ar/Fng5FR0rXTfQdglnp59DaTP9cs40qJld9Gm1OjXSNecHDL3L2mWGVnKtEz0Y6zx+nMEhgvJFDI5j2J0R5piA2LNtMyLmR7dMAE6/qFxCq1JPagJCvMLOtc+t3iBnnJmKH3jZXZ/Sr5YQ8vuY28QyoHFsJK8y6xxzrl49r1MQMX2wPjeqpbKp7Y2rwX4lCd2/mi2e9KlnlUgvhErN8QPJEaPo2En3y9Hj/vTHUmH1bYEeCKOOZ5lovaVKTB4uHvGrwdKvA96iqPfaJHPSs1KhCRM9T55ZTIpA4k2u26EOn0aM2FRVcR0rUq73g== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:45:27 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:45:27 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , netdev@vger.kernel.org, linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com Subject: [RFC PATCH 10/19] net: skb: Switch to using vm_account Date: Tue, 24 Jan 2023 16:42:39 +1100 Message-Id: <9b54eef0b41b678cc5f318bd5ae0917bba5b8e21.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY5PR01CA0077.ausprd01.prod.outlook.com (2603:10c6:10:1f5::7) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: ee42ee53-a49c-420e-9228-08dafdce3232 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X4FOg3pTqEzrbtNhZV/TzqlzK6iYksRf7X01zRRZJgtB61ceWw5M02H6c2/8AX4X6jpt0Oy4Nkrfv8Um7NxQb5kbBGOymB0H/VIdTWNmS1B1AA3IcGWIBtpzWj54z9cXpY3AgUwarAcf4T6MKlzB6Q18tLs2WDvuYkZFZZONvxIr9UBxVbjx6I77E7SkxdnppQHiP/FTp4CPFvaotoGWKgChqfJ0Dn/WLZSgn5MDDi5BHkardrWyPnobV+mVrr4wrduVNTb1FsvYtCJ8Cw/6zkRW9ofeLBEIDNdbbVjjc42xof3RJVZ9uhKhYcTLevhp4SyXUHJnl8L3ZMw2r3et2SJ0ULyureKIzUJcrj9jojPx16mlxWxsQ9TYeTxJhVbhJ6X+g5lTmg7VoE2ztYpYP02wWatmpBbTK5jBurtPebz6QLs1EcqC5CIkgM7CkA7bPKZWDaEW13RfvgWYeBz/kps5E2Vo/qcz845BwJlT1vsxp3zPXx9O+WNVi9q9/tQaiqSy2QTFQjhBjg7G9XjdRz3nLe1JCO2Q/gP2cQVJ7Z4cukytvsCbRmbjdgITqF4J1knrcs+7EX5YIDgMo12Lyc45QmU4RBPvcpJzNR2kJK/jbzLiKwEEQjzwfjcCGE0WRrhAz3NNjCsRmZOhyuVKhg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: K8g3q04AKDRU9iOQM5sWeWNFrDSqaTYGIY8JLNyY5Ch7MRB3P6TSMF5aHbPV/vSwlDtOwyTARzEtSTcZDwUR9obeuANIwT7O0j9TlDKLNN9InYK3HRZOSefK7LMNZve4qAO5wcJ5U+lJWrzM85R1idW4nVOXAHX4LQqlivlJ/02I12HUO76+iVzWDqRtCnbAWWJerEXnhW43w6xst40krfxmG6sWmheGd7aWQDMWMdhUalzoydlgCZOQBcRV36w8So79YPQZdz6dakoeXAmots5Wh0JqU6jzOaffVr3aZLIs44j4u6J0+lwgM7rw2mn3IUdjWFgbaRMO+PO2WsK2digPnlz02DxAVBKOSaOARKkc1//jRS2Et+pTRhdZfOeDQaE8lAobOauTI8I18jvr3DOnSw1aaumXxWEoBjtUD0mjiAFtqveLnrdBvdaWZIfAwJKrrl8lFa5h9OD/5H30pkI3Of4aAnHrz+2HLMqFEmmNiz5Vn9raPJ6vc+rgq/+rx7FAiWa/1UFBIT0EEoApLyg2Ex0uQA0SpW4voskDpzGtFQ8vHKV/uVKxdoqempU/Vi2QvOY0oiOW1FiomFUL4nMdIgHe3msfE5zF+DXJNZgNjhUOMIZeVhi4/TIiQK+Ospv4tKTjTs9F/I0+N7GLEuekHBDHi+iOg6iCCNiGq1taZSl+ZXqDE3T2mmElH4U2WY+eCOAy/gKQ1pTVFMcbQeL+NmhdAS00eaVq3NZZL6NXHS3Hh2kLz4wlsf0uBGs4iuMXCvMkVH8MTycwnZNokHNArTZauYowPZpg4G0LMY0eHWXc+p6QtZ5EOdqEBefBpAShRNuUNJJl2tRKb16jeVGYP4M1EMfkzBFxm+HqI50tPJwuU+fJwX5n+9b9d4B0EPQsucTReSpqkSIo7xm7asiTwGHSNFy4MHasfcvO3jyGfe4QpS9XIOiISNObfOl7VUvC+qp7R1HX8/gpw1SEI/vhosSKID8FJPtwtQLmtv/3CUOzXnRY8EC85cLo0WRV+MLFzYjPV9ch0DEcJkVE2F1gsXOnH1FM+yufAb6555Vpc9evgkkjntjNOhEPuGPXKVwTuPojqiqDZipUJE4yKmG5MvDaCLZOjryUGd2cqRFpifTz1+1vS9a9TrQV9jpx7kbaTiHh0gMxaBrtGPHJT4L9KUh4grOlhwpiWeKZEx50y6s2Hz1yRdpcKO+Ih3BN3wezb06I/mUmW8kK00N5FpdjUHG5V/FRjA/BzI3ZXHE5tEQntDGaRead5oYmHdpNAGPP3b/MeeHfFC+kfHyFB/WiMcDf3TyadxR8/zK0uZCyNmdZVS8HCWqqSoDp0ikce/ZkNj9J/NFrQ38NY59c/vFFlxR4y28CShErvgGDEc33uatXhOHsPciTw86RfBs5PYJj1Lx8ddSY7kjlszjuVGgeOJTF4Qm7yF3Ufr1ntCW9G1P9NK7xrMVvauoJOjDaeG+9QPKcftj63XLkjJn0+zoOVdWrmn/1vGSsGT9dEsjqmBLpjwOfexQ2nvIPh/AlbAGtD205gmhM95MMMTjbIDqy4KksJOnCIolLjCTyEzpcC7sG2jzLzeqvPDZIJ+hs X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ee42ee53-a49c-420e-9228-08dafdce3232 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:45:27.5330 (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: OtuzUzDD66upC3A63HQzCEWHuh0uxSOe/AI/iKdZI4NmKeNoSjN4u6UaYGvK84y+yktDJCOxf3qaU2IhcgmvOQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 68E1C1C0008 X-Stat-Signature: hradhfeih5ot5fqfwrqujdsd5qhoyxry X-HE-Tag: 1674539129-94742 X-HE-Meta: U2FsdGVkX1+3+vHswfCpA//qKwrO0pJ5PSebTFIU8FTfFJrrCdIJNooqwF6kapP/YbPOoK354AIKiTI6I5l4QtAOFhjdYkK1RxnXQ62dUi8gUDsDHyj4CoE1pOgiyMk0mdiYBgHwykOdvTyc18ZYyINdaeQWqYWVZoxtmMHTqqwWbkoFHP/XDmYUFqTNr0uznK5ZrHd1VDvJEGkWenBoDHBzQ/R78IpIbZ5zybNPyRua6thfI1z8TVhSnf6pSm9KZ/Kix51OeRj8RzUuGzLyOl3ISFgV0Jm5LYRJTOF8rjjWBpf4wvVorCtq04b6SuPjwLGiSNz9uQwET2Da+rnnZ9P8UiNdTmALrycjCAStybMz2laY6J0lttxGz+Gp3aozRcS+w1dktn6mM26aWiUZ0psJ2+mieJs1whDlNJWs7pEpysE/B4Qh6asogSwKfha3fH5AmGiJpPLctO2Fcwy60AhcONvWFTiDnMcGPzEFCiljwPaGu5ByO0YTckj3IZfBF11Pigbbw+Z4xHOJDB9iHd9DWBr9qNfSpwJX5uocs0qyvqZWcJ/jZpBDNyjrK3oWFPlxq5uhx1RkTgN1NSJJutitZHbBIThEztQJeImtDDNnq5gp7sFAJn2gI9tQM5q6J7vV2xPUAI0UIdthrkT0DYaPO9PhBHWq94JIFsYlJCpa5GUf9lVPj6Zz4vXdj+I3v+iL7eXqtg4wj9doEjLML/+Y7aRCtPMUsv3IUPTBXLDldtYJs2Tf0slYKf17OpnjoOhi719bjCqrnTXeTlKvp/rRuZv5O8k2ad5+4wx19dI4SGshyKIT0N8Ybd+8PK1Xl4hT2ag/ZhIh8QxrIFij+FlQKhknPzBtUnoyoUORE1IcZmcOnBj8840iAxYzsqUKF882ZWOe5ncAA/GPnro2/7XhERR1F06ej1hliTNNUIBOqSLIkgltgNjY3W5Oretw7feYfOVAtUWREC9BgR8 oH2AuOKK 2DGOOu7ObHvtTDsKmN7bzu5yFGHO3OX9RopoFs2MIdVv48ouCSXidlEs5RdIq8KAF6IJ99FCFucGmZqAt9dWysiaayE8Bs2sd4xJGybI1mbZ9gsP8vh05mZMZCfOBhjSCYUKyExPAYBh1LN2nvU6T8FgjMqrElpH1yqIsW/zCzKclRBr1MIduWtJnKJ+XELerU1FDSMbD62gnnB6ndINrp/SYZ9FSChpSh9sqlYNcfWXEqVzUmGuZnSDuHTySM6I4C39MLKJ6AqLtDzP//OM8HVHbd4aIdcQnEbNWZxAU/ER2sSp0ECOQ6aIdaVSLqG0xHZk8BDyO2PEio7pQM2Ip/QJJdEqT72ZYBY7fGtJdKXfumndGz6hz+FvVow== 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: Switch to using vm_account to charge pinned pages. This will allow a future change to charge the pinned pages to a cgroup to limit the overall number of pinned pages in the system. Signed-off-by: Alistair Popple Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: linux-rdma@vger.kernel.org Cc: rds-devel@oss.oracle.com --- include/linux/skbuff.h | 6 ++--- include/net/sock.h | 2 ++- net/core/skbuff.c | 47 +++++++++++++++---------------------------- net/rds/message.c | 9 +++++--- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 4c84924..c956405 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -554,7 +554,6 @@ struct ubuf_info_msgzc { }; struct mmpin { - struct user_struct *user; unsigned int num_pg; } mmp; }; @@ -563,8 +562,9 @@ struct ubuf_info_msgzc { #define uarg_to_msgzc(ubuf_ptr) container_of((ubuf_ptr), struct ubuf_info_msgzc, \ ubuf) -int mm_account_pinned_pages(struct mmpin *mmp, size_t size); -void mm_unaccount_pinned_pages(struct mmpin *mmp); +int mm_account_pinned_pages(struct vm_account *vm_account, struct mmpin *mmp, + size_t size); +void mm_unaccount_pinned_pages(struct vm_account *vm_account, struct mmpin *mmp); /* This data is invariant across clones and lives at * the end of the header data, ie. at skb->end. diff --git a/include/net/sock.h b/include/net/sock.h index dcd72e6..bc3a868 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -334,6 +334,7 @@ struct sk_filter; * @sk_security: used by security modules * @sk_mark: generic packet mark * @sk_cgrp_data: cgroup data for this cgroup + * @sk_vm_account: data for pinned memory accounting * @sk_memcg: this socket's memory cgroup association * @sk_write_pending: a write to stream socket waits to start * @sk_state_change: callback to indicate change in the state of the sock @@ -523,6 +524,7 @@ struct sock { void *sk_security; #endif struct sock_cgroup_data sk_cgrp_data; + struct vm_account sk_vm_account; struct mem_cgroup *sk_memcg; void (*sk_state_change)(struct sock *sk); void (*sk_data_ready)(struct sock *sk); diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 4a0eb55..bed3fc9 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -1309,42 +1309,25 @@ struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) } EXPORT_SYMBOL_GPL(skb_morph); -int mm_account_pinned_pages(struct mmpin *mmp, size_t size) +int mm_account_pinned_pages(struct vm_account *vm_account, struct mmpin *mmp, + size_t size) { - unsigned long max_pg, num_pg, new_pg, old_pg; - struct user_struct *user; - - if (capable(CAP_IPC_LOCK) || !size) - return 0; + unsigned int num_pg; num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ - max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - user = mmp->user ? : current_user(); + if (vm_account_pinned(vm_account, num_pg)) + return -ENOBUFS; - old_pg = atomic_long_read(&user->locked_vm); - do { - new_pg = old_pg + num_pg; - if (new_pg > max_pg) - return -ENOBUFS; - } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); - - if (!mmp->user) { - mmp->user = get_uid(user); - mmp->num_pg = num_pg; - } else { - mmp->num_pg += num_pg; - } + mmp->num_pg += num_pg; return 0; } EXPORT_SYMBOL_GPL(mm_account_pinned_pages); -void mm_unaccount_pinned_pages(struct mmpin *mmp) +void mm_unaccount_pinned_pages(struct vm_account *vm_account, struct mmpin *mmp) { - if (mmp->user) { - atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); - free_uid(mmp->user); - } + vm_unaccount_pinned(vm_account, mmp->num_pg); + vm_account_release(vm_account); } EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); @@ -1361,9 +1344,12 @@ static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); uarg = (void *)skb->cb; - uarg->mmp.user = NULL; + uarg->mmp.num_pg = 0; + vm_account_init(&sk->sk_vm_account, current, + current_user(), VM_ACCOUNT_USER); - if (mm_account_pinned_pages(&uarg->mmp, size)) { + if (mm_account_pinned_pages(&sk->sk_vm_account, &uarg->mmp, size)) { + vm_account_release(&sk->sk_vm_account); kfree_skb(skb); return NULL; } @@ -1416,7 +1402,8 @@ struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, next = (u32)atomic_read(&sk->sk_zckey); if ((u32)(uarg_zc->id + uarg_zc->len) == next) { - if (mm_account_pinned_pages(&uarg_zc->mmp, size)) + if (mm_account_pinned_pages(&sk->sk_vm_account, + &uarg_zc->mmp, size)) return NULL; uarg_zc->len++; uarg_zc->bytelen = bytelen; @@ -1466,7 +1453,7 @@ static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) u32 lo, hi; u16 len; - mm_unaccount_pinned_pages(&uarg->mmp); + mm_unaccount_pinned_pages(&sk->sk_vm_account, &uarg->mmp); /* if !len, there was only 1 call, and it was aborted * so do not queue a completion notification diff --git a/net/rds/message.c b/net/rds/message.c index b47e4f0..2138a70 100644 --- a/net/rds/message.c +++ b/net/rds/message.c @@ -99,7 +99,7 @@ static void rds_rm_zerocopy_callback(struct rds_sock *rs, struct list_head *head; unsigned long flags; - mm_unaccount_pinned_pages(&znotif->z_mmp); + mm_unaccount_pinned_pages(&rs->rs_sk.sk_vm_account, &znotif->z_mmp); q = &rs->rs_zcookie_queue; spin_lock_irqsave(&q->lock, flags); head = &q->zcookie_head; @@ -367,6 +367,7 @@ static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter * int ret = 0; int length = iov_iter_count(from); struct rds_msg_zcopy_info *info; + struct vm_account *vm_account = &rm->m_rs->rs_sk.sk_vm_account; rm->m_inc.i_hdr.h_len = cpu_to_be32(iov_iter_count(from)); @@ -380,7 +381,9 @@ static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter * return -ENOMEM; INIT_LIST_HEAD(&info->rs_zcookie_next); rm->data.op_mmp_znotifier = &info->znotif; - if (mm_account_pinned_pages(&rm->data.op_mmp_znotifier->z_mmp, + vm_account_init(vm_account, current, current_user(), VM_ACCOUNT_USER); + if (mm_account_pinned_pages(vm_account, + &rm->data.op_mmp_znotifier->z_mmp, length)) { ret = -ENOMEM; goto err; @@ -399,7 +402,7 @@ static int rds_message_zcopy_from_user(struct rds_message *rm, struct iov_iter * for (i = 0; i < rm->data.op_nents; i++) put_page(sg_page(&rm->data.op_sg[i])); mmp = &rm->data.op_mmp_znotifier->z_mmp; - mm_unaccount_pinned_pages(mmp); + mm_unaccount_pinned_pages(vm_account, mmp); ret = -EFAULT; goto err; } From patchwork Tue Jan 24 05:42:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113492 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 84707C25B4E for ; Tue, 24 Jan 2023 05:45:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1F02E6B0089; Tue, 24 Jan 2023 00:45:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19F8D6B008A; Tue, 24 Jan 2023 00:45:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0403C6B008C; Tue, 24 Jan 2023 00:45:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E85786B0089 for ; Tue, 24 Jan 2023 00:45:42 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id BE0FC16010C for ; Tue, 24 Jan 2023 05:45:42 +0000 (UTC) X-FDA: 80388605724.24.B49E1C6 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2077.outbound.protection.outlook.com [40.107.100.77]) by imf15.hostedemail.com (Postfix) with ESMTP id 03272A000B for ; Tue, 24 Jan 2023 05:45:39 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=RjSfZx+d; spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.77 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539140; a=rsa-sha256; cv=pass; b=AeClkoDtDOs4zd3VtcQJ7EaFSOgkZ/mHFUWQJuWsoqkh+gSAqUGsCajANSFFln0Cv4ZrbX Xf+/ffZlWoo49BPWJ3jdwviVWC5pe8NOuscCAqVe0orFsTzqERT4hqH8X8h11mKKJfRHnJ 8El3MK5oGkbJ5u2wCZLfR766llgarQs= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=RjSfZx+d; spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.77 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539140; 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=0W0p5nkXQyU1aX2kot2Ea5g7JV+lHiVX0c1iGBFb/bs=; b=b5JEy1uE8OsR/vtuU4oNnn8kS5NOi3dBqolLPrkInZQjlBgIFgj7xeSUv84DqGRZToArX7 h2XpJ143c2Qh5fi/ElUxzvS2EbJ+9PeJC2QDxZQsh16UgGipDV9AzhvCm/wPfIZRxadc9H bvx9R0GQLLTEU6+hzvtZLi8CLojwPq8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AjVytcuDJkM/FLk3R8BjbHpvTcr7TGho/0IVTECkIQV7ws4n5jkzi0QArse93uAxRASpSMj+muQ1YpjhpTNkuOzVi/Zm7zPDCWATItTCDw1wFEGbPURqJgHDnSd0Rc+FN0poclOjqdOit7wBP/M25ddWmto6a/KmJUHwhc/pmHepWiWjvrpFsvxWiO8mFY3hNsTbjJtgvSQHOgPBHVZFO704w820TyDx6jgcFraq/qOYau41uNm2PyYaPh/YVOyJCRwz7dHjy2+/BTy3ACmLKQUNYqgwNs2CDUVHl51sR7IgrKF1hKSkl5rnBZi7hfPQbeN9jMukENhxS7FPZLCsVA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0W0p5nkXQyU1aX2kot2Ea5g7JV+lHiVX0c1iGBFb/bs=; b=OEgH1/4ExlDB1ARjKQQZlzDPJ5XLoJhcwkWk7U7lWF9NKXSElVLozOyvFx8X28kCePcut4zphfJOvCmNq6trkwixvINvM3ipIHQZRbWRCAA5kzWUXX2/lLfR2L5IwUFlWDwrvqijm37eBAOr94FSiixahzuGKrBdkLjLgseersArWWEYHgyTafL7hNMLMFNJ+zDs2TalDPEoKlMactVcDbFZ4Lw13Ao3PjBT5cYEOZQHNJZyHvI6Z1pPF6DL8pGde62y8nvUbNrcYV0N3ZYQbKJx7Z8Pcvgj90ANN3a3WKiW6B8hK8T+48o2vjDhfPXjNzXHYfG0NA5nJiTgr3EYbw== 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=0W0p5nkXQyU1aX2kot2Ea5g7JV+lHiVX0c1iGBFb/bs=; b=RjSfZx+dfCzyTOWStKMBo05C3rq/ufLUyctmeYXAYJItlL2INf+LVq9u6BKViV5Q3L6REIxgjzJczv8zHROXTFliHUucL1VYy9lXD8V7A3cZwr3xjyhETc+bpnfPZt9dqD+NtE3sYCijNGZ3XwQlckkcjObSr0E3MXvYVBSnfw+iATTAlNpIX427NYo764b9230nPCtuXSN/xx+4vP2i2RDvRZnQzkh6Tq3o/IWibw7jrKzAca07SqadlRMfegDPNx4ijtPbh9EzUdwUUMsLgFsJGdFfqloCfkSs5t/nR/hEkTXlgR0vxjGQUTeil6EJJb2ayQKhAX6x+uPAo6supw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:45:38 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:45:38 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [RFC PATCH 11/19] xdp: convert to use vm_account Date: Tue, 24 Jan 2023 16:42:40 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0058.ausprd01.prod.outlook.com (2603:10c6:10:2::22) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: cf49a282-9822-42ab-b0c7-08dafdce382a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: swlTuo9A26qSmZr/qtf5eeHLVNj1itmA/M8Xyd8AkfRqZUC0/zMPXU6/b50hqyh+VxqX0Z+l9ce2YK04WGI6LfXNmwW7KHPD8RMvsCksOp4emgSGy8VbTAMzvG/Rchbbn2aRUlsBcDxX5Ag2T6XPdihZje97ykN8m5j79DVnDlq8ZdStbNt82GR6C0v8oXN2VwkSpPKEZeH/WP5Vo6b2B0h5ggzaOjutL+hMaCOwuLja9i3GaYayqcnN1gaIsWitahQt60lTFS5qoO6kxqKFnaeZiAxlhHHxH+Yc7NZDnFWpRDZBS4b/yEVRqEbKJ9Hyvi88iOI6fDMwssiX5ybXU+Ojl4HVkiIhIovvpX7HElo3reRfPF6zt9ioQIOL8dm8yhBWhduokLVvAtiuHy2e8/VEQqEcv4IKnmoniSZH1SMcoaPlMdrMwySKma+iCjNApb8TnfLzMA+IpZTugVxSaHbFF5kbKKHL1fRKBzeWC2aljnDYHSjWp8+i1Ajo52nFATl/YRxJ3LJyITbp8QVUleqEGQq1tCxZTtnH+FS8SZ4NEe8gMQFRo894bJ3hSPVMzDEW6V5C+oR6ZQFu2cdvJZFy+yvGgvbV4PxHY51YMSmlypnPV3BMKYaTs0aKJliSWcMFX7sW7h+kE8xbg1ebsg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(66574015)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?6dnhumuSLaHNtcx/G9lQdE9Pslt1?= =?utf-8?q?eIaHs39ck4uBGefrvpPzZWy9TM/ODYU+yb+0m/OkViTBfuH9aKsMboMOALweSXrpB?= =?utf-8?q?tDaRStSreRFN0FCsVNbyxp0QdrHZaxT2GTnrj0f6SHgYkh8hWsrSENyutWjyyrlW6?= =?utf-8?q?AKSbtPa81HfRm5ARzJSDZNT1z4OxjRCmgUjLi6VrIAZGqxTIgoIvdy0M3XcY6qEH4?= =?utf-8?q?NYTfa3vAN+Tmy1gqi3lfQvI21vQHlg4sP8yRH3owsDnwuUL4MFgoalLVpEXujPH6X?= =?utf-8?q?oFsg1C/Y5d7ehSFBSqxI6qsArEkb8lEpQNwAY+CojsgAIG9nz5K1yJJKol0Upr/aY?= =?utf-8?q?lwlAOAcNrY2Yp4M0xZGLB7ENd9CVEWzouk5QIJjmpdEW+kVgFeb8Euxeys8lHbr5l?= =?utf-8?q?5ddgt/vGWlDvphr54r2xddat0smcBZLlvZmLolvAM3F7e9UpxaV1km7iL9ut54WN5?= =?utf-8?q?d9XpDcHSIHsr+Z1mla5P02A1ic05ewwBTZUHST1BRiDXK0+xsb+EkEByzbvbbAc/r?= =?utf-8?q?fRgFlT5uGcKKHdx77pgszSKFvWmgWHXgAkX8stERS+qY49qklX/GYxNQNxGnOUJ6q?= =?utf-8?q?dhI5RTsqqz9zBbuac+FY1K+e4q3zSqCtIzPR8FjtHw1OpAZFYSzmXkskk3oORqejh?= =?utf-8?q?V1m//6qzoDdhfIZ4CfV5WNAF41w4KDjTXwUpp8v45mZRlbcj0vfJ1F6JlwvNOEVe9?= =?utf-8?q?uaT0ZtXBCchPNfdt1OUvbxriQOHM9quNPWHWI8OAtN/6CEV056DeJLgUiaGOLvrA/?= =?utf-8?q?PT6Xz5xETyrcJ48LtuV7YGsGXFpYwpVD3+Hs0VR0jL4GWuWEw+xK53xbK7wpa1nD4?= =?utf-8?q?A8+8LN8Kig0wEWdDNRI491QlP3EB/fHPni5HHQJbhycaubQ+s/GVMjmX9thq8hzhd?= =?utf-8?q?pp9SM/62sgsxTwDiuojhtLbSSRNvjNC/2rfWh9SRwOqcF0vsdbjLukoyEwgWx9w7H?= =?utf-8?q?7aJFXBTew3QaSSnntTR02c/ymZJhN1hYEKFh+Jh1HWp9HfG7kp+U75wXjXKk0+R8y?= =?utf-8?q?K3Ows4ScVJXtW6tFppnLjI6PPTereESjUEt3IlEO2TijP+YhEl6BAWeyyHygbi2Dh?= =?utf-8?q?zFXUmRSf+L4tEpIWjnEG9ZrLNm6CXb7bLoLVq/OddpCbSG4HHh+VdaTzEbcaP72D5?= =?utf-8?q?Rhg8FMAb3BrM7dOdSz+vkedFSvtvU5FUaA04cIplsBP3XlAoPMi+n2VaMrb/BKpFK?= =?utf-8?q?1DLfV/nr5/Lr2eC0Ij6S/aUzmaHUdjQRtr9URHDWnWbtSB8ar6pmqkEN68dpuJIyb?= =?utf-8?q?f/ZNWqpHPX8w+myRCovkkg5jQU1p6rKRDA69ouIxAl1sEjZNOwmvPmGz2CEBUfaFu?= =?utf-8?q?VtXgf96TUc1IFptG2m3y2TYQugV2D2qx4SmIyvwXlxic1RJbrDGW3q9g2cOWmesRu?= =?utf-8?q?Q8Qre4gke6qmSkNy+vsh1aG8AMsSoGRWUKwe0yfd7R9bIcpx9kkeItxnQUaktcZ52?= =?utf-8?q?57EE9EYOxOJ2AeRn7yWwx6sfMu6y37PM8wRTlsek7k+QDnlHtfV+OjYtpF9Y468O3?= =?utf-8?q?E+Kns8WSuqfk?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: cf49a282-9822-42ab-b0c7-08dafdce382a X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:45:37.9539 (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: 1MgMqsKhCJCMsd1D6jWU+xFhtwWJjLx86tJ/pq0DbRkuJbSEVsgnbOZqTlFxEOTRc/Ba/GHQ+wqCB5zpRuCObw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Queue-Id: 03272A000B X-Rspamd-Server: rspam01 X-Stat-Signature: 8fh6y9mi6ujsk65xn9b1azmtnrxa8swg X-HE-Tag: 1674539139-408628 X-HE-Meta: U2FsdGVkX18gUz+5AKdWcjkVVoUmGi1NDD86g403U7fScXc0dgP+QTYkCNStHK+wJeKBss64V3BMIuuP3/94pNp5WnoIMfqOVqMbpzlgeP4Z6XAhu/6SJzYdmujNCNCMC291FZw7jc8NGhSV1WdNGMugeewDvdzc1mOFU+HyOj5vrqshIdjGfkOW3A6JLrsQtAhFogoSvbsV6c0U6EHkrTzEEC3ph0hRooQpDOISFQ4VkPrF+PFJ6hUHDHXjE8kX42SHFYrjTfufIW98MzUGlKNKPyrmxH8l0Ud0SZ5weEGbAiH49Y8YL7Q8EvrJIbtmBcpcvZ15k6+3RaJfRRITmkg0urkTFOYkHzadipiPY4YCP6exUjPDBOo4CMf2svcdsZh+Okk/egBYUN8fChIkeyvbaOrsGJeDI555lg2ENXkA333mBJQVFykk9QOw6TZvIe5ui/eu3nMXW/rOfkyx7UTvPeKXyke4zCMempC7mKV9LX7mLbOfbTEiG/U847889F4MshNzTKRmRzStla3R7Z+dQmusa7txtmTAOEz5dPqACUXve7sLev6T1JMcs7Y1IiMXvLfbMo03omCuJJaHNNQch9jqkZbOz2Zy7aEzdSgeDDhKSfWSuhnXmF+pWleRgLhm7ymnvVHIMnTbRcirwceBeconlnQ9EUTD6mqbqQnzvyItDT7G2rcwBXAIa6cC0uWKlqlA5TDJEnSZ+IgEJIW9aNpMi//IXn8+lujX4up2Be7QVm77CwwHPAxzzhFuqSL0HMSb2o7Yky97vT+szMeNtnHTQ+d7Eb1wumHDuN/+0N2yNgzmip4tVEtiHvuQ+NgqT729TX5L2x6SAhu54C/x4RboKKGbAEm9VUqNoUWrBcjyzBu/S+bZlnM6v0O/kC+XT0sTy/0CUWkPi/Kf0TKc9YgH4zgYKpsa3oudLO4jYDZSdZB9/xe/d4u7AVmNrdDriNfYEAb4Co98iQf AMjmz1Dr 0KFI40krg6cGBEN1s2U9L5nAVfczO4HqNQTvYK6xeKgk+Dv8NS5kCDW5R1vm2rr/LWllsX0jL8r7dMw8z9whsoBbSlGo5RHafqLUlLHfotvSYDJm2kyZ9FdivHlkoC9daqAl4bHrXRKk+LrzLhXYnefuyZ7UolD8w6UBHJt6BATq/vEQFy8WlxPSO7GrKqHaJ+OI/bpqpBEQc4OyXXfRPYcmqC5OLxaT2eOuCBMD36wdE7SbmDDFwvG1XVSn5JSIVNbqh5PxyQ1lQccHnEUidK5Qo+0HiSfx1a7vo8M8/SYM1WRE/JKvUYJc3ghxaJfKXOB7LKsnSQ0ZhmZ9TB7xYBb7UiimbzKhftQTX72ajalv+WqRQ/3nNaPOFDSyScF3WoCy9h5xjN6pEC26NmS594GCTV0uHGIZUPgCsh8OUT+j0n4aGlc8PH9s3szxufJZpPRmp9MRQFJnwEO8+AoK2h3zNgS9/EVPBZIaSBMXZ5eS4iKHtjbS6ZyvPl/34jVtCYC8zHT8VfYicHNEIMglKoywRzQ== 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: Switch to using the new vm_account struct to charge pinned pages and enforce the rlimit. This will allow a future change to also charge a cgroup for limiting the number of pinned pages. Signed-off-by: Alistair Popple Cc: "Björn Töpel" Cc: Magnus Karlsson Cc: Maciej Fijalkowski Cc: Jonathan Lemon Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Jesper Dangaard Brouer Cc: John Fastabend Cc: netdev@vger.kernel.org Cc: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- include/net/xdp_sock.h | 2 +- net/xdp/xdp_umem.c | 38 +++++++++++++------------------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 3057e1a..b0d3c16 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -25,7 +25,7 @@ struct xdp_umem { u32 chunk_size; u32 chunks; u32 npgs; - struct user_struct *user; + struct vm_account vm_account; refcount_t users; u8 flags; bool zc; diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c index 4681e8e..4b5fb2f 100644 --- a/net/xdp/xdp_umem.c +++ b/net/xdp/xdp_umem.c @@ -29,12 +29,10 @@ static void xdp_umem_unpin_pages(struct xdp_umem *umem) umem->pgs = NULL; } -static void xdp_umem_unaccount_pages(struct xdp_umem *umem) +static void xdp_umem_unaccount_pages(struct xdp_umem *umem, u32 npgs) { - if (umem->user) { - atomic_long_sub(umem->npgs, &umem->user->locked_vm); - free_uid(umem->user); - } + vm_unaccount_pinned(&umem->vm_account, npgs); + vm_account_release(&umem->vm_account); } static void xdp_umem_addr_unmap(struct xdp_umem *umem) @@ -54,13 +52,15 @@ static int xdp_umem_addr_map(struct xdp_umem *umem, struct page **pages, static void xdp_umem_release(struct xdp_umem *umem) { + u32 npgs = umem->npgs; + umem->zc = false; ida_free(&umem_ida, umem->id); xdp_umem_addr_unmap(umem); xdp_umem_unpin_pages(umem); - xdp_umem_unaccount_pages(umem); + xdp_umem_unaccount_pages(umem, npgs); kfree(umem); } @@ -127,24 +127,13 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) static int xdp_umem_account_pages(struct xdp_umem *umem) { - unsigned long lock_limit, new_npgs, old_npgs; - - if (capable(CAP_IPC_LOCK)) - return 0; - - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - umem->user = get_uid(current_user()); + vm_account_init(&umem->vm_account, current, + current_user(), VM_ACCOUNT_USER); + if (vm_account_pinned(&umem->vm_account, umem->npgs)) { + vm_account_release(&umem->vm_account); + return -ENOBUFS; + } - do { - old_npgs = atomic_long_read(&umem->user->locked_vm); - new_npgs = old_npgs + umem->npgs; - if (new_npgs > lock_limit) { - free_uid(umem->user); - umem->user = NULL; - return -ENOBUFS; - } - } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, - new_npgs) != old_npgs); return 0; } @@ -204,7 +193,6 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) umem->chunks = chunks; umem->npgs = (u32)npgs; umem->pgs = NULL; - umem->user = NULL; umem->flags = mr->flags; INIT_LIST_HEAD(&umem->xsk_dma_list); @@ -227,7 +215,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) out_unpin: xdp_umem_unpin_pages(umem); out_account: - xdp_umem_unaccount_pages(umem); + xdp_umem_unaccount_pages(umem, npgs); return err; } From patchwork Tue Jan 24 05:42:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113493 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 743A3C54EB4 for ; Tue, 24 Jan 2023 05:47:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12A0A6B0083; Tue, 24 Jan 2023 00:47:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DA746B0085; Tue, 24 Jan 2023 00:47:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E97176B0089; Tue, 24 Jan 2023 00:47:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DC58A6B0083 for ; Tue, 24 Jan 2023 00:47:39 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B1564C0232 for ; Tue, 24 Jan 2023 05:47:39 +0000 (UTC) X-FDA: 80388610638.23.9669830 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2085.outbound.protection.outlook.com [40.107.100.85]) by imf09.hostedemail.com (Postfix) with ESMTP id EC5F4140003 for ; Tue, 24 Jan 2023 05:47:36 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rgnlu9rL; spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539257; a=rsa-sha256; cv=pass; b=DaMgD/K+af4HQR3dmwo6IrCYffjEDkZ0YJFX/BZc3BHj4+QfcvhoMQR9oTYUsHuwshGmM3 +72wccQsKMqfaalBVNmUlQfWIqn6QhnPgPjeqanbFQQBdk3tr+luy6vOwnL6hZp+UUfgaT f/94wh52BXOTSTFIAI3sLl5CK9gH4/4= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rgnlu9rL; spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.100.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539257; 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=OiXAsGxFz4TnMqFpYvIEfO7AjQpGiup8ce6UITBOatk=; b=6HlIhCOML4bzVOBmLDryYVRlYt2YgsmEtvDOcZtik2KeGWPAjhmOq8T1P12VKUXOA+m9JB b7HLmeZUNxgTdG/sham4zMO+yZeC711G8HetLhlIJeb98boRHAzc03Iuavwldpy/L8H2fj IReBv+IicGMWRVisnLgLVQBQQzO87ZM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f6WFpM+KGgVXCLK3CDkMqEyxAZeHDHBmj4F6eNRNycTGGJefFOz78c1+kmsG/nX3X8J8nVCQ/5XRUFJUJqu25gYNhBwVgBIhwFSrZ6/jR6OZ7BjOgyzzBXpS4NxEaWykgIPhj+5fbXAzTpOvQUJ88NeyPBSVAf8VC5Ya5o4vOKpzHjbf14WatO09CtGcoZcfZ7c2HJO//kOwiRzWI2Xyy/vesE4BtG2+pxP1/i6Pad9mzBexcR+cqoSq9jPUQdvAhaWtgqTAESiT6gxNhLInO5yCIee4ziqsz+ydQ6h7Hvhcma5fjNy6v1EnRvdZswUmcQ2De2N6dhJZk4MQ5Zs/Og== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OiXAsGxFz4TnMqFpYvIEfO7AjQpGiup8ce6UITBOatk=; b=R68VTLlUoRb/Or4yD471LhxYYBM99AXZWMElK1NEJDnpE3oN1QL6b/owl262A+Ow232Vbwd8odvoxxSA00gBN1kRZpL2j5PnGJCKVNrX7fGEoI67V3CMlwj3jU2j9AGLRHxYzE7P2Os8KGwcuFIjVln375S4sLyr3eyvXvfFwaMrLf8787HrxBRbA33hklRL3LUsDezVAmaqOJBXHK8sfdPjAInIES8p8bhvM2dMf344ywH0Zv5qWkvMTlcl/jWWBN6AHSf+CcHvcSQt4sNlf3D2VaI0y4rlbM2ZQGIB0VJMZhudMX8ayvu0v9Hq4Z+NM1PkMdF9Xx5ilnXH1FhuTg== 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=OiXAsGxFz4TnMqFpYvIEfO7AjQpGiup8ce6UITBOatk=; b=rgnlu9rLsxAYDcNmduxLEEeqHjkqyJS3VwgYTl7/AIDwfxlMSrj0wUWbB1hiNextRggFqomxQobZDTBP2aMhutu2VibNwARNt+b9Q7eMFhFJNMF9x+umjhMmRbG0FWe3HZ/YsLqRo2dBeFb7A3nzM9TE9FhPlqZ3CSRVYLdkksCs7Ty7EtoaZj2xPj6VvEE4/aJOTT9ibTj++8gnvdY+Sdyq/N0iN+4VhT5kxl3iAoydB+HNzwr0qDA5wQY6ZSys/hCtULqLO53ik4/AMCK03iWoGPL6Uqe/9aDpLpycnuwThsGaBx1cuZljMspBJmO5B/rHUR0Qr8sCNB1gSmTpVw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:47:35 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:47:35 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paolo Bonzini , Alexey Kardashevskiy , linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org Subject: [RFC PATCH 12/19] kvm/book3s_64_vio: Convert account_locked_vm() to vm_account_pinned() Date: Tue, 24 Jan 2023 16:42:41 +1100 Message-Id: <3ef4b18a0ff174fcb2c81f8fafe900b43216d473.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0119.ausprd01.prod.outlook.com (2603:10c6:10:1b8::13) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: a789dbd8-c67b-47ea-db4c-08dafdce7e42 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m3GfK3j4uYELDri/PZKP/QDb+cu8xfOpruywf9pGT78/nwH69NjtTkVZooEbnWZiKxFqCtAhXjLY11r0iP9FHPnmknNOjTctPIlfhASk2aLkBzc2d0pBV1Oo/ykUjlLUXAPuOZSavf7g5pHHZInMEOuotllMKhKfu8wyVqNk/X/UpQPrZMX75W04yw3zMSjOOPPV/2oL/ZOFeBVD4S5BFuRfQj2V95IhiU4+X2cHEGak8YXq8yxD0HsX21paTdNizAplRu/XD/d5pRbHCLgaKA82eI2lajEGBC4OPG+077TAAmZzW22nrzN8NTDAf0WGBj1qo7lDKgqql22IvvjEDVsOB+/hwh25fWeHYf2ijwHuYXMv6TLopA5wylD0zdMzylwTmX9LJPX0wYt1KFQmkXrLigQ8EInx2TGavklWtoQpVq8SVsazALz7kZeWhv8EZBKfKrh/da2yNXwAcmlYE6h2viz/ASpuKSbdAbu9VRw2/9Iph580Jpqie3Rd9u34gUDa3/Q9LZ4wcSDeLbQ5YWdJj0A+uL/sGzepcbVufraiU/G919apFwCBa22MXfTxh9iWkDN0idoxD6tjXg38JvhCM+H7Ba+64++CuC2vafCTriNTI8p/FXYS4fryMjIZ4/ivuIj8cimLsvq0V00pu/KI9HyRdMjddVYkd8gS9QHcTsWcB904/38XoS9JUzHOPQm36eXWoq27/3it9Rcylw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(15650500001)(38100700002)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CYyHDDtdLOQb90LzVyUY4UE/9cWsAcZfB7PLijN5t8usFmfF6v5c4pv9/s13oeP1swBWQJqpzxI0mOa8meFAHe7w30T7z4CfiQBE46Ym0WGEUqYq+ebIBboQxwpyGcvwEc/dIyHnKibvMMUX5xscg7x+ab6TQEdPsr2QVl+UZvv5T8xrjdi8G4HGN21bIjwIHMLfseMLLu/P4zQ4CGCtUzuzEdBNV+Goe0Hvpw1Rc5LxXFitUIcvX4ECyWw7RqevA59iYEOpqV5on+ZA7dWMDR75vL+n+UAuOHkD6SA9UiGSTMGlwSX1HY4cN8Mf0lcG31aX4mBbj9czgxNwxdQi0JHwNiaNBU9zPwuO/XdNfVZ5Y917SzYjVdJUO95onCn4LTIEXBWQpwVxLrd+Niis9l6nCecgTpkgdFZLieLs/6SSW0qQyFMzCnQNiK9sja4j9USoEE3A5nrQafVO1K59SCrgGCyZIHsRCMQUSu1w2yk/dJcN6oyOkOpYSUpTwn4BBmwEVR0o4UAoJs1N39WiEE/+4CUlcbbA5ajvHkfagVX9+tgGYS0v3mrmh5idC58IGR6wkM1WmRRzk6AkFMk94PPfBwtffHvaCR2LwMXTJc+nuVx4LWg+JDxIo0c2k4IImnAo5I2IhsUkOAECjES2tw1ZEJTzVIivRYL3jqN8YfnhUQX/rrwYFKn9ol638j80Aop6Jxm+UG/LwCn/IXhgyKWkNt71xU96GRPr0fS3SQR43PGmN0R6FcY0BGO6knyZq7hLfEobmyh1O/+q4lYFps06BGXPilCyLbsSZrovNMBS7Wnbv6ahnCJ0IlQhGpAcfXwUU7Vh+rvE5dc5PPIG+gWS1OpfvyFaU5G3f+5sWpfrAYXkSzhebRG0LgAu//5sFHGQ+996PJsYWfVjAenIN8/TbBWF3t8R0EZbxRER05oUkl7zaHEY6RZQeAZWnDq+vtnApWiEfOqcFTveMcQQymKhZ5j+XM55HMxbcqDeqZKeEd+ZI899rJGnjyMP17rsZWAn/tiMm5LLCjj0uj/y0YQStGJ5YB47WTO24cel1C3LyC0g4+ttMz6LMKy7+RdNQIuMu3PSTiH+CpHjltrqB9nGv54ebtvWZ/qLVTMYqLcWJTW11HwI/HoR8j0G2UthsLVmIvzFBUWMUu5hecsqSO8FA4y1LiQD0XsA8mr1C/oPBpWcUMf6PN3cqKkQf54I7adepjCycJzJweymWze2drE64nsGwlBB4PIJm15NKOtJ+aIpa/sNiKjsUlT4KTmEtZ3Qi6QbEjl7ixXWTMCbW/gBTGkcu7r/afSSuSnE/HrV6JTCZOPc7pgpYYLrUUCjfWKe348NsCP0HY9OeNcW+/EPD+NGXJociqJVakZkX4ICJCZ0cCWXea6ChWvz4KpZyi/vhkOJyf+/5nBGwwLj8LMypCc1akNoLYeBsH6ry4sne1so4hF16or1LTx2y3WvMl5AJS4qxIPND7434xYmTgI8f1OGOv62C2sK1a6HTPZnmfvOOot0sRjVUjm+iYACFMLTbj3J7nBL6pBeSP+smya9Hrx1CDnOKSxlOTikMLrHWugHyjKEV4HXojG4LDVL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a789dbd8-c67b-47ea-db4c-08dafdce7e42 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:47:35.1466 (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: uSIxosOhyYnggBMrzxUpELFZCvLpxGU9zyZr5Jw4I8eeIkkwcbgdly1TteKS+fCCzoGIToCVbi3Q2ZvS2FdAGw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Queue-Id: EC5F4140003 X-Rspamd-Server: rspam01 X-Stat-Signature: hhtz8yfebfkq36tcdrn7tqc9pueaibgx X-HE-Tag: 1674539256-916043 X-HE-Meta: U2FsdGVkX1/3Oxl/+OKNFtDsUvV0qvBaK7jzxwLNCshmmB/qL7sZ8oKWDYQt3JMZdhngsVP0wRl/vF2BDuXuDdElxuMX1gZPwHxNFSYr9HcO4m2gWa3KiWtGZ95aUUv10+ijieWkGSybKooJW8LgUq1cFHue7m9LSJHjGs1REiGGmBBqQvpaAPIpKNNOTv5PKB9qNtNCN4ZBPZEzoAj8nKPI/9lMBljA2VJ/YRSSQ0cbktGRYoxPmezA6SQ4PesyKWiEgLP3DeljUfn0jR7lgoRTEIEROY+KVqoNLm96zzMkOXDjd4hy8+uNgd5thaGhjKvOT0A679iIlUkPxMgAv1vGG/eXy/aGZlZZZJhhJ3CBkCak2xZA3iNckEzlkbGkWduEHDUMJD0zon1wOWDKPktejaxeEke/dMQ+DloJH4LeTAZK0IxVod4+K/sj09uWx3i0iV66ZiGDnDy7OCwDuF2Rv7DH7LmoQ9JQFY/09JktyffDXM1gh7qaezi3YNnfXF86CriGZGTXnGig1oLmiDXXQ4Zw7lpnVxYZX4njtQfe7GRre8DeW88ECn005SqzLSQjwhZNSNXBoKkD9qJHZHj74LTwm0namlwQG0R2Jfm7TWH3zandoALP1tamHreUcXiHphLUNjetGMjHLvQn3d+OYq5OCA9G49Qk1PclwEQ2QcFsoMDjF/yqZ4bq21pvExkhcmgpnzAONDVMm4K0U/XPDfQNtu/NiaJs/URKxz4IoscsrJmfQCm0lr63wrtyGGY5K53EVpo8vh5B8R/ez9yD7907eKxIzn8i02sq2jnlxIFeJuYfkjjGOSAmEouFUV2/HKqkNISOymxcXk0kBp3DE0R+RDXmeEaKCafDhaxEAZfesjNAIe1edYYMVnRxLnxy7QKN6iz5Jwi/GriSV+b2JUc2nTL+6eCgsPUVC79sxEI85WUe3xVjLjyd9yPV3ymizTrZ3bS8n+uFn1u G7N/gB3V X0qVx2KfvHSHkNUkNljCFnshTL079+4YUJ4hPiav11QTi+d4Ohg+iciKYM1TXCKhTdZHPLCaNqQpGUeNnoxH3O+sMb9fCLjoAIo5T3pcQC0s7ECKpkPL3zsl3obbMuStYFfkByqDPKroqFshXBguEApx5jN5Q0df1+jVr1GeJ14lH9dvqht3jxJ2lBsoPE6F+zfmUwcs1Fy6k0B8c9G3enBMkxM//oBMIUIEYvzZuztoDZGEu83X1fXt98iL/yYECjJCxsziaJXjx9ycv6T1RaGdF0QFhTLNT9eyE6CtxKrk39lOfIywqZa0vQxRsr57ol5562r3ACUKWJPOUkn2WGPbV4W8zCKI577YtCM/BfA7/sr7V5uvocLa3R6C/AqxgE4W6fLkdI+Kch4tsSKNfbWd95sN6Ho1UQBp1zAhY541rQyk/cm6YGuN7hsGpv3H2W3dP3yKSrUJQNtj+j8TdpAqsje60LZt8296eezm95K9w8rh9t7U+mu6etWKPM8qVHUs1cfp7NVeQtWVesSAZju/2KTZP4bnEW0eypJh8cr2jWa5P8t/TzV1VBnlJ5OyUzArz2gEepCeSXin7lsQDy/Ts2Wib/xTZmCspHV/du9KSc2Bj4bFucUCzNLdEmTamu7/uoMBgOYdI+A5IVtjETB5OjGsAQC1kNoxx2TB4WDsnFQw= 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: book3s_64_vio currently accounts for pinned pages with account_locked_vm() which charges the pages to mm->locked_vm. To make this consistent with other drivers switch to using vm_account_pinned(). Signed-off-by: Alistair Popple Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Paolo Bonzini Cc: Alexey Kardashevskiy Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org --- arch/powerpc/kvm/book3s_64_vio.c | 10 +++++----- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 3 +++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c index 95e738e..ecd1deb 100644 --- a/arch/powerpc/kvm/book3s_64_vio.c +++ b/arch/powerpc/kvm/book3s_64_vio.c @@ -273,8 +273,8 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp) } } - account_locked_vm(kvm->mm, - kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false); + vm_unaccount_pinned(&kvm->vm_account, + kvmppc_stt_pages(kvmppc_tce_pages(stt->size))); kvm_put_kvm(stt->kvm); @@ -301,8 +301,8 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, (args->offset + args->size > (ULLONG_MAX >> args->page_shift))) return -EINVAL; - npages = kvmppc_tce_pages(args->size); - ret = account_locked_vm(mm, kvmppc_stt_pages(npages), true); + npages = kvmppc_tce_pages(size); + ret = vm_account_pinned(&kvm->vm_account, kvmppc_stt_pages(npages)); if (ret) return ret; @@ -347,7 +347,7 @@ long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, kfree(stt); fail_acct: - account_locked_vm(mm, kvmppc_stt_pages(npages), false); + vm_unaccount_pinned(&kvm->vm_account, kvmppc_stt_pages(npages)); return ret; } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4f26b24..25ed390 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -717,6 +717,7 @@ struct kvm { */ struct mutex slots_arch_lock; struct mm_struct *mm; /* userspace tied to this vm */ + struct vm_account vm_account; unsigned long nr_memslot_pages; /* The two memslot sets - active and inactive (per address space) */ struct kvm_memslots __memslots[KVM_ADDRESS_SPACE_NUM][2]; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9c60384..770d037 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1142,6 +1142,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) KVM_MMU_LOCK_INIT(kvm); mmgrab(current->mm); kvm->mm = current->mm; + vm_account_init_current(&kvm->vm_account); kvm_eventfd_init(kvm); mutex_init(&kvm->lock); mutex_init(&kvm->irq_lock); @@ -1258,6 +1259,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_srcu: kvm_arch_free_vm(kvm); mmdrop(current->mm); + vm_account_release(&kvm->vm_account); module_put(kvm_chardev_ops.owner); return ERR_PTR(r); } @@ -1327,6 +1329,7 @@ static void kvm_destroy_vm(struct kvm *kvm) preempt_notifier_dec(); hardware_disable_all(); mmdrop(mm); + vm_account_release(&kvm->vm_account); module_put(kvm_chardev_ops.owner); } From patchwork Tue Jan 24 05:42:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113494 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 40ED2C25B4E for ; Tue, 24 Jan 2023 05:47:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F6CD6B0085; Tue, 24 Jan 2023 00:47:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A2DC6B008A; Tue, 24 Jan 2023 00:47:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DC986B0085; Tue, 24 Jan 2023 00:47:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4BDA76B0089 for ; Tue, 24 Jan 2023 00:47:52 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1BF6BA05F2 for ; Tue, 24 Jan 2023 05:47:52 +0000 (UTC) X-FDA: 80388611184.27.D8F4FC2 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2069.outbound.protection.outlook.com [40.107.93.69]) by imf04.hostedemail.com (Postfix) with ESMTP id 5C39140012 for ; Tue, 24 Jan 2023 05:47:48 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=OkXIWNgx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.69 as permitted sender) smtp.mailfrom=apopple@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=1674539268; 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=JHkCArMXy0OdvLO0RAQFjr57zAUQQNccPb96BhM7l+4=; b=7o9VZfQi3SkPN3nGgYCWqKtbQlrvBKAsZk1D8dC0zu8QZclUYpzHsFKpqWwhgBIvqShttF fNgeecbP1jV6NEoRQO7+DXdXu7NcovEpkw680K6a5/vZU0jV/o+Dqhp9MqXzenOrnZPVKL JJczLAmA+hTOCbaTgol94RkH5MIpUnw= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=OkXIWNgx; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.69 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539268; a=rsa-sha256; cv=pass; b=EiPwpgX7wM5huksdZwOhuxuoHf1V78YgExD+IFedh4tvL35YbriNVJtmnmJv9a81qrSBfn o4uyL2KaMRtEmffaJ5WV4XdHWfENfEtJWeL6l9wpOMQHgMUS/6q20oOlzKOw+fUomN76VH pzJ2qpfPn6NNNQFgPdpC0Rvdi/WjF6M= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nVqeVIchqhUITUKkoCUTolmxFecSRzVJZDvyy/ZDkH2/IGyl1z+Lp9QpW9tfHapgXFAuAJlUcaUtHuD8XlUd6QDmQjxOQhgiAsiQDv68pgFa5V2b7Sm50VOBhXu7K+2DoTVjjJllZTkX47pfxiAOhKojHqpnB+RF08hm3q60pvEAmAmFbkgA3e15zOA+VDLQZOaYGkuKcBPmperjcQlodhTNaFNqnzpdz1bWz+VJVcrmxcvOX2aHRiGoTGChnwsMhGAi4G8BF+bRCpHFQdhVfXuylLjG955vFtJ+kchMAg+oEmUQhE1mh99viLHoSAUR/8CLsvoElhFnJcSgwnW0jw== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JHkCArMXy0OdvLO0RAQFjr57zAUQQNccPb96BhM7l+4=; b=Bz9+IR2B11qVnhOMtNVdOUdnxuv55O84hSKQSIZxrTOdjqL29LPDt0X6VW6pe3vCaimEWeEoBx5pYcvZVeB+fN1YdOAMmAQrfs/SKabBk+Hw6NsZ4Zv91orhdN+ibu8tj42/9VQM/lEa1XxthG6EdZQ/kBzotkZE02jdrDgbIUh6N9+MSGoxz35DFFbVLABd6/YnqVc6XH54DNwj7RXvMGUmq5icx7QAP4HsnxiWXPR5No/ZCmWBj/zsAHa9Blw0cuSnxZ9ZWKw5BsxWPiJ7c31jvxfM9iN60wGIgabh4CaVw2q+rMYNd7aCYSvVxjb52EVRglV5awH7GCmpELghUw== 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=JHkCArMXy0OdvLO0RAQFjr57zAUQQNccPb96BhM7l+4=; b=OkXIWNgxDiDGgLHMtSXGhlKxx0DPyHdvKoo49YG7qSKBVhYDH7Jtf/cYs2RfT4U6O176yq+tfUybhgU98BeBOSnKW8EMf+NhWXp89QLNQhE9d6v19q0GBmTVyQaGNZ21AmS0Pm5J1lUc8uRpV2+AYq2oxyJzG7ljaEHxgdUzPqxx3IOXGPyXhs5GN7OI5HaFG8igNzgTxI6cg+3KJNaM7ehzmpYXV4IzFbmbZETJyM4p4uNW0xQtnhxPnfypsAFvgtPbmJXiU5tkyo8S+qfbCTqA7rF7JFrRQwXPy0Lnhig8hG1GkHE4s/xSAWw6/N1DD4+J1Hf74Gh//3INslQcbQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:47:46 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:47:46 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Wu Hao , Tom Rix , Moritz Fischer , Xu Yilun , linux-fpga@vger.kernel.org Subject: [RFC PATCH 13/19] fpga: dfl: afu: convert to use vm_account Date: Tue, 24 Jan 2023 16:42:42 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYXPR01CA0110.ausprd01.prod.outlook.com (2603:10c6:0:2d::19) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: dce7c2be-5135-40c5-3e67-08dafdce8503 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Xx0ryKRawWeSDLJ1jfK2YfejLjG2YGUuJIFGocK9qFPO6LFXIQa6pGyWvAteg3TyAjGb9QDAIoET8ZHzwoJK5UKnLm6zwgR1BtNb/IQ8CZIBBxSedQMbF9fBTDEeigXhbNBh5SDfNOMXzkdCPyY9vPhbSOgtnLqPfH3Hok+nLBpV834cVNTfcO79tDVWTLjWibg6661F/wu64nlbeoD0pdISPfHC/BvIx6R3qKrjFqGD6Lvw0GS2WZMmXgs9YI422xAVHq62ix2/UEj1zhuRk/8h2vD9pCFjBbeLXHyY6nwzsB+OfyacNkY01z9hjksk3X9rn3QYQMTg7gQ9279pKPwdqY1/ri2f4Rkdzu+BUw+t5qj+qTThDURT4hvuh04UQEY6cXIdVANkkxICM+swzFBvP14j2kEHCufaG3Q0AXFS9G0whourcFL2yi5FwRqLtvXCZ0iRQPZxilCuxIWfDAHvnbaTQXArAxtVodpwMCTXeCH4yt6qcMn97/wm0YvVD5Ui9bwGZBrxUoJEsJkmmhnJE8nLMamEsNRr4nxbSZL2IjC5oSOHjTCtO0bxHSHUGv86p2OjkuEIDx8qx2oCE1tb5F1qUR/n+AiuQijIPUTTBZ3CyjqDaA/8UbFGQq1CnZCNZtcgHTB8+2yeouzF+Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MQ0ezOYSN034l3p28FXn9xIG1zjnD+nzp8ve8u/q26gNVCmwhrhkGLwDRnrqp1AtXvYt6x/iLcAdmtxVTGrekY/9B0H9e17dxTWvSs3ro7XH8OqnYvgjctyihAT3DEMK9jmaKcfeXEUN+UORJZNR5VraPLJUxgeq33AXN0/Ga3esEnzCfWF8cYmUX8VATXTbdjEHTo904LOYb9sIjpV+MOdnVQiSjEfYibCYQbeKxTBaRWON0RAh9jaXnB5dOvOs+UDLOs++aee8K/IuF4mJ6jP2jPyasm75UasyO1I1SwXFIAYNDpjF2eWfe8Z5dXbKGBIR96hkLqe93RCzDKTkh3Tq2UO/QxeHNCXaJgJXphkaFh0qtO/cs1Mg35Ogmo4/b0ejWOAu83OUYvJuasejH19cE22TYsYtVYoZEfl7akGX8LVPlw1Bwi/A67N4wABTQLbvisg0QFhb5j1dA/gxpNa7LfDyHX//hkXOHAvSP/85YNldEaE26tN8y/z1V6EUbcb3s1OLBUbxjSxaFFVnzO06TrTvt6x80T1cOyYQgEKTzXE7eqTXUGHVww3NTSVObs4z9lUaC9oYbZriCv4nFIYpZ16doudhLC/C/apYgEyUQx7hPvYBxNduXuN2cvJahRgXYsnuVhMA9tHRUNjrINN+nCy++GOsCue9kxUPXIaCFivgSZpU/y+bj/o/+svexykZP5/WZRR2EXUskZRgJ+t9PLByafVQln1tx4OpghfxpbwxDXOE3XQpQacgh12G3RrBvSR2NqNSyzk/tCpqPuXko+DISmwSMKh19Pi6jJ+23Fc1JZrzGxiS89tNHu4rk3oIesTGFZZkKW1VkOW4s4CyvWXguVePo3V/1jhYCK1P4tPL8u4/mfV3wWbttT/tfSMO9CwS5ey5YgpUAjnsM8R1Dv3bWKq9gct+EoxzkRVBPZem35as14plIoKtrCfFkAd+SBpTwewmP6iNe0v1t2RfjAfp2jI2Y1UModLM9k6/o/dYgTsiuEgxLq8jl7kjz8+ibVkma/XC3RGzFByPyDm5PFy4gPCsg3Mj6yllmW099rLBc7aiH0rg3NQFddsdNn06b0F5K25kUQ9v/YlyjdwqoCCekvydxBY7XBBxtJQ6eGoOSIzlNzCNJei0FZPQAyeQWJmiVXslP8hpWzM0hQULaPaFRqFZRa/Ojp5aDFycaWm3nqRn/Y0Vqht8geaqXa3MccYSUO0upskWcGEmbh0k68iafYKAqgqFpm7CBRiI1K8TVxxgMeW83bzpWCaxGwSOD3ozlhuJoScG1mBQhFJTDjQGI4eeqPT42UBU/dCjv18jwSZ2YiHOWR6QmHT3w4MCY+DZ4uTNMF9UbTkB/v49ccAOk3sRRHFxj9WEMGz1xr9Fg4TEzE6dwzPrObi8MmRKX8zZsrfK76d9UGXLZ166CNjXzxeI7vpqfIXEAtVQyIpmJwREa+lO/u7gqnEJ776XLxlG4/h6wD+D8iK3gsxUx6JlL4ZtG0q8EWPypBPeP/ev6XYcXZJ9N4pOdECcBeYFzCwU7O9TdcnpGTmzdLvGzFj+re8+MIjLq+7tOMx1qNwAz8177nmTeFUYPRUh X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: dce7c2be-5135-40c5-3e67-08dafdce8503 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:47:46.4737 (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: 4bUO7RKaOO6s4F4e1iuGBwzHgY6c56ikphacUw6tEg55g8qiUZhyhJ5szsNdzx7+4LaiP2JM4fwr7+DRYiellw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: o7urdt615ct6oe5um8yjs6d5s9jjzu88 X-Rspamd-Queue-Id: 5C39140012 X-HE-Tag: 1674539268-900462 X-HE-Meta: U2FsdGVkX1+nW7KZKMNWmThgJrrn+YCYQTUSCMJ55JtxUHh/To+o8OE3fhwnVkwtto8f9NPsDuxYDW5/yoLXd16876cXmjbZlH1Y23zicCkAHiVDoIOXMQt2ijitmyqNW3HHgI93P4jNtXHscUjadrDiyObf+/tAd1/MNdYUBVUuznYJ1GCFn/dBKgsD/+bLOJ9dtRKcmrieyCEjpYTGHdk+gax4oa8eJTJUyKissCGta0juG9YJ+xkpfsA7pdh6wc3mhq+URRBU0a3ywhfWJZyHFsQM+9AtPWzr0mk4mNprCtIB3lWD5Umf/XfaaUZvP0PliLhWLwv0/nFsPx5pkhJ1HWstUwhemC4P61dSrg1HdV6Y2PXOHgiBIzfCqZ3pkEDVIanc1iLrPekG6l1U/T+pouga2Jfxe3iQ+dBDJ1zyfe9Rz4A5lq5ASqGdMwZfOt6gIIyYAWun+Y8owQH2itZRcmXypm7nxcgof3jQO59uyliTZ2OuRuGvsemOn1IlGxI2PAhpREtc5IaXfo+mantj3SP7mSWJF43NK3VIuo2VaBPm/Z3xnknzTezPxZ6346YUfe/GxCD8qPStVsweKopz/1H+aM3+ldJRqKKAJeRLQevCK0laTNIvPr22TlAu2wArM/s+GuwPnHz4CmGbHT7eGeIXvfyuZ2QlEf/c90y+T3dcXOHP0ircZYyn+ZEIWdztNGmbrSEYonS6L8GWWPcCxmxa01xRIls5lbAdVPJ0Pe1fOsxRfpxUsAWFAe2rilNDZm6ypYWVioBjaSW229SGFieh714r7AF9HJ8qqDoIrLV/o/Fjt/OnjcQXdv8LPng2Hd1PwGA2HqFyfTVI7vlOkCSCGi2rWlD1+cVLrVOxNKzoLx6oapDMMUG9/g9CPFztGGxhtPueLbjxwB/bx5PVY4+Eacsr6D6NqlUtLv7yYg4qFPyiThcMZHLEEYxQJdKj3kF43JHjq38oGbS etj6f3E/ J6lXG1Lmet98GtDSquDAf3ILZ3I5fCV/XArFg+zG1HKoz/k3t3BbUsOpK/AFsOzlqRxHdTyouJgSFgij+WYrjbxVYoHuKWh4iKQ6d4lbqpsDyuaX0upDvdFVnP5g2A6/5em5Go95jbfw8BcwJI+ijrP3qaKOh/ioj07JswBr9/v9b4pORhzMmJ4SH3F+obBY4RuKCf7wU0H/n1MRn8L0GRcmMvxcpT92b3BTyn+Xgk7b2h42taxubiGrK3p8QeNDaMSfz8YKB5VPCjD6TN/2e+JaWUw6y5siE2aE+tXWsbOKtZ6JRTPnTmYNRZkkaS03lI8OloRVh0I5N9lENtDnMRbpWkxU8pUGUBMnAv31uDtNqIKF5juf9nmSO4yeKmF7bGEO1OlufPYKV//w= 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 charge pinned pages against the pins cgroup drivers must use the vm_account_pinned() functions which requires initialisation of a struct vm_account. Convert the dfl-afu-region code to do this and charge any pins to the pins cgroup. Signed-off-by: Alistair Popple Cc: Wu Hao Cc: Tom Rix Cc: Moritz Fischer Cc: Xu Yilun Cc: linux-fpga@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/fpga/dfl-afu-dma-region.c | 11 ++++++++--- drivers/fpga/dfl-afu.h | 1 + 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/fpga/dfl-afu-dma-region.c b/drivers/fpga/dfl-afu-dma-region.c index 02b60fd..3b99784 100644 --- a/drivers/fpga/dfl-afu-dma-region.c +++ b/drivers/fpga/dfl-afu-dma-region.c @@ -38,7 +38,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata, struct device *dev = &pdata->dev->dev; int ret, pinned; - ret = account_locked_vm(current->mm, npages, true); + ret = vm_account_pinned(®ion->vm_account, npages); if (ret) return ret; @@ -67,7 +67,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata, free_pages: kfree(region->pages); unlock_vm: - account_locked_vm(current->mm, npages, false); + vm_unaccount_pinned(®ion->vm_account, npages); return ret; } @@ -87,7 +87,7 @@ static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata, unpin_user_pages(region->pages, npages); kfree(region->pages); - account_locked_vm(current->mm, npages, false); + vm_unaccount_pinned(®ion->vm_account, npages); dev_dbg(dev, "%ld pages unpinned\n", npages); } @@ -223,6 +223,7 @@ void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata) afu_dma_unpin_pages(pdata, region); node = rb_next(node); + vm_account_release(®ion->vm_account); kfree(region); } } @@ -322,6 +323,8 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata, region->user_addr = user_addr; region->length = length; + vm_account_init_current(®ion->vm_account); + /* Pin the user memory region */ ret = afu_dma_pin_pages(pdata, region); if (ret) { @@ -365,6 +368,7 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata, unpin_pages: afu_dma_unpin_pages(pdata, region); free_region: + vm_account_release(®ion->vm_account); kfree(region); return ret; } @@ -399,6 +403,7 @@ int afu_dma_unmap_region(struct dfl_feature_platform_data *pdata, u64 iova) dma_unmap_page(dfl_fpga_pdata_to_parent(pdata), region->iova, region->length, DMA_BIDIRECTIONAL); afu_dma_unpin_pages(pdata, region); + vm_account_release(®ion->vm_account); kfree(region); return 0; diff --git a/drivers/fpga/dfl-afu.h b/drivers/fpga/dfl-afu.h index e5020e2..b1554e0 100644 --- a/drivers/fpga/dfl-afu.h +++ b/drivers/fpga/dfl-afu.h @@ -51,6 +51,7 @@ struct dfl_afu_mmio_region { * @in_use: flag to indicate if this region is in_use. */ struct dfl_afu_dma_region { + struct vm_account vm_account; u64 user_addr; u64 length; u64 iova; From patchwork Tue Jan 24 05:42:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113495 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 E85B9C38142 for ; Tue, 24 Jan 2023 05:48:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 87C8E6B0083; Tue, 24 Jan 2023 00:48:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 82D616B0089; Tue, 24 Jan 2023 00:48:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 680236B008A; Tue, 24 Jan 2023 00:48:05 -0500 (EST) 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 5870D6B0083 for ; Tue, 24 Jan 2023 00:48:05 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 33534140603 for ; Tue, 24 Jan 2023 05:48:05 +0000 (UTC) X-FDA: 80388611730.03.3B47D33 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2046.outbound.protection.outlook.com [40.107.93.46]) by imf25.hostedemail.com (Postfix) with ESMTP id 5294BA000C for ; Tue, 24 Jan 2023 05:48:02 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=DZuLdAMw; spf=pass (imf25.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.46 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539282; a=rsa-sha256; cv=pass; b=1hWj5qg9HL3dwMeAVH/0bzaiB7Uvtw+NOitmIxnZz3MF4FVu3L5r2qTyaEVRtD9cuP/7TX 4DQEq728M2g+sDAmqHEA3uSqrwhsi9QDtsJcap2gQPV/DZ5oT3sGjpGDv2P62rhXBcjeA3 d0cwh6KOaQfwKz6xzLNvk7j25neA7zI= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=DZuLdAMw; spf=pass (imf25.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.46 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539282; 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=uYENaESUmkouh77B5LvetjBP009/977IJw8aWG3YGvY=; b=PPCGcx3+sALy1cImcsDE0SkNwbER8EK0JeRIQEHHvEtE9g7Y1eQ7zJpRwEU9K+BIUIzKRI nUfVosClfR2wTvZyNc7map64JkWB0ti0GC9lqmY6Xpmww09+/UnckFuQ5UEjYNAAYpAnht RcXZcDNNj6YGp843gN9zlSFfxKURMtA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HkW8CO5oKBeC31hC8SqYp1hdcPQ77SEEaemY6tPRsGgxa01LRZjW76UQaQ8XPTSviUdxK7FwwH51X7SRl7KrwiW+1jSyuijAtQSGqHEfQBr2sDJ0kuUo8XGxNRssXlif3T5T9I6mQSAf0msLJIjBhE8l+FrRcBOjj+CnBPDEt9hbVOg/DAEE8z1PQzpcFNyk+CiUIn9suBlAUjYCXbqDf9TQcPp1H5J/y153tfflUbvrpSYX2qewBHUTqWTOL/as+ciPLkcn/MYBULMhWvMd0tOd321FJYKHgjQyQGA4XFtc4cuT2St/QsJPypJ+9Pc3RnAb4GoSWmimYhQNv3WJHQ== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uYENaESUmkouh77B5LvetjBP009/977IJw8aWG3YGvY=; b=axREdmO3jxkeMm8A8+eYDUKBGQ7HlYxXEuFNPsudsjQQYRwbhVZg9X33ZGoO1FWzntDP8rZLfWYlwFQnvV+M7BkPv0FTLid0WX9gU4aSxu0h3TdDJczHm2JwvcEa3Up12JKrZOnlys5FotPQ4SadJAtWzFTkK9ubwbZ5EvX8DWAQlS7AUVKAfsiURIUPKTXyHQ5cwy+jXRV8uKWz1iJsy+l0Myl8hqBDBN8EpkKbUXBIt0eZLWo4fbUvLsVrprl82ghPhoLavLnhtPKScT2UWHY3s1oA3ptsdpBxGLlF7zIhUWqdlLiI83Z/mxSET4LMXDTIrGKSempAHohsYhgrjQ== 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=uYENaESUmkouh77B5LvetjBP009/977IJw8aWG3YGvY=; b=DZuLdAMwy1AlNDFcAukTIVBL2yrLQknf/VrgFP5xxffSlS22dm4r2ydYi27XErd8sffS51YQv6AHLQ0Ri7f+2Zu0OTuqafHOFr62th2AhBnMcnmbAnEy5yswKnkkZPGfZxMoA/mco/nIrQOZ1VknYcbokGBtJI+dxyaE1VYHu4yhkeKEz0N2ssTUF1CvkW3q/FKIbXI9iZ3u+xTiJtkMnj8f8oBPFb0m9Drl/SyABWMiqZvJ5athhjHx1/HCE4iv/C9fdUuJHTyKnFE6S+1v1IEPOEn2aEmwve3pZF138fYKdchB/SQdGD9F/+Mya7BBbKe/xUh0PQizUTUNwqjh9A== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:48:00 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:48:00 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Tejun Heo , Zefan Li , Andrew Morton Subject: [RFC PATCH 14/19] mm: Introduce a cgroup for pinned memory Date: Tue, 24 Jan 2023 16:42:43 +1100 Message-Id: <183372b80aac73e640d9f5ac3c742d505fc6c1f2.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0128.ausprd01.prod.outlook.com (2603:10c6:10:5::20) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: dc182cd2-fdb2-42e0-4f57-08dafdce8d1d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aRdvV5lKgZRqv5YsUcdGL308yIc/tTqhp5JUegyzR1ML8QZUsB4e0FgFYg+JdbcsL84SBh2KbJnFRYZrjaRuI7JLIJpq7ip7cRffhfFurVVYrBsETVVeU3XClXQFV9CFCa34MtFfM73LOT3Atjj7HtDQOoD5AVVHKSv7ogG+duxVfrlFGMvZXn2sEmCN7sftSVX6j2C6lUZaIIJW6/zkcapb9ypcNn+slvL+59VetbpiLPLCLGfK795dJ+NEj81OzkdyksgfQj7c6fZmpwYO9JBDL27kAcNUgJqKYlB1Et/Cfje4re5yi4+TheU6MEI/Gpy32rBQjh/Kyv8FgaRJjEZdNwl+qrydozviYcWhNpD6OtjPQ5Wv9UKeaaoeLLsvhqBsfWl2oH2zOnDBz2GYQtkE38rjR9qaZlDRcuoqLpNehQhxoAhvaHEkF4uwdOdJiJVQjzmC5oC8orqzFARhVkrlGPIMdvy9PkR5oe0NFobarWSmFDy3cTC7PpKEryibzvSf1tr0SwahmcWmPec6eV+rM3XK5BbVFhAk937B9C0cxTXNd8bV3D8N3VpR+Wcjmj//b5+YQC+HEkiHxMN2jesj101nRAyWSdUUNq8BHZE0+sxPsF4yLrN/0PVHRe5i++Bs4crNqKakfQBnazHN8Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(30864003)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CQy+/yePQeHckVtod6yxX+bR9BQnCfoLtXM9V4jO2WmlZfEAr0WHL+X45yZRq93lFAUUsLDnXxh23L8XsM2/oMW4T32CdKVaT3KE23bA5NQIS8p9Tv15fpuEdgZlI7vAsExBOXUMrnkPs/60vKgzywGYoTaqi4k+uH4KuyzuqMwXXqkNQavO4BAulHy8qSEJ9n8VVK3yGc2u7gpq4mcHhEAIJlC8ZWrceb1FxQRd0+eB9Nz7IiZnpHTAeGwrRRO2yPrGP/nyIjGV7XT1xBJR0RJT2Zbg4sD7bJ9XY+/vsyCENcIJewtF53OAQ433rvKOsq+hMpV5Om6yi2ecPv5YT+nMU9h8pGNvQmPXQViLmyz9Wztsouh/DuaP6Q5vVQsK7Pzjiu7zXa+LB/P07ZFOOxLjcj69Ich2NGe0PukY6wiXDvbQDkmrwJtHPwh6jgB6E427/GOqQcM16j8jffoWfwAbFkjUhW4Z3WTEoxlAtcxYWDxNEPFkEQp8y4HO4ySpnAsnkPrJydQXhtxkvUgiU7h4s0pSvjZSWiJGhlyr3tmmhBeD7+V5+KPh9K0PsO9aFv7wCJCVkPuoHk8pkF7YMYO+5KjgPQUL0gm9ma92F+vps9q8vxwMc64YnR4jVCNS3YTgDpmcUJHwmWxBzOKC9ieMG010c2qLhEDulFpifQ/bLgIASy5g9mvzxSoTc+pyKmj+v8xrBR/7hHJU9sBPEC0jazP0TZOsXReLnt0JWhaJxSJnr22GsMfJY5EnzxOrkuROR6GsVcfdpHg70s1IjKG47UdM/FtTGp4lO1BU3+ie69SC6wkG+KiyiPCcEAuLM9Ic/i+6OtSHVDjhWFOl5alv3mwfJD5qHqhBy02akCKEeTdb7cD81ij2FvoeqHGgJZo/hBQ4kf+JOalA1K/9OCecJXusbGDwqpMh3wslhJi+HTVg/3fvzYyXSqAvC/7XjwWyAZMjRv1S3gTnrGczpgF0aw3/W2g9jCDF/bg7gRRh3GfbcCiSsFW9sIotixtxOZlDMF0UyTqGu0zqZb4RW9rlcG6KywI/CduKAQGIuHzKi4z64LdlFnxmfwEZ3DLqi3eKCKY23O/bbvZrw0K1edFcOq/RkdLCnr1THua3NAQ0j12wYmsatXsPojCSEhNMeEXwUQE6AnUm6PARfYGwXqIVEnUbT7xfBsjKr3Up5M8VSmiB9XyxYR24aD+ScgNle+DIk7jqo+OWPSAxdXxvJHtFA5o6PFLs0JmgWo0iDg9OsrSvOPvzfPt07PwNjms4zebcm6p13/myN4i6c9m/PODJRb2rS/R3+Ii2g1rK8IEV0/n7c7jdPhLhO6QiiQ5jCi/huQuGMmWxbpuU4KluHRFYHjp8d3hgS7VW3CfVHvY7KXvWxVGiLB2cIqAPFfD2geW5PFRW/J5GZ/ZYdJv8Sy2+FfO0McFxjOSH9wHkETYxOuqSG0f9qilG3253jSfgM7wgAKfRApkQeaENl8gfT/pxqHaTunI92DXJBfGWf0444UQe8Vlbx+7D+Qlnip+SWP/TcDhnmZQlwPALqc7Lvyxzy+NQCrtWMPmTcNH0xc4f11ddxRqa3y+cKqwcKPz/ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc182cd2-fdb2-42e0-4f57-08dafdce8d1d X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:48:00.2382 (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: hd2QmQP+D9zTYrLzOiaB/8c6g9delxecZXAmg2+5JCEWG4eSPFQW7D+37Q9TUBPNgHSiN66n5apuNt0fUeYUvw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Queue-Id: 5294BA000C X-Rspamd-Server: rspam01 X-Stat-Signature: kbkcbqg65qbyhjujy8ydixtfjdnwx5e4 X-HE-Tag: 1674539282-455169 X-HE-Meta: U2FsdGVkX1+EFuwvlXaQoZpTMYaMJY1P//aP4C+HoNCOnK4+6iJvM0LDgvAPkMYqt0E7sJWMIkes+UAII0r5Te/t17aIVIY3NtZXSdGW6r39qw/H+1LQyXcx/LfexF4SgSYGlQ0rH+xxWBeVj+z3GnCvp06NeM8ntS92mbQE5f+v338/9XT5misVlvDOoxZZSsG+PCIQTU4dYUkWDGT6yjmR+VqM5bqD1Zz7yfJxPqlCglyi2/K36+PHaeZPkomeJ4o6T9yBWlHsqUs/KF1TzH4/U5YaDozTt2hvJ32wryWOzr6sdT7N8bH60Au9vUrVU2AJ33XXr/U6+dU2+Jw2gMNMdTy9wCA1IWPu9cMc1pmH1IQHNOpkHP1dtbzbBUnOkReGWO8+4iWWELQHQ8dCL0RqgQKgpU79t7/+TIj7hZEg4GK6Nq1m7ApnjRCDmDW2rQFgxtcLcDUNXi0hHWl4hmVP/05fuhoq72Q7fcEgVyShuslhRNasmtX093HLpgwdnPkRh4b6CEu+4CT76PWyPfhIUISwBM5DJueff4rebiNvbT+11wGBS2lqmR8OYprT0tH3VJxCWNk3HAiQFA9v9ecaTjM5lWLN+cxheQf+0MfQQqR0s9gpRp+mkcEclECm5iJSPthPjZG1Pc/lglJrIbVyyMtv1yaK9AyBBnCXYUJ2Fy/bpGTOqxI7Ir7552vA/mrSbE2zR1FvKmD/P5HhNx45/t3IfY14KZmiMc0sMjtnOJEQTvpH49nIb8eDAiQrvWmMw5BlBYSnX33aO+PqLqazy0IOA63NQDXsryK0IO4omqCqE7WGoLKaDszZMmZZ5JEGx0WAZ8s/2R7GhOYJOeiagI7yxBPr8vADAhQI4Fzw9j4GSGw1GF4h3VMKn8DGh7z/xy7GrPF+RTmwlG2gQTpbj36KIQq8ZaIfop282c5/y+5vc0JfZwjigKTfkjaePN8cqN7yCvfbt9pkxPR CBA+eDLl PFVt1VxXX2nXtCU71nzTgoiWtnvQblWG5GmIn3X1TRbX9rbz9+tdvfz09jhZ8pYOhmcEz/P/IV75dY1hwWLMxmOfr1NKs+JFh/+SqSNsUOcbXCMZv38XW9AbJGnwjBIRjgh7SUcaMXLhlSva72r8drVmMsULeDMEs0mTHNwYcF2ssIavtBNzK2TwCjpXcVPIVNsaPWgLAlbquAA2O6b0WnWGA+oiOL69+8jiK36itcJeT2CBaXCknvHRcvGHMG8h4XWbmIhje2Hzk+LOB/iUFxlj3Z+ktpON9CHu7vteUrvQftwfjKs+a65/82TpJqlu2lVjtxxv5WxJ1kyTzEqd1uJq2L8GNa5rrFb+Z4cDqeuGQXRLgdfbyUa2BmM8SvC8cboGC6GR/Qg9/3CID38HX8oGrS7xcvHE4+x8Nap26gLJs2mBhsfLcYb6en/iBWj+MwkbueN+KtFLIIhhCcYsz9cMetk/1rNgPqP2S9823V0g67rCgh+KAGiWxgSgK6M7Iu5JJLHfDex/9RPaXq/hrooWFKMvN+1do+319sUviemjbqgGQx5/uRDZQKw== 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: If too much memory in a system is pinned or locked it can lead to problems such as performance degredation or in the worst case out-of-memory errors as such memory cannot be moved or paged out. In order to prevent users without CAP_IPC_LOCK from causing these issues the amount of memory that can be pinned is typically limited by RLIMIT_MEMLOCK. However this is inflexible as limits can't be shared between tasks and the enforcement of these limits is inconsistent between in-kernel users of pinned memory such as mlock() and device drivers which may also pin pages with pin_user_pages(). To allow for a single limit to be set introduce a cgroup controller which can be used to limit the number of pages being pinned by all tasks in the cgroup. Signed-off-by: Alistair Popple Cc: Tejun Heo Cc: Zefan Li Cc: Johannes Weiner Cc: Andrew Morton Cc: linux-kernel@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: linux-mm@kvack.org --- MAINTAINERS | 7 +- include/linux/cgroup.h | 20 +++- include/linux/cgroup_subsys.h | 4 +- mm/Kconfig | 11 +- mm/Makefile | 1 +- mm/pins_cgroup.c | 273 +++++++++++++++++++++++++++++++++++- 6 files changed, 316 insertions(+) create mode 100644 mm/pins_cgroup.c diff --git a/MAINTAINERS b/MAINTAINERS index f781f93..f8526e2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5381,6 +5381,13 @@ F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c +CONTROL GROUP - PINNED AND LOCKED MEMORY +M: Alistair Popple +L: cgroups@vger.kernel.org +L: linux-mm@kvack.org +S: Maintained +F: mm/pins_cgroup.c + CORETEMP HARDWARE MONITORING DRIVER M: Fenghua Yu L: linux-hwmon@vger.kernel.org diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 3410aec..440f299 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -857,4 +857,24 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ +#ifdef CONFIG_CGROUP_PINS + +struct pins_cgroup *get_pins_cg(struct task_struct *task); +void put_pins_cg(struct pins_cgroup *cg); +void pins_uncharge(struct pins_cgroup *pins, int num); +int pins_try_charge(struct pins_cgroup *pins, int num); + +#else /* CONFIG_CGROUP_PINS */ + +static inline struct pins_cgroup *get_pins_cg(struct task_struct *task) +{ + return NULL; +} + +static inline void put_pins_cg(struct pins_cgroup *cg) {} +static inline void pins_uncharge(struct pins_cgroup *pins, int num) {} +static inline int pins_try_charge(struct pins_cgroup *pins, int num) { return 0; } + +#endif /* CONFIG_CGROUP_PINS */ + #endif /* _LINUX_CGROUP_H */ diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h index 4452354..c1b4aab 100644 --- a/include/linux/cgroup_subsys.h +++ b/include/linux/cgroup_subsys.h @@ -65,6 +65,10 @@ SUBSYS(rdma) SUBSYS(misc) #endif +#if IS_ENABLED(CONFIG_CGROUP_PINS) +SUBSYS(pins) +#endif + /* * The following subsystems are not supported on the default hierarchy. */ diff --git a/mm/Kconfig b/mm/Kconfig index ff7b209..7a32b98 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1183,6 +1183,17 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config CGROUP_PINS + bool "Cgroup for pinned and locked memory" + default y + + help + Having too much memory pinned or locked can lead to system + instability due to increased likelihood of encountering + out-of-memory conditions. Select this option to enable a cgroup + which can be used to limit the overall number of pages locked or + pinned by drivers. + source "mm/damon/Kconfig" endmenu diff --git a/mm/Makefile b/mm/Makefile index 8e105e5..81db189 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_CGROUP_PINS) += pins_cgroup.o diff --git a/mm/pins_cgroup.c b/mm/pins_cgroup.c new file mode 100644 index 0000000..cc310d5 --- /dev/null +++ b/mm/pins_cgroup.c @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Controller for cgroups limiting number of pages pinned for FOLL_LONGETERM. + * + * Copyright (C) 2022 Alistair Popple + */ + +#include +#include +#include +#include +#include +#include + +#define PINS_MAX (-1ULL) +#define PINS_MAX_STR "max" + +struct pins_cgroup { + struct cgroup_subsys_state css; + + atomic64_t counter; + atomic64_t limit; + + struct cgroup_file events_file; + atomic64_t events_limit; +}; + +static struct pins_cgroup *css_pins(struct cgroup_subsys_state *css) +{ + return container_of(css, struct pins_cgroup, css); +} + +static struct pins_cgroup *parent_pins(struct pins_cgroup *pins) +{ + return css_pins(pins->css.parent); +} + +struct pins_cgroup *get_pins_cg(struct task_struct *task) +{ + return css_pins(task_get_css(task, pins_cgrp_id)); +} + +void put_pins_cg(struct pins_cgroup *cg) +{ + css_put(&cg->css); +} + +static struct cgroup_subsys_state * +pins_css_alloc(struct cgroup_subsys_state *parent) +{ + struct pins_cgroup *pins; + + pins = kzalloc(sizeof(struct pins_cgroup), GFP_KERNEL); + if (!pins) + return ERR_PTR(-ENOMEM); + + atomic64_set(&pins->counter, 0); + atomic64_set(&pins->limit, PINS_MAX); + atomic64_set(&pins->events_limit, 0); + return &pins->css; +} + +static void pins_css_free(struct cgroup_subsys_state *css) +{ + kfree(css_pins(css)); +} + +/** + * pins_cancel - uncharge the local pin count + * @pins: the pin cgroup state + * @num: the number of pins to cancel + * + * This function will WARN if the pin count goes under 0, because such a case is + * a bug in the pins controller proper. + */ +void pins_cancel(struct pins_cgroup *pins, int num) +{ + /* + * A negative count (or overflow for that matter) is invalid, + * and indicates a bug in the `pins` controller proper. + */ + WARN_ON_ONCE(atomic64_add_negative(-num, &pins->counter)); +} + +/** + * pins_uncharge - hierarchically uncharge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to uncharge + */ +void pins_uncharge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p; + + for (p = pins; parent_pins(p); p = parent_pins(p)) + pins_cancel(p, num); +} + +/** + * pins_charge - hierarchically charge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to charge + * + * This function does *not* follow the pin limit set. It cannot fail and the new + * pin count may exceed the limit. This is only used for reverting failed + * attaches, where there is no other way out than violating the limit. + */ +static void pins_charge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p; + + for (p = pins; parent_pins(p); p = parent_pins(p)) + atomic64_add(num, &p->counter); +} + +/** + * pins_try_charge - hierarchically try to charge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to charge + * + * This function follows the set limit. It will fail if the charge would cause + * the new value to exceed the hierarchical limit. Returns 0 if the charge + * succeeded, otherwise -EAGAIN. + */ +int pins_try_charge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p, *q; + + for (p = pins; parent_pins(p); p = parent_pins(p)) { + uint64_t new = atomic64_add_return(num, &p->counter); + uint64_t limit = atomic64_read(&p->limit); + + if (limit != PINS_MAX && new > limit) + goto revert; + } + + return 0; + +revert: + for (q = pins; q != p; q = parent_pins(q)) + pins_cancel(q, num); + pins_cancel(p, num); + + return -EAGAIN; +} + +static int pins_can_attach(struct cgroup_taskset *tset) +{ + struct cgroup_subsys_state *dst_css; + struct task_struct *task; + + cgroup_taskset_for_each(task, dst_css, tset) { + struct pins_cgroup *pins = css_pins(dst_css); + struct cgroup_subsys_state *old_css; + struct pins_cgroup *old_pins; + + old_css = task_css(task, pins_cgrp_id); + old_pins = css_pins(old_css); + + pins_charge(pins, task->mm->locked_vm); + pins_uncharge(old_pins, task->mm->locked_vm); + } + + return 0; +} + +static void pins_cancel_attach(struct cgroup_taskset *tset) +{ + struct cgroup_subsys_state *dst_css; + struct task_struct *task; + + cgroup_taskset_for_each(task, dst_css, tset) { + struct pins_cgroup *pins = css_pins(dst_css); + struct cgroup_subsys_state *old_css; + struct pins_cgroup *old_pins; + + old_css = task_css(task, pins_cgrp_id); + old_pins = css_pins(old_css); + + pins_charge(old_pins, task->mm->locked_vm); + pins_uncharge(pins, task->mm->locked_vm); + } +} + + +static ssize_t pins_max_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct cgroup_subsys_state *css = of_css(of); + struct pins_cgroup *pins = css_pins(css); + uint64_t limit; + int err; + + buf = strstrip(buf); + if (!strcmp(buf, PINS_MAX_STR)) { + limit = PINS_MAX; + goto set_limit; + } + + err = kstrtoll(buf, 0, &limit); + if (err) + return err; + + if (limit < 0 || limit >= PINS_MAX) + return -EINVAL; + +set_limit: + /* + * Limit updates don't need to be mutex'd, since it isn't + * critical that any racing fork()s follow the new limit. + */ + atomic64_set(&pins->limit, limit); + return nbytes; +} + +static int pins_max_show(struct seq_file *sf, void *v) +{ + struct cgroup_subsys_state *css = seq_css(sf); + struct pins_cgroup *pins = css_pins(css); + uint64_t limit = atomic64_read(&pins->limit); + + if (limit >= PINS_MAX) + seq_printf(sf, "%s\n", PINS_MAX_STR); + else + seq_printf(sf, "%lld\n", limit); + + return 0; +} + +static s64 pins_current_read(struct cgroup_subsys_state *css, + struct cftype *cft) +{ + struct pins_cgroup *pins = css_pins(css); + + return atomic64_read(&pins->counter); +} + +static int pins_events_show(struct seq_file *sf, void *v) +{ + struct pins_cgroup *pins = css_pins(seq_css(sf)); + + seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pins->events_limit)); + return 0; +} + +static struct cftype pins_files[] = { + { + .name = "max", + .write = pins_max_write, + .seq_show = pins_max_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "current", + .read_s64 = pins_current_read, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "events", + .seq_show = pins_events_show, + .file_offset = offsetof(struct pins_cgroup, events_file), + .flags = CFTYPE_NOT_ON_ROOT, + }, + { } /* terminate */ +}; + +struct cgroup_subsys pins_cgrp_subsys = { + .css_alloc = pins_css_alloc, + .css_free = pins_css_free, + .legacy_cftypes = pins_files, + .dfl_cftypes = pins_files, + .can_attach = pins_can_attach, + .cancel_attach = pins_cancel_attach, +}; From patchwork Tue Jan 24 05:42:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113496 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 C7782C25B4E for ; Tue, 24 Jan 2023 05:48:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BE0A6B0072; Tue, 24 Jan 2023 00:48:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 56DF26B0085; Tue, 24 Jan 2023 00:48:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E8296B0089; Tue, 24 Jan 2023 00:48:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2F4436B0072 for ; Tue, 24 Jan 2023 00:48:19 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 09FD3AA9D3 for ; Tue, 24 Jan 2023 05:48:19 +0000 (UTC) X-FDA: 80388612318.28.EFBB7D7 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2084.outbound.protection.outlook.com [40.107.93.84]) by imf10.hostedemail.com (Postfix) with ESMTP id 3A2F7C0005 for ; Tue, 24 Jan 2023 05:48:15 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=noUdmKq7; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf10.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.84 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539296; 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=W/5i1Fiv3xI56uStZR+8ynxVAuNk9x18QJkcNkaDcTA=; b=471/iB6L9hEwxbDeU6M01Lvji91nc1CQebM8UH414BH8YECN1wqZpGtLKyfxzupPi0pExR 2eEnGyctSnjqyUwtMVEiYfvZYqspkvJtnDUUX5gbJpA0zECkuC91kAjVXBrfqWxklYee+1 pS/IviiQsxWVb2U4WWVPjFGqnipg0qA= ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=noUdmKq7; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf10.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.84 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539296; a=rsa-sha256; cv=pass; b=Tbn5cAdQLz+vPw1WKSffDnvc4Jk+6jXL/ftH+ch0yO4fjpbxx6qBq2vPsXrhvTRe5KoDhY Kqds7zyNnaAZfqvhwEk/KGumL27XWbCV3TWhEpXU1Q/1US5xtqCA91DVp/GB7x5SEUpG4k XN5nB1hmYImbUtCayU5Ci0KcDabFyAc= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CU5A36UM2lrVBA/0kP3zI06rITBuQKvdIuH3Q/gEF0WHLjLCnRifZFtboOFDwpmtGu1XHQY/VWo8Zvy7JskpVlCrQC+qUxIVDWvu09TLDvZRa8OFvn7RZvGO6F0lAF3BUru7Buub9Wy0QBMJtJHTXZ4eILPEymmCvpt2qryUiF0uvsrxYDyR0KB37Sa3JflCEsckaL9deHkE8KPKy+ogvhplGqsm2yNes+memCD4QB+NErcV06J1p7Mb0YGqWcKkJo+nfPFJ8S5uGBW+cWtYFh9cPajYmNBAMRYb63jcC0TFymXD3qXJkdd/x12v2dKns2aO/xquNHeRxjKnXdm2fg== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=W/5i1Fiv3xI56uStZR+8ynxVAuNk9x18QJkcNkaDcTA=; b=QMREMs1RpleKmCih1JA0gxtzPGXskts4Aip8+EsNPKh3frUUtlnL9fHBU14JFV8ZMbqbZJjOlEi8VOiCGj6GhAAsVMIxleyfbgJ2lW1y99SoKeyAVGJYz8agpW5hrS8Nw0DHnIobJm0lHNyPzsJiHelm64NQ3TSwG8J1Zk8l+sY9ADPVDx6L+MbV3Sc1kZhP8nagJ9y6ivNPO2O7PCz42e+GYsVt27VQtnJIqPDea7g3XtX99yUWiTAfzuzuciHoQFfH3Ri1A4acgFUvsV5ECpJfKxFEURHWuMkcrMC8SheNmDh4mmPjHNzaof/nN74f+46p7bYs5EV7CCzIsI4szg== 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=W/5i1Fiv3xI56uStZR+8ynxVAuNk9x18QJkcNkaDcTA=; b=noUdmKq7L7A1uoz1VAMekURGEuYiqz9E77tY/yHmYnMRvS6Zsgb+95kGUfa2NxDQNSLoF2NVF/64Df7Bjh4nTyZ9z70M5iKT9BHL0XywyGYEFmjorE5/B1FXeNYe5ARSelzarS6TeMbMKc2OuA45pIaVgiXWlOEKMeMA2+bFChR7MswdHhORfFQg6s22j/0h749Ev2Wct+25OoiQwpVu4tE1TYQxwbtsmTa5a4NHKaXBTBd77UgZ8/29HEJqw141QmiAdHyU5pflfRqHELOyBfrrSQ08q/DU3jHed2kS20B3KvZwif0/yOntBrzupPIbRoM5l9oP6Kn8khp7wlkavA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:48:14 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:48:14 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple Subject: [RFC PATCH 15/19] mm/util: Extend vm_account to charge pages against the pin cgroup Date: Tue, 24 Jan 2023 16:42:44 +1100 Message-Id: <76d61af0219560c03910a189f1ffa340d108858e.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0074.ausprd01.prod.outlook.com (2603:10c6:10:110::7) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: 921cc3e1-df6a-4199-5a56-08dafdce9594 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5U+Jr9jzrcFyJyohas6fcBRUBR9I73TMDKEU/OxxJWuD7BLW92CMnUMWaQgkP1pO381eZWp8FpYNC4IKtiMd3jNH8urTQpiFVYVe8Vuo44alsDM8TFRzsSkHbEe1XID4ULS4PdRe3EfVmo1bRNzGRxkdWousPed4xn6aBnUZmTCOfqQRxqaUs0W9XiQXGFXTT6G5DaqWpwGReRgl87cyVjbwIr/uHmytKSXLs7tv6FtmlL7oFMM811ntU7eXQuKFyz0mospnbcHLeXETsaEiVEbr9r59kfg3l7mEPLcSqoUsTPWC9KhUvtiavfS/n/cEZ5GY4EgygWiPcGnzL7wkBQm6JP6rJTvkuu7n99C9MGjuZZr95vq3es1aAABbrDWIUzbYv0LQPzLxI3mSmbEQOyM2y4MxM3lr2VhkLQdp9md5WwosDSv5VLCRaEYsJQ4/3XS03OEi1d2zH+JJS7QQ4HOkr5H1HXg1k5dEYhROMmP2vwjhJDINMpG09wAIfeB7UsYG2qNUMbpZxLvKOZZGjFt1bKzAX4ZeMXSnBiTjUN7SuniEBNGQj+ZbyIAatLTKETo0TltAx282LpxVIUDDWy3RP0oz+3h2kd7X+ErHDLiH5a8hovZdqRErP5kJKKbw8ZPVEvPfGVLpeUsB4PtTjA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(26005)(6512007)(6506007)(6666004)(107886003)(83380400001)(6486002)(478600001)(2616005)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5SXMR27HWV1PyxQSabaLithcZT6h1UUawtSIZjMAIM3jWe6xlsJ9J9Lhd9sYEj9opYw9IoYPQi5fLmQg0nu6zMSoqJd1Tg2WTz9mrTaHsPFFfhsmNW+wK6x6rOEB/7mliJyqV9YJDReSBujc7AVBe66USJysbGf2m4bnrwj1y8NoYFsU9/L+Gm0JrdvClR2b+geovaKY/ICNifR3M/Ufj3cU08kG9jZ2iGQdSodqoF6sCjbRH7cYNq+9ov8FbEgKifnY8ZqeLuv+V8893i2RswH/ojQQHYA2GZ+ALbHpaFPSqS8+mYGoFjHDfoHRPk6xdUOK7kq13buhgw0iv9ueNM2/oC0Z01nBM0C5M8c+URl5KzfrC7LkCcWRR/KHbcnGqdOBaHRznGQ+MBRu7/YFIW6AEKVMSSwxDbJs7Xb3pULaFcRek+DFHWeii6p6G6gXgoQUiZ85owtZoEyqB66VWEuHbU5cFbM0GCBKx6MzB5+9CR2ZVcfy2Iee6I/fwwUQlg3RfWQ2k6bAUUUrGC0Bw06HNGDAjBQv5ptUTin26QyPiG4uiL0Wp5d3DHc4WIfHyDBPkDl5gBKBrUymDjETJhSj9wNHqtn5s2MBNCGf0Tn6OClbBmizMOxAwuwHeM7ZpRLHSxD8iDv8krAOqZm7txNdfvrReJdVL1UhoFNj61W8YQwfaWqp2GQRc1MtlycteaOB9SOKIBD2V9NJKQYK2mssX8RKgLUZFMmWxnJ/BJgsR3EzxDi0jCb3kJ/f950akaIJ6arXbM1NWQCUMgzOckwMqsod1n5UuACdlJ/bBgsH1qL6EmQLqMyMH4VKA0Bvp6jUQjdDp5GcnpUYT/trHe6f4PWnwPoAkukxjpFLBCwh33yjTfxdSsBH3zvD9V9Ffb0+Ds/nVaSSt0bQC89jD8fI3zEttKoo+RcN7ZwjQyeLLUcMuZSmfR51v+goMJBEzdwAw+UTeWkcNxnwWomrAvnKWmG6/7Mz8gxVFqPJx7+JEfz7hfW0O0QRphaJGorxB/+9R8d97OgiAofHV3mRyDFZ+b8jWI5UNo5m9VtdFB8mYUL14ugdDYYWMzieTxeGqzigk894I5bCKHXur8t2BtJvn6s+9FEnNBMdzYBHlbmQ6rIISK96rclotekefodEzt0zbecxcA5Bc5Mt0WG9VazL3QnA0UR7oEXKQL1rq3mss2HVYWQNFRaq5pjFVq9YAp+ga3gfSytpDH7LF6WI3rw9pXZMCd2EK5h8uI0DW34ZcNkhXb9geWK9xozY8iD2HO8zfwb4AGPbxG3XXN3TOqT/oSykiZh/nDAIOHNZ2H0enszIQEKggYko2gtq/DIL+GAfnarYszbjjuYq3HIO3i2Qy2KW8Xyy3lvKSF6B43DzP0Tli6aZmooRU+gpzWec5GiUKMPvW1BPd3HYlssCPq3l9o+vevIc0zqFvy1QGbFUbuaAzX6MrxWsh0tuM2suHs86zAqtZsy3TMgY315EIgRI5qHYkw/goUmj0aEnfoyjbx97gSwU9IyFITFp7csQgUOwCRG/f9lX7QyMTUPLzVDrOhJB9c08Cs4qG6tf9q5axVzsLV1JJsqJviZzMveL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 921cc3e1-df6a-4199-5a56-08dafdce9594 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:48:14.2683 (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: ociP2uGxEjGDegyYimdwoiteg4BG2tUPDOEZoQkTvDngSvjkWfUbs3CGjoDzboeYOuCZ6BdQtam3fDnPuzITiQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 3A2F7C0005 X-Stat-Signature: z6zqfijjhepx3f8cda1bd4q5uunfdyfd X-HE-Tag: 1674539295-337188 X-HE-Meta: U2FsdGVkX195wnL6JZSklZ1mFi/fyJbjPOJzvACF64CFm2/LoqJFCLyRh0/h1RxfoYcqXLN50vujzuGYVlTibnSQTROKH28WKDEbu1PB/4xQALpNQ6KHjdCU5Ju2rcdVjaHU2TpnGPtZNHxDTXNWEPSSaYyHHMIxFkf0tBDVc81YaTCi42IyR8R0a3FciMrFIz7d5z62EKzjGqG3Um+Cv4duEslPSvlam7Ky7YsC8RaLnmdS6z/Ixeo9oY8RC/Q6t+gQgddvaz6CNem3UMul1zNmiHvlPzRMA5tu4hCe2ns5sV+v+2bupK1Kc1WCgYaqorXbVQ7zJXrfEydB8aQzy3syyQpUqIOPkGgssL82BfbMjl5BbYqpPUU/iVQi4W2T0PMyfUaRijNSmiBDFmHFNzQjZi5tV5lWbcShXUi+1Euq3Cdfn/YxA/+DDIeaebCktBRSG/l5Uub3bsZqgCt+jDnBnrH4juSVPvaO4aGNilk6joqGR1Tgr6bbII0GQHhikPlHGntlPLXnE5CH/jh9JeX9d3Sss361G5qpM3/R2oW+IpA0wFQBOahl3ET/MvyIgKI3Xz2OntLHDb12wl/09mt2FhtcJ8+mt4Mc2XpvQAU+bFKugaHOqfbuPUSHDumjJv6Fah0G9FxB3pnAc/1Rlyjgm87lfH0Sx/WI7RllSxWckC9HMo6nM76BkCrnuxFXaEmpxTGuyjXQObmTQ7T2lAXuUAEjB3I+n2XaMlsQ/PY92ddyaP5dqxZllkjo/53ezCqBN4U43qu/2N+HWWjMqXF6FCawDnESY/V4XMcV738zmBFosXW9j6bpOndmwRglNcpbmJJFtgDvkKticgjo0bobF9WWSuEY3OhAGTkoGVjmz8usj6iart97giZNX5Qd7DP2Cg9vjTGpWq/S0aPmhHCjpsCEJNgznW+HiiLAHQD/sPAe7oJfffaLf+LOugSRDfffzu7EOFZiUEqncri TYKP1++s oiDw4KWCRzCm1PiWW/ekCykF/LD+MkLgS5jX7h3rLiIhEnfQPEYIwZZWzuiLawn9u0/KYujQeDlQXRpyxcn8kvJMkiPQQ1OvZ3FyRzfr9+1w4obwzAKpkeJjYjx5vqvN6KoX2yF/DuvzGZnnqTk1SHU3keqjJWrkvtD7fdgtQa5avv8WHuErdOGt/7d2xCqL0oEiQIrWikJNDn5CNk5yjZgvk0xM86AYsBZgYaaOyvtciSM7OtjAKr3Na7Fsw4c4kbMyoklTcatDBTRU/kn4ijCamNDLsIG7mvxO6PCl7w5V23bnTYJT/eJIRugZZJ7Ju33bPKHJUMhT2uA2Dm8FqEkGuSXsWsOrjRgmKzqLOSMTYtI/DZgAMeY7C2ShxBR+HbQJFN3qdBwhK554hclQfKxwOyQ== 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: The vm_account_pinned() functions currently only account pages against pinned_vm/locked_vm and enforce limits against RLIMIT_MEMLOCK. Extend these to account pages and enforce limits using the pin count cgroup. Accounting of pages will fail if either RLIMIT_MEMLOCK or the cgroup limit is exceeded. Unlike rlimit enforcement which can be bypassed if the user has CAP_IPC_LOCK cgroup limits can not be bypassed. Signed-off-by: Alistair Popple Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org --- include/linux/mm_types.h | 1 + mm/util.c | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 7de2168..4adf8dc 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1116,6 +1116,7 @@ struct vm_account { struct mm_struct *mm; struct user_struct *user; } a; + struct pins_cgroup *pins_cg; enum vm_account_flags flags; }; diff --git a/mm/util.c b/mm/util.c index af40b1e..e5fb01a 100644 --- a/mm/util.c +++ b/mm/util.c @@ -442,6 +442,7 @@ void vm_account_init(struct vm_account *vm_account, struct task_struct *task, vm_account->a.mm = task->mm; } + vm_account->pins_cg = get_pins_cg(task); vm_account->flags = flags; } EXPORT_SYMBOL_GPL(vm_account_init); @@ -459,6 +460,7 @@ void vm_account_release(struct vm_account *vm_account) free_uid(vm_account->a.user); else mmdrop(vm_account->a.mm); + put_pins_cg(vm_account->pins_cg); } EXPORT_SYMBOL_GPL(vm_account_release); @@ -489,6 +491,15 @@ static int vm_account_cmpxchg(struct vm_account *vm_account, } } +static void vm_unaccount_legacy(struct vm_account *vm_account, + unsigned long npages) +{ + if (vm_account->flags & VM_ACCOUNT_USER) + atomic_long_sub(npages, &vm_account->a.user->locked_vm); + else + atomic64_sub(npages, &vm_account->a.mm->pinned_vm); +} + int vm_account_pinned(struct vm_account *vm_account, unsigned long npages) { unsigned long lock_limit = RLIM_INFINITY; @@ -506,16 +517,19 @@ int vm_account_pinned(struct vm_account *vm_account, unsigned long npages) return ret; } + if (pins_try_charge(vm_account->pins_cg, npages)) { + vm_unaccount_legacy(vm_account, npages); + return -ENOMEM; + } + return 0; } EXPORT_SYMBOL_GPL(vm_account_pinned); void vm_unaccount_pinned(struct vm_account *vm_account, unsigned long npages) { - if (vm_account->flags & VM_ACCOUNT_USER) - atomic_long_sub(npages, &vm_account->a.user->locked_vm); - else - atomic64_sub(npages, &vm_account->a.mm->pinned_vm); + vm_unaccount_legacy(vm_account, npages); + pins_uncharge(vm_account->pins_cg, npages); } EXPORT_SYMBOL_GPL(vm_unaccount_pinned); From patchwork Tue Jan 24 05:42:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113497 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 07198C38142 for ; Tue, 24 Jan 2023 05:48:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A2B1B6B008A; Tue, 24 Jan 2023 00:48:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9DA056B0089; Tue, 24 Jan 2023 00:48:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87BC76B008A; Tue, 24 Jan 2023 00:48:31 -0500 (EST) 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 798626B0085 for ; Tue, 24 Jan 2023 00:48:31 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5D73AAAA1D for ; Tue, 24 Jan 2023 05:48:31 +0000 (UTC) X-FDA: 80388612822.04.C4B715B Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2055.outbound.protection.outlook.com [40.107.93.55]) by imf27.hostedemail.com (Postfix) with ESMTP id 9150540009 for ; Tue, 24 Jan 2023 05:48:28 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rrFIO2dS; spf=pass (imf27.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.55 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539308; a=rsa-sha256; cv=pass; b=Z6JxvgH8oD5V9hhdpCn1xiYx7CNNo6QuTsuK8FpwuXelBONmGzmX3BQJZevBQmjowjK7Un Ucf2ukdrPzgCArnC7wiOzoHvgYakm3XF4BRqeGwd4MPkdGq9KtMEJWnKHNAP8QI1q04k2Z PUxyrhYpr/kp6xYalrQYUddlnRaAmQE= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=rrFIO2dS; spf=pass (imf27.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.55 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539308; 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=KKauSACOmjp+bD2erTq7Z7sPoUZPfQj7diDqarD8XaI=; b=te4NJCmrnWOpHox0HBQX7PHF14Ecm6VGxQd/aHp7Rb65fjJ/+ilWw+Wcobj1+I6O7hwd5Z ZccJTnjBPxDYJgiORvdweB8IxCEBCBc8/ajeeVUGkbWgv0QQOspaI2jveqzsssrHhRPSq/ jCSDwu9bxCgyKnLJKffA85rs7c57MZI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Djnvy2h5fFFFHewpifc5gQWX/zWH/4WryCmRgfW+fGx/AIxqCXqo4ka+LlxfngFsT9zWVut1PWvwmShbw1A3CydL2V4sXJVeww4mtorCL2xtWXlNDzKluXe0w8zoDW6AjrlWshKitqsiOFt0+EOyyTg3yEEegANEi3GI+h/KVFXsK4xvzt+CHOSpXovVdOEJoesLsr8GjmUzXjDcSon4oitk0jbx9O8BfdpIQ1e3oMk3pdmns5W1Xi4aBPSD+meneIz4s6/EVQJkL9drVbn/mpB0LMqUAviZ8KgWjK+9T2lFLIxNBLyy18oWXsMcxThTwDxpJV6tdX0ZLtTU9BVPFg== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=KKauSACOmjp+bD2erTq7Z7sPoUZPfQj7diDqarD8XaI=; b=Y/RLQkN34TLLAkbOm4IEEmUYwogokuoJkJ448HsV6axmSseUK6ePM3giUVAnE7ipd/q13BGY975Szw8vvyNWcaWsEaPcbdgmbeWZN7byJ0CD7ro8tat1Ahhc+t1BckAIBKYk5R2J83FHauyLpH4enJxtM+HO04oun9FTm+E/A/tcbtWc4WWx2yQZ/wrekDbdZtY5QXwtkizpU7zQ96XFGpoCed2GjQmXiSVLuKMOhzUmCETf9hWcjh75OaGdtJ6HLh7L3m4GNfjioiFfOmhZtUvUzRQZguB4k3XysXvq5E1HDQKWJGx7nGKEPmH9e+BezBvzOiAQETSM7erTByy7Ag== 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=KKauSACOmjp+bD2erTq7Z7sPoUZPfQj7diDqarD8XaI=; b=rrFIO2dS0S07TXuOCHnBa/xKtMzAh0p9Y/97yh58OAMl8kppEmSbpi+ehr6vt68Cru4vpnrl9UDqpLT/l/B7n/cMWZoLiEQlINqKV1v36BmetIPoK1TMi2aixQAKBeZlfAHbluielXfN/1hAIMbs9dcV7ME7Ixx10FKgk0x1ldVsYj18RJOtvtj+Uk1xQLUfRQawG6X0GV/+dM1FDRgG8yQoedt2IpadnqrskuIbWK0xrM//4ihzcBmrDQwCJD2BZ4cMkeDoP9VTrE+Z73WXW34ComVk+y9GcmOKp87FHhcTOGKQbl/XO+NuSqAJoiu7OVjKIsAONGbrX5BnuU7uXQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:48:26 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:48:26 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple Subject: [RFC PATCH 16/19] mm/util: Refactor account_locked_vm Date: Tue, 24 Jan 2023 16:42:45 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY2PR01CA0006.ausprd01.prod.outlook.com (2603:10c6:1:14::18) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: eb1ed208-4dce-44b8-cc7a-08dafdce9ca5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MUrLMQP6VYi2FshBWZPFDHh5/5FgB1z7nhp2c4Zk/uXkKXeIB29602GWxBFidb4r1xNk9BYI+0WWBhejAXYMsycA050IGUMKPyIL8V2TgCO2Oc07nQ8uMQsUrAVkziW7QA66Tplh3DvHRmp99glIZeFO8CaUgHUBR95BpKoFJQq5Om2ncQNn4cQaULkii2S5MVrvOrMviwaJ9UCD9qS+8tQP5B2KQ/6DV1Jy39/hx/zVRsTWAf8mq2/F8cFYf+X/LPCal2DYTtj0tJMFrO5h2omEPSXMgGoaCu5U6UGdUFCMuaXCYOcSf6lqxyQUTfC4ftz/7x33kvOdI0EleJfPKRqOW+GpbEgEIGcisIR+i/v1LuAoXj08sqS8gZyEy4+2bTny6Qovj8bZpREcveHQMjmwHKU6oN7aWtmy3cjq6VFYcbuTQOUb77XYCIgPNxgeZoljP0vFYpXICpbdYmfqQ8jnay6h+A1wsCDfkdUUozTrzT5QgoNvRPTSEPmYkBIae2dvdhxFZu87dxHnxB56U1+h//f46OHME37+RIqIwmxFtLg0ZYVfpvKybCgOuxD9um/u8LtrnsOO822z+Pgb7ranOqAKrZHH3GmYVwmtGfZWQWpnO+0EvOmlhmY0y7cpTgfgUFy1skHqhVANf5dRFw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(26005)(6512007)(6506007)(6666004)(107886003)(83380400001)(6486002)(478600001)(2616005)(5660300002)(8936002)(41300700001)(2906002)(15650500001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ai++sjTIWs1atPu1MeV9PIHS2Xj47DNdNs+opiD2JENbcFfLRnGaIaBNWalrNYbNW5qlwSod5fDph7bITQducoMS8MIaJPmfQTKDteepo71LzWTkwJzNSyisxIS8+g38+NuCL4swIAUA5qGRcpxs3e0uE78OwA95pXOFNHp8h7RkcItbmVtBbSz1dstUzfzfyU9LPEEE7rk8iwMZm4R1T8YanCOZqs6lX0uMv5bxG5AUoRI3+qe/kXaubVeIbJDzaqU+K8kqrFVC2MOPQPJGVX+2DFPZUTgERl+dCwEEZC2c7NZPCipqnq9CcWc9LnV+4pV8kSMwk+IgRPy7iVXw0YSiGQRAz3ORFW3VbpanouHno1KZSCtIhP3zgtmYCh5PRKrCADLOl3WGLohBJuMI0bDngAut5gv4zy+Nyd1qG7aSLFqAsmTSyNfDyqU2/Q1meKYiadgzXlzocHj5qC4BFqRyszK9RExhowQDeLUQu5/2YKoba0ea5C0WGQ4uhzty9YXYr2rV8/9gPfVCXz7k1p52BV6lwUglvSnSpJAiT9QiXvfdLcF8yVZ0KnF0ZNyh11HcVbp3KaQs4y2skYp1r0k6HWYtSz9m3+uyk+CeB4QP1EeWLkHmPpm+X0xa9iL/6sCx+r8h/wY9eHZ2h31giHWFwMP7RdIk3onJ8nOLZhyvdMtZHT+Nudj9asqoK786sC1zEX9dwU2F7TJJiN8U0N2kBz7EV7YsocZlmvm/820bsWNg0kSVN7CXUo72L7C2h0YQ9XgorGGw+WLpeCjWQlSN3B7DS4349pFaxckNaTk61JYEjWF1e7gQ9DIUqqzy5YZ5A5oYmCyqOkVVE7ireZe7mkXNJYuZf8dGrUAPAJfDw0RU5optYGr8TgFS0XK8bjaHGR4HnpIVE5SNrMLjdAJMmj4gsWWSdzE3SbWiGuZItR9P7/ZaDCwNq6WCH/dcgvV6lroKdOKJvM8vlpgXGTO0tjWIBmTEqPSJcujMgjQ1KEcdQhnqGMqDadw9omUxLpo+b4MUSyiZeWBfz6yOOx+d8uyZO9JS659rwThMxwH9SV56CpSHiGacAe+C/XXdLEiiS6XvhoLE17oIsxSScBidL5zJRNa6+iBLW9AEG1sWv7wG/P7Geob8jp7KgfCD/V3iprV7Wq2UmiDgCaA0xakX11AcvApojzpmZuLh/LDnIaX8Zm3USeGzUcIdlzN0J7YXuQ2n1VTfzy0DDKaejPhID0LhyVei8cgbUS7dE7D6D1zEJH0QLIqp3nhJLukBs/ZFM/Rtb9W5nmOxve7EZOAwYlsjQFEMZCTm9kEp/sHNXjgCp4HJx1NSmizWJVaPcwEsYR0VaNlDKFpjmjA8LIN3q38fawGjQhzyf4Jg8aWe2NBkQW2/tJH4Kpz5nY4rcGg3FrR6tEcZDfGZpfVY0bRQQ8a2iseMPCCxnCWXwJX3f5jnZ4F8n2U+wH+HfuF5NOYfB7to0deIXnkvr1PRQz45x1uN8rePH5Mex1h/us9di+KmFVyQH7TbKVYW0r+ryPVlmPzOLIhWuMjy5VpzsziJa66kxaczNDMBK3iq/5GKes5xkxeXR8feziT4MFGd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: eb1ed208-4dce-44b8-cc7a-08dafdce9ca5 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:48:26.4704 (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: YX/Ay4g83eSbAYhQHqkCH48N0bksO9HCgAgprE2g3sLqJrFhcA94gFmb0QaiG+LL+lswkhmHNq1wwwj3DEtixg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspam-User: X-Rspamd-Queue-Id: 9150540009 X-Rspamd-Server: rspam01 X-Stat-Signature: 1qmwf4rb4ty8hugonbusueei8fttykkj X-HE-Tag: 1674539308-478180 X-HE-Meta: U2FsdGVkX1/53AcJLtjb1FaN/mRgdI1zmLB3TIvuH5jTy/wMLMXCbVJk7FrXtn8UoLSd7O5P/HhRtxP9AkOjO5duOEvsrxYLPwGKJzZrntBEc0JiWPFyDFnyOW3Gk+R2Bca4/BhNn2AyWWBoAQ8+3tQ/GNcdqNVo25oQJrz+HP+WVsCxNf4Hu6aUlqXhx3Jf+uzz7Z/9X2SjxnzD8sDRaXhxM/eYs8Bd8BKzWLBtXkC4TjaknJYwga/lFUKno1tLalnztueEt1mMgRA5NlnxIQcJMXxwGvsnzvQMz8Z7c+dheOZJ4ZN9zKCy6Wj/vQ8iUmK0f6/OQJeQ+PfzjHn/FZvG+qwQ6KXDeusSUjXYEKAogyilMf05zYtdWR+aMgcR5NMNefWjnC5VdsH5smQ3Rnz93N44VB5cbfBPOmNAzQdXAKaetOA77MftwHvq7gvR0RhZUFzJVzi9Zt1nE5HmKRd1juHtKq6uChhM81MWPvtssXJoX+KubVx75/uGLaaPJu7Vgvwg3Ynv9RNJodngVbFlfGTyHIQVINh/ZkVAU5gJgospz6vjiEZuu9iwv2veV8eoIpeomSQeAuiGzRd5M6aL9g1uU5ze1BvmAO/BuUcnxm3BzdBz+jphqzO6/Wd6RObXVb1vKpFFmc82BY63vSrEUmC7eYyqCX3ZAbrNH2W1OvIQzbgzgdabyMDG3ASRvaJ8UhX8xHN9OEqlDiUJhVqSkEAvDxEG2hcwdzNL+kFpnx88bdGWJgdwvi+0V9KLnCP0VCnkCBIJ+u/apnBfPJHa8kusD3HuRxwG1ZzhlTgX9yyKZ5iSysgIPVp+EAjdYh6UE8uvWCoVq/oLfK8CLhQ+VxL1rjUfVS1Gm3mugQ9j/lSw+CHkpKs18ajjU1DIg8MwZLBaKIRx/eXyEfMOeiol6Bt9zNn1mj/RiTx9roPC+AQcIhDPmCfGXaaazHirSuYzXr6FSBJg6EDUxc8 4O4J4CTy wxJpeUFbWFJnD5cy+gjD6yMQvEJ2Llt7O7bsMb+ZuIg3w6OJCKFzM0oUIHUQdKHjNbBBCj0NaiGcvPZgrvKHv0LV4cy0UVVGILKkmWfseA8kaHyYOiYK/Ch+a/vksn+EMiJVfBCKX609X9JsRrEZzUUvk3VrwLU5r2EJ2RuF+h9iKxgzDXn0xJLMO2XLsuPfg7yudFUGcqJ9Ld+JkcF8tGR4D930VWzT02FvmqjZpYO/awyi3csdFIYa5/ndYri/gPoIYMsi1vlSmqJNhTQKIBJWVaBTCY1p2KluWabzvoM8h+DEjImKsQyHlvJ+Tb/ebHAHqYiRQ7/jHHjEn+bhuBdpzwOeyMzOQwpfYd0yWBn9ypfQhVuCKog4/J56yaUIhyIE+atpfozDzu/JQEUffnMcjpuLjGjX01H+MBcwsIrAXYWX+b3fWJGr1Pg== 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: account_locked_vm() takes a flag to indicate if pages are being accounted or unaccounted for. A flag is also provided to bypass rlimits. However unaccounting of pages always succeeds and the flag to ignore the limits is ignored. The flags make calling code harder to understand so refactor the accounting and unaccounting paths into separate functions. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- include/linux/mm.h | 5 +-- mm/util.c | 73 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f85716..126b756 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2090,9 +2090,10 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int account_locked_vm(struct mm_struct *mm, unsigned long pages); +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim); +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages); struct kvec; int get_kernel_pages(const struct kvec *iov, int nr_pages, int write, diff --git a/mm/util.c b/mm/util.c index e5fb01a..78b060d 100644 --- a/mm/util.c +++ b/mm/util.c @@ -537,7 +537,6 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * @task: task used to check RLIMIT_MEMLOCK * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped * @@ -548,7 +547,7 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * * 0 on success * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; @@ -557,33 +556,44 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, mmap_assert_write_locked(mm); locked_vm = mm->locked_vm; - if (inc) { - if (!bypass_rlim) { - limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) - ret = -ENOMEM; - } - if (!ret) - mm->locked_vm = locked_vm + pages; - } else { - WARN_ON_ONCE(pages > locked_vm); - mm->locked_vm = locked_vm - pages; + if (!bypass_rlim) { + limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; + if (locked_vm + pages > limit) + ret = -ENOMEM; } - pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, - (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, - locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), - ret ? " - exceeded" : ""); + if (!ret) + mm->locked_vm = locked_vm + pages; + + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); /** + * __unaccount_locked_vm - unaccount locked pages to an mm's locked_vm + * @mm: mm to account against + * @pages: number of pages to account + * + * Assumes @mm are valid and that mmap_lock is held as writer. + */ +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + unsigned long locked_vm = mm->locked_vm; + + mmap_assert_write_locked(mm); + WARN_ON_ONCE(pages > locked_vm); + mm->locked_vm = locked_vm - pages; +} +EXPORT_SYMBOL_GPL(__unaccount_locked_vm); + +/** * account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against, may be NULL * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). * @@ -591,7 +601,7 @@ EXPORT_SYMBOL_GPL(__account_locked_vm); * * 0 on success, or if mm is NULL * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) +int account_locked_vm(struct mm_struct *mm, unsigned long pages) { int ret; @@ -599,14 +609,35 @@ int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) return 0; mmap_write_lock(mm); - ret = __account_locked_vm(mm, pages, inc, current, - capable(CAP_IPC_LOCK)); + ret = __account_locked_vm(mm, pages, current, capable(CAP_IPC_LOCK)); mmap_write_unlock(mm); return ret; } EXPORT_SYMBOL_GPL(account_locked_vm); +/** + * unaccount_locked_vm - account locked pages to an mm's locked_vm + * @mm: mm to account against, may be NULL + * @pages: number of pages to account + * + * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). + * + * Return: + * * 0 on success, or if mm is NULL + * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. + */ +void unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + if (pages == 0 || !mm) + return; + + mmap_write_lock(mm); + __unaccount_locked_vm(mm, pages); + mmap_write_unlock(mm); +} +EXPORT_SYMBOL_GPL(unaccount_locked_vm); + unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long pgoff) From patchwork Tue Jan 24 05:42:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113536 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 86808C54E94 for ; Tue, 24 Jan 2023 05:48:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F32E06B0085; Tue, 24 Jan 2023 00:48:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EE3646B0089; Tue, 24 Jan 2023 00:48:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5D3A6B008C; Tue, 24 Jan 2023 00:48:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C64126B0085 for ; Tue, 24 Jan 2023 00:48:42 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9F48C804DE for ; Tue, 24 Jan 2023 05:48:42 +0000 (UTC) X-FDA: 80388613284.13.1DD694A Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2063.outbound.protection.outlook.com [40.107.93.63]) by imf29.hostedemail.com (Postfix) with ESMTP id AF1AA120002 for ; Tue, 24 Jan 2023 05:48:39 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=UW35PYbe; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.63 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); 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=1674539319; 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=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=tInmk8KGqIUe07wdsftX0x0v7EFrwi25l1a4Mztl6oryj1eQ17AXeMbkUdSnwUt/pg3kZI y+ZiRc0vSMFHO7tpbjZbcn8/Ne+WZQxd4UFlTzJZbqQNGyzsVdBD/rophmfGppWSYDxVM0 D8NBb2CF1TLindSeKlplk5Vs+wMXAP0= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=UW35PYbe; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.63 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539319; a=rsa-sha256; cv=pass; b=B3YmtwVni6H4geSd+AKFaHS6YdtDLsRZaBfxGZ+foIIOQRCXBa8NNQTZtkwT8WX+CxDNdC MPNbU4Okdh0Kbb8gAm/fYbXiZSlmk9WLtE8GMnrXSPiPyF3xK4yuQuvS75DY8JL1K6pTue 1yNINKu1YoWBy2scTfFLm2Fo+FQ13y4= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BetaLPteS3tYNdlKU2OUj8TD7ig4NtbwowRAQIKL9KHrnbpzXYk7grbilM1QR7+0p876wkrDtw5sLQPLNdLJ7RwaNezLXheu4Ss4mec67RmIP8RV2vIzoI/cxLo2A3HhgLvv0WJeJ4lQdSnu4zEYubW4vHa1ay1iP4W1NVQEl9n3+jipc1QXB8pG8XrWFOujxLsFLSBsxlzvP4lC5k/wNLqSkjpNPOs9DfYvgH0CyueOw/wlAlNLj8RB1DhG2T3QoO8xk+eUWP6BIstWk0XIscZZQn99F+QnInkGqj5MksHa4DJZAMkF/rZEgxMmnzcJsVwMXULDZ0vWxbBoom0/fA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=E8slnc8W2v4K2aPm9ifb7NeASR71HJfuZJiXRi4co2hCzkJ4+cqzaPL4FwDXoqfvKUHK5yHFlkB9+48JqnmZ064StW+c+0rHKzwILK5ZB1PbUauQor+LN2cq0vMY/7wlIYm4hqb1xHAxICngvIKtA5Ay9baIi/PPrdrl5IKMX8ZFI0QmhaV86FBX4tcxSiVP8xSFccI7uz/l/ipeBjoH4JYUQD4IUK0lVEM9Qw63RHMj0Qed3Y0aQ5CK1aGO9BCn5ev5tv20On2sVTrTrdBX9uXGdkaHTlhe9OpjXlpMQsqTSOlPoFFFhK/JDXDQnG17IlgmAFEEd5TgEHyRbPDpaw== 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=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=UW35PYbei4yr1W/izxJ+bccnl3ihFG9XMEGdPYQeGdYj+if1pTtZR/dgHNf2U7matzXnpi5N34uFQC+lHVieFo7EguFy9rVV38TbQI8CP+k5D/b98w+4PtIDfJr3kZybo8rov96obveOxpuBxznA5rYPAA+ja1pmUCz7Dn/UZur36JfEKqOCel5bnIgEASiEF+gJbwpd7XXbHlTfEZ6MG1n2wtb2zs4VgfspAyakvRJp15T5dBFEq+jcNdSn4/d4XU3w+VyKwf2h/u/nBXTzmykLoi4SA/j3a+eGQtXQ24F12rMQV8UOXZY//6T8RXGHjZcvLucP9mFEiADqvFlrsA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:48:37 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:48:37 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple Subject: [RFC PATCH 17/19] mm: Convert mmap and mlock to use account_locked_vm Date: Tue, 24 Jan 2023 16:42:46 +1100 Message-Id: <767bef7ec4e26a2a9655d9c7e8bad8d1255944f5.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY5PR01CA0049.ausprd01.prod.outlook.com (2603:10c6:10:1fc::14) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: ea8af828-bdef-4a2d-a77f-08dafdcea35c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hSRjf7jBnd9I8B5ptmvdWDiTOGNbef9OU6Kx6BkRqfrIlkGE240awnqYIBW9clXsmM5g5RYr5n5KkX9lexpAtdVkSbdHl7vu1ydRckjewjMhKtz1q+sTPZB8rzngPWNO4sKGd6zu9Mz7GFPsNHEy4XyRTBayk7E5pm6WAcFgzibwS2HXm/fk99PCabrpu3thERFzdkyrb09t4sD70P6QUJV6UjjVdF7VRP5XgJayaWh3C/sgx12vvlRRWAB+ej3nf/t53q2+8zyv9k+Ijro+6vQ7VfAe71myy9+j+GGdIrIbsUBhFobtr1oowfU4fshkSvE7masM1lUv9V7JWOOigMYmTRndwJLUVaA5WCZ8v8lSPbSjY3wK88wkfpGp/YfU9vrMTlsM090EvZLNPZhZnvg07LuJ4s5Llw33HjeyjJ7TQbRSnWdc3WiLgdocNKv8FF7dRFqF3OFL0q79+GWsb3iKiptSvHOnUlKL93qHd7lSZV6Klps4w/mrC+DsiGIQj2itPh72iDX4mhESK0Gd+mISh88VkUMxfXu1CJWSAO5uYImqEefX7NqsTGAgKtU0j+tC97P9V4zz0giocAQ9IeNtFsjdfeo5MHpfxZElrCadtJem7Hqrc4uZiMJOJNdNLTJDYvRE5GN4qzBtDp+cRA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(26005)(6512007)(6506007)(6666004)(107886003)(83380400001)(6486002)(478600001)(2616005)(30864003)(5660300002)(8936002)(41300700001)(2906002)(15650500001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QmuGB1xHOVN64IDR/pt28DFA3ux88wFbNTm2sNirHU48rg6ntt8ytamTyxXu1cEj6e+ByGcd1I/I6hiPojeWoPIZ/s5hYJNGMXZHDSUSmOlcACzKcnpq6bZsqMonDhiFmad7UtCnc04auwWlWLQnp1BviocXCGR3gsDgJTXA3IsdElpXYwoUQJRjHgJp/x1Ycg43Xf8Lq030jFnFPqexXMKJcMVGg3RI+VYLD+MubHFhUAF/lLMsPpUX4p5WBGhLUbMJYJTONuu3fGI7eDWUMpbt9W0ya4JGXOKJ5yD0XlTfwEpb0D8uRP6oj34s0Fo3PHWR7d7pyXU6stmEyXM5DfhyXubvK1kOdsnTGH4aXzYMN0FVCswXNaNwYIikHcLDCB9N+I6ojAlj+FYNFFDHq623O7lAwGlnP5sZCkDngLppmvdCIcoHaPU79TKlrMslfVaGxDCaslxhYRaXsf/CDQkV+EproY1BEE161g+Sg+OQbMyd8w6hFFaA67LNeOKj7I+/Rq6Pi7sCv2fcNfc3vDC6iWx1ZxA6oGook183dZVf4US6jqsOasIMTXZtoZjGPtv11WYtn9G1rSpNLeUGH6lR2ELKFbgcNFesVV5EshCWIgmkU2Wmezl42x5XJ0yB4hphuIjd7orCbp2rN6jSEE0elsdZ/3tAvetf0Tx1bV2EX09utYuryj/QLM0EhUQcqkoUm2NOXHf5quK1IRGrQYGivOdqxUMhVOmUfbZsXyia2+HQFMZaaOJE9zSHsrXRwbbAbKJlYAwUWNHZUMkfAhql/WAjU1NYSggrGIbLBMH0vAytNkm4XOlKAzMF53Z4i+4susoHeLlBLIFrU6zSBYDG++HNXvrNGaVCqLl6hDvlWBfj1SOpISWF5bvw1VMTpR2QQ6S1IQ+UUxdbMHJfV65WbxslvVV0S6MbhomOoIjaaqZSaSA0K9aYPPXaHztGIliYnhpyKmWtHpkz6ewL/TeH43WegDvLplnTWwmxPo5UBaCxG3hqnLC3gNoDnyQ2QXdq59L2dyxR/5zhvWdL68DcLABR419s6USXC/JCLyGqPzSUEndx6m6+LlVQ2wDuL9EFvSo3kHe5cjQqeoG1+1H4LRyHqftkAjNiTMR9ixMLAajqoSz6rCfjp5iTifzQvt2r3imz6FsunrE1NqjWYrz+vBXw58QTANLNrdVItJhmsj6ftPoquj4C3DoJD5tvECPpZaJS+LF+ZvBl9hOnxKMEv+W6ABYJ5T5X2fGM05oPJN3OnuKdnci/CeagtGAOFKmaLcLGrXnkPA+mY8Qz5zUI8PLCTqcZNUiq2fWHQgkv61xJ3UcZWkpd+tJlZU1ZriBtnkFroQMyT9bIXX7DMHw3UYOOjb5g2WAeklTYhUjiItO847CFeG5PDaWy9zPbCKo1PB9bCyb2R1nd1bOtAPf60FW88OV38fAMPIq/uFqCnzTeoiblRUJfoNpQQtUMAoideh/8hBwfZfnDOEOsy5+PBIzaYbO5oJ7ZLg3jHKtJlByuCdVUkNjOSWcrdQeHFVMKpjsasfLUSekm/FTe1Y80b3Wz3dx63yfKXywt7ocdcEDeZ1tHmym0eR7xBDYT X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ea8af828-bdef-4a2d-a77f-08dafdcea35c X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:48:37.5788 (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: 082Y3xLRCGrtTWVy9F21QMDrqvAkUTghI+MSOnlartDxlXmoX3rHBXKk2IEhcvXg7bBAaZHpeoPGrcb36Lda9w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Stat-Signature: bwbtk9j7astsioycqsgwaemgmp683kwy X-Rspam-User: X-Rspamd-Queue-Id: AF1AA120002 X-Rspamd-Server: rspam06 X-HE-Tag: 1674539319-818006 X-HE-Meta: U2FsdGVkX19/RgkcGzM+qm9l/u5gpJgs1dUBn26om7FvDFDdNCRCVl/h6+liuvDph2DXnvrA2UFUKxDNZhp8HCBNLDLW0I7S92v6TP8o6kB6bc4mXalEDOyPlAXRM4dT3FHTLvVD3bDUQwofvelZIDfVa/odRmHzGh9SFdDFEfgnKEWTYaF5BuYMvjJmbfGt0r23YQAGlKXatT9k0FJuxv07Nbc1LBxErKDZ+xYUFcxlWHLP6ifO3c12ZLerESKhyR+WlHTzsxgAQFE+npU9mYsyaGYCqPHiajS7l/Kin8QTK5Uv8/iGrWNHKGuCxMqdDfSU80rypQSTTrVsYlTHj2M7ScTfzL4FZcHvCod6aD+haHs7DxY5CGl/Ckz9HYVIrDa1cQlJw3X6uR9/XEezrhYRfEzn+vB/pNSQ02az7HftVEM3aJSVDL2fXuOx623bK/Dw8xV8IFAvlQGGbPXhe2eleBMWi9GHku0NYD3Fljfymd+aQoEV2IbsgaXST+5Im/lccyg+KKgKy1dUrST4Hkkq2ZaBEmALVU3lqttJDVwq/PirmCX7Ji94ZpLi6oRzmVJZYovHBauYnIRmt0woQo4lykBLcJfbSX0EbB89Jx2bFeI3NBHpRzTf7DI9p/WUWFpIZfIRKU+viFu6UwHytC6FrA/lyWGz1GZgLtAQwnsAKfGJWF9yd8YrO6ZUISrFhY7yuGs5+4GtxCocwhdQLL+kng7fRdnkw9UCRiAUxUJ4XOAmd6vFzaPxO67aK5lAir0Zi1CJHe/JIM3dXNZs2ga011PSiz1gccOSVGw82NWJt26Gdn1s0vmQuM/MpNqCNB+IUKYYmBXJ5Kh2Mw/E9JI/6wraj8mQHJoJagzkcYBG+1TUd65N9yGLInx1QsxCJRFkoICxjLFbiXdtYMWCvZZjS+DzDBr90pNWm43YVeAD/uDSUMCF/xIHzQ5g5RvL4RqfpGz6IWYubgjujKJ KNkwFWeh d2M37FcaaJRBlnkNEGuUVbR1rcreG2Afsg4h9vYE8Lw87UQUkglOfV+gfWVZ7apb2KXHjFtRmm1haauN6XlAt2cKjIuve87s8L3/TS+hZpYo7FUyP7khu9HZw/e4+Il7nnPjeWJv82Ar23OBu9hxSKf5DSsbf9stIEBK22nLXq88BLckHEuiWxlZJXU+PToMEGsI9NMI6EgutdllQmUt7zAjyAiNsmwZiL9Ppxu3VbBusNbnzsEmS51a1UTglD2z3gc6PmRpkhUpHYzfnzIgQ1XMNqGX2iA54K16Q94WcvgNBP1v2KX7MHAT2njTIesGdpxRPqoIu56caSOsFUntvPB6xNorpviwLI4bn5QqYcyCoETICn6jhfeINF+JDZtyurq5T65K0prrbdAKKJIZ7zjCRfutIcNQoO9kt1cXquKjd1+SrYao9yrZ7Pw== 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: A future change introduces a cgroup to control the amount of locked/pinned memory on the system. To ensure memory pinned via mlock and mmap is accounted for use the common account_locked_vm() function. As cgroups can outlive individual processes also unaccount for the locked memory during process teardown. This patch should introduce no user visible change. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/internal.h | 2 +- mm/mlock.c | 76 ++++++++++----------------------------------------- mm/mmap.c | 76 +++++++++++++++++++++++++-------------------------- mm/mremap.c | 54 ++++++++++++++++++++++++++---------- mm/secretmem.c | 6 +--- 5 files changed, 95 insertions(+), 119 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index bcf75a8..7c8c3f2 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -489,8 +489,6 @@ extern long populate_vma_page_range(struct vm_area_struct *vma, extern long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long end, bool write, int *locked); -extern int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len); /* * mlock_vma_page() and munlock_vma_page(): * should be called with vma's mmap_lock held for read or write, diff --git a/mm/mlock.c b/mm/mlock.c index 7032f6d..a97c8c5 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -416,6 +416,20 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out; + /* + * Keep track of amount of locked VM. + */ + nr_pages = (end - start) >> PAGE_SHIFT; + if (!(newflags & VM_LOCKED)) { + __unaccount_locked_vm(mm, nr_pages); + } else if (!(oldflags & VM_LOCKED)) { + if (__account_locked_vm(mm, nr_pages, current, + capable(CAP_IPC_LOCK))) { + ret = -ENOMEM; + goto out; + } + } + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), @@ -439,16 +453,6 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, success: /* - * Keep track of amount of locked VM. - */ - nr_pages = (end - start) >> PAGE_SHIFT; - if (!(newflags & VM_LOCKED)) - nr_pages = -nr_pages; - else if (oldflags & VM_LOCKED) - nr_pages = 0; - mm->locked_vm += nr_pages; - - /* * vm_flags is protected by the mmap_lock held in write mode. * It's okay if try_to_unmap_one unmaps a page just after we * set VM_LOCKED, populate_vma_page_range will bring it back. @@ -517,42 +521,6 @@ static int apply_vma_lock_flags(unsigned long start, size_t len, } /* - * Go through vma areas and sum size of mlocked - * vma pages, as return value. - * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT) - * is also counted. - * Return value: previously mlocked page counts - */ -static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm, - unsigned long start, size_t len) -{ - struct vm_area_struct *vma; - unsigned long count = 0; - unsigned long end; - VMA_ITERATOR(vmi, mm, start); - - /* Don't overflow past ULONG_MAX */ - if (unlikely(ULONG_MAX - len < start)) - end = ULONG_MAX; - else - end = start + len; - - for_each_vma_range(vmi, vma, end) { - if (vma->vm_flags & VM_LOCKED) { - if (start > vma->vm_start) - count -= (start - vma->vm_start); - if (end < vma->vm_end) { - count += end - vma->vm_start; - break; - } - count += vma->vm_end - vma->vm_start; - } - } - - return count >> PAGE_SHIFT; -} - -/* * convert get_user_pages() return value to posix mlock() error */ static int __mlock_posix_error_return(long retval) @@ -585,21 +553,7 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla if (mmap_write_lock_killable(current->mm)) return -EINTR; - locked += current->mm->locked_vm; - if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) { - /* - * It is possible that the regions requested intersect with - * previously mlocked areas, that part area in "mm->locked_vm" - * should not be counted to new mlock increment count. So check - * and adjust locked count if necessary. - */ - locked -= count_mm_mlocked_page_nr(current->mm, - start, len); - } - - /* check against resource limits */ - if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) - error = apply_vma_lock_flags(start, len, flags); + error = apply_vma_lock_flags(start, len, flags); mmap_write_unlock(current->mm); if (error) diff --git a/mm/mmap.c b/mm/mmap.c index 425a934..2c05582 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -160,7 +160,7 @@ static int check_brk_limits(unsigned long addr, unsigned long len) if (IS_ERR_VALUE(mapped_addr)) return mapped_addr; - return mlock_future_check(current->mm, current->mm->def_flags, len); + return 0; } static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, unsigned long newbrk, unsigned long oldbrk, @@ -1184,23 +1184,6 @@ static inline unsigned long round_hint_to_min(unsigned long hint) return hint; } -int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len) -{ - unsigned long locked, lock_limit; - - /* mlock MCL_FUTURE? */ - if (flags & VM_LOCKED) { - locked = len >> PAGE_SHIFT; - locked += mm->locked_vm; - lock_limit = rlimit(RLIMIT_MEMLOCK); - lock_limit >>= PAGE_SHIFT; - if (locked > lock_limit && !capable(CAP_IPC_LOCK)) - return -EAGAIN; - } - return 0; -} - static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) { if (S_ISREG(inode->i_mode)) @@ -1310,9 +1293,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!can_do_mlock()) return -EPERM; - if (mlock_future_check(mm, vm_flags, len)) - return -EAGAIN; - if (file) { struct inode *inode = file_inode(file); unsigned long flags_mask; @@ -1882,22 +1862,27 @@ static int acct_stack_growth(struct vm_area_struct *vma, if (size > rlimit(RLIMIT_STACK)) return -ENOMEM; - /* mlock limit tests */ - if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) - return -ENOMEM; - /* Check to ensure the stack will not grow into a hugetlb-only region */ new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : vma->vm_end - size; if (is_hugepage_only_range(vma->vm_mm, new_start, size)) return -EFAULT; + /* mlock limit tests */ + if (vma->vm_flags & VM_LOCKED) + if (__account_locked_vm(mm, grow << PAGE_SHIFT, current, + capable(CAP_IPC_LOCK))) + return -ENOMEM; + /* * Overcommit.. This must be the final test, as it will * update security statistics. */ - if (security_vm_enough_memory_mm(mm, grow)) + if (security_vm_enough_memory_mm(mm, grow)) { + if (vma->vm_flags & VM_LOCKED) + __unaccount_locked_vm(mm, grow << PAGE_SHIFT); return -ENOMEM; + } return 0; } @@ -1975,8 +1960,6 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) * to guard against concurrent vma expansions. */ spin_lock(&mm->page_table_lock); - if (vma->vm_flags & VM_LOCKED) - mm->locked_vm += grow; vm_stat_account(mm, vma->vm_flags, grow); anon_vma_interval_tree_pre_update_vma(vma); vma->vm_end = address; @@ -2056,8 +2039,6 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) * to guard against concurrent vma expansions. */ spin_lock(&mm->page_table_lock); - if (vma->vm_flags & VM_LOCKED) - mm->locked_vm += grow; vm_stat_account(mm, vma->vm_flags, grow); anon_vma_interval_tree_pre_update_vma(vma); vma->vm_start = address; @@ -2281,7 +2262,7 @@ static inline int munmap_sidetree(struct vm_area_struct *vma, return -ENOMEM; if (vma->vm_flags & VM_LOCKED) - vma->vm_mm->locked_vm -= vma_pages(vma); + __unaccount_locked_vm(vma->vm_mm, vma_pages(vma)); return 0; } @@ -2525,6 +2506,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, struct vm_area_struct *next, *prev, *merge; pgoff_t pglen = len >> PAGE_SHIFT; unsigned long charged = 0; + unsigned long locked = 0; unsigned long end = addr + len; unsigned long merge_start = addr, merge_end = end; pgoff_t vm_pgoff; @@ -2560,6 +2542,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_flags |= VM_ACCOUNT; } + if (vm_flags & VM_LOCKED) { + locked = len >> PAGE_SHIFT; + if (__account_locked_vm(mm, locked, current, + capable(CAP_IPC_LOCK))) { + error = -ENOMEM; + goto unacct_error; + } + } + next = mas_next(&mas, ULONG_MAX); prev = mas_prev(&mas, 0); if (vm_flags & VM_SPECIAL) @@ -2605,7 +2596,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma = vm_area_alloc(mm); if (!vma) { error = -ENOMEM; - goto unacct_error; + goto unlock_error; } vma->vm_start = addr; @@ -2725,8 +2716,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) vma->vm_flags &= VM_LOCKED_CLEAR_MASK; - else - mm->locked_vm += (len >> PAGE_SHIFT); } if (file) @@ -2759,6 +2748,9 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); +unlock_error: + if (locked) + __unaccount_locked_vm(mm, locked); unacct_error: if (charged) vm_unacct_memory(charged); @@ -2942,8 +2934,13 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; + if (flags & VM_LOCKED) + if (__account_locked_vm(mm, len >> PAGE_SHIFT, current, + capable(CAP_IPC_LOCK))) + return -ENOMEM; + if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) - return -ENOMEM; + goto unacct_locked; /* * Expand the existing vma if possible; Note that singular lists do not @@ -2993,8 +2990,6 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, perf_event_mmap(vma); mm->total_vm += len >> PAGE_SHIFT; mm->data_vm += len >> PAGE_SHIFT; - if (flags & VM_LOCKED) - mm->locked_vm += (len >> PAGE_SHIFT); vma->vm_flags |= VM_SOFTDIRTY; validate_mm(mm); return 0; @@ -3003,6 +2998,8 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, vm_area_free(vma); unacct_fail: vm_unacct_memory(len >> PAGE_SHIFT); +unacct_locked: + __unaccount_locked_vm(mm, len >> PAGE_SHIFT); return -ENOMEM; } @@ -3064,7 +3061,7 @@ void exit_mmap(struct mm_struct *mm) { struct mmu_gather tlb; struct vm_area_struct *vma; - unsigned long nr_accounted = 0; + unsigned long nr_accounted = 0, nr_locked = 0; MA_STATE(mas, &mm->mm_mt, 0, 0); int count = 0; @@ -3107,6 +3104,8 @@ void exit_mmap(struct mm_struct *mm) do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted += vma_pages(vma); + if (vma->vm_flags & VM_LOCKED) + nr_locked += vma_pages(vma); remove_vma(vma); count++; cond_resched(); @@ -3116,6 +3115,7 @@ void exit_mmap(struct mm_struct *mm) trace_exit_mmap(mm); __mt_destroy(&mm->mm_mt); + __unaccount_locked_vm(mm, nr_locked); mmap_write_unlock(mm); vm_unacct_memory(nr_accounted); } diff --git a/mm/mremap.c b/mm/mremap.c index fe587c5..67cc4f3 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -574,7 +574,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, bool *locked, unsigned long flags, struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap) { - long to_account = new_len - old_len; + long to_account = (new_len - old_len) >> PAGE_SHIFT; struct mm_struct *mm = vma->vm_mm; struct vm_area_struct *new_vma; unsigned long vm_flags = vma->vm_flags; @@ -594,7 +594,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, return -ENOMEM; if (unlikely(flags & MREMAP_DONTUNMAP)) - to_account = new_len; + to_account = new_len >> PAGE_SHIFT; if (vma->vm_ops && vma->vm_ops->may_split) { if (vma->vm_start != old_addr) @@ -618,16 +618,36 @@ static unsigned long move_vma(struct vm_area_struct *vma, return err; if (vm_flags & VM_ACCOUNT) { - if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT)) + if (security_vm_enough_memory_mm(mm, to_account)) return -ENOMEM; } + /* + * MREMAP_DONTUNMAP clears VM_LOCKED on the old vma and + * implies new_len == old_len so no need to account locked + * pages. + */ + if ((vm_flags & VM_LOCKED) && likely(!(flags & MREMAP_DONTUNMAP))) { + if (__account_locked_vm(mm, to_account, current, + capable(CAP_IPC_LOCK))) { + if (vm_flags & VM_ACCOUNT) + vm_unacct_memory(to_account); + return -ENOMEM; + } + *locked = true; + } + new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, &need_rmap_locks); if (!new_vma) { if (vm_flags & VM_ACCOUNT) - vm_unacct_memory(to_account >> PAGE_SHIFT); + vm_unacct_memory(to_account); + if ((vm_flags & VM_LOCKED) && + likely(!(flags & MREMAP_DONTUNMAP))) { + __unaccount_locked_vm(mm, to_account); + *locked = false; + } return -ENOMEM; } @@ -696,10 +716,11 @@ static unsigned long move_vma(struct vm_area_struct *vma, vma->vm_end == (old_addr + old_len)) unlink_anon_vmas(vma); - /* Because we won't unmap we don't need to touch locked_vm */ return new_addr; } + /* Make sure do_munmap() doesn't unaccount locked pages */ + vma->vm_flags &= VM_LOCKED_CLEAR_MASK; if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) @@ -707,15 +728,11 @@ static unsigned long move_vma(struct vm_area_struct *vma, excess = 0; } - if (vm_flags & VM_LOCKED) { - mm->locked_vm += new_len >> PAGE_SHIFT; - *locked = true; - } - mm->hiwater_vm = hiwater_vm; /* Restore VM_ACCOUNT if one or two pieces of vma left */ if (excess) { + vma->vm_flags = vm_flags; vma->vm_flags |= VM_ACCOUNT; if (split) find_vma(mm, vma->vm_end)->vm_flags |= VM_ACCOUNT; @@ -768,9 +785,6 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) return ERR_PTR(-EFAULT); - if (mlock_future_check(mm, vma->vm_flags, new_len - old_len)) - return ERR_PTR(-EAGAIN); - if (!may_expand_vm(mm, vma->vm_flags, (new_len - old_len) >> PAGE_SHIFT)) return ERR_PTR(-ENOMEM); @@ -1026,6 +1040,16 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, } } + if (vma->vm_flags & VM_LOCKED) { + if (__account_locked_vm(mm, pages, current, + capable(CAP_IPC_LOCK))) { + if (vma->vm_flags & VM_ACCOUNT) + vm_unacct_memory(pages); + ret = -ENOMEM; + goto out; + } + } + /* * Function vma_merge() is called on the extension we are adding to * the already existing vma, vma_merge() will merge this extension with @@ -1038,14 +1062,16 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, extension_pgoff, vma_policy(vma), vma->vm_userfaultfd_ctx, anon_vma_name(vma)); if (!vma) { + // TODO: We always unacct memory + // regardless of VM_ACCOUNT flags? vm_unacct_memory(pages); + __unaccount_locked_vm(mm, pages); ret = -ENOMEM; goto out; } vm_stat_account(mm, vma->vm_flags, pages); if (vma->vm_flags & VM_LOCKED) { - mm->locked_vm += pages; locked = true; new_addr = addr; } diff --git a/mm/secretmem.c b/mm/secretmem.c index 04c3ac9..4515eb4 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -120,13 +120,11 @@ static int secretmem_release(struct inode *inode, struct file *file) static int secretmem_mmap(struct file *file, struct vm_area_struct *vma) { - unsigned long len = vma->vm_end - vma->vm_start; - if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) return -EINVAL; - if (mlock_future_check(vma->vm_mm, vma->vm_flags | VM_LOCKED, len)) - return -EAGAIN; + if (account_locked_vm(vma->vm_mm, vma->vm_end - vma->vm_start)) + return -ENOMEM; vma->vm_flags |= VM_LOCKED | VM_DONTDUMP; vma->vm_ops = &secretmem_vm_ops; From patchwork Tue Jan 24 05:42:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113537 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 4043CC38142 for ; Tue, 24 Jan 2023 05:48:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC5F26B0089; Tue, 24 Jan 2023 00:48:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A4F276B008C; Tue, 24 Jan 2023 00:48:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C9E76B0092; Tue, 24 Jan 2023 00:48:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 72F1F6B0089 for ; Tue, 24 Jan 2023 00:48:55 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 578EE1A01D3 for ; Tue, 24 Jan 2023 05:48:55 +0000 (UTC) X-FDA: 80388613830.17.BC8B66C Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2051.outbound.protection.outlook.com [40.107.93.51]) by imf12.hostedemail.com (Postfix) with ESMTP id 879264000C for ; Tue, 24 Jan 2023 05:48:51 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=WoL9BiR2; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.51 as permitted sender) smtp.mailfrom=apopple@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=1674539331; 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=+cwo0tfKFqlSjGojYIjz88S7pf4ONV6bklIA6oByAF4=; b=KDED2dQAegRnDNGEv/ADlGuLrUkCPgMOcO2g+6bOERtp+mmHa8xttVwlULYoid1Eb4zoIE Yj0KcEOSiyn5ZfGH+cLANPxUymZJ4iVVuCyuSGMp93L6vujTzdXP+P5Qvd1A+KlOu2O2vh fbwZqIM/5wKLPAAz1SvZGfjEIDKkLYk= ARC-Authentication-Results: i=2; imf12.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=WoL9BiR2; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf12.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.51 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539331; a=rsa-sha256; cv=pass; b=cZq+Ai+bJWo+8WeUa8+Wj0hcqojQm/UthUiDEwYHGLHSQs33nvodvk26JkVDN/9d7nzGL+ XKQYfPo+XL/jfv/wC0Yv4SW1F7sfiivK/hEE1Flsh35Oif6geo3UnHzTqT6aApNoR+/PPI T/FVceNQDh49pGmENkXHY/miJ8hY/As= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g08ONDz8RF9X3Ehc4Y8ZmLl5NTpcsTw1TM/qBe4O2dcMfbXBzRMP+urpb69k9AME17Y+tW7+orDNWQLDqMCE5AjWF1hYiuVPrGBdWA8eC0mRPzZzVnsV8LQC5/aJwYADVqa7Op/f/VuN69I+oXcXXxfq/yLtRDkjNAUHcf3kZJ7CewzatflfRogjW58Jis7jtV6q9Gev2zfd/cM3OBZX3ugy3KcyEw3HTkzXUiuZHdGdlu0UEl4fEuHrYrY7P63XGDigBTSJxQERG2b1k/OBJPA1lwPLctXlvrgwGIE/ukMCz5KAXxBAdpnRC+2UQk4nZ0V6+Y0kvIBwFjdvHOxaug== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=+cwo0tfKFqlSjGojYIjz88S7pf4ONV6bklIA6oByAF4=; b=lsB35nKfttiyjjrkW6zSuBBAWPHzIX+qclbw6bgthp4n3j57c/QaBOo/UrObW3d89FGwSRNXy8NjC397VAmwQeICkHomFx52YZCxgmfKmg/akd8F2lJSDvCU29i6tXVhNvHJrr9t7bZ3MPfICRebhuMDNIL9jJNpSVIVVU1dqn0y2OcYzCXSlq85k5pWXHLvivEsKzz/O7bgqVzRcRylFuH7GIzfhc7AHSVU7gOkGYlUPYG2BX1qiSqeEwHRacFQ0AamUUt7OSu34A0ZCHK3PN4pmav+VBrDCmLVLLrX6+Ay8yy8jKyxDDURMJ0OEcwgL9PJuESylaBg4BXkaCx9Xg== 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=+cwo0tfKFqlSjGojYIjz88S7pf4ONV6bklIA6oByAF4=; b=WoL9BiR2yLNy912yNr/5ywnnEnSpU2Z8QQ09z74/o96XY8GFpmvaleX/P6bu79Lz7wVe/onJp9mjlz/S51BLQhrttZ1wSTdMoUU6m6H8QsDbEEmCNKbbCdLoi6a6LGNgOX66QCRaZhXBDHv5oVPZZy2sp1b29um1b2kLvETMNLiYZf8GxEi8kfMFsbJCa4d18m0NTrEije59aOH3TSLCQtL1RnrOFau0EYJ2QlqS8gxnCLmlW0Mwlm1v9Zupyi1+0lmNi/4Q/eBjWZs3hhuBZfG5caU6vYTDTIS4e8pAY3yn3k3U6ij+9hT7K3ggAp2Pnr5L33ahHmBgdA2sFQqmVQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:48:49 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:48:49 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple Subject: [RFC PATCH 18/19] mm/mmap: Charge locked memory to pins cgroup Date: Tue, 24 Jan 2023 16:42:47 +1100 Message-Id: <3883f8fefba4fa039c8bb5ea1882aaf4585dbc07.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0086.ausprd01.prod.outlook.com (2603:10c6:10:110::19) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: df3d31c9-199d-47a4-8ac5-08dafdceaac1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sVqAUNH3AQV7YF8agzDLryko40gLhCW9ldfty/HINCSttBJYg30cPg6g1cKMeJf9jQgavmvD7iUmOOVAFjsPg4bADZzGQmEf8SeBy+DiRi1+A7t2Vt/4aM5qSUSkEMAaD8AHxP9DzBfWa8goZZSO5NdNPvb1WigibWDSo7BW9hXbeHuOCFKcsaJ3oe64Lo5EuDP/TlFlAZG3H6ZQNID4I7VEjHxjfcCZLtUPwLj4X8CpuUXvomL09AghoV8s90bnOAAzzjFLiQos913e65Sb4gWxeypcbLclkfjviuewpisM8Tb1fdc3YgAApy+VjdvhL+W8mqjVHZkTWr7K+Wyfh2INzPkUSyichiYviFMqhI5c37jy4gYdPGihcos0WiRMRAp/3qm9Awwx1NqBa4meifcsvRgIvdtRpnKzGxWFTNQ1D25LlK2dkVVY3NOgozXCMOL6JUxvRiUbsW5oVWExvWAjHzd8baqnKHoZf+nJCiSgn9Jf2ORKw8R2466IYUZtrgbGLUc3sVKDuQwYYwm8blRN7IWWQ3GlZJATcj3EYoZABOUqolE44rjRuFb3z5809cPQbEFSevdi7n8TAMaHyIDH/FwlF6TUGnfm0tFI6tNwXJTOK97CuWLcSiq4N1P/GLsAHbSBvM+3JjPQypRQUg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(26005)(6512007)(6506007)(107886003)(83380400001)(6486002)(478600001)(2616005)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2xoZ+UxT1JWl6ziSaM2I9RqoUmuQbusQjqYIZo2umK8rHZCZnYP8sNTObxND5/PF2zW6gut2lXEpua5w4E9DItYZvXoK8iiar+382IPUBOLEA/0DifwpllrkkF/6aXN+WeyJjOoSj5U/j7sQf3BspFwFNzWYoci0CMsiMDGzfMe+hbjDDu9M3mdM09KY8l6kFqhzhC7K6iCohR/MXkC/x0nAXTAaDCQF6dROB2Isc8WrWj8A66Ul+0TF5409COjv7CskMIIbpkTSMb686MrMTTfOc+yqSi+VQi0Gftmv0Quhz54gK89BvEOE2INpYoXmk7WSb9F93f4soZyqIgw1dYYzPmaN1hRYrl4e8AJXeryzVGpvl4FhR0DIL4rBjhM40zMk2kQ/VrLGZBbgCS54D8Slym1d34bmQHuEhvpvNwfI7TKKKqOs5j1DHOIh8NA6c409Q5ibS/lYsj0aHKVixnwZjcVKbBezfWQpvqkqqWr0Um8zohwZtf4VFmm4gEfCjYTFmPoTQslzD4+8/SBXgEDegnsU3bGoiAHq2fcT2FSavrY2n/ZA/NvcEVfpETZ/YHRWLpLEjP/wcuDruNrM1mgn8fDk8tCLgsqj5jf5JeqGzud9U+uXgK6PbEE0vMs/8mXzO7qMIeSQOAgPe0jFgW5gzh1hRcQNN94bSNpOeqw/aU/SkPl+5Dn5DNu4jE6gbomvemwv7P2BAmayowX1EtXna4R/j0CrmIDzDMA09XQV/a4HXj7iM8oKHmunlpeWnZpZCobeWxNE3vLy6pcVG6ea8xNCEbEN5nUHl5vdmDgSDVNah8rE+bLM+cgDkbLFOcCBut9OyhjfgWQKltR7KqGDDrWKnzN97b1Er033SjvE7MtS28npx5u3w2LNSyJCdl3Zcyb/OHbZQBK3rKZ7sWorx3uH5JyN/Ar77GuVI/lLeMGtz0er7bkXiesVRaM2y7I+vvcW7Fy4V67Zc2tPNjrkUAa/CY8sRDdcdmJBpqbNYMmsj1yw0Ra2Su9Op/DVD1OTCO8e+buRr0c5zer1LJ/nocozG34zFSfpDOCDbC62lQpuJkjuWC8WEXSpsFBi/Dvi0jURyTc/G8iQePOJb3nzsEAXK+IgqhloR8akzgNsqdpL4Nq+09BtBD023OrmKZzS2Oo0fNdUi+BlrhL4Hw8lzi0nRZcDDEcXRQbI3+hMmTnaOvYaqJYAzSy1MOc1y4RBZ5+Oz9L9gg4xBOgZ4qAAloyCYuGO3AHcIbuAVQHWiD4GSH56mM8PbtUp9xvL1w+frFDWvfqC/udZkld4J9gRAGRhFIo+ooZBJDaFSuGPzmXQ6La41lkOl8uZIAYW5/p54C0mVYuyU+zOepxdIEBeq1kxaWxULkSh/J1/i6KW/5yLiuH4FI3rJFQmOTnHNG93cD0j/EgdmcsaJdTqJNz8SGbKzsQzYGVOMO9F1XPW34qwb7LclCAHHe1fNKM3zcQcGhJf0qvKjNHhoX5YlSxzFJ1RnlIbqxYCKWWrb3FZkV8HgDnrC33j77I5EkxYbRn1++X2YTV6DTC+Nmw1+FAETdBu0Knh+MZ/qFOtV3BKwufqp17BkAZvCic71s1C X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: df3d31c9-199d-47a4-8ac5-08dafdceaac1 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:48:49.7966 (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: uIQPRqr7jcxV645b/6A2tvrxcxKneDs/uGpW3WazBaTYZOTgAc6dwKVOfj2nSafkEBSEsG7MOoXFdAM8kwdMzw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 879264000C X-Stat-Signature: e43jz91pnskgecpu7fkfmnsif3oio94m X-Rspam-User: X-HE-Tag: 1674539331-833007 X-HE-Meta: U2FsdGVkX18I8ZwQ6Joe24oeAXGxIP671GZJKHjxD+ZoX9pt8SU5PD/uWdR64yzOG/9JzH1RP4yImGDrPK6kMNoWAwOr7QaRvBj0ScYAOmiOJFbly2Vn3HYFTGNEmphl2KGSRtKnrBDAx5awn2lztALhTlrLVqBef+2FhlL6Miwc2p5qMaly315EcTyIkQsBJdtaFo3u5eEEqrLNLaFI3j9lSjw+7fPpwxe6JejPNMQEWFiRcp4iTpj8o9Bw+Ilm2HLOfeaDvAWSQPjK5UKCkm3aW9NqjE6lJlZrvj4VT76ltOTkGpPeo4f+svgZ4hxQKMN1d+xPYix3PzeeDoJ8r+x6Y14sBEdOpke9o22JYA1MsWIC1Uzs2yDOEgBKUW1G4x01wvW8tzjjOhi0UrkvJCn9JGG20iDgkj7+ypZj8nc5ug3aRQxIG7zut0nqpz8uXWeQ5GQR5bgl6XEuZcUgELfpJmSINQbB7WRiFnHYsPiJweyjnuZywsHteRggFgOgCySKpZpCfHLP1v7UxQn3DeMmyJ04Wgy22hRL2MD4S4+JtLaVJQnKohvu4rtCrmeNEDLd0yvNmNctyyc969Wy9g3N/I2H8ZvE5RG7I1mJ2zKbdr5hF4OZmSNnGxZuKBNbKK7/mHC7fGNSsYuYpxFUromvG45as689xeA0oqFmJsOIGEL4IoQeML4yzbPPaoqVrcvgXV8U8O73Vh14c4SAk8UTmLpkw9izZySzDxkkDReD2yhTHkoV074kx+LmmuDuAyMjJyu8JYsZuqfiV72Nrr609T4UDF3HJAjQD2gSvK/X9TJ4iNd+CxzVepvrh0b+lqjOfA44byK2r2DnVmfkmiZA1IDP19TE0LtcpEMTo0SXYaRIQ2B0VPPBUClu7dh4qKuLaSs9mfJns3+Pt7UyTZfSLupbi4qc6woQf+iq+dbgKpK9uv4Jt9Gx/I6NMTEr1J9aoFUCWnyU7Q2wLQz RsWVm3T3 9h+zhl4zeHhRzqNzwEuh6gHb5YhhXU2vXyfsm5UfaDNfccqjK+VHMYCN6cEdLUnzrVfFXDnvC8q+hWIjqJ0lbEJAerhrsnW2MMGHDAsBx/JcRPZ8meob3V5aMbjIx13QOKYkyazkJL6ZbWCHAjUlOmGTw0qCq+UVPA1MQd3J1NJ7DzHCDB8VS1z51tBZGl6nOidgy4eWM6dmaE74o7DbyN3hHfnfoWyxDY77kw3iECjezx7RHHP0wfj/LuRb9aDsiXsLY+3HnSRmlx8RoF492oVp5znAsYY3kcGimka3FF6F/nia4MMxQg4/IcwA5POGPo4WzJLkwPxYuaGK+pj63/CM19ukO+TiU+SKgTKNCylB0n3s= 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: account_locked_vm() is used to account memory to mm->locked_vm. This adds accounting to the pins cgorup as it behaves similarly and should be accounted against the same global limit if set. This means memory must now be unaccounted for correctly, as the cgroup typically outlives both the mm and the task. It is assumed that callers of account_locked_vm() only do accounting against the current task. Callers that need to do accounting against remote tasks should use account_pinned_vm() and associated struct vm_account to hold references to the cgroup. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/util.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 78b060d..d6159e3 100644 --- a/mm/util.c +++ b/mm/util.c @@ -551,15 +551,21 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; + struct pins_cgroup *pins_cg = get_pins_cg(task); int ret = 0; mmap_assert_write_locked(mm); + if (pins_cg && pins_try_charge(pins_cg, pages)) + return -ENOMEM; + locked_vm = mm->locked_vm; if (!bypass_rlim) { limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) + if (locked_vm + pages > limit) { + pins_uncharge(pins_cg, pages); ret = -ENOMEM; + } } if (!ret) @@ -569,6 +575,12 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); + + if (pins_cg) + put_pins_cg(pins_cg); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); @@ -584,8 +596,18 @@ void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) { unsigned long locked_vm = mm->locked_vm; + /* + * TODO: Convert book3s vio to use pinned vm to ensure + * unaccounting happens to the correct cgroup. + */ + struct pins_cgroup *pins_cg = get_pins_cg(current); + mmap_assert_write_locked(mm); WARN_ON_ONCE(pages > locked_vm); + if (pins_cg) { + pins_uncharge(pins_cg, pages); + put_pins_cg(pins_cg); + } mm->locked_vm = locked_vm - pages; } EXPORT_SYMBOL_GPL(__unaccount_locked_vm); From patchwork Tue Jan 24 05:42:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13113538 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 2B8BFC25B4E for ; Tue, 24 Jan 2023 05:49:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B62176B0083; Tue, 24 Jan 2023 00:49:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AEB966B008C; Tue, 24 Jan 2023 00:49:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 917226B0092; Tue, 24 Jan 2023 00:49:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 793516B0083 for ; Tue, 24 Jan 2023 00:49:06 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 536B71605E7 for ; Tue, 24 Jan 2023 05:49:06 +0000 (UTC) X-FDA: 80388614292.10.6F15C6F Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2072.outbound.protection.outlook.com [40.107.93.72]) by imf05.hostedemail.com (Postfix) with ESMTP id 6CBBB100005 for ; Tue, 24 Jan 2023 05:49:03 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=L9m5dbqS; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.72 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674539343; 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=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=YGg/SRKWYC2GLvaTcG5XEc7hOlsqlxUD23H3ss5j9qvivaA+4Jaw2GoExmjE34WcYXr13J lzIefsLk4uVvjxpruzoXWxwXhnvh2kW3p9H2Zu7sjy/52JksiTo66kGAwARSii14A3egTB wQAihCA3zbA/2UFLeuoNEYqAUXthw2Y= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=L9m5dbqS; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.93.72 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1674539343; a=rsa-sha256; cv=pass; b=wKpfe5bi5DXVxf6w4fRs73oThyAWWWpQO1zBsUQlJQs2FxooEFEULt6Dsp01hIVPi/vKPG vTu5yzTMJpJeT+wR+u1GDJ03pUW6lWZO5mXzO/gwLdBscNiz7TLj68dRl/e9XgH4eb263f lAargzgviibM0oiUMhWEpqXgOEXM7M0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jSm/3kHBRU0tM8XeZYx+hSJdYgI46Gn/Vi0v09qYJZYLroCZl64ypeiS+1+jp5Ji2V00wkqLceGJxxmoYY30NkS23/tSC2GMHmIfSflJZlgMN9lkEzpqaeQR1RTIkEnSH1HZRmPvK4RuPXLJUrTU1kTW9wbm6teH/Xf/aA10tFpqUV0iUBwLy586Icn0qH6DnygCwZzusIDHbxKQeGLtWC4j70JSF+fxC1C6XiGBHxGtH2O1Yid2G7k1KL7oBi+LEmy4nWbst80olTmwLxmtFMfxPKTVGDft/WGcjvpbqfHyB2SVyxglffmOD8OsF+U63o3IRIAGBbOStT9PMeUp6A== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=apamvDn7VYzIigPlZIGjVuHuDL3/R9Z1TRxdP/Z/Ev0qzDRZaIKsg5F8VZxU+KDTQFNjz+3EWLKeUwD6gqlHRWdPUWARaEv3ThF24EeoPxCcx2QqG8lAZQOrEDHmsCcxGQWXrN8YI2sC0nJjOcdleiNRFKxg39CBLhKhp2qAMwQIudisidDE3rahs04qc+GYBdRlS/1HbYl92MyBYQ7Zj2Yehorgd73iEc2w/la50wllNo/vc4mMgIJ4SqL2KG4wDC7PMGfHgH98GbzpuyfpwV7uiednnRuxXSPjsC13/nuDK5jivrHSAM+PxMLMaQvk1oSAGJAvgsq8VBFl/J3C9g== 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=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=L9m5dbqSXd5PZ1FXIMuhCAMwXcfHhBscfWWGOTrz7txmi80DMBxphaqvJ6fNZdyb/zMXzHEqHsaH7/w5R2oXo2LCD5Qpm2ozj+4k+TuMQcmO4gZ6JlyFb5OBaT1aEmLqJZEsIjfgEd2+PAUIiXscE9HB7iKGzHOs5b6W2Rvcl6DUY7LJNrnYQlvWC0/P2pBFx5dtCQ0RaCo/rNiTwHFSdKJrQuTKiQUCooU+sJc2hvG7Dv2V+fSHUC7/Z/wVVNbPYm/0Gt/eYNftxiPhheaXJt73FlPdQ0RKV3QmFwpAeXaaEZC9vdaQ9kJbzZrAQNXzR80yVGyOGbPmm3E+2TlYig== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by PH7PR12MB7793.namprd12.prod.outlook.com (2603:10b6:510:270::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.33; Tue, 24 Jan 2023 05:49:01 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::465a:6564:6198:2f4e%4]) with mapi id 15.20.6002.033; Tue, 24 Jan 2023 05:49:01 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, Alistair Popple , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [RFC PATCH 19/19] selftests/vm: Add pins-cgroup selftest for mlock/mmap Date: Tue, 24 Jan 2023 16:42:48 +1100 Message-Id: <9e24fb108c8fae43c18e0189d86cbb978848fbb0.1674538665.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0014.ausprd01.prod.outlook.com (2603:10c6:10::26) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|PH7PR12MB7793:EE_ X-MS-Office365-Filtering-Correlation-Id: c9337a78-f259-4c02-d6a4-08dafdceb1cb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nZW9EVtfJPRnVQsBgUe+SteJC94dgJXjoUvDNPu/QQ+arMxKSzuu42xU6yQFBXsUN9FRqd6n2bz9Hkto7HdQxbbrn2w+EIlvJkF4CF2ieHnLTD7p8EVGsh9oecrXI+bQqwIVfSD3NzkpuLDhbcPydQ6oGn5w1JmzuGRCOpHyKEVoVgmfo4aLIz968XbyZAQqhhNbx3dNgpOKpwIIsuBhxsy11U4k4xT0y6hHo74zWkjYP6gSHbQRz0//e54MsBWfOuT5QlFJqQdxv50zZpyjHFoQT5+G0etKmG2nWjAaC/1aiFzO+NOiSn71PozGXcIf4F180B7ny5HsiUGp8yBq3Ez4Uic1OLdpE5iz1LSDJ7jjARtJXFwr+T2DB1cKITE2hpGpKFsyDgIiKqrBJYwas0wbGsFIUg18aBWU9TDGWBLRxyPEfa4633lLqLSaNd3xgvq3Qhv/3xAs2rREqsHwx1bNmos9iN01lFZgBnCZ0gh/pqO+3rN5QkShHUnDy74GPPkZ8VjbEv3hKy4sr4I2RybuiZB9z0p+wGOok8kkmqR0wnx0yuTiWhQw+ftszDsMJ7yRTwoByR6R+Qj3DnINVVBh0JyGmWD3EW18d1Rp0n+RYFbteYsAQFfIVJ3Y5YRD/OlGfyYFw7xHMfWxoJwMsA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(136003)(396003)(366004)(376002)(39860400002)(451199015)(36756003)(316002)(4326008)(66556008)(66476007)(8676002)(86362001)(66946007)(186003)(54906003)(26005)(6512007)(6506007)(6666004)(83380400001)(6486002)(478600001)(2616005)(7416002)(5660300002)(8936002)(41300700001)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WPY+GKUJpF9Y0AnpULiDqZkxL9fEGlflMj2N9kpAorwkvBHXgeupmoy3KTTObF5bPE00ymFDCmrtr7v6MwFc/OCWb7g3GGxhWc13XvWaETUs699gIOIrj5bBOKGaBZ+0UrkjcJYTFtp+ANM9RdGm8AWcqA9C4T3Az2MjViioXDZl7ArzSnMuv0QmJ21gs4YVnX/ahMTIXLzkQuFiNYKGx6OMcEIzvgQaodR8zyxcjRTOlg7/i+pGKlKWKanPJ6f/GVJb8buyuS5kpAB4r1IIKV6SBaTLumcbT6wVaFBcVZOuUWnkT9k6XUf6ehFWfW8uAj/lncQCw77ucv1S1OWbjYkh3tPI0czSM7+5fhOISiTJkegqropnn2bxsvBEUgoXm/RQeVGptV+0faOtMfx6GDqrU5sDzaoLEgIWylM2GHLW6m2CWvojnOeLq4dxDj+IHluIUaOvS8sgWNH/KHud6yX1nMZObNHvsX6IIUmyeiEdKr4QT4PpDruaWT/jptQyXgrmI9j9OycqXqo94g9XD9dNFhKPtsWIws/Bf0OpeNAGrrHUqDcc8RA8EQ6bB+Q78vUHb0tFXpPkpBrN/HAm4KNbKJkWTaijhDVeCcfcUAP5pBwr4qwqf6UE6/bAOnIMPQLs2ij2og9o4fERqgg+tx1KN9mMCTJ8IQ+aYCd+s6sGhBrPJfI/fzAoCORnRaXHPyhz6OE5K70mBRmw1DiYG4kN30OaS/WUj3a7fMjabxYJFYpbX54BIi2BbgJxEQ2jy2jPctAiAizCaw3xX+zvgyvARWB6HSKORxzeQhxWMCDx/Z5WXut/ECOUq8DcrMeRqy3plcnaUZKYWVvB1dsxvsL8Y63QOj802W219a8DKMPvBZIWwjRNRc5kNUjJ0v6XtSDmzYn+p1osdjQz/a4tEk9eyDYOyQKJNV92y9cR4sLSEInugPVwSPIDd3q277fVjZjCaDlQZEfv4/0+ndwDxS6Ynq+VjJAz3aZk3F1PRZCfzssDmXi62zCkxeZ0irPWtxQaVAsSlyBkZ5ydpTOBDuICSqM04uBm4cx8cQ0yr2tJm7V6jprvd7naDp06BE6THrkpSYMqtDi9uKmXz8aPFGsMInssrNNKV7GA1MIrsgYcYXxbRZ0dYrJRSSkOu6WFG3wi3INzByAWhfeHjNgsNztHAAVwAFWsomSUup4PB9R7dnHRw8okvwxlVeNLdVyz5eIQTfRwOrxeTM+tI/CUgPzG8isWUzwH8JagEjlPVdm4NY3BHhcZ9zuWTnkexKdWUzIYOn3pYeO5thf5lkYHuW7uowhNd8Yno05EHfCzCJvxoesVK/8mE9Fl/38ktnFy73/OdJrDuMxYLWrIUQvZ/RXVev9+WFy24rKc+Ll/FtRFCyOuQBYC/kH6InLZTJtvebw5YZzevCPUoA83aiMXO6oTQCmFbpxMr/LZxYEXU8stOQMMDcV198aBaernGD+O6F5bBfIs2upvwTV5ST/gGwChYhP2RRy3MJ1U0zK/qMJp7XCMLs3RNewuYpN6MDkZgRnbUwvI39pyZjOb7Wbm78NTKrYBDGZfyCXKrQ1M9x8aE8ms8kfMbxPCLBdPV/Z1 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c9337a78-f259-4c02-d6a4-08dafdceb1cb X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jan 2023 05:49:01.7331 (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: GiK5/ZfAfZsxtXsuiINaaaXjWE7hysh0hrsnuQQ4ocrW2fQxsZCoSysuoOPBUCDeJ7DQUYM1n/JX3OpGOc96vw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7793 X-Rspamd-Queue-Id: 6CBBB100005 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: m6u9b7cwxizi5d4zx88ub3tarec98ix5 X-HE-Tag: 1674539343-382887 X-HE-Meta: U2FsdGVkX190tazSkie7CO/0vPdBawqGKmEITWqV6WE+25UPkok0cnj7Di0c1f49jFgqZ6lIWmcgJRp7TcN8rsQk8xs4oQn10yhzQXbqI9q6k4mmHbWKiNmCkmXAjz8XFmKqz3VXfk1qdS4fS7XDdpmAij7vlg2GMeKT28IsyLClCJ4e0nW0I5Znp6swCY8QC1snpqQ5yLQ4XO7PNwx4R6+duNC9+eIMerqE2cFavlZ0Z8fkQplaA8lLv9YdYC+hFliZO0f49wCfMYOU+z8rLTg5eTjlusZ1ny7cHaNfCj11FX2gE8kh66mpX8ZT2T++c+AWSbuMP/mcxriFDpcZa+r2G/GPlJpoyW2L3lhjqMmgkG9vgaPMdKeacZZfvO6455iA28G7hFfZL86Pmf2Axr9OBJLYWx2WzMJL2RLlNbp5FwseZ7Qwc9hTN18fDRhcR1pqNbKUnhSEvJqDN/DRTHxAqbmvggPiXqHH6hM8oqjieaxQDOBbrFvnb45gdV/YPPJd1z+aVuODL00r+MdE4vbPrjb9nrMNysLdpiY1ZALVIJ+YdQZJTn13E3kjX0CwQNwZMq4vO+n5mo8Khg7Pv+wdGNXB5WFKng9T+d471vc+woHFw5qk0zpJb/6qRxnrjxJbZ9eK7v8Qw+Pw4GuONeKPU34tjkMypDvlhC9TG0JABvD1mRrIWiyp2B7pqFxOIrrgGPzevI2O1yP4+EZZuGHphy0xs6LJm5PIVc40ueM5PJnXRcUWKd9Rwego78Nm4WcmkbIHe1UyCjTYTpjeqJAkZhL6Le0NVBzg0QeQ3kNP4hrQLVrTGwGrbXS+WyPcX27abqItaRvqgso0kpmzPHeO02YO8b/9xb16qnZuGNQxq3bUvW9rijKToEOeJZJawr7Je8s6iIz6gz8DTDZc4yrInICFap3ZeJ2c+Ws7awrPfn+9SxJY7nqvxXlTikLW38+AIDlVvLVXsoKf5iH VI+fCJiB HB/+3S7Zu4wd7vkNLoTldfMKh3Ns4VOWHcc8I/WmKVNcE8rcUeH4xoS5emKDWwhWcgXoE3cOjfe3urp5KZTMoGvOFaN8E3UDS4X4X8tGQFerWb8dnbkFfoNGem6iqUAL5SjN/YTdC4VBy/BDFhxinawj2rxmF8Ow+Z4USJo2VZk8EZq79E+qhhbIT1B7cToitCwVDpq+ZUFlZgq9AyYrZtyOxY79bhPhJPlGn1g/ZxiNrmr3HG/KEGRxQjmOiINnOx5xXTC/hbdpWsUUU5zQsfrp8712OIq90dZGC6hgzxuXl//hLmf7K22gzX4qSGuoPPUNo6GZElKlK2h+W+18vT7jp9BuT3YEJAMh0UHmiBYNatZJWEvBhSvuV2UmwV5te7toO 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: Add some basic tests of mlock/mmap cgroup accounting for pinned memory. Signed-off-by: Alistair Popple Cc: Shuah Khan Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org Cc: linux-kselftest@vger.kernel.org Cc: cgroups@vger.kernel.org --- MAINTAINERS | 1 +- tools/testing/selftests/vm/Makefile | 1 +- tools/testing/selftests/vm/pins-cgroup.c | 271 ++++++++++++++++++++++++- 3 files changed, 273 insertions(+) create mode 100644 tools/testing/selftests/vm/pins-cgroup.c diff --git a/MAINTAINERS b/MAINTAINERS index f8526e2..4c4eed9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5387,6 +5387,7 @@ L: cgroups@vger.kernel.org L: linux-mm@kvack.org S: Maintained F: mm/pins_cgroup.c +F: tools/testing/selftests/vm/pins-cgroup.c CORETEMP HARDWARE MONITORING DRIVER M: Fenghua Yu diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 89c14e4..0653720 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -56,6 +56,7 @@ TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_FILES += ksm_tests TEST_GEN_PROGS += ksm_functional_tests +TEST_GEN_FILES += pins-cgroup ifeq ($(MACHINE),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) diff --git a/tools/testing/selftests/vm/pins-cgroup.c b/tools/testing/selftests/vm/pins-cgroup.c new file mode 100644 index 0000000..c2eabc2 --- /dev/null +++ b/tools/testing/selftests/vm/pins-cgroup.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "../kselftest_harness.h" + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CGROUP_TEMP "/sys/fs/cgroup/pins_XXXXXX" +#define PINS_MAX (-1UL) + +FIXTURE(pins_cg) +{ + char *cg_path; + long page_size; +}; + +static char *cgroup_new(void) +{ + char *cg; + + cg = malloc(sizeof(CGROUP_TEMP)); + strcpy(cg, CGROUP_TEMP); + if (!mkdtemp(cg)) { + perror("Failed to create cgroup"); + return NULL; + } + + return cg; +} + +static int cgroup_add_proc(char *cg, pid_t pid) +{ + char *cg_proc; + FILE *f; + int ret = 0; + + if (asprintf(&cg_proc, "%s/cgroup.procs", cg) < 0) + return -1; + + f = fopen(cg_proc, "w"); + free(cg_proc); + if (!f) + return -1; + + if (fprintf(f, "%ld\n", (long) pid) < 0) + ret = -1; + + fclose(f); + return ret; +} + +static int cgroup_set_limit(char *cg, unsigned long limit) +{ + char *cg_pins_max; + FILE *f; + int ret = 0; + + if (asprintf(&cg_pins_max, "%s/pins.max", cg) < 0) + return -1; + + f = fopen(cg_pins_max, "w"); + free(cg_pins_max); + if (!f) + return -1; + + if (limit != PINS_MAX) { + if (fprintf(f, "%ld\n", limit) < 0) + ret = -1; + } else { + if (fprintf(f, "max\n") < 0) + ret = -1; + } + + fclose(f); + return ret; +} + +FIXTURE_SETUP(pins_cg) +{ + char *cg_subtree_control; + FILE *f; + + if (asprintf(&cg_subtree_control, + "/sys/fs/cgroup/cgroup.subtree_control") < 0) + return; + + f = fopen(cg_subtree_control, "w"); + free(cg_subtree_control); + if (!f) + return; + + fprintf(f, "+pins\n"); + fclose(f); + + self->cg_path = cgroup_new(); + self->page_size = sysconf(_SC_PAGE_SIZE); +} + +FIXTURE_TEARDOWN(pins_cg) +{ + cgroup_add_proc("/sys/fs/cgroup", getpid()); + + rmdir(self->cg_path); + free(self->cg_path); +} + +static long cgroup_pins(char *cg) +{ + long pin_count; + char *cg_pins_current; + FILE *f; + int ret; + + if (asprintf(&cg_pins_current, "%s/pins.current", cg) < 0) + return -1; + + f = fopen(cg_pins_current, "r"); + if (!f) { + printf("Can't open %s\n", cg_pins_current); + getchar(); + free(cg_pins_current); + return -2; + } + + free(cg_pins_current); + + if (fscanf(f, "%ld", &pin_count) == EOF) + ret = -3; + else + ret = pin_count; + + fclose(f); + return ret; +} + +static int set_rlim_memlock(unsigned long size) +{ + struct rlimit rlim_memlock = { + .rlim_cur = size, + .rlim_max = size, + }; + cap_t cap; + cap_value_t capability[1] = { CAP_IPC_LOCK }; + + /* + * Many of the rlimit checks are skipped if a process has + * CAP_IP_LOCK. As this test should be run as root we need to + * explicitly drop it. + */ + cap = cap_get_proc(); + if (!cap) + return -1; + if (cap_set_flag(cap, CAP_EFFECTIVE, 1, capability, CAP_CLEAR)) + return -1; + if (cap_set_proc(cap)) + return -1; + return setrlimit(RLIMIT_MEMLOCK, &rlim_memlock); +} + +TEST_F(pins_cg, basic) +{ + pid_t child_pid; + long page_size = self->page_size; + char *p; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 32*page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(p, MAP_FAILED); + + ASSERT_EQ(cgroup_pins(self->cg_path), 0); + memset(p, 0, 16*page_size); + ASSERT_EQ(mlock(p, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 1); + ASSERT_EQ(mlock(p + page_size, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(mlock(p, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(mlock(p, 4*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 4); + ASSERT_EQ(munlock(p + 2*page_size, 2*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(cgroup_set_limit(self->cg_path, 8), 0); + ASSERT_EQ(mlock(p, 16*page_size), -1); + ASSERT_EQ(errno, ENOMEM); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(cgroup_set_limit(self->cg_path, PINS_MAX), 0); + + /* check mremap() a locked region correctly accounts locked pages */ + ASSERT_EQ(mlock(p, 32*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + p = mremap(p, 32*page_size, 64*page_size, MREMAP_MAYMOVE); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 64); + ASSERT_EQ(munmap(p + 32*page_size, 32*page_size), 0) + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + p = mremap(p, 32*page_size, 32*page_size, MREMAP_MAYMOVE | MREMAP_DONTUNMAP); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + ASSERT_EQ(munlock(p, 32*page_size), 0); + + /* mremap() a locked region should fail if limit exceeded */ + ASSERT_EQ(set_rlim_memlock(32*page_size), 0); + ASSERT_EQ(mlock(p, 32*page_size), 0); + ASSERT_EQ(mremap(p, 32*page_size, 64*page_size, 0), MAP_FAILED); + ASSERT_EQ(munlock(p, 32*page_size), 0); + + /* Exceeds rlimit, expected to fail */ + ASSERT_EQ(set_rlim_memlock(16*page_size), 0); + ASSERT_EQ(mlock(p, 32*page_size), -1); + ASSERT_EQ(errno, ENOMEM); + + /* memory in the child isn't locked so shouldn't increase pin_cg count */ + ASSERT_EQ(mlock(p, 16*page_size), 0); + child_pid = fork(); + if (!child_pid) { + ASSERT_EQ(cgroup_pins(self->cg_path), 16); + ASSERT_EQ(mlock(p, 16*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + return; + + } + waitpid(child_pid, NULL, 0); + + /* check that child exit uncharged the pins */ + ASSERT_EQ(cgroup_pins(self->cg_path), 16); +} + +TEST_F(pins_cg, mmap) +{ + char *p; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 4*self->page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 4); +} + +/* + * Test moving to a different cgroup. + */ +TEST_F(pins_cg, move_cg) +{ + char *p, *new_cg; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 16*self->page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(p, MAP_FAILED); + memset(p, 0, 16*self->page_size); + ASSERT_EQ(mlock(p, 16*self->page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 16); + ASSERT_NE(new_cg = cgroup_new(), NULL); + ASSERT_EQ(cgroup_add_proc(new_cg, getpid()), 0); + ASSERT_EQ(cgroup_pins(new_cg), 16); + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + rmdir(new_cg); +} +TEST_HARNESS_MAIN