From 7c5897933f4f64594602b2e38264af705c061754 Mon Sep 17 00:00:00 2001 From: "Suren A. Chilingaryan" Date: Wed, 5 Aug 2015 18:11:59 +0200 Subject: Use global locks to protect kmem allocation to prevent race while allocating simmultaneously locking kmem pages and any other type of kmem --- pcilib/bar.c | 9 +++++---- pcilib/kmem.c | 6 +++--- pcilib/lock.c | 5 ++++- pcilib/locking.c | 6 +----- pcilib/locking.h | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pcilib/bar.c b/pcilib/bar.c index 17828a8..418f864 100644 --- a/pcilib/bar.c +++ b/pcilib/bar.c @@ -80,7 +80,7 @@ void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { if (ctx->bar_space[bar]) return ctx->bar_space[bar]; - err = pcilib_lock(ctx->locks.mmap); + err = pcilib_lock_global(ctx); if (err) { pcilib_error("Error (%i) acquiring mmap lock", err); return NULL; @@ -88,14 +88,14 @@ void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_PCI ); if (ret) { - pcilib_unlock(ctx->locks.mmap); + pcilib_unlock_global(ctx); pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed", bar); return NULL; } ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_AREA, PCIDRIVER_BAR0 + bar ); if (ret) { - pcilib_unlock(ctx->locks.mmap); + pcilib_unlock_global(ctx); pcilib_error("PCIDRIVER_IOC_MMAP_AREA ioctl have failed for bank %i", bar); return NULL; } @@ -106,7 +106,8 @@ void *pcilib_map_bar(pcilib_t *ctx, pcilib_bar_t bar) { #else res = mmap( 0, board_info->bar_length[bar], PROT_WRITE | PROT_READ, MAP_SHARED, ctx->handle, 0 ); #endif - pcilib_unlock(ctx->locks.mmap); + + pcilib_unlock_global(ctx); if ((!res)||(res == MAP_FAILED)) { pcilib_error("Failed to mmap data bank %i", bar); diff --git a/pcilib/kmem.c b/pcilib/kmem.c index 469c63a..b1d2c5c 100644 --- a/pcilib/kmem.c +++ b/pcilib/kmem.c @@ -80,7 +80,7 @@ pcilib_kmem_handle_t *pcilib_alloc_kernel_memory(pcilib_t *ctx, pcilib_kmem_type memset(kbuf, 0, sizeof(pcilib_kmem_list_t) + nmemb * sizeof(pcilib_kmem_addr_t)); - err = pcilib_lock(ctx->locks.mmap); + err = pcilib_lock_global(ctx); if (err) { pcilib_error("Error (%i) acquiring mmap lock", err); return NULL; @@ -88,7 +88,7 @@ pcilib_kmem_handle_t *pcilib_alloc_kernel_memory(pcilib_t *ctx, pcilib_kmem_type ret = ioctl( ctx->handle, PCIDRIVER_IOC_MMAP_MODE, PCIDRIVER_MMAP_KMEM ); if (ret) { - pcilib_unlock(ctx->locks.mmap); + pcilib_unlock_global(ctx); pcilib_error("PCIDRIVER_IOC_MMAP_MODE ioctl have failed"); return NULL; } @@ -176,7 +176,7 @@ pcilib_kmem_handle_t *pcilib_alloc_kernel_memory(pcilib_t *ctx, pcilib_kmem_type kbuf->buf.blocks[i].mmap_offset = kh.pa & ctx->page_mask; } - pcilib_unlock(ctx->locks.mmap); + pcilib_unlock_global(ctx); //This is possible in the case of error (nothing is allocated yet) or if buffers are not reused diff --git a/pcilib/lock.c b/pcilib/lock.c index f1cbc56..13e363a 100644 --- a/pcilib/lock.c +++ b/pcilib/lock.c @@ -140,7 +140,10 @@ const char *pcilib_lock_get_name(pcilib_lock_t *lock) { int pcilib_lock_custom(pcilib_lock_t *lock, pcilib_lock_flags_t flags, pcilib_timeout_t timeout) { int err; - if (!lock) return 0; + if (!lock) { + pcilib_error("The null lock pointer is passed to lock function"); + return PCILIB_ERROR_INVALID_ARGUMENT; + } struct timespec tm; diff --git a/pcilib/locking.c b/pcilib/locking.c index ffd05ce..7a32605 100644 --- a/pcilib/locking.c +++ b/pcilib/locking.c @@ -46,11 +46,10 @@ int pcilib_init_locking(pcilib_t* ctx) { } ctx->locks.locking = pcilib_get_lock(ctx, PCILIB_LOCK_FLAG_UNLOCKED, "locking"); - ctx->locks.mmap = pcilib_get_lock(ctx, PCILIB_LOCK_FLAG_UNLOCKED, "mmap"); pcilib_unlock_global(ctx); - if ((!ctx->locks.locking)||(!ctx->locks.mmap)) { + if ((!ctx->locks.locking)) { pcilib_error("Locking subsystem has failed to initialized mandatory global locks"); return PCILIB_ERROR_FAILED; } @@ -62,9 +61,6 @@ int pcilib_init_locking(pcilib_t* ctx) { * this functions destroy all locks and then free the kernel memory allocated for them */ void pcilib_free_locking(pcilib_t *ctx) { - if (ctx->locks.mmap) - pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, ctx->locks.mmap); - if (ctx->locks.locking) pcilib_return_lock(ctx, PCILIB_LOCK_FLAGS_DEFAULT, ctx->locks.locking); diff --git a/pcilib/locking.h b/pcilib/locking.h index ae2f368..ccacd63 100644 --- a/pcilib/locking.h +++ b/pcilib/locking.h @@ -20,7 +20,7 @@ typedef struct pcilib_locking_s pcilib_locking_t; struct pcilib_locking_s { pcilib_kmem_handle_t *kmem; /**< kmem used to store mutexes */ pcilib_lock_t *locking; /**< lock used while intializing other locks */ - pcilib_lock_t *mmap; /**< lock used to protect mmap operation */ +// pcilib_lock_t *mmap; /**< lock used to protect mmap operation */ }; #ifdef __cplusplus -- cgit v1.2.3