Skip to content

Commit 51cc3e6

Browse files
tamirdjannau
authored andcommitted
rust: xarray: add insert and reserve
Add `Guard::{insert,reserve}` and `Guard::{insert,reserve}_limit`, which are akin to `__xa_{alloc,insert}` in C. Note that unlike `xa_reserve` which only ensures that memory is allocated, the semantics of `Reservation` are stricter and require precise management of the reservation. Indices which have been reserved can still be overwritten with `Guard::store`, which allows for C-like semantics if desired. `__xa_cmpxchg_raw` is exported to facilitate the semantics described above. Signed-off-by: Tamir Duberstein <tamird@gmail.com>
1 parent 2bd6fb5 commit 51cc3e6

4 files changed

Lines changed: 447 additions & 7 deletions

File tree

include/linux/xarray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,8 @@ void *__xa_erase(struct xarray *, unsigned long index);
563563
void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
564564
void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
565565
void *entry, gfp_t);
566+
void *__xa_cmpxchg_raw(struct xarray *, unsigned long index, void *old,
567+
void *entry, gfp_t);
566568
int __must_check __xa_insert(struct xarray *, unsigned long index,
567569
void *entry, gfp_t);
568570
int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,

lib/xarray.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,9 +1738,6 @@ void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
17381738
}
17391739
EXPORT_SYMBOL(xa_store);
17401740

1741-
static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
1742-
void *old, void *entry, gfp_t gfp);
1743-
17441741
/**
17451742
* __xa_cmpxchg() - Store this entry in the XArray.
17461743
* @xa: XArray.
@@ -1764,7 +1761,29 @@ void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
17641761
}
17651762
EXPORT_SYMBOL(__xa_cmpxchg);
17661763

1767-
static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
1764+
/**
1765+
* __xa_cmpxchg_raw() - Conditionally replace an entry in the XArray.
1766+
* @xa: XArray.
1767+
* @index: Index into array.
1768+
* @old: Old value to test against.
1769+
* @entry: New value to place in array.
1770+
* @gfp: Memory allocation flags.
1771+
*
1772+
* You must already be holding the xa_lock when calling this function.
1773+
* It will drop the lock if needed to allocate memory, and then reacquire
1774+
* it afterwards.
1775+
*
1776+
* If the entry at @index is the same as @old, replace it with @entry.
1777+
* If the return value is equal to @old, then the exchange was successful.
1778+
*
1779+
* This function is the same as __xa_cmpxchg() except that it does not coerce
1780+
* XA_ZERO_ENTRY to NULL on egress.
1781+
*
1782+
* Context: Any context. Expects xa_lock to be held on entry. May
1783+
* release and reacquire xa_lock if @gfp flags permit.
1784+
* Return: The old value at this index or xa_err() if an error happened.
1785+
*/
1786+
void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
17681787
void *old, void *entry, gfp_t gfp)
17691788
{
17701789
XA_STATE(xas, xa, index);
@@ -1784,6 +1803,7 @@ static inline void *__xa_cmpxchg_raw(struct xarray *xa, unsigned long index,
17841803

17851804
return xas_result(&xas, curr);
17861805
}
1806+
EXPORT_SYMBOL(__xa_cmpxchg_raw);
17871807

17881808
/**
17891809
* __xa_insert() - Store this entry in the XArray if no entry is present.

rust/helpers/xarray.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
#include <linux/xarray.h>
44

5+
void *rust_helper_xa_zero_entry(void)
6+
{
7+
return XA_ZERO_ENTRY;
8+
}
9+
510
int rust_helper_xa_err(void *entry)
611
{
712
return xa_err(entry);

0 commit comments

Comments
 (0)