From patchwork Fri Apr 14 00:11:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13210790 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18ED9C77B61 for ; Fri, 14 Apr 2023 00:12:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbjDNAME (ORCPT ); Thu, 13 Apr 2023 20:12:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229733AbjDNAMC (ORCPT ); Thu, 13 Apr 2023 20:12:02 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FA8A3A86 for ; Thu, 13 Apr 2023 17:12:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 85-20020a250d58000000b00b8f380b2bccso7596875ybn.14 for ; Thu, 13 Apr 2023 17:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431119; x=1684023119; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=i+pweVgTeWv08z0ELDTsB0Ltp1Ye2bcZK0KHo4GqVbw=; b=SxumLiXBZqVvPSsO4nE95gXtUUxXVcRm2giZRY9/5ORZ+OjADqAAbFJkobyKcqmfCt ePUsZvwKG04KkaahIzuPRZeniJUEzn01e6frQqq0jAgCaSwlASsR29xtCtNlSjb+T5pj VEWY0EVxAPptAmO48HG5tvNAJPPxntiFot3tECFeEreHjLrDdGnAKPlTKHy30+MbbqI8 64DeVDk4CEwT4IFGcaTIdQJJ7t2C3BZm2t+9Gp59h71CPchlPnK0jhl4w3WF/G3NQ2HG SBMasteYkoK5+FJ8YkRnsN77u+ShB1rljGgMoVKQvjdG4NVzxFbFqiD01CFCoQkbIqFa aeNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431119; x=1684023119; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=i+pweVgTeWv08z0ELDTsB0Ltp1Ye2bcZK0KHo4GqVbw=; b=HC0BKzlgTENMKYLnN19alLe8MgCgyauSwyOFVnK17LFoFwRBNwfvG97/qBVDFzZ3ko X5zmUkxAlLHmijuqjJCM65HjdZdeQhAoFNBiOMRalwkq50KcPu8/oeRziK/mB4cn0YYC gtmpPScpvRxiwFLkRN46Qviss5lcEjvEQsaPhNWEhfCeNRcq/1HNbo3/S5szltACQH3p Nx+3cD8cyYTjMppR37fekhmu+nBWCwRuAnHhCaJT0oLj2IKHEyBqyxjQwA2rg742xJPX 5plpOW9zkj94gUxe4D5+7qdSdIDV0Jk/oVjFtY8Ff+jnoFet4NqDCdXyx3oBUTzVYBqv fVZA== X-Gm-Message-State: AAQBX9fFugBVVPYKvcLf0CFPC21EBA1VndGkzxr9VMEJe+Nd8vuDXYlN IQIV8izPsxo2XS4dnUQU0HcfbcHXdqk6MvH0fzeD5JqeqL+DTJF+rW/OMGrBpl/qviLQFDNsH1x Act6/ngW7jjysrCX6xvom5T/SSUJB54i2LHpL+bNcEMDtHOQTcCFAlb0qJtGnm+EvmP2/pno= X-Google-Smtp-Source: AKy350aFyMPBdyqRFopcQD1OOk7RIztiWHN973sMgROTFTwCsBiX6bTJGMx6PuL8Cu3fjd47w7zygKuC4QNdUU7X4w== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:72d6:0:b0:b8f:55f6:e50f with SMTP id n205-20020a2572d6000000b00b8f55f6e50fmr2609596ybc.1.1681431119162; Thu, 13 Apr 2023 17:11:59 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:49 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: Subject: [RFC PATCH 0/6] Setting memory policy for restrictedmem file From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, muchun.song@linux.dev, feng.tang@intel.com, brgerst@gmail.com, rdunlap@infradead.org, masahiroy@kernel.org, mailhol.vincent@wanadoo.fr, Ackerley Tng Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hello, This patchset builds upon the memfd_restricted() system call that was discussed in the 'KVM: mm: fd-based approach for supporting KVM' patch series [1]. The tree can be found at: https://github.com/googleprodkernel/linux-cc/tree/restrictedmem-set-memory-policy In this patchset, a new syscall is introduced, which allows userspace to set the memory policy (e.g. NUMA bindings) for a restrictedmem file, to the granularity of offsets within the file. The offset/length tuple is termed a file_range which is passed to the kernel via a pointer to get around the limit of 6 arguments for a syscall. The following other approaches were also considered: 1. Pre-configuring a mount with a memory policy and providing that mount to memfd_restricted() as proposed at [2]. + Pro: It allows choice of a specific backing mount with custom memory policy configurations + Con: Will need to create an entire new mount just to set memory policy for a restrictedmem file; files on the same mount cannot have different memory policies. 2. Passing memory policy to the memfd_restricted() syscall at creation time. + Pro: Only need to make a single syscall to create a file with a given memory policy + Con: At creation time, the kernel doesn’t know the size of the restrictedmem file. Given that memory policy is stored in the inode based on ranges (start, end), it is awkward for the kernel to store the memory policy and then add hooks to set the memory policy when allocation is done. 3. A more generic fbind(): it seems like this new functionality is really only needed for restrictedmem files, hence a separate, specific syscall was proposed to avoid complexities with handling conflicting policies that may be specified via other syscalls like mbind() TODOs + Return -EINVAL if file_range is not within the size of the file and tests for this Dependencies: + Chao’s work on UPM [3] [1] https://lore.kernel.org/lkml/20221202061347.1070246-1-chao.p.peng@linux.intel.com/T/ [2] https://lore.kernel.org/lkml/cover.1681176340.git.ackerleytng@google.com/T/ [3] https://github.com/chao-p/linux/commits/privmem-v11.5 --- Ackerley Tng (6): mm: shmem: Refactor out shmem_shared_policy() function mm: mempolicy: Refactor out mpol_init_from_nodemask mm: mempolicy: Refactor out __mpol_set_shared_policy() mm: mempolicy: Add and expose mpol_create mm: restrictedmem: Add memfd_restricted_bind() syscall selftests: mm: Add selftest for memfd_restricted_bind() arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/mempolicy.h | 4 + include/linux/shmem_fs.h | 7 + include/linux/syscalls.h | 5 + include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mempolicy.h | 7 +- kernel/sys_ni.c | 1 + mm/mempolicy.c | 100 ++++++++++--- mm/restrictedmem.c | 75 ++++++++++ mm/shmem.c | 10 +- scripts/checksyscalls.sh | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 8 + .../selftests/mm/memfd_restricted_bind.c | 139 ++++++++++++++++++ .../mm/restrictedmem_testmod/Makefile | 21 +++ .../restrictedmem_testmod.c | 89 +++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 6 + 18 files changed, 454 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/mm/memfd_restricted_bind.c create mode 100644 tools/testing/selftests/mm/restrictedmem_testmod/Makefile create mode 100644 tools/testing/selftests/mm/restrictedmem_testmod/restrictedmem_testmod.c -- 2.40.0.634.g4ca3ef3211-goog