From patchwork Sat Feb 18 00:43:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13145437 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 C6D21C05027 for ; Sat, 18 Feb 2023 00:45:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjBRApN (ORCPT ); Fri, 17 Feb 2023 19:45:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230137AbjBRApA (ORCPT ); Fri, 17 Feb 2023 19:45:00 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF7D655E71 for ; Fri, 17 Feb 2023 16:44:30 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-536587ff9e1so22301597b3.21 for ; Fri, 17 Feb 2023 16:44:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=a9HoC/OaMmMmyFwGFTweDzsomxpTI5zBveAVKH/mxUs=; b=p4J50SCYcCeG3qGRpHChFsOtrnkHXhTK4OUQeA7csD+WhBd4bepBHtG/cyOfbbTSC+ lCFAqzmMUFKlMigeUVQiWbej8Gu0AWjDX2DuN4h3rP0oUlDEosc+NJ4U87QAa5/PzCNE CCI6mGg0jUZFutFG4yOh79Uont3On7YTn2CT4elMJDE1FfadxmUYwEME2piTlq5az1Mq 9aZyrgkj9RfvKY2E3TqhjPL810vpjoE3FXu/K+4aGPj7XXrOfXVxw+yab5AlL0F/7lnv mwMmGTIisoVBNs8uCuNHuCp5dmw5ILOZjOHaFdx58TbpMJJ+4truEcCA49DA7ssvncOM ZPVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=a9HoC/OaMmMmyFwGFTweDzsomxpTI5zBveAVKH/mxUs=; b=m0NASEVopNeNKQWp329qmYbxId69euw/pgbVPwbJzyO/GVRG6rFBYmjtLryIonnGql xnRhDiZz7ZSYUn8XhF3Ffn9Rc+zi3E3bJKgRkOk4ibrez0Jus9Aku8e14rBhSN9Ey9Ue +AqsSWy9F+hqfo1n74dOtHVdV+nk33NUVI1NUzpNqQCRBrZJ4rz55HPXOJfRJhPevslQ DwVpgudUpD2XopBVkudB/WiRyMe2AkeEAtadgz3WGzqEvNCo8M17PP66BTzuLHKZlslz a9VcAt3hzANPXDKL8H94f1vuJIUfKP3pu0AF4aUzkf/X09ITU/ixaPIfwIbKarlLOcIE jVuQ== X-Gm-Message-State: AO0yUKXFs2l3HvkzJBDjtZFWPnWNtpLVxAPQM2LFe20M/NRCGK9BtSKM medzSGdwtCG6bPbZYLrJ4eok1DhtrJ/Zuc4wZwwvF3QWyDttyS01w9taH8hyN1ucz+Amc3L4Zxl vAIqwC3moC3TfmwTgiU6jXqg729E4RzVPQ96Z28lyNxY2PT1d6r/s5fJgsm3+o7ecJKuRYnM= X-Google-Smtp-Source: AK7set/gCiyNU2zsSUQcnpfweO59d4+LMWomYJ6sq6DKFLDhVfbtMpiT7MZoq2d4nExGworQhG3UEyTFbFT8aAYUHA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6902:1024:b0:8fc:686c:cf87 with SMTP id x4-20020a056902102400b008fc686ccf87mr57267ybt.4.1676680995496; Fri, 17 Feb 2023 16:43:15 -0800 (PST) Date: Sat, 18 Feb 2023 00:43:01 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <4ea08e03d57152d505b747a6a570752dd698e315.1676680548.git.ackerleytng@google.com> Subject: [RFC PATCH 1/2] mm: restrictedmem: Add flag as THP allocation hint for memfd_restricted() syscall 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, Ackerley Tng Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Allow userspace to hint the kernel to use Transparent HugePages to back restricted memory on a per-file basis. Signed-off-by: Ackerley Tng --- include/uapi/linux/restrictedmem.h | 1 + mm/restrictedmem.c | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/include/uapi/linux/restrictedmem.h b/include/uapi/linux/restrictedmem.h index 9f108dd1ac4c..f671ccbb43bc 100644 --- a/include/uapi/linux/restrictedmem.h +++ b/include/uapi/linux/restrictedmem.h @@ -4,5 +4,6 @@ /* flags for memfd_restricted */ #define RMFD_TMPFILE 0x0001U +#define RMFD_HUGEPAGE 0x0002U #endif /* _UAPI_LINUX_RESTRICTEDMEM_H */ diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index 97f3e2159e8b..87c829960b31 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -190,19 +190,25 @@ static struct file *restrictedmem_file_create(struct file *memfd) return file; } -static int restrictedmem_create(struct vfsmount *mount) +static int restrictedmem_create(unsigned int flags, struct vfsmount *mount) { struct file *file, *restricted_file; int fd, err; + unsigned long shmem_setup_flags = VM_NORESERVE; fd = get_unused_fd_flags(0); if (fd < 0) return fd; - if (mount) - file = shmem_file_setup_with_mnt(mount, "memfd:restrictedmem", 0, VM_NORESERVE); - else - file = shmem_file_setup("memfd:restrictedmem", 0, VM_NORESERVE); + if (flags & RMFD_HUGEPAGE) + shmem_setup_flags |= VM_HUGEPAGE; + + if (mount) { + file = shmem_file_setup_with_mnt(mount, "memfd:restrictedmem", + 0, shmem_setup_flags); + } else { + file = shmem_file_setup("memfd:restrictedmem", 0, shmem_setup_flags); + } if (IS_ERR(file)) { err = PTR_ERR(file); @@ -230,7 +236,8 @@ static bool is_shmem_mount(struct vfsmount *mnt) return mnt->mnt_sb->s_magic == TMPFS_MAGIC; } -static int restrictedmem_create_from_path(const char __user *mount_path) +static int restrictedmem_create_from_path(unsigned int flags, + const char __user *mount_path) { int ret; struct path path; @@ -250,7 +257,7 @@ static int restrictedmem_create_from_path(const char __user *mount_path) if (unlikely(ret)) goto out; - ret = restrictedmem_create(path.mnt); + ret = restrictedmem_create(flags, path.mnt); mnt_drop_write(path.mnt); out: @@ -261,16 +268,16 @@ static int restrictedmem_create_from_path(const char __user *mount_path) SYSCALL_DEFINE2(memfd_restricted, unsigned int, flags, const char __user *, mount_path) { - if (flags & ~RMFD_TMPFILE) + if (flags & ~(RMFD_TMPFILE | RMFD_HUGEPAGE)) return -EINVAL; if (flags == RMFD_TMPFILE) { if (!mount_path) return -EINVAL; - return restrictedmem_create_from_path(mount_path); + return restrictedmem_create_from_path(flags, mount_path); } else { - return restrictedmem_create(NULL); + return restrictedmem_create(flags, NULL); } } From patchwork Sat Feb 18 00:43:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13145438 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 43A0BC636D6 for ; Sat, 18 Feb 2023 00:45:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230145AbjBRApP (ORCPT ); Fri, 17 Feb 2023 19:45:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230169AbjBRApI (ORCPT ); Fri, 17 Feb 2023 19:45:08 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D12196D7B8 for ; Fri, 17 Feb 2023 16:44:33 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id o14-20020a25810e000000b0095d2ada3d26so1841313ybk.5 for ; Fri, 17 Feb 2023 16:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=9mwmDPnm22mSNSwTn0VodViIvXaPz9XlM8QDWYhbyM8=; b=eMm01hnumElMl3UotGf8lJtLmKUjjLEmp4ZHK0o1H0LRyNA5w7x33BZeE7ifCfM7pg Cd3CBejXIUZT0dDASkiYet2Q11r1oqaxT2jWL7729gQTvNyIV+Z3A265SCXl8KKZFvM6 J2aSWma9rqTSDwyH7vONqfORZObAwWA+6hRLW5QjldLuMtFrDr6CiZLUw313wtVBTkEY Uql860ZaIf2fzbiPBXmxv8De94uhU4jaykP5woFVxM5M2xVs65vFvbC27a8oelqLROik ucodFnqa/c0QR5Ark7refIrmAYAsOKE19K26vuVEVEq0bwodrw1HGi4stiNrcoXlrrSh sdyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9mwmDPnm22mSNSwTn0VodViIvXaPz9XlM8QDWYhbyM8=; b=7UV1WOLKtopnItdvfXuaRePC6EshQA7PNGZuGM1w6tsgLrY9YcGEYIK1+UHPF+Z5EY /Kr8tKEeHcGhLnIY1co732gfG8wuFsihGiPr+oOFSmIH2jmVN7ABs76dIa6Sd1CMzE7G Jot2rKSLqVIU3khL59STUmkLkuaJKCkzkZFNvv8Uu/2cU0TgeUUFgr/Ib1cTq7N4POA0 q7KUqRh4EF4qzW06SH9TSXlCNGUEyl0ayqbcv1tSRhG9BUrAy1yhvGEy73rK2amt7apl FNo9iRKnStLE+GUuEFEt7wnm+F2NgQRDaP+cCpTklml3OEbDa5kC+KWMPrfLtY/FNhxn muVQ== X-Gm-Message-State: AO0yUKXxM6dG0X7LzMlKmJKB1b8OreYD1qaHSHjgONbNBqlfAc12/GVy jEFFzPuEW0LmsqIpTo758J4bZcT2sN/NYITFwXOjvBP125HYSffvndsYh2OUNqUX6Fv/EFNWBFt xp3UhG0wBjh0L3ogg7yPvJJFgpvJmquQpEDOQFAirQnyfzsSUFUvLqsAd33XlrZZsrzZ3cA8= X-Google-Smtp-Source: AK7set8GCw+l6Q5gppx0SI7xgHF70UCy3j05OUIa+XJfbCoB3vIVoiRjffivwnfUze8k0uHuDZvSsWzxIhc/w0Mo2w== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6902:10e:b0:95d:6b4f:a73a with SMTP id o14-20020a056902010e00b0095d6b4fa73amr5895ybh.8.1676681001601; Fri, 17 Feb 2023 16:43:21 -0800 (PST) Date: Sat, 18 Feb 2023 00:43:02 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <67956539824ea9dd66a94d67b046b2f4bb0aa6f2.1676680548.git.ackerleytng@google.com> Subject: [RFC PATCH 2/2] selftests: restrictedmem: Add selftest for RMFD_HUGEPAGE 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, Ackerley Tng Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Tests that when RMFD_HUGEPAGE is specified, restrictedmem will be backed by Transparent HugePages. Signed-off-by: Ackerley Tng --- .../restrictedmem_hugepage_test.c | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/testing/selftests/restrictedmem/restrictedmem_hugepage_test.c b/tools/testing/selftests/restrictedmem/restrictedmem_hugepage_test.c index 0d9cf2ced754..75283d68696f 100644 --- a/tools/testing/selftests/restrictedmem/restrictedmem_hugepage_test.c +++ b/tools/testing/selftests/restrictedmem/restrictedmem_hugepage_test.c @@ -180,6 +180,31 @@ TEST_F(reset_shmem_enabled, restrictedmem_fstat_shmem_enabled_always) close(mfd); } +TEST(restrictedmem_invalid_flags) +{ + int mfd = memfd_restricted(99, NULL); + + ASSERT_EQ(-1, mfd); + ASSERT_EQ(EINVAL, errno); +} + +TEST_F(reset_shmem_enabled, restrictedmem_rmfd_hugepage) +{ + int mfd = -1; + struct stat stat; + + ASSERT_EQ(0, set_shmem_thp_policy("never")); + + mfd = memfd_restricted(RMFD_HUGEPAGE, NULL); + ASSERT_NE(-1, mfd); + + ASSERT_EQ(0, fstat(mfd, &stat)); + + ASSERT_EQ(stat.st_blksize, get_hpage_pmd_size()); + + close(mfd); +} + TEST(restrictedmem_tmpfile_no_mount_path) { int mfd = memfd_restricted(RMFD_TMPFILE, NULL);