Open mkaniewski opened 5 years ago
How did you patch the code exactly?
Have you tried the same experiment on stable/11? Your 11.2-release may contain outdated code. I tried your program on HEAD, and it does not segfault (although it does not really allocate all those buffers, because it runs out of memory).
I tested my program on STABLE/11 and it still crashes with SEGFAULT. The patch which allows to request for 2 000 000 extra buffers (I set it to 4 000 000 millions, to have some reserve) is:
Index: sys/dev/netmap/netmap_mem2.c
===================================================================
--- sys/dev/netmap/netmap_mem2.c (revision 345457)
+++ sys/dev/netmap/netmap_mem2.c (working copy)
@@ -551,7 +551,7 @@
.objminsize = 64,
.objmaxsize = 65536,
.nummin = 4,
- .nummax = 1000000, /* one million! */
+ .nummax = 4000000, /* four million! */
},
},
After some digging I was able to find a source of the problem. It is caused by the fact that netmap use 32 bit integers in multiple places. Therefore if I request a big enough number of buffers then an integer overflow may occur.
For example take a look how netmap_obj_pool.memtotal
is calculated in netmap_finalize_obj_allocator()
:
p->memtotal = p->numclusters * p->_clustsize;
In my case p->numclusters
is equal 2 000 000
and p->_clustsize
is 4096
so result should be 8 192 000 000
. Instead it is 3 897 032 704
because this is the result after overflow on uint32. It is also visible in system logs when you try to run the program (sysctl dev.netmap.verbose must be enabled):
Mar 25 14:29:59 freebsd11 kernel: 598.988402 [1481] netmap_finalize_obj_allocator Pre-allocated 2000000 clusters (4/3805696KB) for 'netmap_buf'
To solve this issue I had to change some of the variable types to size_t
. I would like to change more variables to this type but I am not sure how it will impact netmap internals. In attachement there is a file with my current changes.
Let me know what do you think about it and could we uptsream it. Thanks.
Thanks for spotting the issue. Feel free to open a pull request with your changes. We have some unit tests and integration tests to catch regressions.
On Mon, Mar 25, 2019, 5:46 PM mkaniewski notifications@github.com wrote:
I tested my program on STABLE/11 and it still crashes with SEGFAULT. The patch which allows to request for 2 000 000 extra buffers (I set it to 4 000 000 millions, to have some reserve) is:
Index: sys/dev/netmap/netmap_mem2.c
--- sys/dev/netmap/netmap_mem2.c (revision 345457) +++ sys/dev/netmap/netmap_mem2.c (working copy) @@ -551,7 +551,7 @@ .objminsize = 64, .objmaxsize = 65536, .nummin = 4,
- .nummax = 1000000, / one million! /
- .nummax = 4000000, / four million! / }, },
After some digging I was able to find a source of the problem. It is caused by the fact that netmap use 32 bit integers in multiple places. Therefore if I request a big enough number of buffers then an integer overflow may occur. For example take a look how netmap_obj_pool.memtotal is calculated in netmap_finalize_obj_allocator():
p->memtotal = p->numclusters * p->_clustsize;
In my case p->numclusters is equal 2 000 000 and p->_clustsize is 4096 so result should be 8 192 000 000. Instead it is 3 897 032 704 because this is the result after overflow on uint32. It is also visible in system logs when you try to run the program (sysctl dev.netmap.verbose must be enabled):
Mar 25 14:29:59 freebsd11 kernel: 598.988402 [1481] netmap_finalize_obj_allocator Pre-allocated 2000000 clusters (4/3805696KB) for 'netmap_buf'
To solve this issue I had to change some of the variable types to size_t. I would like to change more variables to this type but I am not sure how it will impact netmap internals. In attachement there is a file with changes.
diff.txt https://github.com/luigirizzo/netmap/files/3004435/diff.txt
Let me know what do you think about it and could we uptsream it. Thanks.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/luigirizzo/netmap/issues/602#issuecomment-476283484, or mute the thread https://github.com/notifications/unsubscribe-auth/AEsSwQ13K4pD_iCeHnxbBEICYkhDwqPtks5vaP12gaJpZM4cCOIt .
I took the provided diff and made PR #637, as we've just run into the segfault issue as well.
Hi,
I have a program which allocates a lot of extra netmap buffers. When there is a lot of them requested ( > 2 millions) the netmap returns them succesfully but I get a segmentation fault when I try to dereference the buffer pointer. I prepared a sample application that shows the problem:
On FreeBSD 11.2 it ends with segmentation fault on first
fprintf
in the loop. In "/var/log/messages" there is a message:which shows that kernel successfully allocated the buffers (to make such a big allocation I had to increase the buffer limit hardcoded in netmap_mem2.c).
These are my sysctls:
I will be glad to hear any idea why such a huge allocation fails. Thanks.