Skip to contentSkip to conversation

vhost-scsi: fix page reference leak in misaligned I/O fallback

PATCH Superseded Jia Jia 3 participants 4 messages Oct 21 – Oct 22

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jia Jia

author
#0 lore raw

PATCH 1/1 vhost-scsi: fix page reference leak in misaligned I/O fallback

iov_iter_get_pages2() returns a byte count that excludes the offset into the first page, but also returns a reference to that page in pages[].

On the misaligned-I/O fallback, vhost_scsi_revert_map_iov_to_sgl() puts pages already added to the SG list. The cleanup loop starts at pages[npages], but accounts for its first page as PAGE_SIZE bytes rather than PAGE_SIZE - offset. If the remaining data starts at a non-zero offset, it can finish one page early and leave a page reference behind.

Use PAGE_SIZE - offset for the first remaining page and clear offset for later pages, as in the mapping loop. Without this, each affected request leaks one page reference.

Fixes bca939d5bcd0 ("vhost-scsi: Dynamically allocate scatterlists")

---
Testing:

- Four fileio vhost-scsi test instances repeatedly issued affected writes using recycled backing pages. After about 1.33 million requests, MemAvailable fell from 5.3 GB to 13 MB and swap was nearly exhausted.

The affected fallback is reachable from guest-issued virtio-scsi I/O.

 drivers/vhost/scsi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/vhost/scsi.c +6 -1
index 4f8c0260bc9e..b7e41c0a91d4 100644
@@ -883,11 +883,16 @@ vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
 	vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg);
 
 	iov_iter_revert(iter, bytes);
+	/*
+	 * pages[npages] is the first page not added to the SG list.  The
+	 * remaining data starts at offset in that page.
+	 */
 	while (bytes) {
-		n = min_t(unsigned int, PAGE_SIZE, bytes);
+		n = min_t(unsigned int, PAGE_SIZE - offset, bytes);
 
 		put_page(pages[npages++]);
 		bytes -= n;
+		offset = 0;
 	}
 
 	return -EINVAL;
Signature · 1 line
2.47.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

iov_iter_get_pages2() returns a byte count that excludes the offset into the first page, but also returns a reference to that page in pages[].

On the misaligned-I/O fallback, vhost_scsi_revert_map_iov_to_sgl() puts pages already added to the SG list. The cleanup loop starts at pages[npages], but accounts for its first page as PAGE_SIZE bytes rather than PAGE_SIZE - offset. If the remaining data starts at a non-zero offset, it can finish one page early and leave a page reference behind.

Quoted 6 lines

Use PAGE_SIZE - offset for the first remaining page and clear offset for later pages, as in the mapping loop. Without this, each affected request leaks one page reference.

Fixes bca939d5bcd0 ("vhost-scsi: Dynamically allocate scatterlists")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Jia Jia

drivers/vhost/scsi.c vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd, · line 887

Quoted 10 lines · Jia Jia
drivers/vhost/scsi.c +2 -0
index 4f8c0260bc9e..b7e41c0a91d4 100644
@@ -883,11 +883,16 @@ vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
  	vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg);
 
  	iov_iter_revert(iter, bytes);
+	/*
+	 * pages[npages] is the first page not added to the SG list.  The

Just a minor nit. There's an extra space before "The" in the comment.

Patch looks ok otherwise. Thanks.

Quoted 15 lines
drivers/vhost/scsi.c +4 -1
+	 * remaining data starts at offset in that page.
+	 */
  	while (bytes) {
-		n = min_t(unsigned int, PAGE_SIZE, bytes);
+		n = min_t(unsigned int, PAGE_SIZE - offset, bytes);
 
  		put_page(pages[npages++]);
  		bytes -= n;
+		offset = 0;
  	}
 
  	return -EINVAL;
Signature · 1 line
2.47.0
author
#3 lore raw
Quoted 9 lines · Jia Jia
Quoted 7 lines
drivers/vhost/scsi.c +0 -0
index 4f8c0260bc9e..b7e41c0a91d4 100644
@@ -883,11 +883,16 @@ vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd,
      vhost_scsi_revert_map_iov_to_sgl(iter, *sgl, sg);
 
Quoted 6 lines

drivers/vhost/scsi.c vhost_scsi_map_to_sgl(struct vhost_scsi_cmd *cmd, · line 887

Quoted 3 lines
drivers/vhost/scsi.c +2 -0
      iov_iter_revert(iter, bytes);
+     /*
+      * pages[npages] is the first page not added to the SG list.  The

Just a minor nit. There's an extra space before "The" in the comment.

Thanks for pointing this out. I'll remove it and send a v2.

Quoted 21 lines

Patch looks ok otherwise. Thanks.

Quoted 15 lines
drivers/vhost/scsi.c +4 -2
+      * remaining data starts at offset in that page.
+      */
      while (bytes) {
-             n = min_t(unsigned int, PAGE_SIZE, bytes);
+             n = min_t(unsigned int, PAGE_SIZE - offset, bytes);
 
              put_page(pages[npages++]);
              bytes -= n;
+             offset = 0;
      }
 
      return -EINVAL;
--

2.47.0