From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (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 C3922288C0E for ; Fri, 3 Jul 2026 17:10:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783098623; cv=none; b=IdvbHi0fihNCEwwLecMpbQB4eOtfAza/oel/oXbYWsxLS89+s5eTOfLju4DbjvJEXsoYRxaWaWk5qRgFJRAutgtywj8LkLdGfBlNEK45ol+gK9Od11JxLFJPEZs+GeHYgEWZvCTFLere2tIjWqCWY9eJloPCx31evsu+nRflhuk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783098623; c=relaxed/simple; bh=JqbyUGSlLgU7xpTZ+Kst/HuDrhY2T70S9MikqXlpd+w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sT9cO1mKiQ9TjQlPTxCiC6570RLCjiKU49B5N5y7ibOT7wjbfPyC916wqaOTWh0gRgX4DvrX8ZpHEKV9t6aqJYiYObse5LrRxUQOC71DqF9/f7TSWN8HPt3jUGhNAdfOelQilxClypjRjOhM0Ull0o3Hm8rNlmPA9nRvx1LiHtg= 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=BJNelsAl; arc=none smtp.client-ip=209.85.222.178 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="BJNelsAl" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-92e855da580so58862885a.1 for ; Fri, 03 Jul 2026 10:10:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1783098621; x=1783703421; 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=n3oZ+zgqAAjsMiarR8e3Jw/svVLzceSmIMfYpYh96XE=; b=BJNelsAlO6FnCo9zXD9lU+4SoRJW3Tg0r1YDSlnhVtpce/fKXpv6TZQ96ggJUP0RW3 OhY5I4MRnhMRmVVyF5tufLWyo6PRrBE2WMLMJ/fTAu/xd93sqmuhvsCKXrIHPo0AmOMZ Fwvb4f4cZNrgoYyiQ9FrQoQKs5K1CQtzKoV7BZd1wh/H0/vDSidipaNbRED6rpYuaodD fLFSsYGJGrSr9QGerD2znfRfvnicK811K+ffhJuhXjbO4/wWWAGx5M0+zI8ucdQqk4Uf 9bpxIv39X++mU0XhhvBHWDFMQlFmqDCzRtt6RDDyj+lTFDDkk9rCxOPK09J6QQfYMFDA qsgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783098621; x=1783703421; 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=n3oZ+zgqAAjsMiarR8e3Jw/svVLzceSmIMfYpYh96XE=; b=IUNJAAjEPdZEs+OQpY6dn9LV2Je6hRbja1rz1NCFWBcpKMnTCmfD+0SYE54jJE0IZt Lngqy6f0+jsDvxt+k3lbBAcaViq5zzgXLe5qOcfv0okcDZKmXAzRh+CkHNaHXLmOYbYX oKHmRSpV0ysUAnFUq0cqjKeX/yq7VLgi6/AtQvUlRudicO6qMPQV04yUju5jshnjCcch cm0SpMyEYr6LMGQkWZKHF+sO4EBcqN98YYNcUNWMfqHpnJlrHtrSyrL6gF48gD5/yufV NjAgi/VhR+8OD4IwC2At61tMPLCLnAO61Ix+M0jw7hiTrLhrKCS67Ow+ZtW8kH/Qbo+3 n0Mw== X-Gm-Message-State: AOJu0YyGdWrrdMpRal+KsDPI7YoXBxuVPXWg8JBIOkEaeLxb2FHBL1iJ 2Job8rO9M4Li8DwSB82canlS+zpIllVW/nkDoo/4FdvcS8Lkq51dQlinKDDg0goVvq0= X-Gm-Gg: AfdE7cms6ic9S8PFVzu3TsTwQyKyKngGIZTvqM81+69tk4BqyKBzis1Zx9YoY4h7OFq oWuLAQPukp3Ai38pBFTBCaHs5dikyhK/mG0Zv60zbkkdpsCOcP++xRojk65xngFetSD0yG1lLRk A6fFzJQ3kMT63qjejPZOAWB5iNfNPRauTecnA2/wF+lHv6NED69zOaARbpglIA1bNkOBzoIO98z Iyn3J+x/FTf2g9+Gb68BKUYA4vaAWSbK86ULXm13w/Sg4hO13g3QxeOQpX3y06Hasdfc+TRTsv2 qxo0xh9t+pTTaAcOzAT9joj4BxJGpAgyV8lAcYOnbswAdoCnPniUf/aONGsUksgR+6MGh1dF9X6 Cyq8jYWUQx8X4ElCWA2g6NCmfcL/E8P0x0tCVQbjLXIbA3OM3mAuKMFZP/dq1VMyVR8q4/U4Z1f ii1oOKuFXhHYqfSj4xSmpuy0y8eN8t+e9rC7RI2uGZ4I1PJCVRXczYe/HyG1AWNcKrU0M= X-Received: by 2002:a05:620a:46a5:b0:915:5379:b511 with SMTP id af79cd13be357-92e9a48e177mr44923985a.43.1783098620659; Fri, 03 Jul 2026 10:10:20 -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-92e90cbe3e2sm200315285a.35.2026.07.03.10.10.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Jul 2026 10:10:19 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.97) (envelope-from ) id 1wfhP8-00000008GuS-3eyQ; Fri, 03 Jul 2026 14:10:18 -0300 Date: Fri, 3 Jul 2026 14:10:18 -0300 From: Jason Gunthorpe To: Yu Zhang Cc: linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, iommu@lists.linux.dev, linux-pci@vger.kernel.org, linux-arch@vger.kernel.org, wei.liu@kernel.org, kys@microsoft.com, haiyangz@microsoft.com, decui@microsoft.com, longli@microsoft.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, bhelgaas@google.com, kwilczynski@kernel.org, lpieralisi@kernel.org, mani@kernel.org, robh@kernel.org, arnd@arndb.de, mhklinux@outlook.com, jacob.pan@linux.microsoft.com, tgopinath@linux.microsoft.com, easwar.hariharan@linux.microsoft.com, mrathor@linux.microsoft.com Subject: Re: [PATCH v2 4/4] iommu/hyperv: Add page-selective IOTLB flush support Message-ID: <20260703171018.GA1968184@ziepe.ca> References: <20260702160518.311234-1-zhangyu1@linux.microsoft.com> <20260702160518.311234-5-zhangyu1@linux.microsoft.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: <20260702160518.311234-5-zhangyu1@linux.microsoft.com> On Fri, Jul 03, 2026 at 12:05:18AM +0800, Yu Zhang wrote: > @@ -401,10 +402,74 @@ static void hv_iommu_flush_iotlb_all(struct iommu_domain *domain) > hv_flush_device_domain(to_hv_iommu_domain(domain)); > } > > +/* > + * Calculate the minimal power-of-two aligned range that covers [start, end] > + * (end is inclusive). Returns a single (page_number, page_mask_shift) > + * descriptor that may over-flush when the range is not naturally aligned. > + */ > +static void hv_iommu_calc_flush_range(unsigned long start, unsigned long end, > + union hv_iommu_flush_va *va) > +{ > + unsigned long start_pfn = HVPFN_DOWN(start); > + unsigned long last_pfn = HVPFN_UP(end + 1) - 1; Pedantically end can be ULONG_MAX, you shouldn't be adding to it since it will overflow. > + unsigned long mask_shift, aligned_pfn; > + > + if (start_pfn == last_pfn) { > + mask_shift = 0; > + } else { > + /* > + * Find the highest bit position where start_pfn and last_pfn > + * differ. A range aligned to one above that bit is the > + * smallest power-of-two region that covers both endpoints. > + */ > + mask_shift = __fls(start_pfn ^ last_pfn) + 1; > + } > + > + aligned_pfn = ALIGN_DOWN(start_pfn, 1UL << mask_shift); I think the whole thing is simpler if it stays using bytes until the end: sz_lg2 = __fls(gather->start ^ gather->end); if (sz_lg2 < HV_PAGE_SHIFT) cmd.sz_lg2 = HV_PAGE_SHIFT; page_number = (gather->start & ~(1UL << sz_lg2)) >> HV_PAGE_SHIFT; page_mask_shift = sz_lg2 - HV_PAGE_SIFT; No overflows that way either Jason