From patchwork Fri Jul 30 08:13:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12410633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 254E7C4338F for ; Fri, 30 Jul 2021 08:13:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BACE960F6B for ; Fri, 30 Jul 2021 08:13:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BACE960F6B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5DDDD8D0003; Fri, 30 Jul 2021 04:13:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 58DD98D0001; Fri, 30 Jul 2021 04:13:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4558D8D0003; Fri, 30 Jul 2021 04:13:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0182.hostedemail.com [216.40.44.182]) by kanga.kvack.org (Postfix) with ESMTP id 2B9BF8D0001 for ; Fri, 30 Jul 2021 04:13:05 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D3E3326DE0 for ; Fri, 30 Jul 2021 08:13:04 +0000 (UTC) X-FDA: 78418538688.25.09C0A9C Received: from mail-qt1-f181.google.com (mail-qt1-f181.google.com [209.85.160.181]) by imf19.hostedemail.com (Postfix) with ESMTP id 8F87EB002444 for ; Fri, 30 Jul 2021 08:13:04 +0000 (UTC) Received: by mail-qt1-f181.google.com with SMTP id h27so5831028qtu.9 for ; Fri, 30 Jul 2021 01:13:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=6jcq19xGjLmUrRwSvlYxDwEEZEY30Ie1/WmTXGS/o98=; b=Kac6JXUK7HkQ+j1nfCyWf78Lx9/J8DhbK9cEe+BUdcchaQcuwA3yUFSUhn/812O94Y NYSWAx93XtwzseOdArk3qWr12pWQKPcJXgne/v78P0RQYFGrJ9nealVyPoIWx/EqYa7m hDroul1FOfEO5SGdz3xKVOhIExAaODMNchQY4wjDIFQbwb10BIZ+bstMTSQIg1LN8qqy eQAeDVhg1sz7SnP/E6f7lZDfkw7dTurQMEXnYyWb1vsMskpJatZ803Gdo+KCv9j+v8j+ ZEaOVViwafQGLKntkVwsZOYOPJK6ZWRqxSUO+LBdEYTTuWXaUrj8wSn5ScQLmiKbcD64 v8vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=6jcq19xGjLmUrRwSvlYxDwEEZEY30Ie1/WmTXGS/o98=; b=pcyrGOxUh/ckG3MDL9zsC+DHC46Y+ENvI/Rw2q4ucc18O9K47vzUOi09RtEMCgs0Gh TWuEdiOSZDz+LGaRUXA8q7tSzrlMd90cC9erjuO39QdmVZmLtEAByJccm1QHmgWOszyA Izq0nK5UHrESxUr6lGcnk6SCrTN18PcXDaPlwxLO7LylQme0JPV9YjcPFPogmVSBn+z2 ojmo58H5lYdZBnBEyXPTvr7/tciq9s6saE9DvF99nExdrqgY8//XkpR/JLfJpkpQE4Zp ugjDiyEEPMB5APIGrQeNaS01xv/ud3hAdnXTtAHfhBbXZy+JYoXsjsvHavZ8q18MklvQ 9gsA== X-Gm-Message-State: AOAM533RyC4P9WwL4ZCWvRtuAxf1HSB4qnwsHOkA7RT3Tws5z55aw3HF 59YfX+kVcdGturx7ugLzeAwmaQ== X-Google-Smtp-Source: ABdhPJyEd1jy6kKL/d+sUdaFX+XbhOvASGlRdpkXHz5LKfA/TN8qNb3mrpImsaMxEE6XRNqjUfjSpg== X-Received: by 2002:ac8:5552:: with SMTP id o18mr1239016qtr.51.1627632783757; Fri, 30 Jul 2021 01:13:03 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id d129sm539530qkf.136.2021.07.30.01.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 01:13:02 -0700 (PDT) Date: Fri, 30 Jul 2021 01:13:00 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: Andrew Morton cc: Hugh Dickins , Shakeel Butt , "Kirill A. Shutemov" , Yang Shi , Miaohe Lin , Mike Kravetz , Michal Hocko , Rik van Riel , Christoph Hellwig , Matthew Wilcox , "Eric W. Biederman" , Alexey Gladkov , Chris Wilson , Matthew Auld , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 16/16] memfd: memfd_create(name, MFD_MEM_LOCK) for memlocked shmem In-Reply-To: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> Message-ID: References: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8F87EB002444 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=Kac6JXUK; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of hughd@google.com designates 209.85.160.181 as permitted sender) smtp.mailfrom=hughd@google.com X-Stat-Signature: axe4bkn19h853qoct3pkk9h3md31nsee X-HE-Tag: 1627632784-481629 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: Now that the size of a memlocked file can be changed, memfd_create() can accept an MFD_MEM_LOCK flag to request memlocking, even though the initial size is of course 0. Signed-off-by: Hugh Dickins Reported-by: kernel test robot --- include/uapi/linux/memfd.h | 1 + mm/memfd.c | 7 +++++-- mm/shmem.c | 13 ++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/memfd.h b/include/uapi/linux/memfd.h index 8358a69e78cc..9113b5aa1763 100644 --- a/include/uapi/linux/memfd.h +++ b/include/uapi/linux/memfd.h @@ -9,6 +9,7 @@ #define MFD_ALLOW_SEALING 0x0002U #define MFD_HUGETLB 0x0004U /* Use hugetlbfs */ #define MFD_HUGEPAGE 0x0008U /* Use huge tmpfs */ +#define MFD_MEM_LOCK 0x0010U /* Memlock tmpfs */ /* * Huge page size encoding when MFD_HUGETLB is specified, and a huge page diff --git a/mm/memfd.c b/mm/memfd.c index 0d1a504d2fc9..e39f9eed55d2 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -248,7 +248,8 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg) #define MFD_ALL_FLAGS (MFD_CLOEXEC | \ MFD_ALLOW_SEALING | \ MFD_HUGETLB | \ - MFD_HUGEPAGE) + MFD_HUGEPAGE | \ + MFD_MEM_LOCK) SYSCALL_DEFINE2(memfd_create, const char __user *, uname, @@ -262,7 +263,7 @@ SYSCALL_DEFINE2(memfd_create, if (flags & MFD_HUGETLB) { /* Disallow huge tmpfs when choosing hugetlbfs */ - if (flags & MFD_HUGEPAGE) + if (flags & (MFD_HUGEPAGE | MFD_MEM_LOCK)) return -EINVAL; /* Allow huge page size encoding in flags. */ if (flags & ~(unsigned int)(MFD_ALL_FLAGS | @@ -314,6 +315,8 @@ SYSCALL_DEFINE2(memfd_create, if (flags & MFD_HUGEPAGE) vm_flags |= VM_HUGEPAGE; + if (flags & MFD_MEM_LOCK) + vm_flags |= VM_LOCKED; file = shmem_file_setup(name, 0, vm_flags); } diff --git a/mm/shmem.c b/mm/shmem.c index fa4a264453bf..a0a83e59ae07 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2395,7 +2395,7 @@ static struct inode *shmem_get_inode(struct super_block *sb, const struct inode spin_lock_init(&info->lock); atomic_set(&info->stop_eviction, 0); info->seals = F_SEAL_SEAL; - info->flags = flags & VM_NORESERVE; + info->flags = flags & (VM_NORESERVE | VM_LOCKED); if ((flags & VM_HUGEPAGE) && transparent_hugepage_allowed(sbinfo) && !test_bit(MMF_DISABLE_THP, ¤t->mm->flags)) @@ -4254,6 +4254,17 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, l inode->i_size = size; clear_nlink(inode); /* It is unlinked */ res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); + if (!IS_ERR(res) && (flags & VM_LOCKED)) { + struct ucounts *ucounts = current_ucounts(); + /* + * Only memfd_create() may pass VM_LOCKED, and it passes + * size 0; but avoid that assumption in case it changes. + */ + if (user_shm_lock(size, ucounts, true)) + SHMEM_I(inode)->mlock_ucounts = ucounts; + else + res = ERR_PTR(-EPERM); + } if (!IS_ERR(res)) res = alloc_file_pseudo(inode, mnt, name, O_RDWR, &shmem_file_operations);