Skip to contentSkip to conversation

contigmem: zero all pages during mmap

PATCH Superseded Jim Harris 2 participants 2 messages Aug 15, 2016 – Aug 16, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jim Harris

author
#0 lore raw

PATCH 1/1 contigmem: zero all pages during mmap

On Linux, all huge pages are zeroed by the kernel before first access by the DPDK application. But on FreeBSD, the contigmem driver would only zero the contiguous memory regions during initial driver load.

DPDK commit b78c91751 eliminated the explicit memset() operation for rte_zmalloc(), which was OK on Linux because the kernel zeroes the pages during app start, but this broke FreeBSD. So this patch explicitly zeroes the pages before they are mmap'd, to ensure equivalent behavior to Linux

---
 lib/librte_eal/bsdapp/contigmem/contigmem.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/librte_eal/bsdapp/contigmem/contigmem.c +6 -2
index c6ca3b9..da971de 100644
@@ -216,15 +216,19 @@ static int
 contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
 		struct vm_object **obj, int nprot)
 {
+	uint64_t buffer_index;
+
 	/*
 	 * The buffer index is encoded in the offset.  Divide the offset by
 	 *  PAGE_SIZE to get the index of the buffer requested by the user
 	 *  app.
 	 */
-	if ((*offset/PAGE_SIZE) >= contigmem_num_buffers)
+	buffer_index = *offset / PAGE_SIZE;
+	if (buffer_index >= contigmem_num_buffers)
 		return EINVAL;
 
-	*offset = (vm_ooffset_t)vtophys(contigmem_buffers[*offset/PAGE_SIZE]);
+	memset(contigmem_buffers[buffer_index], 0, contigmem_buffer_size);
+	*offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index]);
 	*obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
 			curthread->td_ucred);
 
Acked-by on v1 1/1
#1 lore raw
Quoted 37 lines · Jim Harris

On Linux, all huge pages are zeroed by the kernel before first access by the DPDK application. But on FreeBSD, the contigmem driver would only zero the contiguous memory regions during initial driver load.

DPDK commit b78c91751 eliminated the explicit memset() operation for rte_zmalloc(), which was OK on Linux because the kernel zeroes the pages during app start, but this broke FreeBSD. So this patch explicitly zeroes the pages before they are mmap'd, to ensure equivalent behavior to Linux

---
  lib/librte_eal/bsdapp/contigmem/contigmem.c |    8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
lib/librte_eal/bsdapp/contigmem/contigmem.c +2 -1
index c6ca3b9..da971de 100644
@@ -216,15 +216,19 @@ static int
  contigmem_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
  		struct vm_object **obj, int nprot)
  {
+	uint64_t buffer_index;
+
  	/*
  	 * The buffer index is encoded in the offset.  Divide the offset by
  	 *  PAGE_SIZE to get the index of the buffer requested by the user
  	 *  app.
  	 */
-	if ((*offset/PAGE_SIZE) >= contigmem_num_buffers)

lib/librte_eal/bsdapp/contigmem/contigmem.c static int · line 234

Quoted 10 lines
lib/librte_eal/bsdapp/contigmem/contigmem.c +4 -1
+	buffer_index = *offset / PAGE_SIZE;
+	if (buffer_index >= contigmem_num_buffers)
  		return EINVAL;
 
-	*offset = (vm_ooffset_t)vtophys(contigmem_buffers[*offset/PAGE_SIZE]);
+	memset(contigmem_buffers[buffer_index], 0, contigmem_buffer_size);
+	*offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index]);
  	*obj = vm_pager_allocate(OBJT_DEVICE, cdev, size, nprot, *offset,
  			curthread->td_ucred);
 

I think you should include the Fixes line:

Fixes b78c9175118f ("mem: do not zero out memory on zmalloc")

Other than that: