Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2840947imm; Mon, 24 Sep 2018 10:46:56 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbZF2+k2V0KoYSoKh/XbSJulyDE/rY+vWqjFDstJq/aE7oIeohRQXcGQPpMoLymhAr9m7ft X-Received: by 2002:a62:54c7:: with SMTP id i190-v6mr11271377pfb.155.1537811216775; Mon, 24 Sep 2018 10:46:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537811216; cv=none; d=google.com; s=arc-20160816; b=Eq1ZP+DVMVT5M8mQXbGsIYX3jqKz21dYPRq7z1OjXtfFQZH36OgGCwbsIgmstDdH6Q 60ze4b23LKr/PVnyFQhSDrrsjC8cMQZJ0dR9xICo9NOhZEgTNNeVy6zFb0AkIGIEuvYR OCFQHkywp8LYDWrpb/2RUx3elK9Fm7u0T6ld4B5LnUsC76sde/8SjcdlYWNeWG/4yf+U 6WRxEUbITz1w92jOoNRrZp6+3nLX7FrTSwUdBqU+gaPgY24fCHNPZNMBKhZXRcMMcc0d r9CDojn71Gk6oHgPPD0ycEaaqQhJ0y0xa77R7rzteke1o6/mPs8O+d89l0/79W2agrCY MrTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=KpZ3k815IKzXxUoAN1enCMp8CexU9Ac9OZT12vLZ20Q=; b=VwS8RRhoXc008zKFUrw/suiGlxoUvdIJyO8idRBdi2n/08hE9aqdfPND+9n/YGfu8N MhM4wiVUj/K7yweJu/e5q42j5C/Vl29phikcl4bdPgtg8nbefnhydCXKI32Aa7b/AcyY tPzVbVpJbkysn4iP7IaMa1W3y3Z7Py3HHKoCXo2ibRA597ALaFROn31db6dHD8jGFkkp G6aZUzo5gxzXyXl1IyOohUccZ2tXL546/g8wQKZTFqj7nwnI2nIMIVbeU02PCT4yfb3s SY4+MhaKk41VR9s0jDLF6zHMoEw2Bu5AVcjPPpXeo9RBsBTKesHD04j5SE6L6BDA+g91 aCdw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n27-v6si5926745pgb.628.2018.09.24.10.46.41; Mon, 24 Sep 2018 10:46:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730136AbeIXXtg (ORCPT + 99 others); Mon, 24 Sep 2018 19:49:36 -0400 Received: from foss.arm.com ([217.140.101.70]:39704 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729619AbeIXXtg (ORCPT ); Mon, 24 Sep 2018 19:49:36 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0368315BE; Mon, 24 Sep 2018 10:46:20 -0700 (PDT) Received: from localhost (e105922-lin.Emea.Arm.com [10.4.13.28]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9BE2B3F5BD; Mon, 24 Sep 2018 10:46:19 -0700 (PDT) From: Punit Agrawal To: kvmarm@lists.cs.columbia.edu Cc: Punit Agrawal , marc.zyngier@arm.com, will.deacon@arm.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Christoffer Dall Subject: [PATCH v7 3/9] KVM: arm/arm64: Re-factor setting the Stage 2 entry to exec on fault Date: Mon, 24 Sep 2018 18:45:46 +0100 Message-Id: <20180924174552.8387-4-punit.agrawal@arm.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180924174552.8387-1-punit.agrawal@arm.com> References: <20180924174552.8387-1-punit.agrawal@arm.com> X-ARM-No-Footer: FoSSMail Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Stage 2 fault handler marks a page as executable if it is handling an execution fault or if it was a permission fault in which case the executable bit needs to be preserved. The logic to decide if the page should be marked executable is duplicated for PMD and PTE entries. To avoid creating another copy when support for PUD hugepages is introduced refactor the code to share the checks needed to mark a page table entry as executable. Signed-off-by: Punit Agrawal Reviewed-by: Suzuki K Poulose Cc: Christoffer Dall Cc: Marc Zyngier --- virt/kvm/arm/mmu.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c index 5b76ee204000..ec64d21c6571 100644 --- a/virt/kvm/arm/mmu.c +++ b/virt/kvm/arm/mmu.c @@ -1481,7 +1481,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, unsigned long fault_status) { int ret; - bool write_fault, exec_fault, writable, hugetlb = false, force_pte = false; + bool write_fault, writable, hugetlb = false, force_pte = false; + bool exec_fault, needs_exec; unsigned long mmu_seq; gfn_t gfn = fault_ipa >> PAGE_SHIFT; struct kvm *kvm = vcpu->kvm; @@ -1606,19 +1607,25 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, if (exec_fault) invalidate_icache_guest_page(pfn, vma_pagesize); + /* + * If we took an execution fault we have made the + * icache/dcache coherent above and should now let the s2 + * mapping be executable. + * + * Write faults (!exec_fault && FSC_PERM) are orthogonal to + * execute permissions, and we preserve whatever we have. + */ + needs_exec = exec_fault || + (fault_status == FSC_PERM && stage2_is_exec(kvm, fault_ipa)); + if (hugetlb && vma_pagesize == PMD_SIZE) { pmd_t new_pmd = pfn_pmd(pfn, mem_type); new_pmd = pmd_mkhuge(new_pmd); if (writable) new_pmd = kvm_s2pmd_mkwrite(new_pmd); - if (exec_fault) { + if (needs_exec) new_pmd = kvm_s2pmd_mkexec(new_pmd); - } else if (fault_status == FSC_PERM) { - /* Preserve execute if XN was already cleared */ - if (stage2_is_exec(kvm, fault_ipa)) - new_pmd = kvm_s2pmd_mkexec(new_pmd); - } ret = stage2_set_pmd_huge(kvm, memcache, fault_ipa, &new_pmd); } else { @@ -1629,13 +1636,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mark_page_dirty(kvm, gfn); } - if (exec_fault) { + if (needs_exec) new_pte = kvm_s2pte_mkexec(new_pte); - } else if (fault_status == FSC_PERM) { - /* Preserve execute if XN was already cleared */ - if (stage2_is_exec(kvm, fault_ipa)) - new_pte = kvm_s2pte_mkexec(new_pte); - } ret = stage2_set_pte(kvm, memcache, fault_ipa, &new_pte, flags); } -- 2.18.0