From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A60151F8EFF for ; Wed, 21 May 2025 16:02:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747843341; cv=none; b=qVjWoPCWZaXZUKd2UGxhUyGYwdTukOXNIUjFyBocMhfZPyo5ZLlZLw/3Rm9X+UBxTzcZrj1Vh5MIK3+uiUMgeWYzDU1oWFUNi2QuynOhxisRdoq17V38iwvg3EdRABt/1ZhNd8N4CT5YqxTmH2br0XdDbATlrFGHIboHEaKnopY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747843341; c=relaxed/simple; bh=UREKu6qch+HwsQMt1vlqYyRJDWH9laE5S9pm9bS66hE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=K3JNsZNBpyF84x3syrdFv918WGcJ37BMFDWWG9sYMdCm2ahtqftlE2EvJn3RvefUnDqbAxU6yYKNVnrdUkE0twz3zai+una4Egjj5wR+Q/PwqXtDYFuee0hzBUGfcrDIp3lH0+RY3UOfyGHSdW8H4lGQwJhpaO2W+Zj8kORC5PY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=OYLnUG50; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OYLnUG50" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747843338; h=from:from: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:autocrypt:autocrypt; bh=9fZhY6wHMdAbKA49Jq994SZ/xqO7IR+BBcalbyYYXIM=; b=OYLnUG507WjMIVhuptSi/eg9KkKEalIeTZ0RW6v4fUc7aY3W0hws5Dm7V830FFqu0bWyBr 3SH7eZ/TSNaBvj0p1d2laocWhE8GRbx3xZf08iWZ1PH/7vsAW74bIrWTsUvkdKfvNC576i M4LtYXq6fql52j7JvUW/3M3KhOQiouQ= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-548-yJ6lrV1LO_mtiBANqYJ9uQ-1; Wed, 21 May 2025 12:02:15 -0400 X-MC-Unique: yJ6lrV1LO_mtiBANqYJ9uQ-1 X-Mimecast-MFC-AGG-ID: yJ6lrV1LO_mtiBANqYJ9uQ_1747843335 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3a370309dcbso1722470f8f.1 for ; Wed, 21 May 2025 09:02:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747843334; x=1748448134; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=9fZhY6wHMdAbKA49Jq994SZ/xqO7IR+BBcalbyYYXIM=; b=T9MCjBsguCYc3MaCEwyXcm6TbCF0V5NQ7eIGpxZjkp0ULUBEkD+gbiuLYPjf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3a3:62e1:a7db with SMTP id ffacd0b85a97d-3a362e1a997mr13218485f8f.3.1747843334509; Wed, 21 May 2025 09:02:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEVQZhAv284WgoDT4LXe2CAM32OUzc9mP8qdr2b0rFwua5ybh0oAxFZX3kGmiZjGs9lXci0Ng== X-Received: by 2002:a05:6000:180e:b0:3a3:62e1:a7db with SMTP id ffacd0b85a97d-3a362e1a997mr13218436f8f.3.1747843333942; Wed, 21 May 2025 09:02:13 -0700 (PDT) Received: from ?IPV6:2003:d8:2f25:9c00:e2c7:6eb5:8a51:1c60? (p200300d82f259c00e2c76eb58a511c60.dip0.t-ipconnect.de. [2003:d8:2f25:9c00:e2c7:6eb5:8a51:1c60]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a35ca208besm20078586f8f.0.2025.05.21.09.02.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 May 2025 09:02:13 -0700 (PDT) Message-ID: Date: Wed, 21 May 2025 18:02:12 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] vfio/type1: optimize vfio_pin_pages_remote() for huge folio To: Peter Xu Cc: Alex Williamson , lizhe.67@bytedance.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev References: <20250520070020.6181-1-lizhe.67@bytedance.com> <3f51d180-becd-4c0d-a156-7ead8a40975b@redhat.com> <20250520162125.772d003f.alex.williamson@redhat.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacP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N/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 21.05.25 17:40, Peter Xu wrote: > On Wed, May 21, 2025 at 08:35:47AM +0200, David Hildenbrand wrote: >> On 21.05.25 00:21, Alex Williamson wrote: >>> On Tue, 20 May 2025 19:38:45 +0200 >>> David Hildenbrand wrote: >>> >>>> On 20.05.25 09:00, lizhe.67@bytedance.com wrote: >>>>> From: Li Zhe >>>> >>>> Subject: "huge folio" -> "large folios" >>>> >>>>> >>>>> When vfio_pin_pages_remote() is called with a range of addresses that >>>>> includes huge folios, the function currently performs individual >>>> >>>> Similar, we call it a "large" f >>>> >>>>> statistics counting operations for each page. This can lead to significant >>>>> performance overheads, especially when dealing with large ranges of pages. >>>>> >>>>> This patch optimize this process by batching the statistics counting >>>>> operations. >>>>> >>>>> The performance test results for completing the 8G VFIO IOMMU DMA mapping, >>>>> obtained through trace-cmd, are as follows. In this case, the 8G virtual >>>>> address space has been mapped to physical memory using hugetlbfs with >>>>> pagesize=2M. >>>>> >>>>> Before this patch: >>>>> funcgraph_entry: # 33813.703 us | vfio_pin_map_dma(); >>>>> >>>>> After this patch: >>>>> funcgraph_entry: # 15635.055 us | vfio_pin_map_dma(); >>>>> >>>>> Signed-off-by: Li Zhe >>>>> Signed-off-by: Alex Williamson >>>>> --- >>>>> Changelogs: >>>>> >>>>> v2->v3: >>>>> - Code simplification. >>>>> - Fix some issues in comments. >>>>> >>>>> v1->v2: >>>>> - Fix some issues in comments and formatting. >>>>> - Consolidate vfio_find_vpfn_range() and vfio_find_vpfn(). >>>>> - Move the processing logic for huge folio into the while(true) loop >>>>> and use a variable with a default value of 1 to indicate the number >>>>> of consecutive pages. >>>>> >>>>> v2 patch: https://lore.kernel.org/all/20250519070419.25827-1-lizhe.67@bytedance.com/ >>>>> v1 patch: https://lore.kernel.org/all/20250513035730.96387-1-lizhe.67@bytedance.com/ >>>>> >>>>> drivers/vfio/vfio_iommu_type1.c | 48 +++++++++++++++++++++++++-------- >>>>> 1 file changed, 37 insertions(+), 11 deletions(-) >>>>> >>>>> diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c >>>>> index 0ac56072af9f..48f06ce0e290 100644 >>>>> --- a/drivers/vfio/vfio_iommu_type1.c >>>>> +++ b/drivers/vfio/vfio_iommu_type1.c >>>>> @@ -319,15 +319,22 @@ static void vfio_dma_bitmap_free_all(struct vfio_iommu *iommu) >>>>> /* >>>>> * Helper Functions for host iova-pfn list >>>>> */ >>>>> -static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova) >>>>> + >>>>> +/* >>>>> + * Find the first vfio_pfn that overlapping the range >>>>> + * [iova, iova + PAGE_SIZE * npage) in rb tree. >>>>> + */ >>>>> +static struct vfio_pfn *vfio_find_vpfn_range(struct vfio_dma *dma, >>>>> + dma_addr_t iova, unsigned long npage) >>>>> { >>>>> struct vfio_pfn *vpfn; >>>>> struct rb_node *node = dma->pfn_list.rb_node; >>>>> + dma_addr_t end_iova = iova + PAGE_SIZE * npage; >>>>> while (node) { >>>>> vpfn = rb_entry(node, struct vfio_pfn, node); >>>>> - if (iova < vpfn->iova) >>>>> + if (end_iova <= vpfn->iova) >>>>> node = node->rb_left; >>>>> else if (iova > vpfn->iova) >>>>> node = node->rb_right; >>>>> @@ -337,6 +344,11 @@ static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova) >>>>> return NULL; >>>>> } >>>>> +static inline struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova) >>>>> +{ >>>>> + return vfio_find_vpfn_range(dma, iova, 1); >>>>> +} >>>>> + >>>>> static void vfio_link_pfn(struct vfio_dma *dma, >>>>> struct vfio_pfn *new) >>>>> { >>>>> @@ -681,32 +693,46 @@ static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr, >>>>> * and rsvd here, and therefore continues to use the batch. >>>>> */ >>>>> while (true) { >>>>> + struct folio *folio = page_folio(batch->pages[batch->offset]); >>>>> + long nr_pages; >>>>> + >>>>> if (pfn != *pfn_base + pinned || >>>>> rsvd != is_invalid_reserved_pfn(pfn)) >>>>> goto out; >>>>> + /* >>>>> + * Note: The current nr_pages does not achieve the optimal >>>>> + * performance in scenarios where folio_nr_pages() exceeds >>>>> + * batch->capacity. It is anticipated that future enhancements >>>>> + * will address this limitation. >>>>> + */ >>>>> + nr_pages = min((long)batch->size, folio_nr_pages(folio) - >>>>> + folio_page_idx(folio, batch->pages[batch->offset])); >>>>> + if (nr_pages > 1 && vfio_find_vpfn_range(dma, iova, nr_pages)) >>>>> + nr_pages = 1; >>>> >>>> >>>> You seem to assume that the batch really contains the consecutive pages >>>> of that folio. >>> >>> I don't think we are. We're iterating through our batch of pages from >>> GUP to find consecutive pfns. We use the page to get the pfn, the >>> folio, and immediately above, the offset into the folio. batch->size is >>> the remaining length of the page array from GUP and batch->offset is our >>> current index into that array. >> >> Let me try again using an example below .... >> >>>> This is not the case if we obtained the pages through GUP and we have >>>> >>>> (a) A MAP_PRIVATE mapping >>>> >>>> (b) We span multiple different VMAs >>>> >>>> >>>> Are we sure we can rule out (a) and (b)? >>>> >>>> A more future-proof approach would be at least looking whether the >>>> pages/pfns are actually consecutive. >>> >>> The unmodified (pfn != *pfn_base + pinned) test is where we verify we >>> have the next consecutive pfn. Maybe I'm not catching the dependency >>> you're seeing on consecutive pages, I think there isn't one unless >>> we're somehow misusing folio_page_idx() to get the offset into the >>> folio. >> >> Assume our page tables look like this (case (a), a partially mapped large >> pagecache folio mixed with COW'ed anonymous folios): >> >> + page[0] of folio 0 >> | + COWed anonymous folio (folio 1) >> | | + page[4] of folio 0 >> | | | >> v v v >> F0P0 F0P1 F0P2 F1P0 F0P4 P0P5 F0P6 F0P7 >> >> If we GUP that range, we get exactly these pages, except that the PFNs are >> not consecutive, because F0P3 was replaced by another page. The large folio >> is partially mapped. >> >> >> Maybe I misunderstand that code, but wouldn't we just "jump" over F1P0 >> because we assume the batch would contain F1P0, where it really contains >> F0P4? > > Looks like a real issue (even if unlikely setup).. There are other ways to achieve that (e.g., simply mapping parts of multiple files etc.). Of course, for hugetlb (as you note below) such folios partial mappings are impossible. -- Cheers, David / dhildenb