Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vfn/support: introduce rwlock and convert mutex to rwlock for IOMMU look-up #30

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/vfn/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern "C" {
#include <vfn/support/mem.h>
#include <vfn/support/mmio.h>
#include <vfn/support/mutex.h>
#include <vfn/support/rwlock.h>
#include <vfn/support/timer.h>
#include <vfn/support/ticks.h>

Expand Down
1 change: 1 addition & 0 deletions include/vfn/support/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vfn_support_headers = files([
'mem.h',
'mmio.h',
'mutex.h',
'rwlock.h',
'ticks.h',
'timer.h',
])
Expand Down
65 changes: 65 additions & 0 deletions include/vfn/support/rwlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later or MIT */

/*
* This file is part of libvfn.
*
* Copyright (C) 2022 The libvfn Authors. All Rights Reserved.
*
* This library (libvfn) is dual licensed under the GNU Lesser General
* Public License version 2.1 or later or the MIT license. See the
* COPYING and LICENSE files for more information.
*/

#ifndef LIBVFN_SUPPORT_RWLOCK_H
#define LIBVFN_SUPPORT_RWLOCK_H

/*
* DOC: Autolockable read-write lock
*
* Define a __autolock() macro that will lock the given rwlock as well as ensure
* that it is unlocked when going out of scope. This is inspired by the
* polymorphic locking functions in QEMU (include/qemu/lockable.h), but this
* version only supports the pthread_rwlock_t.
*/

static inline pthread_rwlock_t *__rwlock_auto_rdlock(pthread_rwlock_t *rwlock)
{
pthread_rwlock_rdlock(rwlock);
return rwlock;
}

static inline pthread_rwlock_t *__rwlock_auto_wrlock(pthread_rwlock_t *rwlock)
{
pthread_rwlock_wrlock(rwlock);
return rwlock;
}

static inline void __rwlock_auto_unlock(pthread_rwlock_t *rwlock)
{
if (rwlock)
pthread_rwlock_unlock(rwlock);
}

DEFINE_AUTOPTR(pthread_rwlock_t, __rwlock_auto_unlock)

/**
* __autordlock - autolock rdlock
* @x: pointer to pthread rdlock
*
* Lock the rdlock and unlock it automatically when going out of scope.
*/
#define __autordlock(x) \

Check failure on line 51 in include/vfn/support/rwlock.h

View workflow job for this annotation

GitHub Actions / check-patch

Macros with complex values should be enclosed in parentheses
__autoptr(pthread_rwlock_t) \
glue(autordlock, __COUNTER__) __attribute__((__unused__)) = __rwlock_auto_rdlock(x)

/**
* __autowrlock - autolock wrlock
* @x: pointer to pthread wrlock
*
* Lock the wrlock and unlock it automatically when going out of scope.
*/
#define __autowrlock(x) \

Check failure on line 61 in include/vfn/support/rwlock.h

View workflow job for this annotation

GitHub Actions / check-patch

Macros with complex values should be enclosed in parentheses
__autoptr(pthread_rwlock_t) \
glue(autowrlock, __COUNTER__) __attribute__((__unused__)) = __rwlock_auto_wrlock(x)

#endif /* LIBVFN_SUPPORT_RWLOCK_H */
4 changes: 1 addition & 3 deletions src/iommu/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ void iommu_ctx_init(struct iommu_ctx *ctx)
ctx->iova_ranges[0].start = IOVA_MIN;
ctx->iova_ranges[0].last = IOVA_MAX_39BITS - 1;

pthread_mutex_init(&ctx->lock, NULL);

skiplist_init(&ctx->map.list);
pthread_mutex_init(&ctx->map.lock, NULL);
pthread_rwlock_init(&ctx->map.lock, NULL);
}
3 changes: 1 addition & 2 deletions src/iommu/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ struct iova_mapping {
};

struct iova_map {
pthread_mutex_t lock;
pthread_rwlock_t lock;
struct skiplist list;
};

struct iommu_ctx {
struct iova_map map;
struct iommu_ctx_ops ops;

pthread_mutex_t lock;
int nranges;
struct iommu_iova_range *iova_ranges;
};
Expand Down
8 changes: 4 additions & 4 deletions src/iommu/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static int iova_cmp(const void *vaddr, const struct skiplist_node *n)
static int iova_map_add(struct iova_map *map, void *vaddr, size_t len, uint64_t iova,
unsigned long flags)
{
__autolock(&map->lock);
__autowrlock(&map->lock);

struct skiplist_node *update[SKIPLIST_LEVELS] = {};
struct iova_mapping *m;
Expand Down Expand Up @@ -70,7 +70,7 @@ static int iova_map_add(struct iova_map *map, void *vaddr, size_t len, uint64_t

static void iova_map_remove(struct iova_map *map, void *vaddr)
{
__autolock(&map->lock);
__autowrlock(&map->lock);

struct skiplist_node *n, *update[SKIPLIST_LEVELS] = {};

Expand All @@ -83,15 +83,15 @@ static void iova_map_remove(struct iova_map *map, void *vaddr)

static struct iova_mapping *iova_map_find(struct iova_map *map, void *vaddr)
{
__autolock(&map->lock);
__autordlock(&map->lock);

return container_of_or_null(skiplist_find(&map->list, vaddr, iova_cmp, NULL),
struct iova_mapping, list);
}

static void iova_map_clear_with(struct iova_map *map, skiplist_iter_fn fn, void *opaque)
{
__autolock(&map->lock);
__autowrlock(&map->lock);

skiplist_clear_with(&map->list, fn, opaque);
}
Expand Down
Loading