Skip to contentSkip to conversation

io_uring/rsrc: disallow multi-source reg buffers

PATCH In review Pavel Begunkov 3 participants 4 messages Feb 20, 2023 – Feb 21, 2023 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 io_uring/rsrc: disallow multi-source reg buffers

If two or more mappings go back to back to each other they can be passed into io_uring to be registered as a single registered buffer. That would even work if mappings came from different sources, e.g. it's possible to mix in this way anon pages and pages from shmem or hugetlb. That is not a problem but it'd rather be less prone if we forbid such mixing.

---
 io_uring/rsrc.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
io_uring/rsrc.c +8 -7
index a59fc02de598..70d7f94670f9 100644
@@ -1162,18 +1162,19 @@ struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
 	pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
 			      pages, vmas);
 	if (pret == nr_pages) {
+		struct file *file = vmas[0]->vm_file;
+
 		/* don't support file backed memory */
 		for (i = 0; i < nr_pages; i++) {
-			struct vm_area_struct *vma = vmas[i];
-
-			if (vma_is_shmem(vma))
+			if (vmas[i]->vm_file != file)
+				break;
+			if (!file)
 				continue;
-			if (vma->vm_file &&
-			    !is_file_hugepages(vma->vm_file)) {
-				ret = -EOPNOTSUPP;
+			if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file))
 				break;
-			}
 		}
+		if (i != nr_pages)
+			ret = -EOPNOTSUPP;
 		*npages = nr_pages;
 	} else {
 		ret = pret < 0 ? pret : -EFAULT;
Signature · 1 line
2.39.1
#1 lore raw
Quoted 19 lines · Pavel Begunkov

If two or more mappings go back to back to each other they can be passed into io_uring to be registered as a single registered buffer. That would even work if mappings came from different sources, e.g. it's possible to mix in this way anon pages and pages from shmem or hugetlb. That is not a problem but it'd rather be less prone if we forbid such mixing.

---
 io_uring/rsrc.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
io_uring/rsrc.c +0 -0
index a59fc02de598..70d7f94670f9 100644
@@ -1162,18 +1162,19 @@ struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
 	pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
 			      pages, vmas);

io_uring/rsrc.c struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages) · line 1170

Quoted 10 lines
io_uring/rsrc.c +4 -3
 	if (pret == nr_pages) {
+		struct file *file = vmas[0]->vm_file;
+
 		/* don't support file backed memory */
 		for (i = 0; i < nr_pages; i++) {
-			struct vm_area_struct *vma = vmas[i];
-
-			if (vma_is_shmem(vma))
+			if (vmas[i]->vm_file != file)
+				break;

Perhaps, return -EINVAL instead of -EOPNOTSUPP

Quoted 14 lines
io_uring/rsrc.c +4 -4
+			if (!file)
 				continue;
-			if (vma->vm_file &&
-			    !is_file_hugepages(vma->vm_file)) {
-				ret = -EOPNOTSUPP;
+			if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file))
 				break;
-			}
 		}
+		if (i != nr_pages)
+			ret = -EOPNOTSUPP;
 		*npages = nr_pages;
 	} else {
 		ret = pret < 0 ? pret : -EFAULT;
Signature · 1 line
Gabriel Krisman Bertazi
maintainer
#2 lore raw
Quoted 25 lines · Gabriel Krisman Bertazi
Quoted 23 lines · Pavel Begunkov

If two or more mappings go back to back to each other they can be passed into io_uring to be registered as a single registered buffer. That would even work if mappings came from different sources, e.g. it's possible to mix in this way anon pages and pages from shmem or hugetlb. That is not a problem but it'd rather be less prone if we forbid such mixing.

---
 io_uring/rsrc.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
io_uring/rsrc.c +2 -0
index a59fc02de598..70d7f94670f9 100644
@@ -1162,18 +1162,19 @@ struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
 	pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
 			      pages, vmas);
 	if (pret == nr_pages) {
+		struct file *file = vmas[0]->vm_file;
+
 		/* don't support file backed memory */
Quoted 8 lines

io_uring/rsrc.c struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages) · line 1170

Quoted 6 lines
io_uring/rsrc.c +2 -3
 		for (i = 0; i < nr_pages; i++) {
-			struct vm_area_struct *vma = vmas[i];
-
-			if (vma_is_shmem(vma))
+			if (vmas[i]->vm_file != file)
+				break;

Perhaps, return -EINVAL instead of -EOPNOTSUPP

Agree, -EOPNOTSUPP is OK for an unsupported case, but I think -EINVAL makes more sense for the weird case of mixed sources as it's unlikely to ever make sense to support that.

Signature · 1 line
Jens Axboe
#3 lore raw
Quoted 35 lines · Jens Axboe
Quoted 25 lines · Gabriel Krisman Bertazi
Quoted 23 lines · Pavel Begunkov

If two or more mappings go back to back to each other they can be passed into io_uring to be registered as a single registered buffer. That would even work if mappings came from different sources, e.g. it's possible to mix in this way anon pages and pages from shmem or hugetlb. That is not a problem but it'd rather be less prone if we forbid such mixing.

---
  io_uring/rsrc.c | 15 ++++++++-------
  1 file changed, 8 insertions(+), 7 deletions(-)
io_uring/rsrc.c +2 -0
index a59fc02de598..70d7f94670f9 100644
@@ -1162,18 +1162,19 @@ struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
  	pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
  			      pages, vmas);
  	if (pret == nr_pages) {
+		struct file *file = vmas[0]->vm_file;
+
  		/* don't support file backed memory */
Quoted 8 lines

io_uring/rsrc.c struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages) · line 1170

Quoted 6 lines
io_uring/rsrc.c +2 -3
  		for (i = 0; i < nr_pages; i++) {
-			struct vm_area_struct *vma = vmas[i];
-
-			if (vma_is_shmem(vma))
+			if (vmas[i]->vm_file != file)
+				break;

Perhaps, return -EINVAL instead of -EOPNOTSUPP

Quoted 3 lines

Agree, -EOPNOTSUPP is OK for an unsupported case, but I think -EINVAL makes more sense for the weird case of mixed sources as it's unlikely to ever make sense to support that.

ok

Signature · 1 line
Pavel Begunkov