From mboxrd@z Thu Jan 1 00:00:00 1970 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 1897AC43217 for ; Mon, 17 Oct 2022 21:56:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53A136B0072; Mon, 17 Oct 2022 17:56:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E9F76B0075; Mon, 17 Oct 2022 17:56:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B1756B0078; Mon, 17 Oct 2022 17:56:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2C56C6B0072 for ; Mon, 17 Oct 2022 17:56:58 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E694B160EE1 for ; Mon, 17 Oct 2022 21:56:57 +0000 (UTC) X-FDA: 80031802074.08.114117C Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf19.hostedemail.com (Postfix) with ESMTP id E0D4D1A0030 for ; Mon, 17 Oct 2022 21:56:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666043816; x=1697579816; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=+DTNPAffPPvJd+GNSv7BeFveo34E4B6ZbDE8a3YpK5s=; b=Rdt/YQ1Bq623byhL1oFXSNRUThduf09B0Hd7cUxCWw9zF5+b2znE2dwN LydlAz3Q55YwS0VLMl5JUGcsRcSOTHOwp5XZZ4HVWRrCV++CA2K2m3Afr dOTzJC3eNpLAZKlmbwjxZTyfLBA4JtOh1CreYUGEpjb0NIMKemAop+vRv OsM95TjywXv2CweMepWQmf7IHcwgjupwq4GoVS3O4HtcC2v3aJhuRI/h/ fpVTe6j7K6MoZYfDTCq56yntzbPKb/LfAn/Nooj8ySN/fbWgzsl81kjS2 jb22xzs1jv2n9IZQXfemy3+XXV+aznJivxKnp+B+T9S+qq4EpvYHe2wBx Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10503"; a="307591568" X-IronPort-AV: E=Sophos;i="5.95,192,1661842800"; d="scan'208";a="307591568" Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2022 14:56:55 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10503"; a="803493914" X-IronPort-AV: E=Sophos;i="5.95,192,1661842800"; d="scan'208";a="803493914" Received: from dludovic-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.44.179]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2022 14:56:43 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 068F6104611; Tue, 18 Oct 2022 00:56:41 +0300 (+03) Date: Tue, 18 Oct 2022 00:56:40 +0300 From: "Kirill A . Shutemov" To: "Gupta, Pankaj" Cc: Vlastimil Babka , Chao Peng , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@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 , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vishal Annapurve , Yu Zhang , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com Subject: Re: [PATCH v8 1/8] mm/memfd: Introduce userspace inaccessible memfd Message-ID: <20221017215640.hobzcz47es7dq2bi@box.shutemov.name> References: <20220915142913.2213336-1-chao.p.peng@linux.intel.com> <20220915142913.2213336-2-chao.p.peng@linux.intel.com> <20221017161955.t4gditaztbwijgcn@box.shutemov.name> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666043817; a=rsa-sha256; cv=none; b=ztEbbL9dowDFlt1HMxnG4WhI7b6Mhkj+GyLDBNsXkUQ2KCSK08Vez0iay1o1u40qpbvlBv 5brJMXcEQkQFVoZFf7hlLozVWWYP+nRyKTW8fUlfHoqF751i11l0QOCBpC4O8eTjbE0wwj iPZaO7FBg7tREWD+NcotJdRWzCvIgr8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b="Rdt/YQ1B"; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf19.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666043817; 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=O/oWqT7xm8PcHZEbrwnGPS43cxooBBpfFKFe0k0wVh8=; b=4gI9bU5tWSBhNGvf1lAklfTuDJXsBRscVidDec3MInZ7eigwnrqm1lmmnabonTW/t8lgVv P070KPaqtq4YKX1zh4yVRMitdJ/MrTZPCeItXWb8hBHs666WhOp4oJpegX9Qys/XCNSeK6 Svo8Srg6aleAHuoIm0cxIiXzlrKnjI0= X-Stat-Signature: p453hxdjcxr4ng61bsx8ddg987hxkduk X-Rspamd-Queue-Id: E0D4D1A0030 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b="Rdt/YQ1B"; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf19.hostedemail.com: domain of kirill.shutemov@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=kirill.shutemov@linux.intel.com X-Rspamd-Server: rspam11 X-HE-Tag: 1666043816-110723 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: On Mon, Oct 17, 2022 at 06:39:06PM +0200, Gupta, Pankaj wrote: > On 10/17/2022 6:19 PM, Kirill A . Shutemov wrote: > > On Mon, Oct 17, 2022 at 03:00:21PM +0200, Vlastimil Babka wrote: > > > On 9/15/22 16:29, Chao Peng wrote: > > > > From: "Kirill A. Shutemov" > > > > > > > > KVM can use memfd-provided memory for guest memory. For normal userspace > > > > accessible memory, KVM userspace (e.g. QEMU) mmaps the memfd into its > > > > virtual address space and then tells KVM to use the virtual address to > > > > setup the mapping in the secondary page table (e.g. EPT). > > > > > > > > With confidential computing technologies like Intel TDX, the > > > > memfd-provided memory may be encrypted with special key for special > > > > software domain (e.g. KVM guest) and is not expected to be directly > > > > accessed by userspace. Precisely, userspace access to such encrypted > > > > memory may lead to host crash so it should be prevented. > > > > > > > > This patch introduces userspace inaccessible memfd (created with > > > > MFD_INACCESSIBLE). Its memory is inaccessible from userspace through > > > > ordinary MMU access (e.g. read/write/mmap) but can be accessed via > > > > in-kernel interface so KVM can directly interact with core-mm without > > > > the need to map the memory into KVM userspace. > > > > > > > > It provides semantics required for KVM guest private(encrypted) memory > > > > support that a file descriptor with this flag set is going to be used as > > > > the source of guest memory in confidential computing environments such > > > > as Intel TDX/AMD SEV. > > > > > > > > KVM userspace is still in charge of the lifecycle of the memfd. It > > > > should pass the opened fd to KVM. KVM uses the kernel APIs newly added > > > > in this patch to obtain the physical memory address and then populate > > > > the secondary page table entries. > > > > > > > > The userspace inaccessible memfd can be fallocate-ed and hole-punched > > > > from userspace. When hole-punching happens, KVM can get notified through > > > > inaccessible_notifier it then gets chance to remove any mapped entries > > > > of the range in the secondary page tables. > > > > > > > > The userspace inaccessible memfd itself is implemented as a shim layer > > > > on top of real memory file systems like tmpfs/hugetlbfs but this patch > > > > only implemented tmpfs. The allocated memory is currently marked as > > > > unmovable and unevictable, this is required for current confidential > > > > usage. But in future this might be changed. > > > > > > > > Signed-off-by: Kirill A. Shutemov > > > > Signed-off-by: Chao Peng > > > > --- > > > > > > ... > > > > > > > +static long inaccessible_fallocate(struct file *file, int mode, > > > > + loff_t offset, loff_t len) > > > > +{ > > > > + struct inaccessible_data *data = file->f_mapping->private_data; > > > > + struct file *memfd = data->memfd; > > > > + int ret; > > > > + > > > > + if (mode & FALLOC_FL_PUNCH_HOLE) { > > > > + if (!PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len)) > > > > + return -EINVAL; > > > > + } > > > > + > > > > + ret = memfd->f_op->fallocate(memfd, mode, offset, len); > > > > + inaccessible_notifier_invalidate(data, offset, offset + len); > > > > > > Wonder if invalidate should precede the actual hole punch, otherwise we open > > > a window where the page tables point to memory no longer valid? > > > > Yes, you are right. Thanks for catching this. > > I also noticed this. But then thought the memory would be anyways zeroed > (hole punched) before this call? Hole punching can free pages, given that offset/len covers full page. -- Kiryl Shutsemau / Kirill A. Shutemov