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: 13145419 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 B398FC05027 for ; Sat, 18 Feb 2023 00:43:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5880A6B0098; Fri, 17 Feb 2023 19:43:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 55EEA6B009A; Fri, 17 Feb 2023 19:43:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42CB66B009B; Fri, 17 Feb 2023 19:43:18 -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 3430E6B0098 for ; Fri, 17 Feb 2023 19:43:18 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 12C971209EF for ; Sat, 18 Feb 2023 00:43:18 +0000 (UTC) X-FDA: 80478563676.06.09763BC Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 516AE1C0004 for ; Sat, 18 Feb 2023 00:43:16 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=p4J50SCY; spf=pass (imf21.hostedemail.com: domain of 3Ix_wYwsKCEgkmuo1vo83xqqyyqvo.mywvsx47-wwu5kmu.y1q@flex--ackerleytng.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Ix_wYwsKCEgkmuo1vo83xqqyyqvo.mywvsx47-wwu5kmu.y1q@flex--ackerleytng.bounces.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=1676680996; 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=a9HoC/OaMmMmyFwGFTweDzsomxpTI5zBveAVKH/mxUs=; b=Msy0BVcBnCyF0e8/JVLovJSWYH7Dfyz5RdW5qWji73CswgCYMV7Tyb6jhgKHDJCtGlQAFg agVFp4zWWXbAi+UCeNmahTU2ZiH3vNUgXmu5XcCIyAvZIOzSdHzUlNiwnLdDEZYym+ym/N q6LWpAJYG7VBte07OlAKxqgupfi/ppo= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=p4J50SCY; spf=pass (imf21.hostedemail.com: domain of 3Ix_wYwsKCEgkmuo1vo83xqqyyqvo.mywvsx47-wwu5kmu.y1q@flex--ackerleytng.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Ix_wYwsKCEgkmuo1vo83xqqyyqvo.mywvsx47-wwu5kmu.y1q@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676680996; a=rsa-sha256; cv=none; b=ZLk4UPXoFiDi8sXG+mzjvYgR/9pS2uEXLQEh5lYnEZhyB9CMKRovHZQS0ZLGTY2u5XkpRc l0mIMU4ByiVPKQh2QI1+P3vA5qDg6Kd4pUWmjKINhTAw7l/LTVYS5X9mUF/p3Kev43JJ7Z S1PkNDb6HmG9oNfC90Rf4mM2ggAZPQM= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5365741ec51so24651587b3.1 for ; Fri, 17 Feb 2023 16:43:16 -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=2UT1FMsgR8/akoo9aqvoJvwB3iGlvMZZsyE5n5/3HSP0HxlwGAXss6Mw8fGF+cYXV7 0tFot+0OY3K14D1rIde/Wo8aLzclcqIHLp48lJUkSyQ4VxoDSGhVpQbQ5ySkugfSo3o4 keyXVvOsUd7Rsr/yCuA7zGRCpO8kkBM7/iKTJpX7qiCZKinMfPGOJJMYCm16yVZeS3ZE 4L/GyihXcDAK96qLC1HHhE8zr6nyo1F8DUt5MxZZZhFlC/04I+SqlP0iA8SJ5uhHejaa KRysSBfyPxbig/1MpHjIhN3FirS/i/qnXPk5BYL2699Zits5jpB16m1GothUdzT2EDgB aw6w== X-Gm-Message-State: AO0yUKXi+8+oB+S2sS4dV4GKhTiOQEwh568cSp1hPKUGFg957vVVaQsm cacnkw31Q3vsMzz9vlx6FFRY/+YW8/BI6orIgg== 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 X-Rspamd-Queue-Id: 516AE1C0004 X-Stat-Signature: fmhxc4kkynmidf4c8trp5ut55gf864yt X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676680996-105350 X-HE-Meta: U2FsdGVkX1+E4xTnMlBMcXfTCgksNE0aRY9tajucFxXxlkqplpIQ3q9d3qrmnt9KQxJeMTx5giRP1Cazo5grcW9NOeFaIrXy4f6UMPYtcFjZrrl1XfjUyFKJ2X/CBGLNHV71PRZX8VeNhilBeFTqJOB/sn0ibmpGkN9jRhxZEuWTbxamRtHDc1XL3xk45u4KUBnr9V1O8ct6XrT2jd4vLduRx9G+2TtzuPCrnwlhGxW4r/+fvR7RUJ45rqqm9TZjEMF2B5dmJqBrRo5LgaFpZyh5CkOxSgUITc6xgrtfoYoPXwU5TODcx81cuUMfx+3hugXD8g18AR+tQWVJMe0yXZAnjIDX7Mt5lQlawTXAo3zzPmeWdQnEfiSly+8tCphGfbg6v2WkFsTWytL2Udjy2XzowiN7aPihxr/ReBH070dPmkz+hlFFefBdmac8ynLgxH7vvILrt/gUM3/0j/hmObyR0C0uupNqmVmdHGGZvxvlJ7mVmN/owsFqhdwpmmuiNWF9koz9ZYlAd9RTi/GAKxt6yOZD9ELG7JSSatTZ04fPVjZuQNJ7kB6pYF59/V2CunbYDItHAcjvakzUUSKpBn3dWXh7+Csw+Jz/B5c6tJfdm6NBGUJ1ofnUaPdncLqVXmDu5bmasiv7nWG2G/tXpC9ITp4uvLecIFEvIG43pFg2YviFKzwe4FXyBl4nogFmYZzfcghBnBo7rTzllCbvovhBoOVYRv88WY6hyAp1DIHc93KNDgDG1QIq0/cdHhVN4+6EzlB3hdltql6zQyrB3mMYYOLy4LwiENAtOXR9/WE//0VakFoq590HD+wlF9Yv9QiLDyajfyo/9yszwf5Kr58XgllGx/ptTaLP/51NiVIxBRcVx+388WZTgSDSxEs/YVePCBUzZaa7wreen1iBzAKX0dOK5Z4zAOoH3ggsIzHIzMfU4C1g2Ofb125VtnkSsPyE7DrwwosRDDvvijV 7vGNkgR7 eC4sgGyLBFbXkpI1AWyqximgrernojtalzeTNFEtcmbAlxkE9P9ItZwc5v0CskONqfKMhD4uY5MP/4gSsraC0vwH0cab7Bci/ORCDOW/5qbul97wYIUi/q4b7xXdV35hu7C6e6DBy/pDe3tSCbnsUT8DUk0YnLWPm9WgW+F/RfZQzFcwPZQIuieW9sh3G2OAk6UfLumLlQtARn6qDUY8np0dbrLmKNKRAoGLhTZ2oMXu+wIbMYcTSwtuLbeyrYG1gKpVK/4JvrbpRD77E+s/1q24zk178n0KZwQc8wiCRv7lUW2a/KkXLJ+Ao1XZXVnbLjvJd2zr3wJhqiTA1mOTVJCShyXva1z9iBXPBFC17ms8yhjj5lXv4xEn6zHPGuf2vvxzg6nocCJjO84AEjUfLbBGvUvAWdRvtOjav1eYLOPEy5WIXFh/S0KnOOEc7C2yMeqOE4GVF2gNiKLrsNHtuWlmDbgqbVwV9zktSYwMJ6uEdNy1wGcB0Ar30AcYNYEulEZvZDeo/W6x+4mDjOGivUOvM1Nis77xETFc24IUQ86DsNk2i5X14/4J3kKa2RqcoIB3kTOOUFuusL3/cKQ3HmmdWSHvw+fIAE1+q9K0yv5F363FiYGhUB2gieQF0EN2qRhWD6iLJRI2peHogkQugCteNk+yJha5wgyQQ0QkzvUcPKjbDqAVb0lOsCow7IeadgT0j7WCtspiaVvE= 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: 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); } }