From patchwork Mon Feb 6 07:47:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129451 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38A87C05027 for ; Mon, 6 Feb 2023 07:49:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D198F6B0075; Mon, 6 Feb 2023 02:49:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA3D26B0078; Mon, 6 Feb 2023 02:49:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF4936B007B; Mon, 6 Feb 2023 02:49:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 995C06B0075 for ; Mon, 6 Feb 2023 02:49:58 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6911A160BA6 for ; Mon, 6 Feb 2023 07:49:58 +0000 (UTC) X-FDA: 80436093276.25.B46ADE5 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2074.outbound.protection.outlook.com [40.107.223.74]) by imf18.hostedemail.com (Postfix) with ESMTP id ABA0D1C0008 for ; Mon, 6 Feb 2023 07:49:55 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=dAAIMDW5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf18.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669795; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/U8SJoSKDmLPXz4EfnAjwHkQK/6X73YSJ1HtmZ4UlyY=; b=v+8Q45Db9twsYWf0MR76+/K2MUjUzGucUe+UKQkaLLLdNXEiv2M8PGlQpx9j7OQD4tNnrx q/Zcc+jcuAD873viTCjZW1qz7qbWFgabbjVCLWe9bW24iJCTMQoYgl9B41pwi7nnfa2L4v fbX8qnpDcs/J3olnlgn32s9+tlIZlmw= ARC-Authentication-Results: i=2; imf18.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=dAAIMDW5; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf18.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.74 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669795; a=rsa-sha256; cv=pass; b=YcJ4D40OanETZ9mkggCXEkNgylW4pUwe06nG7pizR1jvPUfI4cYIC/UmbXlKnCdu5bSeSQ eqfsie0iYelo4VHgBswTauJfGH/sue7U9EpYIlhx0rfi7bRNtn4gTpAb8IfQnK2zOXyCzb u2pvkfHTyOMN8FEn7SbQ5ko7kdyAIrw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hiUOKde3JnExUrE//qUgKpl+6GpkO5kv1pyyyUfYMB8hhDLlGfutXJEAyycwzqwiZ/OYFN/7xneLVvSyxvxk0AIaIlWeGqFJwX5Qm5eWt8gUMaCep9dVNXoQtjYJOWAnM+g+YAxJaE+KvD2pLgtbpvdGagB8c5uh4lDb1skkiQc4+2dnN6pOluHlTSVt+6172q3uGTgUn45vgYPrc01+KyAgOY5UT0mc1knvOSvjXBqykHYGF20p6nqQxOhr2zYuJKMUbjIiuukEwAZrHOFWlpWs2+/ibNYf39Di1opEu306xWNQUIoGWyS4zFAVxX7SSEWwXCAdnB8Otgr5+jsQ/A== 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=/U8SJoSKDmLPXz4EfnAjwHkQK/6X73YSJ1HtmZ4UlyY=; b=XeuX1XoPJs1REchFnlDl+XV43L79W8E30JxFO2gD8QhCqTgoDRCwBCKp1YrMRCmrwWGhg4zeoiOT3oacHc6YMe625/RTLAjA+vrl2WNUXdbbIwRsQ7ygyIgkZQ1fc1sjM6/ljGGnTdjzKg6KwZ3B50ltsyCY7L5Es49XX8MQ71zfJnsI4zjMRpySqJ4RVj4k0BLtLFKPmzIyTqdS60M/OR5MXXPBeLdiXyqxmwtChy/44/NbJ6oomCWPLOhgJDh++8pxLSWD4a9Eingrcw9kIj2HtFjA0iwJSwjnWuaHU2lW8v6c0pnNBJ+aLNpJNTjZ3UuluK1wqc4zF6KctVun5A== 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=/U8SJoSKDmLPXz4EfnAjwHkQK/6X73YSJ1HtmZ4UlyY=; b=dAAIMDW5Q0uOtcJk21Jl8UlVFM2xF6/6jf8SZTj7poyAZJEhAz+4IK3Gnjh8Kj7m5zHwcQdem7WNx94PYk768sb4b4ryluMsF9kSJBa1QHx+T99XapGI6gXMN69mZ9UomyDR8tA4WQ3g0l5YnammbpVFW0GI1ZDr3XV2Uo3QniO5T369TPCuPTi/lz+2EN0q9QOwLMvlxb9Wdy6yauSHCDpHAnqtzzT5mU3f7/JeuDXLFZgdyhevQ1y7OMHumnUNyzOF1xM/lj7cbestDuosqy7s4w+emoriYKZFsRnd00XTdrTsAVGkIjpw+w67IgyNslCJM4ohhfqvpJauUvxZ2Q== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DS0PR12MB8573.namprd12.prod.outlook.com (2603:10b6:8:162::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.32; Mon, 6 Feb 2023 07:49:53 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::4bd4:de67:b676:67df]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::4bd4:de67:b676:67df%6]) with mapi id 15.20.6064.032; Mon, 6 Feb 2023 07:49:53 +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, "Daniel P . Berrange" , Alex Williamson , Alistair Popple Subject: [PATCH 15/19] mm/util: Extend vm_account to charge pages against the pin cgroup Date: Mon, 6 Feb 2023 18:47:52 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY2PR01CA0019.ausprd01.prod.outlook.com (2603:10c6:1:14::31) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DS0PR12MB8573:EE_ X-MS-Office365-Filtering-Correlation-Id: 5304682a-003c-4ba0-7927-08db0816bbd2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9pqCY7aiHhVea1fBbDGml4HA2yjB9ffOiBCQiepLv3dI25PrVbY2Sc+mcQFlIQVWoTFS/HviX7kk1Dro9fbRIaOUm+n66DuTHAQZT3KoIYB+biaKJL5B1XTnPUIWTUoDqsg9Qc5kTrv82gvn31KM2YDmCA+tw6iFoKIjZ7zWt9dmMXDUvwryfRkDhlcJZ6D6+1aYSt4A9dw40Au2Lc8fTGnzCkjVspEC60iamkE+VvwFmvb7/g70gunuDZ9oNas97AQQLjpPDgWBUUK++89NkVBaW99yqUkIO41E9YPBGWOpmesmEZEjtS8zYgfTpgCvkBC4AZ1sfXxAFNizARkj3yvPlZ15YtDSsPFi37JaGnpz02cFh8Eb8nFu853VYLRjSBvFoLjjeI2gR/RqccVJjPrZGrJ5QlnkOWhY0TkLz4BMe+lAhKfuk2EOsWnueaF8oV/dAZw9AJ0X9kc+rJx4uk76trHBBoed5W7gmoHaHjDIoTfwR45Mm3uvavzbN9lxffomGK0R4CuxkGjkLaf0JPCempnwuJuTfI8qv4DZZQliTpvCa0AyS26JO6X9V0SG/45Eu7g46oK3SknhGQk6KAjASg/UGQLhCRG4b+GjIbsnf/gtcCNs7xE+lKDUJy+FNdxeKcuWqL5RFS0DqmKpXg== 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:(13230025)(4636009)(376002)(396003)(136003)(39860400002)(366004)(346002)(451199018)(54906003)(316002)(86362001)(107886003)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-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kXGpqDyNkTM1mnwGPjrKMWk0nOT4zafgmWTGI2webd/i6WoOG6BNaDatc3uIg4SLuPfv5na3wT6GOVk8O82dlKrSy+hsodnMfN1uIGWIBnSkSyfTSsOnPTl6CdjH/U2qShbHIPKXOBRWDo/0BkflZpb9mZqzXmPffzEpDYU4VtlapiZwJjdbs7obqLtDhVqqmkoJxBhVC1XGJlZaDkbga9JRyQvyLI9Yw4JWc6uzM50GlzfDg4ZcQ92slsJCc4SY4NIWfvcgw06RBrQlgFdVHnZ3HgR3XWWsDT0NfvK66AnYXmjjml2xusIn8iZY+6bePYmUSq66Hj63OSsfcooLX9v5jkwWiAX2DY4woM+ASKUCOgdESB8aLiUPu2IBnlmlgT8A31xEWipizMrYxTZccj1c3s4amn1G4ENuq6IhogmpQaVOwptN03wBicPN56TAoVy2zXxi/kum5R2MErlUinwdU2pSH2ibmgeCoPYSbysohSURARiCQ2VMETOYLi1LvXzi8PEIJVDIkWc+AziJygpanCmlOiQnMEwRoZCsKAzwGw0re8gXWuYY5P X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5304682a-003c-4ba0-7927-08db0816bbd2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Feb 2023 07:49:53.7898 (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: pHiH4yNzHOKl8lB1/tqrFYEcZ5GZ7zB4hXjQDFhgsIZjYiBDDvs+db6oKKpdttdPxHrv69MlSjOmpFFByMTStQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: ABA0D1C0008 X-Stat-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GeLgmHC4KsVBYD8Riwrvmdx4FShH3xzh3PGnnvsu6ru/LZhGEwFK2XpiIhzC6YjjcMKYVyey2BbatrUFGlyBYgb8JNNySoCwaX6gQb7DrjqOOs93HMI8n3fx1c9Y3SOAucfA+ETERI12FgwLfTzTAcMwN+dT1rOFZXYzayYdUwyhCEW6UZU3lzdr1V5gh1Rvlali6um4M/Fz+kny81FjAn90mYhYDe6FfmDW4O4rXQtVa5DjaEupJlDCw5jhFob1JP1VqJ03dIegPGeD4X4C6YH2X5t+5THdde8qSRoqAOJY9Jc+ywAaBJXc/H/yu6L1kwxampFlvI0G9Tlw9d94DvgLkhVIEOpIkk7XS2I+Dkbf1sGIhzJJ8u1ymvgciB18vOXZIa5hefYNUQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The vm_account_pinned() functions currently only account pages against pinned_vm/locked_vm and enforce limits against RLIMIT_MEMLOCK. Extend these to account pages and enforce limits using the pin count cgroup. Accounting of pages will fail if either RLIMIT_MEMLOCK or the cgroup limit is exceeded. Unlike rlimit enforcement which can be bypassed if the user has CAP_IPC_LOCK cgroup limits can not be bypassed. Signed-off-by: Alistair Popple Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org --- include/linux/vm_account.h | 1 + mm/util.c | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/linux/vm_account.h b/include/linux/vm_account.h index b4b2e90..4fd5d3a 100644 --- a/include/linux/vm_account.h +++ b/include/linux/vm_account.h @@ -31,6 +31,7 @@ struct vm_account { struct task_struct *task; struct mm_struct *mm; struct user_struct *user; + struct pins_cgroup *pins_cg; enum vm_account_flags flags; }; diff --git a/mm/util.c b/mm/util.c index d8c19f8..0e93625 100644 --- a/mm/util.c +++ b/mm/util.c @@ -453,6 +453,7 @@ void vm_account_init(struct vm_account *vm_account, struct task_struct *task, mmgrab(task->mm); vm_account->mm = task->mm; + vm_account->pins_cg = get_pins_cg(task); vm_account->flags = flags; } EXPORT_SYMBOL_GPL(vm_account_init); @@ -472,6 +473,7 @@ void vm_account_release(struct vm_account *vm_account) free_uid(vm_account->user); mmdrop(vm_account->mm); + put_pins_cg(vm_account->pins_cg); } EXPORT_SYMBOL_GPL(vm_account_release); @@ -502,6 +504,17 @@ static int vm_account_cmpxchg(struct vm_account *vm_account, } } +static void vm_unaccount_legacy(struct vm_account *vm_account, + unsigned long npages) +{ + if (vm_account->flags & VM_ACCOUNT_USER) { + atomic_long_sub(npages, &vm_account->user->locked_vm); + atomic64_sub(npages, &vm_account->mm->pinned_vm); + } else { + atomic64_sub(npages, &vm_account->mm->pinned_vm); + } +} + /** * vm_account_pinned - Charge pinned or locked memory to the vm_account. * @vm_account: pointer to an initialised vm_account. @@ -537,6 +550,11 @@ int vm_account_pinned(struct vm_account *vm_account, unsigned long npages) if (vm_account->flags & VM_ACCOUNT_USER) atomic64_add(npages, &vm_account->mm->pinned_vm); + if (!pins_try_charge(vm_account->pins_cg, npages)) { + vm_unaccount_legacy(vm_account, npages); + return -ENOMEM; + } + return 0; } EXPORT_SYMBOL_GPL(vm_account_pinned); @@ -548,12 +566,8 @@ 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->user->locked_vm); - atomic64_sub(npages, &vm_account->mm->pinned_vm); - } else { - atomic64_sub(npages, &vm_account->mm->pinned_vm); - } + vm_unaccount_legacy(vm_account, npages); + pins_uncharge(vm_account->pins_cg, npages); } EXPORT_SYMBOL_GPL(vm_unaccount_pinned);