From mboxrd@z Thu Jan 1 00:00:00 1970 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 00744C25B4E for ; Tue, 24 Jan 2023 18:27:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 936D26B0072; Tue, 24 Jan 2023 13:27:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E6406B0075; Tue, 24 Jan 2023 13:27:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D59C6B0078; Tue, 24 Jan 2023 13:27:31 -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 7116C6B0072 for ; Tue, 24 Jan 2023 13:27:31 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 4F157AB11A for ; Tue, 24 Jan 2023 18:27:31 +0000 (UTC) X-FDA: 80390525502.05.AEF56B2 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf01.hostedemail.com (Postfix) with ESMTP id 7995D4001E for ; Tue, 24 Jan 2023 18:27:29 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="cBbJb/Nx"; spf=pass (imf01.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674584849; a=rsa-sha256; cv=none; b=5COUHKLS2mXAW+9yx1/YhoyKtgzI37Fb96GH8gokasIv8BkYecLhX2/1ANSAuTvyQFOCxx s8y9S5c714Rk7/SbiG5+4VvQv3b06/ia38DqDSrCX4+6xeCdJ4rhIrX/ISkiivpiavh2Xp A37FVdd3V88y1BmJwkpzB334sa2RwDk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="cBbJb/Nx"; spf=pass (imf01.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674584849; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RIwygwRM9tzobh7QaVo0cnmbFvKeH50XulwOSqSD844=; b=fYaB3gZ84n/EeHZ4Wy+qU939PL2Cc6WLgItVDZITrA5jNiLRkyCQbaOoHXrw7io30nfJec vpyTN8820Pz51CtVQfzt2rqcJ8EkpS9dvh4cReVI7Le6RwUw0UdrWT2XPkCOWNZipezDXC emXfwJsYiFybGsgu9HdepZK76L+tako= Received: by mail-ej1-f47.google.com with SMTP id vw16so41381300ejc.12 for ; Tue, 24 Jan 2023 10:27:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=RIwygwRM9tzobh7QaVo0cnmbFvKeH50XulwOSqSD844=; b=cBbJb/Nx3OmOGYqdI8Yo1XttKVQwMvETRTZnmA/yBdPsHIR2qt4/Iv5TghMNeWcf7g 3NwPmcWCiVrWVMIFPwvpqPx777Yo9B1ExCJfTWMFeKrUQllNjdvxr0DdCrlBzyrV0gov T7fZd0SmjzqKN8Jz/z4d6G+zYENz5OUYmVrXV2gxXpGfvL2OHf5b+orFKoR9hII5TC11 uHTu8Shf5tqIRjjT4iaN2O23SRGB5uf9WaYPy4t4ihT2foBPhFAGS9TDdMbZxzUEywuo RGHCjr7/DDLcUoFp0xwsRRdmX81H2m/F8AKB5c/eM0Z+e0GoeLC9zauUJ/knC5ZVdtwt hM+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RIwygwRM9tzobh7QaVo0cnmbFvKeH50XulwOSqSD844=; b=1wwEtae+5N9JG6hj+sKX7KFsqwIHarMCuJIZCJnxfrEJuceugCYnIbK2nNgPxV0elp Qn8SeZ8yAjssIISgUUQi6Lc0Q6XqMYLZq6+PDjgdzJMXe7/MZ3aTDJ/QEKiNMGbt9lCr SLpuDH8/o2qQ0qnHJepIUik5kcUofv8DjRJlZttPDfuv6UUH46hWJHv6tjASF683x8rN UQoV0V8WPChnGsKlklZtBAzowD6bE8FcA2/tyPOYy8pFBj1QFV1zxXVmLb/b0/RuHOFY uQ7bLesEcybzv3sYNHMVxLRh0eCqSnrHDrux41FxzzBQN8u8JxQTf303caAHAVxhyenW vIrA== X-Gm-Message-State: AFqh2koJsBiwKkcjaz5VUwyiElJTkQjcOtJouRKUG3URXGv5ZJsQEyJJ HTmDA93C7xq6vzSJEuzCsj1AZIdGej0gzylQveWzew== X-Google-Smtp-Source: AMrXdXsu4OXpseg7nR0i1smFRE12hMtFwaOjRfzoI8Hv61dk6zz58TcLoy62SrgqOS77rNtGXfXSXi9UZI+OwNj5rx4= X-Received: by 2002:a17:906:3487:b0:861:7caf:c252 with SMTP id g7-20020a170906348700b008617cafc252mr3272304ejb.212.1674584847639; Tue, 24 Jan 2023 10:27:27 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Yosry Ahmed Date: Tue, 24 Jan 2023 10:26:49 -0800 Message-ID: Subject: Re: [RFC PATCH 00/19] mm: Introduce a cgroup to limit the amount of locked and pinned memory To: Alistair Popple Cc: linux-mm@kvack.org, cgroups@vger.kernel.org, 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 Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Queue-Id: 7995D4001E X-Rspamd-Server: rspam01 X-Stat-Signature: bi6chxucu54jrpaizy19f96n5xfunocy X-HE-Tag: 1674584849-895101 X-HE-Meta: U2FsdGVkX1/yTfF5l0fLaZTDRYPZt+0SrFmBSfBCohLVocNqKG5srvqVejOupn2Bn5qdljIFyaa9EBLkiQH+nCs8AiilwA3Scx7narPKjaTBo7PjXWvAK/uLE3z7aJsDdIYUvDgmjqiF2kQEtdwDVOWJZpFmvGjHL7gM0getp+dEs0ZzrX8ZaNiukR6n0dpluojExogk9sK9DczM7azJiYQmBGuAr6uqnyVTnEml2+ddGWiksBY8UFiqdTpaQLuuXxYoCjriBmeVHn8V0pz1HuOjodiBWMbz1uiLCn0tdfDQ1uA/yU77VxijbNMH1aWuyo10SrukUYiN2h3Wa3yPG46THTpo+a0bfvmFaI7xcTVZ8QlLjqUAmlaiLiZfLeFVX4oEv4cP2vvVqV5nfHCjf7vB0XpcfL4p3IF6U5v5kZR8F0jv3R6JEP3yY2o5fF5KzcIoKJpo+bQQZohsQ1RY8ZRt2FyLWfrSfFNYHhgZhuY3aeVlZKsWd87Vmh96ZJeB4vJ//5EuKehhIm4pt9DipqLujWq389hmSo6T4jirxDafboeyN6QlqVGS9lhk2XTkDvyd8eQ5+t0XfW25EZqnTeBP6HcEMpG2XsapuSxjZOidCOAlbl882E+FUE3SU2cPEOL/kxOQUv3V4dQ8S5EFMRD1dwbSmIKFBX7wINDL3eMpHc0uDJp6FzmKw8P3a+oOXnHLHE729sEIfhZTDH/Ek74RSmM0JvgR0gqOKbAXuOCGxo59IlCDMCNgOoaisAqJHfwneCwbqPJhrNm7LPJjmcc4OxEuaG1nVbLIoKiOwNHYcQEGOSrl97ohygbfNoWqaTQBRqh05+OADPbggF2Jd+6MCdyjcZ7aGN8/WoFPyj7FdXUF4raJHnscJTfTDHBj4NBPFrduWjzhSn5pmPO+/NgLR8590DVQhMjz+Q4zYY8MhqSu6TLEdiXpadlyBQpeQ7bDnf7yxeIueh5awHy g2SlJgBp nh/MYJmVASsR5tU9elmHL/py9RhqlPUEik2Xa6KbncF9J5kEQ2CxElYC49R0sJ2YMaYM1QV4XKPqQFkSDOjjiV1fOz7BDGiSQzC3JEE3e2kecGx+GoHyygCa4pD6PW9Jj3TYCEG8oIMux3a067vAhcrsxPPL1dVRFQIr9f58eIJmdmlszbBOpKBH77OvUsbtV/7RiUq6prYKWwjyWLPY5i8FjWQeiDs81f3fhJJPZRdqw0d0wUeWxJnAYGDOB5EptWLmLwvVbNzfmiv6PDyJ4bmbboJkT+Dwq1tjmw1eFNbitTdHk0L/ncQ9v5o7MoAiZKUID3b1mHCASwPk= 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: On Mon, Jan 23, 2023 at 9:43 PM Alistair Popple wrote: > > Having large amounts of unmovable or unreclaimable memory in a system > can lead to system instability due to increasing the likelihood of > encountering out-of-memory conditions. Therefore it is desirable to > limit the amount of memory users can lock or pin. > > From userspace such limits can be enforced by setting > RLIMIT_MEMLOCK. However there is no standard method that drivers and > other in-kernel users can use to check and enforce this limit. > > This has lead to a large number of inconsistencies in how limits are > enforced. For example some drivers will use mm->locked_mm while others > will use mm->pinned_mm or user->locked_mm. It is therefore possible to > have up to three times RLIMIT_MEMLOCKED pinned. > > Having pinned memory limited per-task also makes it easy for users to > exceed the limit. For example drivers that pin memory with > pin_user_pages() it tends to remain pinned after fork. To deal with > this and other issues this series introduces a cgroup for tracking and > limiting the number of pages pinned or locked by tasks in the group. > > However the existing behaviour with regards to the rlimit needs to be > maintained. Therefore the lesser of the two limits is > enforced. Furthermore having CAP_IPC_LOCK usually bypasses the rlimit, > but this bypass is not allowed for the cgroup. > > The first part of this series converts existing drivers which > open-code the use of locked_mm/pinned_mm over to a common interface > which manages the refcounts of the associated task/mm/user > structs. This ensures accounting of pages is consistent and makes it > easier to add charging of the cgroup. > > The second part of the series adds the cgroup and converts core mm > code such as mlock over to charging the cgroup before finally > introducing some selftests. I didn't go through the entire series, so apologies if this was mentioned somewhere, but do you mind elaborating on why this is added as a separate cgroup controller rather than an extension of the memory cgroup controller? > > > As I don't have access to systems with all the various devices I > haven't been able to test all driver changes. Any help there would be > appreciated. > > Alistair Popple (19): > mm: Introduce vm_account > drivers/vhost: Convert to use vm_account > drivers/vdpa: Convert vdpa to use the new vm_structure > infiniband/umem: Convert to use vm_account > RMDA/siw: Convert to use vm_account > RDMA/usnic: convert to use vm_account > vfio/type1: Charge pinned pages to pinned_vm instead of locked_vm > vfio/spapr_tce: Convert accounting to pinned_vm > io_uring: convert to use vm_account > net: skb: Switch to using vm_account > xdp: convert to use vm_account > kvm/book3s_64_vio: Convert account_locked_vm() to vm_account_pinned() > fpga: dfl: afu: convert to use vm_account > mm: Introduce a cgroup for pinned memory > mm/util: Extend vm_account to charge pages against the pin cgroup > mm/util: Refactor account_locked_vm > mm: Convert mmap and mlock to use account_locked_vm > mm/mmap: Charge locked memory to pins cgroup > selftests/vm: Add pins-cgroup selftest for mlock/mmap > > MAINTAINERS | 8 +- > arch/powerpc/kvm/book3s_64_vio.c | 10 +- > arch/powerpc/mm/book3s64/iommu_api.c | 29 +-- > drivers/fpga/dfl-afu-dma-region.c | 11 +- > drivers/fpga/dfl-afu.h | 1 +- > drivers/infiniband/core/umem.c | 16 +- > drivers/infiniband/core/umem_odp.c | 6 +- > drivers/infiniband/hw/usnic/usnic_uiom.c | 13 +- > drivers/infiniband/hw/usnic/usnic_uiom.h | 1 +- > drivers/infiniband/sw/siw/siw.h | 2 +- > drivers/infiniband/sw/siw/siw_mem.c | 20 +-- > drivers/infiniband/sw/siw/siw_verbs.c | 15 +- > drivers/vdpa/vdpa_user/vduse_dev.c | 20 +-- > drivers/vfio/vfio_iommu_spapr_tce.c | 15 +- > drivers/vfio/vfio_iommu_type1.c | 59 +---- > drivers/vhost/vdpa.c | 9 +- > drivers/vhost/vhost.c | 2 +- > drivers/vhost/vhost.h | 1 +- > include/linux/cgroup.h | 20 ++- > include/linux/cgroup_subsys.h | 4 +- > include/linux/io_uring_types.h | 3 +- > include/linux/kvm_host.h | 1 +- > include/linux/mm.h | 5 +- > include/linux/mm_types.h | 88 ++++++++- > include/linux/skbuff.h | 6 +- > include/net/sock.h | 2 +- > include/net/xdp_sock.h | 2 +- > include/rdma/ib_umem.h | 1 +- > 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 +- > mm/Kconfig | 11 +- > mm/Makefile | 1 +- > mm/internal.h | 2 +- > mm/mlock.c | 76 +------ > mm/mmap.c | 76 +++---- > mm/mremap.c | 54 +++-- > mm/pins_cgroup.c | 273 ++++++++++++++++++++++++- > mm/secretmem.c | 6 +- > mm/util.c | 196 +++++++++++++++-- > net/core/skbuff.c | 47 +--- > net/rds/message.c | 9 +- > net/xdp/xdp_umem.c | 38 +-- > tools/testing/selftests/vm/Makefile | 1 +- > tools/testing/selftests/vm/pins-cgroup.c | 271 ++++++++++++++++++++++++- > virt/kvm/kvm_main.c | 3 +- > 48 files changed, 1114 insertions(+), 401 deletions(-) > create mode 100644 mm/pins_cgroup.c > create mode 100644 tools/testing/selftests/vm/pins-cgroup.c > > base-commit: 2241ab53cbb5cdb08a6b2d4688feb13971058f65 > -- > git-series 0.9.1 >