From patchwork Mon Feb 6 07:47:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129430 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 BFA1CC61DA4 for ; Mon, 6 Feb 2023 07:48:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 357A66B0073; Mon, 6 Feb 2023 02:48:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E0216B0074; Mon, 6 Feb 2023 02:48:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 132096B0075; Mon, 6 Feb 2023 02:48:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 039BC6B0073 for ; Mon, 6 Feb 2023 02:48:26 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C58E9A0B88 for ; Mon, 6 Feb 2023 07:48:25 +0000 (UTC) X-FDA: 80436089370.08.9BB986D Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2048.outbound.protection.outlook.com [40.107.220.48]) by imf07.hostedemail.com (Postfix) with ESMTP id E293840006 for ; Mon, 6 Feb 2023 07:48:22 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=A+FFKqJY; spf=pass (imf07.hostedemail.com: domain of apopple@nvidia.com designates 40.107.220.48 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669703; 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=xC0ascId9hC0FQ0mqA9gGlsD4Tec21nIaiiKW1ANuos=; b=TLrWXaGb2RIrpnZhQaB5GOh+bt/R99dKsNRd/YhObQMQBladJyb413/FTL/wtv4DRtypAt yufiGXxohD2SLO5UWEqQa3cNyWProh8kbqfPvq15tcUcATOps8hgVeuRhPSNH60v54BY1d qf2MpgIfEXXldJCH1SgiM5Vfa04410s= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=A+FFKqJY; spf=pass (imf07.hostedemail.com: domain of apopple@nvidia.com designates 40.107.220.48 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669703; a=rsa-sha256; cv=pass; b=bgGQnXhBtJuEJWkUl3OpdhSUBmhcHBcn+optZFiFSQ08rRMovQGHTdXYaxyltVmvyqEE2N x73c56LqG7cTTNqFdy5KWR8idvM8p+j4RmE59Y5EZFDIXyM6bzo9AI5B4x4spkasPkfhyX LeSoaFK9m7x2EUVBEgX1IF3o6dPaaZg= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aLsp8OkfnvgUX+LaCm1Dc+hdKUsiL3axpftCKUHDm1OQkZpSVWKd41QJpSxq9ko55snTctoS/hMmNjRr+itI3WbB3Q3AzmA6538HBTiXXlDO0S3AaJVzGqI3eu5++vIHZ28QtTlNa+k+dSpLAd+wLCU2EFbS1oHBSxKOk5zueaVmKcpYV50EsgXkylZrJGkkEePSOvrV9I/Bsj2oa8vxDvjsc+kGnGm5e5xycHnK4lkQOFyzYKvS14dWfW/63g0BN0S08F1XDTOjdIwd3UKL6vwTI7+T6eKerf0GqbZqLxKFY5wI1baz3a/2JfDv/WRas+jgQG8iAAdpSy/yR16sQg== 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=xC0ascId9hC0FQ0mqA9gGlsD4Tec21nIaiiKW1ANuos=; b=e2vefkFqyA3sW3pHTN46sHlZlBtRrXMyAf4In3Tgp7gdMSg10ziiTG5iwAIpdN+Nv9Oog6rQy/dMDY2dO6w3grxgRGp9gRCGkURF3SWuxdZ5pDPWPjWfF80aA0GfSO+a7RMpi+RETUh04airuQKTuoiT7NdVEbit7kL7o+ESiApp+a+LNO8scmfZwfOWs/wTgyVxb6qqRBkFNcpqOIFrxMM0d+LT8KWcaV3OeF5iSjB1ePBi+f8eg+llIHnurirOEwoKyueOezpUAMsA1l275ngrS9mwcE1qemLfX/zE+eYf570QvCXAfD87djcKO8wu77Flv39WV5CzVvqq2LSVCQ== 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=xC0ascId9hC0FQ0mqA9gGlsD4Tec21nIaiiKW1ANuos=; b=A+FFKqJYzlIAVESoxZN19dbMh3tOUmAXg71BZGbiuV4HgT2WwNhXvo9H+EqVd1hYY7W1iy9x2eJ4H3XwaGr2jO5d9GIETU1TZlbpbaiAHeuE5em9pnJ4MNG1/6OKDbNRjYa1RYLSWzUTRajUam0hS8av8ruJv6K2TuFY+X4QQbLJGSDzZBmJnFND8gFdvARudQbQbxKxMrYh7Rhm7JbL033fzE2OtsmZD+IYJ1sO5/fcSIk019OYzNFb21GL+KvbDlEwCq+4mkJl2w71KvdFVvoO4dgzK/vrr1yASUdmbDEzRitZTTs59gTXhfNc79Z05lPOjSUH1lVBxsi65AXCxA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY8PR12MB7097.namprd12.prod.outlook.com (2603:10b6:930:63::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 07:48:21 +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:48:21 +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 , 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: [PATCH 01/19] mm: Introduce vm_account Date: Mon, 6 Feb 2023 18:47:38 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0010.ausprd01.prod.outlook.com (2603:10c6:10:e8::15) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|CY8PR12MB7097:EE_ X-MS-Office365-Filtering-Correlation-Id: f1f32d26-9f86-4df0-cce9-08db081684ae X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: d9R20AXDYdOn6o7fqjRKlbn88gfGEhrLB0K3l/C7lo/hVvC76CZp4upyM7QvG93ysjuMVTpq/EYzK3rKe+gwBlyHxY43+Hqd/7szzD+TETPh4YIz49dsl8Pco7UcHmIYR1jDb462M2A51jpFSyZ6+sGx3FpEttvQxlIDeCWQn6283qmfDF4QeL0MGLkLR28DA19JymNK1RtyPkmfKZ09MLSCaFGJ3h12WGdvnJ5w7VGFIKFvz5stxIwwdIt0+gYERQCSlt2L26GRq9KV2GcYX8YE/OcLGZSrecKGGipuwqQm8Z7Ynh+TybkKzd+Y+ZdZKAXFYvSW5vAWUTT6n5GHEu+1RV85ersB37hzKHCRBbBjQk2dt2lN4HS7GS390bwhNfyVDFRZjE/rWx/Sf6l81vQ0i636fN5go+YWW9m/Cs2GeJ2kUXo2o5oo3aZ6hLhWLaDc2cMg8f+dO2hQNOm31Wmf2fNIgwK112xrMKjJwP7uMzzPMRVJHnTwtouclvVkulpeSj9ZswUbyFtJMziahx/BcMaIlFXt789djlRAEZlvA5Dolz1bNDtuZdc6P5ZWA0RBVH2YzDo/Bl5+FviW2ege3AA0jp0C20IRSY3uXiO/9yQxMIEjpPmWqNjE32CPYcudf9GcWGjjbN/MTX2aSQ== 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)(366004)(39860400002)(396003)(136003)(346002)(376002)(451199018)(38100700002)(36756003)(6506007)(6666004)(6512007)(26005)(186003)(478600001)(86362001)(6486002)(8676002)(66476007)(83380400001)(66946007)(2616005)(66556008)(4326008)(316002)(54906003)(7416002)(2906002)(8936002)(5660300002)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +Q21fH/s4Vq+Fkqt0pEjMYKv/M0hPKFwEmzpwjecwZ8dEKYu6PTd6B1USsF/VIewWV1Iy7hIjEqgKdvNy9XCHb6UCEjDlG2t6luAcyANcpffEfiNXj358C55PHbMpnatLbnb82LSxL2sxcLHQRbRVzZM4h6g/3499Op4qz4eIG6rIq3sP9GRrHx6Tqds5OHj2oy53ycVE3dECU0kComR0bp0U7UTdqpK3MVIK9uKGtcSo4EkCbqps8ws+uJxEI5RpRZ5p7iBSlY1ReOPfXul3U6G9MPEBkweNR5Wo4HcAc+BjX1x0G6BMm8BJAXdvBORkAl4CFmowC8Z7E5ZKBPUT5ioGLu+OjJVqhEa8hveZC2M2WJRwoyMFTyjy2k0f0mQRnvj+PbEIX9nx0SvfTjUFABqKwU2Z/1Fm7Xv9WusQn3HTFnsyEhVU+QqZxtf77029QaAn7uZJvEiblZXE7TUUdcHfTdyjgSVOCgszweWtFLTMBCKcBoMx6yuvAiU7de3NbVmKBBV8LRfJqjM7RFqUHnRfyvtK1pKsfXe4zb2OgYq63aCbk2+0hF6r+nw/5hVzf3oLt+yiapdrUwdK204DQoAk7KTXc0CUaKI+YNGo7bKPIG5cPGui7kSYT8ZHQawvSD5trq1ErBFkeBBXADoptjHtDfi3Pg7Ff9bjSqv6u42noCshGmM1eBj496JFjjjXcJhMESM0UuKS0F2TKDR4mzK8GH/muRsjSoL0JfMn014g6VRrKHh+a22/xsmKrh3u4HljrqaMeDB4He5JHwM8s6+kyeU3CkKgOVeyN4xgfwHQ6JVUOEPzp4X3pKkge5V1WIX3TkAs/DwB8n2QfUb6ruFRychXQVgfdrAn7N5YcN5N2pPqmatRWTthyyJoTWcZlP35J8yKRhYIQwQbIQ2MaYqf5KLWk134Bzhxzo196snfUec2RW4rFit/7Kw9A1emZZT6nrbdKxVMb2x4rLRIxM6jc3snl4WQrocd8r+Jex/vzsh4zSwhhQK/ZeitOfxrewOqeLpG4hAG6rV+1VYTwlTQSKpvx/dthFLHcaIWOWtpB7h22PdCdxl4ctObXm+w4mkFr6WPaoITf8lxJbTwuYTIvy/Mq7HbwmfN07KQI8tkrtcRQ3iewm7r0HFtWQlYXGN5XBXsek/Aegzb5gF66fpZ5YIsbXbHelBwOnZo3v5sLifuZO8AKQCMFaIOK1Twc761o7A+q72PinuISoun30BXs8bsrh8ysRQpqLPQZ+xf48dz4zs6W4s9UdVoaLqG6poioJSXQUKEZX9xL5SLtn6VQrm9k6GtYtOTt9BqPFEyEwU4aQb7bAr9tjtFNFQyul65a2UajRWmTk8oOjmPQjG+0Na8AIoKRBXDTYolD25NtClc2GWUtxdF/r0g6WMambOrmoQN6PDvnNKiH2Y3LfxvCe/hoG96VD9Gr2SfaI7ut/DvIDGvS6cWJREA9GsI7fQpkHJD5us1nkC88mmR73jv99T+Zys5Jd8GStZTQDKhn9QFylG+VJvDSWAbh1nlVL/o3r8ujdUkBp+ttAnY5R83qnC7BMvidrwhW6C5R1H2kqzZKGGZ+24X44EGeMu X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f1f32d26-9f86-4df0-cce9-08db081684ae 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:48:21.2970 (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: VKLsBrolhjVwe4eRftYA1HkxxQ/SlJh07YGyjddLzMudWGCcOJdVx+UWim/wuIqJ+Bp6tmOMG+wJxI7NtMvsPg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7097 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E293840006 X-Stat-Signature: 7m5h5oxaee9jhjogup17b71yrgrjqw5b X-HE-Tag: 1675669702-195609 X-HE-Meta: U2FsdGVkX19T6qwuCb/c/cYqJYFkwofXWVzMqD7CfJyep5tHs8RS2Vv5Zzw0kIS9LqxcmdmFuR3wFluop45yYOJgHmdmHU2eSON/UWGm4Hv99wZ7l+dGf2s6LU38NYwElpx7CRaOJe/ct4gWvwSQTJZkWfsD6Ymp+LXIeg3YxAs46drrnU0HhZDENdhecu5aE69RQpu9hP/ZqPBYR9/BtXIu0x7HIHI8FV6rvGdMKNPxVNM9SMwtWRQ1+T1oysbrc6rDEMiBuu0zW2mx9LOxOV0Tu9xvVzdjQIkM+y9w+PkNVsCTKXguXjBSGkODLmCZcJZL1NtZ6csO8a90vt3T5hmnVx0R7IX8MhnnwzWrqWmtAx+fHTBCDh5It40qbsm3212hlutlS07GNwidtJDDwtHCBm3+dhbtHKYMqj8sGWrMYD3iNn8G7WtDxiVF9wwE0iWa8+bAdMvPF8/kZ115xGrwvexKAyXxRhWei4EfVI9brcLFPe1HcBiBNZy0+QqpWbck7urvVjfnD49WA7fRD5CTc/LmXpHg5KcnKM2cK1qfd/pg0F/x+/5//+1n2PIM+HMHhX9jF/2Zw4EWT1aSXLqXLuEK9e7XLmpzggm2gubtoJjnmAH/VLtTB/pYR3R7gAw58ocL7DErOUL4cQCWD5pVqo7HHLK27OKM3EnyVn5PgDJcTEksnL6MHZeUwOkOyb5C+OU4f9zs920yKC2MHBBD2lnklMVXnOV+uodTXieXacDDr4r3U+t5dZe++vhOc3kYYUwQOSqQHC+A8kHvNQHtXQVtT2dXiRQ1FLEX53ubBXZxf+6Ljas64E7NoypSKZnS6/EeK1cJplvU2G+PI4oobaZ3omPYaRr3GxxWcMCvey/7011Sj4ub1JLrU0cgZP3efiCDew5wfq+SMeXm+Hyyjan6UTq0f9O88etwbnbHLg1ifKL9J5aQ4cp5ruj6OP4iySpQ0hQ4zZWCB9B iSTnxR7u ky2LgJ9GPnDTMbHddLdT45stmMAPNjHnoQjayC+wBzBMTMli5goiGkDgCmxn708nWcb3iw6T7M3TKOxA9qu+DiZ7RpB5FwRUaQI+PocBedQ8COj7rwtKC+hdbecP8jHYkIasvnvvq6ZgyUIZFz0AcPH8XqGF1NAw17fBHEiVeHPY1buXF7Nv5F0i0QJsAYKE2jKZCi7fFl+MMHXPVYbCQ2iDo2kFdbreLqKlnkAAVKsYuNoRuzSm57+oWTbQA2b0oOFIG8iPJNq82YsIt2Ymff5P/u+d51Xidtx6bOZnEd6lT3nFEplgAU1FhNzGtvjXtBC0SZV3TdNwUuVZ9MwP4M4/5jMXeqn7a1LwfWTZ/6r+3OBjOi65K41XGO6H/xDrZlOc8ZnEQmGTBP7cf+gVZJmXRO7evXitCxxmw7/18vuCDZqMgZ5+wjNesLeYl2ZayCBKYXazS/mVupLUU8u56NmiHYUt2BgYaXKXFE+zkhQoEUkGDzegdRqxTrycmVNRlooiiuTCqpcUNFFOCIL00dGavg76YeTuho7XncLtiP6vJUOw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Kernel drivers that pin pages should account these pages against either user->locked_vm and/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/vm_account.h | 56 +++++++++++++++++- mm/util.c | 127 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 183 insertions(+) create mode 100644 include/linux/vm_account.h diff --git a/include/linux/vm_account.h b/include/linux/vm_account.h new file mode 100644 index 0000000..b4b2e90 --- /dev/null +++ b/include/linux/vm_account.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_VM_ACCOUNT_H +#define _LINUX_VM_ACCOUNT_H + +/** + * 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: Account 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_USER. VM_ACCOUNT_TASK is used by + * pre-existing drivers to maintain existing accounting against + * mm->pinned_mm rather than user->locked_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_USER = 0, + VM_ACCOUNT_BYPASS = 1, + VM_ACCOUNT_TASK = 1, +}; + +struct vm_account { + struct task_struct *task; + struct mm_struct *mm; + struct user_struct *user; + enum vm_account_flags flags; +}; + +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. + */ +static inline void vm_account_init_current(struct vm_account *vm_account) +{ + vm_account_init(vm_account, current, NULL, VM_ACCOUNT_TASK); +} + +void vm_account_release(struct vm_account *vm_account); +int vm_account_pinned(struct vm_account *vm_account, unsigned long npages); +void vm_unaccount_pinned(struct vm_account *vm_account, unsigned long npages); + +#endif /* _LINUX_VM_ACCOUNT_H */ diff --git a/mm/util.c b/mm/util.c index b56c92f..d8c19f8 100644 --- a/mm/util.c +++ b/mm/util.c @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -431,6 +432,132 @@ void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) #endif /** + * 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 uninitialised 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->task = get_task_struct(task); + + if (flags & VM_ACCOUNT_USER) + vm_account->user = get_uid(user); + + mmgrab(task->mm); + vm_account->mm = task->mm; + vm_account->flags = flags; +} +EXPORT_SYMBOL_GPL(vm_account_init); + +/** + * 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) +{ + put_task_struct(vm_account->task); + if (vm_account->flags & VM_ACCOUNT_USER) + free_uid(vm_account->user); + + mmdrop(vm_account->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->user->locked_vm); + else + cur_pages = atomic64_read(&vm_account->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->user->locked_vm, + cur_pages, new_pages) == cur_pages; + } else { + return atomic64_cmpxchg(&vm_account->mm->pinned_vm, + cur_pages, new_pages) == cur_pages; + } +} + +/** + * 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) +{ + 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; + } + + /* + * Always add pinned pages to mm->pinned_vm even when we're + * not enforcing the limit against that. + */ + if (vm_account->flags & VM_ACCOUNT_USER) + atomic64_add(npages, &vm_account->mm->pinned_vm); + + return 0; +} +EXPORT_SYMBOL_GPL(vm_account_pinned); + +/** + * 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) +{ + 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); + } +} +EXPORT_SYMBOL_GPL(vm_unaccount_pinned); + +/** * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against * @pages: number of pages to account From patchwork Mon Feb 6 07:47: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: 13129431 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 C7735C64EC3 for ; Mon, 6 Feb 2023 07:48:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4413B6B0074; Mon, 6 Feb 2023 02:48:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 353F76B0075; Mon, 6 Feb 2023 02:48:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A8136B0078; Mon, 6 Feb 2023 02:48:35 -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 09DB46B0074 for ; Mon, 6 Feb 2023 02:48:35 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D4EBA1A0AF7 for ; Mon, 6 Feb 2023 07:48:34 +0000 (UTC) X-FDA: 80436089748.14.06E6D8C Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf20.hostedemail.com (Postfix) with ESMTP id 064F01C0006 for ; Mon, 6 Feb 2023 07:48:30 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=fHCzouLa; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf20.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669711; 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=acjFfLRivjpU9Lg3kvi0rHomIWvWZtcIDwMkhY2Ky00=; b=weZTsKbgKZEOPAtF1s0L9sgbjtgUUWxWaZ822BdnGWvkgcqlA3TZntevj/8MCR9OLWR90G 98QPdk0dcrzNhJ+eXO/k8mnlITw3bj/MvWdDQ/VBhTBYkNcZf+ljONBuOa3UeCHMLaYL3I /hOS9ENNyFPYhxmP4kuQk25S1eh1Ri4= ARC-Authentication-Results: i=2; imf20.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=fHCzouLa; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf20.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669711; a=rsa-sha256; cv=pass; b=xBH63rwBA6nckb3wz2U5tJregyZzig00T6U4npqmPwZPQ0TmibvBt6URM7VsTBMeqx2A2n osDvZwImXn3K+A0H8PmvEpGGJm6kqyUPsuKeA6oSV+jU6obGXOwZ6GrX84VkwpE61Sc20G r6V90kzDosuwDuRtw1uCmHTCCgDMCm0= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g1HGJAAb6x79B/2uO5UNl7Wjc2N7fO6FI2Dh1hbWDDlv+NO5uTH6mAdcm+er4k8nzu8P0Kaskg3FjHIudIEyRFfVc9Xm0Vk4e0HCDKF0MnkAmGYeoOz1TxF9jUHoP5rWgtbNqzdKjT/nhBnQVpGpsOmo11SmN+OhWMVQc8a3pxFwCVuSBKUFpmuYEYL9tW9J4OqExG8v9wf3KR1LoFdsLRxTuFo9mrXN5sxOPEk9l5jiCTs2QJNp66zeZI9ZeDpGsJCib950WH5sOGgsBpMI6sdq/39XLEmNcRQEo0wrXgYinIoFrAcUd1SZ9Tkb0u7ydaqjIuPUMKsr0WAVQ/xlwQ== 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=acjFfLRivjpU9Lg3kvi0rHomIWvWZtcIDwMkhY2Ky00=; b=WvQHQQY6ns3n0KefoFCSMggyX/x7tUCFBPrbp9NdXBsKIpZia9IjWTqt3eSONUAX+yLUVf/f7jSzoAsBeMeFbcbxdyGcuCNe8fhii7LnJQwbEVhoQP9ez9OILgeKvxr7XE6uG1UX4f3wtfuyx7uueogMthclboyNaqd3qFFXp9gdFuU9z5OQmVn7sX41V3f9gtlgjEpjlE4qtYykdA3BQ0qzRDi9puFRt2fttZpYdTSlTHSwl2yUwb/g78fOqsXXPtwz0BfZPXlM7XilI1VnZOxEpgjccCLr2WPMYZSyjgS55En9q1eQ/dhi5wL5ZcrgPrKpLkHNz+1GhTQZnn1PQw== 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=acjFfLRivjpU9Lg3kvi0rHomIWvWZtcIDwMkhY2Ky00=; b=fHCzouLaE5kk62Ua+XJqCu+1MTMXe9QNNft5c35YdS0i50bvUAWqQwwDrn6WVqaAYfc5Aidg/xzaQ5SFBBhZ4gA+1ACieJwCaudY6l1BzZjCaxKVgn4qTaBx9hGKlo3/y6J/vkzzjo2G9GpCRfcCOkH2sQmIVJC57BwZg+W0DjiRsab/SpX6BqJr5kYCky0e9uZliBjJYNKp4n5PYxO1SsvltINtqi+iaoiKq/hCDojR1uoXMTeQmZEY3+t1wjV97EZ0mdNWs5YOSNhOa1mvSuEUpJttEznQpB3KvsQ9L9ynFNXmo9SFjZFg14jddI/QyfyTAlVenv/bKHF6vnVsrw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY8PR12MB7097.namprd12.prod.outlook.com (2603:10b6:930:63::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 07:48:29 +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:48:28 +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 , "Michael S. Tsirkin" , Jason Wang , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org Subject: [PATCH 02/19] drivers/vhost: Convert to use vm_account Date: Mon, 6 Feb 2023 18:47:39 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYYP282CA0007.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:b4::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_|CY8PR12MB7097:EE_ X-MS-Office365-Filtering-Correlation-Id: d3c58a87-391d-4ca5-0a89-08db08168933 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qrNBIKLnIaAVk8pRj+5fZdPHiWQLbiCyBMI2zKg2cn/Fb5SIPPTq4lnCgIO9odXsb8ca5CT7XeYfek6V6/WonuEcy5JdMn6sLVlCiVnJ8+Ov1rIIBf9gLZjZuBCjnvH/Wd6xnfauUrOnS+56ZmvqmsGiKMU6TZbflY5PyeUFiyx3IRDXixQZZIT5ym6ZQ7MKh8raEJ/LH1o+UP7NJoPzzuQ48Jb0ifrzKOsxekho/unsBaIl2VV/xLiygpn8AGWQxju5/jRKJnca49WShesGkRCwuOwffP4oE2yAHv1f8cozt4/Yv0H5Uz2ztFJoql8uXWgUwQRX6IUgRTwCE7tQQuLG+Qz4yuo5eO9UsiTTacmJ8Km9Z6QVPwNo0KCrtfKt5095/INsZiz3W/Z6LobWgqDJ54JjdrO5q7VngPFH4X8T9mvvUz8lrVAsBxtlxyWg+EpHxRy7IUxEZP5qoAKo6gPJP0O2ia04BzC700tnSx/zt72yOijmqvpn2vGzxW2oWQqmePxjQduwtPn9uh9Dk2DH6zAoNnmfSHc6IIHnLxiJZaS8qc/la4R7HFKCn5HbPPtBif8eAJAqs5LEH2cvuBbe9xD5r6x0dls19uIBKIJ8FQRsdEQfuF6JagQheOknCfPj5fxaLNnCmWp0X7T88g== 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)(366004)(39860400002)(396003)(136003)(346002)(376002)(451199018)(38100700002)(36756003)(6506007)(6666004)(6512007)(26005)(186003)(478600001)(86362001)(6486002)(8676002)(66476007)(83380400001)(66946007)(2616005)(66556008)(4326008)(316002)(54906003)(7416002)(2906002)(8936002)(5660300002)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BcZpvnNwT9Gb780Y2kPdCwKodPcm4oSD6aoJb8it9MH7scy9VMdJ8eQmHUo/tYk3av3s2dxZOcg3ogvWJjgYJU4CmqrjLA6eULGYWsluLBw3iUNDcrusaQMgU/JBCYKX4CFq4sbi4garbnQxS/dBlAc5UWaBAEJv37P0BnLLCYvW/Dwx9ye4gQxLpwkc2n5jCYJeEc6WoZby6mWJO568dMYKg2KqFAI8FtEb6Bo9JoSvaYrJXHnMENDbbxrwkfTQtV8qXbCNBsDq1bG4y4mUzAcpT6ltL5rHPqvzBcfp9a0GvjXvPnCh1LX1l8HQUHGtYt64+hq7tVHXRKdpIcExFvOwAmgADX31MwxqDpZ1EsigM4KMtzPfDVx/vfmJvaGRTwbpGx0EbqDCfRpQ/NiwpLIUmNj2TLoK9Fgqx52KVlNmvkfN/l+nVudtM3k3zgo7LK0zFvA4Opm+lkj5nJUUHw5HsDR07JdM7STkTeJ6+JFp8GUKNp2S3lSzvg+LD0UHH+9NTqzGYQR1OwQmv9X9qGGWuHfFfRshcD81zL7CSri0B6r0PZn2Skm99ITXb4U7QwWYgs8Mvnu0nltCKavJuxcKRHUid7Kc7yBRK6mvMSgyp1Vs1mvQ+0wMs/inEdtR24LXqGfQ2aWUSH4B4rhEUuKmK5VFvCbXiIbnoW07w8zy+rE9MQ4V2KxbUqGETOP/tnKo7LAqgyPnAZ86Evukt1YLLGqhWC9r7V7i+mv4n/P7mgmDR9Jk1VLcWKqTWA7TOBQHOcEGIUkPABm1MrZhjL02MNgERX18/btiG1nGe6WpG4Hb/+zY3+a16GI9j88p3JSFcs5xMYdMUgaDQElzTqvfLZRqq8dtaxudsmCkpMPccM85x3bsyKBzyUhOP5MgnZ7/qkaCt9tFdWlBIzyV6B0DN/8vtjiFBwpFdpR4rj0IU8zBuus0sVu51D/E4eGnljmwG46lyuzyj3hbjrpiJxL9cB+tZiZKndr5GDKLeOr5sV1FGNUfePKzE7Zb3cLL2zqzvs68vmMFuSwXCjEGF0hiTUqmUeAceR2buVQ29OrYUgQL5hgOPrjdgQnhr0AOdQyUVu+ZxlHmB9rtkkghCCZvOqN+HZQ5L5Qfu1tumWUtGXKur+We/kQfOlcujaDorNtRdjDJ57GxX1e5QaE153VWR9tbzgD3JE9w4nww7MTRGrthmSwRkuN2D6gN6680FZRM0Px/D0U8S1b+pXvjU36m3/F7jAHDOiewSiudjzHu2kmF/NEChDPlDSFg2JjWnmtwXC4I2NBznXKXqG09yDjvXfhsuspBJRQA5AXnbiEbStPOeCzR5Cmb9xR7X17C9fQEW3gmKHacB9XA1q9yijtgrp3HSRHGuDWlC8M7HjTVWQR8WM/jbVCBXk7zyCwt3eX/Ij0h6a/HZhYTBUCfUWznODflv9lgUyMH4VvEt9Ic7Ks70kksherMhreAowx2RXIO+QQfJcDbFi90lmLWQnUx5vZMdmzU90pOXc8Mjtz2U3x5Ed8urI5Ni3+MqGIBprHYtdNtEDAXVR+dp3tWVhl0URpCAcBafVezXDC9KtVY3LizG/14x+HYNs6/KqUA X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d3c58a87-391d-4ca5-0a89-08db08168933 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:48:28.8589 (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: XPSgdGbBaHcZDUXMyU+Av5M6xE1fuCzjJ6iTymGC1b4edw5OZwM9c0xmYlltTHFtDUBV871cdAahWmlacV+qeg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7097 X-Rspamd-Queue-Id: 064F01C0006 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: uu1ahr5uzt5bms6t7mex5rsbofxpg13h X-HE-Tag: 1675669710-572163 X-HE-Meta: U2FsdGVkX19dbYf1BX7KO7EetRiB1+AIrvhhQe7AvQ9qNAx3GQmF6dsKmasYhK0BVr3MmOA86YqjyQdcPdlex13WtW5uu78E/p/1WICtlm3GhRS87dpzXErOU9jzdRaJMqlwUKH7bkQuPvP1PSrllGWnTPajWviqjbGyDEKDYRVwTeGOp4jpe5/DjufFGGPigLlmHR6CRtfa8UuUWv6enquwlDnL/JQWi6CcG8ME6FfLG+vdy7wQ2ZafX/PVt6iuciGa5dy65ETQwDf/JSzdaUAWHrxRNJLK424rW87kSpXs7IYXSpOwdhBzIWcNTgagbaPYXweS/K3c54jjZmlM34QjDP7xdlW4+/6qa4rg1lSZlg/pFtFr8i83KWyJPZONPfETNFIpuvZqqFm6x11gEx4JNMCbjRD1rCM5un8z4LEBUz/gw7bdNxyNGNNld3JYm0vjRfdo6mX6dwQ1rHjZbBLdDSlZ8aXny/u6tJQV0lkmm7v6CaKn0ufkHfwCRjTlbfLzg0pQibVuGKK/x75gp/aK0HaFOj5ZtzFDKaJuBN0umPXIfQ1PJzzhRpwGPkKwAjHZrIdCspqiD3xREUkU0VVZf7f1gpp203eQ1kisBSJouhYlSfl8RIcdwF1d3MDH9HiGcoL8huDiwQj49ppeo8OMEkm+pJfrKzA0JC83b0/pf50l4vjA2Z9OIqsFzKHV+WB4uT86VugyBl5wjIxTdf609R8JM29OgC/pmCLdKKMLVddLwYRWOVn5p3AaOop99Dy8IyrRdMlbn8gbCpGcDYP2OOJWNkUs53TpQZo71bw+3eiPR1QTTt29DdFRCZ3wsqQvBYG7ICeD5nMMdzJ0v+3M4tfVpODO5C+3p0oaS3b600ui+xq04pXyaeAC9BSH0NX+ZvnTU3in81ArkruFgpNL9nBiEBHpCiCHb0GEkZks4kuOEHXrLUbMF50bN2ETKtcMb8r7DggwMPBCtZy 45M6TBe+ prnYAtF9J399Lly3/ICQwVA+pyiHBuR4OmtHXWai2sdSvEpnRusaX4FMtb4LFyYtGut7y3+B6518a+seEaCIp1Y/vC7frsOPnhufyRxiM7v5LHWdWpm1RfpphRc3MMApKS5aQR8/7EOGJ5O+99zGgQhGr67n/GADDxAIWjFuhaBfX1Y+BP7YGns1es/1HWqk1KynKq5LMPWImjHiipILZPhIz4pLnduBosGH4IY2SVKEH6MW9BZVwJf0GtNzP5nL6WZGRRQS20YFu6ulwmdabH7OK6cZCj8JP0a7wIL2lOYxwZxRCT31m9RGmQbPOaN1ZOv06udYKJtwHYWApNfXw1VOhNWJ+3i+loRmeh3IAH7o3DB4bYdXdEfYUCbE4Z6eTfccC0RZaEnsfiiKp+s0hBQ7vzwwhphqBJxYueigFe4AEPc3cf2+Br3MXKQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert vhost to use the new vm_account structure and associated account_pinned_vm() functions. This means vhost will start enforcing RLIMIT_MEMLOCK when a user does not have CAP_IPC_LOCK and fail the mapping request. 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 | 17 ++++++++++------- drivers/vhost/vhost.c | 2 ++ drivers/vhost/vhost.h | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index ec32f78..d970fcc 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,10 +780,14 @@ 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) - return r; + goto out_unaccount; if (ops->dma_map) { r = ops->dma_map(vdpa, asid, iova, size, pa, perm, opaque); @@ -794,15 +798,14 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb, r = iommu_map(v->domain, iova, pa, size, perm_to_iommu_flags(perm)); } - if (r) { + if (r) vhost_iotlb_del_range(iotlb, iova, iova + size - 1); - return r; - } +out_unaccount: if (!vdpa->use_va) - atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm); + vm_unaccount_pinned(&dev->vm_account, PFN_DOWN(size)); - return 0; + return r; } static void vhost_vdpa_unmap(struct vhost_vdpa *v, 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..b2434dd 100644 --- a/drivers/vhost/vhost.h +++ b/drivers/vhost/vhost.h @@ -14,6 +14,7 @@ #include #include #include +#include struct vhost_work; typedef void (*vhost_work_fn_t)(struct vhost_work *work); @@ -144,6 +145,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 Mon Feb 6 07:47:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129432 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 0DE62C61DA4 for ; Mon, 6 Feb 2023 07:48:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BE1C6B0075; Mon, 6 Feb 2023 02:48:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 948746B0078; Mon, 6 Feb 2023 02:48:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 799EB6B007B; Mon, 6 Feb 2023 02:48:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5EC606B0075 for ; Mon, 6 Feb 2023 02:48:40 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 36219A6ABF for ; Mon, 6 Feb 2023 07:48:40 +0000 (UTC) X-FDA: 80436090000.15.3D66929 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf05.hostedemail.com (Postfix) with ESMTP id 74191100004 for ; Mon, 6 Feb 2023 07:48:37 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=EG2h74uw; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.40 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=1675669717; 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=LhgVGBn1pGz/qFGyTs5jsxzxhZjuaYEqMKugHfht1g8=; b=dhCtp8WkaAnO5iXfbdQ14HdCMlHK4ndwv5lSJ9ctxj2gXsnE2x0VSpu34EKwd+ftl/6v5x scIcyUuPfYkDwrPyD6xLibUmjhyEBHfb6rx8kzcYPFL5g0yFpvjWP2E2wjbpmZ5rTxWp3V WbY4LqGAcFTtJ/I99gDYQCr+IeQP3Fg= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=EG2h74uw; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.40 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=1675669717; a=rsa-sha256; cv=pass; b=SjWXfqJGtKcX9h3XNShEYPFqeAZ74A0FqH0txKXvpzrx/qVjnIVTYDQGOJiJfmRcw95g1x j3KxP0stP2NdAHBTJWPBBrD1MjE0enJBjWD9UL7PljjWOgKla9A+KMtRBZgLnHVLuSEPUd iwYPaHZUArprOs7A4QCV9n3eBtDmkR8= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gUcES+pqo8/gaEV3PIFIib52lHn/VOrsx8jpSMRqFVAbxhEWmN4n/jz1lmkOFPC+v/ona85JZMVKWGyE+dP7g9xQNODasZtLIrlPAxi9UppgdNPz3UgQph7F+qteVnFm4pHB5EAw5myrCcmPZnTm7Q1gPlGF05jgS5uwBghj9HRBvtHwIXf013ytQQX1ej6gO6PFu0BpVmnIc2ExWlXlKfcdzFRpf2ap6GQcXS2mebZ2FlhDYPwZG2Bk+zPf2I/6S1UjpvwuEkJJKyVRrirYE5jXT9HF36yC38RAijgrDQSbe8CpfSbVLAIhc3Ue79/nwmaL+hBOe9GS5KSorNhOlg== 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=LhgVGBn1pGz/qFGyTs5jsxzxhZjuaYEqMKugHfht1g8=; b=RTie0AH6Ja7bdpQzSh5zdBasN0/5ojxTsM9jwnwtNcwBVdEvFEXIKVV7+g+Wwcs8lVE76MB92ITe+WQ9Ruovaai+fX1EoQiJ24irLiDm+P+m9ShUuraEX7H5S/k0pGl0RhE82Ndwy9i/hdLXTX06EVQyPBOV9DD82Cu2cLYoKe6xHe0exusqZnzW0iWHqbZUnTqtE1e8Du2GBJNwuTRi6zh9bBwXu1o06TKN9fh4DhqNGenUMGwfT9ve7ljc+eFgo9dlvhzp4F3m/CsC/MtVKVxQ2qT+QN8a1AcZfZw/TQsBP2AuJOAemzXZ0gR0AZoPWUIXPEKs5AcrFqjghg2aqw== 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=LhgVGBn1pGz/qFGyTs5jsxzxhZjuaYEqMKugHfht1g8=; b=EG2h74uw6Cq6Qi5G5zGTzvDShXAREPg8KHKhk+2GPaAzK+tFLY3MT14mM5g2K1/ZG3mh3Ft15z2j+U5iMcWXbGYi3uI4kckvs0M73U983ggfRMQFz/jQfcXKvt1JqMDvPkz8AxcYBo2atzcnlbTTj2JjXJtlSlz1FJot3i/WV9NKUsNBSU4jw17F/8pdWkkWKzc4NWnvBCZfWPVUBOXFN+Qp7Q67fVBe2bSs+Ig0BP+las8ZCbRLCsBGCTUUQQAF8l6GzjYxONrywhz24NlvhW5pLF87RqKW7dWXRpDpeVywpCGz9DUTlMD0qb+yfysENSEH1rJ6SM8TOV6+RHbbzA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY8PR12MB7097.namprd12.prod.outlook.com (2603:10b6:930:63::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 07:48:35 +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:48: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, "Daniel P . Berrange" , Alex Williamson , Alistair Popple , "Michael S. Tsirkin" , Jason Wang , virtualization@lists.linux-foundation.org Subject: [PATCH 03/19] drivers/vdpa: Convert vdpa to use the new vm_structure Date: Mon, 6 Feb 2023 18:47:40 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0096.ausprd01.prod.outlook.com (2603:10c6:10:111::11) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|CY8PR12MB7097:EE_ X-MS-Office365-Filtering-Correlation-Id: 4f96889b-f944-4864-2769-08db08168d53 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hQiwSvhTcfTWvrs7VXaaan5BN+DICeV4GPI0BBRWGC+rNyrbf9kpsKW3RLEeol7Zm81u8gvOfgIp9+UorH5S+tW77GrVIrWFc5xBmSvA66wy6zcYzGrjES+LLW6f/S8RoV4Q16obP7fudBNRHnzp3dQpAp2xEb9RFlMnEEJg46tzo/Jkga2phFSgq408NR46v0J5xbXQRl2Y4IH86wZHKA7M0X5vhsE1GREpdg5VJ+mz4P4Oeg6Ne496cn8OGdXhPF0c3nTFZdIAI1j4uDvg0bxuZke878M0+nwMPR8W1qlqqDAGphbGQ5w/64hTEHa/o2TNbNhhQJbN8jeZGgugVNV8ahzKYSzAvTF6DDLANRk7vFCUzWte9OjNa2T+h1gfeNYlbGulSS1bRH9taEbN3xiRB4nEw7aKy7droXDOw3xASWCHx8CS77JwTQEZqv4T77Zt3uVlpJEvK9Bcpr0LLlmav2S6sulz2EVhlnG/tIJsFxduGYg/IUy5VcX+xz6fcg1HkHc7uTDkWCLliHZZ2jFsNiHIEH2hNmFi7cMkO/ZMz00K6qmsV8JdTCVfp2yitw6v9NSyk3BhwVXPjz4aLoMk0bPu8QRxwcRdgLnNiEkQrCLNy3DJyns5wdebyCjZpCQwJX310T78qQPNLTtrpg== 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)(366004)(39860400002)(396003)(136003)(346002)(376002)(451199018)(38100700002)(36756003)(6506007)(6666004)(6512007)(26005)(186003)(478600001)(86362001)(6486002)(8676002)(66476007)(83380400001)(66946007)(2616005)(66556008)(4326008)(316002)(54906003)(7416002)(2906002)(8936002)(5660300002)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k0mLF4eRyOue/HvSpJitpT6GorGUlHNMDaBeTwzcOkP+IHge4vG0XvM/3k1Kb112t12MKY8alY7sgvTb0cVZ6JGyRBoEINfXVWXFIYCEb8wzoZMeOOClB/gusKDqpGl2b57ZClnF5WLKbElPmkg9oVApmhw+IpF6a864u4Zu2dGSuTLj7JdHwdafz+/saMGh8tM8ux/zj9KZ1lvakrUfhMSPYTDos48Foae+flFBFaljn3LF1U+tzbjkJB9mB0wnOvbRrGKgntE85yz4xacN8W6bYt7aYKmRu3CfT5LEsJbUox6Mbh/Z6+S2y2Kl0sTLhODmz/sShp4Wdx5FbuKo/Y4hJQfKL05z0yu04Y+lQuXIO61LwcsXKGWHjIrnvfss0He96SCwZ6tzSef0bBSWV6vAQQvXeo54moxZEQss9M27H4xA9lmsLapxTJUA1fcsIWk1+sbdKN6ldn58B/yAGRB7pDeKb4kfeYcfM1Ucfs0u8gmX3S8Q2lGThhxrqrGfe6mvG09Fc+mDpjafH/4xH2a+4cvzvOxeIPrf22qlAReo8MRbDjiWWzZpKtbZ0OrpuZE0QuU5WzgToM2YWFJiuUj20Vcw2cpi9k0/D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uthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Feb 2023 07:48:35.7803 (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: Zukif6znujczilGtEBPn7wj4+KQRdgve8X5+fOdAxWvqS2ez0x9z6k+im9OFh3SMgIuhyXqLexI0/d6efbD8Rg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7097 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 3s5836wmzz5nu4cmwc6nf7rqxzs8ikjb X-Rspamd-Queue-Id: 74191100004 X-HE-Tag: 1675669717-487939 X-HE-Meta: U2FsdGVkX187++FLFkqvioaFx0eRBvV8rh06Ijne1RpCGMUJZvqjF8MUojKV9DMNZzCwOE+p+0P1hJbqzF+C0GoHcub2wLdwDpf9xVsfSWzCAcVKSyLzKmF9/uD9ur8Af+O/yEEj/+6kUTgQz04GtYzWVt2cutPhSkZeDZ91NUmIoSH70ll7jpUQp/QvGrI3k5jKtRMX/ogoUhBh2EvkgKtdwfhqo/+iteNqc6WxyqAKPvBR7Suvw+WCXHlDYh3K3rGVHBeOWGPcNkY9hceXERoFDVypvzHNNRVi7lmt2QjXAG6cNkR91bhRSoZyD9jKjcj01lhERJBofL4eb9r6grDKTewfrCfXYe57MDrK4UcHqErquMafv2co/qwL120MY8oUVLUL5WiVOosa+ruQbO0XEPDjgyuplO2msuTyChxVT9n01/0L3vfLc3Uxk5Rd3cNAAqNfd/qGpQKZXtoKNvVLgYi/N3j9RznPXbSQ5ujy5EZWuT06GiuF9GRkn5yGN0esw6j4UvHrROFXY4GrJ75uOvGqpCizdJiIULI+udE7yoknITENj0iEmGEOomZg8Dtw6NGwLY1MeUVsJmS44HOrYQstzBYa/gpUySJwh7tR4LFTb/VmjopY6oc+KbRAplJeLda3KtvN/ji/G9pG1DIQ5CXBnrK56o6lzqTKsO/KmZZtvB/CRmRtpOyGp2dcRPpliZ7H6kILUfMW8arZGp2fObusM/7KOX7259MwZAM1vPatWrokMPjCdNk6A6nBTuBw25ii8XOFm5fnar0mJQ0AuqUjOU8cmYAOysmLpn62BuBrNYIicJew0TPT4kJYwXXwFmXB99QVGiS5vBvh06cwRf92sljOS0UN7rUP46a1G2u26gD8Z7C9jt+vs09HdWVwdzdGhtddnbcBrA8vNfq73w8FRJdNTAruADlCGZkZCJwOMRsq4LscPXZMiiEqvoThwbgVj6Ar95ScBTZ eRz/NPwo ++t2trpWmwTXzsWcbN0I/vuAQPeqskyd2uVanHzTuu09Vqx1eYl9UYnvppbegXEVE1+kr64DYRayVCH+jwlI6POk3wCiQ6Qgguk8NoIVJAg5b3w2uQ8haPeZ9f3e2de7QJBZBG8OcQhXbyJzXNd2pwqEbd8cO1ktp3cp46b2cataefbu5+6HIcn5VYa94xUvr6tGpYtEYPogXlPP+9mfuMcxDumMhm6YWVS0YQajetOYbAtBBMJ0a97CTzInYTo6k86DsKf4JOBthGqE4mf7Qhjb880NDNewZJ43AOdkWBobChnN1grjgJB7xrfAwI9MC0AZZP+LLd3oN9kMD0WzrCcfKXCQFmp48NokM4WbTk5H2T1qLy1BN7xwZqdpia2DTwrimZM/ljOsMSsLfgewhXwdTe9XuCxF1i8haJBPdaPy/vaCge9hdqepiVQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert vdpa to use the new vm_structure and associated account_pinned_vm() functions. This also fixes a bug where vduse_dev_reg_umem() could exceed the rlimit due to non-atomically checking and updating mm->pinned_vm which could lead to a race. Signed-off-by: Alistair Popple Cc: "Michael S. Tsirkin" Cc: Jason Wang Cc: virtualization@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org --- drivers/vdpa/vdpa_user/vduse_dev.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c index 0c3b486..bc300e2 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -70,7 +71,7 @@ struct vduse_umem { unsigned long iova; unsigned long npages; struct page **pages; - struct mm_struct *mm; + struct vm_account vm_account; }; struct vduse_dev { @@ -950,8 +951,7 @@ static int vduse_dev_dereg_umem(struct vduse_dev *dev, vduse_domain_remove_user_bounce_pages(dev->domain); unpin_user_pages_dirty_lock(dev->umem->pages, dev->umem->npages, true); - atomic64_sub(dev->umem->npages, &dev->umem->mm->pinned_vm); - mmdrop(dev->umem->mm); + vm_unaccount_pinned(&dev->umem->vm_account, dev->umem->npages); vfree(dev->umem->pages); kfree(dev->umem); dev->umem = NULL; @@ -967,7 +967,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, struct page **page_list = NULL; struct vduse_umem *umem = NULL; long pinned = 0; - unsigned long npages, lock_limit; + unsigned long npages; int ret; if (!dev->domain->bounce_map || @@ -990,8 +990,8 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, mmap_read_lock(current->mm); - lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK)); - if (npages + atomic64_read(¤t->mm->pinned_vm) > lock_limit) + vm_account_init_current(&umem->vm_account); + if (vm_account_pinned(&umem->vm_account, npages)) goto out; pinned = pin_user_pages(uaddr, npages, FOLL_LONGTERM | FOLL_WRITE, @@ -1006,22 +1006,21 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, if (ret) goto out; - atomic64_add(npages, ¤t->mm->pinned_vm); - umem->pages = page_list; umem->npages = pinned; umem->iova = iova; - umem->mm = current->mm; - mmgrab(current->mm); dev->umem = umem; out: - if (ret && pinned > 0) + if (ret && pinned > 0) { unpin_user_pages(page_list, pinned); + vm_unaccount_pinned(&umem->vm_account, npages); + } mmap_read_unlock(current->mm); unlock: if (ret) { + vm_account_release(&umem->vm_account); vfree(page_list); kfree(umem); } From patchwork Mon Feb 6 07:47: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: 13129433 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 3F1BCC61DA4 for ; Mon, 6 Feb 2023 07:48:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D41DD6B0078; Mon, 6 Feb 2023 02:48:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC9C06B007B; Mon, 6 Feb 2023 02:48:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF5996B007D; Mon, 6 Feb 2023 02:48:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9CDC46B0078 for ; Mon, 6 Feb 2023 02:48:47 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 64A8EA0B6A for ; Mon, 6 Feb 2023 07:48:47 +0000 (UTC) X-FDA: 80436090294.13.8BCE345 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf27.hostedemail.com (Postfix) with ESMTP id 820534000B for ; Mon, 6 Feb 2023 07:48:44 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Y4HQgHQb; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 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=1675669724; 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=Ggww8xGqVkEn1m6fFSqIeGeKfBiyQH3gSOfKyWdB+MU=; b=hC5sbA3ztYiidUnffLsfmxsCKXuw4F9ZG/yMw99iFxRUrvBsZxeNIvTJD67m/bQeE/r81G qnGeETDL/CIxLNKf544M21FYcIK/5HSelGzSF33zhczL5Z3Ukcy8OVWzL6V0S1zaEOKb7B ncYKaZXldZlHkJZlZvpdcrQlgqyTABc= ARC-Authentication-Results: i=2; imf27.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Y4HQgHQb; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf27.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 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=1675669724; a=rsa-sha256; cv=pass; b=ww8t4nbXA4mF/pUjSHgs6xCWlbwQmbs7MHXxKnr/41IsA0CMInbxLBBXZdxoVLl3pTvuI8 I2yUyQqzG7bgUqYu89rngedwANw0CYpumLhFegDSJJUHTPjgHqrAlhDTnUwjdsUIFT0Y81 mo5ZiQW9deNOAZVaYcGPStk0A4TfVgk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E3zRX50/Lp7tD2TtC29nYwzq6A4eIeezs2ezZLJbj5liFzIktI7OS/65Hdh0xdp/PbjYV4dFv+Sfu+uAVbcPXM8eyXcRocoD2zYb2z/CjuVbgm8wovpcT3YDm8Xc6nM8tbGiKCvwSE23Wky+fOWMCzGDoPVmQRD3fA1WLL3UwycdPIC5/p+uybOlCh1MzBl+B75jF/G774DNZqcRUJ+0Op60U7lfa4JnFF0616GDdrUQxuI69isqjPKJaTg25VkAYWAM4tk6zwg9aJvrbGYMRcMXK9TLfMTmDixIohIc98CIWnJmGhPE8kG3ZO3sYzL8rIXc/P5PRKjgvYqAmfNTMg== 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=Ggww8xGqVkEn1m6fFSqIeGeKfBiyQH3gSOfKyWdB+MU=; b=nRqcU8SlI5ozKvuRTjwIW7R1YRuy+MsMXgCydX2k1NVuRPIGWcYO8v5Xv4jnYToHxkvADsrPTDLwrUvNDhxWy8WF0yPBNQPUG+jlpNQTy7572cdqNHgV+oP/R2sRd05uPR3VcyUY/5LKmnMIkDEz0ta3etjkfVH4U8FOVAI/uD8OqMf6iaK8edHggSwZSUXatrSS6TxXu3XNiH8Z/RTEtSkKO7FZBpkm/7kMghp8mEqm0sECRqs5QO4kYWsHR3s1zM61ZQCwbZKmhjgXiYWJm2i6jVGaV4H+Gyxw2V9KsoEGgTDt1E4tWgvsWNXfQ5u9CVTPHm9aVfyXpwIVtnLWpA== 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=Ggww8xGqVkEn1m6fFSqIeGeKfBiyQH3gSOfKyWdB+MU=; b=Y4HQgHQbpd91DuQaDucY1BFD5ChpJLapbErCJK2J+Qdg5Zx3stAKxZfg6VuRoL8s4vyXjQXCzFra6r6nA+gOs06zJbufQur7eGvFXmHJbzoSFxkGL6DF+KhHE3q3v1ciT5JfcQtrpuAa7n1zHzaXkTUZEZfvuMMfQKE71BIrdvzHusNUsFlE0TrMLO/PV0c0lXrVjw4CBw7P+B5+SVnn04YuhMPSKptsOCYFvaEaexRvZykf3inWlxlrpw8GKCfR1+2MVlVy2sYAd831w0X9oO2VtIruCF1GfWwqfQyAItcM+2e0HwP31U+iWy2oh1Y/G8H42/B1RHbxRGtzlWnuuA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY8PR12MB7097.namprd12.prod.outlook.com (2603:10b6:930:63::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 07:48:42 +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:48:42 +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 , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [PATCH 04/19] infiniband/umem: Convert to use vm_account Date: Mon, 6 Feb 2023 18:47:41 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYXPR01CA0103.ausprd01.prod.outlook.com (2603:10c6:0:2d::12) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|CY8PR12MB7097:EE_ X-MS-Office365-Filtering-Correlation-Id: 5a73dbe3-3319-4ce2-5ac6-08db08169148 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F/B80LQTVpL3jSLjBEjJ9QzNuXDWASskW1lHet9GroRZuF0YWlRLQJ2l6+dKcvBNT4uIdj3tgA50O6uDks7H3QyeQQ2mmNBfrQwGddxplhOX+lzizG0xwlfnhw733kRTbNW5m9EF7VmCDQU/ksswQ0Xvfq8Zq5s/1UC4s79qXLLvWzyRqJah4PaY4VukpAf2mK2sTLhRsGaV6p8fdYqI3lR4POWM93c/dKkAxlQRI1TzhldgsdwWxlXfew/fjG+QalrH9eRuTwPNLzgF/9s4+R239SOVYyzPdOq//F8y2RIxBal2+R78/9ZQkj7F/8dFZ0z8ET88cIyClSZIqNkOYA8/1YeyVWPGUG84L8qdhCssAy54zvJBGXlpzMkl4XjBp0yo9xqHAqwmu84/gql6ORMSdWpkFbleqf0pxocB31fOAliRLE26qLtbqQ5/1Lca0tKOMITqd7aBQJe8IrKMOSwTmj4A+z8CJ1yt7E9cQo19MW0LjJsqd5DhISCVs0UdExjs44vaMeUT/xLHO1DHOzY/Sbk/juEiillJDTHT6z++t33Sss25mBfe3AwjY4SPgZ2HzujYCccPx1VNfUfWbHNgowcQCR+Y6K0iLAeGv3EktxQpf8nahk4jzTTFBrN2 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)(366004)(39860400002)(396003)(136003)(346002)(376002)(451199018)(38100700002)(36756003)(6506007)(6512007)(26005)(186003)(478600001)(86362001)(6486002)(8676002)(66476007)(83380400001)(66946007)(2616005)(66556008)(4326008)(316002)(54906003)(7416002)(2906002)(8936002)(5660300002)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ubW3xrU0eGbFRq+G6RJcwlsfu6Ao+kgomSilgOyEGWZ8GnBsXzXYNDlWM32S9LpWG3caLY+45OVebDiKsG9RjYQeSCNLseBRz9Q/nsngnSZ44QPcejoiJJjHzbe+9ASDL3KZ8Vd0FbNgelO3asfiSNs0ykO57Hd+OOqppUqmwpg7qGk4FHDR1rZAQZHVxbyBswPbqdDLmXVPJRxyCYz4g/dIWPObS6YibcM90G1kPud6Gd5blcwePU/DQnD4ZbnfqTi9NAxTLz826/zRE4QvOemkr6fxeGr3PGVoSADxhqbu3CVMSY0Gq+0slPx+k9H6FuCEXqCS+wCVlcLsb3rNn4KJzc9RR6qwUx1IAXOLDhhxzkHWeRWm7RpWkM353mwqX5pqx7c2zklTbKFIvUzos4hva0ZzNjGOZJKkXRguAFsTKMyK8Tt26D/Ns1IxMBNu0ogQD9zsGTvBcvoq2mdnjJkH+x8SU8+r2xdUC2IC48OwBPfd9O68iZybjp5hi0m69EU3qrtcrReabHCrO8UdlOL7243ACaNmlyABkfU7nkpKepp9R/Y+ScXtI7UMtyNAuVB5UluoNjLx5SATikTtw3PtXPj7herx2KK1/o4QHS4ytiLWQRauR0Ws5IsuY34yrO46U72rdx1OhHXfLQ5FfPdDIYm+ygPb5/6WSDtwnf6pYl/41nKB46r8CqJpsGKRxl+fXlc6aLTc0HUfX++4VycAwoCRmWeM3cQXQehFj22YVnMpSUZ72bvYKsBKZvBwAS2e6VpDieFx3qm2e7di/Q7qfeNmeY3uMX91421Vq9H+jPqS6i2NbI84Z8PkJPevUu+1jyhZF6+MZ2fee+SoP0ej9TZOPhjjP6k0bUJHvHRgNXNrfe5doszBcgA8QxcXajOIPoQDnzbS/aADmmNO0lmPTRh3sfBtZo+Y/9OywcgJS2X/JKVA+KWID+sg/Mj8JQT6Mf0B+7Yc9IrP76s7IdRSyVbwCBhqFbSnSRy6NbEGh8EPkIQs/coWPsjzqdN77uueeJ19G3sj6OZTe4kcqMm/bmGB4ckjmBJ919mPyo55zKyQWnDcjsPxCzWlkzdNhB3K5qPbi7G3kAaz8qYId16bwGZPrtOwpf5EelwU35o2B7ndMRLqlEyGSpWH7fNS6fW9hlNlG+jcFpO6rRe79QlXH8I9m4iEbxiySI3St6d0lzO+cUX8H+0kcAMswl5HYwzMJpYKvRsQMEnzR255R2ocS4daaQwzJsP1RBaLJ52cus8zYbOpvYvQSjy2a5LDeu73N4hMIbrEhUQT5ChLS1NLaVdjr8gbt1akfdL6bpbbMnnWV8bv0aC54TcvNKS+Ea8b5udNZfZWD1J4odCyKsX2QXxwIs4UP5+CwYxbY4AGVtxB5nc1i9UQRCLplKteBtBcmzKOF6ugM99Dyo8d1JzOejv5dkdXCneCfDsamuTe/PKBihj2UHA0YSZdSzOY3vjXsyfib9hdwrNQmS+uL+u6RxerWAXuDRWa7M8M0C/HudBP80dHFEgxiVEn7NnRTfXODB3v3WRylzD02OaypRCs1sYhWDuc649+9Wfhf4wF6+wFKVpWik+fdBDInjfZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5a73dbe3-3319-4ce2-5ac6-08db08169148 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:48:42.3891 (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: 7xkdaR23n6b0+bhaWr+Avl1uc+gTnm7SJDHdHr2AR6iQKfTKWkLr3YjTvCIVM1p3eNklwHEvskiS3YS7BrKIEA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7097 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 820534000B X-Rspam-User: X-Stat-Signature: fh1eq6hkad1y8weqhpfhaotxxs87hg8n X-HE-Tag: 1675669724-366253 X-HE-Meta: U2FsdGVkX1/pM6yi1EmvbtyNxYwM4MA576jfxPN8dw6YlSfM8rnKkremN+/YxSTPT12Z100Iejp6h1F3BFzMf3Uc3/co5PfHZRy1J7Dafmy160qnJlaapwvTHCxjZfISSmKN5329aPNs+JCYdZ3RV87509MeNTnHZUm5fFjl9SkYoOqD6c017JxWtjcWr+a5SnTRICLUlnRVXfTqfdKHsxBjaML/eDyUqKdRObHDIz1zxAzui4WXM4tmoYVby+T7ONDYBb2brDt8Th7dR7su8Ck90hHienoqmjirhQJNJbWNTa6Ov8CTEHCCE4aZoKznDr0VBhYqihzQdsjeEX4KeYHzKFK1lnUuZQGb2GEvtR30sNWjH2XNOfY6Sykf3bpw0/LRyjds5/Ipf0xciCZbXfqgzTtZPwvV88Mt+/3I/0/4zKNEfUXa/0xXKpQiRnzgwzR7vLjiva3HmrE2eAFR7SrTWHKEyZNYvDIZjKnfA8JBPA9+SzoqlpwWp61ogdQTBzeX47Q4HTJODVLAxf1C+qSYIAyG4a9xYrDWRMF+oqypAvrW4ePQo9G2dlxGbOr0iQc72fxmea5O+ZUqWUllFo30Ni/pESHCtJBzzbGTSHIndTZxFht9yoKG+21MfA8+NRqI0GQZ8b2mbeIy+7R+rxHR9GZIEGdPOcRqb564oBa7TC+5TWGst9wliZyUrjuazCI/jqQe65iGqrCbl7gKc2j52cMzMQFR7s4nzXtj99usMlp1qH/mNpR+T6aQMnzx8v22DcDSozAZDkFLbalyTicySilkcMc+19DNMQdfYYFnSPjCrkM7iH918YKWHHs9QNc3IV6JVU29jEtJYRDn+JdlSqPWXVlLR3G+ChQzpLrbWuKBUXNWDIVo19u57nEINqosrbxBmf7tE5bwTxiUI3pglevv0lSzCvR5YUjNz1I1EVzREQ0wke+iLwP+z4iDBx0ArK/21tT2GLJl/Xt C8/i2Hlq 5IAmUdxccEiL8cz5s4kPuFjps/OdIJm/2ATxFX5DbCqcEP8KE8r1rCiCjGFRhxXzWSeVZ+IWMzMNpqULyvWdR89h6cokOlbGakD7qFknlwn5nnVnFQq569KNqKmKgAZSAX5gM2JkG84RvfJ48534trPS7Fwt9pzL1PvKsgc3LyzZvUGBqRcBt3aGFFJ4lUypEA0aupJ0U+fxR81Go/KN7vZ4m2Er1F2svOLmnXyZtQ4brcBz5uYWn+tAc6cCG8xuMceZkXfW38NFzPpkLBd+kei7xgsHoPfoQJrAMbpjwHRjhY0YrpGKN0yXN8NYdH/+6xDDdyG7MGD4DtnTxtdCvz/J6iIUh/cfGh5UZ6PAmfLOsj05WO5fP3CXNg3o1DfRMMS4DOCQsRd+wd/JwmhGghTWD4w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Converts the infiniband core umem code to use the vm_account structure so that pinned pages can be charged to the correct cgroup with account_pinned_vm(). Signed-off-by: Alistair Popple Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/core/umem.c | 16 ++++++---------- drivers/infiniband/core/umem_odp.c | 6 ++++++ include/rdma/ib_umem.h | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c index 755a9c5..479b7f0 100644 --- a/drivers/infiniband/core/umem.c +++ b/drivers/infiniband/core/umem.c @@ -149,8 +149,6 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, { struct ib_umem *umem; struct page **page_list; - unsigned long lock_limit; - unsigned long new_pinned; unsigned long cur_base; unsigned long dma_attr = 0; struct mm_struct *mm; @@ -186,6 +184,7 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, umem->writable = ib_access_writable(access); umem->owning_mm = mm = current->mm; mmgrab(mm); + vm_account_init_current(&umem->vm_account); page_list = (struct page **) __get_free_page(GFP_KERNEL); if (!page_list) { @@ -199,11 +198,7 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, goto out; } - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - new_pinned = atomic64_add_return(npages, &mm->pinned_vm); - if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) { - atomic64_sub(npages, &mm->pinned_vm); + if (vm_account_pinned(&umem->vm_account, npages)) { ret = -ENOMEM; goto out; } @@ -248,12 +243,13 @@ struct ib_umem *ib_umem_get(struct ib_device *device, unsigned long addr, umem_release: __ib_umem_release(device, umem, 0); - atomic64_sub(ib_umem_num_pages(umem), &mm->pinned_vm); + vm_unaccount_pinned(&umem->vm_account, ib_umem_num_pages(umem)); out: free_page((unsigned long) page_list); umem_kfree: if (ret) { mmdrop(umem->owning_mm); + vm_account_release(&umem->vm_account); kfree(umem); } return ret ? ERR_PTR(ret) : umem; @@ -275,8 +271,8 @@ void ib_umem_release(struct ib_umem *umem) __ib_umem_release(umem->ibdev, umem, 1); - atomic64_sub(ib_umem_num_pages(umem), &umem->owning_mm->pinned_vm); - mmdrop(umem->owning_mm); + vm_unaccount_pinned(&umem->vm_account, ib_umem_num_pages(umem)); + vm_account_release(&umem->vm_account); kfree(umem); } EXPORT_SYMBOL(ib_umem_release); diff --git a/drivers/infiniband/core/umem_odp.c b/drivers/infiniband/core/umem_odp.c index e9fa22d..4fbca3e 100644 --- a/drivers/infiniband/core/umem_odp.c +++ b/drivers/infiniband/core/umem_odp.c @@ -130,6 +130,7 @@ struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, umem->ibdev = device; umem->writable = ib_access_writable(access); umem->owning_mm = current->mm; + vm_account_init_current(&umem->vm_account); umem_odp->is_implicit_odp = 1; umem_odp->page_shift = PAGE_SHIFT; @@ -137,6 +138,7 @@ struct ib_umem_odp *ib_umem_odp_alloc_implicit(struct ib_device *device, ret = ib_init_umem_odp(umem_odp, NULL); if (ret) { put_pid(umem_odp->tgid); + vm_account_release(&umem->vm_account); kfree(umem_odp); return ERR_PTR(ret); } @@ -179,6 +181,7 @@ ib_umem_odp_alloc_child(struct ib_umem_odp *root, unsigned long addr, umem->address = addr; umem->writable = root->umem.writable; umem->owning_mm = root->umem.owning_mm; + umem->vm_account = root->umem.vm_account; odp_data->page_shift = PAGE_SHIFT; odp_data->notifier.ops = ops; @@ -239,6 +242,7 @@ struct ib_umem_odp *ib_umem_odp_get(struct ib_device *device, umem_odp->umem.address = addr; umem_odp->umem.writable = ib_access_writable(access); umem_odp->umem.owning_mm = current->mm; + vm_account_init_current(&umem_odp->umem.vm_account); umem_odp->notifier.ops = ops; umem_odp->page_shift = PAGE_SHIFT; @@ -255,6 +259,7 @@ struct ib_umem_odp *ib_umem_odp_get(struct ib_device *device, err_put_pid: put_pid(umem_odp->tgid); + vm_account_release(&umem_odp->umem.vm_account); kfree(umem_odp); return ERR_PTR(ret); } @@ -278,6 +283,7 @@ void ib_umem_odp_release(struct ib_umem_odp *umem_odp) kvfree(umem_odp->pfn_list); } put_pid(umem_odp->tgid); + vm_account_release(&umem_odp->umem.vm_account); kfree(umem_odp); } EXPORT_SYMBOL(ib_umem_odp_release); diff --git a/include/rdma/ib_umem.h b/include/rdma/ib_umem.h index 92a673c..065cd2c 100644 --- a/include/rdma/ib_umem.h +++ b/include/rdma/ib_umem.h @@ -10,6 +10,7 @@ #include #include #include +#include #include struct ib_ucontext; @@ -19,6 +20,7 @@ struct dma_buf_attach_ops; struct ib_umem { struct ib_device *ibdev; struct mm_struct *owning_mm; + struct vm_account vm_account; u64 iova; size_t length; unsigned long address; From patchwork Mon Feb 6 07:47:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129434 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 19F9AC61DA4 for ; Mon, 6 Feb 2023 07:48:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D4B66B007B; Mon, 6 Feb 2023 02:48:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 85C9A6B007D; Mon, 6 Feb 2023 02:48:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B0B56B007E; Mon, 6 Feb 2023 02:48:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 59A0F6B007B for ; Mon, 6 Feb 2023 02:48:53 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 30133140B49 for ; Mon, 6 Feb 2023 07:48:53 +0000 (UTC) X-FDA: 80436090546.12.B7E8ACA Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by imf15.hostedemail.com (Postfix) with ESMTP id 63942A000D for ; Mon, 6 Feb 2023 07:48:50 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VGMmwdNV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 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=1675669730; 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=dbbfhEFK8Y5g+MZUUcF50R2sJRtJy6ACgT2r4mlmBvQ=; b=q9BaU7citQ2CCQG7dyza7NK81Lzm1itGNb0sx4SnVpAuf9r+zwX71dCPnojjbMvTgjYgcN SEq2VIzOv8w19/26vcfwB8dUFZjD5vCbxpMfhHTKBxLLtoqJHg9gU6TzkOFxWBUrht/tL9 fBS4AYJZG1kDPqXl4NZJIh/vyLoxhsw= ARC-Authentication-Results: i=2; imf15.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VGMmwdNV; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf15.hostedemail.com: domain of apopple@nvidia.com designates 40.107.236.41 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=1675669730; a=rsa-sha256; cv=pass; b=Jm6dgaIwGMl9qjc7WVGzCamDaXLS84lMjbkAng6R+f4HyiDjq+37hABjDqo9n7e4b8zXZJ o/4QSLwjFECfDrOMdCLUcYbVqO5whkDdGY2Gnd8n1gimJuLwTGhmxg4TGMHMRI8aEO5etG 3ayvsX2FYegQy8bWzIEk0S9q71+IxTE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XN2jl/HppWe2WbAIMAP0hRU+gmLJ8m8azfsx4T2fHkNR61TRgEGZ44NeyHuSbxda6NryeHkfrWbq63Vxtp1zXYaIkorIk8m+cIHfVF9x+iK5W6jnd2ZbN6theKf2u8keLZvndkprY04XDl5viaWga8GkM/1JzZqbofc7wLr599/RxToXo8xHr6NDWknk9LomxzBcEFh+cZVmE+1Be2lwAAVUTxYMJ1bLDhlFFOHNhgV+87aFoNzJVqcta71Ay3wcfPnP0binqM8sLAYd6Jo4Wf9mZTVype8SzRHoY3kAiujeU+eQ0qnwTOUnpQMsEro88ABOrH6Ez5PXTwe2psLoGA== 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=dbbfhEFK8Y5g+MZUUcF50R2sJRtJy6ACgT2r4mlmBvQ=; b=mnTUGofFQWPhl3fcsDP3n2cRv3qzr1bRl0HVJ02Lxc8TzF93TRVBXbMXUU68lt4Tx648Gdq1bubNI8fKhBrukIS+8b4U5kyE+hCXuNUo2i0A/KSCacXQ2mbfIvpCFeplwLyBjmYs2CovgCqVnhgyzUUS+PXCHc95FcN0yXfM5WMh7IZPFXkxRsN0WCLgqRLn1F+PK3ALsDdLsqyMyAJDdIfUx3IFmGVSgnlEi0rcwPKFBrOoVCYn79FfvwcLrxEB3dmXdpSvi3sYHFG7tf7q/pVLJ3An8d+GxeuUV0VRKMVEWrhmRfPQmkN64dH2yug2qGlQZNdg9xzBtQ4jLPq3dQ== 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=dbbfhEFK8Y5g+MZUUcF50R2sJRtJy6ACgT2r4mlmBvQ=; b=VGMmwdNVNSN3VYOHfasKm4H2Vx/Wsl7VT43FSwx/C/VBjckm7Z0Ur3KvfO19Ogny3/QHdBpZR2TxH1cyjqViL8X1LvbN7064TIHNBeh5EjfdDvEfA9S3YswQnr4H1rsB9do+VmEPmu6pR+CKJrKAesYOh7UiZhgT6Xw1jeOcZwImkHzmAKb+GtfQH0O95kt1BZek0Szut/nml4gDoO4uPrTyJpYp8S8EaeFo1NPKt7hQA+eomC/TPpsGxux4huucOmRA290Iagg9Fe09yDrmQVpkKxf8qRnjTtgW/tFPs/ooArhZWI/x21/myiFVMKdhOB7I3OyX/xtuxucWY24OvQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by CY8PR12MB7097.namprd12.prod.outlook.com (2603:10b6:930:63::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 07:48:48 +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:48:48 +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 , Bernard Metzler , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [PATCH 05/19] RMDA/siw: Convert to use vm_account Date: Mon, 6 Feb 2023 18:47:42 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0180.ausprd01.prod.outlook.com (2603:10c6:10:52::24) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|CY8PR12MB7097:EE_ X-MS-Office365-Filtering-Correlation-Id: d1719398-e6af-44b3-bd37-08db081694c8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mEgnJ2byJYYRp5TDKz4QiINbbC95btTBNqenhr792EFCNVZ5vubMIiUg9pa5i8+JU4s6qxBvrlhVt62u/Zl0xs1+bEMXnhrKqnvck8WgN666G9HSZaXadccoWUbVd/7M5ctXxwfasjKap9ptYFuOcMnI8YWN7oJ9/3//X5c5lNJXiG02aE4Kb9/DLSlFMKNINOUdY71D44WUnVevEHmNItB9w0LfSU4UEynLrCz4RHyQyCupVnofIhNqzbqySBF4Px441wauBPi0JNCdYF/88QHoquXb+bfHXE+zJhh53sxPAtT6G3Rnjb3OY9Cy91M8Im96+qROqIa4V8TSSL7ZBEoFpw14soRTthjWO6wFthqoDpsDjzaXwsMScJkeOCwpnyOKvjj/I9D/Se1J1X/il9oow1t1+HtvqbvTtb6m5jZgJh+mKl96Or9M5Y9VQGnFTKAiLM5cMQr/h6B7Xex0+lGlc7+7ziT2i09YSsdW5CTYPtkUsLwIQ6/oBjJM0vQC3ZvG8QvkcNzbbw4Y6YLW2CbcfHtJ/Jh9jmkkGcIMc93pXD2LrFY4qtS4GYO+JuwRXmFJQuiHkUNI9oxN5kewX2Ez7C9QmUzbRRMy38siwC5FFdLW7tl0QR9JjSXHcQAyO4f+QOF/1jys2tDj8jYDeg== 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)(366004)(39860400002)(396003)(136003)(346002)(376002)(451199018)(38100700002)(36756003)(6506007)(6666004)(6512007)(26005)(186003)(478600001)(86362001)(6486002)(8676002)(66476007)(83380400001)(66946007)(2616005)(66556008)(4326008)(316002)(54906003)(7416002)(2906002)(8936002)(5660300002)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 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MPyE+B5XykMfwVyPLCAqypLzNwHL5BD/htEZg2Ea5XApVVFCgNlsWnYQ4xOfA4DEB5VDMjfrF4Lt96O8VZukD03QHEXxk6dXd8wN0YsgVpUsqCjZ48uYbpNFjGyUfffGy1YPE5l+vj7rW8C7Uno20VN+1ou1JIv/sBYR1gyxc7ZTm9KyPQ/rs7HcIaDkhQ7zms8yQ8VsD2Kil+OAftlXJsypAw/wYZvZAirWFGaOjaHmCsN/bdfw7mHn7VI11bFi88VhUklQFUkQrNAO3wE2xKKtBMuHxXyBA9lFXsQErduHwoHy3eRbW3gWQkchIDeHPEhEiHjPOosZvzogl+SLpx9Da5rHvqo73UsF6sUTYk43A9O/hfrur8OTrExrN3M0aFu6o9Ly+kXtRZi86zzMdLsH0Xnea1al5RwYUZ4vV7p5ypIGwmu30mR4zzu8kderGOKFxHzFycWeC9pp7KPnFInUaGgg9Qp4VcigyW/5yN2LOisgkIzHfeO0E+cLWwKuJ3YiGdRgLMDovtfbzmIS01cDomgzsAo0btV+7j5qIGD3lGr4L X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d1719398-e6af-44b3-bd37-08db081694c8 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:48:48.2637 (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: sPTS0ifI+wgKwzc8U+7q4QMN/tEOXb02+weR/NeNFNKlKfv+KJERpFtqaAe3R5M1A2XI8D6T5oQAMoc8J3A+vg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7097 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 63942A000D X-Rspam-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fEQAGVY/7a+R889sVik3FP8RS5MB2TMJxQZU0lAe3sdRO311qtSRNRVVUKoe/u7CcDZzNelhrbOojKrO8TVG/SpOeq0ssfDQdON6zahkh+NGwtQXmNie2BUwY86OJstunr/qhqi2fXl+HTDPY8Q1al9S+zmJJGFVj7bo0YOgG0HV8tgOQu8r/TLa5xIDL8YWuecH2XSTRJNjyavZ5q5ZpHFViYO82z+JG7Dy2yqXwVaujfNK+mbD6xgc2GJk1OoMZx6iV3ROOoQlNI4vnpjyar0YsutnXrQigZx3urv7rZd3voSeJZ0gRKTTT5hTKfInF5/t2Ir9g/rjEC5N28TcTHuKy67aFEjj/cYidEjWag12ftKOxCs/gwQZ1qkwm+LWp4G0gjeMnqZiDxBexZPz/lSzF9kFWLr6IUxXu4xyvv99g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert to using a vm_account structure to account pinned memory to both the mm and the pins cgroup. Signed-off-by: Alistair Popple Cc: Bernard Metzler Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Bernard Metzler --- drivers/infiniband/sw/siw/siw.h | 3 ++- drivers/infiniband/sw/siw/siw_mem.c | 21 +++++++-------------- drivers/infiniband/sw/siw/siw_verbs.c | 15 --------------- 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/drivers/infiniband/sw/siw/siw.h b/drivers/infiniband/sw/siw/siw.h index 2f3a9cd..6d4aabd 100644 --- a/drivers/infiniband/sw/siw/siw.h +++ b/drivers/infiniband/sw/siw/siw.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "iwarp.h" @@ -124,7 +125,7 @@ struct siw_umem { int num_pages; bool writable; u64 fp_addr; /* First page base address */ - struct mm_struct *owning_mm; + struct vm_account vm_account; }; struct siw_pble { diff --git a/drivers/infiniband/sw/siw/siw_mem.c b/drivers/infiniband/sw/siw/siw_mem.c index f51ab2c..be90121 100644 --- a/drivers/infiniband/sw/siw/siw_mem.c +++ b/drivers/infiniband/sw/siw/siw_mem.c @@ -68,7 +68,6 @@ static void siw_free_plist(struct siw_page_chunk *chunk, int num_pages, void siw_umem_release(struct siw_umem *umem, bool dirty) { - struct mm_struct *mm_s = umem->owning_mm; int i, num_pages = umem->num_pages; for (i = 0; num_pages; i++) { @@ -79,9 +78,9 @@ void siw_umem_release(struct siw_umem *umem, bool dirty) kfree(umem->page_chunk[i].plist); num_pages -= to_free; } - atomic64_sub(umem->num_pages, &mm_s->pinned_vm); + vm_unaccount_pinned(&umem->vm_account, umem->num_pages); + vm_account_release(&umem->vm_account); - mmdrop(mm_s); kfree(umem->page_chunk); kfree(umem); } @@ -365,9 +364,7 @@ struct siw_pbl *siw_pbl_alloc(u32 num_buf) struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) { struct siw_umem *umem; - struct mm_struct *mm_s; u64 first_page_va; - unsigned long mlock_limit; unsigned int foll_flags = FOLL_LONGTERM; int num_pages, num_chunks, i, rv = 0; @@ -385,20 +382,16 @@ struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) if (!umem) return ERR_PTR(-ENOMEM); - mm_s = current->mm; - umem->owning_mm = mm_s; umem->writable = writable; - mmgrab(mm_s); + vm_account_init_current(&umem->vm_account); if (writable) foll_flags |= FOLL_WRITE; - mmap_read_lock(mm_s); + mmap_read_lock(current->mm); - mlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if (atomic64_add_return(num_pages, &mm_s->pinned_vm) > mlock_limit) { + if (vm_account_pinned(&umem->vm_account, num_pages)) { rv = -ENOMEM; goto out_sem_up; } @@ -434,14 +427,14 @@ struct siw_umem *siw_umem_get(u64 start, u64 len, bool writable) } } out_sem_up: - mmap_read_unlock(mm_s); + mmap_read_unlock(current->mm); if (rv > 0) return umem; /* Adjust accounting for pages not pinned */ if (num_pages) - atomic64_sub(num_pages, &mm_s->pinned_vm); + vm_unaccount_pinned(&umem->vm_account, num_pages); siw_umem_release(umem, false); diff --git a/drivers/infiniband/sw/siw/siw_verbs.c b/drivers/infiniband/sw/siw/siw_verbs.c index 906fde1..8fab009 100644 --- a/drivers/infiniband/sw/siw/siw_verbs.c +++ b/drivers/infiniband/sw/siw/siw_verbs.c @@ -1321,8 +1321,6 @@ struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len, struct siw_umem *umem = NULL; struct siw_ureq_reg_mr ureq; struct siw_device *sdev = to_siw_dev(pd->device); - - unsigned long mem_limit = rlimit(RLIMIT_MEMLOCK); int rv; siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n", @@ -1338,19 +1336,6 @@ struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len, rv = -EINVAL; goto err_out; } - if (mem_limit != RLIM_INFINITY) { - unsigned long num_pages = - (PAGE_ALIGN(len + (start & ~PAGE_MASK))) >> PAGE_SHIFT; - mem_limit >>= PAGE_SHIFT; - - if (num_pages > mem_limit - current->mm->locked_vm) { - siw_dbg_pd(pd, "pages req %lu, max %lu, lock %lu\n", - num_pages, mem_limit, - current->mm->locked_vm); - rv = -ENOMEM; - goto err_out; - } - } umem = siw_umem_get(start, len, ib_access_writable(rights)); if (IS_ERR(umem)) { rv = PTR_ERR(umem); From patchwork Mon Feb 6 07:47:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129442 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 41170C61DA4 for ; Mon, 6 Feb 2023 07:49:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A33426B007D; Mon, 6 Feb 2023 02:49:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BC706B007E; Mon, 6 Feb 2023 02:49:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E92E6B0080; Mon, 6 Feb 2023 02:49:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 66DB96B007D for ; Mon, 6 Feb 2023 02:49:00 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 46EE01407F1 for ; Mon, 6 Feb 2023 07:49:00 +0000 (UTC) X-FDA: 80436090840.12.7861178 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2058.outbound.protection.outlook.com [40.107.95.58]) by imf29.hostedemail.com (Postfix) with ESMTP id 70BE4120007 for ; Mon, 6 Feb 2023 07:48:56 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="aw/rHJr2"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669736; 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=i/0d5FeS1Bthw7AbdBW2nD+WSiMSiecdaEvA9kRz1hA=; b=D1JgrYkQkn/7zTY1+3p7auIkWyBLTg+mTYvOovQHoiqDqqx17kfmN1biIr4Iyk9jzOTccw X34mkeO5WI9Dw3++DkkQScsnrfoRVeZoClKwvcq9BCrLDBUacWmFTh/0zsZBYe8nUmx7GX IJChd9OZkJ8B6l4PhCgC1tKMfZuBOEo= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="aw/rHJr2"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669736; a=rsa-sha256; cv=pass; b=o8dMnGdAqfcl+CrSYEOAuO43djgB9caqoKNFbSuKUi/GrY+b7QPUH2XnSrXonujwO4Dyu2 NjQgE9X/jK0DQCZBc6nwxyJab9woIju/TzD3pUNUafME961gRIsQjiHvIEl6quzO8tyI4X vEbqh6pIi62jlvm3iRBrfavFO2Hr/pQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GKJrhrOon4wlA5wPX4OgjWNUpE9E+c1jOh93c/zCGBbeLn07FIVt4YX4sE9IqemJ8c1yi4CZPKt0xXD7GRr6bnwCbvQWGE+romi83aNMzkn7FIwS+6DIIP0gHwBnU9MwQ8bkLRWWA4nQYrLmRKRG+pfpB+4QqxHU775Gyy7ET3SyaZAFYL92xfkMtWpb6jcU3lK08/z17bV/BE1gESDWEn3n+MGEhwrCo1e6WRyetwzNmxLTL4TgFZ+6pb4TA3uYMh5xj/X7wjH3ywcPiAfAN5pkKKef7c8jxhYV074amLxtrWlO8Oo7lBZTDIncmGl//0COup14QuwThztkNPhh0w== 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=i/0d5FeS1Bthw7AbdBW2nD+WSiMSiecdaEvA9kRz1hA=; b=kkoj2JaYpXi8X54ed9xTPQErejMk9izMe09As2gSABGWKF2gbe0vkJ0/Yf8hdBVRkMRvK4ihURt2xIJ28eH3vPgVFvnrR2UdcNpkVwaz5M3P01HK8iPs5/AoBawMFuxWHVlArSOFCurC0Fxnl79VXpKr0khtqxQ7P569snIGkstxE8cpdXfQEPjJH75ydxqcM0bpy9Zi9IzhkralT+QLsYB0hRvdxE8n1+0qHCGiLAcWYa0uEpwgArGZqHVLGF6mYOTba31eVPmS5Q7SyyRFG1OPwWMwawXjHJTio1hBoh15fySLC+JbauswPUcvpifky5+T+wFUQpR2VMJF9oyJuw== 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=i/0d5FeS1Bthw7AbdBW2nD+WSiMSiecdaEvA9kRz1hA=; b=aw/rHJr2ROPaFuGDV679uXnmMAVYxwJKSnQkyoLcw8StHbziYVi6NRsPvM7sHHS79m9ckuv3d6jk6AyVcrVplCXNH2/eOIUyD1MHXph6KJdLz3w+i6r1thkA4iEvEh/pOzTSma4LyfpC5NvgHsNmEyJ4/SJHbq+gRw4rcuRP1l7UPCrbfN9Puxz7MpQE66lA5G+iGGp5+qml0eX5+gb0lKtSyvK9lgOjo2gm/o/4Utv+ZrX/BBBV7wNZvVMxRJPzU+2mEOXR43REOdP+pgdN1f+k4vl/C/zk4yDCxxgb78jXMhY66NgnALKtjhQg2MCeNVwe978L/CV7THEfMW2Ibw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM4PR12MB5118.namprd12.prod.outlook.com (2603:10b6:5:391::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.31; Mon, 6 Feb 2023 07:48:54 +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:48: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 , Christian Benvenuti , Nelson Escobar , Jason Gunthorpe , Leon Romanovsky , linux-rdma@vger.kernel.org Subject: [PATCH 06/19] RDMA/usnic: convert to use vm_account Date: Mon, 6 Feb 2023 18:47:43 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYYP282CA0010.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:b4::20) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DM4PR12MB5118:EE_ X-MS-Office365-Filtering-Correlation-Id: e7cfeabc-8a1a-4d0c-50c3-08db081697b5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3Em9qIZeJCfdgX0Lzj0VhOFmFq2EkCQRhWl4EGeJW5wv581NiTeB3qWDj/2KkrcOuqtnmFNpvGMPFFyDafJpKhr+yt/lAAuBJTJ/0EzEpVBbFXaEKjBPdT8BU15HuRumyMEkfx0BbYoM13wDthnNA6deYNF03KPugeUqFR/I0H/AkMobrm96K5PVwrC9EMyq3fWTwYdoaiC5isDXTd+NxsDCazGyi2fhfraxkB1aBbOL3n/bla6jM91LeGZ1TBDL49o9MzdNaU1Jk3xGfusYQzrtK2Ich94VqN9la43JjIqT7gGPSFwJZreNCddsabmeeOgh/CDMOwN5NWo6GocFPptuxXA/Y4dXehbxWHiDOFfKWaTiOXLaceClgOu+74ccrL7CuMW7EPomjUDZ0Wo1YO6Wcp3Xn1l66TW7tZ+PuPx/Zmj0jdjbPhsyIa2Jshmgi1/ED3XyYzgPOOc11ED/Od6E5UhrWw6Eb3yKNrFiSrRsIg/bTzQfg4DYVtF2G6Y/+eaXfatkPWJCVHbvNABN0QB2Wmz1KVr3xq+sAjruExCxNLGNfxo8qG4bAh+feJBni5/mNiWzqM2sHFV+LC0elXXDi2YMCYt42jrBcVAnIayEzChmu5f4Qr1uBNlx+a5o0wDkbisjqLX1cmwX5jrDrw== 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)(39860400002)(366004)(396003)(346002)(136003)(451199018)(66946007)(66476007)(4326008)(41300700001)(8676002)(38100700002)(8936002)(6506007)(6512007)(26005)(316002)(54906003)(7416002)(66556008)(6666004)(5660300002)(478600001)(6486002)(83380400001)(186003)(2616005)(86362001)(36756003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gu/4IBsNj6nwZ9YzlHu6d9380Bnoq2pXo6uSCdgiZzrOqndXqixi020w2Sj8cE73xa6O2iU1HKpzwDh7IPVhsmrfRVv9scORa8lbM05alj5/eGk6hqAA4ZXvFTyZZQMzmJ443F58pP6X/ksLqkOGBmofIqLQui79wP0fHuFMPFsitEYe5rEV4UU3q0pNDCxHIH9Vdox5/A7GhucomumQ0UTZxAsMVK2o/uPMY2F8EFm4BzAa11Eq86IK/V3qatq/7m6oWNg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ir430Hgti9Qqx0O/bMFXh/BP7iYGkrRvFHZwnVjc8/Rw5CkTjydK+ViI/Eq9uOKXHN9JVwy1sDvhWNozutF1xH7aij8vS2OubOPV2i27wRLE3iRPClIQRlMD8+SXng7ECiMuAy4LQHDzIaDaJsjRo+QihH0UhwiEacDtR9n4maS6wB57XlSwBGX25NBNqhWgZS0X8+09xky6KnQAH+A26Uxt6nR+L7sCb79CYN8i2ZJFS2XyA2+/hIbKVFqZvv0Om X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e7cfeabc-8a1a-4d0c-50c3-08db081697b5 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:48:53.1852 (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: iAeyFeiJEkHUn1ZeyVxeCBngdX4GNzhLl6PF7Zc0nYgz31KSrcPpqt+6HXJvWuZhTRt7P2ZNwHdFXo6uVF7lsg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5118 X-Rspamd-Queue-Id: 70BE4120007 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: if3k1mgrdx4xdmd1cudayjz9tep19tgy X-HE-Tag: 1675669736-419507 X-HE-Meta: U2FsdGVkX19lrBhj6LOnq2afUUR4i/EWqbJN9ylWvkB2QsOlEjY+ZaqzPYO5q8L0VxW+0++Bg/SSkwLzUAeFCVKYzuqxbUFj9oPddPHiP0P2Cg1W02qFZsaSbmPqeZsnYpKjksXyDKU/v9Pl73MNax/nzlmZy9rK05po1Tb8f507sy3pRdNFShL5ELsuf3H59PHh3SvlPvK4kp+zEknuf3t8XH/WAzGk4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26Ic3iekfDGyzvQPjmelG+K4aqBQwOLvISx2VV4aBqUqj5tV6rWbVcfQPbvf7me/skplQYMXBfvjrv+ssBhkeSLR3ySG33UNuTqiT7t/ZLHjWNIR1mQotzUJHS3KX7dW29Y0pWca9jfCMA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert to using a vm_account structure to account pinned memory to both the mm and the pins cgroup. Signed-off-by: Alistair Popple Cc: Christian Benvenuti Cc: Nelson Escobar Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/hw/usnic/usnic_uiom.c | 14 ++++++-------- drivers/infiniband/hw/usnic/usnic_uiom.h | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.c b/drivers/infiniband/hw/usnic/usnic_uiom.c index c301b3b..8952ee5 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.c +++ b/drivers/infiniband/hw/usnic/usnic_uiom.c @@ -89,8 +89,6 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, struct page **page_list; struct scatterlist *sg; struct usnic_uiom_chunk *chunk; - unsigned long locked; - unsigned long lock_limit; unsigned long cur_base; unsigned long npages; int ret; @@ -123,10 +121,9 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, uiomr->owning_mm = mm = current->mm; mmap_read_lock(mm); - locked = atomic64_add_return(npages, ¤t->mm->pinned_vm); - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) { + vm_account_init_current(&uiomr->vm_account); + if (vm_account_pinned(&uiomr->vm_account, npages)) { + npages = 0; ret = -ENOMEM; goto out; } @@ -178,7 +175,8 @@ static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, out: if (ret < 0) { usnic_uiom_put_pages(chunk_list, 0); - atomic64_sub(npages, ¤t->mm->pinned_vm); + vm_unaccount_pinned(&uiomr->vm_account, npages); + vm_account_release(&uiomr->vm_account); } else mmgrab(uiomr->owning_mm); @@ -430,7 +428,7 @@ void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr) { __usnic_uiom_reg_release(uiomr->pd, uiomr, 1); - atomic64_sub(usnic_uiom_num_pages(uiomr), &uiomr->owning_mm->pinned_vm); + vm_unaccount_pinned(&uiomr->vm_account, usnic_uiom_num_pages(uiomr)); __usnic_uiom_release_tail(uiomr); } diff --git a/drivers/infiniband/hw/usnic/usnic_uiom.h b/drivers/infiniband/hw/usnic/usnic_uiom.h index 5a9acf9..5238d06 100644 --- a/drivers/infiniband/hw/usnic/usnic_uiom.h +++ b/drivers/infiniband/hw/usnic/usnic_uiom.h @@ -36,6 +36,7 @@ #include #include +#include #include "usnic_uiom_interval_tree.h" @@ -72,6 +73,7 @@ struct usnic_uiom_reg { struct list_head chunk_list; struct work_struct work; struct mm_struct *owning_mm; + struct vm_account vm_account; }; struct usnic_uiom_chunk { From patchwork Mon Feb 6 07:47:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129443 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 D5E38C61DA4 for ; Mon, 6 Feb 2023 07:49:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 750156B007E; Mon, 6 Feb 2023 02:49:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D65C6B0080; Mon, 6 Feb 2023 02:49:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 501F66B0081; Mon, 6 Feb 2023 02:49:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 386BD6B007E for ; Mon, 6 Feb 2023 02:49:05 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 129101A0AF7 for ; Mon, 6 Feb 2023 07:49:05 +0000 (UTC) X-FDA: 80436091050.20.41A889E Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2072.outbound.protection.outlook.com [40.107.95.72]) by imf19.hostedemail.com (Postfix) with ESMTP id 24BE41A0003 for ; Mon, 6 Feb 2023 07:49:01 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=HdDvwTln; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.72 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669742; 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=9EJyBunn0zT5d/t0YMoiL/ukwvRArAw0I+071Y/Pm2k=; b=ZsaYBI7vINnt2pDD+mI7sv21iqTfBw5ypTK83DNGV1BbFscXSRU4QYuTbRIuW6zqrRrwWQ XvsRePqDVre6bZy7Deet5IgL1C+VD9NWWcnAIZTQ2ZUmhdLVi12+tHWdOlQhaY9fVZ3LWj rAUOOy9OpmzUxS2x5Vi7Z+wo8gqsQRI= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=HdDvwTln; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.72 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669742; a=rsa-sha256; cv=pass; b=a8txrrdhpdsnfGJa9WbCsh0kpBOhnxJihJg2LnkmFvrVT6f8Sl9atkDdcCM2JVufXiOIet NVFasrX1QJRizl29l12111yRuZzd7sjX9seb6y/9s4nJ+x679BkkfCLeWyXIS5z2s9Fhg/ Hk8RJn1sj9bNOrCQc+0ZFBIoYalZTYw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FTNiPCF3AmyP3MYgZuTvz3MSn9Uq9zP3GgybfDDeC3e3lCUyL0UGqIWzr+L5C71E7bgB6hMmGqYga3WpRF56apR1HE3eRZlWxASaqHKXKdrBokTb7Ozg/gO+l2MC4Ld5RljeihEUYBK9IM+kWk+geqtPeyJJkTgdOKsQKcPlNU2CJzCmfZD3HRuO5byjQ3bAfKjWgC2LTdbxt81fe8oUrE5q0F7fKIWmJ4GxjfDOgl2M5gLeYH+QNuhWmmTTyxMbTmy4wRn+SpAGi/WUyBWrFnLE3DX+YUX78/SNDZ+BGjfT8chJUx110ciY+Y5Djwrz6rWHr6YPWf6uMI4gQbl1Cw== 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=9EJyBunn0zT5d/t0YMoiL/ukwvRArAw0I+071Y/Pm2k=; b=VDyB2Hv2n45OEPJ8dQ9xlV4KOt3ZfMypAsGfaul0is5oM+cezUOq6+OHZ0fUJovFJF1q4cWJJgOKxMxDGFhGamJggn1ODOBfp0mEGCjfNHnbqtTIbsc94O42ORC7WJJ7pMeTSnqikVAvJ7uTW+vnSfpNgvtObwjqcPrr7oR96GErwF89fjgxsXW3FI0ibBHxFTl55wvrb8NvlK1yP1sgcni4B+6jPZDnPel+17kjARloof10xce5QhXn5ojJwHKo2qYOUHXUIGykKRujWbmldQPbSl2WPmS87CwCt2Yj9P0m2AuSYfTMBu9F2iZ2T8mQ5lZxxGJTdwndM+Tm4KVQJQ== 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=9EJyBunn0zT5d/t0YMoiL/ukwvRArAw0I+071Y/Pm2k=; b=HdDvwTlno/2TJFtEwBIiS5c+AD1oEMvYqSxurzNyHD01OoIabNHwyzh/mIkTTgM9RtXxdonmgc5LImTBneN3gImrJwxvCtO3prL0TKaAWcP2MQIz0mjULA2GezhsbkxUfHouo9nN2zMGi79arJIK2cKCP0k29UU3oY1Vmesh6XMNMyXzk6ZdTnewXJuiGT0l1P6WEaPvoOtgok2ByKpKE+ZvFtpqZ8PxHNcfPKVqlAxX/8758AmEMs0krkoG0pqz0I6A0XwdxrMuKnjldSim9waR2MPiOJ0ZDre6eK3KkIk9o3QjRm4x6CRM0eqLykse91XKNIZp1mbef0MmDS5IEA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM4PR12MB5118.namprd12.prod.outlook.com (2603:10b6:5:391::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.31; Mon, 6 Feb 2023 07:49:00 +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:00 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, "Daniel P . Berrange" , Alex Williamson , Alistair Popple , Cornelia Huck , kvm@vger.kernel.org Subject: [PATCH 07/19] vfio/type1: Charge pinned pages to pinned_vm instead of locked_vm Date: Mon, 6 Feb 2023 18:47:44 +1100 Message-Id: <60d8812272042f497b8d661f031f2cb593370b84.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0191.ausprd01.prod.outlook.com (2603:10c6:10:52::35) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DM4PR12MB5118:EE_ X-MS-Office365-Filtering-Correlation-Id: a122e17a-0e75-495d-a900-08db08169bd0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: D19c20F/3H5aBZUJtRuOisDUPLPCb3g6LkxaoePLUkkit594ylhxEln8lwG/d82+XWI05rsnfmCDpC1vsCuFjbdq0T+KCv+KHNH3Z1aAqkjU33XcFrBUQZbMEeFQgtvkBiGTeIJSKIQ2MddZlxG3YfOmSew5kvepMZq48suLfmTQwBEwo33I70s+Ja7PUUn3QNSE2FUThE0XFwbxgUaMIVe9GUF7NxVwc0qy2wj7pQXxrzDJgUHC0Q/JBTDXEsmthnznbrRHDiwJWaA+aWWrSoIXSx0VRQWGcVYheLkGBLu+xXeyIC2Gw18dBemMc1lxOq9jPC/DEAWZgqE7doc79lVQdyjo3KWLVWjm0JFbRAuXGheBNJAPf+5889LNCR5KX85ktUrTfOcg9QmjlEGFM6ixS39AqYugLMq7WkqF9RollRnLkbbzSyyHvE7kVodm3SJcWO9ptdE4zFYUWLipDugvSD+eqj9yI9v3B3/ox962XFxUFNzLjsjNCmQByB6eqtA1ZuSgg2tVdvGMkg9/RJlmsGzydh7de7yXSi9TUo5rfcq4YSgkGmaVEU3CzfVio1l9S/WGrRARhBlmNFsOVmZEcnOu6RLIM+iasIdbBDJLU4TfVxUJv/FRYQK+cf6Yk+LM9OBZtSt76bhD3O/kNw== 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)(39860400002)(366004)(396003)(346002)(136003)(451199018)(66946007)(66476007)(4326008)(41300700001)(8676002)(38100700002)(8936002)(6506007)(6512007)(26005)(316002)(54906003)(7416002)(66556008)(6666004)(5660300002)(478600001)(6486002)(83380400001)(186003)(2616005)(86362001)(36756003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7zc1D3qcVz4pASVKaNOAYrrUtV515FbAbdEHWZkkbDHYjdqtKw0XPemXYJSYwcfzLz2N9qbtXnZJfs0tBfDB3GSEH1wVwNPZ1X6UY1BJ5KrPEplmF0HpxUZPsWX/gkHfhPPhl/FvT4t/8RXEzjCW9utzJPt1FSXPpNFy0wJvExmYLYGZliTc1vfC87bBfJ8P/upzU6ZwIuW0LWEBZ0TU7hNJQUK2ux5fpkRiux5QqH1EpPELod6+8+qHoXHKoWqjCAOCNFCIiM5F3sMg3f35sg62LsokIe5xTlMQWHSYlIOtv5HNIHWZaLJrqiPqD5GHCUJMUoruk4gxNmPkdWy4YTA91t2WFJajEHXQchyNePxdN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1/nduNR+t2xGPB8cOeXF132uSMdDue+oaGLwGRzbK4ltbp+RExjC37GcxTHoXk5J2BXSYY7x4hlNeGz425lHvovqNcU2nJ4s8/HNl3flko8QKgwAAa8P2g/ug2lOCRA/95k2JHR2Kid1U X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a122e17a-0e75-495d-a900-08db08169bd0 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:00.0753 (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: wp+R7my8ypqqhhSOfLO+TVLqtpDWGatzGK9uJJC/Lw68ZvcGUlzUXp6UMcD/A7fO22cL5YxY86EhX2g6PtdhlQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5118 X-Rspamd-Queue-Id: 24BE41A0003 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: mpw4eemo4bmccgxw7ohuc5d4n77b9kw5 X-HE-Tag: 1675669741-50464 X-HE-Meta: U2FsdGVkX18Yzm0yBmHkZJyII9edFb1jIsZEXI2v8PcV+P1+UENYJXwMNkCbjckpFstaGKtPS2yAf9+P+2kFdSMcIb7yT5FEKvSa4QcFKQnA9ark5fk+7rn7087S8xSsg3qsq1hCY/jTBlXH+9kZfiA8KHZCgfrogSSgkyrUbsQ+v1DSnalHuFEjodBXrpDsJpCZOTeiVcNS/RtH2FudaZQIZDKZByEzJ9lKzVArDg6Tm8IXj41iQmEXdaRChGiKXZ1fKc2uUjrT6MkK1xfNigePoClLPvWZ2y8ENUaN0EIjYx++vo4Ce/nnTLFvrYjRjtHuktzi4/f/BQ9t60QxGwOCr3G43uDxXXOG8l8ibph6JIrb+7vi5yKp3S5dhfYLNIbZvq+kqr4ccAo19ixI8gpB4369pYAwpQIaJijyO9LzaShtW99QwnRgcx7sP9u/gI9CxJWcP/hGEgU4i2w8hstnB2ppMWIoOdg3sNejJgkqkO9Xl/PyL3jfSJ5vLxRILIFYBavF5c11FHLotiKlJ0WmVzRkDNBJBTVtC8tM20eq5Q2kbFjVAISoKoUgvKYg7ZvYCKDAWJbKD7HHziwXSy9L9d+73B+Y9qX4fAOm5xP+t0twDBuNUVuwQ0AuhyullGjPwKBthIk495nq1HNKxT36AqaItTu5GB0L0Xs8xDJDwRuAJoaN7owHJckcQtSVvzoKrB6hK65F8qLOlS18u4TUInB9XPc5I0LHI8puG8wyrOJXP9pIj641fdPHsnQHzB9J7VS4GtiNFivmJFkqFtKsm9gMvw5K4oZyryF53JbyM6hIZTQa0FE7L1Qrymu9C4F9q3kko+cIeBdXJWG7PpAZWye+yusHPPTuSI+D4FQOWboLcPW0WK1tL9nvbK/txB62kreuTNUWKPSvd7g89rnnRdTVLXyDsE4mM90IkWabzffUWsHqp+CrQOfPZVXFiFgi4XgOecKMIg0uZOs erJssOTZ XqaDX6fS6pz4/q0h3pVW55gXO3vZBPAaNuyrMYq7Ypip74psSVrOcBKSggD9L2kX62+MNPO6YYJgFVZQ0BH/eupD+BjNFsLNXPUCybnYwPceuBZVpx7fpeA1vOrUohLMNXtQrVkSWLt5xgSSVqPyW5QnU/Nsy6qhbZvQQ4hK7rxEWh5ZI4EHLlCl1l+dy31fvdIVTOAu8IlHqrbzrCeQ6DKDLoWsl10OlyfuA/XymTxTjSxb1vr2GHscMhDSlT8QDAMA1ET3qpqFFMfnOUi5wcPsTJvxb4Rrkvr8pD3XwWLpKeRZglgLER4LcTR80CFKDYTTyAUqt+yFCwcocLV3Qbh5svZQuvkYOSdVOacydtA96ytxVrFpjKD9bgQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This switches the charge to pinned_vm to be consistent with other drivers that pin pages with FOLL_LONGTERM. It also allows the use of the vm_account helper struct which makes a future change to implement cgroup accounting of pinned pages easier to implement as that requires a reference to the cgroup to be maintained. Signed-off-by: Alistair Popple Cc: Alex Williamson Cc: Cornelia Huck Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/vfio/vfio_iommu_type1.c | 60 +++++++++------------------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 23c24fe..a3957b8 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -38,6 +38,7 @@ #include #include #include +#include #include "vfio.h" #define DRIVER_VERSION "0.2" @@ -95,11 +96,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 +413,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 +691,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 +712,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 +731,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 +755,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 +778,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 +806,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 +894,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 +1135,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 +1647,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 +2372,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 Mon Feb 6 07:47:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129444 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 63136C05027 for ; Mon, 6 Feb 2023 07:49:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3C276B0080; Mon, 6 Feb 2023 02:49:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC54A6B0081; Mon, 6 Feb 2023 02:49:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF1816B0082; Mon, 6 Feb 2023 02:49:12 -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 B9F3D6B0080 for ; Mon, 6 Feb 2023 02:49:12 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F12631C61A2 for ; Mon, 6 Feb 2023 07:49:11 +0000 (UTC) X-FDA: 80436091302.12.46DBA55 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2042.outbound.protection.outlook.com [40.107.95.42]) by imf19.hostedemail.com (Postfix) with ESMTP id 2F0D71A0011 for ; Mon, 6 Feb 2023 07:49:09 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VT3h3Buu; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.42 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669749; 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=4aNGQ6CEFF/Iwo4DWZWCLfPBaTkYJe6nei2IWX2bVLk=; b=6hJDhpCd32t/sc/DUlKxdkdtIDah5KkbUdQMWJK+YDiHsFQpvqTMY/A6vWp27bS1JdfWFa mHLS4DWhJAE3ptWwsuKkva/gCmXRxWMgq/h26xxgYLZoJtLKLhw4t1tUygSEk4F9XroNQh KxiR5FRMVQbDoCw42EkRo0we3K7Q080= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=VT3h3Buu; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.42 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669749; a=rsa-sha256; cv=pass; b=LIXzMbVeQP2J8IzqGmeQVgph1hZgRX54An4MJHglwM5HV8VfmLEXrAWZdhWp3B/Erjakvx 0h/zaS7RC7R1HgBs/V5gFbRWBx4H/qjn10MGa7zMPnjhq49b2m172wSovN0e3uTi39xXPl wOR5o8+R06mQ13oqIlcTg9/w52tbybk= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ScbFAXZwDSHDQYWfo+yBKa5EfS66fkePVN0KUmOunWodLhHCV4ULVvfwNauI0zQ2eBvnik0jEi90CMzWz5DR1c6Cz2v8NQaBE+tWBt7nr4z0CWa10xiznBdgp1Nm+k1z4xDUJ7ye12qJ7T+J4cEtfuMMrxPdgZ3XbBqw6GRxD/ZwtvmU5PvrZGd1TDzht3yybqzIY3WAfqqINM5HegVLwJ+xrvke7Cb//NFdGo4yyK1cll0HqzKuL5jQ3bDL7w+1MLnYchBhYmlkXkwOAb1VEKqP6k55wRdeIn1vd7jSMr0Otg5wbt/EgYcaeFtLjUUsCOnTXcifHQvh3wCKcJH/mA== 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=4aNGQ6CEFF/Iwo4DWZWCLfPBaTkYJe6nei2IWX2bVLk=; b=LqvQqbW1VJRiXVEeYE2GmDfVFnPTJiQFDnWiyaE2fKtllJZNL8ZrjRiGcS+1GgtroJtApXou7U1szAKFuscrS8aTGqqBt1H1R4fte3aXwMG2Fm8ZpDmj7WmM3I542Qd5Itnh10o4X0h/BC6C2Dke/IswXSCFqovNS6UHd3FJwaDpvMHShncjv/gUFDEsj+ZCtlv29xxAX+8YqNctu7D42Y8wWdhoKx2wFvKSnwhCzUNWDZgMSg5ZTDnhECuel5n0D2Z6CYfCro/c3uCQnvcaAwRFbg2s7k6sgVTccfZQlbemI34922wSM58l57WYsgNnbpIEbY1eC4nSpEToU8vadA== 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=4aNGQ6CEFF/Iwo4DWZWCLfPBaTkYJe6nei2IWX2bVLk=; b=VT3h3BuuFakyBa1tv3zMLWgdy0gFVM4xRbhSGmYmbGmqzVcCtyci/LlF1QBXJUbYcfqQfRAGEzvCGyS97l5/O8gteE7GX0CdtPbs52p6BURnxvnjHJspqYOjLDaThTCu4IvpaqqjDxhkgNVXSsp9lkSnDOTmiGW5EtiOkLLfcHwq6/fZSwaW5ITif9x+5mFkec0S+WcCxKzehbDCIKbbn/BUXbT/NNndSJ4xwCA2g+piUzTPeaOveLcnKjI/epZ6yyBYT86+NrBi/q8u9ZzZfezDPbPUSCaxiY05/P+coJEKaaS0S864sYfyKsSPOyxFPjBpmlmG4Jslgofey51zyQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM4PR12MB5118.namprd12.prod.outlook.com (2603:10b6:5:391::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.31; Mon, 6 Feb 2023 07:49:07 +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:07 +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 , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Cornelia Huck , Alexey Kardashevskiy , linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org Subject: [PATCH 08/19] vfio/spapr_tce: Convert accounting to pinned_vm Date: Mon, 6 Feb 2023 18:47:45 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYXPR01CA0088.ausprd01.prod.outlook.com (2603:10c6:0:2e::21) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DM4PR12MB5118:EE_ X-MS-Office365-Filtering-Correlation-Id: d4a9ea4d-62d6-4d24-e89f-08db0816a035 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iWUpf7QiSNpZ/BcD+AET9d7eIgcFareDzNOWhk99Dr/Ree6FI7pWqkP+D6TDXFKgY8TZ5XQ+RaPznI/kT55B3NJLbNkZrC6XyIUQ+uS+KzEPaNXGt0SPCwRVLF/egr6P5SexkJX9JwUJcKB6DKsLauOBEqRBUrgsWkSdBZRL0xisfwyTYcfgT4aUxOD6PveYHjh05ByLg2KqTXLbEZegTendZQWWSRguwakP8Pu4pSc7oSAKtf47yRVRj7sZvUKxHjmIEpwt1UXuONjAYH/+WTs8eDQTKj0+Oc96d5UOKXKIDxeYnAgNSeyKs3qWuwVmGdPoEdfHE8Bc0z2JrhYs44Z+51tYalcIMwPp2JWpLdeLtopMhcyR7CxIoEn/6FV/6Jp1+MQ5ymTSs91yipbEPNSwq00AGa1/MCqsZ+UT+zC3FmhhkdG6TjvRt9Q5e3Uj6xOskk3CW2y20+iqlSdM8liDU7ATBa+RDMqKOuczBmU5xlRR+4UIF1MlbNhlq2d2zlO9AF0Xat+u3C++9xus7by4OHqgYNtrtBtz723yxLGYW/72hska2T12DgReKGk+zTVqtepdD92mhiMdFY6VpIiUoEQN5D6+OhZR/0iCP0csDkfh2HsLjhte2EcaA2JjFmeXrzRdyu3g3649c31Alw== 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)(39860400002)(366004)(396003)(346002)(136003)(451199018)(66946007)(66476007)(4326008)(41300700001)(8676002)(38100700002)(8936002)(6506007)(6512007)(26005)(316002)(54906003)(7416002)(66556008)(6666004)(5660300002)(478600001)(6486002)(83380400001)(15650500001)(186003)(2616005)(86362001)(36756003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: J1lKquI9iAaemJegDUiKMox/qBsp+Xu2+DY++QRpmFu2QoWhovc9ib2zZo1G0eBx99eI1o60+2qoVPxEZsYAur9+TbUbor5EWDFYAuP2q7YyF9B6C64YH/301qDJdftQOmSC9O/wYd2RBUvPyPAtIjIj+4zEkJoFQcRlDv2hyMunJFFSTcWWm2TSqAbQoCTBKJ4kD0c+LSHsF9HYlZfQlkh/8X1snQVDsd/zBgU7rZ3R1zTmWVnm6jTDddQm7I4gRRnTHtyTGNe6NCMsB6eMHU6qgPaSF/A5iwsn3NnN6qUwU0tRwF1twT38zBAO2NmATvUJHDJkW8PixdBPoO+gbsDzPdxnaZRdo+zpnOZhvVx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PfdqtvRAPQQYkWmWrEhzYYWR9Zf8I/2LwYnjCWbIy+234kzcnayjyKBTeqaQSvoIrUEu5N/xxoYTRZC2RUNO56usZwjOJ3Xg6iwgmPyci3b7yOmY0q02UDrefLlnVT97xyfmbwrDsxQTRGpDnGXMdzRSo1mAb+1+GPViNtvykK01 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d4a9ea4d-62d6-4d24-e89f-08db0816a035 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:07.4497 (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: 52T72HI+k1c4VKZrgbWoZZ7MGyhRNB9OFQRaWOU/PKhx3Et+QT3kFLjIAGrHRAdgJ7sUVRlsW6ZUl94FqMJZnA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5118 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 2F0D71A0011 X-Stat-Signature: cwcdfjbo1sooqwibyw6abpum8c197gzk X-HE-Tag: 1675669749-494921 X-HE-Meta: U2FsdGVkX19D0WLqOj+dn6wIZJCEIvF7GFk00Br+gvZUzQTSEJo3fuyjbLox54z2/qwqyGfStOIp+TZJlkJ0y0j4UdxTNzWlajc901b7aDMzlawZvzlZfhMyLb9irFKArxDDrll69PmZfmROQCmcNSIUd2DMLb/LCg519TGlWLyWx5+HH7RImT9MbbiwykyxyBeYx6fGgVvhjUL4XUBoFlbTKDNJs13fYfhAcbHuxxxXFH75GB1Sr5w5EYIzZGgeyLggl6Xb3Fd2lQrcca4w6aDUYycNKi6/9rJI3cNYMP33O3Mf6vq4bRwf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/zKB0p/zDECwr7iXntuHMaR/eFuJNrE/B2dgVJVrYPTm1FKYmeEf21NZup3V09LfPcZRDWpL6yi/aeXFC4vNX3zaGX+7OAWcqabytF1ODigmxpPK4kA4UmR0S0qIG39QY2S21lE8yzJVzqOI9alIbK4z1kGXcrgyugaKKR4Lx+jRgjnTACwyT1H1rGu4mIHXTQVf1KTtEq6sZtJI0ilTXLZu5yMst0uOnPOuFRRGc6UT8wMjx5nB+ND4GF79Y/C7yfQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert from accounting pages against locked_vm to accounting them to pinned_vm. This allows struct vm_account to be used to track the mm_struct used to charge the pages. A future change also uses this to track a cgroup for controlling pinned pages. Signed-off-by: Alistair Popple Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Alex Williamson Cc: Cornelia Huck Cc: Alexey Kardashevskiy Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org --- arch/powerpc/mm/book3s64/iommu_api.c | 30 ++++++++++++++++++----------- drivers/vfio/vfio_iommu_spapr_tce.c | 16 ++++++++++----- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/arch/powerpc/mm/book3s64/iommu_api.c b/arch/powerpc/mm/book3s64/iommu_api.c index 7fcfba1..338b111 100644 --- a/arch/powerpc/mm/book3s64/iommu_api.c +++ b/arch/powerpc/mm/book3s64/iommu_api.c @@ -18,6 +18,7 @@ #include #include #include +#include static DEFINE_MUTEX(mem_list_mutex); @@ -30,6 +31,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 +64,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 +181,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 +236,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 +287,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..454ccc4 100644 --- a/drivers/vfio/vfio_iommu_spapr_tce.c +++ b/drivers/vfio/vfio_iommu_spapr_tce.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "vfio.h" #include @@ -67,6 +68,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 +84,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 +294,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 +313,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 +375,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 +624,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 +644,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 Mon Feb 6 07:47:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129445 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 38A23C636D3 for ; Mon, 6 Feb 2023 07:49:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6DF76B0073; Mon, 6 Feb 2023 02:49:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AF4E76B0081; Mon, 6 Feb 2023 02:49:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 920686B0082; Mon, 6 Feb 2023 02:49:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7B0A26B0073 for ; Mon, 6 Feb 2023 02:49:19 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4C592120BB2 for ; Mon, 6 Feb 2023 07:49:19 +0000 (UTC) X-FDA: 80436091638.08.D7376C3 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2053.outbound.protection.outlook.com [40.107.95.53]) by imf03.hostedemail.com (Postfix) with ESMTP id 7A5A02000A for ; Mon, 6 Feb 2023 07:49:16 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Gf8LWJbG; spf=pass (imf03.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.53 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669756; 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=r9iLlWenkXS+qWZNyDxmBNuM9bHSYN74X2PJc4f6L4s=; b=Ea1QeGrpr4idrlno9ecX4ebwqJUdOVhJnmBEyBB4Gm2wdrdF0OuNCY4rqArbFrSwOf+dPB RITrM1uluGTJ7v1j8IUh+iSsvVn2lPo5ob4VU/sj4qtK5+ZWJr4dyvq2zkZ4+z4m0rAEY1 Bt0DDE/3B5VRMlBBlmsNVVTKDnDk0X0= ARC-Authentication-Results: i=2; imf03.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=Gf8LWJbG; spf=pass (imf03.hostedemail.com: domain of apopple@nvidia.com designates 40.107.95.53 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669756; a=rsa-sha256; cv=pass; b=lMAp8E+kRU0tCPhDoQ5uWxHK/fp8yuABECH1UbAOwjcn3LDERYoPCsRgWu5ezbi4D/TU5i fc4+wV9jZv/X1zX7eKWzhRb8Q+dxiF1ZfXWVETpsHisDM8w/iPUehFmcswY0APauunIr9e +yyBGXqBln/CZzTAErWfnrFBkzSSUGY= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Tto6HDLH7veTHgG9SgcxScWU8/ZVYhlCZNpKm0CL8b/NkVFJ7mxrz2j2ozWxv8bhr5B2K6LM7xOgWRgWNgE83eRhzhAwY+TCPDrzbUVLSEAKcZuvxj7+muE8+6a73MJ5f72vKTmzAXnYDfX4Gm7ui0L3mC5QXX/WSpF1xjRqmNU5GG9pLeQU8eZIaiWWgtfjRGfnzpSD/oOe4Ou8wQ3cDlJT0xyb1I/0MWEMRK7WsD82+ClKnPP4EzxVuUoE1pq2m/4oA/4G+DX+3LifWs8opySTBxkUGgc97DVN2yKb0mi8D6OQubT2YkjKL6lBhGVbkZ+9jsKE2YgwKQFtcx9cUw== 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=r9iLlWenkXS+qWZNyDxmBNuM9bHSYN74X2PJc4f6L4s=; b=US4drmNjS2dMnPKb1UZzmNp7bdWqcuZGJy7ui+bE9RCN2vl9KjlZKq5PzGdzZyPUgw0iX3XjFKFryyzCxHuevZUltMSWyAxyyYctKsi3v91ftLDg5euJbgsQjdVhtf+O87qMSe+qq2Fd2ebTjg7bq6/eBKfAzLpcepPFcNU41TnF8SNc7MjeEqKleRSHxH3/+u4LH/7ixlp4gIvuiSu3Dt4oEyxv2xX54c0zAXpk5was2XHvWeY+05DxeH+5Jmg0OIs7ziHxKAypoD02DJTAwnBOD26i4hejAGPSWGSisg5OFIZBqoWFVrISLlFSUTZnHAkZXMEYxuJQK47fbWwudQ== 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=r9iLlWenkXS+qWZNyDxmBNuM9bHSYN74X2PJc4f6L4s=; b=Gf8LWJbGXqVUQFIga30K7D5qcFdSDCuAnHvDMezj8AWMzPHRlHJmHv/EgBw3+Pek+2ot4lkY6DzrLks7zPkBQc0mKU7N/HXV7xHUHNFXe/FjnT4ImGnQ26NfbptOrwfbPeylxKJquBK2fUlZUBJFgqjueq6jhj9Ndn42XPKFEL2403dAn3q+H8JGNR3Rc+nAG+Y5bOrcbt7BXB7TdIJhzQDUiESy9IEcx2aKaP9ZQGlk6F2V1Bdp1RfcVhmntk2GLmBchLbVhbgS63AFirVVKjYGHbKotlZw1pKWYxqYi/KkYXUd4gYpnPeE0n5GIN+7ze1yk7KsDLIBPsE4ccatcw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM4PR12MB5118.namprd12.prod.outlook.com (2603:10b6:5:391::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.31; Mon, 6 Feb 2023 07:49:14 +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:14 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, "Daniel P . Berrange" , Alex Williamson , Alistair Popple , Jens Axboe , Pavel Begunkov , io-uring@vger.kernel.org Subject: [PATCH 09/19] io_uring: convert to use vm_account Date: Mon, 6 Feb 2023 18:47:46 +1100 Message-Id: <44e6ead48bc53789191b22b0e140aeb82459e75f.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYCPR01CA0027.ausprd01.prod.outlook.com (2603:10c6:10:e::15) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DM4PR12MB5118:EE_ X-MS-Office365-Filtering-Correlation-Id: e6c72ef4-683f-4fab-e4ad-08db0816a3f1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W9IdeFq7Vrg8CD9yMMSSYTG9kGqws1YS8W2dUbffEu98uQ1Gi2Z3B6t9juAX1RdjSfZyBFpNzopuwa7jw6/x+SrXzlgcGw8isGBU+2pohoLvR01U9OX+Ml94XuHAHlblXkXIDxOJNoR4EMs4dgN1/tU21Ec4rpCw6qItcq1F4oHigpXlANsPg3/5hihtPXhGCr87JDpK6aUE6t27QIB6WMo+DqLVnxgKoRRBnYpkxcHQzMqmTEyfhuQC7Bm62o8mNGBx1I4JK9Gk1KKbPWKn/yXEaQ1VsO/4kH0WyJiWAAed/zs7hMDmKwjPjBfyOxtgmadSUSKsmjZoLjAgX/t/gbyDEEY4fYHP+okBJlHgb8vJ9Yog/ZuBJOBZ0kx/llNFtslu7PYpIpxrEeLjUaP8OmRe+jecSjMb/fhJUp2NVPG9jON6L7+V00IQdalw6NF21h5uc09+CJCtTBl47kGidM3yohwuB59O61B7TXrDZpEjZmuTgFSzA0IVwnZv1nju7x59YaTOW+XOv3mMSdBN2IBdDVFP2/a7B+h1aIyqnd8nSQggTIsEC7KqiStReFR8h+WtvIgOM/CRTzDJ13AsMoqJNt56/eDzxzmLTnTBRbbmaOldDHcWfsbYpBUNy10hjQQ6rVexiYY6IdMcVHdO7w== 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)(39860400002)(366004)(396003)(346002)(136003)(451199018)(66946007)(66476007)(4326008)(41300700001)(8676002)(38100700002)(8936002)(6506007)(6512007)(26005)(316002)(54906003)(7416002)(66556008)(6666004)(5660300002)(478600001)(6486002)(83380400001)(186003)(2616005)(86362001)(36756003)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sJB/lu8SsYSUOQ3U33wUhkBHtA5BcV+QZ1uLtPiwnJiVJHTZQVsIF6mIvextTHE/AK9JGcVBH01l9E0igxUfiaJ1F9Coo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Ze6uFNY+X0ktTB5RPrx+h40MjrF+S++7HzmkNoY76YxITq2J+yI5HOkx5Pg7gRjCSYX0Vq6UOt+1FTHwg2A7XDBR70/zplgj7nS5aPZHxBoXsZM2soI4plGyNgJox4Yd34JGVDmsfZrc/YLVl8RthC+o/kkLPGl0TPCK4WfGKY64Dd06GewbV/UGwZKHN+hopKXw07i7KXiNyI7xDy4OhkvU/02Ga2kPahSq9pBIo5lCIlDMD24I3DHg+ahuAdDGC+B80zYArW1qUR87+23ccG8j7yR/Rl1qBzr92LBFjYneJMdBYJfpKf9wu2UJYBkHLgLp7/Tgbf7tE8QHf1ExoR3vPzs8QC5QhhK+sHiycjFIpkctXOxQaJ/SoK4GIRqPrkoA+G1VXwuXoQC509DCGh X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e6c72ef4-683f-4fab-e4ad-08db0816a3f1 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:13.7304 (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: nCGlYGM5jkLy5MtbClu9G5nINFV9C9n+5uWcbYP52urB5fWT5ZaxjYfStseduUlIEppg6KcFL2431p/aXlfS9A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5118 X-Rspamd-Queue-Id: 7A5A02000A X-Stat-Signature: pcr69y4wotyetfur4gp3smsb9ibeohxz X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675669756-869645 X-HE-Meta: U2FsdGVkX1+Koxz0kn0CcRIqfeji0461FmhwGH11O+P3zt0X+QNumZkQWgDhQtKjvN3n/B5/iYt9RiYkxmIe88Obls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pb9OyFKMGOrY+P/duX45jh7T9RQ4sDMeJo86zWaUVTRrkKYqBCF+5R8MQxku/XYooec+NVIZGPhYgjsug5RvfrjYp27/9xc/9bifLlYjWNd9mcz7erb+T8+AvB6ucjGHt83rUPVN33gpBKWLb2Q8X0qGJ7J+bv+F3XgbYPLNysclSB0jk8NWF1Cz64Cj35OpgnSMPwgLjuug+MzoyOtMaRqLerjKaSMygfJwJCIjqYP19g5FupoI173dbyD7Yx/DdPs7hSUgSRD+QMlKfQXZ2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Convert io_uring to use vm_account instead of directly charging pages against the user/mm. Rather than charge pages to both user->locked_vm and mm->pinned_vm this will only charge pages to user->locked_vm. Signed-off-by: Alistair Popple Cc: Jens Axboe Cc: Pavel Begunkov Cc: io-uring@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- include/linux/io_uring_types.h | 4 ++-- io_uring/io_uring.c | 20 +++--------------- io_uring/notif.c | 4 ++-- io_uring/notif.h | 10 +++------ io_uring/rsrc.c | 38 +++-------------------------------- io_uring/rsrc.h | 9 +-------- 6 files changed, 17 insertions(+), 68 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 128a67a..45ac75d 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -5,6 +5,7 @@ #include #include #include +#include #include struct io_wq_work_node { @@ -343,8 +344,7 @@ struct io_ring_ctx { struct io_wq_hash *hash_map; /* Only used for accounting purposes */ - struct user_struct *user; - struct mm_struct *mm_account; + struct vm_account vm_account; /* ctx exit and cancelation */ struct llist_head fallback_llist; diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 0a4efad..912da4f 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2744,15 +2744,11 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) #endif WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); - if (ctx->mm_account) { - mmdrop(ctx->mm_account); - ctx->mm_account = NULL; - } + vm_account_release(&ctx->vm_account); io_mem_free(ctx->rings); io_mem_free(ctx->sq_sqes); percpu_ref_exit(&ctx->refs); - free_uid(ctx->user); io_req_caches_free(ctx); if (ctx->hash_map) io_wq_put_hash(ctx->hash_map); @@ -3585,8 +3581,9 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, ctx->syscall_iopoll = 1; ctx->compat = in_compat_syscall(); - if (!capable(CAP_IPC_LOCK)) - ctx->user = get_uid(current_user()); + vm_account_init(&ctx->vm_account, current, current_user(), + VM_ACCOUNT_USER | + (capable(CAP_IPC_LOCK) ? VM_ACCOUNT_BYPASS : 0)); /* * For SQPOLL, we just need a wakeup, always. For !SQPOLL, if @@ -3619,15 +3616,6 @@ static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, goto err; } - /* - * This is just grabbed for accounting purposes. When a process exits, - * the mm is exited and dropped before the files, hence we need to hang - * on to this mm purely for the purposes of being able to unaccount - * memory (locked/pinned vm). It's not used for anything else. - */ - mmgrab(current->mm); - ctx->mm_account = current->mm; - ret = io_allocate_scq_urings(ctx, p); if (ret) goto err; diff --git a/io_uring/notif.c b/io_uring/notif.c index c4bb793..0f589fa 100644 --- a/io_uring/notif.c +++ b/io_uring/notif.c @@ -17,8 +17,8 @@ static void io_notif_complete_tw_ext(struct io_kiocb *notif, bool *locked) if (nd->zc_report && (nd->zc_copied || !nd->zc_used)) notif->cqe.res |= IORING_NOTIF_USAGE_ZC_COPIED; - if (nd->account_pages && ctx->user) { - __io_unaccount_mem(ctx->user, nd->account_pages); + if (nd->account_pages) { + vm_unaccount_pinned(&ctx->vm_account, nd->account_pages); nd->account_pages = 0; } io_req_task_complete(notif, locked); diff --git a/io_uring/notif.h b/io_uring/notif.h index c88c800..e2cb44a 100644 --- a/io_uring/notif.h +++ b/io_uring/notif.h @@ -43,11 +43,9 @@ static inline int io_notif_account_mem(struct io_kiocb *notif, unsigned len) unsigned nr_pages = (len >> PAGE_SHIFT) + 2; int ret; - if (ctx->user) { - ret = __io_account_mem(ctx->user, nr_pages); - if (ret) - return ret; - nd->account_pages += nr_pages; - } + ret = __io_account_mem(&ctx->vm_account, nr_pages); + if (ret) + return ret; + nd->account_pages += nr_pages; return 0; } diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 18de10c..aa44528 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -42,49 +42,19 @@ void io_rsrc_refs_drop(struct io_ring_ctx *ctx) } } -int __io_account_mem(struct user_struct *user, unsigned long nr_pages) +int __io_account_mem(struct vm_account *vm_account, unsigned long nr_pages) { - unsigned long page_limit, cur_pages, new_pages; - - if (!nr_pages) - return 0; - - /* Don't allow more pages than we can safely lock */ - page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - cur_pages = atomic_long_read(&user->locked_vm); - do { - new_pages = cur_pages + nr_pages; - if (new_pages > page_limit) - return -ENOMEM; - } while (!atomic_long_try_cmpxchg(&user->locked_vm, - &cur_pages, new_pages)); - return 0; + return vm_account_pinned(vm_account, nr_pages); } static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) { - if (ctx->user) - __io_unaccount_mem(ctx->user, nr_pages); - - if (ctx->mm_account) - atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); + vm_unaccount_pinned(&ctx->vm_account, nr_pages); } static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) { - int ret; - - if (ctx->user) { - ret = __io_account_mem(ctx->user, nr_pages); - if (ret) - return ret; - } - - if (ctx->mm_account) - atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); - - return 0; + return vm_account_pinned(&ctx->vm_account, nr_pages); } static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 2b87436..d8833d0 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -167,12 +167,5 @@ static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) int io_files_update(struct io_kiocb *req, unsigned int issue_flags); int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); -int __io_account_mem(struct user_struct *user, unsigned long nr_pages); - -static inline void __io_unaccount_mem(struct user_struct *user, - unsigned long nr_pages) -{ - atomic_long_sub(nr_pages, &user->locked_vm); -} - +int __io_account_mem(struct vm_account *vm_account, unsigned long nr_pages); #endif From patchwork Mon Feb 6 07:47:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129446 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 D99B7C61DA4 for ; Mon, 6 Feb 2023 07:49:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 726EA6B0072; Mon, 6 Feb 2023 02:49:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AF5D6B0081; Mon, 6 Feb 2023 02:49:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DA756B0082; Mon, 6 Feb 2023 02:49:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 37A1B6B0072 for ; Mon, 6 Feb 2023 02:49:26 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 08EE2160BAA for ; Mon, 6 Feb 2023 07:49:26 +0000 (UTC) X-FDA: 80436091932.03.2ECE743 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2065.outbound.protection.outlook.com [40.107.223.65]) by imf14.hostedemail.com (Postfix) with ESMTP id 1938F10000E for ; Mon, 6 Feb 2023 07:49:22 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=LCGOkAUc; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.65 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669763; a=rsa-sha256; cv=pass; b=D/YlBpencnRgkLiEz4qIWRIN2hMgM0yNFHiWGPol1bg4Bd4F+LOVaPp8eekYM0Tbz7OM6y chGKsjfD4HlkRoTtKbAjNWmuDck1blEQRnPXcSzAbUnIJTLpalfAGYHvLFBfwv+aCMy9HW ZWPyEIH/MeMir+c0BXcn+IougjjlwGw= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=LCGOkAUc; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.65 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669763; 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=CYW4u4ayE2LZHX01iQfnpEcejDU0yOEhVudNlOUAn3Q=; b=WAMBJi5IoKw8GNDuFonuEAi3urfI2Wz63w4y2I2FSknpop+5Xh59DjkzTFrriysK7C7smj fldkyHv6d5b+Lk0ALx5CmccdqdoqvGbMmz9Twp8cDfJLM8RTJsx8AEUpMFJqqO4GMCivN0 ydkiVpI12lTV8svwaYhVZLxdqBB5RHo= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N9bc9Rni+JvKnGuBzzNjA3HFQdH8BUD73poGipHQPVvXZk8lqhD5m5xluB82xR1ZYPlLFMMcA/XJIRBbC8IXGIDSKNVea+O+Fzii0g30tNJ3O2S4AmzxOEqW3D2Qvuon+MQVlLn7GyTXVSEfFbYsTHlDJuK1Hh6wJkvfzT3p562S43cw8vxf8oasBS4ixhxYXtmK59OEPclkXB8Mi/sY/ZofOuTGX3w5Obb8kh8yf7/SzYGIcxkB4vPovi+3a7nO8oyAqS6w8L7ZCB9708bLY1RCvTO+b/TwDEY7p5mrK76AkDmB4Tv6uTExcL3gYbtjyaIsaeoD4aj9Gzx8TKXt3g== 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=CYW4u4ayE2LZHX01iQfnpEcejDU0yOEhVudNlOUAn3Q=; b=KxpDdjzItMs7oMHnWxbFIbAiMDe//rfEgzw8Xx4uyE41tMhzwhGMc9WU4LSAvslw0x8f0uk0OFbxX2nv/Sw06BUWw1caobMo/yoVYiRhlH6HGYkzUwLy8g/qM6N5PcvasVmOrSp9ZPccZ9o0vI2mOxly+cvdsvq7uhA5PVZKkIEYzof55ofQxwt412MqxWjBKo3LnWmWsKK+phwGkyLhLdO9KT0UEjmGBGS0Rms7C+fURopd0z3Yi/4TjAmsWJX87X2CdWD8XUJJMxH0EkLPHeqOpe7bwIx5mfbz2gf0xZD1Pr8FLFxprpMCRBMBvIRkxVLTCKLm5rlGTgs8jbSMtg== 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=CYW4u4ayE2LZHX01iQfnpEcejDU0yOEhVudNlOUAn3Q=; b=LCGOkAUcbw0vOxMEIqkDhbrUyi+Q/QrFLfm8WDWZwttEqb6SGC6ytVojDYGQ3W8mMwzlLiMgJMgSnx7+zNvr2LQEVYWRea3af/eSAGZaYBm8XzYgewKMCKiq4zxplUG3zXxw671Q60xeqaEtCiKb8tpwKOhM5UdGv6OXJ9hczT+M3xm4a21xSkGcU8nGENwG9zuRr0h/SAFp2kCyXV9veXiWao7bXHNqUZXFR9Wzh29VahK6lyIG+GmV2adHPhQQ+mei1crxC3ZXczm/kChnd/igJYJxz3yWKrpKcjzz4EPYAw9JvWxNelIov5X8e2T6DgBJVaiRUgFG7UANPuk7dw== 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:21 +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:20 +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 , netdev@vger.kernel.org, linux-rdma@vger.kernel.org, rds-devel@oss.oracle.com Subject: [PATCH 10/19] net: skb: Switch to using vm_account Date: Mon, 6 Feb 2023 18:47:47 +1100 Message-Id: <60a51160809d14e3e4a1c681cd71e37ab303adb8.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY5PR01CA0054.ausprd01.prod.outlook.com (2603:10c6:10:1fc::20) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DS0PR12MB8573:EE_ X-MS-Office365-Filtering-Correlation-Id: 691012f9-8863-4506-9d29-08db0816a82e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xFfTjdXiD80AhU3nEngcc8g/lI3dcbeLVrSsnPMuN+fKt+EWkUDZSnio4VD5lBMIAuch6KJGUCWhOCGe97jmA4HFkA3G84zc7kOnJNo+4MLzmGCFKWZ58ld4u3ICGWJQz7E1OldMsI+Qkp4M0PUdPdY0XQctSnshwjc4Jh+DUFBvgFH+PybQOVEfBxHsKNJeWXOAK/c+8PqVrdvLNe4fb+rKjQWGawqPelgzvC8OcK88/6ZHU/RuMY5A6sb0Y3NTIxR2BENvqGg45YO4MyJgUR5pM5KB2DjSMMGNEh9GkBigxsWeRB/bequ4IIQx/LqXDxn33Oep8MGwlayGJKUN6I2Y+XmHh4tav/jy5WHFLO6H9ONAUem8VosBR63H1yAAZ/gTM+l8YEWVGnkMEN5xFaSklhZuzCwvGCE1hX1xBFWKBTMSWT8xhEji+40hdE7deebHd8hIAx9UGKslTsLOD/ropcmE6fU1gTdHIdjP54TH+bzIs+HNp29TlV+IUKyDgyjs1sld85lCV7/oy9AGHl1i651Vv2m0r3khm+xBwtzUCkLTG+sSEu/7vsvRSDbThcqMQTxrKkNw7uF//O+sdboeot1AKmC2gDR2aGH8JIw0TBdRKPXyoyfU+LbDFM5QjfCZPMny+mzgBEOiEUg7xw== 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)(6666004)(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-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HfySn4qGwrHdAGcR9NI3HHGKHYCbb8+FZraSoTtJ41bryVQNnyttKxXVHU+32ojDted1A2fFyVPNEGqoAhxdnAB+J0ZjKD2qn042Bt60Mg4QqsxbSQLaEcj2LoNbEJHvpvOzCXiuNZ17/vUQQkoudgNqoypgvnHU7Jfm+ZKm5YfU7xmaCTkHelHzSnHYcJ/3n2x0SySAStQrusifTU3GZ0mxsYgyCaPn4CDQI/hvZligYWwohJexh0AjvZ8u/VAOjKburqaoZclhK+QoC/FdYHk9ApNob4C5RPJz21mx/r12JAd7dwc+Ks1QWECAcGxnNSASPbO4JsKFD9ZFwHBOddRWq6FZliqGkqoUYPHUlDNJDrAzr5S/8H0JNp4RuHp4e3plu+7dH5+yAUAIKkiMdyyM1n7f2cDi/iLf+1sTFu/g+71qZ2aWBRFZQFZNlLXqfplApfT/QwF3FzLcKKtMeuflej8h/Pm2wIkMXLFhwbcu/Vj4CeL9wFfUcVmu9JtnnbWI9JkYNglMvP403q+JB8AFkMPpTjb+YP6Z4c05x1B0fSdz+JvPGIm8kDHfXCbuRqZBdcPQm7joEcghy8YWUbHVA5kHCi+Zhf5l6OJC3J4InvaGpoAHbboDmvMBgxt2Unz8Tqkzbv4FjWDm2qAajyAM0atSvgpx5nI9Lqv8RI0gsdqyJIVe9Gp290GXZasucrYdOQgEb3WikjEwS+4r2OhyT3L5lU592zbk+kf3H7J1rpojfwZC78FBtDo2kmkVkS8ZgguQBgpYY0JYTheuAGeO+knzPQBHdHR04gDI+bKsKBaTixeUh7+6D3cdgtxxO0mbZQMPHMpAqXHcNjNYcXk0rFlABhDJ20rD/YSdQ0IPAc7C5iKr9LuLKrKAoKXBNBdaa/Z1FgC7PHpWPvCRnd2FudRBU/tAKN768c8Sx9D2+9fLTJukfahYnq0w8kbqwzBAqMIfkNyTMOD+qad00i1nK+FXXa+ZgendkomXTAaVnDVb5NIT3TiA59U6nAFGCtmZeiECIHXVwrz53higNp3ExZo/LD91upJyyru3h5il3StfoSfL9Uf3QbWoMVoMvcK/WvQu93nymozXUrh5Y87oenl1sxFO5JpYGZ4eao2yzaC9lCR3hbaVtwi9EZ6WVvPcE6mkLC/E4xQ8tdTtYKOOnG0feJwuV8FfmWyl/ztSBy/tnOZS0+6Nk2guzAswhZyzCKZKu6rp3/msB3GCceYnlKuxUODxmzf5yyABMDNJOF6Jsh8fM7d8n7Mo9dWcoyC3dvIatLEYziKUN0oI+7cHcYOPHCPwVdR4GctlQGQ0XA4Jd4JMInxFGRTXH8gWeo3U1Is8HWkniMwMLTyfVDp/B4puew5UKe5NAgDa4GIoBJHM5mdm8RpoAlnepw3lENFQQpVUCe/92S7MJdj9nkZ/g86a3YbiVXbzZl1wyioWtBBOHSolbOEpgAs+AOvelqCk+ysmyEeVxY0EuO0hfIYWT1GJRVt0JVVT+qsqNlxGU0zCCt3d/3SVqQif1nGZo22fUDjJf6eSLJ5lRvVlteDhd6q3gmcaPQrKOTvMdfsP55vduCCcBo5M72Zfy1Hb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 691012f9-8863-4506-9d29-08db0816a82e 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:20.8549 (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: dpb1f26IarNDW8rGdBUOwWkregUlG126Mwd8oOA6dUNXCiRaJC2iJ1ljjSQMDKqz02PWIBAGu0RngiLg8yUHaA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspam-User: X-Rspamd-Queue-Id: 1938F10000E X-Rspamd-Server: rspam01 X-Stat-Signature: 6cqskt4bkahw6uawqqbzeftt4yzz51py X-HE-Tag: 1675669762-847044 X-HE-Meta: U2FsdGVkX1+Zz+HJciuWt8eWs4H/H50kuS4qhezOFHHFRVgp5+VNgVH5opLCItKeT7m6pCnJEKlbcGIXZHVnQtqsRqXQNGfhXJzyj6fzTaKY2B5Zy4mvH2uHeN8D2Oe2zFvSGY22yChXxjLsqxlMja52OqiJgKWrDoMe1h77jaR7nhE62THH46HHQGGdU0XUu+eEXVsIksdLJaSFtjeoZMUEa7/4XqZvDSs69MrefwhVKT2efLObpz5Su2LLIwN18SOUSZBOLTKhipfrSTkr5GFrQpr1i1b+u6R1dfOFeC6q3sKG4bho1LUAgV86TKtGQoc44RXL5Jtpb25JyeK1cmiMNIaA9kEey+7kUTpLOUBJKV08YYdiczI6uJqqAyslb3giXpBLNg80pkYIRlbUCnFLFPpEJ6zCJbj24I8lil9PxaYkt119LiOcIeehkEpKPlxkK3mL/7vy6Og9mxgpZ7AXQ3VP2RfjN4PrPle6lrrjOT3m5yq59oHt9JeEq+Xsky+X3rSr/Tfi0Qz41WTKN2RBJMWGmP//TDgwVMfzLsnIeHMjHGbMn70vfcUJ9XLJAy1kYHKGXy98+oalqKEfACgGnBAmYjaqDzmjYkH5XcWCLKLGVImXRCrZVkkt+ak7hQyTw0Fh9K62fkb+wfz7N4IRuNx0aG3BESkruEiPfCeAkiz089puNPDrKqPnDn+iHNqiAntRcUCHZQDgJ1Eg6/8jlwfazOJs4+mpnsYh+mZmDCIVBSVmGd0hIX4KVtJQxJ/duDMk6o1ThvN6QEblSWByCUrSjUtODKaN7J9PrdNkWKqRmOayKwzyyXIaEec3ntinDlwoBKSX4qgnj/zFpNlfEdbPYKUEw3OafmFwp3KKtuL4VtLIqzkcnJqq3pYUZwYMREaxzZst578U5HjFMckOcAKsByJAsEOSLQ9PJ8tAN0jih88WkNw/dP+o1Rmatp7kYJRljPHr72Eet/7 XOHbjq69 RkcV8/CBJrOSeJXRAQI0yiukEVwVX7cHGF6OWY3XLfOjq41DDhPK5/JfP316qrFVP36PXE6dEZlw/c5JAHdUwC0vtbd/SzpyTBOgtMapdc75NDQF5QQ0nfa4vYZCCIfMBmaN6R03gICh7Xv90Ro/PKkKbp4sM6eR9vYF9Z7n1Fzx2n+DkBQm2j0cpem8im9WhJPltNBxfKm3KvPlYOzRItjX3c+Ml5Ja6+vqmr0yN0g5Rg17vCrJ3w73rnXMGmwPDKy6rE593aatm+FnqL+LxpKp+HASvmUR7JXb/vKWKybeL6/NIECS80DwuvDzx/yZp8eDjopMq+iSCLf9W+Dv7WOPD94LVOBinMG50KRor/XcGn8todCzwyqlzeA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Switch to using vm_account to charge pinned pages. This will allow a future change to charge the pinned pages to a cgroup to limit the overall number of pinned pages in the system. Signed-off-by: Alistair Popple Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org Cc: linux-rdma@vger.kernel.org Cc: rds-devel@oss.oracle.com --- include/linux/skbuff.h | 7 +++--- include/net/sock.h | 3 +++- net/core/skbuff.c | 47 +++++++++++++++---------------------------- net/rds/message.c | 10 ++++++--- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 4c84924..14f29a0 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #if IS_ENABLED(CONFIG_NF_CONNTRACK) @@ -554,7 +555,6 @@ struct ubuf_info_msgzc { }; struct mmpin { - struct user_struct *user; unsigned int num_pg; } mmp; }; @@ -563,8 +563,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..0e756d3 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -63,6 +63,7 @@ #include #include #include +#include #include #include #include @@ -334,6 +335,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 +525,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..4595540 100644 --- a/net/rds/message.c +++ b/net/rds/message.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include "rds.h" @@ -99,7 +100,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 +368,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 +382,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 +403,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 Mon Feb 6 07:47:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129447 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 82D4AC61DA4 for ; Mon, 6 Feb 2023 07:49:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A79B6B0081; Mon, 6 Feb 2023 02:49:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 131B96B0082; Mon, 6 Feb 2023 02:49:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9E836B0083; Mon, 6 Feb 2023 02:49:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D4CB16B0081 for ; Mon, 6 Feb 2023 02:49:33 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B01B1A0B7B for ; Mon, 6 Feb 2023 07:49:33 +0000 (UTC) X-FDA: 80436092226.22.A69FE26 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2084.outbound.protection.outlook.com [40.107.223.84]) by imf30.hostedemail.com (Postfix) with ESMTP id 0383A80014 for ; Mon, 6 Feb 2023 07:49:30 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=dNyGq21X; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf30.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.84 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=1675669771; 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=ekaPX/00S+h70lFxojvYEzzxngZqMCIdq9dGbDFXgv0=; b=2GLFKYSrSaQNSntF+NcvCuHGkehps2wigbcCCC6G+7Skn40YU0+RXabYtIdXIxHcy+eYay jKxwIat/QA4PAtpcvqBtgBejFRJG+KNvgR1KosFPLCrjoUrBl02XfPTOE+hS/AFLTTPCnW IwzSf2Ai/Q4BKzEfCb75raDa7C17KJM= ARC-Authentication-Results: i=2; imf30.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=dNyGq21X; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf30.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.84 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=1675669771; a=rsa-sha256; cv=pass; b=jeD87p9t2JMS60drXcOoyiW+mIi2oGfpe0EZbJzs4NGehQaczzz3xOPHp9sJQCkkZyM51U Zz/CXNPcybkkiP3rvWIgGczIDl2erEzclNLHC0GTYYFdYRZ6QPOjVsVakSgYPLm5mM0uBQ anMTrWoyluSfHkE7W1Vnnn6kbqtm/Ug= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fl0haf409z2GVuUVMTZhi74sVRNd/3eb3P+XKkXnOrz/OZay6AtJ+tlwc1aZhLVpArLa9aEv6XGmL2kHSNcMwyt+TG95tPddfsQYKjHPpki8hvX5nJHMI0SRWQYk0HYIDl5tVq2yKNTTzFGyKmXlNODVjuVz3+ObltL36ZyQhmXQczcaOjDCKjQVydnMHlzEj2nvm3uI5RnIBPEt2D0K+h5xC2bentbNI4CCphhWtQ7112FIsdxQ46MSgEEm3o0iWmd686aof8ngHzfeS8AZGhrqRY1O6c6/a3xfqhNSFJmqJc0mDM9wOw7LxQl+tiuVNqm+r+LdC4dWCQvdxm2fgA== 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=ekaPX/00S+h70lFxojvYEzzxngZqMCIdq9dGbDFXgv0=; b=BK3OS8gOFEMksJ5VUhjDVC+0VX9SjWKxeKdwBquaqec5ZUx+y7/efgn3BS28Mi6oLOk2KIaEIVrUPXZAhhHJfgOfVbX/hD/pRcWU2zYSIJ5QPgy5Xxd/z1cErKhbtrsLj1glJD+iXd3A0qzkdg9O3XP6yuwMSXCjG7aex+pvBBmYoN8uNFy7e7E0uiOOiUXyoXF08vTnxNOOOopxH5JjpmMk/iHeAdUjuMOW59ncl57U7wTY488D0sGHJx/I5CiPjTWyV1tdIg16qzENQOyS0HVAGHSZS10ISckpLfVCWUQrNDMIs9OXYIJ2htnCq4xqmpC9Axlm8bs93hRI5gblsQ== 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=ekaPX/00S+h70lFxojvYEzzxngZqMCIdq9dGbDFXgv0=; b=dNyGq21XS2lvzTJSJQWV2Dx2hCd50LZlN6ntem5wh+6FJ/wf25TDeGen5qzV/Ru+sSrrRfFfbT2vX0LXlq9OCiuFpBI7KMGEUOyWR6fOXlmu1oVxm+MKYfKxOSs17oG9LlGjoGhF9kxJWb4vSXo/L+2bzdrveRZ7QGe/9BJ6D1ZOXD71dpYeFa9kyJfAUNPBXvBBiuKAoTkCB9TNSundH10vtOke9XV5FGVFHq9TfBBSkuNyXy595aiL5Go9zAK6DAFviaAhBZGTVTWhHUbjkI3iI5WmEX/SU1onOp1pJvG/7mW5f5AdV9KcXE4gbAW5baCMauVktp/tKI5dQecLUQ== 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:27 +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: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, "Daniel P . Berrange" , Alex Williamson , 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: [PATCH 11/19] xdp: convert to use vm_account Date: Mon, 6 Feb 2023 18:47:48 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYAPR01CA0006.ausprd01.prod.outlook.com (2603:10c6:1::18) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|DS0PR12MB8573:EE_ X-MS-Office365-Filtering-Correlation-Id: 4e499f2e-8b0b-4fff-206e-08db0816ac13 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: U/Hb5bUvPgq/8Bo/zhdDbA4PUQM4ddPIcEFNo7CVj8XX4kbiyc/wkH3Kp1WtHKJMiUG5tR1bYnYSI76gmFHStENAOhqNWW3CXT7bQp6RTqgIEtBpq2hsclqS4KNds1lKhU9c+UnZfOlsJMqDte4FbavG2LRznoGOMIVDtpg9TUojH0btY4kRvyAKcl8enew/vHUNWuEPfUx700I1hHO6teXAPLBXayByqXmuvl1AfcSNaVhLiqbOM+vREwqQEFDAEkqQfTZ7/7U/i6CzV/zDWLKrSZOnahP/PmyinY/kap4pNBnXyi+BCkzGfDfIdDW0XlFcqhSrSubdBDMw5kwQoOhB57LDCDevnsz1+B/xtgk56PDdP4Mxs6Q/o4Zkodw6uThm6TaB6wsN06uBi68F3vUs3Drq40ujckInxTegARQVlbHQ/LegvaWh43lulz+YE7Wy9n9lT6FvIWL7OoYadIzAPPnORUJJRRo1IGAcmzWVfYOQsLyELfGfdSlToN6X7uM46Z9LFTtLL9PFLYnYg2nvm9VJ2p0M/q69vpOc0O6qq4KhQxyvT71NJIC6t9VAQi4aFIfaqIkwVxqiOLYEEhRBpVDR3TkDb7hEJLkVzruSCx69/o0TIZzAbciD9oMN39mkfXEOyrH7vdOChdS+hQ== 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)(6666004)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(66574015)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?D7uxsrNSd4gdjxjXR39Ev+fBUv88?= =?utf-8?q?xIdG+/vw6hQF4DdiL5i0N1SWaPStyyHjrljxkxxN5PhVwiarIhwn1PYfhNsk4Es6v?= =?utf-8?q?xb9qvz08NBRAibPAgoH5oYtNLg5/U8Dpwq6Mqj/9cP3Ati6K2KwUB19x8eCfINY9u?= =?utf-8?q?DDJ5Qj79Tcq3UTdV3V0Y0yi/wGH+Q02O38d6UywtNb7qXaH0K2/iZ83su6hwOWiFa?= =?utf-8?q?Nl8FHRjAr/i6GocDXnTO1c2z3PHz4H6mcaNp73rlFjcVsbDjTqaeqI/tvHcL1EO4m?= =?utf-8?q?2/QyncpA/4m5/+tzxtEoooMVNrOSbT+V+8iUGq7P/+ssw3Z5DOPxApgyI5vuwTRnC?= =?utf-8?q?AZhOIDlJfHocP2vlKTm8EKkUtpQoTRAmcLOb2/1mZeQ4c3mW6ueeS0S0Yzjr7ER1F?= =?utf-8?q?wFD4cpQPzB+0F/23gaSjxukg2SP5tGG11rv952yHZEG88YMdMvzNPvL+HDXKUDK26?= =?utf-8?q?unpp6Bo/ZbFnmYF+GWNkhVtQRT0EOZtfC6ojqpmwDDc2fX1aQXIwPZTd1VegU0ivd?= =?utf-8?q?HydwqINnjl3aTI+vUQZefxdViuY9IGKKRAfYncHdrKXcMTTbkrFQOnOZ5b7/2G2oy?= =?utf-8?q?Herzo6E+EFFUm7S6fm0F6HNLct1ffAWbOOBWbRyPh/JtXgIk3SXRtArIzq4f9jsK5?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B6AIAhNoyY?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4e499f2e-8b0b-4fff-206e-08db0816ac13 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:27.3700 (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: XJm4eb5u09y5J03JJHgvgQg5AfLScdpIxX+UezKBxSPaIYMXVvTqX7hlUQm5cMjyyZJZmClUqbaQCKoadT1diw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: q8dnui79iknt8rmrww53efd7txqo3hr1 X-Rspamd-Queue-Id: 0383A80014 X-HE-Tag: 1675669770-776893 X-HE-Meta: U2FsdGVkX1/H6EMAH5e3a64ATO8t1EzRFw8tYD2i34eXde/6MMDP6bqjgPOpEd3AAY84gwnDazICJgAIxOESPwTEj0+ZI+6X0k/L9FDIQUGqkSzvAHehYSQDHsb+ZwV0OfjwX+W4XrOtDoHmOwpFFDfdeHYPJjsiQiEJZM5HAhvKVKqLuKz6iKkuMt345pLsNXUGtFkcA4JvotaXE8EhFFCkvs+ik532NjXArS4jN1bChFG7pXpyAGEHMiAu8geb4LfBAoU9SY8u2iCWWQ4+bMyLFxUCycmBnrVqbxfdQyilVqy0tTHBFYAbUdw2FY3H2KIqqSJnblxgr5dc3B3yMWUIFkXmluXkysUCTgRrIzMXX3d+j9pHQuugGUzF4kOk+UIazdMS88DRrfDPd5n9LF0ibt1edV4pqMgdXOKUmEkDKpA+7Vy6tCCRiCsR0Btt4hwxi0RzNHjC1ChqAi7pFvfZdVxEm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=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Switch to using the new vm_account struct to charge pinned pages and enforce the rlimit. This will allow a future change to also charge a cgroup for limiting the number of pinned pages. Signed-off-by: Alistair Popple Cc: "Björn Töpel" Cc: Magnus Karlsson Cc: Maciej Fijalkowski Cc: Jonathan Lemon Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Jesper Dangaard Brouer Cc: John Fastabend Cc: netdev@vger.kernel.org Cc: bpf@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- include/net/xdp_sock.h | 3 ++- net/xdp/xdp_umem.c | 38 +++++++++++++------------------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/include/net/xdp_sock.h b/include/net/xdp_sock.h index 3057e1a..9a21054 100644 --- a/include/net/xdp_sock.h +++ b/include/net/xdp_sock.h @@ -12,6 +12,7 @@ #include #include #include +#include #include struct net_device; @@ -25,7 +26,7 @@ struct xdp_umem { u32 chunk_size; u32 chunks; u32 npgs; - struct user_struct *user; + struct vm_account vm_account; refcount_t users; u8 flags; bool zc; diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c index 4681e8e..4b5fb2f 100644 --- a/net/xdp/xdp_umem.c +++ b/net/xdp/xdp_umem.c @@ -29,12 +29,10 @@ static void xdp_umem_unpin_pages(struct xdp_umem *umem) umem->pgs = NULL; } -static void xdp_umem_unaccount_pages(struct xdp_umem *umem) +static void xdp_umem_unaccount_pages(struct xdp_umem *umem, u32 npgs) { - if (umem->user) { - atomic_long_sub(umem->npgs, &umem->user->locked_vm); - free_uid(umem->user); - } + vm_unaccount_pinned(&umem->vm_account, npgs); + vm_account_release(&umem->vm_account); } static void xdp_umem_addr_unmap(struct xdp_umem *umem) @@ -54,13 +52,15 @@ static int xdp_umem_addr_map(struct xdp_umem *umem, struct page **pages, static void xdp_umem_release(struct xdp_umem *umem) { + u32 npgs = umem->npgs; + umem->zc = false; ida_free(&umem_ida, umem->id); xdp_umem_addr_unmap(umem); xdp_umem_unpin_pages(umem); - xdp_umem_unaccount_pages(umem); + xdp_umem_unaccount_pages(umem, npgs); kfree(umem); } @@ -127,24 +127,13 @@ static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) static int xdp_umem_account_pages(struct xdp_umem *umem) { - unsigned long lock_limit, new_npgs, old_npgs; - - if (capable(CAP_IPC_LOCK)) - return 0; - - lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - umem->user = get_uid(current_user()); + vm_account_init(&umem->vm_account, current, + current_user(), VM_ACCOUNT_USER); + if (vm_account_pinned(&umem->vm_account, umem->npgs)) { + vm_account_release(&umem->vm_account); + return -ENOBUFS; + } - do { - old_npgs = atomic_long_read(&umem->user->locked_vm); - new_npgs = old_npgs + umem->npgs; - if (new_npgs > lock_limit) { - free_uid(umem->user); - umem->user = NULL; - return -ENOBUFS; - } - } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, - new_npgs) != old_npgs); return 0; } @@ -204,7 +193,6 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) umem->chunks = chunks; umem->npgs = (u32)npgs; umem->pgs = NULL; - umem->user = NULL; umem->flags = mr->flags; INIT_LIST_HEAD(&umem->xsk_dma_list); @@ -227,7 +215,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) out_unpin: xdp_umem_unpin_pages(umem); out_account: - xdp_umem_unaccount_pages(umem); + xdp_umem_unaccount_pages(umem, npgs); return err; } From patchwork Mon Feb 6 07:47:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129448 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 40F90C05027 for ; Mon, 6 Feb 2023 07:49:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D18CB6B0083; Mon, 6 Feb 2023 02:49:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CA26C6B0085; Mon, 6 Feb 2023 02:49:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF50D6B0087; Mon, 6 Feb 2023 02:49:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 98F0B6B0083 for ; Mon, 6 Feb 2023 02:49:38 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6C29B80AA2 for ; Mon, 6 Feb 2023 07:49:38 +0000 (UTC) X-FDA: 80436092436.19.2DB2A71 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2081.outbound.protection.outlook.com [40.107.223.81]) by imf09.hostedemail.com (Postfix) with ESMTP id ACE29140005 for ; Mon, 6 Feb 2023 07:49:35 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=CHQwI+iS; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.81 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=1675669775; 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=sABmSCNhqUaki37ROAvn5I2o7zlTWP9iNz0mlKPN/jY=; b=qiTz1jf30em1PJFrKieA2iz+27AIjyEvHjBcT74rKRzt1h9RDwVm5LSLpwgQDYHK0VIqF4 Gu3QJ7BqpoXscjgDkdTpL5lwnNZM4MM/hcarkrT0uaq3NniPbcoqjZQO57PgR98HKZ0onA Jv9l7CGHQFUQVRvm3nvs3/E1eAb7pMw= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=CHQwI+iS; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.81 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=1675669775; a=rsa-sha256; cv=pass; b=miKcsOUsBfagLHy2/yUBL+Fazdqaf70vO5RDJDxfLVg8ZAnIoS7fpqzwpZaT6thA+O2y4f qtys2wo0r597LRvtst5ipz3b/xTPKUNgbPCPLbxNF+GqBGiCRsWEc8e95jwWSAnvuPlGnE R7oME+B6iD/tLhHo7Xc7C58Lw2FIQBE= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=obie/PwJcOe2hDpi6+/p0mbSZE+iDhqWzJUUigICaut5IiNVmMZcMK0s1zPImn5mryTULm8oG6FX05Q+fIoIJ48WxWZLG8j0S6AYWXDjPrYtZmdOlO7ztoKDhgjInSqWe+pDAxICHsYnluMjoSz3U6wTTjmbmpBLuTOW7bnT7WwcXK8BaXp+ZAoAmghhz/eZDuTVhoSgq+Yub+eQmBp00p/B16DOhWTzesK9GBmTheTGJjSgpHFdMgQbvi3dH7PyuFOrwI3gdkOuY0Q4e8Z/k3JksCiPrU2s71luD/qYO2rlokmq8mLCMFwg7ekZxSXAu3c85YcujhyBylouCQBJ6Q== 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=sABmSCNhqUaki37ROAvn5I2o7zlTWP9iNz0mlKPN/jY=; b=CFPPMeAcsBiiJ46UEfy5VNfnk4mAIN6Dr9ckIFEop+AVXMXunqipKYFibH3NfUpVVLybvP4oE7GZRlJH7uE57qY7G3+DUvMQ+X3WbITsattT5PXhPfZ5CrVtPRHeOJNDLO2oTZDuawODWrGJDJcOIIcHRpgi6MXri/KS6PEB8CmBZPXonbKPMwEBFBcQ+wfiEwYtKcGA99gATAvqib8pez3DEKJbGIJea+7+BrO5kC0YcQF+hd5Q7uDl65rGhKN4jFj1jwP1gtSF0kA6BOp+xuoSJas3Oqyi4kkDQsSmdccxxrq5XtsJdnf3MxW349vZAQmWTJzukswV4GNS2HNZVQ== 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=sABmSCNhqUaki37ROAvn5I2o7zlTWP9iNz0mlKPN/jY=; b=CHQwI+iS0hEE2NUjjhh1a8duKr1QEY6gPXkrUEGnhAhiYZh+EwVnEAFuu6/3arCe0qEFPyeE2mBJS0Udgi5k66OavT3AMmplZcz+uqJ83WF5eUQ5sS442WqtwUFWuLpFxK/zyKakGOwypeQsFr1EktqHWHS5HGj9MTzIj/AKquxhLnOuUR71GyVwKhW56iQeZoCko6AgUbTs5rLuExRipjp4QI8mrKGzItovJHdZJzO2Fpz/Ba0JCxnBKnH4N5jmB2+0c3yQah1YL+DcG/bEQext5pLO3aDQcjy6sqSW9YyWytpFSiGFz0j/RXveAB7XaOc8DdrOQSGve00kTSX1UA== 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:34 +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:34 +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 , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Paolo Bonzini , Alexey Kardashevskiy , linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org Subject: [PATCH 12/19] kvm/book3s_64_vio: Convert account_locked_vm() to vm_account_pinned() Date: Mon, 6 Feb 2023 18:47:49 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY0PR01CA0008.ausprd01.prod.outlook.com (2603:10c6:10:1bb::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_|DS0PR12MB8573:EE_ X-MS-Office365-Filtering-Correlation-Id: 1711d04f-1228-49b9-6835-08db0816affc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ivvth6W4nqFwCtLaRy31c0XU0ftAT7d0a+ncwklm/9hY8CDYIaLq8S9Jt2xQjUHxCuE39zwggwD/XO53FRHcYW1qtRs2tTLOW7vRhqIrBHYMJ9323/945oJADFzxZKHDVs6lUFv50zoYf0bfEusrFpmVmPVJQLmXVgYDaqhXSlybJ54Nb+MoKlEYynZapcQ13LWsg2+98QqVzcvI9NiZH4u12UOdiXeL29R/tul5Gx5oHmlnrsYMHLVbYioDcqqURbUci5Mwsw5cdFsI2/jjQdQ1LGAlr4ca4tdToJy6ks9DEVIb0CoVjPrJ3mmvqOtileYU5KI091ItQnZZdNTO9G+Ew9R+MxfNlFWij0HejOfkyr+GwPn/bJvNDs0QZGHbV5zwtu/7CvLHGuEsBLQbtuPSyAyOaNtdkwsBgX4ILtycqzAUXa2SuJNL3KL+iQU8SK/635mU4gavA2nari5HHpXFjPKm3fPLJsBM6IGHUdYfGDyF9/tMbN1Wfm+HbYu2hhi1aZuach6sacGBh0z7TgaMd0iDiWPRD5BLqE8YTEFT/D16746WxEkx0Ok1H6xxQ245Udui6PaqaB+SHubEythSM2HiRTk3B8ltzXn/PygycjHCQxhMrJpallA2QZIeQxTKJiB/X78qMo62cUKse58eA40a69NMAtKUimwAxyrRhv7TbnRiI5nrrqZKWN/2vbvQs0zCJkwryFodHzS3dw== 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)(6666004)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(15650500001)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Ay8XosogCpkzfmHNeC+DwsG6+2Mjp57f5iWgheg8h1dEFMr2NETA+gBVc0bY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1sfG/xAwFRniir2Ipt3GbDiPjS34t7ca5A4XOk9bmxL5hJBpNF3us5+InTE9rH7qgH86T4ojOv4GLqIu/DlX3+JSDYDDFNLDGUQ0ZM0+QPi4ZO3zLsuPmOkK9CH03s+TLaKhzmlzvq/xU6W57wj7uF4s5o91nJwe9mYjKpLa6BrVzProPwFqtJoNi7y2wgI/j9353o1+8FLEY/veLPTutass9w4mSPjMDX0kHwfJp3ew6+VYOfimqvzYs/4X4NMXUJR4u6qhEJOarofjVSmAWiD5oGmjxS2RbCujzBeCgsO0y/ixQfmGbY5RR5a+/JLYdWq1WQV3jZqcbxo+6AwmXmxVyNdKZeNq7vU6xc53fke44EmeHgk5LjnTiEKr5CyKIrY2ReJSxnjFxfcIUbO6W+E0U6fdlJB5GmcPDcggpLbUqX2wDPn9PQDGXxt6HTikcSBMkvLb+ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1711d04f-1228-49b9-6835-08db0816affc 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:33.9164 (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: c9J1tJaY4pyotV0qCSTqLu5myKDaf6kn5nhV5WxMGeFphKFkr68yrsw6s6yLnhpSy6sjXSjUlVCrpf09HaGxPA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: ACE29140005 X-Rspam-User: X-Stat-Signature: skj7n9b7fk4jnuffsdnz53yk5kb81kh3 X-HE-Tag: 1675669775-937546 X-HE-Meta: U2FsdGVkX18iHKATWK6bZUhDHnAN+03tA5a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6733x33tAhdAtRvLx2JmULKAT3va7na7nzb40dDnmVr0kfyb4tnWBWcx+DARDXCbECfDdUTSejZg0PIqJiWfvypx+JMtKezPiqJLmyyvSW5vqWz3oznqWcKihH41l7EbWaAZW3fRdPEq8BQPMZSFArblYQGn1JL5BykSLIZCeLYbAP32KirtTHcf85Ug1eiXH+aCDUAO9hjcVFYH/TmuWs5S8iQYSpx2vNW5YkO8H9LlP4CqEqDMkqmcT30LVU67GN3zmjwQxc6LkrrTI102jv2Q7oulUz0TiflC7LoZLLjNC/20TWhKzM2iF69D9VPXrCK5nSNeXe/szDAXyhDvJgpO7Pn7OuFooVa1Jj4SB/Sz8NPhF/du83/3RUV90a4Pd8Cto5CalzI8gbR13Qy4eI4PU0rS/b9zs+ZZWzdSplYCKzn7vkGLJgQJFZsFZrU/YXutBdzQe+zgZDUuvVkvd2pLZGIOk6kiReXg+7LTs8r4WzNQ+jGrgGtM16wrnXFVUpIh3P4os5w3qIRriOx2vlzMsO4OYgxY2/Ru6PapOwwGdTMXGbPOwpiVGVXc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: book3s_64_vio currently accounts for pinned pages with account_locked_vm() which charges the pages to mm->locked_vm. To make this consistent with other drivers switch to using vm_account_pinned(). Signed-off-by: Alistair Popple Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: Paolo Bonzini Cc: Alexey Kardashevskiy Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org --- arch/powerpc/kvm/book3s_64_vio.c | 10 +++++----- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 3 +++ 3 files changed, 10 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..bd7a7be 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -717,6 +718,7 @@ struct kvm { */ struct mutex slots_arch_lock; struct mm_struct *mm; /* userspace tied to this vm */ + struct vm_account vm_account; unsigned long nr_memslot_pages; /* The two memslot sets - active and inactive (per address space) */ struct kvm_memslots __memslots[KVM_ADDRESS_SPACE_NUM][2]; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9c60384..770d037 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1142,6 +1142,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) KVM_MMU_LOCK_INIT(kvm); mmgrab(current->mm); kvm->mm = current->mm; + vm_account_init_current(&kvm->vm_account); kvm_eventfd_init(kvm); mutex_init(&kvm->lock); mutex_init(&kvm->irq_lock); @@ -1258,6 +1259,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_srcu: kvm_arch_free_vm(kvm); mmdrop(current->mm); + vm_account_release(&kvm->vm_account); module_put(kvm_chardev_ops.owner); return ERR_PTR(r); } @@ -1327,6 +1329,7 @@ static void kvm_destroy_vm(struct kvm *kvm) preempt_notifier_dec(); hardware_disable_all(); mmdrop(mm); + vm_account_release(&kvm->vm_account); module_put(kvm_chardev_ops.owner); } From patchwork Mon Feb 6 07:47:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129449 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 90B5BC636D3 for ; Mon, 6 Feb 2023 07:49:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CE7F6B0073; Mon, 6 Feb 2023 02:49:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 158566B0074; Mon, 6 Feb 2023 02:49:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEC8C6B0075; Mon, 6 Feb 2023 02:49:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D3D376B0073 for ; Mon, 6 Feb 2023 02:49:44 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B5584140311 for ; Mon, 6 Feb 2023 07:49:44 +0000 (UTC) X-FDA: 80436092688.14.1A0CDB5 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2071.outbound.protection.outlook.com [40.107.223.71]) by imf05.hostedemail.com (Postfix) with ESMTP id 08E10100009 for ; Mon, 6 Feb 2023 07:49:41 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=szKlcEMH; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.71 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669782; 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=BwomzvkBEgGdYf53g0ZL4eXFuNuoN5c1QyS6vm/9qcg=; b=axTq+6M9MwBwqZkMqRPkrn0wK1PHQs5KdZpM0gEkcmXIj61w9o4XHA/fgHmhlFG1yXj8C5 mBHAPSUJEjla4vzE+0fvz/g7kLoCe9k6ot4dcYz8++E2Njx5Cw3q2WP6G4aZ9KU8+gHZNw +xdCxmTKU3RwQmCjjtmGpRWPS6eiBaM= ARC-Authentication-Results: i=2; imf05.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=szKlcEMH; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf05.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.71 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669782; a=rsa-sha256; cv=pass; b=t5g2bfbGN3YXY3XV+Eua4yL098fiTkvTOQnoEN4ZvQSUNbAtcrQJZ8JU141eof3V0zZvUT QwOOpsWpGClfjf4mdRNP61kHMFzs58fwCEsXfeDg+N4SaJliSO+593lwh8OCiwee5yr0/o euEpck6pCwxriB2bnkWVH1L1F78Rx6M= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LPvQxGiTkoiWrZ5zej42gaaZmCy0r2LFi2JJxCW5pegeXGXROCuhL+wljc/9MFIA8/zc3hF/gtTiy/fD0EDoeNIuKceTpu2APp8dCPhkK6GvkW5KQO6loRUhLjXAOL5AGClnyvFLB29kX49a5fe3wJrHH9RgYEDJOWucAgtRxXCKbIMxkhD7Fran/EhydcMnhw0mb5hAl7Te+S3ohdcQNIXeATXKBVZKU2lg8MTz17SCQeZeIEP7GQ0HMAeUSd+O249vEpkhcJdOAXtYhNvMy1qMOWBlZS+JM+q92jv3wFB/aOcqzc1QtIseO+8RVHXGt9FRcohfWEzqiIduevaT5A== 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=BwomzvkBEgGdYf53g0ZL4eXFuNuoN5c1QyS6vm/9qcg=; b=HvGq/9Db+VX9JBQK9/VtN4fT1wA1SjRB/GMrkFnhITV9x2rN+I8CzIo6M9CeAes612bAqsi+LqiRgWnp1NNw6wD1ViQR/SN2BHl3XfKOeUEHR8Vua1djfXbISmMVO6wT4V43KysUbHvwhSMqzUOHeZVrZeJDXLXrl1uaOnPL0BI5s1vLxHvMHdGNwv5r6atA1RfOA83ieNVWlr1cWJ/myDWxVdBsIYwQzX3Ycze7WkKi2rpit+EUG7eWk3Z4kbMMPqg0TzbIPwwHMo8YpgqT8QaIB+qogAur3xBiw4uHW+F+bqIJ75vN8HpMpbzYa9g64iA1qaivvcEyfp4UeWD7QQ== 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=BwomzvkBEgGdYf53g0ZL4eXFuNuoN5c1QyS6vm/9qcg=; b=szKlcEMHt1BgLN5SGRfE+C6PmQoXkp0dTCEshpWFI/Smn6e5C+3VYLY2QZzhs3jW5Zr7Wb/+8xkUe/R4GNRV+cmPWyFKJL8Rtr9kBvKwp3wwldrosMOZQ3UIpx5OzAWgfJTO92qN5mGGd9qIo11rdP7k19YYqVqCHD9Jwlu5k6n83GJqUsitF+DtgNz/H+KWLy8+f5on9rsznstPZHuGI34hmt2ynwQbuw/phRbFl5Ax34Z1QisK+TUxM+R2451e3RmAO8/TsTWUYggivf2fiJuilRN4NluWPVknhV7EPnUE8NEfTowCMIVRHcPY5sjsFOxFtNnxAdD7+VElgb+BPg== 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:40 +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:40 +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 , Wu Hao , Tom Rix , Moritz Fischer , Xu Yilun , linux-fpga@vger.kernel.org Subject: [PATCH 13/19] fpga: dfl: afu: convert to use vm_account Date: Mon, 6 Feb 2023 18:47:50 +1100 Message-Id: <4c3fcdfd0617ca7e21bb826c8749adcf00bfe956.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SY6PR01CA0093.ausprd01.prod.outlook.com (2603:10c6:10:111::8) 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: 6822060c-def8-45db-664e-08db0816b399 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CDx0ysmFC40w7m91S+8U0DYJ94WMQ65n7J0cRFi/UtBy+LhuTz1paH0LNMooPPPAwwoUiWDvrBRLq6zdNVn3EgJnZ3/WM+gt/leFaI1d8EJLYnBapMlpCaKp0JsEoLLxw5Js6bRcx5q0d0q1PrvIpltXVuzWguf+Z3HP3cFF7A0AgKY7vJkFRRAiOFon/UFq7VC2+gAZVr0iFCk8cqRV4AziMgAJx+qPcKOj4qrHeaK6AEOLeAQ7SsEsopTKcU8mRvs8f7E72FKGmVUadgCFf4LGKb6RKep4Do/GxPfcp0XFfh0YoUYbzj1mRNdOpwDXDJePg5juc1Q8Ja4E4BKxOeZHny8+oaYfVAiZ9HdKjdgCZNhpk8FNYrMuN0Ac7e+DoGV7jXm2m/8zJvYzD/8QokrSWJcmktaKERy43LJSTorXKNLcjHSQm9NU4/Y0G8o/Ayb+0Qflj4ad3fvypFFATZJnCjATWTBg4zpCNmD+MRHVaexhn172/4x7+3aKBxJV8nRYyVISxSgQywE3l+zsi+bOQXTApmge0Eu8ZdoDpCN0pDuzmMLqpT9D1OxAWuae/Qi1/SBgotzY/hbwQtB9RvjSUJ2ZQvvpPBndsbz05Ohs0cGfiTCVVDV78pzDgNLv21gTnsFJoZNyEV8dae6TTQ== 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)(6666004)(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-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NUtzb5X5UNLvv4FIgh9XzomWui5nSY1LhzIKgG5erK9DlDJ2L/PSsivDrcH0cHzGAlnAYoXc5X7cdLoCEcShz3j2GEdKTPozqdOxrqfoVCEbAUl73y16R7IKBFEBrb2RU/SrJnLmxLzyex5ex3hzR425ULvO1bxKFtC6RIjqqvzIrYcVchUZOjC31sqHbLGzUZx8tBOAFDagaoUPKa79Q9RUdjJzsqIRNXG+kojmlRmYgKQqcNlE2voSqhZ6npKQt+zE/+EdwjHvYw+MoLR/O0/aHteZ38qlkQPRqsEs099J0nxRTZNpV9Nk4m9luE7fx3DimDybanGTnOoOkLf5WgY7mfkVec0ZmyIqx6Wl+QOsWuJ0bhM8vmk2wOYgu+sxjhosYCXzhyfmBE2OtdnPcjHH591A94RZ6wX64k+CSW0Bcsr5kxY+b8A/4FNvBa79Ea3gI+alzRlVOeH1pnyyEvQCu0uBSzCt9ra9uJDZd/HTrEt892XabY9oqxdqKvJXrWYl0FaViLtsZMbuuyY34kmSXHwz7tx+2ke+ewFK3GxZCmMRQevOA8ZS8FGKP3ShhHYOI5nMpHEvR1eaBKa1oyuuVh4rUxWACeXzBBExcz4tnU24gOjlRvRXOrVM6V13DktiQ9GDDKBNcCSy1PVziLToEVduz3c7T8liZYjE1lJw0uy/ifOcf0y3MKWLqGBUoA7zzMoO35UwWTbqYuFD45Bhy2X18OIWIRVP/d6rsn3N8MvWlwIzghJhP1lHaKGY/IzfN5wtoQiOZ+4Ry932Yk9h2fP7DWpSTQHCDUpp6fc4c3NQnbtWtZud64goBnp26McW9/mu/pFXXq1HsSa+FE2lb5JOz8svDn2aS9UHsuJ5MT4e7idfU5KmT0adRrsHHbz1QMiAvG1d639nXZmwJ251V/rhT2JNC2yS4BvxbsbvNgCr6uAxpFGGK9VC/+nm7SPK5G7V3z2oDCKrEBwre/lXGRWg+c780adQSNmFS+JmUngkQwLBUFPMbXQY4gVjzsrZy4rMageeG/Y5ZX5sZ5nomG0H1BXGtM8gy5WyftyeHCEpLO5bPfSzdV3+LEuuuItsLc2bT1WXgZppQMrIntUkmLk/jiiEdf+PjciuIorbOuOjvWkoUSE5AtXVFvLa5DWl49dcT+gtbmaSDi7wcWIn96RezwuZ7pjDVZ9hpUgvY0dmTF3AR+x1RyA4aY7hAgLuaCesRFZkTquRwfnNAsAG2SWCCxv9l8vs3dfoHrdRt6YCRsybyMzG4ZVmP4YgWIcUBF7YzRoUwHn16BJ1VfBwSb/1XHdn5s68Pa0w7zN2A3afidhBy3FqUKas6utRFSSPvLsian1jnJ8TnHrJJiodu5mY9qya1j53KKs77rlOw9RhkTTAkEUmTMx0GvF/98S3p/C2rm+cC+HSbPkR1up32LhW7WNtc0ckcjYXza4YLoenZe41C3qw+y8KAFsBG6V5tD5Nj3BqVZndmE3K/4NC8McScDBnUCYfrnxeRHumNpOKCHjgn7wOAcTwMS+Nnx7aig2S6pnEtLWNshmWNeCiqV+d3htScxdnes0VaAJMeYxKlpF2KAVDPl9y6uu2 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6822060c-def8-45db-664e-08db0816b399 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:40.0254 (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: CuSL9rlvFjl/HyfoUGZRCwT31ulEpvCM2H5SkvAPmqtVS9b6f+w2qAPpJbKBV/EgUo2/N114qMiod9Bw2sS2cg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Queue-Id: 08E10100009 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: nmip15gkgqiqpa5ex1iwy3ffni9nmwar X-HE-Tag: 1675669781-172449 X-HE-Meta: U2FsdGVkX184ndyRavYFX2ZTWa/6KLRi+PC2kiBfXA/3W108XMQdhVAWKcsQeievJBKeB36YS5dud928WBaYPo3lPYqhmo9kZKp1uz+lQsNg8AxB/efHfzTNPhv36vRPVUxVLCAH1sWblG8KvdA/CAJO3ov3eWmyPOLyq0jOMTEabafY3S1ZIs8A9wzP2WO6u8bYoxYPVwGy+vt8P8ep05MweH4s9whfbeVlfmLN2FgqsM4nIMNRxTqPh1MIx9eM1FpRGFBdnQirV6GA0lkUbkE57g9Aj0zE0NLyuA+MI8nCEzgVxIzpBbq1PVqxrJUqOQUnA9cIJqtsmJp4z+lHkZzRft3OPJhZ4PVbWHcb9MTC6BVbmKT0kvvXxkDIqqXGO9ihYR9/OuiRBsGAvjRLHHbpoTPUqRo2VYxPNFpI4Mjfm1UueN6hjx92wegqdIBk0ZAEfORYzqb8XcOiqZMaGDFrZsADM8Qp++nKgo6cbAipjbHdZ4jDHQBIaObwCILLSOrAKrmmNRkkV6hzalvaKBKsJnHK1Y2Vu1m8gpP38CHgvgAHozdd/UX5SnNClLsmSNKimVpDiBKBLgb5rbdAKwoMO1JBfYOR2QFoyTkw4LxE8D5Y61JMMvRX5A61ab8TORddWVMbok9n/D9rjpCVhMstjb5/tuDwEL638HC4cZzHnR7ljxESW9WbIQ7Jt/8B0ZLxD2COYp2ClRov50+3/ATmkg5Y1aiJYWS8sPLVKXo5iGTcFYrvI7K4F2HBYyzNH211TyClFWgYNYFegQDUeMdqTrbKGHlwnRRugtK6nD/rm4lScSqWgcpE1x+YCQl+KmYhL4nNV2qZ4AeMo158Bcudmmb8WnNY5sDguX5wDZDr3SQpru9hYt6pCi+AdavB7d5d4DQkUeWltmM1GrhGeixVCLf0eM/lTXHWB0v7c5shSsaviabh9uRNljML+tQDLflJ7yAcXt3siAoe9oX z0HJ9Qhp 8LXxetPR55+qdvnuCDu5E2QXd5Eruz1G2bc/oRZ0tBTkVSg/UsLXZSq4FsksTL1Psn0KzD6qRKycb8iRJ3xFFBPNRejPZGzbzNWuGw4GU3l6nQRZJ+Z15gDvIrJdMVuYYUdXpDYraNrV72R5rSOmvm3u8YBi4b47kiTLRlecWcI3guNjdB0w2jSQB9Uy3nwaUFGE0FCMAmgC7WcZVmKMJalKuFXZ7siTT2SPXgNklyGles0W/vOIGHyFziEUeBPL3SRY1wyyQKtB4QUNpctTM7TTVDA5MWuCl5mANuZY3/kgtgV1MvI57xq2YfHOWWiXtyBK93neA04grzz8fSzyVLluTw54dcBrre3KQEDj+Iq7E0Uue3um1eECvyfs+44+pUEvvuiXjPAwS2xI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: To charge pinned pages against the pins cgroup drivers must use the vm_account_pinned() functions which requires initialisation of a struct vm_account. Convert the dfl-afu-region code to do this and charge any pins to the pins cgroup. Signed-off-by: Alistair Popple Cc: Wu Hao Cc: Tom Rix Cc: Moritz Fischer Cc: Xu Yilun Cc: linux-fpga@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/fpga/dfl-afu-dma-region.c | 11 ++++++++--- drivers/fpga/dfl-afu.h | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/fpga/dfl-afu-dma-region.c b/drivers/fpga/dfl-afu-dma-region.c index 02b60fd..3b99784 100644 --- a/drivers/fpga/dfl-afu-dma-region.c +++ b/drivers/fpga/dfl-afu-dma-region.c @@ -38,7 +38,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata, struct device *dev = &pdata->dev->dev; int ret, pinned; - ret = account_locked_vm(current->mm, npages, true); + ret = vm_account_pinned(®ion->vm_account, npages); if (ret) return ret; @@ -67,7 +67,7 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata, free_pages: kfree(region->pages); unlock_vm: - account_locked_vm(current->mm, npages, false); + vm_unaccount_pinned(®ion->vm_account, npages); return ret; } @@ -87,7 +87,7 @@ static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata, unpin_user_pages(region->pages, npages); kfree(region->pages); - account_locked_vm(current->mm, npages, false); + vm_unaccount_pinned(®ion->vm_account, npages); dev_dbg(dev, "%ld pages unpinned\n", npages); } @@ -223,6 +223,7 @@ void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata) afu_dma_unpin_pages(pdata, region); node = rb_next(node); + vm_account_release(®ion->vm_account); kfree(region); } } @@ -322,6 +323,8 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata, region->user_addr = user_addr; region->length = length; + vm_account_init_current(®ion->vm_account); + /* Pin the user memory region */ ret = afu_dma_pin_pages(pdata, region); if (ret) { @@ -365,6 +368,7 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata, unpin_pages: afu_dma_unpin_pages(pdata, region); free_region: + vm_account_release(®ion->vm_account); kfree(region); return ret; } @@ -399,6 +403,7 @@ int afu_dma_unmap_region(struct dfl_feature_platform_data *pdata, u64 iova) dma_unmap_page(dfl_fpga_pdata_to_parent(pdata), region->iova, region->length, DMA_BIDIRECTIONAL); afu_dma_unpin_pages(pdata, region); + vm_account_release(®ion->vm_account); kfree(region); return 0; diff --git a/drivers/fpga/dfl-afu.h b/drivers/fpga/dfl-afu.h index e5020e2..2ca6117 100644 --- a/drivers/fpga/dfl-afu.h +++ b/drivers/fpga/dfl-afu.h @@ -18,6 +18,7 @@ #define __DFL_AFU_H #include +#include #include "dfl.h" @@ -51,6 +52,7 @@ struct dfl_afu_mmio_region { * @in_use: flag to indicate if this region is in_use. */ struct dfl_afu_dma_region { + struct vm_account vm_account; u64 user_addr; u64 length; u64 iova; From patchwork Mon Feb 6 07:47:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129450 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 9BE5DC636D3 for ; Mon, 6 Feb 2023 07:49:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4184A6B0074; Mon, 6 Feb 2023 02:49:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A1446B0075; Mon, 6 Feb 2023 02:49:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CDCC6B0078; Mon, 6 Feb 2023 02:49:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 066916B0074 for ; Mon, 6 Feb 2023 02:49:52 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D5755120BC3 for ; Mon, 6 Feb 2023 07:49:51 +0000 (UTC) X-FDA: 80436092982.04.F24FCC1 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2086.outbound.protection.outlook.com [40.107.223.86]) by imf28.hostedemail.com (Postfix) with ESMTP id EB52BC0002 for ; Mon, 6 Feb 2023 07:49:48 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="nbwt/S3U"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf28.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.86 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669789; 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=67W7G5XqVQ/WEqZY3BEK3/0UYthlxkah8cbfNm84T68=; b=6ex8IYZ5W+I1WUm1NV2HN/hiPWv72mTWHLPS3OpSlMHUXvPKPE3H22QlZgE8EVwL+EvI21 B/8CJ3EFyAG9lWosdzEh8g0Fh5djwqVjWCTWu3ZiHLtzc5ZdONOOmhexQUcBF1gTQepe5p ZKZQIxsJ8mRwxW85M4dlZzSAqkvM0yA= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="nbwt/S3U"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf28.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.86 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669789; a=rsa-sha256; cv=pass; b=bF77mmhhM1KeEKghvanhXCV7LFq8KbfsO11OmriWjawy9gX+MbBkgL7DMmNCfI3eLGVoa2 PhmpL5Rcxvy4cNsOxgnn/Prv1KIb+QlyO4+WMk1CMwzMcxvyiGkJQSBfgzLWdHmD1/SjFt UepwO48TsuGMEW1SIh1jIWlXg1vz9eI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WPMugDb3K7VLwP+qPD3IW09XSncxBAySSWi9HNMG3ZMxVRbB0cT2+2UGt8TO8pA4T+GtX+4v21zMIboTuxT2BpGRIjBWKot9cAM0m4h+q3a08A7is7pOtYZ9k5FeXqCLH4y7ChHRYEArS2mYFwmwM3Lp+HZcbSeaF2ffyUDSjQ/7eTB1U0gz+HFWjxWCirAUKlmZOB4b696k6WbMGawayw7i11sVtdKPXg1er6w/MMQ9b2yhmcx2u86vgOGwEd6A0be7Xfb0sBKEF3X42dOXmzl3A2HfgurTS2f2WV28rPOCXq46zb3EI3Oe+awdfGgW0QnUtljkvB5FqO3+hSAWbg== 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=67W7G5XqVQ/WEqZY3BEK3/0UYthlxkah8cbfNm84T68=; b=iJfJt434uaasZGJCT4ix91v7/hDgybDtscIUYe7yZ/UhmIrrI8GsUytKMXkDhX+iDwtAzm+bVKgkGLJWAPXJaE28H6rvovANPz+nB0oLVo1qATP+OM8gjVbua+q6+QBNpQskwW1LvJ+yjh8NwMkeOvsVeC4V0tkzBaw9zvfLyu//e6HrbFuWdqb6u6r5aEKFYMPtiNv+eMAlFjKHiyHzWnKzDCSm/8gNpUWaf+iZWqeTh3Ntye54BrjHmi3DH6HPw4x5+rtFKG9t4YRPjk4B02BYI/3tnhS+rqCExumUz2ALGAQrr4lAU+7f9EwZQt/RRIyFY/n68dxS/WIMM3m/gQ== 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=67W7G5XqVQ/WEqZY3BEK3/0UYthlxkah8cbfNm84T68=; b=nbwt/S3UZ8XqtXWM0lOLCqHrD7InkYkc90zJhknYiD8orctYZvNKQDN82tCkVSJRF95vZJvbfPvqn4KeJVzW9PEN6NJ5u40Sk7cPDVoA56ULRpeG1klRlzqgr7Q22o8Ry8f5EBj1PziIK4VuT/TDO8qdvhynADmtxR6Ejr7oCVTU5EOiz+ZgqqyFPUjt5cnMRPBIjj7jOLEAoyTOmdtUaD+HpmhW6lpb2Vi59DRsGlF2/gAahgX0cGnky+LmIhINeU2xgfqJhD0QIj0vRR5fdJnkZQNmTfkF0Fsg5q/SNAXnvoz4TmoJQ3AC7rK0dpaJmbCxUVeTLBwWaNxGIklMBg== 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:47 +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:46 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, "Daniel P . Berrange" , Alex Williamson , Alistair Popple , Tejun Heo , Zefan Li , Andrew Morton Subject: [PATCH 14/19] mm: Introduce a cgroup for pinned memory Date: Mon, 6 Feb 2023 18:47:51 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0092.ausprd01.prod.outlook.com (2603:10c6:10:3::32) 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: b4e4a522-d38f-420b-49e5-08db0816b79f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dzr5rw2KFFgMukaQWx8cTx2cDDmEyKDDI8Db41e3wffekUhWiQ0KxQPG967Ny1rCKcsIF3HVTcdjjbE/PxltjyLQc/hoVAD4rEH5XjPboqRy12efrJyo+D2oRRAhMVDbta+ZeM8nYs9dj94DORztZzQ73nO/JSslxm3FySEtLjlsJCC+ToHZUaJD6hxQs0iUyIWOXsG1xU8LrL4J+BWkePxLzp0ab9nlkw52mbN453b3UfOkprggTiGo6PjewO4u3jxRJl+V7sPS8zMOH2jlrSwcgi10QTTz+hz58nWBXkVSOi04euM5PBDByVKNkouEKCvsPJY6aw4fCxBcby6+lrqaFEzeUctNt9JkIbJC7yfx9VdRpJjKgN/M+f6yZzJiTqMGUAcY6Asg4Kdy7RENW9+W9YaioKABJTH6E9kmyMTutXi1bzY4IR9pbiEBRrh4JTl2cmcTF2m223vuIPLCcMFAN4aBZR86mSN28m08LUEOU5e6G48arSv4dgHTj4LRNGCLQqU9FKqGqmvGddzn0yh1RTalKGM4Ai8ht3yfVYMwkZ4ReOM6+s8qQSkOuMB2v0mjqPij5AyHY0plXg95wTjiUBzNYsresxHGs6fZfj8wjEbKyKG7aQmwBEqFZEdX6pASnqKyiwCSVYGGvXUOLQ== 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)(6666004)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(30864003)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jskS2os3I4atajsRYYf6bHzyGFPYQQsZWmjNoxEVxmjCTpPufXCrGIrz0lBPXQRqpjjNJQ6zXuOzSr8X1Z7x/nlQmpNfVQMXg2hWovWTw39sjr/fNi+KVRTM4ygLPHUa6Bx2H4CcCImgifxMNlzJaDJq1eFPkbqefQn5v73RJgYBqM3fZ0FuREe8/48abH5UMot19UtSQ58oKLqSghtIwvf4DCs3FpNbB+W9Y6+SG8WXsRVYyG+YQbbofcH+5ijujmOZpm2uTzVGgEF/wP0ITLrAna4R4E0V7UHzAOxV9XyVBKvSbMlsUTsWttJ9vnc/ON/m9aJR+PcJqicHqPFvyUOJM3Pd77Or2kWPmx+kS4e88FJeMxddxXi9VIHL7bLBdYbMi+loTvJhYF8Pi4AndLT9mFGjIejJ3puyThJL3xpdciJotKBj1LkfdG3wuUHgLq41/ArCZ5I3Q/IKLrZwpOQtdJC7v4sfllAJBka7OPrTzPXOuTEdHWeEoPXXjdI64nQ/1ahz6JtnJhO1MtlBdJwshJY7F4CDqREtuZgTRyXyYBv2CftL3umhymPhA1kVvrSTofu7Wpn8EFRk6Wxama+NxD3YWsk3N7grFVfrQLqw6uECd4AYgDXX2WxFxhvSPE9ai3kBrQVpQRW8FuGtdbHRs8ui4Je01XNBzR/TRzIrQ8NgOUgR5Pu7jOMmvIXiyXOldbicXOqwf/pk0xbcxnr6hxd+y2XnYk5KRMvMukLH5W0DZq9JZ9q4WIcwoiuUxTCuPDauIrbOlq2wEQvwWLU30c2NTIa+XuxLut1bDuE0Sz0Diehqb8hF7zXo26bQe2g6ieYoxIwt7s/RzGAAwzmj3Im9Qxu6vMmLn0khKxd9MbmbfxSDdHNotdxWgB5jLiCOwXolO0wnwhfgHj7vc0bt4/rMMBj9vXpX/DZ5Jfi4qONYptEEtkIU5+bwE9YjOE8Cu38uBSedi0kuC8YjzsSD4gQusqPLV0aDkNWYbDuEY5PtstwZYqCqR4wZKOp2qYX934BLDcJOlSr4PHbJqcclbtfP7QPnaFNQFWHSYaZ7mN8BtA0P2vr6j+NzyUlenBqEav7MhU1FYhPEjaniYxZ3GbiGYPsr7DE5HSrpVyYTOJxPnB3yUcM3apOHHSK02U/QGhtKY26ZITxUK9r/YXNC77fOt1iLFB9xB66aJM1/zwtU+p3W7vW8P0JiJRfcI2U3sHtQq4w9rvbxmy/9n0D9SfRQO9KQcA9zN01mFCdSXQ1PvdZDf1ofuC2IwMCmrz4lKVm2W1dPa2ZR3XiA1zDZ9j3mJui5vVtZNyyRdnpOjgQi4WFUcJIUjN7f4pp6XDsgXWmxo4HKIl18UXnfErBqynfhvveBKFipaVwmIVGezk44shcvMoLTH+QnB69iltd7BUgmF9Ib4k72U5I0MtTsrZahtolSTFAZvId4LXQXnc+lAYPRa1a/Cc0RobnXNoYoK61BHOhXsl7081Nbb69KUKM9CVw8Y3hlJhKJfZfNcetWYCKPSjmrKlPhLI8wLshCs0Qn02n1WkpkpfIdQbYi+zr/gzsDjmXFpe9CGBOZtYTdDpS5rwcJ/MJ0fIFN X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b4e4a522-d38f-420b-49e5-08db0816b79f 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:46.8841 (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: Uw7shkhp0nozyKHfhCyagx37WKAuCW97CcAMHQEQlN5RJCzM4EjouQI4LsjKCvBkTM6QDSpdTe51pNaCtdCbbQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Queue-Id: EB52BC0002 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 1tg9wawhr9uq4ixcu5ediac3xpibfq41 X-HE-Tag: 1675669788-486042 X-HE-Meta: U2FsdGVkX18yUYeoUB6LLVC1Nr2iThFTA7o/X2dmnIgKkvCW7jnMjwjN9Px1j4mYUY5lnn1JgGAzEBlfhkVawE4ozuL6dsDoyfBwzYMqxEVGnMawcu2gvhhnbXmXkFtdA4KaijcblFdvBisCSgrRSBuW9xAtcCAaMmTws4ousa/m+PiVz0rhblPFRjLrAtMjWTZ8g4jv+dCjcZd1UWx13JpUWvc5fsRrNNQsS5nhjc3QeGoy5YRYfIJZSh1P2835gM21nSQUTI0XpmiIxJ3Phvpgucmu3T/f5Tc//rQokkuk7AN5bGMeBCEK+oUOS1O+QbW5LvsopBPu7HQ2dpnPc4bdjaD1LCXnPQNhrZhaKztk+v1U8BitHXkNWKmY5wcheEjwxXDsdUqxIdoKUArJFOli9s/dM5D5tnMHkVSAjOUlH9RtfOLFBWlvXkHQNxbmAt+kvGXCQEdxtx2v+G3KvU6c6qZOO8XqNW63pzk49ipzK4H96bRwVEAOetBhI0bZj6UYMclV2O76STZ82ISpkGro23brmb56F+wso0dDkHmViFQN6vM9KDCxLuc7PmtSVNjYa/3gj0HDqbnVNRmWpe7vUvjks8QOfVYOb1mEZ6i5Hp4TBURRbp1TJRWRWaFgNPdh5RPTqhs3jftCE53ec8MVUia1+zwAyERFghlytF3pEPfxzS13UMmdqREQhDZSxfQd38FdfiQvckVlgWVc/clF0R7zecTghe1HBG5K/mpaJVErZ3XA6TYtEgdN4Rfx9Lbnj1tQVCt+pLc9Ar0TZFXoiz6sJF+BLyV/fQC0e8rnD7NCVulEYvmEDi1daKg8cWNblRJBarx7uzt34Ons58r1892fsj1TQXb2fDWKuSQoj5nJFVaKomb1Of29hDJY/vz6t5XFSna1ryuY/XfkB/ghFFfXP08NxRHcguu+Wb0fLlrHGz1Am52yi6Pg6532NGsrZxKIlaIHfd1Q/sq Erhy+HsI jAJqHyujuX3UNr7zWWfe9s1e6OvhX43PlZfGd6HL/TEV3lyfjQN+5Bwxn5/tbawwCeuk2zQWrD8tlqoz+WA68qdKF0pLQvnzbIaUr/x3wIYdHa95ci0V+Yd0h0GXqsy84aRvsMl/eGxGaWtwhL4XtEU1d6VwTjFnQbcbgx2x96XqUWIK3W9ZGfCfdg16YAC30nTY0lCXxzDsZsmWyMuMnTZbQ1En9auAAgOIKY0jANCWdbZbu3/HJ72amf1sdUfqh6rvqLwj6xprad9cy2J3lg1B0tqcHJhvYnhILoKOrfxFNyGDZ8PdxPMtFEkqtw6X8Vj19OCuJ2oiYtr6qqftCeOHvDK1OYrmapOuKQUdxchc5+mv5r5uhovZNsJQSHf1fabTuleFpIlGh3QLmuPzAWWbJU+59YSM5CrZ8Awg3dm3RUT8taEek6gqHRCdYDI9ai/mpTkeoBu4Z98MpqnK+UFIX4wGnAcXmGFSLQhqNpUnUIj4EB8p9Gh0/gTntRzswoFmM/2agKl1uVYb4q38MW8wxlOVHl7x/oR5/IujEEkmdyy4JIIF7BIzejA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: If too much memory in a system is pinned or locked it can lead to problems such as performance degradation or in the worst case out-of-memory errors as such memory cannot be moved or paged out. In order to prevent users without CAP_IPC_LOCK from causing these issues the amount of memory that can be pinned is typically limited by RLIMIT_MEMLOCK. However this is inflexible as limits can't be shared between tasks and the enforcement of these limits is inconsistent between in-kernel users of pinned memory such as mlock() and device drivers which may also pin pages with pin_user_pages(). To allow for a single limit to be set introduce a cgroup controller which can be used to limit the number of pages being pinned by all tasks in the cgroup. Signed-off-by: Alistair Popple Cc: Tejun Heo Cc: Zefan Li Cc: Johannes Weiner Cc: Andrew Morton Cc: linux-kernel@vger.kernel.org Cc: cgroups@vger.kernel.org Cc: linux-mm@kvack.org --- MAINTAINERS | 7 +- include/linux/cgroup.h | 20 +++- include/linux/cgroup_subsys.h | 4 +- mm/Kconfig | 11 +- mm/Makefile | 1 +- mm/pins_cgroup.c | 273 +++++++++++++++++++++++++++++++++++- 6 files changed, 316 insertions(+) create mode 100644 mm/pins_cgroup.c diff --git a/MAINTAINERS b/MAINTAINERS index f781f93..f8526e2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5381,6 +5381,13 @@ F: tools/testing/selftests/cgroup/memcg_protection.m F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c +CONTROL GROUP - PINNED AND LOCKED MEMORY +M: Alistair Popple +L: cgroups@vger.kernel.org +L: linux-mm@kvack.org +S: Maintained +F: mm/pins_cgroup.c + CORETEMP HARDWARE MONITORING DRIVER M: Fenghua Yu L: linux-hwmon@vger.kernel.org diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 3410aec..d98de25 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -857,4 +857,24 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ +#ifdef CONFIG_CGROUP_PINS + +struct pins_cgroup *get_pins_cg(struct task_struct *task); +void put_pins_cg(struct pins_cgroup *cg); +void pins_uncharge(struct pins_cgroup *pins, int num); +bool pins_try_charge(struct pins_cgroup *pins, int num); + +#else /* CONFIG_CGROUP_PINS */ + +static inline struct pins_cgroup *get_pins_cg(struct task_struct *task) +{ + return NULL; +} + +static inline void put_pins_cg(struct pins_cgroup *cg) {} +static inline void pins_uncharge(struct pins_cgroup *pins, int num) {} +static inline bool pins_try_charge(struct pins_cgroup *pins, int num) { return 1; } + +#endif /* CONFIG_CGROUP_PINS */ + #endif /* _LINUX_CGROUP_H */ diff --git a/include/linux/cgroup_subsys.h b/include/linux/cgroup_subsys.h index 4452354..c1b4aab 100644 --- a/include/linux/cgroup_subsys.h +++ b/include/linux/cgroup_subsys.h @@ -65,6 +65,10 @@ SUBSYS(rdma) SUBSYS(misc) #endif +#if IS_ENABLED(CONFIG_CGROUP_PINS) +SUBSYS(pins) +#endif + /* * The following subsystems are not supported on the default hierarchy. */ diff --git a/mm/Kconfig b/mm/Kconfig index ff7b209..4472043 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1183,6 +1183,17 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config CGROUP_PINS + bool "Cgroup controller for pinned and locked memory" + depends on CGROUPS + default y + help + Having too much memory pinned or locked can lead to system + instability due to increased likelihood of encountering + out-of-memory conditions. Select this option to enable a cgroup + controller which can be used to limit the overall number of + pages procceses in a cgroup may lock or have pinned by drivers. + source "mm/damon/Kconfig" endmenu diff --git a/mm/Makefile b/mm/Makefile index 8e105e5..81db189 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_CGROUP_PINS) += pins_cgroup.o diff --git a/mm/pins_cgroup.c b/mm/pins_cgroup.c new file mode 100644 index 0000000..2d8c6c7 --- /dev/null +++ b/mm/pins_cgroup.c @@ -0,0 +1,273 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Controller for cgroups limiting number of pages pinned for FOLL_LONGETERM. + * + * Copyright (C) 2022 Alistair Popple + */ + +#include +#include +#include +#include +#include +#include + +#define PINS_MAX (-1ULL) +#define PINS_MAX_STR "max" + +struct pins_cgroup { + struct cgroup_subsys_state css; + + atomic64_t counter; + atomic64_t limit; + + struct cgroup_file events_file; + atomic64_t events_limit; +}; + +static struct pins_cgroup *css_pins(struct cgroup_subsys_state *css) +{ + return container_of(css, struct pins_cgroup, css); +} + +static struct pins_cgroup *parent_pins(struct pins_cgroup *pins) +{ + return css_pins(pins->css.parent); +} + +struct pins_cgroup *get_pins_cg(struct task_struct *task) +{ + return css_pins(task_get_css(task, pins_cgrp_id)); +} + +void put_pins_cg(struct pins_cgroup *cg) +{ + css_put(&cg->css); +} + +static struct cgroup_subsys_state * +pins_css_alloc(struct cgroup_subsys_state *parent) +{ + struct pins_cgroup *pins; + + pins = kzalloc(sizeof(struct pins_cgroup), GFP_KERNEL); + if (!pins) + return ERR_PTR(-ENOMEM); + + atomic64_set(&pins->counter, 0); + atomic64_set(&pins->limit, PINS_MAX); + atomic64_set(&pins->events_limit, 0); + return &pins->css; +} + +static void pins_css_free(struct cgroup_subsys_state *css) +{ + kfree(css_pins(css)); +} + +/** + * pins_cancel - uncharge the local pin count + * @pins: the pin cgroup state + * @num: the number of pins to cancel + * + * This function will WARN if the pin count goes under 0, because such a case is + * a bug in the pins controller proper. + */ +void pins_cancel(struct pins_cgroup *pins, int num) +{ + /* + * A negative count (or overflow for that matter) is invalid, + * and indicates a bug in the `pins` controller proper. + */ + WARN_ON_ONCE(atomic64_add_negative(-num, &pins->counter)); +} + +/** + * pins_uncharge - hierarchically uncharge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to uncharge + */ +void pins_uncharge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p; + + for (p = pins; parent_pins(p); p = parent_pins(p)) + pins_cancel(p, num); +} + +/** + * pins_charge - hierarchically charge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to charge + * + * This function does *not* follow the pin limit set. It cannot fail and the new + * pin count may exceed the limit. This is only used for reverting failed + * attaches, where there is no other way out than violating the limit. + */ +static void pins_charge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p; + + for (p = pins; parent_pins(p); p = parent_pins(p)) + atomic64_add(num, &p->counter); +} + +/** + * pins_try_charge - hierarchically try to charge the pin count + * @pins: the pin cgroup state + * @num: the number of pins to charge + * + * This function follows the set limit. It will fail if the charge would cause + * the new value to exceed the hierarchical limit. Returns true if the charge + * succeeded, false otherwise. + */ +bool pins_try_charge(struct pins_cgroup *pins, int num) +{ + struct pins_cgroup *p, *q; + + for (p = pins; parent_pins(p); p = parent_pins(p)) { + uint64_t new = atomic64_add_return(num, &p->counter); + uint64_t limit = atomic64_read(&p->limit); + + if (limit != PINS_MAX && new > limit) + goto revert; + } + + return true; + +revert: + for (q = pins; q != p; q = parent_pins(q)) + pins_cancel(q, num); + pins_cancel(p, num); + + return false; +} + +static int pins_can_attach(struct cgroup_taskset *tset) +{ + struct cgroup_subsys_state *dst_css; + struct task_struct *task; + + cgroup_taskset_for_each(task, dst_css, tset) { + struct pins_cgroup *pins = css_pins(dst_css); + struct cgroup_subsys_state *old_css; + struct pins_cgroup *old_pins; + + old_css = task_css(task, pins_cgrp_id); + old_pins = css_pins(old_css); + + pins_charge(pins, task->mm->locked_vm); + pins_uncharge(old_pins, task->mm->locked_vm); + } + + return 0; +} + +static void pins_cancel_attach(struct cgroup_taskset *tset) +{ + struct cgroup_subsys_state *dst_css; + struct task_struct *task; + + cgroup_taskset_for_each(task, dst_css, tset) { + struct pins_cgroup *pins = css_pins(dst_css); + struct cgroup_subsys_state *old_css; + struct pins_cgroup *old_pins; + + old_css = task_css(task, pins_cgrp_id); + old_pins = css_pins(old_css); + + pins_charge(old_pins, task->mm->locked_vm); + pins_uncharge(pins, task->mm->locked_vm); + } +} + + +static ssize_t pins_max_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off) +{ + struct cgroup_subsys_state *css = of_css(of); + struct pins_cgroup *pins = css_pins(css); + uint64_t limit; + int err; + + buf = strstrip(buf); + if (!strcmp(buf, PINS_MAX_STR)) { + limit = PINS_MAX; + goto set_limit; + } + + err = kstrtoll(buf, 0, &limit); + if (err) + return err; + + if (limit < 0 || limit >= PINS_MAX) + return -EINVAL; + +set_limit: + /* + * Limit updates don't need to be mutex'd, since it isn't + * critical that any racing fork()s follow the new limit. + */ + atomic64_set(&pins->limit, limit); + return nbytes; +} + +static int pins_max_show(struct seq_file *sf, void *v) +{ + struct cgroup_subsys_state *css = seq_css(sf); + struct pins_cgroup *pins = css_pins(css); + uint64_t limit = atomic64_read(&pins->limit); + + if (limit >= PINS_MAX) + seq_printf(sf, "%s\n", PINS_MAX_STR); + else + seq_printf(sf, "%lld\n", limit); + + return 0; +} + +static s64 pins_current_read(struct cgroup_subsys_state *css, + struct cftype *cft) +{ + struct pins_cgroup *pins = css_pins(css); + + return atomic64_read(&pins->counter); +} + +static int pins_events_show(struct seq_file *sf, void *v) +{ + struct pins_cgroup *pins = css_pins(seq_css(sf)); + + seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pins->events_limit)); + return 0; +} + +static struct cftype pins_files[] = { + { + .name = "max", + .write = pins_max_write, + .seq_show = pins_max_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "current", + .read_s64 = pins_current_read, + .flags = CFTYPE_NOT_ON_ROOT, + }, + { + .name = "events", + .seq_show = pins_events_show, + .file_offset = offsetof(struct pins_cgroup, events_file), + .flags = CFTYPE_NOT_ON_ROOT, + }, + { } /* terminate */ +}; + +struct cgroup_subsys pins_cgrp_subsys = { + .css_alloc = pins_css_alloc, + .css_free = pins_css_free, + .legacy_cftypes = pins_files, + .dfl_cftypes = pins_files, + .can_attach = pins_can_attach, + .cancel_attach = pins_cancel_attach, +}; From patchwork 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-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mnSJNOyeedE/vXRDi3G0lpuoTZbj75HMvgFQ9We9gTKg6PSYlqO74q6IDUFfjFr04XmgDJczXXyKkSlm1WHJD9QJT87bVdC0vvGkqbdq8tlObilm55D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qzXmPffzEpDYU4VtlapiZwJjdbs7obqLtDhVqqmkoJxBhVC1XGJlZaDkbga9JRyQvyLI9Yw4JWc6uzM50GlzfDg4ZcQ92slsJCc4SY4NIWfvcgw06RBrQlgFdVHnZ3HgR3XWWsDT0NfvK66AnYXmjjml2xusIn8iZY+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-Signature: i8yzgsjywpwqd1wym1zqrqrt658gzef7 X-Rspam-User: X-HE-Tag: 1675669795-190530 X-HE-Meta: U2FsdGVkX19RH87Sj+4ycdpa+tV+WiWXz8S4xZqF+YJ2bFhPDlfwBaV4LZDcVIV2+O09XBdfxkrC4d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N+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); From patchwork Mon Feb 6 07:47:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129452 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 D1856C63797 for ; Mon, 6 Feb 2023 07:50:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AFC86B0078; Mon, 6 Feb 2023 02:50:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6398B6B007B; Mon, 6 Feb 2023 02:50:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 464656B007D; Mon, 6 Feb 2023 02:50:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 30CC36B0078 for ; Mon, 6 Feb 2023 02:50:05 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 01DA380B28 for ; Mon, 6 Feb 2023 07:50:04 +0000 (UTC) X-FDA: 80436093570.17.546F568 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2084.outbound.protection.outlook.com [40.107.223.84]) by imf14.hostedemail.com (Postfix) with ESMTP id 3E1C8100002 for ; Mon, 6 Feb 2023 07:50:02 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=XW8hnGQn; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.84 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669802; 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=QEHW8jtg1/hfxonS1lsmN1J+cQAP43J/WL1D3f0LfQQ=; b=lcw+U48vyqn+xRapt+2V0Sr7BfvfYvcvcKd+qxE0U1qeSbp8pq+hZ23Q3jL75EZPhXIQvG LU96zbv15oTEBri87/SlhQikXLmXtPPIKZzQUM8uRz4Hy7Y602QA5oBXqglwX+rVZ0LHy7 loHF7O5QDLTixfYSdOpJqgSQWpAPXYU= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=XW8hnGQn; spf=pass (imf14.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.84 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669802; a=rsa-sha256; cv=pass; b=DICkghKV0m0nT2h6h757Y/Ia/i7Z4InAKFeoa7IH/DCW+CKbGm3zjlF+44B6u2hdaM63K8 JGFDl7qBgNh5SpeXlvNucZxwl/uEHXARFbz5i7ycZo6WS4+XuLtGPdymbXIXVDd6mB8paS xNN8x6PtnDtKK0yZVzsirT5nnzOhfKI= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kWjQetCOL1IOxz7vpvyfK+0Cd33y/o/NEcpXxKh5P1+ZrQf+YsEjwinD1l9fg3ISZgzzYrDVQCLlPzl4l/i3OS5EoiAY/Gp3M7fqv6ksk2Em5l/xQpK9mvehByBXmbAqCEzGq/K2Hds8SeTYx/4vVaZ64XaSrrBnY34644XeCGSL7l/tsMZ50jKAAeBmZ/l6D6gQO+6gsndnaqgSdKA2V7Ap6vW4LjMQWqLD4Ho4TboPACcBG4PTJDoVp7hHWAG9lK3oS3OxkGyml2v62VRcByTpix8HzAlXv+aGhj5McyBO7lMLue7xjCPByVpT3lHRYeyxRx1UpAJHF7ValgfJWg== 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=QEHW8jtg1/hfxonS1lsmN1J+cQAP43J/WL1D3f0LfQQ=; b=jJvbWqLxgbYxhQptiGJXZQiYcU6kVPKGSIr5duSAFPMglMnxQvO3lj0Wt2kq646uuAaNQXn60s2CAYap/nh28jmW7CZxp20MCAuIEhs2HZOBMxbftTVfFT4Pw8KZk7nHXTqyOE8Tp6sDfUa+VAY3Q03AHtWdlijr37Lg+JF9Z5OcqrdIuaUYrLi3t4VosaRO3fyu/i9isf9y+SlhfEQ4r7N8eoUKsQE3yrBe779dFfl3xIVuFSwucYn7ocHk/SeBWYKz0phl6HuARJzbxrzH8Pp5lnAOQj/K77VCSzdqTYqxlqbf8cpQp0StmFpOF5pPRTjGMQCgsKr+BLfxtnsNZg== 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=QEHW8jtg1/hfxonS1lsmN1J+cQAP43J/WL1D3f0LfQQ=; b=XW8hnGQniuHn9VXIbENHf4WtjGwfzYug4MygncqFpMEtxF1AB+JDfJQ1Tkx2Z1NKd9y1GTZ51iOmErTZM6w6prtjsRFCVBSZg1hsJDgXKUb1Th6VDzUvEg2T3VP1bcZ3Lo3GLKGvYUh8ohsGixuvfGHKyUnQaZIpI+9O3muKXUBYJUmhq/4iyR95wj9mSBDig0OJy5h6iUf/27sY4hyBibl4jcQQVcf4W7F6y83+FPNdEuihY0/h/lsaIkmNJzeeR+Ag2vKMhiSBL9+BlpZ9iUbwG5fozIsD8hnjq5ILOrfmpSxFb8QwFkky4+FBBWx/GLbNeKUNFqVI6IBwKkrY5Q== 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:50:00 +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:50:00 +0000 From: Alistair Popple To: linux-mm@kvack.org, cgroups@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jgg@nvidia.com, jhubbard@nvidia.com, tjmercier@google.com, hannes@cmpxchg.org, surenb@google.com, mkoutny@suse.com, daniel@ffwll.ch, "Daniel P . Berrange" , Alex Williamson , Alistair Popple Subject: [PATCH 16/19] mm/util: Refactor account_locked_vm Date: Mon, 6 Feb 2023 18:47:53 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0093.ausprd01.prod.outlook.com (2603:10c6:10:3::33) 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: 9db330a4-1aa2-4d64-980f-08db0816bfd3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5kM4XDhr1D/mKBdbDY2lMn7dtF5X4TVQ1MEhmUz9G4uhDzwM/fO9qVuffLHUmSXdIVbM4BCFQe648p+NeM3zfRwZ2Ra42/Dajim+M31nKdkWgb6v0pF3KuhTzGPfwlf6H75gVGI1T9SslnFNnGwQFbo/r9IZs12whuQKvpyzadBANlhPdYvzTiE6xzXT6LuGt6lT5ILcwtCYGNe5hhC4kpCwS0bS1jrggvM8zC17mU5xPpLfbpVdfaPgc+JCTnSRDsPN3rPlNX4/FQM+kdn/Cmy3zfuSQGd7XJXTaSN2VatDvI2/x+urvba8ndJroHF10ZAoo2f//rN8QMBwZKna7AgGGR5dQJN6SzeuLUjKvrCGDkvR6BSHLlK2JDfin7xVkeKAzQBlVouYhx3ARnvQSOgjMitv2LsXJ34qTPs9FHXtR/V0jfGFyd5q+bUz9X7LloOq1lb/iArV72f6ZI13t7NGPI2rGlC+coHWE95Ts6wUNAgVqe0mHbSwpXVFbqvnS+lW/cwJMMeXF4jYlj48nq7KrL8hwJw4ZlXi/fe5DTVEmiKXDUrWURnfdwd95SWoIbaDSfqJ/V2QvOhtpLErYVmTE4ASfMdmE00mlgk4cs+mlpNWZblJKfmHykx7F5/klB3UIl2K65XkI47f02oUhg== 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)(6666004)(107886003)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(15650500001)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0DxbcFt2BlVgcJiQ7dPYPczJRG/zZP8NGAJeoBO9QntBrkcy1PqALwI05MnOhxHIy9VWSg1yEEvQMT/Rp5yOsnyG9IRhSj1UaptFhWXJ6zETbDr+aR4L/FXJyRqX02HVYIg41Dr6nyeXp1IdEyG0xypxg6s7tfHJYVv+mq4a9o6TVyfqfJQak4HhmllIeVmZvmAijsSClBS9b4ogCE3CiKtfEPi23WbUJUKvU55GuQ8rJHdw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Ss0MyVxYftgXlXJPnhLGthXaCI7ZkHSuJr/5SNfV+ac4NEcYYbo9mVNQl0LGuj/ZEosqHmsyMm5Xi/zZ8Hj1jG6GygZnYLFRZKV+doYb372Amzc1DSRGHE6FPQA8yUxD2SuqqNXTSEP6/QKRdP+m4tBky0t4vshezrRANjGhpQl0JWn9dSSUoQHaQDPrh7rkeVyEaF7SfsSTcONN76wJcI+9mvcNDMIuZAQnHzPpwMYKwWC2Sy/B8A58MbvnbWGYnUnH74W X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9db330a4-1aa2-4d64-980f-08db0816bfd3 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:50:00.5081 (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: AI9Z7Fltk+94VB3iChMgHrGBmz4J5uTx5CyN5GljI4YVl933AZgacw6g7m5Ot/i+vbp/LPl8YNNGjrar9RWcrw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Queue-Id: 3E1C8100002 X-Stat-Signature: e4xhgb5odx6ep31sqbhne3iyhjcngu5j X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1675669802-840102 X-HE-Meta: U2FsdGVkX1/vYJELSRIZ84jjnZ8IJvaQv7A9vM/EsGbamJFkRI+yG2J2isVNP1y0WmQTgp8w46frpYqsM6kGg33lorgSxwetcxThta8osvICGVtv3KZGI1PpS7NPHlWg9B+8orpj02rhHLmSq0wX24eJvzFapf/Hllz+6EeeFGvr55rbJ9Oy/kIuGnsC9BujvSBcCPEOaABfHHVVOyJQLKDw1BKkcofJ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1gZd48kBKnKHPeCCSM69tTEPL47ctSjnc35SERF5Wk5JYFkcic6Ex1l2SUmM/2tf2lkqeiatZWKO0v5arkptoayd8ueCIASLk1pdFAvJzi1GA4+NvGz/+GtflY1WxrJhJzV4gA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: account_locked_vm() takes a flag to indicate if pages are being accounted or unaccounted for. A flag is also provided to bypass rlimits. However unaccounting of pages always succeeds and the flag to ignore the limits is ignored. The flags make calling code harder to understand so refactor the accounting and unaccounting paths into separate functions. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- include/linux/mm.h | 5 +-- mm/util.c | 73 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f85716..126b756 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2090,9 +2090,10 @@ int get_user_pages_fast(unsigned long start, int nr_pages, int pin_user_pages_fast(unsigned long start, int nr_pages, unsigned int gup_flags, struct page **pages); -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc); -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int account_locked_vm(struct mm_struct *mm, unsigned long pages); +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim); +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages); struct kvec; int get_kernel_pages(const struct kvec *iov, int nr_pages, int write, diff --git a/mm/util.c b/mm/util.c index 0e93625..1ca0dfe 100644 --- a/mm/util.c +++ b/mm/util.c @@ -575,7 +575,6 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * __account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * @task: task used to check RLIMIT_MEMLOCK * @bypass_rlim: %true if checking RLIMIT_MEMLOCK should be skipped * @@ -586,7 +585,7 @@ EXPORT_SYMBOL_GPL(vm_unaccount_pinned); * * 0 on success * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, +int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; @@ -595,33 +594,44 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc, mmap_assert_write_locked(mm); locked_vm = mm->locked_vm; - if (inc) { - if (!bypass_rlim) { - limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) - ret = -ENOMEM; - } - if (!ret) - mm->locked_vm = locked_vm + pages; - } else { - WARN_ON_ONCE(pages > locked_vm); - mm->locked_vm = locked_vm - pages; + if (!bypass_rlim) { + limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; + if (locked_vm + pages > limit) + ret = -ENOMEM; } - pr_debug("%s: [%d] caller %ps %c%lu %lu/%lu%s\n", __func__, task->pid, - (void *)_RET_IP_, (inc) ? '+' : '-', pages << PAGE_SHIFT, - locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), - ret ? " - exceeded" : ""); + if (!ret) + mm->locked_vm = locked_vm + pages; + + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); /** + * __unaccount_locked_vm - unaccount locked pages to an mm's locked_vm + * @mm: mm to account against + * @pages: number of pages to account + * + * Assumes @mm are valid and that mmap_lock is held as writer. + */ +void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + unsigned long locked_vm = mm->locked_vm; + + mmap_assert_write_locked(mm); + WARN_ON_ONCE(pages > locked_vm); + mm->locked_vm = locked_vm - pages; +} +EXPORT_SYMBOL_GPL(__unaccount_locked_vm); + +/** * account_locked_vm - account locked pages to an mm's locked_vm * @mm: mm to account against, may be NULL * @pages: number of pages to account - * @inc: %true if @pages should be considered positive, %false if not * * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). * @@ -629,7 +639,7 @@ EXPORT_SYMBOL_GPL(__account_locked_vm); * * 0 on success, or if mm is NULL * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. */ -int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) +int account_locked_vm(struct mm_struct *mm, unsigned long pages) { int ret; @@ -637,14 +647,35 @@ int account_locked_vm(struct mm_struct *mm, unsigned long pages, bool inc) return 0; mmap_write_lock(mm); - ret = __account_locked_vm(mm, pages, inc, current, - capable(CAP_IPC_LOCK)); + ret = __account_locked_vm(mm, pages, current, capable(CAP_IPC_LOCK)); mmap_write_unlock(mm); return ret; } EXPORT_SYMBOL_GPL(account_locked_vm); +/** + * unaccount_locked_vm - account locked pages to an mm's locked_vm + * @mm: mm to account against, may be NULL + * @pages: number of pages to account + * + * Assumes a non-NULL @mm is valid (i.e. at least one reference on it). + * + * Return: + * * 0 on success, or if mm is NULL + * * -ENOMEM if RLIMIT_MEMLOCK would be exceeded. + */ +void unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) +{ + if (pages == 0 || !mm) + return; + + mmap_write_lock(mm); + __unaccount_locked_vm(mm, pages); + mmap_write_unlock(mm); +} +EXPORT_SYMBOL_GPL(unaccount_locked_vm); + unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long pgoff) From patchwork Mon Feb 6 07:47:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129453 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 B3451C63797 for ; Mon, 6 Feb 2023 07:50:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 577546B007D; Mon, 6 Feb 2023 02:50:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 500096B0085; Mon, 6 Feb 2023 02:50:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32C556B0087; Mon, 6 Feb 2023 02:50:12 -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 1B2FD6B007D for ; Mon, 6 Feb 2023 02:50:12 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EE1C81C633F for ; Mon, 6 Feb 2023 07:50:11 +0000 (UTC) X-FDA: 80436093822.28.A5ECF17 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2081.outbound.protection.outlook.com [40.107.223.81]) by imf01.hostedemail.com (Postfix) with ESMTP id 3983240007 for ; Mon, 6 Feb 2023 07:50:08 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=HiTkcGel; spf=pass (imf01.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.81 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669809; a=rsa-sha256; cv=pass; b=RVe8b2EhZwyhzdZQdrdh7kwulNVQ8DUUDpvkhNbaAuY1KLdhTSGEhallzmAw/cEgDWgczT GTVbUI5zHZhH4d9NbMOoWC4ytwpKxkwpVTxy1vM+w1Z/8wZm3aFJBAKTlpxrU99PizSGZy S8Kp9Di9x5NuKkTRWF+Vb6NnbOFL1Es= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=HiTkcGel; spf=pass (imf01.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.81 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669809; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=E0KIODYt8WUULUheOq/0bzhoYsb/z2oM3kbJh7RbyN8Oegz9M/xvU9juRRqtwNCi5tV0hz Z7ZANf0CV4y5uPar2eDy7sRORNpqMJhZZl8xKsX2gu0P2tDTfQTpp5mqOqYH+SDfYJO0BU pH/VPXiIojr5IXQIMZMdRklez24bvck= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MPAS6xf1LAX8nbqvyAPjry7Ok3FZJMWJFBf9Fbo8M7i7sIyOpVEhUTfhlZUz0lTROVxTyxkyNX8XSxpc0ToKn9Bcl+wTUp/nPlodlxGD762wQAjKIs5ciz4+XUdeAIPJrMlzl/BH13sjo0ChWjOn2CfuhXw4ihTGVUibE+4fxTDaI9w+6qDzp+c848l1tpG6cdS0VhZ6HQil/1p0fcSmCGYgswbXfnvHwP855kDAV2Cj7ikvO2z12It+p5IHbf538E31u82Awpf4ywAXDu+PJN74fYdT0wupoE7Ln2rm6Fw3Gp+Jsxvrr0bjAZrkRmrvk4EyEInejJQMQg2wLa3DxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=nrw/buuxNkvje+ziK0hFoBOp6Zue6IEW526Bxf9/QKOe7o4hK7U84Nf9DHdlXw/6J6v2k8ZfgpBQUlRjzNvt/ojioy6TPEGszu5u9n1xOx+O0XOwwSIVJvwev+yzBijqEHhJRyfTeARTMEZtTt4Rgr5Tbc7dKtU+jVbEZcyzmTnO8thAyT0YtAmnHB3JPc4lDf6Z7HS0Rf8vP3/MaO0B/nIeQnF7403I1KKYPtWhSMYb508Fe1lnRwCIR7ArwdVpjoFBwl6ABv37nk6GMn4I3VKMs0q4mKBZH9gt0YVXoXNWlRBYkMVEgAzJZLjZjrU9PMZ1h+PPyVkT5YindexsQw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UULxjfjg3cKi8O85puJF65UHMaoo5iv3EW5HbTo5aKQ=; b=HiTkcGelB2wmweWAVNOmNkdU0rKyyXrnj7D3F7u2iDvA97S4KIV+yn7o+6KkADoYBokw3zgP5YOldpcyfFY2QcbYkmNEUUdS9DCvsGa0toTCaBaG1exG0y/r9nWMQB3TzCd53GzIfEGiArBODGSaUuj7pNrqRR1H4iCkvuKqiUiinGbdAT4MmQQyCNmm/wSmt4Cfr7oLi0Z32OOzFN8Jd6Ht859/mwAYJhNSTTgAlPvgCTFef94HceTm07YxCtjiSQdhpcW/BsrhCVWxlJ6Jdxyuwl1TKZpgQza6x5k+x+ce/GI+PeI1UhfdDhvIwevcuE186gG6pEUIM+ufeb94bQ== 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:50:07 +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:50:07 +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 17/19] mm: Convert mmap and mlock to use account_locked_vm Date: Mon, 6 Feb 2023 18:47:54 +1100 Message-Id: <83b0638b0aa88ac57656111ea9e1e771f2b755e3.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYYP282CA0001.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:b4::11) 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: c719672c-a25e-4472-baf3-08db0816c396 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TzvseV88qNLSsZY++QftlI/fk/YJ15Zq6eR8ZNKN5dXaNb8Lh7bOQjT5MHKOUHHMS87hBP/D4NNeho4WMdU1Sto3z+mY995YURjRPpRmqBgQ4OYyZVALEG6zP+z0PJ/B2nnFtRcw/2uT3TPXDLq8H/z4BBLRZFbJTV+GpGF5HDh6vmRbkX29fP3IOA9ky89b9i9dyMJUw6XKRcjOpauYyP63/31+oooepryfo1hXul4886/U/uHRKm5ClV/W2XmvIfZ1LDo8Km5vSQiE/rfD645H7jDN15IT1SddAB6uK4fMuQPl+vomKNjmyNnMykGQK/DQBo1Czc2mXkmhXYQNsKxufbMRawcHX8LZDPreYE1GqmVD79AyJ59YRqjucmUWAPsIbImdtDhY9Gh8TqK7W6cH8wELYsAL0c5hxNwv+d2195KsriKVjwkhh4eQmcuppsc0w718QOkleqgNYIfEkdoVpNyI9S6BdNKlQQAn7LXbM35V1Dskkb/MBFy65iCl+RUCs9Qn7dcDdzmKGIT6b1YSAjJkBV/hcNT33tPDXBV2pdlitg0ImMyV3OcJXSYzYp2PXaakD+GM4nFQK0Z4WVswVij6RUOfjksG2XHlpdnVrEC3qD2anI3R4uCjqsGvo8i3vxSOwdb110VsFCTulA== 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)(6666004)(107886003)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(15650500001)(30864003)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476007)(8676002)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: IJLIgaaFcbO0fRdT4i/Qk3vSH8n7ATsGeJRSU1NpCfmbtbM2AKURep5S4/XtHV/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HEGYNj8iYZs2sFNPCrxblrmWV9RiuX8t1fStaNVmb7slV1i8G0N291yVnleZFvPowgOPtSva2b/ag706vlV6gHhaC3fxip5RiOsUkUX6Po5UJvpgcQoaYywoWt5ZX//fWkqaIWdkhpAlFNpd9NcQByP8WQroPkHqQ/n8jdfY/gGOCtHeWjnAUJO4u7r2suiyKWrYGjD+CeSEAjfsGh4oQdNqKsTPMbo97MVttir1WHiiBT6xtdJqrhafWAEOdzRKtsVs9IdFMGGZJeWtnWjPh2cUVrQZVptM+TcCrCN+iXgXnywIFoBKVgFRiqSu5i0dXnZyAFg5x2XEK1jTPN/p5nZenjWcsIA1nlwjiGMZIrTmbm5P3DNrMxE4bBls0bIabweDP+ykMda64BJqQV5t/xBJS0g3BNqO+rzA+vK3ycsLQMuZGShetGPxi4E0sFkQa X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c719672c-a25e-4472-baf3-08db0816c396 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:50:06.9451 (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: mwpity7BF1x59t16Wj0FBo9LE+yZ69ukmJnQWN1euVwEo38i77kLduaxC/oj+AhQ3BGF+elulwNSAobgy9fsxQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspam-User: X-Rspamd-Queue-Id: 3983240007 X-Rspamd-Server: rspam01 X-Stat-Signature: oxmyqif3prw9ts7agsp3w1nfedczxjma X-HE-Tag: 1675669808-91029 X-HE-Meta: U2FsdGVkX19krzbRYxm9cAbH4B6Q4e0/7s662zmr2lH8rtwRTSTg1AM/r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lVO7or18OKgD85CMzwDt1Hf9KNBr34XZS3sZ0+JZtQCitIYFSJGLucbXUZm7cICz/GgOckY5wzB6CKXAZzmgABP4OTugIFeEwMuICFI+ToGPkNPRdeMvL0curUAKCGqomL7r/ZS9gGixCzJfB9atul+9WfSPkn14sTZuj6t63n+5ieR0NRk4QkkYUp7uV6THLDOwjM1xYQBcg7Tv1ej3e9ShptlaGCPNvRpCJDDOsQ0CojMeb10o8CRDMBdIL1HhPq2jctjzD83dV+LagKD1J92MAtCVMAZ4e9Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: A future change introduces a cgroup to control the amount of locked/pinned memory on the system. To ensure memory pinned via mlock and mmap is accounted for use the common account_locked_vm() function. As cgroups can outlive individual processes also unaccount for the locked memory during process teardown. This patch should introduce no user visible change. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/internal.h | 2 +- mm/mlock.c | 76 ++++++++++----------------------------------------- mm/mmap.c | 76 +++++++++++++++++++++++++-------------------------- mm/mremap.c | 54 ++++++++++++++++++++++++++---------- mm/secretmem.c | 6 +--- 5 files changed, 95 insertions(+), 119 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index bcf75a8..7c8c3f2 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -489,8 +489,6 @@ extern long populate_vma_page_range(struct vm_area_struct *vma, extern long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long end, bool write, int *locked); -extern int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len); /* * mlock_vma_page() and munlock_vma_page(): * should be called with vma's mmap_lock held for read or write, diff --git a/mm/mlock.c b/mm/mlock.c index 7032f6d..a97c8c5 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -416,6 +416,20 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out; + /* + * Keep track of amount of locked VM. + */ + nr_pages = (end - start) >> PAGE_SHIFT; + if (!(newflags & VM_LOCKED)) { + __unaccount_locked_vm(mm, nr_pages); + } else if (!(oldflags & VM_LOCKED)) { + if (__account_locked_vm(mm, nr_pages, current, + capable(CAP_IPC_LOCK))) { + ret = -ENOMEM; + goto out; + } + } + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); *prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), @@ -439,16 +453,6 @@ static int mlock_fixup(struct vm_area_struct *vma, struct vm_area_struct **prev, success: /* - * Keep track of amount of locked VM. - */ - nr_pages = (end - start) >> PAGE_SHIFT; - if (!(newflags & VM_LOCKED)) - nr_pages = -nr_pages; - else if (oldflags & VM_LOCKED) - nr_pages = 0; - mm->locked_vm += nr_pages; - - /* * vm_flags is protected by the mmap_lock held in write mode. * It's okay if try_to_unmap_one unmaps a page just after we * set VM_LOCKED, populate_vma_page_range will bring it back. @@ -517,42 +521,6 @@ static int apply_vma_lock_flags(unsigned long start, size_t len, } /* - * Go through vma areas and sum size of mlocked - * vma pages, as return value. - * Note deferred memory locking case(mlock2(,,MLOCK_ONFAULT) - * is also counted. - * Return value: previously mlocked page counts - */ -static unsigned long count_mm_mlocked_page_nr(struct mm_struct *mm, - unsigned long start, size_t len) -{ - struct vm_area_struct *vma; - unsigned long count = 0; - unsigned long end; - VMA_ITERATOR(vmi, mm, start); - - /* Don't overflow past ULONG_MAX */ - if (unlikely(ULONG_MAX - len < start)) - end = ULONG_MAX; - else - end = start + len; - - for_each_vma_range(vmi, vma, end) { - if (vma->vm_flags & VM_LOCKED) { - if (start > vma->vm_start) - count -= (start - vma->vm_start); - if (end < vma->vm_end) { - count += end - vma->vm_start; - break; - } - count += vma->vm_end - vma->vm_start; - } - } - - return count >> PAGE_SHIFT; -} - -/* * convert get_user_pages() return value to posix mlock() error */ static int __mlock_posix_error_return(long retval) @@ -585,21 +553,7 @@ static __must_check int do_mlock(unsigned long start, size_t len, vm_flags_t fla if (mmap_write_lock_killable(current->mm)) return -EINTR; - locked += current->mm->locked_vm; - if ((locked > lock_limit) && (!capable(CAP_IPC_LOCK))) { - /* - * It is possible that the regions requested intersect with - * previously mlocked areas, that part area in "mm->locked_vm" - * should not be counted to new mlock increment count. So check - * and adjust locked count if necessary. - */ - locked -= count_mm_mlocked_page_nr(current->mm, - start, len); - } - - /* check against resource limits */ - if ((locked <= lock_limit) || capable(CAP_IPC_LOCK)) - error = apply_vma_lock_flags(start, len, flags); + error = apply_vma_lock_flags(start, len, flags); mmap_write_unlock(current->mm); if (error) diff --git a/mm/mmap.c b/mm/mmap.c index 425a934..2c05582 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -160,7 +160,7 @@ static int check_brk_limits(unsigned long addr, unsigned long len) if (IS_ERR_VALUE(mapped_addr)) return mapped_addr; - return mlock_future_check(current->mm, current->mm->def_flags, len); + return 0; } static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma, unsigned long newbrk, unsigned long oldbrk, @@ -1184,23 +1184,6 @@ static inline unsigned long round_hint_to_min(unsigned long hint) return hint; } -int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len) -{ - unsigned long locked, lock_limit; - - /* mlock MCL_FUTURE? */ - if (flags & VM_LOCKED) { - locked = len >> PAGE_SHIFT; - locked += mm->locked_vm; - lock_limit = rlimit(RLIMIT_MEMLOCK); - lock_limit >>= PAGE_SHIFT; - if (locked > lock_limit && !capable(CAP_IPC_LOCK)) - return -EAGAIN; - } - return 0; -} - static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) { if (S_ISREG(inode->i_mode)) @@ -1310,9 +1293,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!can_do_mlock()) return -EPERM; - if (mlock_future_check(mm, vm_flags, len)) - return -EAGAIN; - if (file) { struct inode *inode = file_inode(file); unsigned long flags_mask; @@ -1882,22 +1862,27 @@ static int acct_stack_growth(struct vm_area_struct *vma, if (size > rlimit(RLIMIT_STACK)) return -ENOMEM; - /* mlock limit tests */ - if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) - return -ENOMEM; - /* Check to ensure the stack will not grow into a hugetlb-only region */ new_start = (vma->vm_flags & VM_GROWSUP) ? vma->vm_start : vma->vm_end - size; if (is_hugepage_only_range(vma->vm_mm, new_start, size)) return -EFAULT; + /* mlock limit tests */ + if (vma->vm_flags & VM_LOCKED) + if (__account_locked_vm(mm, grow << PAGE_SHIFT, current, + capable(CAP_IPC_LOCK))) + return -ENOMEM; + /* * Overcommit.. This must be the final test, as it will * update security statistics. */ - if (security_vm_enough_memory_mm(mm, grow)) + if (security_vm_enough_memory_mm(mm, grow)) { + if (vma->vm_flags & VM_LOCKED) + __unaccount_locked_vm(mm, grow << PAGE_SHIFT); return -ENOMEM; + } return 0; } @@ -1975,8 +1960,6 @@ int expand_upwards(struct vm_area_struct *vma, unsigned long address) * to guard against concurrent vma expansions. */ spin_lock(&mm->page_table_lock); - if (vma->vm_flags & VM_LOCKED) - mm->locked_vm += grow; vm_stat_account(mm, vma->vm_flags, grow); anon_vma_interval_tree_pre_update_vma(vma); vma->vm_end = address; @@ -2056,8 +2039,6 @@ int expand_downwards(struct vm_area_struct *vma, unsigned long address) * to guard against concurrent vma expansions. */ spin_lock(&mm->page_table_lock); - if (vma->vm_flags & VM_LOCKED) - mm->locked_vm += grow; vm_stat_account(mm, vma->vm_flags, grow); anon_vma_interval_tree_pre_update_vma(vma); vma->vm_start = address; @@ -2281,7 +2262,7 @@ static inline int munmap_sidetree(struct vm_area_struct *vma, return -ENOMEM; if (vma->vm_flags & VM_LOCKED) - vma->vm_mm->locked_vm -= vma_pages(vma); + __unaccount_locked_vm(vma->vm_mm, vma_pages(vma)); return 0; } @@ -2525,6 +2506,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, struct vm_area_struct *next, *prev, *merge; pgoff_t pglen = len >> PAGE_SHIFT; unsigned long charged = 0; + unsigned long locked = 0; unsigned long end = addr + len; unsigned long merge_start = addr, merge_end = end; pgoff_t vm_pgoff; @@ -2560,6 +2542,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vm_flags |= VM_ACCOUNT; } + if (vm_flags & VM_LOCKED) { + locked = len >> PAGE_SHIFT; + if (__account_locked_vm(mm, locked, current, + capable(CAP_IPC_LOCK))) { + error = -ENOMEM; + goto unacct_error; + } + } + next = mas_next(&mas, ULONG_MAX); prev = mas_prev(&mas, 0); if (vm_flags & VM_SPECIAL) @@ -2605,7 +2596,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma = vm_area_alloc(mm); if (!vma) { error = -ENOMEM; - goto unacct_error; + goto unlock_error; } vma->vm_start = addr; @@ -2725,8 +2716,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, is_vm_hugetlb_page(vma) || vma == get_gate_vma(current->mm)) vma->vm_flags &= VM_LOCKED_CLEAR_MASK; - else - mm->locked_vm += (len >> PAGE_SHIFT); } if (file) @@ -2759,6 +2748,9 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); +unlock_error: + if (locked) + __unaccount_locked_vm(mm, locked); unacct_error: if (charged) vm_unacct_memory(charged); @@ -2942,8 +2934,13 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; + if (flags & VM_LOCKED) + if (__account_locked_vm(mm, len >> PAGE_SHIFT, current, + capable(CAP_IPC_LOCK))) + return -ENOMEM; + if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT)) - return -ENOMEM; + goto unacct_locked; /* * Expand the existing vma if possible; Note that singular lists do not @@ -2993,8 +2990,6 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, perf_event_mmap(vma); mm->total_vm += len >> PAGE_SHIFT; mm->data_vm += len >> PAGE_SHIFT; - if (flags & VM_LOCKED) - mm->locked_vm += (len >> PAGE_SHIFT); vma->vm_flags |= VM_SOFTDIRTY; validate_mm(mm); return 0; @@ -3003,6 +2998,8 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, vm_area_free(vma); unacct_fail: vm_unacct_memory(len >> PAGE_SHIFT); +unacct_locked: + __unaccount_locked_vm(mm, len >> PAGE_SHIFT); return -ENOMEM; } @@ -3064,7 +3061,7 @@ void exit_mmap(struct mm_struct *mm) { struct mmu_gather tlb; struct vm_area_struct *vma; - unsigned long nr_accounted = 0; + unsigned long nr_accounted = 0, nr_locked = 0; MA_STATE(mas, &mm->mm_mt, 0, 0); int count = 0; @@ -3107,6 +3104,8 @@ void exit_mmap(struct mm_struct *mm) do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted += vma_pages(vma); + if (vma->vm_flags & VM_LOCKED) + nr_locked += vma_pages(vma); remove_vma(vma); count++; cond_resched(); @@ -3116,6 +3115,7 @@ void exit_mmap(struct mm_struct *mm) trace_exit_mmap(mm); __mt_destroy(&mm->mm_mt); + __unaccount_locked_vm(mm, nr_locked); mmap_write_unlock(mm); vm_unacct_memory(nr_accounted); } diff --git a/mm/mremap.c b/mm/mremap.c index fe587c5..67cc4f3 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -574,7 +574,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, bool *locked, unsigned long flags, struct vm_userfaultfd_ctx *uf, struct list_head *uf_unmap) { - long to_account = new_len - old_len; + long to_account = (new_len - old_len) >> PAGE_SHIFT; struct mm_struct *mm = vma->vm_mm; struct vm_area_struct *new_vma; unsigned long vm_flags = vma->vm_flags; @@ -594,7 +594,7 @@ static unsigned long move_vma(struct vm_area_struct *vma, return -ENOMEM; if (unlikely(flags & MREMAP_DONTUNMAP)) - to_account = new_len; + to_account = new_len >> PAGE_SHIFT; if (vma->vm_ops && vma->vm_ops->may_split) { if (vma->vm_start != old_addr) @@ -618,16 +618,36 @@ static unsigned long move_vma(struct vm_area_struct *vma, return err; if (vm_flags & VM_ACCOUNT) { - if (security_vm_enough_memory_mm(mm, to_account >> PAGE_SHIFT)) + if (security_vm_enough_memory_mm(mm, to_account)) return -ENOMEM; } + /* + * MREMAP_DONTUNMAP clears VM_LOCKED on the old vma and + * implies new_len == old_len so no need to account locked + * pages. + */ + if ((vm_flags & VM_LOCKED) && likely(!(flags & MREMAP_DONTUNMAP))) { + if (__account_locked_vm(mm, to_account, current, + capable(CAP_IPC_LOCK))) { + if (vm_flags & VM_ACCOUNT) + vm_unacct_memory(to_account); + return -ENOMEM; + } + *locked = true; + } + new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT); new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff, &need_rmap_locks); if (!new_vma) { if (vm_flags & VM_ACCOUNT) - vm_unacct_memory(to_account >> PAGE_SHIFT); + vm_unacct_memory(to_account); + if ((vm_flags & VM_LOCKED) && + likely(!(flags & MREMAP_DONTUNMAP))) { + __unaccount_locked_vm(mm, to_account); + *locked = false; + } return -ENOMEM; } @@ -696,10 +716,11 @@ static unsigned long move_vma(struct vm_area_struct *vma, vma->vm_end == (old_addr + old_len)) unlink_anon_vmas(vma); - /* Because we won't unmap we don't need to touch locked_vm */ return new_addr; } + /* Make sure do_munmap() doesn't unaccount locked pages */ + vma->vm_flags &= VM_LOCKED_CLEAR_MASK; if (do_munmap(mm, old_addr, old_len, uf_unmap) < 0) { /* OOM: unable to split vma, just get accounts right */ if (vm_flags & VM_ACCOUNT && !(flags & MREMAP_DONTUNMAP)) @@ -707,15 +728,11 @@ static unsigned long move_vma(struct vm_area_struct *vma, excess = 0; } - if (vm_flags & VM_LOCKED) { - mm->locked_vm += new_len >> PAGE_SHIFT; - *locked = true; - } - mm->hiwater_vm = hiwater_vm; /* Restore VM_ACCOUNT if one or two pieces of vma left */ if (excess) { + vma->vm_flags = vm_flags; vma->vm_flags |= VM_ACCOUNT; if (split) find_vma(mm, vma->vm_end)->vm_flags |= VM_ACCOUNT; @@ -768,9 +785,6 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) return ERR_PTR(-EFAULT); - if (mlock_future_check(mm, vma->vm_flags, new_len - old_len)) - return ERR_PTR(-EAGAIN); - if (!may_expand_vm(mm, vma->vm_flags, (new_len - old_len) >> PAGE_SHIFT)) return ERR_PTR(-ENOMEM); @@ -1026,6 +1040,16 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, } } + if (vma->vm_flags & VM_LOCKED) { + if (__account_locked_vm(mm, pages, current, + capable(CAP_IPC_LOCK))) { + if (vma->vm_flags & VM_ACCOUNT) + vm_unacct_memory(pages); + ret = -ENOMEM; + goto out; + } + } + /* * Function vma_merge() is called on the extension we are adding to * the already existing vma, vma_merge() will merge this extension with @@ -1038,14 +1062,16 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, extension_pgoff, vma_policy(vma), vma->vm_userfaultfd_ctx, anon_vma_name(vma)); if (!vma) { + // TODO: We always unacct memory + // regardless of VM_ACCOUNT flags? vm_unacct_memory(pages); + __unaccount_locked_vm(mm, pages); ret = -ENOMEM; goto out; } vm_stat_account(mm, vma->vm_flags, pages); if (vma->vm_flags & VM_LOCKED) { - mm->locked_vm += pages; locked = true; new_addr = addr; } diff --git a/mm/secretmem.c b/mm/secretmem.c index 04c3ac9..4515eb4 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -120,13 +120,11 @@ static int secretmem_release(struct inode *inode, struct file *file) static int secretmem_mmap(struct file *file, struct vm_area_struct *vma) { - unsigned long len = vma->vm_end - vma->vm_start; - if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) return -EINVAL; - if (mlock_future_check(vma->vm_mm, vma->vm_flags | VM_LOCKED, len)) - return -EAGAIN; + if (account_locked_vm(vma->vm_mm, vma->vm_end - vma->vm_start)) + return -ENOMEM; vma->vm_flags |= VM_LOCKED | VM_DONTDUMP; vma->vm_ops = &secretmem_vm_ops; From patchwork Mon Feb 6 07:47:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129454 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 9854FC05027 for ; Mon, 6 Feb 2023 07:50:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A4716B0087; Mon, 6 Feb 2023 02:50:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3535B6B0088; Mon, 6 Feb 2023 02:50:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A62D6B0089; Mon, 6 Feb 2023 02:50:18 -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 0715B6B0087 for ; Mon, 6 Feb 2023 02:50:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DD53C160BB7 for ; Mon, 6 Feb 2023 07:50:17 +0000 (UTC) X-FDA: 80436094074.23.0246D21 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2044.outbound.protection.outlook.com [40.107.223.44]) by imf04.hostedemail.com (Postfix) with ESMTP id 253F640004 for ; Mon, 6 Feb 2023 07:50:14 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=ZtlV3xqk; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.44 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=1675669815; 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=kJx4d6L32xsb7lM+TLJ9IAT+Zep1O/ydPt8JKIy3HJs=; b=ZMgM7OXjehtsqaGrNyOxwq1Kk5rYbtKPgNCVmaZnT3A2knj4ZOQZkiPob+hidp/5RCV/tE tOlahVJ0RK+8Lm94TrWgYATt+NvDMMi2ckWXDkNl4+UV7khnVQw5v5EZ1K7n/4NkKMO9Tu LQ00KJfH/TCta7P7sC3x3Z8vQLg2VMA= ARC-Authentication-Results: i=2; imf04.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=ZtlV3xqk; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf04.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.44 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=1675669815; a=rsa-sha256; cv=pass; b=Di95MiYM+m27/9t4FppIcookkx9ydq3fdMh0Ao3EEnXQ+TTcbIztW5rkxp8JALBnbdlbSw 8i/FgTQsA3c5KzXY7Rin/1b4RBeSqI7V2YCYJreI8kMKCIZi4Vg+px/34NXQmDxifD3FGh f4jSg60NmvnDZ8xQLU8fwDNZXfcDHKQ= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RyP2XQnz0N73PcQxjcUT4uAOGaufarRgV3t4vLzSzzaurNS9YOcieNt9HHUpRyhwuM0ST/JTfrnniUiAz2oiucF4XvGxWynkY75nZ+/jKnYF5jyJfTFHJLzLl+inSQwhMUp8KF/KTV6R0uHjgfrLSiQzdcb1w/PvSdL/whUjfUrVgoT0XHwiTaYRCpIpkuHalxJIBNxvQkJZ+M34gmal3k7aTaYlxnf1FZs2FlRsifhjhSiiUTTYgpp7TyZLepR00cZWTRFO5Vl65rxRkGfwPYmaCuzRjYw17K5klZ03gT+QESmo5i4AZg+VzA4BP9dJ3jf92mFMPCEYcQtJMJBfzg== 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=kJx4d6L32xsb7lM+TLJ9IAT+Zep1O/ydPt8JKIy3HJs=; b=ffHCQDtLu++R1E5ReQ7NDzjd2FnKcI31hET478cSznvJlv8G0gIdRGmRi5ovquC71bWDlL3D6+QbMGZacXA/lCwmCpeTFM13yhgSlUzhUUj9KSftTbmdDfMNVcPrh4lGp8gYIzzVm9MlilIb7XqLKIR+WCii0mBFs9TLYxqUTlGDY5ZlMzx3rW6jbN5nbnTOgzU78KBoqWZVQijbW2YIpB1a2PAihRChWoHiG6qBbl8fZ7Gtjnz7uWkLhJBs3AucxGtk52dUSy3xz+Kbyvj/mGn4YsqIkOTRx+jmFqkX+P8PUHkR5ft9LW0YSUvvdsqfYgbZBBhii8UPBuVSo4TIXg== 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=kJx4d6L32xsb7lM+TLJ9IAT+Zep1O/ydPt8JKIy3HJs=; b=ZtlV3xqkVCdEoJMfxTySEMtXYlBcu3RxzlI8GNdhznj8a3/UJ6829hjPsh6TZDRu2tpHt00m6JeVT9mO19xTSFl14OLTKMl2qMVxWKCQ08L7ww//5xTfGaLbzbp2s/s+uLONIvlAYGNugDKhqxzwxxpHpX1Q05Ec8ZZUZEusqbBXkWlLLK1Eb7YUEv1W9yawV01c44sPu3WlKPNxoO1W2hfj+pC9GmCitT/gDCXV23DrMjJW056Q12HoqU6w/6LxfLme95bxIla/oE9b5Tf8DZO97bHQwNjl3phF0Ws5x7pCH4BQc+7zmpLqeADSEpnxfCVrYTxZfdoWv2as/21C2A== 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:50:13 +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:50:13 +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 18/19] mm/mmap: Charge locked memory to pins cgroup Date: Mon, 6 Feb 2023 18:47:55 +1100 Message-Id: X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYBPR01CA0114.ausprd01.prod.outlook.com (2603:10c6:10:1::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_|DS0PR12MB8573:EE_ X-MS-Office365-Filtering-Correlation-Id: 427e0896-7913-4612-52a1-08db0816c76f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: N8UuAVBxTWt5/pJf6aD+fYRN9CGKIzqzua+ZekDKIw+u7Cw+ngRBnvwoB9EW06W5OR0HXypCbToXB/cTcd5hiyTJ4YX4oO9HDgWQ64BG1KiuYLkpOYZfB9slmftj8NkWdQMDICe4WBXEAudCmcOSQpCOVpdMi/UhZdvuH1pZtbZrSg0jh32quXs3bIsZh/I+WAET/3XXCpMr4+ZnA1P14qXZR1GGx+UvgPTLunxC0V3bx9EMasETCjCThtsfLbPoQlyi/xXMFqF4N5AMD6+Ggcx/PYVi46aL5nnpIQS1piY4WVYUz6r/yGju67Hw2klWH6/UgXbVn6p2oPQOrUVL/DkCe/WilMc60kJsFbu/ASymE+K8BUoQBoqefccmV2J5IQab1Nm5oQ6n6h4dYvPMCzFxe/uZ6DscTTIgCKgE3W/bhaglxgsPWqQG5+aCypZ4hqCrRcjKkIJ5hu2vhB1yUq4iQsDz1zDM6T/dgbV26YW+Jrusm+AVcjKbr59XR4/09BclrJ0OjBoo7tyF8XARfOT4an4oPbYDJvUzT34OHJaTUSauIUkw6aBmCNAH5iLUWR5hsrxp3vf3lAG59oP4GKI5Z6z2vAHJDaOLw+36QOOi0Ayqvl0QKAnlu1SkoOoCznQ1YH5U4TKxwNkqESttoA== 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)(6666004)(107886003)(6486002)(38100700002)(186003)(2616005)(6506007)(26005)(478600001)(6512007)(7416002)(5660300002)(83380400001)(2906002)(36756003)(66946007)(8936002)(4326008)(41300700001)(66476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JBRWDSAs51qCJ2LqdhVJqwQZ6HOr5zz8VG3PTKVvQUKbflh2hf0LzJ0UDeGJ/+5xt81MysKO6VIcEElzItaP+lrrwX9ooJ+sTQWdI8Htq9Lxus9hnccpTFqmaVqQfxIhDIfSyqxv2qiaAUmWYENztI8W/pdTYAvSX+6Dmj4U3QQguzMoHuY/A9xU0ZXsadHFEhGh0KF2W8A5LPt4WSB6UolJ8T1uaNfmqZHKrMjZ9P004V0HSzhoV3uo6ZD0Pa8I73QgtzYTsKMjTacKcbXZgbPhGioZvLA4EZTdjWrS2lzZ5HJPAT/FO8Gm2C5UWy/2Y4ndsjX+Haq9k5c07U8wL0Le846YqGhqna+JnRUPv+xoJNaKPqpiutkX9qoF7mRn31zGwTrz8YYXtQL+by/WxJZOa6XPJSR9TAIG0g9xFEIVNuPlXyCkNJv2CptGfhmkYmsH+3Gr4cF1eOWtFDCPDAkkQPXJV+ubu4+1Q4mQMhH6v5DN3cF2YZ6EV5MphqpPQOlr9u3GhFBBwt0x4RCVlUeFqdEoU88XsrsQxekmUkMCAP54hc8+2jswdSuEFxkvPyie5g73YQgJQXm/7CKzyMT0q01G3Bv2NS8CUew X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 427e0896-7913-4612-52a1-08db0816c76f 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:50:13.2727 (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: tD8S4PSSN2NXQP7cLnr9ez07RFo1dGSONbQ2/RD76uWwk7J/TTGeB0H+KUhyxz4kuaksMFWXf+J2eqKvEedMsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 253F640004 X-Stat-Signature: ggfnaprj48z6xenofu93oxjihuzi763n X-Rspam-User: X-HE-Tag: 1675669814-97177 X-HE-Meta: U2FsdGVkX19npgWGC+ddr7L7M+Q0Z4CwdpYmQutNN1Hz46pXk30iA7CtDCFQfI/XAOQtmyPzGtdwotZ1MhMVPy3EyV9LmbBlMr2+kIBHGDaDkqSkRTfv606I7CkeI2GTO21TsSSUvTSkZHrCACulHbelRPMTAEh5D5cgujpVfLC4QiTA9VLjP4lR0K0AAJcKMVZu+EWxEd0gaJIWXu6tlJ2FroZlVwbVr7oxouUGhXOTZPYcosH1ROGiNq6Ku94k/J5jtdvfcoboDmG7RAB4avhbZA5CXI7p1sRD6NgjWRY5uVE7sCwB9hTLN1r34/QVWGsFrxMwWXf9tRYri6vB9gt+/yw4RK/48Y6vvZWNK+vJTxhkAgnAT4C7rKmxQf1Et9UHk+IZieKp6rRwG45TwLUOxl+7smmQS/Fgu1rNEEo6gdkJFoz9x+U5mbAkBr7hechcpdR9xrR7rste0YO65xb+7udOyWqRkVypNCVdh8Z5GIFlnD+pxYzw7x8sej333oid91/lDpbY6acbrQFwcLq/830WHql/NwWkiir27btqiWWOmGzw4mbKQ7kMOE+TA3B75Vo6g4rriItYA1beFkxp3BQ1TIrU7E61v5nAz0DMo6pYD8toe6W7r0zmwTRYdVkt8VhN1WT5+yizzs0wz7loneQUeD4rGhqxgwjTkjb8n/DIN9JSoHKEcFCySrE3qK/NEI01eBLhxUki7grjMKGfV+fsa3Yd2KgSz37Y11hMzu/gsjZLQchBIC1QPhnvp8/nZKdt6Vnr4Qo0UkhYkq4Il7o/E9+X2p0U9EYyJ7T6/pZG/OyVHEBlLmaNCGx70A2hz1qZo5GqE7ekq8LBK01LVw0tNXP+PRFzL4WL8byqWiV7xabcsdROPV6YCKPzDgwPnBvwfpo/OONtJ+wHqrXI2qQPXyu+zcLEGzOIG1KrQwhbrQTqlO6aKIkFNkrtfgd6Bq8ykYnZQ3fYr2s J2KwJ+dO /SZXoxb4j2FAOXR1PDEDS6AwCQgBPqDlRCRduf2icDB04xj5IXbEjUpkZrWEkhUQqhdlAjQf363gVQbqVxrBrgbWjCG87eYYtlY0qhwBWz0QzucA2xQgCbkYWN9xAADGwoZqnSf24s4FgMhJ9v6OGCzDisjG1gXne2pKn6gJnPIw7dJWXMH3rQ/Dmlh/FB9yImarFjb2e68n8rJPnZHGQPtrKDgTgY2AKkIXBQEmkyljcgg29zDdd10Hf7su3lQd22HQe2df8VMR+6osX/+4T2Vh8g3bULOyQcxDI2K4oohrxg02c45VR+vyze525QE9IngDQxLxddyaB6Kv6I05jKbLtq29J5XiCGKnJ2eGag5i4lMI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000244, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: account_locked_vm() is used to account memory to mm->locked_vm. This adds accounting to the pins cgorup as it behaves similarly and should be accounted against the same global limit if set. This means memory must now be unaccounted for correctly, as the cgroup typically outlives both the mm and the task. It is assumed that callers of account_locked_vm() only do accounting against the current task. Callers that need to do accounting against remote tasks should use account_pinned_vm() and associated struct vm_account to hold references to the cgroup. Signed-off-by: Alistair Popple Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/util.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 1ca0dfe..755bada 100644 --- a/mm/util.c +++ b/mm/util.c @@ -589,15 +589,21 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, struct task_struct *task, bool bypass_rlim) { unsigned long locked_vm, limit; + struct pins_cgroup *pins_cg = get_pins_cg(task); int ret = 0; mmap_assert_write_locked(mm); + if (pins_cg && !pins_try_charge(pins_cg, pages)) + return -ENOMEM; + locked_vm = mm->locked_vm; if (!bypass_rlim) { limit = task_rlimit(task, RLIMIT_MEMLOCK) >> PAGE_SHIFT; - if (locked_vm + pages > limit) + if (locked_vm + pages > limit) { + pins_uncharge(pins_cg, pages); ret = -ENOMEM; + } } if (!ret) @@ -607,6 +613,12 @@ int __account_locked_vm(struct mm_struct *mm, unsigned long pages, (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); + pr_debug("%s: [%d] caller %ps %lu %lu/%lu%s\n", __func__, task->pid, + (void *)_RET_IP_, pages << PAGE_SHIFT, locked_vm << PAGE_SHIFT, + task_rlimit(task, RLIMIT_MEMLOCK), ret ? " - exceeded" : ""); + + if (pins_cg) + put_pins_cg(pins_cg); return ret; } EXPORT_SYMBOL_GPL(__account_locked_vm); @@ -622,8 +634,18 @@ void __unaccount_locked_vm(struct mm_struct *mm, unsigned long pages) { unsigned long locked_vm = mm->locked_vm; + /* + * TODO: Convert book3s vio to use pinned vm to ensure + * unaccounting happens to the correct cgroup. + */ + struct pins_cgroup *pins_cg = get_pins_cg(current); + mmap_assert_write_locked(mm); WARN_ON_ONCE(pages > locked_vm); + if (pins_cg) { + pins_uncharge(pins_cg, pages); + put_pins_cg(pins_cg); + } mm->locked_vm = locked_vm - pages; } EXPORT_SYMBOL_GPL(__unaccount_locked_vm); From patchwork Mon Feb 6 07:47:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 13129455 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 3C7A4C05027 for ; Mon, 6 Feb 2023 07:50:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF2056B0089; Mon, 6 Feb 2023 02:50:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C7A566B008A; Mon, 6 Feb 2023 02:50:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACD1D6B008C; Mon, 6 Feb 2023 02:50:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 974916B0089 for ; Mon, 6 Feb 2023 02:50:24 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6F5E2160BB6 for ; Mon, 6 Feb 2023 07:50:24 +0000 (UTC) X-FDA: 80436094368.08.B3952B9 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2078.outbound.protection.outlook.com [40.107.223.78]) by imf19.hostedemail.com (Postfix) with ESMTP id B4DF91A0003 for ; Mon, 6 Feb 2023 07:50:21 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=kVvPNwBi; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.78 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1675669821; a=rsa-sha256; cv=pass; b=acAoUjwCIsdpNrPpbqrDzuYQCA180PhB6jzkQcTehgwhDb9tiVN+T4YGU4vGUiRofpElX/ cGyGoNaWSdHFwPmancS18qM5hJCA12xXUYhxuQ4Z8Kw4h4Vpx14XAH9nutMADL05rNf9pn IIsd/hAoqzR2Lho6uiQbjrESq7kp2jc= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=kVvPNwBi; spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.78 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675669821; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=NHEVbxgBliBHMo9ZiR0gDz7YiDOiKSWmZncX53Ltei1ViBu5q+NKHkJSspHiWLFeYPa3Qo 7zsYqoDp9A7Yf6uRsaF4r+cSDCijI4jTz09TQfLx00W45Ydud1KGIE8gyF6xLvYwDVgvUp iqmtGVTtBUAKzlI0OMkdXKjqdgmaTYA= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Im7MEEVN4FWEx3zCb+jxDxt6oqesrmj8wEvyNhB6xZWYLmNr8YJXfqXWgxp0JvAIIXXdUnrO96MGX7+XrY25hIfkVOGBVsn2Figp5RwnKVNY70RpHmDwh8BhJCEQSoJDTvh9pWSRLJa9mJrdPdI3/timqioSJ2uD8tURUPv571Hfev7ZeKatRw7XEZcFN+Dfl08r4F0Uq+fqLnHaEKJnUOuE4ZGF8WR1sYqFACWL5xks82FH9i91x063H+8KsIGxTQVt0s+3RP9+X2Y+p8t8tPaFR4t/2KDfOMNGR3J+apzlJOvC2Uw8dS2ggut5XjsdygdkpqL6aIKTsa2gpiAu1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=S3LRHDSF95Tx4J6VA9J6urDze9zWFzHyWsmZiwRPG8JJEaOQOgkPWyZ5TuEI+DHP5JB5ZZfiLSynKg9QXeRz8TyTGWmKjNNGQUdIIiMYxeVZ1rzPnTc6JYwghRZ2SJnqTaP/389fVUpnOhQO1l2UPFGisARyo2Lciofjl+XgzaAJ5k8NSgb/lkKZPNk1RoGyA+h1u8RQklqn3wTZqKzzB5iJ1NFS9ulU19AwzNJZ2p7UQI3DVv48+ewMlfv5bmRLtjhxtXIoQztM8uw2VDRC9ISCSrT8i5KMThjR49aeeUXL6LItBPZ6225fYLdJqNy5OzmqcSmToAQHw65KtyHO5Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=CGKTTJbiu7jo+yKcPAlQKTQIKt8ia+61r7AgkXzDJs4=; b=kVvPNwBixSEKdOtpAfVIoKMGsQdXxYk24fgyI253NvLsXcQWCV9659gz3zqc7aYzWX6DrVJfCdezr5YAvVyZt8PlxpG2J5KbszClhFSkw8xAeADMD1gLKJGkafXE9BeTlOAp+Zxs1R6FuJ2t35g4NfxRBbK9CSX9qjPG/A1wkbHG4LJTYm26ACVEBwi/Zx2Co+3SluiTDJTpDHFJv9nk6iX+beV3QuD32Ls4HdQUj33P7Ug/q0oOHp0GypmRt3kaJM57nIfalsiCFYqHdotasEmSHOdlQoYneLf21VI0x26SYY3JMnc58Xfv9QOUq3r3EdpgI4/5wnxUBYQyd69aZw== 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:50:20 +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:50:20 +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 , Shuah Khan , linux-kselftest@vger.kernel.org Subject: [PATCH 19/19] selftests/vm: Add pins-cgroup selftest for mlock/mmap Date: Mon, 6 Feb 2023 18:47:56 +1100 Message-Id: <2bd6038b4a1571631e7797ce0f47a133f52acd9c.1675669136.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: References: X-ClientProxiedBy: SYCPR01CA0045.ausprd01.prod.outlook.com (2603:10c6:10:e::33) 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: d11c3ca0-e577-4cea-9a76-08db0816cb58 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9dWZD2t/+Z7V9QOIUUWaxrSGi0KuhVSIbQ77D2ROgVKVnwxGP52AevErTClN4BqAy21B5fx21j6tBiYlt8u35F60dW8g90c/XKmkVbPhuynbVr6O+4Xghv14ztWgD/29bEkInSoVrxBkn/08YV1MUq9x4RChn3IkP6M+jIQLrUT1scnll48P/ztTwbl0IDBav8icxKqw3/RCPT+PO3kX4NT2mc7s04u7e7KLHTOhOIRxPsusGcYsL6+w1r3dUct98mhm++I1+CKwtb6dFisPWefIHJTICoX53HpZ2KoOnR7lz1J6Kw1jx/cAlLFdzoiygI++gsSkhWTSxui/Jq2ERMn/UjhQYNc3jWXKcgrbJmVfemrw0U84V3963AxNslhkPWNvP3VePQFbPXj+jpugf+QzEUMAZfDn5Uk989t65iJJpKzGdK5zckXmd99UeXwsRYPQXni1J8zzETrAWXnrF4mu2rGw7OHG+AUg4/v0QVWVildJ07/HpedRKRKNeZlmwp2KU+lQBys3n/Bkfu8X/mM/uCdM5GzA5C99yWLxorPDW4JhXFApebCvHSrCvJiQ19m6bFejTrOciu3q1zeRzrRIBNA7BHN+u3vu0JBMwY+JCVbXPHThQ70b+I8B6TaT9LxCml/0t/fhHwHV+NssoA== 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)(6666004)(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-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3j3ry8KiLXLZp8Riv1ALQgPDVgFrUuvNKw26OWlhJphF7+Hc8o5BbQbqnuN/Nw3ZHBrpWrui2J+NXfFlizPM9vjj8Gkk8/nsxRcdCKXUPkmgg0o4NXntKMUjVdiKtZDSUM7DVOfDJuzX++7b42gLeXteGJCy0kG5kM7z3oXOO++mXFnJ6ii5BDy3tLAhDpWmXTz2ZQDLsDK6+xsmS9o4ZklXY9TxRAIn+bIc8tv8SXNFT/ed9QCyK9PMb1t6a9Afxxeyb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HBolYTQevQz8MowdOUs6g0GSr9bzaOMnNI8qlmADJFoRv50tYLDcfza44pzces2sluUAsK3NnRId7WaB8emV/AFIgHj1ITn4XornfziLewz5vPTuVaJBEYELm+dHxWkLvCdOt5Tc3gzv6VJg4zcuFKm8xJkagwI2oYfvVuYw2xAuTF3s8AjlPTz3l/PVaWmw1kMtSC1O1S1z3eiQAuzB1e+Xq4C3GcdolcN9UyabSP79S7wsrNNGC09EU0HhObYU9 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d11c3ca0-e577-4cea-9a76-08db0816cb58 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:50:19.9753 (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: KClDenTLQ/TczAn7Sbd3dGzzwGDXykfOgI4zkvGApKdmx3a7WPKSSGaevP2sFCVTgUe7bCPuUu4K9rn6fFxZFw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8573 X-Rspam-User: X-Rspamd-Queue-Id: B4DF91A0003 X-Rspamd-Server: rspam01 X-Stat-Signature: su43imx1rj8xep5suwig3r6ecemspow9 X-HE-Tag: 1675669821-978866 X-HE-Meta: U2FsdGVkX18QOZJJeES4dsNGax5NrY4X8nMiUOfzMJzDiH9C5GVJ/xQZWyBEFbfhAOVLTpSGevyVwNqzGNjWKsxtvlenxo3ie9NSLRouyEWEk0WNdM2HfjOkCumUgQQcEyLvouHAT6RaA3h9oa2hmnPy1xHoPjBhaoL46+vY634GRPgue3v8qx2OWuEuOrYHqHseDipNELCdMZTQ3veW+hn4FhrPubx3d4jAoQ4mqqFJaYouOgYW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+DyNFVRJBYfWRhhStsPtux2yZskvGfq3iSXY/9Oxoz8a7avyEH5dpsniKbt8FFd X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add some basic tests of mlock/mmap cgroup accounting for pinned memory. Signed-off-by: Alistair Popple Cc: Shuah Khan Cc: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org Cc: linux-kselftest@vger.kernel.org Cc: cgroups@vger.kernel.org --- MAINTAINERS | 1 +- tools/testing/selftests/vm/Makefile | 1 +- tools/testing/selftests/vm/pins-cgroup.c | 271 ++++++++++++++++++++++++- 3 files changed, 273 insertions(+) create mode 100644 tools/testing/selftests/vm/pins-cgroup.c diff --git a/MAINTAINERS b/MAINTAINERS index f8526e2..4c4eed9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5387,6 +5387,7 @@ L: cgroups@vger.kernel.org L: linux-mm@kvack.org S: Maintained F: mm/pins_cgroup.c +F: tools/testing/selftests/vm/pins-cgroup.c CORETEMP HARDWARE MONITORING DRIVER M: Fenghua Yu diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 89c14e4..0653720 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -56,6 +56,7 @@ TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_FILES += ksm_tests TEST_GEN_PROGS += ksm_functional_tests +TEST_GEN_FILES += pins-cgroup ifeq ($(MACHINE),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) diff --git a/tools/testing/selftests/vm/pins-cgroup.c b/tools/testing/selftests/vm/pins-cgroup.c new file mode 100644 index 0000000..c2eabc2 --- /dev/null +++ b/tools/testing/selftests/vm/pins-cgroup.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "../kselftest_harness.h" + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CGROUP_TEMP "/sys/fs/cgroup/pins_XXXXXX" +#define PINS_MAX (-1UL) + +FIXTURE(pins_cg) +{ + char *cg_path; + long page_size; +}; + +static char *cgroup_new(void) +{ + char *cg; + + cg = malloc(sizeof(CGROUP_TEMP)); + strcpy(cg, CGROUP_TEMP); + if (!mkdtemp(cg)) { + perror("Failed to create cgroup"); + return NULL; + } + + return cg; +} + +static int cgroup_add_proc(char *cg, pid_t pid) +{ + char *cg_proc; + FILE *f; + int ret = 0; + + if (asprintf(&cg_proc, "%s/cgroup.procs", cg) < 0) + return -1; + + f = fopen(cg_proc, "w"); + free(cg_proc); + if (!f) + return -1; + + if (fprintf(f, "%ld\n", (long) pid) < 0) + ret = -1; + + fclose(f); + return ret; +} + +static int cgroup_set_limit(char *cg, unsigned long limit) +{ + char *cg_pins_max; + FILE *f; + int ret = 0; + + if (asprintf(&cg_pins_max, "%s/pins.max", cg) < 0) + return -1; + + f = fopen(cg_pins_max, "w"); + free(cg_pins_max); + if (!f) + return -1; + + if (limit != PINS_MAX) { + if (fprintf(f, "%ld\n", limit) < 0) + ret = -1; + } else { + if (fprintf(f, "max\n") < 0) + ret = -1; + } + + fclose(f); + return ret; +} + +FIXTURE_SETUP(pins_cg) +{ + char *cg_subtree_control; + FILE *f; + + if (asprintf(&cg_subtree_control, + "/sys/fs/cgroup/cgroup.subtree_control") < 0) + return; + + f = fopen(cg_subtree_control, "w"); + free(cg_subtree_control); + if (!f) + return; + + fprintf(f, "+pins\n"); + fclose(f); + + self->cg_path = cgroup_new(); + self->page_size = sysconf(_SC_PAGE_SIZE); +} + +FIXTURE_TEARDOWN(pins_cg) +{ + cgroup_add_proc("/sys/fs/cgroup", getpid()); + + rmdir(self->cg_path); + free(self->cg_path); +} + +static long cgroup_pins(char *cg) +{ + long pin_count; + char *cg_pins_current; + FILE *f; + int ret; + + if (asprintf(&cg_pins_current, "%s/pins.current", cg) < 0) + return -1; + + f = fopen(cg_pins_current, "r"); + if (!f) { + printf("Can't open %s\n", cg_pins_current); + getchar(); + free(cg_pins_current); + return -2; + } + + free(cg_pins_current); + + if (fscanf(f, "%ld", &pin_count) == EOF) + ret = -3; + else + ret = pin_count; + + fclose(f); + return ret; +} + +static int set_rlim_memlock(unsigned long size) +{ + struct rlimit rlim_memlock = { + .rlim_cur = size, + .rlim_max = size, + }; + cap_t cap; + cap_value_t capability[1] = { CAP_IPC_LOCK }; + + /* + * Many of the rlimit checks are skipped if a process has + * CAP_IP_LOCK. As this test should be run as root we need to + * explicitly drop it. + */ + cap = cap_get_proc(); + if (!cap) + return -1; + if (cap_set_flag(cap, CAP_EFFECTIVE, 1, capability, CAP_CLEAR)) + return -1; + if (cap_set_proc(cap)) + return -1; + return setrlimit(RLIMIT_MEMLOCK, &rlim_memlock); +} + +TEST_F(pins_cg, basic) +{ + pid_t child_pid; + long page_size = self->page_size; + char *p; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 32*page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(p, MAP_FAILED); + + ASSERT_EQ(cgroup_pins(self->cg_path), 0); + memset(p, 0, 16*page_size); + ASSERT_EQ(mlock(p, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 1); + ASSERT_EQ(mlock(p + page_size, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(mlock(p, page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(mlock(p, 4*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 4); + ASSERT_EQ(munlock(p + 2*page_size, 2*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(cgroup_set_limit(self->cg_path, 8), 0); + ASSERT_EQ(mlock(p, 16*page_size), -1); + ASSERT_EQ(errno, ENOMEM); + ASSERT_EQ(cgroup_pins(self->cg_path), 2); + ASSERT_EQ(cgroup_set_limit(self->cg_path, PINS_MAX), 0); + + /* check mremap() a locked region correctly accounts locked pages */ + ASSERT_EQ(mlock(p, 32*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + p = mremap(p, 32*page_size, 64*page_size, MREMAP_MAYMOVE); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 64); + ASSERT_EQ(munmap(p + 32*page_size, 32*page_size), 0) + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + p = mremap(p, 32*page_size, 32*page_size, MREMAP_MAYMOVE | MREMAP_DONTUNMAP); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + ASSERT_EQ(munlock(p, 32*page_size), 0); + + /* mremap() a locked region should fail if limit exceeded */ + ASSERT_EQ(set_rlim_memlock(32*page_size), 0); + ASSERT_EQ(mlock(p, 32*page_size), 0); + ASSERT_EQ(mremap(p, 32*page_size, 64*page_size, 0), MAP_FAILED); + ASSERT_EQ(munlock(p, 32*page_size), 0); + + /* Exceeds rlimit, expected to fail */ + ASSERT_EQ(set_rlim_memlock(16*page_size), 0); + ASSERT_EQ(mlock(p, 32*page_size), -1); + ASSERT_EQ(errno, ENOMEM); + + /* memory in the child isn't locked so shouldn't increase pin_cg count */ + ASSERT_EQ(mlock(p, 16*page_size), 0); + child_pid = fork(); + if (!child_pid) { + ASSERT_EQ(cgroup_pins(self->cg_path), 16); + ASSERT_EQ(mlock(p, 16*page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 32); + return; + + } + waitpid(child_pid, NULL, 0); + + /* check that child exit uncharged the pins */ + ASSERT_EQ(cgroup_pins(self->cg_path), 16); +} + +TEST_F(pins_cg, mmap) +{ + char *p; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 4*self->page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0); + ASSERT_NE(p, MAP_FAILED); + ASSERT_EQ(cgroup_pins(self->cg_path), 4); +} + +/* + * Test moving to a different cgroup. + */ +TEST_F(pins_cg, move_cg) +{ + char *p, *new_cg; + + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + p = mmap(NULL, 16*self->page_size, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + ASSERT_NE(p, MAP_FAILED); + memset(p, 0, 16*self->page_size); + ASSERT_EQ(mlock(p, 16*self->page_size), 0); + ASSERT_EQ(cgroup_pins(self->cg_path), 16); + ASSERT_NE(new_cg = cgroup_new(), NULL); + ASSERT_EQ(cgroup_add_proc(new_cg, getpid()), 0); + ASSERT_EQ(cgroup_pins(new_cg), 16); + ASSERT_EQ(cgroup_add_proc(self->cg_path, getpid()), 0); + rmdir(new_cg); +} +TEST_HARNESS_MAIN