linux-nova icon indicating copy to clipboard operation
linux-nova copied to clipboard

Possible issues when allocate page-sized memory in snapshot management

Open iaoing opened this issue 1 year ago • 0 comments

Issue

In snapshot management, NOVA uses kmalloc to allocate the page-sized memory, and then checks the alignment, as the below code shows.

https://github.com/NVSL/linux-nova/blob/976a4d1f3d5282863b23aa834e02012167be6ee2/fs/nova/snapshot.c#L313-L320

However, the allocated memory space might not be aligned to the page size. I have encountered this situation where the allocation is successful but the alignment is not satisfied, but I am not sure whether it was caused by VMs or small DRAM size that was used in the VM.

The documentation of kernel-5.1 does not say the alignment is guaranteed (https://www.kernel.org/doc/html/v5.1/core-api/memory-allocation.html). There also have discussions regarding the alignment of kmalloc (https://lwn.net/Articles/787740/).

From kernel-5.4, the documentation confirms the alignment guarantee of kmalloc. However, it also suggests to use the page allocator for large allocations. The blow code is the APIs of the page allocator.

https://github.com/NVSL/linux-nova/blob/976a4d1f3d5282863b23aa834e02012167be6ee2/include/linux/gfp.h#L524-L555

Fix

Replacing kmalloc as __get_free_page and kfree as free_page, as below code shows

// new_page = (unsigned long)kmalloc(PAGE_SIZE, GFP_KERNEL);
new_page = __get_free_page(GFP_KERNEL);
if (!new_page || ENTRY_LOC(new_page)) {
    // kfree((void *)new_page);
    free_page((unsigned long)new_page);
    nova_err(sb, "%s: allocation failed\n", __func__);
    return -ENOMEM;
}

// and other places to fix

iaoing avatar Jan 02 '24 03:01 iaoing