From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77500261B9B; Thu, 30 Apr 2026 02:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777514771; cv=none; b=qebemUjs3eBvdEdsINfstb5Muyei5Y2ZkEGcetSExJqX/O6x8qoZ1ZEhdY2hOiX42f+nKwwvxOy6IqrFPHiGV6MsZvWCHXStkTHD++LUH2Solz+GSsE8df/ZwLoxDdXCjwoOvnhgdWi040GbuBvSDFy9A6FbpvogbAwh13PFwbs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777514771; c=relaxed/simple; bh=65lhpD6rP/YvSNozN+fqdK4CdsF7vKfZvJZHa0xJE48=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=HXuafjLDhhqDlJ5+MLqkw5gXXaDTibuYhXKzqhzaIBSYz7+gXpZId4pl7IpsBeDgm3f49xcfHqkxDVA3fwcauy0jDCyjcLVY7c3oDxsqqb6AjCGtJI3CPbzL2jW02CKWoK0lOjOXEerC9fRh5L0IpFelNMv3EiexzG927mqe+Ak= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=n+DdwBXC; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="n+DdwBXC" Received: from [192.168.0.88] (192-184-212-33.fiber.dynamic.sonic.net [192.184.212.33]) by linux.microsoft.com (Postfix) with ESMTPSA id 17A9E20B716C; Wed, 29 Apr 2026 19:06:10 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 17A9E20B716C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1777514770; bh=6UCMWSX9Tq9LLa9FeKzKfc8c5i+ljfS37AZnjjcKSGM=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=n+DdwBXCBo7vk3ys7RUnkkjMl82lXLlOwmho1Rz/0RCCEFa/A1zA+fw2N4i15L05J 92t300jgY1hgsqpXGIdGQ9dnE+Dp1yZH6KwDnhp908oITEVz5Z+8vxut4GcQDhtius khOqNPOEP4aqFz/A4SjhMYi+dyzLCbepXdjikX5Y= Message-ID: <61e5d806-b5d5-ab2c-0e09-6def449d5582@linux.microsoft.com> Date: Wed, 29 Apr 2026 19:06:08 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.1 Subject: Re: [PATCH v2] mshv: Simplify GPA map/unmap hypercall helpers Content-Language: en-US To: Stanislav Kinsburskii , kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, longli@microsoft.com Cc: linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org References: <177748126383.33250.14844440376241852870.stgit@skinsburskii-cloud-desktop.internal.cloudapp.net> From: Mukesh R In-Reply-To: <177748126383.33250.14844440376241852870.stgit@skinsburskii-cloud-desktop.internal.cloudapp.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/29/26 09:48, Stanislav Kinsburskii wrote: > Clean up hv_do_map_gpa_hcall() and hv_call_unmap_gpa_pages() after the > preceding bug-fix patches: > > Move "done += completed" before the status checks so that pages mapped > by a partially-successful batch are included in the error cleanup unmap. > Previously these mappings were leaked on failure. > > While here, improve type safety and readability: > - Change "int done" to "u64 done" to match the u64 page_count it is > compared against, avoiding signed/unsigned comparison hazards. > - Use u64 for loop iteration and batch size variables consistently. > - Add proper braces to the for-loop body in hv_do_map_gpa_hcall(). > - Remove unnecessary "ret" variable from hv_call_unmap_gpa_pages(). > - Simplify the error-path unmap to use "done << large_shift" directly > instead of mutating done in place. > what changed in V2? > Fixes: 621191d709b14 ("Drivers: hv: Introduce mshv_root module to expose /dev/mshv to VMMs") > Signed-off-by: Stanislav Kinsburskii > --- > drivers/hv/mshv_root_hv_call.c | 55 +++++++++++++++------------------------- > 1 file changed, 20 insertions(+), 35 deletions(-) > > diff --git a/drivers/hv/mshv_root_hv_call.c b/drivers/hv/mshv_root_hv_call.c > index e5992c324904a..1f19a4ca824f0 100644 > --- a/drivers/hv/mshv_root_hv_call.c > +++ b/drivers/hv/mshv_root_hv_call.c > @@ -195,8 +195,8 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, > struct hv_input_map_gpa_pages *input_page; > u64 status, *pfnlist; > unsigned long irq_flags, large_shift = 0; > - int ret = 0, done = 0; > - u64 page_count = page_struct_count; > + u64 done = 0, page_count = page_struct_count; > + int ret = 0; > > if (page_count == 0 || (pages && mmio_spa)) > return -EINVAL; > @@ -213,8 +213,8 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, > } > > while (done < page_count) { > - ulong i, completed, remain = page_count - done; > - int rep_count = min(remain, HV_MAP_GPA_BATCH_SIZE); > + u64 i, completed, remain = page_count - done; > + u64 rep_count = min_t(u64, remain, HV_MAP_GPA_BATCH_SIZE); > > local_irq_save(irq_flags); > input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); > @@ -224,23 +224,13 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, > input_page->map_flags = flags; > pfnlist = input_page->source_gpa_page_list; > > - for (i = 0; i < rep_count; i++) > - if (flags & HV_MAP_GPA_NO_ACCESS) { > + for (i = 0; i < rep_count; i++) { > + if (flags & HV_MAP_GPA_NO_ACCESS) > pfnlist[i] = 0; > - } else if (pages) { > - u64 index = (done + i) << large_shift; > - > - if (index >= page_struct_count) { > - ret = -EINVAL; > - break; > - } > - pfnlist[i] = page_to_pfn(pages[index]); > - } else { > + else if (pages) > + pfnlist[i] = page_to_pfn(pages[(done + i) << large_shift]); Entire file is 80 cols, please don't cause this one overflow. Thanks, -Mukesh > + else > pfnlist[i] = mmio_spa + done + i; > - } > - if (ret) { > - local_irq_restore(irq_flags); > - break; > } > > status = hv_do_rep_hypercall(HVCALL_MAP_GPA_PAGES, rep_count, 0, > @@ -248,29 +238,26 @@ static int hv_do_map_gpa_hcall(u64 partition_id, u64 gfn, u64 page_struct_count, > local_irq_restore(irq_flags); > > completed = hv_repcomp(status); > + done += completed; > > if (hv_result_needs_memory(status)) { > ret = hv_call_deposit_pages(NUMA_NO_NODE, partition_id, > HV_MAP_GPA_DEPOSIT_PAGES); > if (ret) > break; > - > } else if (!hv_result_success(status)) { > ret = hv_result_to_errno(status); > break; > } > - > - done += completed; > } > > if (ret && done) { > u32 unmap_flags = 0; > > - if (flags & HV_MAP_GPA_LARGE_PAGE) { > + if (flags & HV_MAP_GPA_LARGE_PAGE) > unmap_flags |= HV_UNMAP_GPA_LARGE_PAGE; > - done <<= large_shift; > - } > - hv_call_unmap_gpa_pages(partition_id, gfn, done, unmap_flags); > + hv_call_unmap_gpa_pages(partition_id, gfn, > + done << large_shift, unmap_flags); > } > > return ret; > @@ -305,7 +292,7 @@ int hv_call_unmap_gpa_pages(u64 partition_id, u64 gfn, u64 page_count_4k, > struct hv_input_unmap_gpa_pages *input_page; > u64 status, page_count = page_count_4k; > unsigned long irq_flags, large_shift = 0; > - int ret = 0, done = 0; > + u64 done = 0; > > if (page_count == 0) > return -EINVAL; > @@ -319,8 +306,8 @@ int hv_call_unmap_gpa_pages(u64 partition_id, u64 gfn, u64 page_count_4k, > } > > while (done < page_count) { > - ulong completed, remain = page_count - done; > - int rep_count = min(remain, HV_UMAP_GPA_PAGES); > + u64 completed, remain = page_count - done; > + u64 rep_count = min_t(u64, remain, HV_UMAP_GPA_PAGES); > > local_irq_save(irq_flags); > input_page = *this_cpu_ptr(hyperv_pcpu_input_arg); > @@ -333,15 +320,13 @@ int hv_call_unmap_gpa_pages(u64 partition_id, u64 gfn, u64 page_count_4k, > local_irq_restore(irq_flags); > > completed = hv_repcomp(status); > - if (!hv_result_success(status)) { > - ret = hv_result_to_errno(status); > - break; > - } > - > done += completed; > + > + if (!hv_result_success(status)) > + return hv_result_to_errno(status); > } > > - return ret; > + return 0; > } > > int hv_call_get_gpa_access_states(u64 partition_id, u32 count, u64 gpa_base_pfn, > >