Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3521478pxb; Mon, 24 Jan 2022 11:18:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJyEDI+sQVUJV/yDy+cIUPYE5UdzMPG8YhtrdgvghxdZUYua8pCRQzLEyM6yrh3GzMsv30X8 X-Received: by 2002:a17:90a:1782:: with SMTP id q2mr3313098pja.145.1643051913833; Mon, 24 Jan 2022 11:18:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643051913; cv=none; d=google.com; s=arc-20160816; b=PYOS9jVVPuzY0sHav7Fi7rj03bX7dDQQuDTd4hpiIHDhhrQodkTllM47gjBU19cmCk 0ChZMFLW1oRrzNxa0D1smMcb7D/kdfnjXBvIVmK0S1wGGp+ujZxz8EvL8nRvOY9RyjKZ xH59GJYg4ChfRH4suls9tb05MmluQoZGGmFQ2MAxbi+TtZ0qMs9Ftz4S3pqbMLNHwq5d mf2xM0LjY+x1cwDuheaOCYNgIx28KDzttStxvPMIyw7xE9WL4/yy2+LNqX4qYuXj+dve jkcWfe7Bbtr9graBs6so0UjJ1/Ax0eUNr/w7hhkDzRf5nZILiq7o7htTKevI3bKnbRXT AIhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=iDl/4HOX9BaxrjEHCI0U6bv7Dbjvqi0Vz5/9JNYMEmg=; b=oDW8JcSIzLHUPzjQ386bsaNLtd5YQ0G6eGISX+boQXZKv6wCGfv97Vq6PUxMq7YqT4 Z7fbOYYBxq+M2NgqSFmTuJoPU8LKPiW7g2Uj64hXlgUybsSgRS8pUe9lq2UIGHaYquJC 5TPBbmGffuX0G8/eeddwUFO03F97st9S3G14vSrqEUqFFB2gnd9OikKSfgTH+r1k2aTe o1ZBE6cSRf18ejnxpD78vAAh85TEYlvhMo3RTnKYQH4TKs1m1RAOA3vd2pnY+NgeXczf ChE6tpJXxyjjL7OmzzIMlLdBHhIxowZ4A3vhjXLToapxjF+wNtOqylMrf2Lw1D924vSR tyhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KLRPcaAb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f24si10693805pgg.792.2022.01.24.11.18.20; Mon, 24 Jan 2022 11:18:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KLRPcaAb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243165AbiAXNaU (ORCPT + 99 others); Mon, 24 Jan 2022 08:30:20 -0500 Received: from mga07.intel.com ([134.134.136.100]:10513 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239975AbiAXNaS (ORCPT ); Mon, 24 Jan 2022 08:30:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643031018; x=1674567018; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:in-reply-to; bh=s3MS9jlacq/GjKUc1J7BgRzARDrYpUsH/uofSxi2lpg=; b=KLRPcaAbDHrNZpMyBof98siiy0gAuPPtlI4CKpGuBwNxzRetDKWphdC2 M1oo/sIUZg4nrBGVO7nf9hlVbZSIAk28FR33u2qrrLP8crYqrxVoqC1Ve cmSg8ewfLxDCphmv31A8o4+OaeeCPJMYavnkHXm3z2TtHIiLW5PkXs9Z5 v7X0BUG/w3HXdktUQLGkGqIqKHVxb2InaeKZh+1njd7FhTt6wRy/BI88k W4hfEndP3MtbUrRgJ698e30lU51j0NlhN71QgYKyLSWHo8nzZT3ZksVNr Oh95EDet0nyKSanTgNQsC+shQhzmHA0UFcS5Sx7sUpfC/zB+Iyx2PqSPz w==; X-IronPort-AV: E=McAfee;i="6200,9189,10236"; a="309362579" X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="309362579" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jan 2022 05:30:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,311,1635231600"; d="scan'208";a="532077916" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.192.101]) by fmsmga007.fm.intel.com with ESMTP; 24 Jan 2022 05:30:05 -0800 Date: Mon, 24 Jan 2022 21:29:36 +0800 From: Chao Peng To: Steven Price Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Subject: Re: [PATCH v4 02/12] mm/memfd: Introduce MFD_INACCESSIBLE flag Message-ID: <20220124132936.GA55051@chaop.bj.intel.com> Reply-To: Chao Peng References: <20220118132121.31388-1-chao.p.peng@linux.intel.com> <20220118132121.31388-3-chao.p.peng@linux.intel.com> <8f1eba03-e5e9-e9fc-084d-0ef683093d65@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8f1eba03-e5e9-e9fc-084d-0ef683093d65@arm.com> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 21, 2022 at 03:50:55PM +0000, Steven Price wrote: > On 18/01/2022 13:21, Chao Peng wrote: > > Introduce a new memfd_create() flag indicating the content of the > > created memfd is inaccessible from userspace. It does this by force > > setting F_SEAL_INACCESSIBLE seal when the file is created. It also set > > F_SEAL_SEAL to prevent future sealing, which means, it can not coexist > > with MFD_ALLOW_SEALING. > > > > The pages backed by such memfd will be used as guest private memory in > > confidential computing environments such as Intel TDX/AMD SEV. Since > > page migration/swapping is not yet supported for such usages so these > > pages are currently marked as UNMOVABLE and UNEVICTABLE which makes > > them behave like long-term pinned pages. > > > > Signed-off-by: Chao Peng > > --- > > include/uapi/linux/memfd.h | 1 + > > mm/memfd.c | 20 +++++++++++++++++++- > > 2 files changed, 20 insertions(+), 1 deletion(-) > > > > diff --git a/include/uapi/linux/memfd.h b/include/uapi/linux/memfd.h > > index 7a8a26751c23..48750474b904 100644 > > --- a/include/uapi/linux/memfd.h > > +++ b/include/uapi/linux/memfd.h > > @@ -8,6 +8,7 @@ > > #define MFD_CLOEXEC 0x0001U > > #define MFD_ALLOW_SEALING 0x0002U > > #define MFD_HUGETLB 0x0004U > > +#define MFD_INACCESSIBLE 0x0008U > > > > /* > > * Huge page size encoding when MFD_HUGETLB is specified, and a huge page > > diff --git a/mm/memfd.c b/mm/memfd.c > > index 9f80f162791a..26998d96dc11 100644 > > --- a/mm/memfd.c > > +++ b/mm/memfd.c > > @@ -245,16 +245,19 @@ long memfd_fcntl(struct file *file, unsigned int cmd, unsigned long arg) > > #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1) > > #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) > > > > -#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB) > > +#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB | \ > > + MFD_INACCESSIBLE) > > > > SYSCALL_DEFINE2(memfd_create, > > const char __user *, uname, > > unsigned int, flags) > > { > > + struct address_space *mapping; > > unsigned int *file_seals; > > struct file *file; > > int fd, error; > > char *name; > > + gfp_t gfp; > > long len; > > > > if (!(flags & MFD_HUGETLB)) { > > @@ -267,6 +270,10 @@ SYSCALL_DEFINE2(memfd_create, > > return -EINVAL; > > } > > > > + /* Disallow sealing when MFD_INACCESSIBLE is set. */ > > + if (flags & MFD_INACCESSIBLE && flags & MFD_ALLOW_SEALING) > > + return -EINVAL; > > + > > /* length includes terminating zero */ > > len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); > > if (len <= 0) > > @@ -315,6 +322,17 @@ SYSCALL_DEFINE2(memfd_create, > > *file_seals &= ~F_SEAL_SEAL; > > } > > > > + if (flags & MFD_INACCESSIBLE) { > > + mapping = file_inode(file)->i_mapping; > > + gfp = mapping_gfp_mask(mapping); > > + gfp &= ~__GFP_MOVABLE; > > + mapping_set_gfp_mask(mapping, gfp); > > + mapping_set_unevictable(mapping); > > + > > + file_seals = memfd_file_seals_ptr(file); > > + *file_seals &= F_SEAL_SEAL | F_SEAL_INACCESSIBLE; > > This looks backwards - the flags should be set on *file_seals, but here > you are unsetting all other flags. Thanks Steve. '|=' actually should be used here. Chao > > Steve > > > + } > > + > > fd_install(fd, file); > > kfree(name); > > return fd; > >