Skip to content

Commit 71d7ef8

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 2b49922 commit 71d7ef8

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
@@ -195,25 +195,30 @@ INSTANTIATE_TEST_SUITE_P(devdaxProviderTest, umfProviderTest,
195195

196196
TEST_P(umfProviderTest, create_destroy) {}
197197

198-
TEST_P(umfProviderTest, alloc_page64_align_0) {
199-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_NONE);
198+
TEST_P(umfProviderTest, alloc_page_align_0) {
199+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_NONE);
200200
}
201201

202-
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
203-
test_alloc_free_success(provider.get(), page_plus_64, page_size / 2,
202+
TEST_P(umfProviderTest, alloc_2page_align_page_size) {
203+
test_alloc_free_success(provider.get(), 2 * page_size, page_size,
204204
PURGE_NONE);
205205
}
206206

207207
TEST_P(umfProviderTest, purge_lazy) {
208-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_LAZY);
208+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_LAZY);
209209
}
210210

211211
TEST_P(umfProviderTest, purge_force) {
212-
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_FORCE);
212+
test_alloc_free_success(provider.get(), page_size, 0, PURGE_FORCE);
213213
}
214214

215215
// negative tests using test_alloc_failure
216216

217+
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
218+
test_alloc_failure(provider.get(), page_plus_64, page_size / 2,
219+
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
220+
}
221+
217222
TEST_P(umfProviderTest, alloc_page64_align_page_minus_1_WRONG_ALIGNMENT_1) {
218223
test_alloc_failure(provider.get(), page_plus_64, page_size - 1,
219224
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
@@ -236,7 +241,8 @@ TEST_P(umfProviderTest, alloc_3_pages_WRONG_ALIGNMENT_3_pages) {
236241
}
237242

238243
TEST_P(umfProviderTest, alloc_WRONG_SIZE) {
239-
test_alloc_failure(provider.get(), -1, 0,
244+
size_t size = (size_t)(-1) & ~(page_size - 1);
245+
test_alloc_failure(provider.get(), size, 0,
240246
UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC,
241247
UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED);
242248
}

0 commit comments

Comments
 (0)