From patchwork Fri Nov 19 13:47:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Peng X-Patchwork-Id: 12628885 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E572BC433F5 for ; Fri, 19 Nov 2021 13:51:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9583961247 for ; Fri, 19 Nov 2021 13:51:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9583961247 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 79D886B007E; Fri, 19 Nov 2021 08:49:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 725E26B0080; Fri, 19 Nov 2021 08:49:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C7346B0081; Fri, 19 Nov 2021 08:49:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0185.hostedemail.com [216.40.44.185]) by kanga.kvack.org (Postfix) with ESMTP id 4F1E06B007E for ; Fri, 19 Nov 2021 08:49:20 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 04AB8182888CC for ; Fri, 19 Nov 2021 13:49:10 +0000 (UTC) X-FDA: 78825811260.04.43B3679 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by imf31.hostedemail.com (Postfix) with ESMTP id 2283C10529A5 for ; Fri, 19 Nov 2021 13:49:06 +0000 (UTC) X-IronPort-AV: E=McAfee;i="6200,9189,10172"; a="233134235" X-IronPort-AV: E=Sophos;i="5.87,247,1631602800"; d="scan'208";a="233134235" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Nov 2021 05:49:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.87,247,1631602800"; d="scan'208";a="507904885" Received: from chaop.bj.intel.com ([10.240.192.101]) by orsmga008.jf.intel.com with ESMTP; 19 Nov 2021 05:49:00 -0800 From: Chao Peng To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, qemu-devel@nongnu.org Cc: 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 , Chao Peng , "Kirill A . Shutemov" , luto@kernel.org, john.ji@intel.com, susie.li@intel.com, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Subject: [RFC v2 PATCH 04/13] KVM: Add fd-based memslot data structure and utils Date: Fri, 19 Nov 2021 21:47:30 +0800 Message-Id: <20211119134739.20218-5-chao.p.peng@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211119134739.20218-1-chao.p.peng@linux.intel.com> References: <20211119134739.20218-1-chao.p.peng@linux.intel.com> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 2283C10529A5 X-Stat-Signature: rkkt7fbr6t65w6q6egiborirx4mrumpz Authentication-Results: imf31.hostedemail.com; dkim=none; spf=none (imf31.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.120) smtp.mailfrom=chao.p.peng@linux.intel.com; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=intel.com (policy=none) X-HE-Tag: 1637329746-686437 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: For fd-based memslot store the file references for shared fd and the private fd (if any) in the memslot structure. Since there is no 'hva' concept we cannot call hva_to_pfn() to get a pfn, instead kvm_memfd_ops is added to get_pfn/put_pfn from the memory backing stores that provide these fds. Signed-off-by: Yu Zhang Signed-off-by: Chao Peng --- include/linux/kvm_host.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 99e9f9969703..1d4ac0c9b63b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -424,6 +424,12 @@ static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) */ #define KVM_MEM_MAX_NR_PAGES ((1UL << 31) - 1) +struct kvm_memfd_ops { + kvm_pfn_t (*get_pfn)(struct kvm_memory_slot *slot, struct file *file, + gfn_t gfn, bool alloc, int *order); + void (*put_pfn)(kvm_pfn_t pfn); +}; + struct kvm_memory_slot { gfn_t base_gfn; unsigned long npages; @@ -433,6 +439,9 @@ struct kvm_memory_slot { u32 flags; short id; u16 as_id; + struct file *file; + struct file *priv_file; + struct kvm_memfd_ops *memfd_ops; }; static inline bool kvm_slot_dirty_track_enabled(struct kvm_memory_slot *slot) @@ -1310,6 +1319,20 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn) return gfn_to_memslot(kvm, gfn)->id; } +static inline bool memslot_is_memfd(const struct kvm_memory_slot *slot) +{ + if (slot && slot->memfd_ops) + return true; + return false; +} + +static inline bool memslot_has_private(const struct kvm_memory_slot *slot) +{ + if (slot && slot->priv_file) + return true; + return false; +} + static inline gfn_t hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) {