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)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QSDKVRsjmnaBA0dHK3zLGH7t2j3luWzG1YTnS0CRppsmxRzxWAX9nbkI0Lg1luHKCEBzXhZPsh6pNkMYzcoqG4pOm7LO4HvcMHEh8/G9bYZiuFfeB+Y5DvafNiDGNBrIRA7OcVjDaOIfcjQovda21+7+VNj07fOgNpqbg84TGmXdK/KacuSZnIbqn/pXnq07MYLn3T8cSqL20+muD83tapi+knWpozP4qusz+d1XcVSpnhZBrytBEMUPp/Dq021szGeOunQvDUPOa48nfNRz9h0Jb+9j8fC+b3iO10iFB5Mk5mWqMJf6ToMMx9iznNs7S40TOYsMkvITuxvlJYgKqx42N3bx0adFVt7pGhRfYe7KmGuInx1b1qwbx3u3hz/IkT68Tx+bT9LeTCwWTrtd24dlCY7nvkEagZaw4Bhhwy3tt4oqTYFL0J0dQDMDq/v7rDzkZAoodMLYPhfkUW7pHWKn9apDB5Hf+32yeHs6PXcKytAzyjLovbAdOzSdhqAAAEfW+jxYFIRg3pkA4t5S20yL6tNfxT18yzbUqaMAV/9kTyVLFFj/3nG7HWb9dMtoL/eaXXb3IrHlY50oNe1sECjTLg68V4 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4f96889b-f944-4864-2769-08db08168d53 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: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); }