Skip to content

Commit 41e15e8

Browse files
committed
Fix handling arguments of devdax_alloc()
Fix handling arguments of devdax_alloc(): - both size and alignment have to be a multiple of the page size - alignment have to be a power of 2. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 11f2136 commit 41e15e8

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/provider/provider_devdax_memory.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,24 @@ static umf_result_t devdax_alloc(void *provider, size_t size, size_t alignment,
228228
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
229229
}
230230

231-
// alignment must be a power of two and a multiple of sizeof(void *)
232-
if (alignment &&
233-
((alignment & (alignment - 1)) || (alignment % sizeof(void *)))) {
234-
LOG_ERR("wrong alignment: %zu (not a power of 2 or a multiple of "
235-
"sizeof(void *))",
236-
alignment);
231+
// alignment must be a power of two and a multiple or a divider of the page size
232+
if (alignment && ((alignment & (alignment - 1)) ||
233+
((alignment % DEVDAX_PAGE_SIZE_2MB) &&
234+
(DEVDAX_PAGE_SIZE_2MB % alignment)))) {
235+
LOG_ERR("wrong alignment: %zu (not a power of 2 or a multiple or a "
236+
"divider of the page size (%zu))",
237+
alignment, DEVDAX_PAGE_SIZE_2MB);
237238
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
238239
}
239240

241+
if (IS_NOT_ALIGNED(alignment, DEVDAX_PAGE_SIZE_2MB)) {
242+
alignment = ALIGN_UP(alignment, DEVDAX_PAGE_SIZE_2MB);
243+
}
244+
245+
if (IS_NOT_ALIGNED(size, DEVDAX_PAGE_SIZE_2MB)) {
246+
size = ALIGN_UP(size, DEVDAX_PAGE_SIZE_2MB);
247+
}
248+
240249
devdax_memory_provider_t *devdax_provider =
241250
(devdax_memory_provider_t *)provider;
242251

test/provider_devdax_memory.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,30 @@ INSTANTIATE_TEST_SUITE_P(devdaxProviderTest, umfProviderTest,
190190

191191
TEST_P(umfProviderTest, create_destroy) {}
192192

193-
TEST_P(umfProviderTest, alloc_page64_align_0) {
194-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_NONE);
193+
TEST_P(umfProviderTest, alloc_page_align_0) {
194+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_NONE);
195195
}
196196

197-
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
198-
test_alloc_free_success(provider.get(), page_plus_64, page_size / 2,
197+
TEST_P(umfProviderTest, alloc_2page_align_page_size) {
198+
test_alloc_free_success(provider.get(), 2 * page_size, page_size,
199199
PURGE_NONE);
200200
}
201201

202202
TEST_P(umfProviderTest, purge_lazy) {
203-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_LAZY);
203+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_LAZY);
204204
}
205205

206206
TEST_P(umfProviderTest, purge_force) {
207-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_FORCE);
207+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_FORCE);
208208
}
209209

210210
// negative tests using test_alloc_failure
211211

212+
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
213+
test_alloc_failure(provider.get(), page_plus_64, page_size / 2,
214+
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
215+
}
216+
212217
TEST_P(umfProviderTest, alloc_page64_align_page_minus_1_WRONG_ALIGNMENT_1) {
213218
test_alloc_failure(provider.get(), page_plus_64, page_size - 1,
214219
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
@@ -231,7 +236,8 @@ TEST_P(umfProviderTest, alloc_3_pages_WRONG_ALIGNMENT_3_pages) {
231236
}
232237

233238
TEST_P(umfProviderTest, alloc_WRONG_SIZE) {
234-
test_alloc_failure(provider.get(), -1, 0,
239+
size_t size = (size_t)(-1) & ~(page_size - 1);
240+
test_alloc_failure(provider.get(), size, 0,
235241
UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC,
236242
UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED);
237243
}

0 commit comments

Comments
 (0)