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 CD4173090C2 for ; Fri, 3 Jul 2026 17:10:21 +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=1783098624; cv=none; b=USd1BIkp9ZI7PVj24wx+V94fqhS2RMRdagGqHxrNNdAXGBhBY1u5MYioiK9tdcUjkV2X6Jn8Tnu4IUP/Qi6DQ69Ofphf0/huPgf2Vwh5QNEUE8kBYDqGpXIfPULzIrx35wxCNLc4F2uBgSqQ4lb6x7eozfYTTAcYGMCXLwSyKSE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783098624; 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=jKYZUkogewqMDQXs8GslxPm/qhCi5/N0nvAXMVLVlnea3+kBtyDpKwm1kfbHWWaH1/AsGY3L4Rd5V/tUsaVHk6dc2FZKHugvV7moRDAl80Lk85ROSK3QxNqcz9DFUxdJhIeI3RUlOqtA0OjPhkAKmjLYlTnyZzX6xxMsVQnkhXc= 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.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="BJNelsAl" Received: by mail-qk1-f179.google.com with SMTP id af79cd13be357-92e50979c71so70642085a.3 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=ezD4fUPoeRIuZ4QE8esqcdetWnS/kJ4GmW7K5yTdCZmeIw2aS7uoorw5KppwfqgRlZ AG27+ocImUF4Z1mbrldIiV591bj4JdAqj2g54kdjS1VNCnAgi5d7YhlScwdndmqnZXOJ Eaj2dg9/uUZOtlS+IbqM/6oyeXMEZP7ZDfxW85FqrjCbZD1l5ibd0kKNQZqE7WIBy4vY oRUmsUX76h3zYqQcgXs78l4q3SYXFRTRXiCuNg0VOtU6lhknjuFSnq7d8ScuTsjGxRhS b+LIcVKDU5q4GirO/y67k8DultRsm8BTbk4frM3LnnAoSmCbneojoiVKETeLxCZTUAOx oBMQ== X-Forwarded-Encrypted: i=1; AFNElJ9Pff13EKM5wHxfZ4HAqlaewZQ/BeO0ICJ0hxKR0ZXb7NHkEIvGASfVRfYe+9Wdr1X1S6ro6984gO5c4jc=@vger.kernel.org X-Gm-Message-State: AOJu0YzcHMwf6+vA82+IQKPHJ33vFQIvgHhjwpOQEQM/Qs28zkR3XjR5 uqvEnabPIv6ecXOIr4NCQEvKwKuQd2Tclq2tsSM/YRbqJZXbrw1zCHlcD+PPNLfugg8= X-Gm-Gg: AfdE7ckMITZTyIN0ZTwMOJ2MWqOxI9FvV8McvhhKSil/Lm2P6Hs8W/eZNspVXSJIqXm Al38//KaJeC7ObSZUMVqigcSMO+gE0hW/ZoWmBiMmZqRlF0bbsMGOlYkNPR7/W3NHWka20kRdmD nTtgih6xY+OqxGtwmDK8T8ckljGokXcxPZhy1PsXYQSpHUBHX+sPwsoRykQ5giMtuE/ITprm8ox ty6MEGhuLs+cyyvTtKA13gCUgzu5ekayke/BEA6oX7qK68p/Sm3pdYJhN7KpdwyoF3u0EzOK/tG +oAOIxTnvjRMld6mjHQV244rH/RgeZANCQ4U3RwEE8t/4ZcjHj+uw+kUBzu7gdvv2lJpJH3yduy NWoTDCcPgj7HNpH+nssVM33yVQ/ZD5NKzgUr36+G3tcXDcqhzvvZjPXN3tFNSvEoldY3yLHz7WA D0VaAArFIDoSJICHiFJR7ovGTtWN7pa6eE3sRlYJfglhBIr5leHnOzc728LArLt3MiniA= 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-hyperv@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