Skip to contentSkip to conversation

common: Use posix_memalign for allocating userptr buffers

PATCH In review Brandon Cheo Fusi 4 participants 4 messages Jun 25, 2024 – Sep 10, 2025 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 common: Use posix_memalign for allocating userptr buffers

When dealing with a userptr pointing to a buffer in userspace, videobuf2 swaps the corresponding physical pages with other pages so we have a contiguous area of memory for DMA. This only works if the userptr is page aligned.

The current way of allocating user buffers using malloc only guarantees alignment up to alignof(max_align_t), which is usually
16. So replace malloc with posix_memalign to ensure the returned pointer is on a page boundary.

---
 utils/common/v4l-helpers.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
utils/common/v4l-helpers.h +5 -4
index cf0e92d..aa9ee26 100644
@@ -1661,10 +1661,11 @@ static inline int v4l_queue_alloc_bufs(struct v4l_fd *f,
 		return 0;
 	for (b = from; b < v4l_queue_g_buffers(q); b++) {
 		for (p = 0; p < v4l_queue_g_num_planes(q); p++) {
-			void *m = malloc(v4l_queue_g_length(q, p));
-
-			if (m == NULL)
-				return errno;
+			void *m;
+			int ret = posix_memalign(&m, getpagesize(),
+						v4l_queue_g_length(q, p));
+			if (ret)
+				return ret;
 			v4l_queue_s_userptr(q, b, p, m);
 		}
 	}
Signature · 1 line
2.39.2
#1 lore raw

Hi Brandon,

Quoted 9 lines · Brandon Cheo Fusi

When dealing with a userptr pointing to a buffer in userspace, videobuf2 swaps the corresponding physical pages with other pages so we have a contiguous area of memory for DMA. This only works if the userptr is page aligned.

The current way of allocating user buffers using malloc only guarantees alignment up to alignof(max_align_t), which is usually
16. So replace malloc with posix_memalign to ensure the returned pointer is on a page boundary.

With which driver is this tested? USERPTR is really supposed to work with malloc()ed memory.

There have been drivers in the past that used contiguous DMA but still allowed USERPTR, but that was a hack and these days you would use DMABUF.

Regards,

Hans

Quoted 26 lines
---
 utils/common/v4l-helpers.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
utils/common/v4l-helpers.h +5 -4
index cf0e92d..aa9ee26 100644
@@ -1661,10 +1661,11 @@ static inline int v4l_queue_alloc_bufs(struct v4l_fd *f,
 		return 0;
 	for (b = from; b < v4l_queue_g_buffers(q); b++) {
 		for (p = 0; p < v4l_queue_g_num_planes(q); p++) {
-			void *m = malloc(v4l_queue_g_length(q, p));
-
-			if (m == NULL)
-				return errno;
+			void *m;
+			int ret = posix_memalign(&m, getpagesize(),
+						v4l_queue_g_length(q, p));
+			if (ret)
+				return ret;
 			v4l_queue_s_userptr(q, b, p, m);
 		}
 	}
#2 lore raw
Quoted 9 lines · Hans Verkuil

Hi Brandon,

Quoted 5 lines · Brandon Cheo Fusi

When dealing with a userptr pointing to a buffer in userspace, videobuf2 swaps the corresponding physical pages with other pages so we have a contiguous area of memory for DMA. This only works if the userptr is page aligned.

Quoted 7 lines
Quoted 4 lines

The current way of allocating user buffers using malloc only guarantees alignment up to alignof(max_align_t), which is usually
16. So replace malloc with posix_memalign to ensure the returned pointer is on a page boundary.

With which driver is this tested? USERPTR is really supposed to work with malloc()ed memory.

In theory yes, but the entire idea of USERPTR is shaky enough that some hardware may not work if the pointer is not aligned enough. Another aspect is security - IOMMUs can only work on page granularity, so if the first page of the buffer is preceded (or last page followed) by some other data, that data would be leaked to the hardware.

That said, that doesn't seem to be what the commit description refers to and actually it's kind of wrong - videobuf2 doesn't do anything to make the userptr memory contiguous. It's all down to the DMA mapping ops of the device, and if it's behind an IOMMU, it should just work regardless of the alignment, minus the user data leak caveat I mentioned.

Besides that, any idea why this function actually even uses userptr? (I assume this is libv4l?)

Quoted 35 lines

There have been drivers in the past that used contiguous DMA but still allowed USERPTR, but that was a hack and these days you would use DMABUF.

Regards,

Hans

Quoted 26 lines
---
 utils/common/v4l-helpers.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
utils/common/v4l-helpers.h +5 -4
index cf0e92d..aa9ee26 100644
@@ -1661,10 +1661,11 @@ static inline int v4l_queue_alloc_bufs(struct v4l_fd *f,
              return 0;
      for (b = from; b < v4l_queue_g_buffers(q); b++) {
              for (p = 0; p < v4l_queue_g_num_planes(q); p++) {
-                     void *m = malloc(v4l_queue_g_length(q, p));
-
-                     if (m == NULL)
-                             return errno;
+                     void *m;
+                     int ret = posix_memalign(&m, getpagesize(),
+                                             v4l_queue_g_length(q, p));
+                     if (ret)
+                             return ret;
                      v4l_queue_s_userptr(q, b, p, m);
              }
      }
#3 lore raw

Hi Brandon,

There have been no replies to the questions raised for this patch, so I am marking it as Rejected in patchwork.

If you believe this is really needed, then just repost and give some more information.

Regards,

Hans

Quoted 70 lines · Tomasz Figa
Quoted 9 lines · Hans Verkuil

Hi Brandon,

Quoted 5 lines · Brandon Cheo Fusi

When dealing with a userptr pointing to a buffer in userspace, videobuf2 swaps the corresponding physical pages with other pages so we have a contiguous area of memory for DMA. This only works if the userptr is page aligned.

Quoted 7 lines
Quoted 4 lines

The current way of allocating user buffers using malloc only guarantees alignment up to alignof(max_align_t), which is usually
16. So replace malloc with posix_memalign to ensure the returned pointer is on a page boundary.

With which driver is this tested? USERPTR is really supposed to work with malloc()ed memory.

In theory yes, but the entire idea of USERPTR is shaky enough that some hardware may not work if the pointer is not aligned enough. Another aspect is security - IOMMUs can only work on page granularity, so if the first page of the buffer is preceded (or last page followed) by some other data, that data would be leaked to the hardware.

That said, that doesn't seem to be what the commit description refers to and actually it's kind of wrong - videobuf2 doesn't do anything to make the userptr memory contiguous. It's all down to the DMA mapping ops of the device, and if it's behind an IOMMU, it should just work regardless of the alignment, minus the user data leak caveat I mentioned.

Besides that, any idea why this function actually even uses userptr? (I assume this is libv4l?)

Quoted 35 lines

There have been drivers in the past that used contiguous DMA but still allowed USERPTR, but that was a hack and these days you would use DMABUF.

Regards,

Hans

Quoted 26 lines
---
 utils/common/v4l-helpers.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
utils/common/v4l-helpers.h +5 -4
index cf0e92d..aa9ee26 100644
@@ -1661,10 +1661,11 @@ static inline int v4l_queue_alloc_bufs(struct v4l_fd *f,
              return 0;
      for (b = from; b < v4l_queue_g_buffers(q); b++) {
              for (p = 0; p < v4l_queue_g_num_planes(q); p++) {
-                     void *m = malloc(v4l_queue_g_length(q, p));
-
-                     if (m == NULL)
-                             return errno;
+                     void *m;
+                     int ret = posix_memalign(&m, getpagesize(),
+                                             v4l_queue_g_length(q, p));
+                     if (ret)
+                             return ret;
                      v4l_queue_s_userptr(q, b, p, m);
              }
      }