Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp897950pxy; Thu, 22 Apr 2021 16:40:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyWM1M4FxYtHVtTww+QNN8dKTvnUQowgeZjnKLLU3mgdKmx0IzbgYTul8npkks8voJy/Zfz X-Received: by 2002:a63:f443:: with SMTP id p3mr1119407pgk.378.1619134826561; Thu, 22 Apr 2021 16:40:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619134826; cv=none; d=google.com; s=arc-20160816; b=QniTNum9AXwcQxU89P8uO4apMuSOWJ965L6TJ7zMEWTbHfKct4ltU4pVYQvflsqs5E qk7G3OY07SmBYoPxaeuqBWajwJyGarsm/+FmH+XP+YGN6V2rzVplVsd09GXPzgmvJoC2 S5xPmKDP+TqmJgjCYE+EQ1I5TyQXI3+0EwQhC61a4TWEWOWdNfu9NPEVVIJJevLT0/aC 87QgbaXh7DZ1OJqXYjFfd0eK2EODlUxWJg2Vjw5iNRtxoAHapxfeIUvgchmj1llegg2p U5Irkpt8r+VIKWywfbRLDIvH0kKClWL2cFwhqzhjINDl6icIidMuYVOXko3XXzQCPq+J bHXg== 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=bcDE3sqsWHuqQL+B9smTf1hp8AVzLqNa56QsRBV4nHA=; b=xPwfDoTDeSG1HKpwz5ajC8g1bElwK8ZUI+H08oRcaxSk69eZaNVBaTIQ8oZN5XcwfI DArMLA85xARmhSMiBlx79hRUDxsXVAQkFK51IS673HZoCTYpOrw6oWspOL3Dnu+iTzsc nO6vSxat+Nv+UuHP+VJZ7OZxO9drVYvj0SsuN8mtxyYSKWr4iwby7wZnzQKRNUmLpONw kjqT1+zo09ugQu5H3NE3gqalS1yKn/RSKRexG+AEn8laj3bBHLfsCGnpp6i8UhMfxUhn um8jO7deABjLJT2+f1o/ODZesLF0P51HziUyBOnwPujJie3oVAv6FzKLUQHO+V9B3tzW PhvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eqGj8k7r; 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 e7si7883000pjh.93.2021.04.22.16.40.14; Thu, 22 Apr 2021 16:40:26 -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=eqGj8k7r; 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 S239821AbhDVXj2 (ORCPT + 99 others); Thu, 22 Apr 2021 19:39:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:42848 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235977AbhDVXj2 (ORCPT ); Thu, 22 Apr 2021 19:39:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1619134732; 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=bcDE3sqsWHuqQL+B9smTf1hp8AVzLqNa56QsRBV4nHA=; b=eqGj8k7rW6ubI0ApnQ4XFbIxuEl8jMqD0xjHFb9ELxoy5k5xmVmnCNuDOdHydJBHuLwHft PA9j/0axg3CLMOFSm/QI2VMk0zPDa/c+x0WXeav2K/4IKvrmARNkZm2HSleuL/hvYwKXGf B2fbJtCJPhj7XmyyhXqiTcBFmyuUgz0= 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-375-9G7cYNIXOj-Tj_QQxtYAhg-1; Thu, 22 Apr 2021 19:38:50 -0400 X-MC-Unique: 9G7cYNIXOj-Tj_QQxtYAhg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7F25B91104; Thu, 22 Apr 2021 23:38:31 +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 B228759479; Thu, 22 Apr 2021 23:38:28 +0000 (UTC) Reply-To: Gavin Shan Subject: Re: [PATCH] KVM: arm64: Correctly handle the mmio faulting To: Marc Zyngier Cc: Keqian Zhu , kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, cjia@nvidia.com, linux-arm-kernel@lists.infradead.org, "Wanghaibin (D)" References: <1603297010-18787-1-git-send-email-sashukla@nvidia.com> <8b20dfc0-3b5e-c658-c47d-ebc50d20568d@huawei.com> <2e23aaa7-0c8d-13ba-2eae-9e6ab2adc587@redhat.com> <871rb3rgpl.wl-maz@kernel.org> <87v98eq0dh.wl-maz@kernel.org> From: Gavin Shan Message-ID: <33eb4850-2476-01ae-e3db-5bd20c1a2c23@redhat.com> Date: Fri, 23 Apr 2021 11:38:38 +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: <87v98eq0dh.wl-maz@kernel.org> 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.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Marc, On 4/22/21 4:50 PM, Marc Zyngier wrote: > On Thu, 22 Apr 2021 03:02:00 +0100, > Gavin Shan wrote: >> On 4/21/21 9:59 PM, Marc Zyngier wrote: >>> On Wed, 21 Apr 2021 07:17:44 +0100, >>> Keqian Zhu wrote: >>>> On 2021/4/21 14:20, Gavin Shan wrote: >>>>> On 4/21/21 12:59 PM, Keqian Zhu wrote: >>>>>> On 2020/10/22 0:16, Santosh Shukla wrote: >>>>>>> The Commit:6d674e28 introduces a notion to detect and handle the >>>>>>> device mapping. The commit checks for the VM_PFNMAP flag is set >>>>>>> in vma->flags and if set then marks force_pte to true such that >>>>>>> if force_pte is true then ignore the THP function check >>>>>>> (/transparent_hugepage_adjust()). >>>>>>> >>>>>>> There could be an issue with the VM_PFNMAP flag setting and checking. >>>>>>> For example consider a case where the mdev vendor driver register's >>>>>>> the vma_fault handler named vma_mmio_fault(), which maps the >>>>>>> host MMIO region in-turn calls remap_pfn_range() and maps >>>>>>> the MMIO's vma space. Where, remap_pfn_range implicitly sets >>>>>>> the VM_PFNMAP flag into vma->flags. >>>>>> Could you give the name of the mdev vendor driver that triggers this issue? >>>>>> I failed to find one according to your description. Thanks. >>>>>> >>>>> >>>>> I think it would be fixed in driver side to set VM_PFNMAP in >>>>> its mmap() callback (call_mmap()), like vfio PCI driver does. >>>>> It means it won't be delayed until page fault is issued and >>>>> remap_pfn_range() is called. It's determined from the beginning >>>>> that the vma associated the mdev vendor driver is serving as >>>>> PFN remapping purpose. So the vma should be populated completely, >>>>> including the VM_PFNMAP flag before it becomes visible to user >>>>> space. >>> >>> Why should that be a requirement? Lazy populating of the VMA should be >>> perfectly acceptable if the fault can only happen on the CPU side. >>> >> >> It isn't a requirement and the drivers needn't follow strictly. I checked >> several drivers before looking into the patch and found almost all the >> drivers have VM_PFNMAP set at mmap() time. In drivers/vfio/vfio-pci.c, >> there is a comment as below, but it doesn't reveal too much about why >> we can't set VM_PFNMAP at fault time. >> >> static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma) >> { >> : >> /* >> * See remap_pfn_range(), called from vfio_pci_fault() but we can't >> * change vm_flags within the fault handler. Set them now. >> */ >> vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; >> vma->vm_ops = &vfio_pci_mmap_ops; >> >> return 0; >> } >> >> To set these flags in advance does have advantages. For example, >> VM_DONTEXPAND prevents the vma to be merged with another >> one. VM_DONTDUMP make this vma isn't eligible for >> coredump. Otherwise, the address space, which is associated with the >> vma is accessed and unnecessary page faults are triggered on >> coredump. VM_IO and VM_PFNMAP avoids to walk the page frames >> associated with the vma since we don't have valid PFN in the >> mapping. > > But PCI clearly isn't the case we are dealing with here, and not > everything is VFIO either. I can *today* create a driver that > implements a mmap+fault handler, call mmap() on it, pass the result to > a memslot, and get to the exact same result Santosh describes. > > No PCI, no VFIO, just a random driver. We are *required* to handle > that. > hmm, ok. I was thinking it's related to VFIO mdev driver when Santosh was talking about "mdev driver". Anyway, it's always nice to support the case :) Thanks, Gavin