From patchwork Tue May 28 06:49:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963885 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 03B6D13AD for ; Tue, 28 May 2019 06:50:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E412828682 for ; Tue, 28 May 2019 06:49:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D734D286EE; Tue, 28 May 2019 06:49:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 793A028691 for ; Tue, 28 May 2019 06:49:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 880046B0278; Tue, 28 May 2019 02:49:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 854DD6B0276; Tue, 28 May 2019 02:49:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D0A86B027A; Tue, 28 May 2019 02:49:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id 278E36B0276 for ; Tue, 28 May 2019 02:49:57 -0400 (EDT) Received: by mail-pf1-f199.google.com with SMTP id k22so15000815pfg.18 for ; Mon, 27 May 2019 23:49:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:mime-version :content-transfer-encoding:message-id; bh=i6DWUGGg1HyuR/MCRfKvZTfnu5h+b1ZPXh8sDn/DfnM=; b=GI43/WUKYtMYUe6+MfOoHhpoCMDIt1/+C245LY3/+fuf/EppN/VXfdaNYaE+mX14fZ Soar6ua4GyxEohJLox+Tdgk0hHJkt9VbG4uSzR9sF8UEP4Va0487+1JuQcKzwAVmHsvc kmuPLW57ehh3VA5w+vHGCO1iYxb683vEiN++dtqIR9XjDgxql95r+fP64qByG9tfgML+ VHZB7R0GQxH8TWe/WjaV9SkFi9lJUXGf/DYbq+4NpnImf3/MkXv+U3rvLDFWwPS+ZmOl 2u7tQMZZsIObdCNQGaaQN9kvsNNM91cN8jlPtYBigzMhnxI/vHXxrJ5DvMl6bilakPHq SPWA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAXgrwr6c6enYW8XhMiClGD0TyOt2lRoC3juP2Lz8wmZR549uJR6 9dsQ1Y6ZeEiN7ap5ME/ZRC+6eH0MXHXG2wVcsp769kNdwqO0PM+3ZOrXQZ5xfrSclDSQOoRNbEr b6BPIsUfgRZG5JsPG7HN9M/bV79sWji/q6CWmvVTlyKMMN/RRpfK+CGjQAJ4IVqgyGQ== X-Received: by 2002:a62:bd0e:: with SMTP id a14mr121368780pff.44.1559026196775; Mon, 27 May 2019 23:49:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqxhWYtmeDpF4KHvrX/sZZRFiq0RWEArUVMBFtlDgfGJ+T1UN6k2orChXpVdOHaTjdLvNrmk X-Received: by 2002:a62:bd0e:: with SMTP id a14mr121368718pff.44.1559026195316; Mon, 27 May 2019 23:49:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026195; cv=none; d=google.com; s=arc-20160816; b=JPEl0EZK1OWtNtA6J6ic5ax6M7BM+xWPCmKDW4ahAjqJ1mdFGUHpyJMj/jSPYzR4Tv QY9gzlQ69mvbQMATxNJBZsG8qU5Gn7zASRQQVlM39i1WEHYqlECvEcM10h2ICLDvo3hK q5AjuLTvPMQDHXfe2pmaOkzqsylSAdjGvGHjI2tr8HqkoLe28YjLKbcf6XI43MtTcAR6 o4014JIQaqg80rW0a1r7BcL/YMXgDdIThH9RvEhBgpynWcNh5V4DuTI2P6LrvDy9pcsB koPOCMv35qbw3AAKWX/A+t5iUzvKNOp1Ubf0f7iKD8qHT/5KRjrvSEYNocEsmq4exVvS oLQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:content-transfer-encoding:mime-version:references :in-reply-to:date:subject:cc:to:from; bh=i6DWUGGg1HyuR/MCRfKvZTfnu5h+b1ZPXh8sDn/DfnM=; b=qKQhVnngq7M+lSyRNOLRnsPF30Ky2ld5Q0QRtioJTu3xahjxBH+3giI6tC5jGIomis RwrblB8HOjnL5BmNs6aDAYNxTFtcPoVKUXK5TD4s7KUeG7OUFbjZi4yZADEPEnlshyut ufnxAOdL4L6zBtBXZ/l6NDyNOZ4oEs9V31Cj2R4IxcTBr1dd8vVeWu1480klUy13NEyX eKhXE+b93+NmuIIaFV/h5xiWSYh7p46i1oHAmIQ48PPz0pKErg5iCPkhj1dkuN31yHfw J4/Nmoqj7j//a97z+A6BgoakBL+E6/u24eTPExXTHM9WDaI5a8h/k81lKC6H4gG2jgU4 WfCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com. [148.163.156.1]) by mx.google.com with ESMTPS id u123si12652716pgc.238.2019.05.27.23.49.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:49:55 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) client-ip=148.163.156.1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6btNF018799 for ; Tue, 28 May 2019 02:49:54 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0a-001b2d01.pphosted.com with ESMTP id 2srx3nkxmy-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:49:54 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:49:52 +0100 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:48 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6nkH853543116 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:46 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 782C911C052; Tue, 28 May 2019 06:49:46 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B779F11C04C; Tue, 28 May 2019 06:49:44 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:44 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [PATCH v4 1/6] kvmppc: HMM backend driver to manage pages of secure guest Date: Tue, 28 May 2019 12:19:28 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19052806-0028-0000-0000-0000037220B6 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0029-0000-0000-00002431DFA1 Message-Id: <20190528064933.23119-2-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP HMM driver for KVM PPC to manage page transitions of secure guest via H_SVM_PAGE_IN and H_SVM_PAGE_OUT hcalls. H_SVM_PAGE_IN: Move the content of a normal page to secure page H_SVM_PAGE_OUT: Move the content of a secure page to normal page Signed-off-by: Bharata B Rao --- arch/powerpc/include/asm/hvcall.h | 4 + arch/powerpc/include/asm/kvm_book3s_hmm.h | 29 ++ arch/powerpc/include/asm/kvm_host.h | 14 + arch/powerpc/include/asm/ultravisor-api.h | 2 + arch/powerpc/include/asm/ultravisor.h | 17 + arch/powerpc/kvm/Makefile | 3 + arch/powerpc/kvm/book3s_hv.c | 20 + arch/powerpc/kvm/book3s_hv_hmm.c | 474 ++++++++++++++++++++++ 8 files changed, 563 insertions(+) create mode 100644 arch/powerpc/include/asm/kvm_book3s_hmm.h create mode 100644 arch/powerpc/kvm/book3s_hv_hmm.c diff --git a/arch/powerpc/include/asm/hvcall.h b/arch/powerpc/include/asm/hvcall.h index 463c63a9fcf1..2f6b952deb0f 100644 --- a/arch/powerpc/include/asm/hvcall.h +++ b/arch/powerpc/include/asm/hvcall.h @@ -337,6 +337,10 @@ #define H_TLB_INVALIDATE 0xF808 #define H_COPY_TOFROM_GUEST 0xF80C +/* Platform-specific hcalls used by the Ultravisor */ +#define H_SVM_PAGE_IN 0xEF00 +#define H_SVM_PAGE_OUT 0xEF04 + /* Values for 2nd argument to H_SET_MODE */ #define H_SET_MODE_RESOURCE_SET_CIABR 1 #define H_SET_MODE_RESOURCE_SET_DAWR 2 diff --git a/arch/powerpc/include/asm/kvm_book3s_hmm.h b/arch/powerpc/include/asm/kvm_book3s_hmm.h new file mode 100644 index 000000000000..21f3de5f2acb --- /dev/null +++ b/arch/powerpc/include/asm/kvm_book3s_hmm.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __POWERPC_KVM_PPC_HMM_H__ +#define __POWERPC_KVM_PPC_HMM_H__ + +#ifdef CONFIG_PPC_UV +extern unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, + unsigned long gra, + unsigned long flags, + unsigned long page_shift); +extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm, + unsigned long gra, + unsigned long flags, + unsigned long page_shift); +#else +static inline unsigned long +kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gra, + unsigned long flags, unsigned long page_shift) +{ + return H_UNSUPPORTED; +} + +static inline unsigned long +kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gra, + unsigned long flags, unsigned long page_shift) +{ + return H_UNSUPPORTED; +} +#endif /* CONFIG_PPC_UV */ +#endif /* __POWERPC_KVM_PPC_HMM_H__ */ diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 184becb62ea4..c0c9c3455ac4 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -858,4 +858,18 @@ static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {} static inline void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) {} static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} +#ifdef CONFIG_PPC_UV +extern int kvmppc_hmm_init(void); +extern void kvmppc_hmm_free(void); +extern void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free); +#else +static inline int kvmppc_hmm_init(void) +{ + return 0; +} + +static inline void kvmppc_hmm_free(void) {} +static inline void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free) {} +#endif /* CONFIG_PPC_UV */ + #endif /* __POWERPC_KVM_HOST_H__ */ diff --git a/arch/powerpc/include/asm/ultravisor-api.h b/arch/powerpc/include/asm/ultravisor-api.h index 15e6ce77a131..51c4e0b5d197 100644 --- a/arch/powerpc/include/asm/ultravisor-api.h +++ b/arch/powerpc/include/asm/ultravisor-api.h @@ -20,5 +20,7 @@ /* opcodes */ #define UV_WRITE_PATE 0xF104 #define UV_RETURN 0xF11C +#define UV_PAGE_IN 0xF128 +#define UV_PAGE_OUT 0xF12C #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */ diff --git a/arch/powerpc/include/asm/ultravisor.h b/arch/powerpc/include/asm/ultravisor.h index 4ffec7a36acd..1e4c51799b43 100644 --- a/arch/powerpc/include/asm/ultravisor.h +++ b/arch/powerpc/include/asm/ultravisor.h @@ -44,6 +44,23 @@ static inline int uv_register_pate(u64 lpid, u64 dw0, u64 dw1) return ucall(UV_WRITE_PATE, retbuf, lpid, dw0, dw1); } +static inline int uv_page_in(u64 lpid, u64 src_ra, u64 dst_gpa, u64 flags, + u64 page_shift) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_PAGE_IN, retbuf, lpid, src_ra, dst_gpa, flags, + page_shift); +} + +static inline int uv_page_out(u64 lpid, u64 dst_ra, u64 src_gpa, u64 flags, + u64 page_shift) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_PAGE_OUT, retbuf, lpid, dst_ra, src_gpa, flags, + page_shift); +} #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_ULTRAVISOR_H */ diff --git a/arch/powerpc/kvm/Makefile b/arch/powerpc/kvm/Makefile index 4c67cc79de7c..d1a79fb5f806 100644 --- a/arch/powerpc/kvm/Makefile +++ b/arch/powerpc/kvm/Makefile @@ -71,6 +71,9 @@ kvm-hv-y += \ book3s_64_mmu_radix.o \ book3s_hv_nested.o +kvm-hv-$(CONFIG_PPC_UV) += \ + book3s_hv_hmm.o + kvm-hv-$(CONFIG_PPC_TRANSACTIONAL_MEM) += \ book3s_hv_tm.o diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 57ce45a997c7..2918616198de 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -76,6 +76,8 @@ #include #include #include +#include +#include #include "book3s.h" @@ -1084,6 +1086,18 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6)); break; + case H_SVM_PAGE_IN: + ret = kvmppc_h_svm_page_in(vcpu->kvm, + kvmppc_get_gpr(vcpu, 4), + kvmppc_get_gpr(vcpu, 5), + kvmppc_get_gpr(vcpu, 6)); + break; + case H_SVM_PAGE_OUT: + ret = kvmppc_h_svm_page_out(vcpu->kvm, + kvmppc_get_gpr(vcpu, 4), + kvmppc_get_gpr(vcpu, 5), + kvmppc_get_gpr(vcpu, 6)); + break; default: return RESUME_HOST; } @@ -4421,6 +4435,7 @@ static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, struct kvm_memory_slot *dont) { if (!dont || free->arch.rmap != dont->arch.rmap) { + kvmppc_hmm_release_pfns(free); vfree(free->arch.rmap); free->arch.rmap = NULL; } @@ -5494,11 +5509,16 @@ static int kvmppc_book3s_init_hv(void) no_mixing_hpt_and_radix = true; } + r = kvmppc_hmm_init(); + if (r < 0) + pr_err("KVM-HV: kvmppc_hmm_init failed %d\n", r); + return r; } static void kvmppc_book3s_exit_hv(void) { + kvmppc_hmm_free(); kvmppc_free_host_rm_ops(); if (kvmppc_radix_possible()) kvmppc_radix_exit(); diff --git a/arch/powerpc/kvm/book3s_hv_hmm.c b/arch/powerpc/kvm/book3s_hv_hmm.c new file mode 100644 index 000000000000..713806003da3 --- /dev/null +++ b/arch/powerpc/kvm/book3s_hv_hmm.c @@ -0,0 +1,474 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * HMM driver to manage page migration between normal and secure + * memory. + * + * Based on Jérôme Glisse's HMM dummy driver. + * + * Copyright 2018 Bharata B Rao, IBM Corp. + */ + +/* + * A pseries guest can be run as a secure guest on Ultravisor-enabled + * POWER platforms. On such platforms, this driver will be used to manage + * the movement of guest pages between the normal memory managed by + * hypervisor (HV) and secure memory managed by Ultravisor (UV). + * + * Private ZONE_DEVICE memory equal to the amount of secure memory + * available in the platform for running secure guests is created + * via a HMM device. The movement of pages between normal and secure + * memory is done by ->alloc_and_copy() callback routine of migrate_vma(). + * + * The page-in or page-out requests from UV will come to HV as hcalls and + * HV will call back into UV via uvcalls to satisfy these page requests. + * + * For each page that gets moved into secure memory, a HMM PFN is used + * on the HV side and HMM migration PTE corresponding to that PFN would be + * populated in the QEMU page tables. + */ + +#include +#include +#include +#include + +struct kvmppc_hmm_device { + struct hmm_device *device; + struct hmm_devmem *devmem; + unsigned long *pfn_bitmap; +}; + +static struct kvmppc_hmm_device kvmppc_hmm; +spinlock_t kvmppc_hmm_lock; + +struct kvmppc_hmm_page_pvt { + unsigned long *rmap; + unsigned int lpid; + unsigned long gpa; +}; + +struct kvmppc_hmm_migrate_args { + unsigned long *rmap; + unsigned int lpid; + unsigned long gpa; + unsigned long page_shift; +}; + +#define KVMPPC_PFN_HMM (0x1ULL << 61) + +static inline bool kvmppc_is_hmm_pfn(unsigned long pfn) +{ + return !!(pfn & KVMPPC_PFN_HMM); +} + +void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free) +{ + int i; + + for (i = 0; i < free->npages; i++) { + unsigned long *rmap = &free->arch.rmap[i]; + + if (kvmppc_is_hmm_pfn(*rmap)) + put_page(pfn_to_page(*rmap & ~KVMPPC_PFN_HMM)); + } +} + +/* + * Get a free HMM PFN from the pool + * + * Called when a normal page is moved to secure memory (UV_PAGE_IN). HMM + * PFN will be used to keep track of the secure page on HV side. + */ +/* + * TODO: In this and subsequent functions, we pass around and access + * individual elements of kvm_memory_slot->arch.rmap[] without any + * protection. Figure out the safe way to access this. + */ +static struct page *kvmppc_hmm_get_page(unsigned long *rmap, + unsigned long gpa, unsigned int lpid) +{ + struct page *dpage = NULL; + unsigned long bit, hmm_pfn; + unsigned long nr_pfns = kvmppc_hmm.devmem->pfn_last - + kvmppc_hmm.devmem->pfn_first; + unsigned long flags; + struct kvmppc_hmm_page_pvt *pvt; + + if (kvmppc_is_hmm_pfn(*rmap)) + return NULL; + + spin_lock_irqsave(&kvmppc_hmm_lock, flags); + bit = find_first_zero_bit(kvmppc_hmm.pfn_bitmap, nr_pfns); + if (bit >= nr_pfns) + goto out; + + bitmap_set(kvmppc_hmm.pfn_bitmap, bit, 1); + hmm_pfn = bit + kvmppc_hmm.devmem->pfn_first; + dpage = pfn_to_page(hmm_pfn); + + if (!trylock_page(dpage)) + goto out_clear; + + *rmap = hmm_pfn | KVMPPC_PFN_HMM; + pvt = kzalloc(sizeof(*pvt), GFP_ATOMIC); + if (!pvt) + goto out_unlock; + pvt->rmap = rmap; + pvt->gpa = gpa; + pvt->lpid = lpid; + hmm_devmem_page_set_drvdata(dpage, (unsigned long)pvt); + spin_unlock_irqrestore(&kvmppc_hmm_lock, flags); + + get_page(dpage); + return dpage; + +out_unlock: + unlock_page(dpage); +out_clear: + bitmap_clear(kvmppc_hmm.pfn_bitmap, + hmm_pfn - kvmppc_hmm.devmem->pfn_first, 1); +out: + spin_unlock_irqrestore(&kvmppc_hmm_lock, flags); + return NULL; +} + +/* + * Release the HMM PFN back to the pool + * + * Called when secure page becomes a normal page during UV_PAGE_OUT. + */ +static void kvmppc_hmm_put_page(struct page *page) +{ + unsigned long pfn = page_to_pfn(page); + unsigned long flags; + struct kvmppc_hmm_page_pvt *pvt; + + spin_lock_irqsave(&kvmppc_hmm_lock, flags); + pvt = (struct kvmppc_hmm_page_pvt *)hmm_devmem_page_get_drvdata(page); + hmm_devmem_page_set_drvdata(page, 0); + + bitmap_clear(kvmppc_hmm.pfn_bitmap, + pfn - kvmppc_hmm.devmem->pfn_first, 1); + *(pvt->rmap) = 0; + spin_unlock_irqrestore(&kvmppc_hmm_lock, flags); + kfree(pvt); +} + +/* + * migrate_vma() callback to move page from normal memory to secure memory. + * + * We don't capture the return value of uv_page_in() here because when + * UV asks for a page and then fails to copy it over, we don't care. + */ +static void +kvmppc_hmm_migrate_alloc_and_copy(struct vm_area_struct *vma, + const unsigned long *src_pfn, + unsigned long *dst_pfn, + unsigned long start, + unsigned long end, + void *private) +{ + struct kvmppc_hmm_migrate_args *args = private; + struct page *spage = migrate_pfn_to_page(*src_pfn); + unsigned long pfn = *src_pfn >> MIGRATE_PFN_SHIFT; + struct page *dpage; + + *dst_pfn = 0; + if (!(*src_pfn & MIGRATE_PFN_MIGRATE)) + return; + + dpage = kvmppc_hmm_get_page(args->rmap, args->gpa, args->lpid); + if (!dpage) + return; + + if (spage) + uv_page_in(args->lpid, pfn << args->page_shift, + args->gpa, 0, args->page_shift); + + *dst_pfn = migrate_pfn(page_to_pfn(dpage)) | + MIGRATE_PFN_DEVICE | MIGRATE_PFN_LOCKED; +} + +/* + * This migrate_vma() callback is typically used to updated device + * page tables after successful migration. We have nothing to do here. + * + * Also as we don't care if UV successfully copied over the page in + * kvmppc_hmm_migrate_alloc_and_copy(), we don't bother to check + * dst_pfn for any errors here. + */ +static void +kvmppc_hmm_migrate_finalize_and_map(struct vm_area_struct *vma, + const unsigned long *src_pfn, + const unsigned long *dst_pfn, + unsigned long start, + unsigned long end, + void *private) +{ +} + +static const struct migrate_vma_ops kvmppc_hmm_migrate_ops = { + .alloc_and_copy = kvmppc_hmm_migrate_alloc_and_copy, + .finalize_and_map = kvmppc_hmm_migrate_finalize_and_map, +}; + +/* + * Move page from normal memory to secure memory. + */ +unsigned long +kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa, + unsigned long flags, unsigned long page_shift) +{ + unsigned long addr, end; + unsigned long src_pfn, dst_pfn; + struct kvmppc_hmm_migrate_args args; + struct vm_area_struct *vma; + int srcu_idx; + unsigned long gfn = gpa >> page_shift; + struct kvm_memory_slot *slot; + unsigned long *rmap; + int ret = H_SUCCESS; + + if (page_shift != PAGE_SHIFT) + return H_P3; + + srcu_idx = srcu_read_lock(&kvm->srcu); + slot = gfn_to_memslot(kvm, gfn); + rmap = &slot->arch.rmap[gfn - slot->base_gfn]; + addr = gfn_to_hva(kvm, gpa >> page_shift); + srcu_read_unlock(&kvm->srcu, srcu_idx); + if (kvm_is_error_hva(addr)) + return H_PARAMETER; + + end = addr + (1UL << page_shift); + + if (flags) + return H_P2; + + args.rmap = rmap; + args.lpid = kvm->arch.lpid; + args.gpa = gpa; + args.page_shift = page_shift; + + down_read(&kvm->mm->mmap_sem); + vma = find_vma_intersection(kvm->mm, addr, end); + if (!vma || vma->vm_start > addr || vma->vm_end < end) { + ret = H_PARAMETER; + goto out; + } + ret = migrate_vma(&kvmppc_hmm_migrate_ops, vma, addr, end, + &src_pfn, &dst_pfn, &args); + if (ret < 0) + ret = H_PARAMETER; +out: + up_read(&kvm->mm->mmap_sem); + return ret; +} + +static void +kvmppc_hmm_fault_migrate_alloc_and_copy(struct vm_area_struct *vma, + const unsigned long *src_pfn, + unsigned long *dst_pfn, + unsigned long start, + unsigned long end, + void *private) +{ + struct page *dpage, *spage; + struct kvmppc_hmm_page_pvt *pvt; + unsigned long pfn; + int ret = U_SUCCESS; + + *dst_pfn = MIGRATE_PFN_ERROR; + spage = migrate_pfn_to_page(*src_pfn); + if (!spage || !(*src_pfn & MIGRATE_PFN_MIGRATE)) + return; + if (!is_zone_device_page(spage)) + return; + dpage = hmm_vma_alloc_locked_page(vma, start); + if (!dpage) + return; + pvt = (struct kvmppc_hmm_page_pvt *) + hmm_devmem_page_get_drvdata(spage); + + pfn = page_to_pfn(dpage); + ret = uv_page_out(pvt->lpid, pfn << PAGE_SHIFT, + pvt->gpa, 0, PAGE_SHIFT); + if (ret == U_SUCCESS) + *dst_pfn = migrate_pfn(pfn) | MIGRATE_PFN_LOCKED; +} + +/* + * This migrate_vma() callback is typically used to updated device + * page tables after successful migration. We have nothing to do here. + */ +static void +kvmppc_hmm_fault_migrate_finalize_and_map(struct vm_area_struct *vma, + const unsigned long *src_pfn, + const unsigned long *dst_pfn, + unsigned long start, + unsigned long end, + void *private) +{ +} + +static const struct migrate_vma_ops kvmppc_hmm_fault_migrate_ops = { + .alloc_and_copy = kvmppc_hmm_fault_migrate_alloc_and_copy, + .finalize_and_map = kvmppc_hmm_fault_migrate_finalize_and_map, +}; + +/* + * Fault handler callback when HV touches any page that has been + * moved to secure memory, we ask UV to give back the page by + * issuing a UV_PAGE_OUT uvcall. + */ +static vm_fault_t kvmppc_hmm_devmem_fault(struct hmm_devmem *devmem, + struct vm_area_struct *vma, + unsigned long addr, + const struct page *page, + unsigned int flags, + pmd_t *pmdp) +{ + unsigned long end = addr + PAGE_SIZE; + unsigned long src_pfn, dst_pfn = 0; + + if (migrate_vma(&kvmppc_hmm_fault_migrate_ops, vma, addr, end, + &src_pfn, &dst_pfn, NULL)) + return VM_FAULT_SIGBUS; + if (dst_pfn == MIGRATE_PFN_ERROR) + return VM_FAULT_SIGBUS; + return 0; +} + +static void kvmppc_hmm_devmem_free(struct hmm_devmem *devmem, + struct page *page) +{ + kvmppc_hmm_put_page(page); +} + +static const struct hmm_devmem_ops kvmppc_hmm_devmem_ops = { + .free = kvmppc_hmm_devmem_free, + .fault = kvmppc_hmm_devmem_fault, +}; + +/* + * Move page from secure memory to normal memory. + */ +unsigned long +kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa, + unsigned long flags, unsigned long page_shift) +{ + unsigned long addr, end; + struct vm_area_struct *vma; + unsigned long src_pfn, dst_pfn = 0; + int srcu_idx; + int ret = H_SUCCESS; + + if (page_shift != PAGE_SHIFT) + return H_P3; + + if (flags) + return H_P2; + + srcu_idx = srcu_read_lock(&kvm->srcu); + addr = gfn_to_hva(kvm, gpa >> page_shift); + srcu_read_unlock(&kvm->srcu, srcu_idx); + if (kvm_is_error_hva(addr)) + return H_PARAMETER; + + end = addr + (1UL << page_shift); + + down_read(&kvm->mm->mmap_sem); + vma = find_vma_intersection(kvm->mm, addr, end); + if (!vma || vma->vm_start > addr || vma->vm_end < end) { + ret = H_PARAMETER; + goto out; + } + ret = migrate_vma(&kvmppc_hmm_fault_migrate_ops, vma, addr, end, + &src_pfn, &dst_pfn, NULL); + if (ret < 0) + ret = H_PARAMETER; +out: + up_read(&kvm->mm->mmap_sem); + return ret; +} + +static u64 kvmppc_get_secmem_size(void) +{ + struct device_node *np; + int i, len; + const __be32 *prop; + u64 size = 0; + + np = of_find_node_by_path("/ibm,ultravisor/ibm,uv-firmware"); + if (!np) + goto out; + + prop = of_get_property(np, "secure-memory-ranges", &len); + if (!prop) + goto out_put; + + for (i = 0; i < len / (sizeof(*prop) * 4); i++) + size += of_read_number(prop + (i * 4) + 2, 2); + +out_put: + of_node_put(np); +out: + return size; +} + +static int kvmppc_hmm_pages_init(void) +{ + unsigned long nr_pfns = kvmppc_hmm.devmem->pfn_last - + kvmppc_hmm.devmem->pfn_first; + + kvmppc_hmm.pfn_bitmap = kcalloc(BITS_TO_LONGS(nr_pfns), + sizeof(unsigned long), GFP_KERNEL); + if (!kvmppc_hmm.pfn_bitmap) + return -ENOMEM; + + spin_lock_init(&kvmppc_hmm_lock); + + return 0; +} + +int kvmppc_hmm_init(void) +{ + int ret = 0; + unsigned long size; + + size = kvmppc_get_secmem_size(); + if (!size) { + ret = -ENODEV; + goto out; + } + + kvmppc_hmm.device = hmm_device_new(NULL); + if (IS_ERR(kvmppc_hmm.device)) { + ret = PTR_ERR(kvmppc_hmm.device); + goto out; + } + + kvmppc_hmm.devmem = hmm_devmem_add(&kvmppc_hmm_devmem_ops, + &kvmppc_hmm.device->device, size); + if (IS_ERR(kvmppc_hmm.devmem)) { + ret = PTR_ERR(kvmppc_hmm.devmem); + goto out_device; + } + ret = kvmppc_hmm_pages_init(); + if (ret < 0) + goto out_device; + + pr_info("KVMPPC-HMM: Secure Memory size %lx\n", size); + return ret; + +out_device: + hmm_device_put(kvmppc_hmm.device); +out: + return ret; +} + +void kvmppc_hmm_free(void) +{ + kfree(kvmppc_hmm.pfn_bitmap); + hmm_device_put(kvmppc_hmm.device); +} From patchwork Tue May 28 06:49:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963887 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B263813AD for ; Tue, 28 May 2019 06:50:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9EBCC28682 for ; Tue, 28 May 2019 06:50:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8E566286EE; Tue, 28 May 2019 06:50:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0084328682 for ; Tue, 28 May 2019 06:50:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD99A6B0276; Tue, 28 May 2019 02:49:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A7C1B6B027A; Tue, 28 May 2019 02:49:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 806406B027C; Tue, 28 May 2019 02:49:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 480EE6B0278 for ; Tue, 28 May 2019 02:49:57 -0400 (EDT) Received: by mail-pf1-f198.google.com with SMTP id 77so11391596pfu.1 for ; Mon, 27 May 2019 23:49:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:message-id; bh=B7JDsk73IdojO0N6gIey7LRtWsA77hCHFai+fTFI3Bs=; b=A5ql0rAOOgIQOl00E7RvK7UjIR0Bb1pXZeiMWlLhagCZHoUwH2jmBlfcOlxMn7V9Pe eF88snkiwOU/TKPFsbC2EVFEEwtvPIzkkI84o9Qq3lU5rBJcJ1kuVO99kA1FxU9+35wF GbnCnEWpkKmx0kFjkS1dWzyq1QOB61QzmKu/uCGNBrSyPYWP3qs9AHJaW2LyYJxm9ZdG tqtNdxJA0X/k+pZPOGopPWFNYV4K9RDq93y8zhZowgTyadx6LpLzsze2k+W1aPlno5Rk zPv9d7MYd+nr1POoZfcXgcqY1ssNun/rG/KsdSG8PeClN2nly10+O57L4KfSFXSNn5V0 WGZA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAWs8wUVzH+lHmwqbLWyF1uHP8PvP259cuvzUo4ZP3BHEV40tZkj xgse93Y2tby+sI5w5/kkIoAe4UtEdDFP1+0xW7siJUunWCY/WW5NHJrkj9f++0chTYnuDVrlUQq jL3HOw5jJckrydY0J7rT3Nv+LL7Mysv3EWr1PvmI2dgvYnqCAZMBcjW9vjjzuuN0nvg== X-Received: by 2002:a17:902:e40f:: with SMTP id ci15mr135710154plb.280.1559026196889; Mon, 27 May 2019 23:49:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwDQ7aSQLv0mVVAQ0OVRDBQKG/YQlHRhyH08EYCOjA5kgXWCZIBKmMs47Tj4EA18moLIRCS X-Received: by 2002:a17:902:e40f:: with SMTP id ci15mr135710096plb.280.1559026195698; Mon, 27 May 2019 23:49:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026195; cv=none; d=google.com; s=arc-20160816; b=s/LZuLlEUy4krNPMUJmZzXkAqX+YM4DwPqHD9qyKfjoTc4I+nUxeWo6yT6Dpkhp+u2 8X5xLInglXVxgPTRbR1B/4loDd8mG3Irzj5HBJVnyDNqZTfkEMXZNzVEfe8IyXEqMt5M ZzfUjZxQmLpcZwGyJRzK2jbJmuidYa97pQWwpE9dHKohltJnuLDqq7zzUkiQY+kjnmw/ JgZlBjT6A+aJ7+zNssDdukQjHsLzUiuMkHHnBvgZRSVMZj3+K1yxCqopTUfiAN/DKjYl KbqJC/6kKTC7BxrLJlUYLn7LiSt64BGCwX/5vK2CZ44sd0agGvp2gvdr+SvR7jW/YyTG 1D5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:references:in-reply-to:date:subject:cc:to:from; bh=B7JDsk73IdojO0N6gIey7LRtWsA77hCHFai+fTFI3Bs=; b=kgPuZ/SnP+m+5XyDaSOfyZoSU+53D8BVnq8J1E7oMIAtZ0btAeNDEZJm5xNu+EG0Qa zVRfP4iCbRc7fwuaeVw6ioRgP8DHu/SuAHiYWTQePlD+PPAHhMD15EBvxEwF5/IQcljf ruxsCcid02eaXctEVo8vU+FhA3Br06kaXRb7EXAO4cPchwD44HtgvgsGvvWF2gq4ArCG CVMEhcDuaHIcHgFXeZtl/CFot/h1HqUeXKfv4SonuNDi22jlTqpOy7m3P+kdylDOIRtZ GofkJis35sOt4LQjRCtkVVbD5mTh7MvdUdvZPui/iXImq6avHpGLW3SzRqpIp0gveh32 I3gw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com. [148.163.156.1]) by mx.google.com with ESMTPS id z19si23849532pfc.265.2019.05.27.23.49.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:49:55 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) client-ip=148.163.156.1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6burw098355 for ; Tue, 28 May 2019 02:49:55 -0400 Received: from e06smtp02.uk.ibm.com (e06smtp02.uk.ibm.com [195.75.94.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sry5bj0we-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:49:54 -0400 Received: from localhost by e06smtp02.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:49:52 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp02.uk.ibm.com (192.168.101.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:50 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6nmuI54657230 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:48 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 731DF11C04A; Tue, 28 May 2019 06:49:48 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BBBBD11C04C; Tue, 28 May 2019 06:49:46 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:46 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [PATCH v4 2/6] kvmppc: Shared pages support for secure guests Date: Tue, 28 May 2019 12:19:29 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19052806-0008-0000-0000-000002EB1A71 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0009-0000-0000-00002257E5D2 Message-Id: <20190528064933.23119-3-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=776 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP A secure guest will share some of its pages with hypervisor (Eg. virtio bounce buffers etc). Support shared pages in HMM driver. Signed-off-by: Bharata B Rao --- arch/powerpc/include/asm/hvcall.h | 3 ++ arch/powerpc/kvm/book3s_hv_hmm.c | 58 +++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/arch/powerpc/include/asm/hvcall.h b/arch/powerpc/include/asm/hvcall.h index 2f6b952deb0f..05b8536f6653 100644 --- a/arch/powerpc/include/asm/hvcall.h +++ b/arch/powerpc/include/asm/hvcall.h @@ -337,6 +337,9 @@ #define H_TLB_INVALIDATE 0xF808 #define H_COPY_TOFROM_GUEST 0xF80C +/* Flags for H_SVM_PAGE_IN */ +#define H_PAGE_IN_SHARED 0x1 + /* Platform-specific hcalls used by the Ultravisor */ #define H_SVM_PAGE_IN 0xEF00 #define H_SVM_PAGE_OUT 0xEF04 diff --git a/arch/powerpc/kvm/book3s_hv_hmm.c b/arch/powerpc/kvm/book3s_hv_hmm.c index 713806003da3..333829682f59 100644 --- a/arch/powerpc/kvm/book3s_hv_hmm.c +++ b/arch/powerpc/kvm/book3s_hv_hmm.c @@ -45,6 +45,7 @@ struct kvmppc_hmm_page_pvt { unsigned long *rmap; unsigned int lpid; unsigned long gpa; + bool skip_page_out; }; struct kvmppc_hmm_migrate_args { @@ -212,6 +213,45 @@ static const struct migrate_vma_ops kvmppc_hmm_migrate_ops = { .finalize_and_map = kvmppc_hmm_migrate_finalize_and_map, }; +/* + * Shares the page with HV, thus making it a normal page. + * + * - If the page is already secure, then provision a new page and share + * - If the page is a normal page, share the existing page + * + * In the former case, uses the HMM fault handler to release the HMM page. + */ +static unsigned long +kvmppc_share_page(struct kvm *kvm, unsigned long *rmap, unsigned long gpa, + unsigned long addr, unsigned long page_shift) +{ + + int ret; + unsigned int lpid = kvm->arch.lpid; + struct page *hmm_page; + struct kvmppc_hmm_page_pvt *pvt; + unsigned long pfn; + int srcu_idx; + + if (kvmppc_is_hmm_pfn(*rmap)) { + hmm_page = pfn_to_page(*rmap & ~KVMPPC_PFN_HMM); + pvt = (struct kvmppc_hmm_page_pvt *) + hmm_devmem_page_get_drvdata(hmm_page); + pvt->skip_page_out = true; + } + + srcu_idx = srcu_read_lock(&kvm->srcu); + pfn = gfn_to_pfn(kvm, gpa >> page_shift); + srcu_read_unlock(&kvm->srcu, srcu_idx); + if (is_error_noslot_pfn(pfn)) + return H_PARAMETER; + + ret = uv_page_in(lpid, pfn << page_shift, gpa, 0, page_shift); + kvm_release_pfn_clean(pfn); + + return (ret == U_SUCCESS) ? H_SUCCESS : H_PARAMETER; +} + /* * Move page from normal memory to secure memory. */ @@ -242,9 +282,12 @@ kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa, end = addr + (1UL << page_shift); - if (flags) + if (flags & ~H_PAGE_IN_SHARED) return H_P2; + if (flags & H_PAGE_IN_SHARED) + return kvmppc_share_page(kvm, rmap, gpa, addr, page_shift); + args.rmap = rmap; args.lpid = kvm->arch.lpid; args.gpa = gpa; @@ -291,8 +334,17 @@ kvmppc_hmm_fault_migrate_alloc_and_copy(struct vm_area_struct *vma, hmm_devmem_page_get_drvdata(spage); pfn = page_to_pfn(dpage); - ret = uv_page_out(pvt->lpid, pfn << PAGE_SHIFT, - pvt->gpa, 0, PAGE_SHIFT); + + /* + * This same alloc_and_copy() callback is used in two cases: + * - When HV touches a secure page, for which we do page-out + * - When a secure page is converted to shared page, we touch + * the page to essentially discard the HMM page. In this case we + * skip page-out. + */ + if (!pvt->skip_page_out) + ret = uv_page_out(pvt->lpid, pfn << PAGE_SHIFT, + pvt->gpa, 0, PAGE_SHIFT); if (ret == U_SUCCESS) *dst_pfn = migrate_pfn(pfn) | MIGRATE_PFN_LOCKED; } From patchwork Tue May 28 06:49:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963891 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4BD0314C0 for ; Tue, 28 May 2019 06:50:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3B38128682 for ; Tue, 28 May 2019 06:50:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E617286EE; Tue, 28 May 2019 06:50:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7407A28682 for ; Tue, 28 May 2019 06:50:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12F776B027F; Tue, 28 May 2019 02:49:59 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id F35A86B027A; Tue, 28 May 2019 02:49:58 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CECB06B027F; Tue, 28 May 2019 02:49:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yw1-f69.google.com (mail-yw1-f69.google.com [209.85.161.69]) by kanga.kvack.org (Postfix) with ESMTP id A5C336B0279 for ; Tue, 28 May 2019 02:49:58 -0400 (EDT) Received: by mail-yw1-f69.google.com with SMTP id j127so17160398ywd.0 for ; Mon, 27 May 2019 23:49:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:message-id; bh=GlvOpkagGXZ9gtQl9pvZlhJubi4axmFCfg0lI+KwUAc=; b=SRS1+Qxz+aiVxaRW274bQJj1ZeXD2BM00ht2kWl6zh6aFW4Wdsje4cnnPXwNarw8WC g3uiPjjVpql3HJa92B2emH2QrE+pc1G5uK/N8212KjD+Tqtw3QYwV/TYR4wWyEo6hXhG uKozCzr3dKYQbWiTBlPbPZjtMd7BUGvMcZTfrfDTT1ctPC/3TSSRL3OADHOT8WFYv4T6 NawpglMT4Bbkls0k1Gw9L9gVGqpg8bjnxPBaGTpeODVGwcTn1EBKovkqBoqYjuUZ3GEJ itWWCRLRPFATiZprYiLNjeoEP+9AfvDFppQpzfSCNY+ykDYMwQOu55tg1ewCOnPCU/lc LpzA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAUbk1Qq+9DF2fvbNB+VAJcrMtfiyRye9IjOGY9u2rp7+g/32eBI 0H50Ll3lxCjWGSnIEkk60K63LW1fYVIuxxft2+i7MaqFUs05+HphAYBMk2BenBOJrti0BtHSe6W nTKicd+pmyGMJ9Zd9ZQHcZWlIwzBnbLQGx+8zVZaMYbk8w/lgTJ5JKTkUPTnAUIYzRw== X-Received: by 2002:a81:3685:: with SMTP id d127mr10806525ywa.359.1559026198401; Mon, 27 May 2019 23:49:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqwS9WhflKdA84XWffy4Fq0K2KBiXdSHiH7OjeCVlbN00p89ZUuNkGvdzylUanlUFdO7kK6T X-Received: by 2002:a81:3685:: with SMTP id d127mr10806494ywa.359.1559026197219; Mon, 27 May 2019 23:49:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026197; cv=none; d=google.com; s=arc-20160816; b=IhydV1fbsohqJcDDkoYBUzLHzLXcmkX59dnjkRU4sYIpjRdjILbmGKEi0rutT/Vh5f pXQNwfujzS+/Gg0rHkgCe9cuteq7OAREKeckZsJzaMKV7003C+49UujOzfJR9mPToP2N 7c7VP0jbAOY1uwK/MPnTxdFsDeivb4BB7KdMxTaT0HHg32Bd0P42RoXIkrQKV08/0bgV 7zYF3FYlOgN/pam+FaPXP6IIA4igDCjlHpZBSQFETJZKQfhEo+ty3oqPIZEefPAOkOpK E89Lldx683GIKPUNSWJb/5upmg+9uIi2z8Sxx61wyIx5GpFZD7rW+HDr7w8kDuPtqvXc qB2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:references:in-reply-to:date:subject:cc:to:from; bh=GlvOpkagGXZ9gtQl9pvZlhJubi4axmFCfg0lI+KwUAc=; b=heKnjvqpD3J8wq6GyX0+8lENfr8/JFBpYEsTIbDrjxYgO8nWP+D6bskrtVaoiWliZV ReRZhyz4fBfl6kDrWqVZtToXLhWRXpU2z6ayyGM57fY7q5ygV5L/Wc3FC5w87AOV1FZd BMru2L4VI0AOgp5fjk9odbgtQw+eiRYPy/D2cmepzs54i5GlnMwSPWv4zsJz6xk8PoBF zfFF+fB4HSdxmS1m18JtwOQ+5HyH91Gn/7la+b3bTppOnCF2X58kIEOxgiC5Aqd2EPSl WJZAhAOHE5n213a2KudlffRwcLfoj7P63qr6NZGAZlHkQuIy4KUQPSd+T7vEs6kgtD/w w4UA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com. [148.163.158.5]) by mx.google.com with ESMTPS id n5si4128104ywf.219.2019.05.27.23.49.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:49:57 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) client-ip=148.163.158.5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6bupP066385 for ; Tue, 28 May 2019 02:49:56 -0400 Received: from e06smtp03.uk.ibm.com (e06smtp03.uk.ibm.com [195.75.94.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 2srxuy2ry6-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:49:56 -0400 Received: from localhost by e06smtp03.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:49:55 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp03.uk.ibm.com (192.168.101.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:52 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6noDp57671850 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:50 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6B4D211C052; Tue, 28 May 2019 06:49:50 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B503811C04A; Tue, 28 May 2019 06:49:48 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:48 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [PATCH v4 3/6] kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls Date: Tue, 28 May 2019 12:19:30 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19052806-0012-0000-0000-000003201D34 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0013-0000-0000-00002158E30A Message-Id: <20190528064933.23119-4-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=846 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP H_SVM_INIT_START: Initiate securing a VM H_SVM_INIT_DONE: Conclude securing a VM As part of H_SVM_INIT_START register all existing memslots with the UV. H_SVM_INIT_DONE call by UV informs HV that transition of the guest to secure mode is complete. Signed-off-by: Bharata B Rao Acked-by: Paul Mackerras --- arch/powerpc/include/asm/hvcall.h | 2 ++ arch/powerpc/include/asm/kvm_book3s_hmm.h | 12 ++++++++ arch/powerpc/include/asm/kvm_host.h | 4 +++ arch/powerpc/include/asm/ultravisor-api.h | 1 + arch/powerpc/include/asm/ultravisor.h | 9 ++++++ arch/powerpc/kvm/book3s_hv.c | 7 +++++ arch/powerpc/kvm/book3s_hv_hmm.c | 34 +++++++++++++++++++++++ 7 files changed, 69 insertions(+) diff --git a/arch/powerpc/include/asm/hvcall.h b/arch/powerpc/include/asm/hvcall.h index 05b8536f6653..fa7695928e30 100644 --- a/arch/powerpc/include/asm/hvcall.h +++ b/arch/powerpc/include/asm/hvcall.h @@ -343,6 +343,8 @@ /* Platform-specific hcalls used by the Ultravisor */ #define H_SVM_PAGE_IN 0xEF00 #define H_SVM_PAGE_OUT 0xEF04 +#define H_SVM_INIT_START 0xEF08 +#define H_SVM_INIT_DONE 0xEF0C /* Values for 2nd argument to H_SET_MODE */ #define H_SET_MODE_RESOURCE_SET_CIABR 1 diff --git a/arch/powerpc/include/asm/kvm_book3s_hmm.h b/arch/powerpc/include/asm/kvm_book3s_hmm.h index 21f3de5f2acb..3e13dab7f690 100644 --- a/arch/powerpc/include/asm/kvm_book3s_hmm.h +++ b/arch/powerpc/include/asm/kvm_book3s_hmm.h @@ -11,6 +11,8 @@ extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gra, unsigned long flags, unsigned long page_shift); +extern unsigned long kvmppc_h_svm_init_start(struct kvm *kvm); +extern unsigned long kvmppc_h_svm_init_done(struct kvm *kvm); #else static inline unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gra, @@ -25,5 +27,15 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gra, { return H_UNSUPPORTED; } + +static inine unsigned long kvmppc_h_svm_init_start(struct kvm *kvm) +{ + return H_UNSUPPORTED; +} + +static inine unsigned long kvmppc_h_svm_init_done(struct kvm *kvm); +{ + return H_UNSUPPORTED; +} #endif /* CONFIG_PPC_UV */ #endif /* __POWERPC_KVM_PPC_HMM_H__ */ diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index c0c9c3455ac4..845fd2a73506 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -272,6 +272,10 @@ struct kvm_hpt_info { struct kvm_resize_hpt; +/* Flag values for kvm_arch.secure_guest */ +#define KVMPPC_SECURE_INIT_START 0x1 /* H_SVM_INIT_START has been called */ +#define KVMPPC_SECURE_INIT_DONE 0x2 /* H_SVM_INIT_DONE completed */ + struct kvm_arch { unsigned int lpid; unsigned int smt_mode; /* # vcpus per virtual core */ diff --git a/arch/powerpc/include/asm/ultravisor-api.h b/arch/powerpc/include/asm/ultravisor-api.h index 51c4e0b5d197..05b17f4351f4 100644 --- a/arch/powerpc/include/asm/ultravisor-api.h +++ b/arch/powerpc/include/asm/ultravisor-api.h @@ -20,6 +20,7 @@ /* opcodes */ #define UV_WRITE_PATE 0xF104 #define UV_RETURN 0xF11C +#define UV_REGISTER_MEM_SLOT 0xF120 #define UV_PAGE_IN 0xF128 #define UV_PAGE_OUT 0xF12C diff --git a/arch/powerpc/include/asm/ultravisor.h b/arch/powerpc/include/asm/ultravisor.h index 1e4c51799b43..9befa6fea8db 100644 --- a/arch/powerpc/include/asm/ultravisor.h +++ b/arch/powerpc/include/asm/ultravisor.h @@ -61,6 +61,15 @@ static inline int uv_page_out(u64 lpid, u64 dst_ra, u64 src_gpa, u64 flags, return ucall(UV_PAGE_OUT, retbuf, lpid, dst_ra, src_gpa, flags, page_shift); } + +static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size, + u64 flags, u64 slotid) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_REGISTER_MEM_SLOT, retbuf, lpid, start_gpa, + size, flags, slotid); +} #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_ULTRAVISOR_H */ diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 2918616198de..3683e517541f 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -1098,6 +1098,13 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) kvmppc_get_gpr(vcpu, 5), kvmppc_get_gpr(vcpu, 6)); break; + case H_SVM_INIT_START: + ret = kvmppc_h_svm_init_start(vcpu->kvm); + break; + case H_SVM_INIT_DONE: + ret = kvmppc_h_svm_init_done(vcpu->kvm); + break; + default: return RESUME_HOST; } diff --git a/arch/powerpc/kvm/book3s_hv_hmm.c b/arch/powerpc/kvm/book3s_hv_hmm.c index 333829682f59..18fec814401d 100644 --- a/arch/powerpc/kvm/book3s_hv_hmm.c +++ b/arch/powerpc/kvm/book3s_hv_hmm.c @@ -55,6 +55,40 @@ struct kvmppc_hmm_migrate_args { unsigned long page_shift; }; +unsigned long kvmppc_h_svm_init_start(struct kvm *kvm) +{ + struct kvm_memslots *slots; + struct kvm_memory_slot *memslot; + int ret = H_SUCCESS; + int srcu_idx; + + srcu_idx = srcu_read_lock(&kvm->srcu); + slots = kvm_memslots(kvm); + kvm_for_each_memslot(memslot, slots) { + ret = uv_register_mem_slot(kvm->arch.lpid, + memslot->base_gfn << PAGE_SHIFT, + memslot->npages * PAGE_SIZE, + 0, memslot->id); + if (ret < 0) { + ret = H_PARAMETER; + goto out; + } + } + kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_START; +out: + srcu_read_unlock(&kvm->srcu, srcu_idx); + return ret; +} + +unsigned long kvmppc_h_svm_init_done(struct kvm *kvm) +{ + if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)) + return H_UNSUPPORTED; + + kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE; + return H_SUCCESS; +} + #define KVMPPC_PFN_HMM (0x1ULL << 61) static inline bool kvmppc_is_hmm_pfn(unsigned long pfn) From patchwork Tue May 28 06:49:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963893 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7CA0D13AD for ; Tue, 28 May 2019 06:50:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CA3928682 for ; Tue, 28 May 2019 06:50:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 60F15286EE; Tue, 28 May 2019 06:50:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D908F28682 for ; Tue, 28 May 2019 06:50:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3424A6B027A; Tue, 28 May 2019 02:49:59 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 12E026B0279; Tue, 28 May 2019 02:49:59 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E49336B0281; Tue, 28 May 2019 02:49:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by kanga.kvack.org (Postfix) with ESMTP id AA7A66B027A for ; Tue, 28 May 2019 02:49:58 -0400 (EDT) Received: by mail-yb1-f198.google.com with SMTP id h143so4675942ybg.6 for ; Mon, 27 May 2019 23:49:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:message-id; bh=ni6Y8NNMk18EmIsu7mg+RFI57ZrHdKGoCeGMtguHjBQ=; b=FExJrGq60u6BXhBRKGRwify72oYU0q0tf3FTuX9qnoEBGo8Rdw2ZgsJZARDpTfdWHe gavtylR0y+N6m/drf3goC1kwsIxvI5YKWTdNETFGwYikZFWU5ia3Om9xFOuUtE1OB3bE HVCA7q7b6OKzGB75dPdTxuSWri/I9i/2cAuNpqlrnxoxgB7oRFdISakHzim2v6vvtNxQ 4UroqzIXMCmb5k9sFrWrctQNpX5MTe0GMp1cOf20S00+CIqjNRzbjA2kgk0yoAgkWiqh j35RHLo/qSx3n63nSWMZRmpxvsaoFydAssjb+GMB30mYahKcqL8WijDUTQC0ceK18its ZVWg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAWxX9+8O/lCSkL08jioZwCmgvQ3t79OmhY6mnXITWVxonxOAVZ+ XmNcA/864nmoX1HWiiV9lpS8lOc50QvuGvQNPu/YdeO9Q71kC9eCX/QWBG5Mpn8zqgtLX4JB7+K HSnR1BzCUn4suONJ9DVLqrRlZmimJ5QbLDYC4QCTrkGoyyMJ0PcAplIwE2SmiSUCilQ== X-Received: by 2002:a25:8803:: with SMTP id c3mr37072603ybl.173.1559026198436; Mon, 27 May 2019 23:49:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzhyUdiEFiYbb3YdywoCId60lF9sLVTUgqZbmPtRPXbQDuqGRyZkNuTEt1FBNruRaSMQ18e X-Received: by 2002:a25:8803:: with SMTP id c3mr37072580ybl.173.1559026197439; Mon, 27 May 2019 23:49:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026197; cv=none; d=google.com; s=arc-20160816; b=wrV1R5kkRjXoskVvCqI4s6cZr9umeRa62JB8mNODLGweMdmh3ke+o7CgzhVVBIPFap /cV1OmCcPYkWkAipTiPCv2GTP6+ZcW12GjfS4e0I15uBOTmP4byrQ5n1z6Rs50BHIs8b J4AqCdE+DLEXYWSL2uAxoimdY6pGBbSJyyi5LR+s6dCTDrZjeoKBTL1TjvsHUXVxigLU uCuJK4+5cugS8yFNoDJoE2ZsLt6jMh90yn7KMTEXjFew85BYgOAKn/Koq65qjOnS06XM Jbx+8ZTq6rL/H5P7oGDSbx8Ln0V4UkWh46zPvVNxpcgJAXpw75dkwfgTIV5ab1P8TxsO UZ5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:references:in-reply-to:date:subject:cc:to:from; bh=ni6Y8NNMk18EmIsu7mg+RFI57ZrHdKGoCeGMtguHjBQ=; b=t20oNkX+bSKAzyDf3rUL73LVFNnCt0VpL4p8c1OPyO2CRGlWg37InerUy/t2mwRzil oPQCCL2g4ucPNeZPsTIHPmZ4iUK9XlaKjKlk3tOJ6v7/gZ+P4uYRxFe1vTk+JKR6rbne eBTkGR5Von6BnToLKxL0UGkb81gQfJHaIbEO5w7A53QaL7ZEBp9y3OiTzf6JnDHr4cOy vJ++aK8LJBjg+DwDoMzNtjX1O8c2qO/tOPDdxynHE5WNpHdkFaOaB0OKLG0bubQW43Qz 42g4xXv4hTGQuvuPRHK4fhdmximoO8FfA01x0wsazc623UL1/z9xTWL24YZrZeP75LDX 8YOQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com. [148.163.158.5]) by mx.google.com with ESMTPS id 84si2427912ybl.321.2019.05.27.23.49.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:49:57 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) client-ip=148.163.158.5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6cgIu078404 for ; Tue, 28 May 2019 02:49:57 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0a-001b2d01.pphosted.com with ESMTP id 2srxxjtahn-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:49:56 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:49:55 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:54 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6nq5A20774936 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:52 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6124D11C058; Tue, 28 May 2019 06:49:52 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B4D8211C050; Tue, 28 May 2019 06:49:50 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:50 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [PATCH v4 4/6] kvmppc: Handle memory plug/unplug to secure VM Date: Tue, 28 May 2019 12:19:31 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19052806-0028-0000-0000-0000037220BB X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0029-0000-0000-00002431DFA6 Message-Id: <20190528064933.23119-5-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=815 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP Register the new memslot with UV during plug and unregister the memslot during unplug. Signed-off-by: Bharata B Rao Acked-by: Paul Mackerras --- arch/powerpc/include/asm/ultravisor-api.h | 1 + arch/powerpc/include/asm/ultravisor.h | 7 +++++++ arch/powerpc/kvm/book3s_hv.c | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/arch/powerpc/include/asm/ultravisor-api.h b/arch/powerpc/include/asm/ultravisor-api.h index 05b17f4351f4..35b71e01177d 100644 --- a/arch/powerpc/include/asm/ultravisor-api.h +++ b/arch/powerpc/include/asm/ultravisor-api.h @@ -21,6 +21,7 @@ #define UV_WRITE_PATE 0xF104 #define UV_RETURN 0xF11C #define UV_REGISTER_MEM_SLOT 0xF120 +#define UV_UNREGISTER_MEM_SLOT 0xF124 #define UV_PAGE_IN 0xF128 #define UV_PAGE_OUT 0xF12C diff --git a/arch/powerpc/include/asm/ultravisor.h b/arch/powerpc/include/asm/ultravisor.h index 9befa6fea8db..5113457c4743 100644 --- a/arch/powerpc/include/asm/ultravisor.h +++ b/arch/powerpc/include/asm/ultravisor.h @@ -70,6 +70,13 @@ static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size, return ucall(UV_REGISTER_MEM_SLOT, retbuf, lpid, start_gpa, size, flags, slotid); } + +static inline int uv_unregister_mem_slot(u64 lpid, u64 slotid) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_UNREGISTER_MEM_SLOT, retbuf, lpid, slotid); +} #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_ULTRAVISOR_H */ diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 3683e517541f..5ef35e230453 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -78,6 +78,7 @@ #include #include #include +#include #include "book3s.h" @@ -4498,6 +4499,24 @@ static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) && ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES)) kvmppc_radix_flush_memslot(kvm, old); + /* + * If UV hasn't yet called H_SVM_INIT_START, don't register memslots. + */ + if (!kvm->arch.secure_guest) + return; + + /* + * TODO: Handle KVM_MR_MOVE + */ + if (change == KVM_MR_CREATE) { + uv_register_mem_slot(kvm->arch.lpid, + new->base_gfn << PAGE_SHIFT, + new->npages * PAGE_SIZE, + 0, + new->id); + } else if (change == KVM_MR_DELETE) { + uv_unregister_mem_slot(kvm->arch.lpid, old->id); + } } /* From patchwork Tue May 28 06:49:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963895 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3C93C13AD for ; Tue, 28 May 2019 06:50:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2AC1E28682 for ; Tue, 28 May 2019 06:50:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EE49286EE; Tue, 28 May 2019 06:50:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64ABE28682 for ; Tue, 28 May 2019 06:50:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A4436B027C; Tue, 28 May 2019 02:50:03 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0DE7A6B0281; Tue, 28 May 2019 02:50:03 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E70CC6B0283; Tue, 28 May 2019 02:50:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by kanga.kvack.org (Postfix) with ESMTP id BCA3B6B027C for ; Tue, 28 May 2019 02:50:02 -0400 (EDT) Received: by mail-yb1-f198.google.com with SMTP id k10so3841944ybd.14 for ; Mon, 27 May 2019 23:50:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:message-id; bh=B/1DQJluSOy/rrZ6cqxK2UTxrpnxCrmQcp+qzHtwDPI=; b=mcFQKS1VejSgPUg4nbpctbULkuFk4+reV55rHut3iC6EFfmzoV4ahaDdoRoCgtjFI5 DtK0Zyp+76uqTd15GBMnL1rWcDYjl/me2w/2YhVTtWuo3GIpxKlGESMiBZaJgjumE52S D/2eOimShuHrVMSxSS06p0FQpqyYpYAEi+XeS4ykVDAxlLLnyPZX7nl0cACy0wKN/F2Z muvvWeDgq+IHzvghL4De3yHlE/5RF+BaL9PJ21IPUjXtMudDIQY6LWBDgoWXlCu1XhTI r5w9Jgh1V0E3NHNQlDlIO4Fb6nc5IwpJF/8xBgXoPeCmDojszMX5h5e5ShPwCs7fTETU W6Fg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAXYyaWR9WzUjR1calM1UW+XFKxEy3DKfQ3Mr9y3JR+RogTr15DV wd3rqhdMLNfOEuIHEAPcI1xUiKoil1nkcSZdj/1aV/B1lR9LES5cIgPl+tsQEjqL0+n1ow8EsEP Y6dILjQBA5oPXhfakTFQsQaKRwOurwEffbW54EjRVo85NhIIVn6CDmHsyuIOdphaWBQ== X-Received: by 2002:a81:2817:: with SMTP id o23mr59659522ywo.395.1559026202485; Mon, 27 May 2019 23:50:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqy9oRFIspZHg4lZv/NOmevsDcG8tFQvJvv1JKBC5aEOzrmuGBd/6l+ZJB+lzozDDE1ALl6S X-Received: by 2002:a81:2817:: with SMTP id o23mr59659481ywo.395.1559026201273; Mon, 27 May 2019 23:50:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026201; cv=none; d=google.com; s=arc-20160816; b=AHePC/3H6tkVZT1u/21Yl06F2NdZwYxjBWwB4b2ERVhcNNE2ojXLtwWdqjsI9qsPKM bGAFqQhVgW8ZvS/LJM131MdXCp9Ui4GZBFN7JBxtoyOFocd2sNgga9+FGjX5NcOjWu5a QkUKIf6g72idQ22Cx5zG0uPtSBLOyZ+INfAOcWwdMwlbYgBwER8z78varrlu3H2/AHeo AQ9GaD9mEcbWNgbvCwJJqEXnPa+MoICiUgF3KKj6L2T4z2F6XiMmIrPPVb7OJHaot/8c wXC3/uary6rHyij666lBCQNEC7Vfn/p2B/gnCdfhmSqsDERqRbT2PIi4fKvnlfyKAC2t OyrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:references:in-reply-to:date:subject:cc:to:from; bh=B/1DQJluSOy/rrZ6cqxK2UTxrpnxCrmQcp+qzHtwDPI=; b=D9YANaV2MBDvZbFNjB0Z5IAxzLFoL1MEtrvQKkRa8XlQMiQDyKFJJ1tAyHGbMbdKKc vHd3+v6dpj3SWqgGrrwzOAGHxetYso2R58ukahLI2LagZBmY7caqCy9i5dwQBm3GoTCE 5zGG3jTIhOkczSKL9W0NsUYr/H4dJBanYYMZxGMt2VDSgKA9ovXGR2jqK70RJOfw+W+g 7a3k3U2f3LZOMFrL+uiD8ZmCP/FgUzUQJbCtap5MfjC//jvn/zDpFoV26hO9nrdz5UaO rDIwzfeVIxvxDKaLBzjKwgFhyap/PNTnFqly2alwbYcmKb4pNT23H/pFGAiuRZVCH4Mt MM0w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com. [148.163.158.5]) by mx.google.com with ESMTPS id k82si3018770ybb.440.2019.05.27.23.50.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:50:01 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) client-ip=148.163.158.5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6bupR066385 for ; Tue, 28 May 2019 02:50:01 -0400 Received: from e06smtp03.uk.ibm.com (e06smtp03.uk.ibm.com [195.75.94.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 2srxuy2s1r-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:50:00 -0400 Received: from localhost by e06smtp03.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:49:59 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp03.uk.ibm.com (192.168.101.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:56 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6nsuZ49217660 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:54 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6A4F411C04A; Tue, 28 May 2019 06:49:54 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AE4F811C050; Tue, 28 May 2019 06:49:52 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:52 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [RFC PATCH v4 5/6] kvmppc: Radix changes for secure guest Date: Tue, 28 May 2019 12:19:32 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19052806-0012-0000-0000-000003201D37 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0013-0000-0000-00002158E30C Message-Id: <20190528064933.23119-6-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP - After the guest becomes secure, when we handle a page fault of a page belonging to SVM in HV, send that page to UV via UV_PAGE_IN. - Whenever a page is unmapped on the HV side, inform UV via UV_PAGE_INVAL. Signed-off-by: Bharata B Rao --- arch/powerpc/include/asm/kvm_host.h | 13 +++++++++++++ arch/powerpc/include/asm/ultravisor-api.h | 1 + arch/powerpc/include/asm/ultravisor.h | 7 +++++++ arch/powerpc/kvm/book3s_64_mmu_radix.c | 19 +++++++++++++++++++ arch/powerpc/kvm/book3s_hv_hmm.c | 23 +++++++++++++++++++++++ 5 files changed, 63 insertions(+) diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 845fd2a73506..63d56f7a357e 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -866,6 +866,8 @@ static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} extern int kvmppc_hmm_init(void); extern void kvmppc_hmm_free(void); extern void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free); +extern bool kvmppc_is_guest_secure(struct kvm *kvm); +extern int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa); #else static inline int kvmppc_hmm_init(void) { @@ -874,6 +876,17 @@ static inline int kvmppc_hmm_init(void) static inline void kvmppc_hmm_free(void) {} static inline void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free) {} + + +static inline bool kvmppc_is_guest_secure(struct kvm *kvm) +{ + return false; +} + +static inline int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa) +{ + return -EFAULT; +} #endif /* CONFIG_PPC_UV */ #endif /* __POWERPC_KVM_HOST_H__ */ diff --git a/arch/powerpc/include/asm/ultravisor-api.h b/arch/powerpc/include/asm/ultravisor-api.h index 35b71e01177d..eaca65ea2070 100644 --- a/arch/powerpc/include/asm/ultravisor-api.h +++ b/arch/powerpc/include/asm/ultravisor-api.h @@ -24,5 +24,6 @@ #define UV_UNREGISTER_MEM_SLOT 0xF124 #define UV_PAGE_IN 0xF128 #define UV_PAGE_OUT 0xF12C +#define UV_PAGE_INVAL 0xF138 #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */ diff --git a/arch/powerpc/include/asm/ultravisor.h b/arch/powerpc/include/asm/ultravisor.h index 5113457c4743..28dbc0f0eddb 100644 --- a/arch/powerpc/include/asm/ultravisor.h +++ b/arch/powerpc/include/asm/ultravisor.h @@ -77,6 +77,13 @@ static inline int uv_unregister_mem_slot(u64 lpid, u64 slotid) return ucall(UV_UNREGISTER_MEM_SLOT, retbuf, lpid, slotid); } + +static inline int uv_page_inval(u64 lpid, u64 gpa, u64 page_shift) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_PAGE_INVAL, retbuf, lpid, gpa, page_shift); +} #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_ULTRAVISOR_H */ diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm/book3s_64_mmu_radix.c index f55ef071883f..e5d63449ad77 100644 --- a/arch/powerpc/kvm/book3s_64_mmu_radix.c +++ b/arch/powerpc/kvm/book3s_64_mmu_radix.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include /* * Supported radix tree geometry. @@ -923,6 +925,9 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu, if (!(dsisr & DSISR_PRTABLE_FAULT)) gpa |= ea & 0xfff; + if (kvmppc_is_guest_secure(kvm)) + return kvmppc_send_page_to_uv(kvm, gpa & PAGE_MASK); + /* Get the corresponding memslot */ memslot = gfn_to_memslot(kvm, gfn); @@ -980,6 +985,11 @@ int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, unsigned long gpa = gfn << PAGE_SHIFT; unsigned int shift; + if (kvmppc_is_guest_secure(kvm)) { + uv_page_inval(kvm->arch.lpid, gpa, PAGE_SIZE); + return 0; + } + ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift); if (ptep && pte_present(*ptep)) kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot, @@ -997,6 +1007,9 @@ int kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, int ref = 0; unsigned long old, *rmapp; + if (kvmppc_is_guest_secure(kvm)) + return ref; + ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift); if (ptep && pte_present(*ptep) && pte_young(*ptep)) { old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_ACCESSED, 0, @@ -1021,6 +1034,9 @@ int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, unsigned int shift; int ref = 0; + if (kvmppc_is_guest_secure(kvm)) + return ref; + ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift); if (ptep && pte_present(*ptep) && pte_young(*ptep)) ref = 1; @@ -1038,6 +1054,9 @@ static int kvm_radix_test_clear_dirty(struct kvm *kvm, int ret = 0; unsigned long old, *rmapp; + if (kvmppc_is_guest_secure(kvm)) + return ret; + ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift); if (ptep && pte_present(*ptep) && pte_dirty(*ptep)) { ret = 1; diff --git a/arch/powerpc/kvm/book3s_hv_hmm.c b/arch/powerpc/kvm/book3s_hv_hmm.c index 18fec814401d..a0d47fc140b6 100644 --- a/arch/powerpc/kvm/book3s_hv_hmm.c +++ b/arch/powerpc/kvm/book3s_hv_hmm.c @@ -55,6 +55,11 @@ struct kvmppc_hmm_migrate_args { unsigned long page_shift; }; +bool kvmppc_is_guest_secure(struct kvm *kvm) +{ + return !!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE); +} + unsigned long kvmppc_h_svm_init_start(struct kvm *kvm) { struct kvm_memslots *slots; @@ -478,6 +483,24 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa, return ret; } +int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa) +{ + int srcu_idx; + unsigned long pfn; + int ret; + + srcu_idx = srcu_read_lock(&kvm->srcu); + pfn = gfn_to_pfn(kvm, gpa >> PAGE_SHIFT); + srcu_read_unlock(&kvm->srcu, srcu_idx); + if (is_error_noslot_pfn(pfn)) + return -EFAULT; + + ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gpa, 0, PAGE_SHIFT); + kvm_release_pfn_clean(pfn); + + return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT; +} + static u64 kvmppc_get_secmem_size(void) { struct device_node *np; From patchwork Tue May 28 06:49:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bharata B Rao X-Patchwork-Id: 10963897 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1EFDF16C1 for ; Tue, 28 May 2019 06:50:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F0E9205F8 for ; Tue, 28 May 2019 06:50:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 02EE626530; Tue, 28 May 2019 06:50:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 122DB28691 for ; Tue, 28 May 2019 06:50:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91E806B0279; Tue, 28 May 2019 02:50:04 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 857476B0281; Tue, 28 May 2019 02:50:04 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 71EB86B0283; Tue, 28 May 2019 02:50:04 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-yb1-f200.google.com (mail-yb1-f200.google.com [209.85.219.200]) by kanga.kvack.org (Postfix) with ESMTP id 4FDFB6B0279 for ; Tue, 28 May 2019 02:50:04 -0400 (EDT) Received: by mail-yb1-f200.google.com with SMTP id e7so17461236ybk.22 for ; Mon, 27 May 2019 23:50:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:in-reply-to:references:message-id; bh=m2/So3fUO/pdqncvY4EBoM+imLujA4//BfO+ZYr3GqM=; b=hDTuk7L5ZhRaFnk0wPHMjYrdfKLCtGvzGqC1eYIhu7BZoQBoWJ8lPKF6UXSk3M0Qct WYvEEubIsIIK95uv+R9LBTwX0iKKCp0kuti7MrwMzKc8xnYQeYQ0SefH2eXEPEh/c0+Z RdYv9hbXVBP9+x6SCetFrFySshmNl8RyVGeyZdK8NIcOKqZX7O34rEqi+hvAcgNAIWUt 36d+yClMwnN7QeIHA9q+L0JDhrbHvo+og/nPuefqjI6pAyOMrUFIk8y7gsaEqsowXFKE t7OO/8+2M95IIHdcemfpprsTzGFjfzZkpL90ou7Dsa0vXpXD2x+USjiWHV5Z1OT6LuvO ZT0Q== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com X-Gm-Message-State: APjAAAUHlr6I4+Q6wvFaETLmzDYcwxDCJxtYmnzSljci5p6CTtLZ7cb3 XQQ42fDRyQFOOvj2K0BvyV5namZzDWTj0kcsA6xz5Eq39vneNVdYIwnX+Z33eVoNy8kHFMoMMBM o0nrvfjDnYMAIyVqpVLVFZrWQxzK5WkDAG/H7cEGonNLLPPHAHM0pRfm6GsKjVyQSjQ== X-Received: by 2002:a81:52d1:: with SMTP id g200mr59686014ywb.338.1559026204012; Mon, 27 May 2019 23:50:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqw42dQ3Nri2ltxX2pNfc5F0HBbSjAHjbvn/z9CvXez24AyhYlFOT6McCGeVl1rFfaGvdDmT X-Received: by 2002:a81:52d1:: with SMTP id g200mr59685972ywb.338.1559026202577; Mon, 27 May 2019 23:50:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559026202; cv=none; d=google.com; s=arc-20160816; b=WU1Pkab3MEeLxltIupbZ80FlbAGWYnqS7XWscNY1l4sEdEXXnZg7ag80QJGakX0+eB jJC7O1EHT2P/TXrXUhyoFwSE3v0C5Qs79kH9X2o8xVhQtWMon2A2S+Lz9S32Q301dSKE joBYsWjbSDrXUtUm2MPTzlUTzwIiYEiuCzCh/lIREEyb+bHOhZBTQZbFOFeLT04BA933 CvdJjARFP+ItIKKgbhzBKNXYq5iLpomvTFth6qA6MUslf6vYWk3HSRxTEGF3gvIuZw0w CBUKng8h+VdoWHIwYu+oY4doKW2nos3iOTEq5Y8nww0yAohfOenXLacR4bjG4j/ZD8PP ux6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:references:in-reply-to:date:subject:cc:to:from; bh=m2/So3fUO/pdqncvY4EBoM+imLujA4//BfO+ZYr3GqM=; b=fH0doyvYqde8z5L9gsLoAZoAfvE//X1qeAHK6ajWv3uk5cjr/YEpRRwMoTe1tSi6c4 fQRtGwpj4mZXARxzxD7BRpF5xY3DzqMmK4/Yp9ZKX4ZOlQvqvwYgCC0jxCs5nEkTl5KD N49LCV77zR69R3hseQKyd8Nfli5lcyEn2Fi9hMUhiYPrFuAqRHojOhQfCoPtn04Qun7/ rDNCZUZ560Ihh6hMCtLWhiA3imQzT/39E5R2nK1PiJPj8nQBIZcb2HW0pAwovm7Wr6or 3OMQmdu0DZpyVUyolJXY6FSsH1uK2fd/4l14SQd+h3qorAkJ6wQcEs+8c9HPP3aogZY9 hgGw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com. [148.163.158.5]) by mx.google.com with ESMTPS id h189si3815711ybh.209.2019.05.27.23.50.02 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 23:50:02 -0700 (PDT) Received-SPF: pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) client-ip=148.163.158.5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of bharata@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=bharata@linux.ibm.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4S6buOD195033 for ; Tue, 28 May 2019 02:50:02 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0b-001b2d01.pphosted.com with ESMTP id 2srwjt51mc-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 28 May 2019 02:50:02 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 28 May 2019 07:50:00 +0100 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 28 May 2019 07:49:58 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4S6nulm51445890 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 28 May 2019 06:49:56 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6C92C11C04C; Tue, 28 May 2019 06:49:56 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B5C2011C05B; Tue, 28 May 2019 06:49:54 +0000 (GMT) Received: from bharata.in.ibm.com (unknown [9.124.35.100]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 28 May 2019 06:49:54 +0000 (GMT) From: Bharata B Rao To: linuxppc-dev@lists.ozlabs.org Cc: kvm-ppc@vger.kernel.org, linux-mm@kvack.org, paulus@au1.ibm.com, aneesh.kumar@linux.vnet.ibm.com, jglisse@redhat.com, linuxram@us.ibm.com, sukadev@linux.vnet.ibm.com, cclaudio@linux.ibm.com, Bharata B Rao Subject: [RFC PATCH v4 6/6] kvmppc: Support reset of secure guest Date: Tue, 28 May 2019 12:19:33 +0530 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190528064933.23119-1-bharata@linux.ibm.com> References: <20190528064933.23119-1-bharata@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19052806-0028-0000-0000-0000037220BD X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19052806-0029-0000-0000-00002431DFA8 Message-Id: <20190528064933.23119-7-bharata@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-05-28_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905280045 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: X-Virus-Scanned: ClamAV using ClamSMTP Add support for reset of secure guest via a new ioctl KVM_PPC_SVM_OFF. This ioctl will be issued by QEMU during reset and in this ioctl, we ask UV to terminate the guest via UV_SVM_TERMINATE ucall, reinitialize guest's partitioned scoped page tables and release all HMM pages of the secure guest. After these steps, guest is ready to issue UV_ESM call once again to switch to secure mode. Signed-off-by: Bharata B Rao Signed-off-by: Sukadev Bhattiprolu [Implementation of uv_svm_terminate() and its call from guest shutdown path] --- arch/powerpc/include/asm/kvm_host.h | 6 ++ arch/powerpc/include/asm/kvm_ppc.h | 4 ++ arch/powerpc/include/asm/ultravisor-api.h | 1 + arch/powerpc/include/asm/ultravisor.h | 7 ++ arch/powerpc/kvm/book3s_hv.c | 23 +++++++ arch/powerpc/kvm/book3s_hv_hmm.c | 83 +++++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 12 ++++ include/uapi/linux/kvm.h | 1 + tools/include/uapi/linux/kvm.h | 1 + 9 files changed, 138 insertions(+) diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h index 63d56f7a357e..c220bcfe7726 100644 --- a/arch/powerpc/include/asm/kvm_host.h +++ b/arch/powerpc/include/asm/kvm_host.h @@ -868,6 +868,7 @@ extern void kvmppc_hmm_free(void); extern void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free); extern bool kvmppc_is_guest_secure(struct kvm *kvm); extern int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa); +extern int kvmppc_hmm_svm_off(struct kvm *kvm); #else static inline int kvmppc_hmm_init(void) { @@ -887,6 +888,11 @@ static inline int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa) { return -EFAULT; } + +static inline int kvmppc_hmm_svm_off(struct kvm *kvm) +{ + return 0; +} #endif /* CONFIG_PPC_UV */ #endif /* __POWERPC_KVM_HOST_H__ */ diff --git a/arch/powerpc/include/asm/kvm_ppc.h b/arch/powerpc/include/asm/kvm_ppc.h index bc892380e6cd..aee4b81e9558 100644 --- a/arch/powerpc/include/asm/kvm_ppc.h +++ b/arch/powerpc/include/asm/kvm_ppc.h @@ -188,6 +188,7 @@ extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm, extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm); extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm); extern void kvmppc_setup_partition_table(struct kvm *kvm); +extern int kvmppc_reinit_partition_table(struct kvm *kvm); extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, struct kvm_create_spapr_tce_64 *args); @@ -332,6 +333,7 @@ struct kvmppc_ops { int size); int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, int size); + int (*svm_off)(struct kvm *kvm); }; extern struct kvmppc_ops *kvmppc_hv_ops; @@ -961,5 +963,7 @@ static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb) } extern void xics_wake_cpu(int cpu); +extern void kvmppc_hmm_free_memslot_pfns(struct kvm *kvm, + struct kvm_memslots *slots); #endif /* __POWERPC_KVM_PPC_H__ */ diff --git a/arch/powerpc/include/asm/ultravisor-api.h b/arch/powerpc/include/asm/ultravisor-api.h index eaca65ea2070..6d59c64f30ce 100644 --- a/arch/powerpc/include/asm/ultravisor-api.h +++ b/arch/powerpc/include/asm/ultravisor-api.h @@ -25,5 +25,6 @@ #define UV_PAGE_IN 0xF128 #define UV_PAGE_OUT 0xF12C #define UV_PAGE_INVAL 0xF138 +#define UV_SVM_TERMINATE 0xF13C #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */ diff --git a/arch/powerpc/include/asm/ultravisor.h b/arch/powerpc/include/asm/ultravisor.h index 28dbc0f0eddb..1c60b7328f09 100644 --- a/arch/powerpc/include/asm/ultravisor.h +++ b/arch/powerpc/include/asm/ultravisor.h @@ -84,6 +84,13 @@ static inline int uv_page_inval(u64 lpid, u64 gpa, u64 page_shift) return ucall(UV_PAGE_INVAL, retbuf, lpid, gpa, page_shift); } + +static inline int uv_svm_terminate(u64 lpid) +{ + unsigned long retbuf[UCALL_BUFSIZE]; + + return ucall(UV_SVM_TERMINATE, retbuf, lpid); +} #endif /* !__ASSEMBLY__ */ #endif /* _ASM_POWERPC_ULTRAVISOR_H */ diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 5ef35e230453..4a423c92fb18 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -4571,6 +4571,22 @@ void kvmppc_setup_partition_table(struct kvm *kvm) kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1); } +/* + * Called from KVM_PPC_SVM_OFF ioctl at guest reset time when secure + * guest is converted back to normal guest. + */ +int kvmppc_reinit_partition_table(struct kvm *kvm) +{ + int ret; + + ret = kvmppc_init_vm_radix(kvm); + if (ret) + return ret; + + kvmppc_setup_partition_table(kvm); + return 0; +} + /* * Set up HPT (hashed page table) and RMA (real-mode area). * Must be called with kvm->lock held. @@ -4950,6 +4966,7 @@ static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) if (nesting_enabled(kvm)) kvmhv_release_all_nested(kvm); kvm->arch.process_table = 0; + uv_svm_terminate(kvm->arch.lpid); kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0); } kvmppc_free_lpid(kvm->arch.lpid); @@ -5391,6 +5408,11 @@ static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, return rc; } +static int kvmhv_svm_off(struct kvm *kvm) +{ + return kvmppc_hmm_svm_off(kvm); +} + static struct kvmppc_ops kvm_ops_hv = { .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, @@ -5433,6 +5455,7 @@ static struct kvmppc_ops kvm_ops_hv = { .enable_nested = kvmhv_enable_nested, .load_from_eaddr = kvmhv_load_from_eaddr, .store_to_eaddr = kvmhv_store_to_eaddr, + .svm_off = kvmhv_svm_off, }; static int kvm_init_subcore_bitmap(void) diff --git a/arch/powerpc/kvm/book3s_hv_hmm.c b/arch/powerpc/kvm/book3s_hv_hmm.c index a0d47fc140b6..c9ce8b003ef5 100644 --- a/arch/powerpc/kvm/book3s_hv_hmm.c +++ b/arch/powerpc/kvm/book3s_hv_hmm.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include struct kvmppc_hmm_device { struct hmm_device *device; @@ -60,6 +62,30 @@ bool kvmppc_is_guest_secure(struct kvm *kvm) return !!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE); } +int kvmppc_hmm_svm_off(struct kvm *kvm) +{ + int ret = 0; + int i; + + if (kvmppc_is_guest_secure(kvm)) { + ret = uv_svm_terminate(kvm->arch.lpid); + if (ret != U_SUCCESS) { + ret = -EINVAL; + goto out; + } + + ret = kvmppc_reinit_partition_table(kvm); + if (ret) + goto out; + kvm->arch.secure_guest = 0; + for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) + kvmppc_hmm_free_memslot_pfns(kvm, + __kvm_memslots(kvm, i)); + } +out: + return ret; +} + unsigned long kvmppc_h_svm_init_start(struct kvm *kvm) { struct kvm_memslots *slots; @@ -91,6 +117,11 @@ unsigned long kvmppc_h_svm_init_done(struct kvm *kvm) return H_UNSUPPORTED; kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE; + if (kvm_is_radix(kvm)) { + pr_info("Guest went secure, freeing HV side radix pgtables\n"); + kvmppc_free_radix(kvm); + } + return H_SUCCESS; } @@ -113,6 +144,58 @@ void kvmppc_hmm_release_pfns(struct kvm_memory_slot *free) } } +/* + * Drop HMM pages that we maintain for the secure guest + * + * We mark the pages to be skipped from UV_PAGE_OUT when there is HMM + * fault on these pages. Next we get these pages, force HMM fault, + * do fault migration to replace the HMM PTEs from QEMU page tables + * with normal PTEs from newly allocated pages. + */ +static void kvmppc_hmm_drop_pages(struct kvm_memory_slot *free, struct kvm *kvm) +{ + int i; + struct kvmppc_hmm_page_pvt *pvt; + unsigned long pfn; + + for (i = 0; i < free->npages; i++) { + unsigned long *rmap = &free->arch.rmap[i]; + struct page *hmm_page; + + if (kvmppc_is_hmm_pfn(*rmap)) { + hmm_page = pfn_to_page(*rmap & ~KVMPPC_PFN_HMM); + pvt = (struct kvmppc_hmm_page_pvt *) + hmm_devmem_page_get_drvdata(hmm_page); + pvt->skip_page_out = true; + + pfn = gfn_to_pfn(kvm, pvt->gpa >> PAGE_SHIFT); + if (is_error_noslot_pfn(pfn)) + continue; + kvm_release_pfn_clean(pfn); + } + } +} + +/* + * Called from KVM_PPC_SVM_OFF ioctl when secure guest is reset + * + * UV has already cleaned up the guest, we release any HMM pages that + * we maintain + */ +void kvmppc_hmm_free_memslot_pfns(struct kvm *kvm, struct kvm_memslots *slots) +{ + struct kvm_memory_slot *memslot; + int srcu_idx; + + if (!slots) + return; + + srcu_idx = srcu_read_lock(&kvm->srcu); + kvm_for_each_memslot(memslot, slots) + kvmppc_hmm_drop_pages(memslot, kvm); + srcu_read_unlock(&kvm->srcu, srcu_idx); +} + /* * Get a free HMM PFN from the pool * diff --git a/arch/powerpc/kvm/powerpc.c b/arch/powerpc/kvm/powerpc.c index 3393b166817a..0c5fae004adf 100644 --- a/arch/powerpc/kvm/powerpc.c +++ b/arch/powerpc/kvm/powerpc.c @@ -42,6 +42,8 @@ #include #include #endif +#include +#include #include "timing.h" #include "irq.h" @@ -2423,6 +2425,16 @@ long kvm_arch_vm_ioctl(struct file *filp, r = -EFAULT; break; } + case KVM_PPC_SVM_OFF: { + struct kvm *kvm = filp->private_data; + + r = 0; + if (!kvm->arch.kvm_ops->svm_off) + goto out; + + r = kvm->arch.kvm_ops->svm_off(kvm); + break; + } default: { struct kvm *kvm = filp->private_data; r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2fe12b40d503..c1dbdd428025 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1327,6 +1327,7 @@ struct kvm_s390_ucas_mapping { #define KVM_PPC_GET_RMMU_INFO _IOW(KVMIO, 0xb0, struct kvm_ppc_rmmu_info) /* Available with KVM_CAP_PPC_GET_CPU_CHAR */ #define KVM_PPC_GET_CPU_CHAR _IOR(KVMIO, 0xb1, struct kvm_ppc_cpu_char) +#define KVM_PPC_SVM_OFF _IO(KVMIO, 0xb2) /* ioctl for vm fd */ #define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device) diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 6d4ea4b6c922..03c89f4a01b2 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1319,6 +1319,7 @@ struct kvm_s390_ucas_mapping { #define KVM_PPC_GET_RMMU_INFO _IOW(KVMIO, 0xb0, struct kvm_ppc_rmmu_info) /* Available with KVM_CAP_PPC_GET_CPU_CHAR */ #define KVM_PPC_GET_CPU_CHAR _IOR(KVMIO, 0xb1, struct kvm_ppc_cpu_char) +#define KVM_PPC_SVM_OFF _IO(KVMIO, 0xb2) /* ioctl for vm fd */ #define KVM_CREATE_DEVICE _IOWR(KVMIO, 0xe0, struct kvm_create_device)