Skip to content
Snippets Groups Projects
  • Nadav Har'El's avatar
    c8845bb5
    malloc: Fix small allocations with alignment > size. · c8845bb5
    Nadav Har'El authored
    
    When a small allocation is requested with large alignment, we ignored
    the alignment, and as a consequence posix_memalign() or
    alloc_phys_contiguous_aligned() could crash when it failed to achieve
    the desired alignment. This is not a common case (usually, size >= alignment,
    and the new C11 aligned_alloc() even supports only this case), but still
    it might happen, and we saw it in cloudius-systems/capstan#75.
    
    When size < alignment, this patch changes the size so we can achieve the
    desired alignment. For small alignments, this means setting size=alignment,
    so for example to get an alignment of 1024 bytes we need at least 1024-byte
    allocation. This is a waste of memory, but as these allocations are rare,
    we expect this to be acceptable. For large alignments, e.g., alignment=8192,
    we don't need size=alignment but we do need size to be large enough so we'll
    use malloc_large() (malloc_large() already supports arbitrarily large
    alignments).
    
    This patch also adds test cases to tst-align.so to test alignments larger
    than the desired size.
    
    Fixes #271 and cloudius-systems/capstan#75.
    
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
    Signed-off-by: default avatarAvi Kivity <avi@cloudius-systems.com>
    c8845bb5
    History
    malloc: Fix small allocations with alignment > size.
    Nadav Har'El authored
    
    When a small allocation is requested with large alignment, we ignored
    the alignment, and as a consequence posix_memalign() or
    alloc_phys_contiguous_aligned() could crash when it failed to achieve
    the desired alignment. This is not a common case (usually, size >= alignment,
    and the new C11 aligned_alloc() even supports only this case), but still
    it might happen, and we saw it in cloudius-systems/capstan#75.
    
    When size < alignment, this patch changes the size so we can achieve the
    desired alignment. For small alignments, this means setting size=alignment,
    so for example to get an alignment of 1024 bytes we need at least 1024-byte
    allocation. This is a waste of memory, but as these allocations are rare,
    we expect this to be acceptable. For large alignments, e.g., alignment=8192,
    we don't need size=alignment but we do need size to be large enough so we'll
    use malloc_large() (malloc_large() already supports arbitrarily large
    alignments).
    
    This patch also adds test cases to tst-align.so to test alignments larger
    than the desired size.
    
    Fixes #271 and cloudius-systems/capstan#75.
    
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
    Signed-off-by: default avatarAvi Kivity <avi@cloudius-systems.com>