Skip to content

Commit a023030

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 f72ad05 commit a023030

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() - Conditionally replace an entry in the XArray.
17461743
* @xa: XArray.
@@ -1767,7 +1764,29 @@ void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
17671764
}
17681765
EXPORT_SYMBOL(__xa_cmpxchg);
17691766

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

17881807
return xas_result(&xas, curr);
17891808
}
1809+
EXPORT_SYMBOL(__xa_cmpxchg_raw);
17901810

17911811
/**
17921812
* __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)