From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f179.google.com (mail-qk1-f179.google.com [209.85.222.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF6562D3A7C for ; Sun, 31 May 2026 23:58:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780271919; cv=none; b=frBuIrABMiRunOIrC1WLAlhMeVBI/VL0uDvGZJwo6clHEPQSeV2R2IT0meKiGVLEAQxORXyau+yzIgRPgustzDXFaA+8wAgSvEA9zySQjHFbyPjMaLtVF2Qp9+LaUB2Mav08vTFX1FVmSvxM0sLaKnPHgy/9ygcanpsDk7ykenw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780271919; c=relaxed/simple; bh=ooYjiyJ6bESeHmXTkdx8VEEz9XJkeLo2Rh6zUaPq15U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=j26Sxuq+UM+FJXzkC6hTV9BSNRtjz982VyN1kOpLJjy5RNRaWhSK25+3/8GiDdQQL6yMvZ4YlIaG1+yZahi83d4bhTUjLceEVtNSx8XLrJ55ldob7L8ZMlCgPw38KLOeW+5e69HxL68cnMAco4+9ZKFQTq+85PmDBvLs2sVT0rI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca; spf=pass smtp.mailfrom=ziepe.ca; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b=dbSa7QpQ; arc=none smtp.client-ip=209.85.222.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="dbSa7QpQ" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-915671abd29so5150585a.0 for ; Sun, 31 May 2026 16:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1780271917; x=1780876717; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=hTfGHM2qB3g/FLyvX8FCIrm9+eNCgSr89Naia4gruos=; b=dbSa7QpQSCIRaTEyv4Dl2wlQd7AOtrnoXzin92ac5CGCF7lAz6V1rkhE6RuyqFX86b fWE2C6sey4Ar5iljgqC2iYHETLz05MxQlg3bjCHBb0xT6pieYwbckyMtAgqbD3SD43EB PpSqMva/IdyvHKQnT6rOmXd0dgl+KIFVgL5YkkAPCGeY7i8LJ/Rl2hy2vj/LGHQtV9bz 82ai3qc0CMwNi9y6mcoomoJ/Ai1UhsdI/tck3KDVnTWRxN+4olAEoOgnN0oiXH3ZBU68 PkHOgv1blrEalsYRWq2ysvKCIBr0u+15tgFRWg5jXNrZjvXacFXUh1WCjbnRFbLn1N/U 4ekg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780271917; x=1780876717; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hTfGHM2qB3g/FLyvX8FCIrm9+eNCgSr89Naia4gruos=; b=FeUyOaivujJCkZIru4plF6GbZIYTnCzFix0jteFfdyNG0ls7r/v3ZRsnDsnZRuB925 6ggUpIuJzVOSgHDpNxHLNx7UET0af66ID99Cjia1uwD72P1uwVjWKzJdSNOu5Ve017Jm HlNwNtXBkX2m2NpvgcDGn8R6Ll6kTSP6wqLhCe4A8NIyQ4ejh5FnhPr2qbF8f28S76Ni dsuFVTNMeSTkZMAxDVqhhlm/AQUfDh6kqQuolRwl/f23x8CZ9DypqbfjJ6SDpkObAO3e pyKT2D4O0RctH7o68XGh6b031DTQCEL6oRcA3Tv5SdNJ01hA50lozLXTNr5tEfLHRNTT QI1w== X-Forwarded-Encrypted: i=1; AFNElJ+xusv0D0VeP7kK+6h8Ov37CFDBZe9Mn2kKfG+GqiZ7EUjqXHdT3xg76+/zpkduwWQveAgDti9lNyYj2q0=@vger.kernel.org X-Gm-Message-State: AOJu0YwC22xF3rpYMEsD2ifQWPGGVW3bJnADZLMspB/LnmyY53fkEWGd TvGeyeqvSTmm4SJ8ppk239TJlrGpS70ROTAN3ITE5sqjF8gVQakEWraZ7ewUCDrE41s= X-Gm-Gg: Acq92OEkp13keYMpHgSIEbwJ3lap9lfklGR3rizVmjuK3B8h1cLBjMZc0qBbmpdbUPN NHzcUy0/WvVv7vXHDHTTTdn0U0SJv79rFVATAiNDaBKj2L5qauDP6QzmwGt64ntrx2F518YgEXV VllxJRH+dAEYlI/9VIQ4m69G420S+BDdmhlsRamJ3+JDXbD3nobtg+XaFhED8cOOer+ae/ePypM UnwTXYYUnS2DLkxuqR0pbqbCcGFHxOkvLi3IjQjJ+HX7UfMnNnD13PrDma39SuRU6Ng2N2lzxA+ STdZTMCg6A5PzjkBA+W5aoY5DtQhkIfs8vm1+XzZFibMp0LCcgLPHNahpxaoFP0Grvkr0CUKc+2 txEMpHBSv0Ehi6rjcCKCOGK5PzS5ypd4ZRJjmFWP20MC9hJOTtH9pQ2SuBzqwDOezZVVGpc0Zbv iylZeE8/F9XlpbCM4uz6LTEaigLdKF7veGXoPWfxyMgBesi/H5Y+QIl/7fd0/i0+/OEUXoxBeno jbiRurlD8zb37UR X-Received: by 2002:a05:620a:bc8:b0:8cd:97de:bb52 with SMTP id af79cd13be357-9153d9703b8mr1495082185a.22.1780271916895; Sun, 31 May 2026 16:58:36 -0700 (PDT) Received: from ziepe.ca (crbknf0213w-47-54-130-67.pppoe-dynamic.high-speed.nl.bellaliant.net. [47.54.130.67]) by smtp.gmail.com with ESMTPSA id af79cd13be357-9153262e2a6sm837284285a.40.2026.05.31.16.58.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 May 2026 16:58:36 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.97) (envelope-from ) id 1wTq39-00000001Lt5-3GHA; Sun, 31 May 2026 20:58:35 -0300 Date: Sun, 31 May 2026 20:58:35 -0300 From: Jason Gunthorpe To: Guanghui Feng Cc: boris.brezillon@collabora.com, robh@kernel.org, steven.price@arm.com, adrian.larumbe@collabora.com, maarten.lankhorst@linux.intel.com, mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com, liviu.dudau@arm.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, alex@shazbot.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kevin.tian@intel.com, baolu.lu@linux.intel.com, suravee.suthikulpanit@amd.com, dwmw2@infradead.org, xlpang@linux.alibaba.com, oliver.yang@linux.alibaba.com, shiyu.zsq@linux.alibaba.com, wei.guo.simon@linux.alibaba.com Subject: Re: [PATCH 7/9] vfio/iommufd: use iova_to_phys_length for efficient unmap Message-ID: <20260531235835.GX2487554@ziepe.ca> References: <20260529115116.GR2487554@ziepe.ca> <20260531093637.3893199-1-guanghuifeng@linux.alibaba.com> <20260531093637.3893199-8-guanghuifeng@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260531093637.3893199-8-guanghuifeng@linux.alibaba.com> On Sun, May 31, 2026 at 05:36:35PM +0800, Guanghui Feng wrote: > /* > - * This is pretty slow, it would be nice to get the page size > - * back from the driver, or have the driver directly fill the > - * batch. > + * Use iova_to_phys_length to get both the physical address > + * and the PTE page size in a single page table walk, allowing > + * us to skip ahead by the contiguous region size instead of > + * walking the page tables for every PAGE_SIZE step. > */ > - phys = iommu_iova_to_phys(domain, iova) - page_offset; > - if (!batch_add_pfn(batch, PHYS_PFN(phys))) > - return; > - iova += PAGE_SIZE - page_offset; > + phys = iommu_iova_to_phys_length(domain, iova, &pgsize) - > + page_offset; > + if (!pgsize || pgsize < PAGE_SIZE) > + pgsize = PAGE_SIZE; It is actually a bug if it returns something < PAGE_SIZE, it should WARN_ON and try to continue. > @@ -1177,25 +1177,41 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma, > > iommu_iotlb_gather_init(&iotlb_gather); > while (pos < dma->size) { > - size_t unmapped, len; > + size_t unmapped, len, pgsize; > phys_addr_t phys, next; > dma_addr_t iova = dma->iova + pos; > > - phys = iommu_iova_to_phys(domain->domain, iova); > + /* Single page table walk returns both phys and PTE size */ > + phys = iommu_iova_to_phys_length(domain->domain, iova, > + &pgsize); > if (WARN_ON(!phys)) { > pos += PAGE_SIZE; > continue; > } > + if (!pgsize || pgsize < PAGE_SIZE) > + pgsize = PAGE_SIZE; > > /* > * To optimize for fewer iommu_unmap() calls, each of which > * may require hardware cache flushing, try to find the > * largest contiguous physical memory chunk to unmap. > + * > + * Calculate remaining contiguous bytes within this PTE from > + * our position, then try to join following physically > + * contiguous PTEs. > */ > - for (len = PAGE_SIZE; pos + len < dma->size; len += PAGE_SIZE) { > - next = iommu_iova_to_phys(domain->domain, iova + len); > + len = pgsize - (iova & (pgsize - 1)); > + for (; pos + len < dma->size; ) { > + size_t next_pgsize; Things should be arranged so the iommu_iova_to_phys_length() always returns the best length, either because it called into iommupt to get it or because it accumulated internally on an old driver. Probably to make this work well the API should include the last address to reach so it can stop iterating at the right point. Jason