From nobody Fri Dec 19 10:56:27 2025 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F52030BB87; Thu, 6 Nov 2025 22:13:34 +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=1762467215; cv=none; b=IG9ErhbSKoYMytwY7cxQUzUMlJZi18jaZlMuF5+7ISHXSsewF7r2aDkI08WkQWH07HNglmX1GPbV5NVFvIFFreXZkZHKixPFyf8F/aBjVm/UOC4UjegPOLYKYYxUapaba7axIXzw+Nesv48SNLyrESQdkZvP0s4v+lqv4HJs2dA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762467215; c=relaxed/simple; bh=NoAV/qqLTNPnOHQkJhqWiMxjidQ0Q1L5/4bu4KvL/24=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=aHftA1DgAk8VvpCFTS19zxzepC/SWsQGZxQdKNZ6LyPWsP07SddNX1Gpc+OXNipG4oLADMCjaZaJlTupq5+qDSHPURAv1U3VnZSSHRMKNtCbC7RTnqArEpQzIlkiwynDuNonHCbWtk3l5HJxrDh9ifBoMufxplw6j3jGAU8T0NU= 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=AVr7+Fp3; 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="AVr7+Fp3" Received: by linux.microsoft.com (Postfix, from userid 1032) id 3556D201DAF3; Thu, 6 Nov 2025 14:13:34 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3556D201DAF3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1762467214; bh=YRIZduxeBia6u4O1awQgNLFFEi+mMnIOX8zBN1sXBzM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AVr7+Fp3PkuY9xiJkm1kkLldc9jMweIZ7AH3cvbCsNnuwx/jMg55x9vU6pMVFojAb QdQau7ZWIad7mZkWWlfSja+ybwiN/3nA1HusgdFwB0IH4ovYV0HMnVrdFejmgR9dNA QW2Vvp1qDxOjIgJODb0cr4GLJUSJks5bFymPq6cM= From: Nuno Das Neves To: linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, mhklinux@outlook.com, magnuskulke@linux.microsoft.com Cc: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, longli@microsoft.com, skinsburskii@linux.microsoft.com, prapal@linux.microsoft.com, mrathor@linux.microsoft.com, muislam@microsoft.com, Nuno Das Neves Subject: [PATCH v2 1/2] mshv: Fix create memory region overlap check Date: Thu, 6 Nov 2025 14:13:30 -0800 Message-Id: <1762467211-8213-2-git-send-email-nunodasneves@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1762467211-8213-1-git-send-email-nunodasneves@linux.microsoft.com> References: <1762467211-8213-1-git-send-email-nunodasneves@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The current check is incorrect; it only checks if the beginning or end of a region is within an existing region. This doesn't account for userspace specifying a region that begins before and ends after an existing region. Change the logic to a range intersection check against gfns and uaddrs for each region. Remove mshv_partition_region_by_uaddr() as it is no longer used. Fixes: 621191d709b1 ("Drivers: hv: Introduce mshv_root module to expose /de= v/mshv to VMMs") Reported-by: Michael Kelley Closes: https://lore.kernel.org/linux-hyperv/SN6PR02MB41575BE0406D3AB22E1D7= DB5D4C2A@SN6PR02MB4157.namprd02.prod.outlook.com/ Signed-off-by: Nuno Das Neves --- drivers/hv/mshv_root_main.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c index 814465a0912d..25a68912a78d 100644 --- a/drivers/hv/mshv_root_main.c +++ b/drivers/hv/mshv_root_main.c @@ -1206,21 +1206,6 @@ mshv_partition_region_by_gfn(struct mshv_partition *= partition, u64 gfn) return NULL; } =20 -static struct mshv_mem_region * -mshv_partition_region_by_uaddr(struct mshv_partition *partition, u64 uaddr) -{ - struct mshv_mem_region *region; - - hlist_for_each_entry(region, &partition->pt_mem_regions, hnode) { - if (uaddr >=3D region->start_uaddr && - uaddr < region->start_uaddr + - (region->nr_pages << HV_HYP_PAGE_SHIFT)) - return region; - } - - return NULL; -} - /* * NB: caller checks and makes sure mem->size is page aligned * Returns: 0 with regionpp updated on success, or -errno @@ -1230,15 +1215,21 @@ static int mshv_partition_create_region(struct mshv= _partition *partition, struct mshv_mem_region **regionpp, bool is_mmio) { - struct mshv_mem_region *region; + struct mshv_mem_region *region, *rg; u64 nr_pages =3D HVPFN_DOWN(mem->size); =20 /* Reject overlapping regions */ - if (mshv_partition_region_by_gfn(partition, mem->guest_pfn) || - mshv_partition_region_by_gfn(partition, mem->guest_pfn + nr_pages - 1= ) || - mshv_partition_region_by_uaddr(partition, mem->userspace_addr) || - mshv_partition_region_by_uaddr(partition, mem->userspace_addr + mem->= size - 1)) + hlist_for_each_entry(rg, &partition->pt_mem_regions, hnode) { + u64 rg_size =3D rg->nr_pages << HV_HYP_PAGE_SHIFT; + + if ((mem->guest_pfn + nr_pages <=3D rg->start_gfn || + rg->start_gfn + rg->nr_pages <=3D mem->guest_pfn) && + (mem->userspace_addr + mem->size <=3D rg->start_uaddr || + rg->start_uaddr + rg_size <=3D mem->userspace_addr)) + continue; + return -EEXIST; + } =20 region =3D vzalloc(sizeof(*region) + sizeof(struct page *) * nr_pages); if (!region) --=20 2.34.1 From nobody Fri Dec 19 10:56:27 2025 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F30F30BBB0; Thu, 6 Nov 2025 22:13:35 +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=1762467216; cv=none; b=kvIVYgYQCnknXuCTU4OTcouGwSmQQFbz1Mys82zz7u/eRqw5TsOZF2ER0PXkNdKvI7a1dSPi22QlXjewoMauZ3MSETKmBE89xwdPrgABhDx7/uSGT1CWHPUpTmqTF1vM5pcvTeBIOeN/5YcpCfRA3ckShyy4IVsgHs4uMXfCR8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762467216; c=relaxed/simple; bh=1GsE3ZQQUNdKJ93/v6q7dsqth8ASVKYkWHm9x63xlwI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=W9sTLQKsK02TBp8rqwVh6779UXQDM3TIjxLmzL1ewQ2eV2xuJd1hVpP+6o6V21GpAae+BFm4oI5/L/saRN4O50VgoQYW6W3Es+vXtpPh0WIuv3vpv2foPHnm5WBtpUenuz6HplVlvy1uevlJTGckukgCga1ZDB046pvRJoCi8xU= 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=qngDrBqK; 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="qngDrBqK" Received: by linux.microsoft.com (Postfix, from userid 1032) id 45B99211CFAF; Thu, 6 Nov 2025 14:13:35 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 45B99211CFAF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1762467215; bh=RmRanQLOp0XTHdqwcRIWaYAnmlFXYk+Veiu4Gf5Xd8I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qngDrBqKN8nTsvpAvV+Ptw/TQfqM7S4wP2eJIfkcLw/3q4astbld1BrDmY8Ifmq4n dBR/bMP3OgXl+t0wb0qu2Jd2Yltu3r2E9Znai1/GvZ3j8mVyExuML3YrLwdGREq1bX P7dcHJDYxxmZKlppHfar8ozDuNsb16byeCVCxtU4= From: Nuno Das Neves To: linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, mhklinux@outlook.com, magnuskulke@linux.microsoft.com Cc: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, longli@microsoft.com, skinsburskii@linux.microsoft.com, prapal@linux.microsoft.com, mrathor@linux.microsoft.com, muislam@microsoft.com, Nuno Das Neves Subject: [PATCH v2 2/2] mshv: Allow mappings that overlap in uaddr Date: Thu, 6 Nov 2025 14:13:31 -0800 Message-Id: <1762467211-8213-3-git-send-email-nunodasneves@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1762467211-8213-1-git-send-email-nunodasneves@linux.microsoft.com> References: <1762467211-8213-1-git-send-email-nunodasneves@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Magnus Kulke Currently the MSHV driver rejects mappings that would overlap in userspace. Some VMMs require the same memory to be mapped to different parts of the guest's address space, and so working around this restriction is difficult. The hypervisor itself doesn't prohibit mappings that overlap in uaddr, (really in SPA; system physical addresses), so supporting this in the driver doesn't require any extra work: only the checks need to be removed. Since no userspace code until now has been able to overlap regions in userspace, relaxing this constraint can't break any existing code. Signed-off-by: Magnus Kulke Signed-off-by: Nuno Das Neves --- drivers/hv/mshv_root_main.c | 8 ++------ include/uapi/linux/mshv.h | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c index 25a68912a78d..b1821b18fa09 100644 --- a/drivers/hv/mshv_root_main.c +++ b/drivers/hv/mshv_root_main.c @@ -1220,12 +1220,8 @@ static int mshv_partition_create_region(struct mshv_= partition *partition, =20 /* Reject overlapping regions */ hlist_for_each_entry(rg, &partition->pt_mem_regions, hnode) { - u64 rg_size =3D rg->nr_pages << HV_HYP_PAGE_SHIFT; - - if ((mem->guest_pfn + nr_pages <=3D rg->start_gfn || - rg->start_gfn + rg->nr_pages <=3D mem->guest_pfn) && - (mem->userspace_addr + mem->size <=3D rg->start_uaddr || - rg->start_uaddr + rg_size <=3D mem->userspace_addr)) + if (mem->guest_pfn + nr_pages <=3D rg->start_gfn || + rg->start_gfn + rg->nr_pages <=3D mem->guest_pfn) continue; =20 return -EEXIST; diff --git a/include/uapi/linux/mshv.h b/include/uapi/linux/mshv.h index 9091946cba23..b10c8d1cb2ad 100644 --- a/include/uapi/linux/mshv.h +++ b/include/uapi/linux/mshv.h @@ -123,7 +123,7 @@ enum { * @rsvd: MBZ * * Map or unmap a region of userspace memory to Guest Physical Addresses (= GPA). - * Mappings can't overlap in GPA space or userspace. + * Mappings can't overlap in GPA space. * To unmap, these fields must match an existing mapping. */ struct mshv_user_mem_region { --=20 2.34.1