Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp864244pxy; Thu, 22 Apr 2021 15:38:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/RPhcN8W5swE0FU1UugkAJ2PMFfEIu3de0x/WTV1rog5xW2I7JLFF+fozsZnHB90jmYcK X-Received: by 2002:aa7:814c:0:b029:250:13db:3c6e with SMTP id d12-20020aa7814c0000b029025013db3c6emr872473pfn.65.1619131116329; Thu, 22 Apr 2021 15:38:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619131116; cv=none; d=google.com; s=arc-20160816; b=XxvBbroz5DfmHXnzkH13oGyaYgSP/dgH2CKy/6z57wZvzeNn5kqygRabCbo1Qn1yGo 9hsgz2+nRhUIyZbp0yL0CPc9FqF2+oftEYwWd3akXcn0fzP/5FGUK5qet3Zo5TqBi6ig xYKhgaKCfeHW71YRNrNN8tVqYjLCUGRTvI3repae/zcRBweZ90UW2LDkJeJlsRLsxQGB Lwx4PPIByfgNtprLO4rqOYHFHbfv0J7mkbCxSnDDMhGRhJlyWuapA6F9QyDoiuLSSkSF MinskhjMzP65uxOK99Vg4KGpQWEWdsMhpFH6eY2jZsB2Xcn9zheXZuPMrPw0DdTUSRPx DrkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:reply-to:dkim-signature; bh=FIN9E2GU73DP80qk98W7edTbeO39ckNz6/z6iV+ZVaE=; b=v4y9LXiRV9qB6oN881LWsSHjWV/8KXP76kZXoo04TteRWD7bLYJ5dsjDg4aFwQUb6w Yj02hERI9mRmOjE+mHYjBRy8R50WvfxryrT1VXNrky644sBrRpJXDOYHMlNQ9w5Es+fu Yzx4WSffIS+zzjVOaOR9rQ5geJLAC4elu/LPdkzfNEW0Y6JVXoA7pt9yebw3HPdQWLe/ OU97QA+O1BfsfEG8AYF8GJW0so0Hn3TdCtJvgehRcGnj7k3LM2X9mNHtVrBX/8uN978z Wo1GsC4Mp+8aQjQC02oNj7HjShrjCPKUZt9/lYWcT/crbw9UagYtnVV0IkLNRDRkoxX2 NRxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bVntay4j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p23si4844255pgk.180.2021.04.22.15.38.23; Thu, 22 Apr 2021 15:38:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bVntay4j; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239666AbhDVWhq (ORCPT + 99 others); Thu, 22 Apr 2021 18:37:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59157 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239650AbhDVWho (ORCPT ); Thu, 22 Apr 2021 18:37:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619131029; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FIN9E2GU73DP80qk98W7edTbeO39ckNz6/z6iV+ZVaE=; b=bVntay4jJX7gdN24/oNtstjQEB3YUpHzg5El+OkV1B39oqQ8tj9tAQPb+/5X/tBSVmscmY AlBS8DAFpXxClTWXyLwBy2FtnQfVJ7EuU+H8K+g2g6n+bAmAaRn+OoId/gfsnltliPzMIN DD2XVfrCXjv1qsNxT3NgEu2yJBV9kuY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-425-qt4M-qynM1mJku2DtY-EmA-1; Thu, 22 Apr 2021 18:37:05 -0400 X-MC-Unique: qt4M-qynM1mJku2DtY-EmA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E57C71927800; Thu, 22 Apr 2021 22:37:03 +0000 (UTC) Received: from [10.64.54.94] (vpn2-54-94.bne.redhat.com [10.64.54.94]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5F8195C3E9; Thu, 22 Apr 2021 22:37:00 +0000 (UTC) Reply-To: Gavin Shan Subject: Re: [PATCH v4 2/2] kvm/arm64: Try stage2 block mapping for host device MMIO To: Keqian Zhu , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Marc Zyngier Cc: wanghaibin.wang@huawei.com References: <20210415140328.24200-1-zhukeqian1@huawei.com> <20210415140328.24200-3-zhukeqian1@huawei.com> From: Gavin Shan Message-ID: Date: Fri, 23 Apr 2021 10:37:12 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.0 MIME-Version: 1.0 In-Reply-To: <20210415140328.24200-3-zhukeqian1@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/16/21 12:03 AM, Keqian Zhu wrote: > The MMIO region of a device maybe huge (GB level), try to use > block mapping in stage2 to speedup both map and unmap. > > Compared to normal memory mapping, we should consider two more > points when try block mapping for MMIO region: > > 1. For normal memory mapping, the PA(host physical address) and > HVA have same alignment within PUD_SIZE or PMD_SIZE when we use > the HVA to request hugepage, so we don't need to consider PA > alignment when verifing block mapping. But for device memory > mapping, the PA and HVA may have different alignment. > > 2. For normal memory mapping, we are sure hugepage size properly > fit into vma, so we don't check whether the mapping size exceeds > the boundary of vma. But for device memory mapping, we should pay > attention to this. > > This adds get_vma_page_shift() to get page shift for both normal > memory and device MMIO region, and check these two points when > selecting block mapping size for MMIO region. > > Signed-off-by: Keqian Zhu > --- > arch/arm64/kvm/mmu.c | 61 ++++++++++++++++++++++++++++++++++++-------- > 1 file changed, 51 insertions(+), 10 deletions(-) > Reviewed-by: Gavin Shan > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index c59af5ca01b0..5a1cc7751e6d 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -738,6 +738,35 @@ transparent_hugepage_adjust(struct kvm_memory_slot *memslot, > return PAGE_SIZE; > } > > +static int get_vma_page_shift(struct vm_area_struct *vma, unsigned long hva) > +{ > + unsigned long pa; > + > + if (is_vm_hugetlb_page(vma) && !(vma->vm_flags & VM_PFNMAP)) > + return huge_page_shift(hstate_vma(vma)); > + > + if (!(vma->vm_flags & VM_PFNMAP)) > + return PAGE_SHIFT; > + > + VM_BUG_ON(is_vm_hugetlb_page(vma)); > + > + pa = (vma->vm_pgoff << PAGE_SHIFT) + (hva - vma->vm_start); > + > +#ifndef __PAGETABLE_PMD_FOLDED > + if ((hva & (PUD_SIZE - 1)) == (pa & (PUD_SIZE - 1)) && > + ALIGN_DOWN(hva, PUD_SIZE) >= vma->vm_start && > + ALIGN(hva, PUD_SIZE) <= vma->vm_end) > + return PUD_SHIFT; > +#endif > + > + if ((hva & (PMD_SIZE - 1)) == (pa & (PMD_SIZE - 1)) && > + ALIGN_DOWN(hva, PMD_SIZE) >= vma->vm_start && > + ALIGN(hva, PMD_SIZE) <= vma->vm_end) > + return PMD_SHIFT; > + > + return PAGE_SHIFT; > +} > + > static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > struct kvm_memory_slot *memslot, unsigned long hva, > unsigned long fault_status) > @@ -769,7 +798,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > return -EFAULT; > } > > - /* Let's check if we will get back a huge page backed by hugetlbfs */ > + /* > + * Let's check if we will get back a huge page backed by hugetlbfs, or > + * get block mapping for device MMIO region. > + */ > mmap_read_lock(current->mm); > vma = find_vma_intersection(current->mm, hva, hva + 1); > if (unlikely(!vma)) { > @@ -778,15 +810,15 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > return -EFAULT; > } > > - if (is_vm_hugetlb_page(vma)) > - vma_shift = huge_page_shift(hstate_vma(vma)); > - else > - vma_shift = PAGE_SHIFT; > - > - if (logging_active || > - (vma->vm_flags & VM_PFNMAP)) { > + /* > + * logging_active is guaranteed to never be true for VM_PFNMAP > + * memslots. > + */ > + if (logging_active) { > force_pte = true; > vma_shift = PAGE_SHIFT; > + } else { > + vma_shift = get_vma_page_shift(vma, hva); > } > > switch (vma_shift) { > @@ -854,8 +886,17 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > return -EFAULT; > > if (kvm_is_device_pfn(pfn)) { > + /* > + * If the page was identified as device early by looking at > + * the VMA flags, vma_pagesize is already representing the > + * largest quantity we can map. If instead it was mapped > + * via gfn_to_pfn_prot(), vma_pagesize is set to PAGE_SIZE > + * and must not be upgraded. > + * > + * In both cases, we don't let transparent_hugepage_adjust() > + * change things at the last minute. > + */ > device = true; > - force_pte = true; > } else if (logging_active && !write_fault) { > /* > * Only actually map the page as writable if this was a write > @@ -876,7 +917,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, > * If we are not forced to use page mapping, check if we are > * backed by a THP and thus use block mapping if possible. > */ > - if (vma_pagesize == PAGE_SIZE && !force_pte) > + if (vma_pagesize == PAGE_SIZE && !(force_pte || device)) > vma_pagesize = transparent_hugepage_adjust(memslot, hva, > &pfn, &fault_ipa); > if (writable) >