Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp510433rwj; Thu, 22 Dec 2022 10:31:42 -0800 (PST) X-Google-Smtp-Source: AMrXdXu/V7/g+wewPtlVv4S9vAooppZILPgmeDBTNOtwbwP/D4vAPCxPJntQh1tFXnjEmLSybML5 X-Received: by 2002:a05:6a00:4288:b0:578:74bc:c6da with SMTP id bx8-20020a056a00428800b0057874bcc6damr5957822pfb.17.1671733901960; Thu, 22 Dec 2022 10:31:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671733901; cv=none; d=google.com; s=arc-20160816; b=w1jqnIrDPXD7mey8LQnC9YM91mxdw6rgKayAkuLp0unzEKxReMpsxrgURh7LgsD+Dn 9AquPLQbsMyt6c8PDoAlAn3ySMH+J+hP77obFLXfv6wTltuTHmKMLciSH0DBl65TD92b Nokb+iUB+WZdHzwdCwN9w1A3z0mGzoySk25ta43fzE9UyQExQIkAtNpMRQuNEMTDxTgO X4TzHZ8fq6y2GE4NAHe0pnA4tCkWynjAxMCJgxtRn5GnMVmYWJS+A/IJ0bK2VQZ9K8/J ppv0zH5DSUjF1tXY+uynKxzLX6psqh91oxEH//SDpTA7G3yyCWK/uZhbBOQgnTq+ZI4g iX+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=752C77UYWYC/kCXAv9m+o+bKyybany3JU3TCXHGZR0U=; b=Yk6dEF5+CDlEBhOhsfyJ/EG/TbVpZyOx8PU7klmPQAp/QTqAWsXYV4PCesDMjIsXF/ 3xqR2PKvMyCoHhPm8xxS9smVX4/xQgg+OT2aJIjwW02mA+D97+C6V4lLD8aQxqCo59lh gF47ty29sSoOFrTcOLGjIgphYpJp+sSrKEdOUykcr6GPe5f4FjA+gYxjb5kq7ef6d2L1 qqyKvRKDibiLXrdFhAPpQXYgtii/AnJliX+g+cakXbWV3Jk1Ct2B+pst3r6EJAA11jo4 eCugkBgSfO46HgsggmvXuDRfirJzhnGcWrjxtdivEfx5KgN1ZKjVUK0rTyK7gSgwvkUJ 6qWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=mnWHipNR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o8-20020a056a0015c800b0056bb424088csi1322542pfu.212.2022.12.22.10.31.33; Thu, 22 Dec 2022 10:31:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=mnWHipNR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229904AbiLVSPg (ORCPT + 68 others); Thu, 22 Dec 2022 13:15:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230357AbiLVSPc (ORCPT ); Thu, 22 Dec 2022 13:15:32 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50AE11CB00 for ; Thu, 22 Dec 2022 10:15:29 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id u4-20020a17090a518400b00223f7eba2c4so2665146pjh.5 for ; Thu, 22 Dec 2022 10:15:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=752C77UYWYC/kCXAv9m+o+bKyybany3JU3TCXHGZR0U=; b=mnWHipNRICDDr9o9B9k9tzUHQ5DlJw7QNZU1NFFDGUJ/WFgmaKL6adljuLzxsRCR1T yOt1llvxRLddf3o8tFdD1XSrfvJKY91Y6BA+Ue/bxbYYHAufNWWHf+w2InX7LDglGQta 9K3BVA4lZMYr2iIsmJ+f1M3V5mNVtVOBrDe0He7TJ193jL7X5zGQlNAIk8two/do47Qa +IAmme0Ak9mW8lBakzH8LqHkhC04c8aYC5+Yadk9FKgybASwcKtR2kDtwpwTkfLtjN0F Pf78IoRu0kt98rlTKH1ZJdwA91Ht9u39VkBXmY2L8neh0zXKIViW79WjZdA8w13OMOlE OnLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=752C77UYWYC/kCXAv9m+o+bKyybany3JU3TCXHGZR0U=; b=wRX2o9DzHc5lg747wVvjKv+nhI5VuhNfxCy1us68cClAAzzqq2lexR7lTsnn67gtj9 ZD/z5Ln8/iq5NEijlBwhGgXVq2ldlwNhqKYRrAbU0uypxVHqtJLXZqHjPXYOlnd6+Rp2 GgYOrjdN+27dPsfegoxZVER1AMrVohRAr2s1Y7i6wDXFEEYX63NSF5hh8NdjjPx5qQ4U rV1H5XYEPBrIfoxOpqsjn6RF8Twpxq6xpLEEAaLfwE5wcGq0kHWDts7G1pM86tuiye6j /cM6UJDgT/0ts3u9ZnHVNyTNiOagFZ3RUPnKZC/AKG5pnMDDSue00U59bRQ8JGbA7kV8 96WQ== X-Gm-Message-State: AFqh2krhrlTNvyOqo8SLHT6eYipHsU17ZBm6bhJQBjYN3GC0DvNPfSfO uVAyzmDCD+Eosfe5FgetXrXVNQ== X-Received: by 2002:a17:903:41ca:b0:189:6624:58c0 with SMTP id u10-20020a17090341ca00b00189662458c0mr1174380ple.3.1671732928592; Thu, 22 Dec 2022 10:15:28 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id a7-20020a17090a70c700b00219feae9486sm3443216pjm.7.2022.12.22.10.15.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 10:15:28 -0800 (PST) Date: Thu, 22 Dec 2022 18:15:24 +0000 From: Sean Christopherson To: Chao Peng Cc: "Huang, Kai" , "tglx@linutronix.de" , "linux-arch@vger.kernel.org" , "kvm@vger.kernel.org" , "jmattson@google.com" , "Lutomirski, Andy" , "ak@linux.intel.com" , "kirill.shutemov@linux.intel.com" , "Hocko, Michal" , "qemu-devel@nongnu.org" , "tabba@google.com" , "david@redhat.com" , "michael.roth@amd.com" , "corbet@lwn.net" , "bfields@fieldses.org" , "dhildenb@redhat.com" , "linux-kernel@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "x86@kernel.org" , "bp@alien8.de" , "linux-api@vger.kernel.org" , "rppt@kernel.org" , "shuah@kernel.org" , "vkuznets@redhat.com" , "vbabka@suse.cz" , "mail@maciej.szmigiero.name" , "ddutile@redhat.com" , "qperret@google.com" , "arnd@arndb.de" , "pbonzini@redhat.com" , "vannapurve@google.com" , "naoya.horiguchi@nec.com" , "wanpengli@tencent.com" , "yu.c.zhang@linux.intel.com" , "hughd@google.com" , "aarcange@redhat.com" , "mingo@redhat.com" , "hpa@zytor.com" , "Nakajima, Jun" , "jlayton@kernel.org" , "joro@8bytes.org" , "linux-mm@kvack.org" , "Wang, Wei W" , "steven.price@arm.com" , "linux-doc@vger.kernel.org" , "Hansen, Dave" , "akpm@linux-foundation.org" , "linmiaohe@huawei.com" Subject: Re: [PATCH v10 1/9] mm: Introduce memfd_restricted system call to create restricted user memory Message-ID: References: <20221202061347.1070246-1-chao.p.peng@linux.intel.com> <20221202061347.1070246-2-chao.p.peng@linux.intel.com> <5c6e2e516f19b0a030eae9bf073d555c57ca1f21.camel@intel.com> <20221219075313.GB1691829@chaop.bj.intel.com> <20221220072228.GA1724933@chaop.bj.intel.com> <126046ce506df070d57e6fe5ab9c92cdaf4cf9b7.camel@intel.com> <20221221133905.GA1766136@chaop.bj.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221221133905.GA1766136@chaop.bj.intel.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 21, 2022, Chao Peng wrote: > On Tue, Dec 20, 2022 at 08:33:05AM +0000, Huang, Kai wrote: > > On Tue, 2022-12-20 at 15:22 +0800, Chao Peng wrote: > > > On Mon, Dec 19, 2022 at 08:48:10AM +0000, Huang, Kai wrote: > > > > On Mon, 2022-12-19 at 15:53 +0800, Chao Peng wrote: > > But for non-restricted-mem case, it is correct for KVM to decrease page's > > refcount after setting up mapping in the secondary mmu, otherwise the page will > > be pinned by KVM for normal VM (since KVM uses GUP to get the page). > > That's true. Actually even true for restrictedmem case, most likely we > will still need the kvm_release_pfn_clean() for KVM generic code. On one > side, other restrictedmem users like pKVM may not require page pinning > at all. On the other side, see below. > > > > > So what we are expecting is: for KVM if the page comes from restricted mem, then > > KVM cannot decrease the refcount, otherwise for normal page via GUP KVM should. No, requiring the user (KVM) to guard against lack of support for page migration in restricted mem is a terrible API. It's totally fine for restricted mem to not support page migration until there's a use case, but punting the problem to KVM is not acceptable. Restricted mem itself doesn't yet support page migration, e.g. explosions would occur even if KVM wanted to allow migration since there is no notification to invalidate existing mappings. > I argue that this page pinning (or page migration prevention) is not > tied to where the page comes from, instead related to how the page will > be used. Whether the page is restrictedmem backed or GUP() backed, once > it's used by current version of TDX then the page pinning is needed. So > such page migration prevention is really TDX thing, even not KVM generic > thing (that's why I think we don't need change the existing logic of > kvm_release_pfn_clean()). Wouldn't better to let TDX code (or who > requires that) to increase/decrease the refcount when it populates/drops > the secure EPT entries? This is exactly what the current TDX code does: I agree that whether or not migration is supported should be controllable by the user, but I strongly disagree on punting refcount management to KVM (or TDX). The whole point of restricted mem is to support technologies like TDX and SNP, accomodating their special needs for things like page migration should be part of the API, not some footnote in the documenation. It's not difficult to let the user communicate support for page migration, e.g. if/when restricted mem gains support, add a hook to restrictedmem_notifier_ops to signal support (or lack thereof) for page migration. NULL == no migration, non-NULL == migration allowed. We know that supporting page migration in TDX and SNP is possible, and we know that page migration will require a dedicated API since the backing store can't memcpy() the page. I don't see any reason to ignore that eventuality. But again, unless I'm missing something, that's a future problem because restricted mem doesn't yet support page migration regardless of the downstream user.