1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

libdm: Introduce dm_malloc_aligned

This commit is contained in:
Alasdair G Kergon 2018-01-09 20:25:43 +00:00
parent 943b217797
commit ea96381534
6 changed files with 51 additions and 3 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.147 - Version 1.02.147 -
===================================== =====================================
Introduce dm_malloc_aligned for page alignment of buffers.
Version 1.02.146 - 18th December 2017 Version 1.02.146 - 18th December 2017
===================================== =====================================

View File

@ -257,6 +257,13 @@ static int _aligned_io(struct device_area *where, char *buffer,
/* Perform the I/O directly. */ /* Perform the I/O directly. */
return _io(where, buffer, should_write, reason); return _io(where, buffer, should_write, reason);
#ifndef DEBUG_MEM
/* Allocate a bounce buffer with an extra block */
if (!(bounce_buf = bounce = dm_malloc_aligned((size_t) widened.size, 0))) {
log_error("Bounce buffer malloc failed");
return 0;
}
#else
/* Allocate a bounce buffer with an extra block */ /* Allocate a bounce buffer with an extra block */
if (!(bounce_buf = bounce = dm_malloc((size_t) widened.size + block_size))) { if (!(bounce_buf = bounce = dm_malloc((size_t) widened.size + block_size))) {
log_error("Bounce buffer malloc failed"); log_error("Bounce buffer malloc failed");
@ -268,6 +275,7 @@ static int _aligned_io(struct device_area *where, char *buffer,
*/ */
if (((uintptr_t) bounce) & mask) if (((uintptr_t) bounce) & mask)
bounce = (char *) ((((uintptr_t) bounce) + mask) & ~mask); bounce = (char *) ((((uintptr_t) bounce) + mask) & ~mask);
#endif
/* Do we need to read into the bounce buffer? */ /* Do we need to read into the bounce buffer? */
if ((!should_write || buffer_was_widened) && if ((!should_write || buffer_was_widened) &&
@ -887,7 +895,7 @@ int dev_write(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t r
int dev_set(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, int value) int dev_set(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, int value)
{ {
size_t s; size_t s;
char buffer[4096] __attribute__((aligned(8))); char buffer[4096] __attribute__((aligned(4096)));
if (!dev_open(dev)) if (!dev_open(dev))
return_0; return_0;

View File

@ -318,7 +318,6 @@ static int _label_read(struct device *dev, uint64_t scan_sector, struct label **
{ {
struct lvmcache_info *info; struct lvmcache_info *info;
struct find_labeller_params *flp; struct find_labeller_params *flp;
char *readbuf = NULL;
if ((info = lvmcache_info_from_pvid(dev->pvid, dev, 1))) { if ((info = lvmcache_info_from_pvid(dev->pvid, dev, 1))) {
log_debug_devs("Reading label from lvmcache for %s", dev_name(dev)); log_debug_devs("Reading label from lvmcache for %s", dev_name(dev));

View File

@ -0,0 +1 @@
dm_malloc_aligned_wrapper

View File

@ -2075,6 +2075,8 @@ uint32_t dm_tree_get_cookie(struct dm_tree_node *node);
*/ */
void *dm_malloc_wrapper(size_t s, const char *file, int line) void *dm_malloc_wrapper(size_t s, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__)); __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_malloc_aligned_wrapper(size_t s, size_t a, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_zalloc_wrapper(size_t s, const char *file, int line) void *dm_zalloc_wrapper(size_t s, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__)); __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_realloc_wrapper(void *p, unsigned int s, const char *file, int line) void *dm_realloc_wrapper(void *p, unsigned int s, const char *file, int line)
@ -2086,6 +2088,7 @@ int dm_dump_memory_wrapper(void);
void dm_bounds_check_wrapper(void); void dm_bounds_check_wrapper(void);
#define dm_malloc(s) dm_malloc_wrapper((s), __FILE__, __LINE__) #define dm_malloc(s) dm_malloc_wrapper((s), __FILE__, __LINE__)
#define dm_malloc_aligned(s, a) dm_malloc_aligned_wrapper((s), (a), __FILE__, __LINE__)
#define dm_zalloc(s) dm_zalloc_wrapper((s), __FILE__, __LINE__) #define dm_zalloc(s) dm_zalloc_wrapper((s), __FILE__, __LINE__)
#define dm_strdup(s) dm_strdup_wrapper((s), __FILE__, __LINE__) #define dm_strdup(s) dm_strdup_wrapper((s), __FILE__, __LINE__)
#define dm_free(p) dm_free_wrapper(p) #define dm_free(p) dm_free_wrapper(p)

View File

@ -18,7 +18,6 @@
#ifdef VALGRIND_POOL #ifdef VALGRIND_POOL
#include "memcheck.h" #include "memcheck.h"
#endif #endif
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
@ -26,6 +25,8 @@ void *dm_malloc_aux(size_t s, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__)); __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_malloc_aux_debug(size_t s, const char *file, int line) void *dm_malloc_aux_debug(size_t s, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__)); __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
static void *_dm_malloc_aligned_aux(size_t s, size_t a, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_zalloc_aux(size_t s, const char *file, int line) void *dm_zalloc_aux(size_t s, const char *file, int line)
__attribute__((__malloc__)) __attribute__((__warn_unused_result__)); __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
void *dm_zalloc_aux_debug(size_t s, const char *file, int line) void *dm_zalloc_aux_debug(size_t s, const char *file, int line)
@ -286,6 +287,30 @@ void *dm_malloc_aux(size_t s, const char *file __attribute__((unused)),
return malloc(s); return malloc(s);
} }
/* Allocate size s with alignment a (or page size if 0) */
static void *_dm_malloc_aligned_aux(size_t s, size_t a, const char *file __attribute__((unused)),
int line __attribute__((unused)))
{
void *memptr;
int r;
if (!a)
a = getpagesize();
if (s > 50000000) {
log_error("Huge memory allocation (size %" PRIsize_t
") rejected - metadata corruption?", s);
return 0;
}
if ((r = posix_memalign(&memptr, a, s))) {
log_error("Failed to allocate %" PRIsize_t " bytes aligned to %d: %s", s, a, strerror(r));
return 0;
}
return memptr;
}
void *dm_zalloc_aux(size_t s, const char *file, int line) void *dm_zalloc_aux(size_t s, const char *file, int line)
{ {
void *ptr = dm_malloc_aux(s, file, line); void *ptr = dm_malloc_aux(s, file, line);
@ -303,6 +328,12 @@ void *dm_malloc_wrapper(size_t s, const char *file, int line)
return dm_malloc_aux_debug(s, file, line); return dm_malloc_aux_debug(s, file, line);
} }
void *dm_malloc_aligned_wrapper(size_t s, size_t a, const char *file, int line)
{
/* FIXME Implement alignment when debugging - currently just ignored */
return _dm_malloc_aux_debug(s, file, line);
}
void *dm_zalloc_wrapper(size_t s, const char *file, int line) void *dm_zalloc_wrapper(size_t s, const char *file, int line)
{ {
return dm_zalloc_aux_debug(s, file, line); return dm_zalloc_aux_debug(s, file, line);
@ -340,6 +371,11 @@ void *dm_malloc_wrapper(size_t s, const char *file, int line)
return dm_malloc_aux(s, file, line); return dm_malloc_aux(s, file, line);
} }
void *dm_malloc_aligned_wrapper(size_t s, size_t a, const char *file, int line)
{
return _dm_malloc_aligned_aux(s, a, file, line);
}
void *dm_zalloc_wrapper(size_t s, const char *file, int line) void *dm_zalloc_wrapper(size_t s, const char *file, int line)
{ {
return dm_zalloc_aux(s, file, line); return dm_zalloc_aux(s, file, line);