From a348899c24ca3dc9c778e46c5e7c0273aa379edf Mon Sep 17 00:00:00 2001 From: Alexander Gall Date: Tue, 23 Feb 2021 11:49:24 +0100 Subject: [PATCH] lib.ctable: fix hugepages allocation size mmap() silently rounds up the size of the memory region to a multiple of the huge page size. This causes munmap() to fail if called with the original value. --- src/lib/ctable.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/ctable.lua b/src/lib/ctable.lua index 6397f649c4..fb575efdc5 100644 --- a/src/lib/ctable.lua +++ b/src/lib/ctable.lua @@ -160,11 +160,14 @@ end -- hugepages, not this code. local try_huge_pages = true local huge_page_threshold = 1e6 +local huge_page_size = memory.get_huge_page_size() local function calloc(t, count) if count == 0 then return 0, 0 end local byte_size = ffi.sizeof(t) * count + local alloc_byte_size = byte_size local mem, err if try_huge_pages and byte_size > huge_page_threshold then + alloc_byte_size = ceil(byte_size/huge_page_size) * huge_page_size mem, err = S.mmap(nil, byte_size, 'read, write', 'private, anonymous, hugetlb') if not mem then @@ -179,7 +182,7 @@ local function calloc(t, count) if not mem then error("mmap failed: " .. tostring(err)) end end local ret = ffi.cast(ffi.typeof('$*', t), mem) - ffi.gc(ret, function (ptr) S.munmap(ptr, byte_size) end) + ffi.gc(ret, function (ptr) S.munmap(ptr, alloc_byte_size) end) return ret, byte_size end