2023-11-08 15:11:44 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <linux/log2.h>
|
|
|
|
#include <linux/slab.h>
|
2024-10-19 15:27:09 -07:00
|
|
|
#include <linux/vmalloc.h>
|
2023-11-08 15:11:44 -07:00
|
|
|
#include "darray.h"
|
|
|
|
|
2024-08-22 00:50:22 -07:00
|
|
|
int __bch2_darray_resize_noprof(darray_char *d, size_t element_size, size_t new_size, gfp_t gfp)
|
2023-11-08 15:11:44 -07:00
|
|
|
{
|
|
|
|
if (new_size > d->size) {
|
|
|
|
new_size = roundup_pow_of_two(new_size);
|
|
|
|
|
2024-10-19 15:27:09 -07:00
|
|
|
/*
|
|
|
|
* This is a workaround: kvmalloc() doesn't support > INT_MAX
|
|
|
|
* allocations, but vmalloc() does.
|
|
|
|
* The limit needs to be lifted from kvmalloc, and when it does
|
|
|
|
* we'll go back to just using that.
|
|
|
|
*/
|
|
|
|
size_t bytes;
|
|
|
|
if (unlikely(check_mul_overflow(new_size, element_size, &bytes)))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
void *data = likely(bytes < INT_MAX)
|
|
|
|
? kvmalloc_noprof(bytes, gfp)
|
|
|
|
: vmalloc_noprof(bytes);
|
2023-11-08 15:11:44 -07:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2024-06-21 13:29:32 -07:00
|
|
|
if (d->size)
|
|
|
|
memcpy(data, d->data, d->size * element_size);
|
2023-12-30 12:38:29 -07:00
|
|
|
if (d->data != d->preallocated)
|
|
|
|
kvfree(d->data);
|
2023-11-08 15:11:44 -07:00
|
|
|
d->data = data;
|
|
|
|
d->size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|