Commit cc46b955 authored by Matthew Sakai's avatar Matthew Sakai Committed by Mike Snitzer

dm vdo: add basic hash map data structures

This patch adds two hash maps, one keyed by integers, the other by
pointers, and also a priority heap. The integer map is used for locking of
logical and physical addresses. The pointer map is used for managing
concurrent writes of the same data, ensuring that those writes are
deduplicated. The priority heap is used to minimize the search time for
free blocks.
Co-developed-by: default avatarJ. corwin Coburn <corwin@hurlbutnet.net>
Signed-off-by: default avatarJ. corwin Coburn <corwin@hurlbutnet.net>
Co-developed-by: default avatarMichael Sclafani <dm-devel@lists.linux.dev>
Signed-off-by: default avatarMichael Sclafani <dm-devel@lists.linux.dev>
Signed-off-by: default avatarMatthew Sakai <msakai@redhat.com>
Signed-off-by: default avatarMike Snitzer <snitzer@kernel.org>
parent d9e894d9
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright 2023 Red Hat
*/
#ifndef VDO_INT_MAP_H
#define VDO_INT_MAP_H
#include <linux/compiler.h>
#include <linux/types.h>
/**
* DOC: int_map
*
* An int_map associates pointers (void *) with integer keys (u64). NULL pointer values are
* not supported.
*
* The map is implemented as hash table, which should provide constant-time insert, query, and
* remove operations, although the insert may occasionally grow the table, which is linear in the
* number of entries in the map. The table will grow as needed to hold new entries, but will not
* shrink as entries are removed.
*/
struct int_map;
int __must_check
vdo_make_int_map(size_t initial_capacity, unsigned int initial_load, struct int_map **map_ptr);
void vdo_free_int_map(struct int_map *map);
size_t vdo_int_map_size(const struct int_map *map);
void *vdo_int_map_get(struct int_map *map, u64 key);
int __must_check
vdo_int_map_put(struct int_map *map, u64 key, void *new_value, bool update, void **old_value_ptr);
void *vdo_int_map_remove(struct int_map *map, u64 key);
#endif /* VDO_INT_MAP_H */
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright 2023 Red Hat
*/
#ifndef VDO_POINTER_MAP_H
#define VDO_POINTER_MAP_H
#include <linux/compiler.h>
#include <linux/types.h>
/*
* A pointer_map associates pointer values (<code>void *</code>) with the data referenced by
* pointer keys (<code>void *</code>). <code>NULL</code> pointer values are not supported. A
* <code>NULL</code> key value is supported when the instance's key comparator and hasher functions
* support it.
*
* The map is implemented as hash table, which should provide constant-time insert, query, and
* remove operations, although the insert may occasionally grow the table, which is linear in the
* number of entries in the map. The table will grow as needed to hold new entries, but will not
* shrink as entries are removed.
*
* The key and value pointers passed to the map are retained and used by the map, but are not owned
* by the map. Freeing the map does not attempt to free the pointers. The client is entirely
* responsible for the memory management of the keys and values. The current interface and
* implementation assume that keys will be properties of the values, or that keys will not be
* memory managed, or that keys will not need to be freed as a result of being replaced when a key
* is re-mapped.
*/
struct pointer_map;
/**
* typedef pointer_key_comparator - The prototype of functions that compare the referents of two
* pointer keys for equality.
* @this_key: The first element to compare.
* @that_key: The second element to compare.
*
* If two keys are equal, then both keys must have the same the hash code associated with them by
* the hasher function defined below.
*
* Return: true if and only if the referents of the two key pointers are to be treated as the same
* key by the map.
*/
typedef bool pointer_key_comparator(const void *this_key, const void *that_key);
/**
* typedef pointer_key_hasher - The prototype of functions that get or calculate a hash code
* associated with the referent of pointer key.
* @key: The pointer key to hash.
*
* The hash code must be uniformly distributed over all u32 values. The hash code associated
* with a given key must not change while the key is in the map. If the comparator function says
* two keys are equal, then this function must return the same hash code for both keys. This
* function may be called many times for a key while an entry is stored for it in the map.
*
* Return: The hash code for the key.
*/
typedef u32 pointer_key_hasher(const void *key);
int __must_check vdo_make_pointer_map(size_t initial_capacity,
unsigned int initial_load,
pointer_key_comparator comparator,
pointer_key_hasher hasher,
struct pointer_map **map_ptr);
void vdo_free_pointer_map(struct pointer_map *map);
size_t vdo_pointer_map_size(const struct pointer_map *map);
void *vdo_pointer_map_get(struct pointer_map *map, const void *key);
int __must_check vdo_pointer_map_put(struct pointer_map *map,
const void *key,
void *new_value,
bool update,
void **old_value_ptr);
void *vdo_pointer_map_remove(struct pointer_map *map, const void *key);
#endif /* VDO_POINTER_MAP_H */
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2023 Red Hat
*/
#include "priority-table.h"
#include <linux/log2.h>
#include "errors.h"
#include "memory-alloc.h"
#include "permassert.h"
#include "status-codes.h"
/* We use a single 64-bit search vector, so the maximum priority is 63 */
enum {
MAX_PRIORITY = 63
};
/*
* All the entries with the same priority are queued in a circular list in a bucket for that
* priority. The table is essentially an array of buckets.
*/
struct bucket {
/*
* The head of a queue of table entries, all having the same priority
*/
struct list_head queue;
/* The priority of all the entries in this bucket */
unsigned int priority;
};
/*
* A priority table is an array of buckets, indexed by priority. New entries are added to the end
* of the queue in the appropriate bucket. The dequeue operation finds the highest-priority
* non-empty bucket by searching a bit vector represented as a single 8-byte word, which is very
* fast with compiler and CPU support.
*/
struct priority_table {
/* The maximum priority of entries that may be stored in this table */
unsigned int max_priority;
/* A bit vector flagging all buckets that are currently non-empty */
u64 search_vector;
/* The array of all buckets, indexed by priority */
struct bucket buckets[];
};
/**
* vdo_make_priority_table() - Allocate and initialize a new priority_table.
* @max_priority: The maximum priority value for table entries.
* @table_ptr: A pointer to hold the new table.
*
* Return: VDO_SUCCESS or an error code.
*/
int vdo_make_priority_table(unsigned int max_priority, struct priority_table **table_ptr)
{
struct priority_table *table;
int result;
unsigned int priority;
if (max_priority > MAX_PRIORITY)
return UDS_INVALID_ARGUMENT;
result = uds_allocate_extended(struct priority_table, max_priority + 1,
struct bucket, __func__, &table);
if (result != VDO_SUCCESS)
return result;
for (priority = 0; priority <= max_priority; priority++) {
struct bucket *bucket = &table->buckets[priority];
bucket->priority = priority;
INIT_LIST_HEAD(&bucket->queue);
}
table->max_priority = max_priority;
table->search_vector = 0;
*table_ptr = table;
return VDO_SUCCESS;
}
/**
* vdo_free_priority_table() - Free a priority_table.
* @table: The table to free.
*
* The table does not own the entries stored in it and they are not freed by this call.
*/
void vdo_free_priority_table(struct priority_table *table)
{
if (table == NULL)
return;
/*
* Unlink the buckets from any entries still in the table so the entries won't be left with
* dangling pointers to freed memory.
*/
vdo_reset_priority_table(table);
uds_free(table);
}
/**
* vdo_reset_priority_table() - Reset a priority table, leaving it in the same empty state as when
* newly constructed.
* @table: The table to reset.
*
* The table does not own the entries stored in it and they are not freed (or even unlinked from
* each other) by this call.
*/
void vdo_reset_priority_table(struct priority_table *table)
{
unsigned int priority;
table->search_vector = 0;
for (priority = 0; priority <= table->max_priority; priority++)
list_del_init(&table->buckets[priority].queue);
}
/**
* vdo_priority_table_enqueue() - Add a new entry to the priority table, appending it to the queue
* for entries with the specified priority.
* @table: The table in which to store the entry.
* @priority: The priority of the entry.
* @entry: The list_head embedded in the entry to store in the table (the caller must have
* initialized it).
*/
void vdo_priority_table_enqueue(struct priority_table *table, unsigned int priority,
struct list_head *entry)
{
ASSERT_LOG_ONLY((priority <= table->max_priority),
"entry priority must be valid for the table");
/* Append the entry to the queue in the specified bucket. */
list_move_tail(entry, &table->buckets[priority].queue);
/* Flag the bucket in the search vector since it must be non-empty. */
table->search_vector |= (1ULL << priority);
}
static inline void mark_bucket_empty(struct priority_table *table, struct bucket *bucket)
{
table->search_vector &= ~(1ULL << bucket->priority);
}
/**
* vdo_priority_table_dequeue() - Find the highest-priority entry in the table, remove it from the
* table, and return it.
* @table: The priority table from which to remove an entry.
*
* If there are multiple entries with the same priority, the one that has been in the table with
* that priority the longest will be returned.
*
* Return: The dequeued entry, or NULL if the table is currently empty.
*/
struct list_head *vdo_priority_table_dequeue(struct priority_table *table)
{
struct bucket *bucket;
struct list_head *entry;
int top_priority;
if (table->search_vector == 0) {
/* All buckets are empty. */
return NULL;
}
/*
* Find the highest priority non-empty bucket by finding the highest-order non-zero bit in
* the search vector.
*/
top_priority = ilog2(table->search_vector);
/* Dequeue the first entry in the bucket. */
bucket = &table->buckets[top_priority];
entry = bucket->queue.next;
list_del_init(entry);
/* Clear the bit in the search vector if the bucket has been emptied. */
if (list_empty(&bucket->queue))
mark_bucket_empty(table, bucket);
return entry;
}
/**
* vdo_priority_table_remove() - Remove a specified entry from its priority table.
* @table: The table from which to remove the entry.
* @entry: The entry to remove from the table.
*/
void vdo_priority_table_remove(struct priority_table *table, struct list_head *entry)
{
struct list_head *next_entry;
/*
* We can't guard against calls where the entry is on a list for a different table, but
* it's easy to deal with an entry not in any table or list.
*/
if (list_empty(entry))
return;
/*
* Remove the entry from the bucket list, remembering a pointer to another entry in the
* ring.
*/
next_entry = entry->next;
list_del_init(entry);
/*
* If the rest of the list is now empty, the next node must be the list head in the bucket
* and we can use it to update the search vector.
*/
if (list_empty(next_entry))
mark_bucket_empty(table, list_entry(next_entry, struct bucket, queue));
}
/**
* vdo_is_priority_table_empty() - Return whether the priority table is empty.
* @table: The table to check.
*
* Return: true if the table is empty.
*/
bool vdo_is_priority_table_empty(struct priority_table *table)
{
return (table->search_vector == 0);
}
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright 2023 Red Hat
*/
#ifndef VDO_PRIORITY_TABLE_H
#define VDO_PRIORITY_TABLE_H
#include <linux/list.h>
/*
* A priority_table is a simple implementation of a priority queue for entries with priorities that
* are small non-negative integer values. It implements the obvious priority queue operations of
* enqueuing an entry and dequeuing an entry with the maximum priority. It also supports removing
* an arbitrary entry. The priority of an entry already in the table can be changed by removing it
* and re-enqueuing it with a different priority. All operations have O(1) complexity.
*
* The links for the table entries must be embedded in the entries themselves. Lists are used to
* link entries in the table and no wrapper type is declared, so an existing list entry in an
* object can also be used to queue it in a priority_table, assuming the field is not used for
* anything else while so queued.
*
* The table is implemented as an array of queues (circular lists) indexed by priority, along with
* a hint for which queues are non-empty. Steven Skiena calls a very similar structure a "bounded
* height priority queue", but given the resemblance to a hash table, "priority table" seems both
* shorter and more apt, if somewhat novel.
*/
struct priority_table;
int __must_check vdo_make_priority_table(unsigned int max_priority,
struct priority_table **table_ptr);
void vdo_free_priority_table(struct priority_table *table);
void vdo_priority_table_enqueue(struct priority_table *table, unsigned int priority,
struct list_head *entry);
void vdo_reset_priority_table(struct priority_table *table);
struct list_head * __must_check vdo_priority_table_dequeue(struct priority_table *table);
void vdo_priority_table_remove(struct priority_table *table, struct list_head *entry);
bool __must_check vdo_is_priority_table_empty(struct priority_table *table);
#endif /* VDO_PRIORITY_TABLE_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment