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: 13113515 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2DC7EC38142 for ; Tue, 24 Jan 2023 05:43:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232609AbjAXFns (ORCPT ); Tue, 24 Jan 2023 00:43:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232036AbjAXFnq (ORCPT ); Tue, 24 Jan 2023 00:43:46 -0500 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2065.outbound.protection.outlook.com [40.107.94.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B16CD3B0FD; Mon, 23 Jan 2023 21:43:39 -0800 (PST) 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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: 13113516 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66D19C25B4E for ; Tue, 24 Jan 2023 05:44:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232818AbjAXFoA (ORCPT ); Tue, 24 Jan 2023 00:44:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232784AbjAXFn5 (ORCPT ); Tue, 24 Jan 2023 00:43:57 -0500 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2062.outbound.protection.outlook.com [40.107.95.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D7293B646; Mon, 23 Jan 2023 21:43:51 -0800 (PST) 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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: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: 13113517 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90AF0C54E94 for ; Tue, 24 Jan 2023 05:45:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232910AbjAXFpv (ORCPT ); Tue, 24 Jan 2023 00:45:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232900AbjAXFpr (ORCPT ); Tue, 24 Jan 2023 00:45:47 -0500 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20607.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::607]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB0833BDB2; Mon, 23 Jan 2023 21:45:00 -0800 (PST) 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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: 13113518 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D973C25B4E for ; Tue, 24 Jan 2023 05:46:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232981AbjAXFqd (ORCPT ); Tue, 24 Jan 2023 00:46:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232982AbjAXFq1 (ORCPT ); Tue, 24 Jan 2023 00:46:27 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2065.outbound.protection.outlook.com [40.107.93.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 836743B674; Mon, 23 Jan 2023 21:45:59 -0800 (PST) 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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: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: 13113545 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 493DAC25B4E for ; Tue, 24 Jan 2023 05:49:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232588AbjAXFtb (ORCPT ); Tue, 24 Jan 2023 00:49:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232999AbjAXFtV (ORCPT ); Tue, 24 Jan 2023 00:49:21 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20620.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::620]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B65AB3C2BD; Mon, 23 Jan 2023 21:49:01 -0800 (PST) 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== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; 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 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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); }