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: 13113475 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 8E3AEC54E94 for ; Tue, 24 Jan 2023 05:43:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229666AbjAXFns (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: netdev@vger.kernel.org X-Patchwork-State: RFC 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: 13113476 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 A20F6C38142 for ; Tue, 24 Jan 2023 05:44:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232815AbjAXFoA (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: netdev@vger.kernel.org X-Patchwork-State: RFC 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: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: 13113546 X-Patchwork-Delegate: kuba@kernel.org 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 6D8ACC25B4E for ; Tue, 24 Jan 2023 05:56:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232609AbjAXF4F (ORCPT ); Tue, 24 Jan 2023 00:56:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229666AbjAXF4D (ORCPT ); Tue, 24 Jan 2023 00:56:03 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20600.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::600]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D38323DB2; Mon, 23 Jan 2023 21:55:36 -0800 (PST) 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== 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: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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC 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: 13113477 X-Patchwork-Delegate: kuba@kernel.org 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 6B75DC54E94 for ; Tue, 24 Jan 2023 05:46:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233026AbjAXFqh (ORCPT ); Tue, 24 Jan 2023 00:46:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232967AbjAXFqd (ORCPT ); Tue, 24 Jan 2023 00:46:33 -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 8C0FE3BD8D; Mon, 23 Jan 2023 21:46:11 -0800 (PST) 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== 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: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 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org X-Patchwork-State: RFC 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; }