Commit 9d21874d authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'idr-2018-02-06' of git://git.infradead.org/users/willy/linux-dax

Pull idr updates from Matthew Wilcox:

 - test-suite improvements

 - replace the extended API by improving the normal API

 - performance improvement for IDRs which are 1-based rather than
   0-based

 - add documentation

* 'idr-2018-02-06' of git://git.infradead.org/users/willy/linux-dax:
  idr: Add documentation
  idr: Make 1-based IDRs more efficient
  idr: Warn if old iterators see large IDs
  idr: Rename idr_for_each_entry_ext
  idr: Remove idr_alloc_ext
  cls_u32: Convert to idr_alloc_u32
  cls_u32: Reinstate cyclic allocation
  cls_flower: Convert to idr_alloc_u32
  cls_bpf: Convert to use idr_alloc_u32
  cls_basic: Convert to use idr_alloc_u32
  cls_api: Convert to idr_alloc_u32
  net sched actions: Convert to use idr_alloc_u32
  idr: Add idr_alloc_u32 helper
  idr: Delete idr_find_ext function
  idr: Delete idr_replace_ext function
  idr: Delete idr_remove_ext function
  IDR test suite: Check handling negative end correctly
  idr test suite: Fix ida_test_random()
  radix tree test suite: Remove ARRAY_SIZE
parents 4ed8244e ac665d94
.. SPDX-License-Identifier: CC-BY-SA-4.0
=============
ID Allocation
=============
:Author: Matthew Wilcox
Overview
========
A common problem to solve is allocating identifiers (IDs); generally
small numbers which identify a thing. Examples include file descriptors,
process IDs, packet identifiers in networking protocols, SCSI tags
and device instance numbers. The IDR and the IDA provide a reasonable
solution to the problem to avoid everybody inventing their own. The IDR
provides the ability to map an ID to a pointer, while the IDA provides
only ID allocation, and as a result is much more memory-efficient.
IDR usage
=========
Start by initialising an IDR, either with :c:func:`DEFINE_IDR`
for statically allocated IDRs or :c:func:`idr_init` for dynamically
allocated IDRs.
You can call :c:func:`idr_alloc` to allocate an unused ID. Look up
the pointer you associated with the ID by calling :c:func:`idr_find`
and free the ID by calling :c:func:`idr_remove`.
If you need to change the pointer associated with an ID, you can call
:c:func:`idr_replace`. One common reason to do this is to reserve an
ID by passing a ``NULL`` pointer to the allocation function; initialise the
object with the reserved ID and finally insert the initialised object
into the IDR.
Some users need to allocate IDs larger than ``INT_MAX``. So far all of
these users have been content with a ``UINT_MAX`` limit, and they use
:c:func:`idr_alloc_u32`. If you need IDs that will not fit in a u32,
we will work with you to address your needs.
If you need to allocate IDs sequentially, you can use
:c:func:`idr_alloc_cyclic`. The IDR becomes less efficient when dealing
with larger IDs, so using this function comes at a slight cost.
To perform an action on all pointers used by the IDR, you can
either use the callback-based :c:func:`idr_for_each` or the
iterator-style :c:func:`idr_for_each_entry`. You may need to use
:c:func:`idr_for_each_entry_continue` to continue an iteration. You can
also use :c:func:`idr_get_next` if the iterator doesn't fit your needs.
When you have finished using an IDR, you can call :c:func:`idr_destroy`
to release the memory used by the IDR. This will not free the objects
pointed to from the IDR; if you want to do that, use one of the iterators
to do it.
You can use :c:func:`idr_is_empty` to find out whether there are any
IDs currently allocated.
If you need to take a lock while allocating a new ID from the IDR,
you may need to pass a restrictive set of GFP flags, which can lead
to the IDR being unable to allocate memory. To work around this,
you can call :c:func:`idr_preload` before taking the lock, and then
:c:func:`idr_preload_end` after the allocation.
.. kernel-doc:: include/linux/idr.h
:doc: idr sync
IDA usage
=========
.. kernel-doc:: lib/idr.c
:doc: IDA description
Functions and structures
========================
.. kernel-doc:: include/linux/idr.h
.. kernel-doc:: lib/idr.c
...@@ -16,6 +16,7 @@ Core utilities ...@@ -16,6 +16,7 @@ Core utilities
atomic_ops atomic_ops
refcount-vs-atomic refcount-vs-atomic
cpu_hotplug cpu_hotplug
idr
local_ops local_ops
workqueue workqueue
genericirq genericirq
......
...@@ -103,18 +103,6 @@ CRC Functions ...@@ -103,18 +103,6 @@ CRC Functions
.. kernel-doc:: lib/crc-itu-t.c .. kernel-doc:: lib/crc-itu-t.c
:export: :export:
idr/ida Functions
-----------------
.. kernel-doc:: include/linux/idr.h
:doc: idr sync
.. kernel-doc:: lib/idr.c
:doc: IDA description
.. kernel-doc:: lib/idr.c
:export:
Math Functions in Linux Math Functions in Linux
======================= =======================
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
#include <linux/radix-tree.h> #include <linux/radix-tree.h>
#include <linux/gfp.h> #include <linux/gfp.h>
#include <linux/percpu.h> #include <linux/percpu.h>
#include <linux/bug.h>
struct idr { struct idr {
struct radix_tree_root idr_rt; struct radix_tree_root idr_rt;
unsigned int idr_base;
unsigned int idr_next; unsigned int idr_next;
}; };
...@@ -31,10 +31,26 @@ struct idr { ...@@ -31,10 +31,26 @@ struct idr {
/* Set the IDR flag and the IDR_FREE tag */ /* Set the IDR flag and the IDR_FREE tag */
#define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT)) #define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT))
#define IDR_INIT \ #define IDR_INIT_BASE(base) { \
{ \ .idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER), \
.idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER) \ .idr_base = (base), \
.idr_next = 0, \
} }
/**
* IDR_INIT() - Initialise an IDR.
*
* A freshly-initialised IDR contains no IDs.
*/
#define IDR_INIT IDR_INIT_BASE(0)
/**
* DEFINE_IDR() - Define a statically-allocated IDR
* @name: Name of IDR
*
* An IDR defined using this macro is ready for use with no additional
* initialisation required. It contains no IDs.
*/
#define DEFINE_IDR(name) struct idr name = IDR_INIT #define DEFINE_IDR(name) struct idr name = IDR_INIT
/** /**
...@@ -82,80 +98,52 @@ static inline void idr_set_cursor(struct idr *idr, unsigned int val) ...@@ -82,80 +98,52 @@ static inline void idr_set_cursor(struct idr *idr, unsigned int val)
void idr_preload(gfp_t gfp_mask); void idr_preload(gfp_t gfp_mask);
int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index, int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t);
unsigned long start, unsigned long end, gfp_t gfp, int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id,
bool ext); unsigned long max, gfp_t);
int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t);
/** void *idr_remove(struct idr *, unsigned long id);
* idr_alloc - allocate an id void *idr_find(const struct idr *, unsigned long id);
* @idr: idr handle
* @ptr: pointer to be associated with the new id
* @start: the minimum id (inclusive)
* @end: the maximum id (exclusive)
* @gfp: memory allocation flags
*
* Allocates an unused ID in the range [start, end). Returns -ENOSPC
* if there are no unused IDs in that range.
*
* Note that @end is treated as max when <= 0. This is to always allow
* using @start + N as @end as long as N is inside integer range.
*
* Simultaneous modifications to the @idr are not allowed and should be
* prevented by the user, usually with a lock. idr_alloc() may be called
* concurrently with read-only accesses to the @idr, such as idr_find() and
* idr_for_each_entry().
*/
static inline int idr_alloc(struct idr *idr, void *ptr,
int start, int end, gfp_t gfp)
{
unsigned long id;
int ret;
if (WARN_ON_ONCE(start < 0))
return -EINVAL;
ret = idr_alloc_cmn(idr, ptr, &id, start, end, gfp, false);
if (ret)
return ret;
return id;
}
static inline int idr_alloc_ext(struct idr *idr, void *ptr,
unsigned long *index,
unsigned long start,
unsigned long end,
gfp_t gfp)
{
return idr_alloc_cmn(idr, ptr, index, start, end, gfp, true);
}
int idr_alloc_cyclic(struct idr *, void *entry, int start, int end, gfp_t);
int idr_for_each(const struct idr *, int idr_for_each(const struct idr *,
int (*fn)(int id, void *p, void *data), void *data); int (*fn)(int id, void *p, void *data), void *data);
void *idr_get_next(struct idr *, int *nextid); void *idr_get_next(struct idr *, int *nextid);
void *idr_get_next_ext(struct idr *idr, unsigned long *nextid); void *idr_get_next_ul(struct idr *, unsigned long *nextid);
void *idr_replace(struct idr *, void *, int id); void *idr_replace(struct idr *, void *, unsigned long id);
void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id);
void idr_destroy(struct idr *); void idr_destroy(struct idr *);
static inline void *idr_remove_ext(struct idr *idr, unsigned long id) /**
{ * idr_init_base() - Initialise an IDR.
return radix_tree_delete_item(&idr->idr_rt, id, NULL); * @idr: IDR handle.
} * @base: The base value for the IDR.
*
static inline void *idr_remove(struct idr *idr, int id) * This variation of idr_init() creates an IDR which will allocate IDs
* starting at %base.
*/
static inline void idr_init_base(struct idr *idr, int base)
{ {
return idr_remove_ext(idr, id); INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
idr->idr_base = base;
idr->idr_next = 0;
} }
/**
* idr_init() - Initialise an IDR.
* @idr: IDR handle.
*
* Initialise a dynamically allocated IDR. To initialise a
* statically allocated IDR, use DEFINE_IDR().
*/
static inline void idr_init(struct idr *idr) static inline void idr_init(struct idr *idr)
{ {
INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER); idr_init_base(idr, 0);
idr->idr_next = 0;
} }
/**
* idr_is_empty() - Are there any IDs allocated?
* @idr: IDR handle.
*
* Return: %true if any IDs have been allocated from this IDR.
*/
static inline bool idr_is_empty(const struct idr *idr) static inline bool idr_is_empty(const struct idr *idr)
{ {
return radix_tree_empty(&idr->idr_rt) && return radix_tree_empty(&idr->idr_rt) &&
...@@ -174,50 +162,38 @@ static inline void idr_preload_end(void) ...@@ -174,50 +162,38 @@ static inline void idr_preload_end(void)
} }
/** /**
* idr_find - return pointer for given id * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
* @idr: idr handle * @idr: IDR handle.
* @id: lookup key * @entry: The type * to use as cursor
* * @id: Entry ID.
* Return the pointer given the id it has been registered with. A %NULL
* return indicates that @id is not valid or you passed %NULL in
* idr_get_new().
* *
* This function can be called under rcu_read_lock(), given that the leaf * @entry and @id do not need to be initialized before the loop, and
* pointers lifetimes are correctly managed. * after normal termination @entry is left with the value NULL. This
* is convenient for a "not found" value.
*/ */
static inline void *idr_find_ext(const struct idr *idr, unsigned long id) #define idr_for_each_entry(idr, entry, id) \
{ for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
return radix_tree_lookup(&idr->idr_rt, id);
}
static inline void *idr_find(const struct idr *idr, int id)
{
return idr_find_ext(idr, id);
}
/** /**
* idr_for_each_entry - iterate over an idr's elements of a given type * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
* @idr: idr handle * @idr: IDR handle.
* @entry: the type * to use as cursor * @entry: The type * to use as cursor.
* @id: id entry's key * @id: Entry ID.
* *
* @entry and @id do not need to be initialized before the loop, and * @entry and @id do not need to be initialized before the loop, and
* after normal terminatinon @entry is left with the value NULL. This * after normal termination @entry is left with the value NULL. This
* is convenient for a "not found" value. * is convenient for a "not found" value.
*/ */
#define idr_for_each_entry(idr, entry, id) \ #define idr_for_each_entry_ul(idr, entry, id) \
for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id) for (id = 0; ((entry) = idr_get_next_ul(idr, &(id))) != NULL; ++id)
#define idr_for_each_entry_ext(idr, entry, id) \
for (id = 0; ((entry) = idr_get_next_ext(idr, &(id))) != NULL; ++id)
/** /**
* idr_for_each_entry_continue - continue iteration over an idr's elements of a given type * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
* @idr: idr handle * @idr: IDR handle.
* @entry: the type * to use as cursor * @entry: The type * to use as a cursor.
* @id: id entry's key * @id: Entry ID.
* *
* Continue to iterate over list of given type, continuing after * Continue to iterate over entries, continuing after the current position.
* the current position.
*/ */
#define idr_for_each_entry_continue(idr, entry, id) \ #define idr_for_each_entry_continue(idr, entry, id) \
for ((entry) = idr_get_next((idr), &(id)); \ for ((entry) = idr_get_next((idr), &(id)); \
......
...@@ -356,24 +356,9 @@ int radix_tree_split(struct radix_tree_root *, unsigned long index, ...@@ -356,24 +356,9 @@ int radix_tree_split(struct radix_tree_root *, unsigned long index,
int radix_tree_join(struct radix_tree_root *, unsigned long index, int radix_tree_join(struct radix_tree_root *, unsigned long index,
unsigned new_order, void *); unsigned new_order, void *);
void __rcu **idr_get_free_cmn(struct radix_tree_root *root, void __rcu **idr_get_free(struct radix_tree_root *root,
struct radix_tree_iter *iter, gfp_t gfp, struct radix_tree_iter *iter, gfp_t gfp,
unsigned long max); unsigned long max);
static inline void __rcu **idr_get_free(struct radix_tree_root *root,
struct radix_tree_iter *iter,
gfp_t gfp,
int end)
{
return idr_get_free_cmn(root, iter, gfp, end > 0 ? end - 1 : INT_MAX);
}
static inline void __rcu **idr_get_free_ext(struct radix_tree_root *root,
struct radix_tree_iter *iter,
gfp_t gfp,
unsigned long end)
{
return idr_get_free_cmn(root, iter, gfp, end - 1);
}
enum { enum {
RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */ RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */
......
#include <linux/bitmap.h> #include <linux/bitmap.h>
#include <linux/bug.h>
#include <linux/export.h> #include <linux/export.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -7,71 +8,184 @@ ...@@ -7,71 +8,184 @@
DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap); DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap);
static DEFINE_SPINLOCK(simple_ida_lock); static DEFINE_SPINLOCK(simple_ida_lock);
int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index, /**
unsigned long start, unsigned long end, gfp_t gfp, * idr_alloc_u32() - Allocate an ID.
bool ext) * @idr: IDR handle.
* @ptr: Pointer to be associated with the new ID.
* @nextid: Pointer to an ID.
* @max: The maximum ID to allocate (inclusive).
* @gfp: Memory allocation flags.
*
* Allocates an unused ID in the range specified by @nextid and @max.
* Note that @max is inclusive whereas the @end parameter to idr_alloc()
* is exclusive. The new ID is assigned to @nextid before the pointer
* is inserted into the IDR, so if @nextid points into the object pointed
* to by @ptr, a concurrent lookup will not find an uninitialised ID.
*
* The caller should provide their own locking to ensure that two
* concurrent modifications to the IDR are not possible. Read-only
* accesses to the IDR may be done under the RCU read lock or may
* exclude simultaneous writers.
*
* Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
* or -ENOSPC if no free IDs could be found. If an error occurred,
* @nextid is unchanged.
*/
int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
unsigned long max, gfp_t gfp)
{ {
struct radix_tree_iter iter; struct radix_tree_iter iter;
void __rcu **slot; void __rcu **slot;
int base = idr->idr_base;
int id = *nextid;
if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr))) if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
return -EINVAL; return -EINVAL;
if (WARN_ON_ONCE(!(idr->idr_rt.gfp_mask & ROOT_IS_IDR)))
idr->idr_rt.gfp_mask |= IDR_RT_MARKER;
radix_tree_iter_init(&iter, start); id = (id < base) ? 0 : id - base;
if (ext) radix_tree_iter_init(&iter, id);
slot = idr_get_free_ext(&idr->idr_rt, &iter, gfp, end); slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);
else
slot = idr_get_free(&idr->idr_rt, &iter, gfp, end);
if (IS_ERR(slot)) if (IS_ERR(slot))
return PTR_ERR(slot); return PTR_ERR(slot);
*nextid = iter.index + base;
/* there is a memory barrier inside radix_tree_iter_replace() */
radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr); radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE); radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
if (index)
*index = iter.index;
return 0; return 0;
} }
EXPORT_SYMBOL_GPL(idr_alloc_cmn); EXPORT_SYMBOL_GPL(idr_alloc_u32);
/** /**
* idr_alloc_cyclic - allocate new idr entry in a cyclical fashion * idr_alloc() - Allocate an ID.
* @idr: idr handle * @idr: IDR handle.
* @ptr: pointer to be associated with the new id * @ptr: Pointer to be associated with the new ID.
* @start: the minimum id (inclusive) * @start: The minimum ID (inclusive).
* @end: the maximum id (exclusive) * @end: The maximum ID (exclusive).
* @gfp: memory allocation flags * @gfp: Memory allocation flags.
* *
* Allocates an ID larger than the last ID allocated if one is available. * Allocates an unused ID in the range specified by @start and @end. If
* If not, it will attempt to allocate the smallest ID that is larger or * @end is <= 0, it is treated as one larger than %INT_MAX. This allows
* equal to @start. * callers to use @start + N as @end as long as N is within integer range.
*
* The caller should provide their own locking to ensure that two
* concurrent modifications to the IDR are not possible. Read-only
* accesses to the IDR may be done under the RCU read lock or may
* exclude simultaneous writers.
*
* Return: The newly allocated ID, -ENOMEM if memory allocation failed,
* or -ENOSPC if no free IDs could be found.
*/ */
int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
{ {
int id, curr = idr->idr_next; u32 id = start;
int ret;
if (WARN_ON_ONCE(start < 0))
return -EINVAL;
ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
if (ret)
return ret;
return id;
}
EXPORT_SYMBOL_GPL(idr_alloc);
if (curr < start) /**
curr = start; * idr_alloc_cyclic() - Allocate an ID cyclically.
* @idr: IDR handle.
* @ptr: Pointer to be associated with the new ID.
* @start: The minimum ID (inclusive).
* @end: The maximum ID (exclusive).
* @gfp: Memory allocation flags.
*
* Allocates an unused ID in the range specified by @nextid and @end. If
* @end is <= 0, it is treated as one larger than %INT_MAX. This allows
* callers to use @start + N as @end as long as N is within integer range.
* The search for an unused ID will start at the last ID allocated and will
* wrap around to @start if no free IDs are found before reaching @end.
*
* The caller should provide their own locking to ensure that two
* concurrent modifications to the IDR are not possible. Read-only
* accesses to the IDR may be done under the RCU read lock or may
* exclude simultaneous writers.
*
* Return: The newly allocated ID, -ENOMEM if memory allocation failed,
* or -ENOSPC if no free IDs could be found.
*/
int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
{
u32 id = idr->idr_next;
int err, max = end > 0 ? end - 1 : INT_MAX;
id = idr_alloc(idr, ptr, curr, end, gfp); if ((int)id < start)
if ((id == -ENOSPC) && (curr > start)) id = start;
id = idr_alloc(idr, ptr, start, curr, gfp);
if (id >= 0) err = idr_alloc_u32(idr, ptr, &id, max, gfp);
idr->idr_next = id + 1U; if ((err == -ENOSPC) && (id > start)) {
id = start;
err = idr_alloc_u32(idr, ptr, &id, max, gfp);
}
if (err)
return err;
idr->idr_next = id + 1;
return id; return id;
} }
EXPORT_SYMBOL(idr_alloc_cyclic); EXPORT_SYMBOL(idr_alloc_cyclic);
/** /**
* idr_for_each - iterate through all stored pointers * idr_remove() - Remove an ID from the IDR.
* @idr: idr handle * @idr: IDR handle.
* @fn: function to be called for each pointer * @id: Pointer ID.
* @data: data passed to callback function *
* Removes this ID from the IDR. If the ID was not previously in the IDR,
* this function returns %NULL.
*
* Since this function modifies the IDR, the caller should provide their
* own locking to ensure that concurrent modification of the same IDR is
* not possible.
*
* Return: The pointer formerly associated with this ID.
*/
void *idr_remove(struct idr *idr, unsigned long id)
{
return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL);
}
EXPORT_SYMBOL_GPL(idr_remove);
/**
* idr_find() - Return pointer for given ID.
* @idr: IDR handle.
* @id: Pointer ID.
*
* Looks up the pointer associated with this ID. A %NULL pointer may
* indicate that @id is not allocated or that the %NULL pointer was
* associated with this ID.
*
* This function can be called under rcu_read_lock(), given that the leaf
* pointers lifetimes are correctly managed.
*
* Return: The pointer associated with this ID.
*/
void *idr_find(const struct idr *idr, unsigned long id)
{
return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base);
}
EXPORT_SYMBOL_GPL(idr_find);
/**
* idr_for_each() - Iterate through all stored pointers.
* @idr: IDR handle.
* @fn: Function to be called for each pointer.
* @data: Data passed to callback function.
* *
* The callback function will be called for each entry in @idr, passing * The callback function will be called for each entry in @idr, passing
* the id, the pointer and the data pointer passed to this function. * the ID, the entry and @data.
* *
* If @fn returns anything other than %0, the iteration stops and that * If @fn returns anything other than %0, the iteration stops and that
* value is returned from this function. * value is returned from this function.
...@@ -86,9 +200,14 @@ int idr_for_each(const struct idr *idr, ...@@ -86,9 +200,14 @@ int idr_for_each(const struct idr *idr,
{ {
struct radix_tree_iter iter; struct radix_tree_iter iter;
void __rcu **slot; void __rcu **slot;
int base = idr->idr_base;
radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) { radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
int ret = fn(iter.index, rcu_dereference_raw(*slot), data); int ret;
if (WARN_ON_ONCE(iter.index > INT_MAX))
break;
ret = fn(iter.index + base, rcu_dereference_raw(*slot), data);
if (ret) if (ret)
return ret; return ret;
} }
...@@ -98,9 +217,9 @@ int idr_for_each(const struct idr *idr, ...@@ -98,9 +217,9 @@ int idr_for_each(const struct idr *idr,
EXPORT_SYMBOL(idr_for_each); EXPORT_SYMBOL(idr_for_each);
/** /**
* idr_get_next - Find next populated entry * idr_get_next() - Find next populated entry.
* @idr: idr handle * @idr: IDR handle.
* @nextid: Pointer to lowest possible ID to return * @nextid: Pointer to an ID.
* *
* Returns the next populated entry in the tree with an ID greater than * Returns the next populated entry in the tree with an ID greater than
* or equal to the value pointed to by @nextid. On exit, @nextid is updated * or equal to the value pointed to by @nextid. On exit, @nextid is updated
...@@ -111,35 +230,55 @@ void *idr_get_next(struct idr *idr, int *nextid) ...@@ -111,35 +230,55 @@ void *idr_get_next(struct idr *idr, int *nextid)
{ {
struct radix_tree_iter iter; struct radix_tree_iter iter;
void __rcu **slot; void __rcu **slot;
int base = idr->idr_base;
int id = *nextid;
slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid); id = (id < base) ? 0 : id - base;
slot = radix_tree_iter_find(&idr->idr_rt, &iter, id);
if (!slot) if (!slot)
return NULL; return NULL;
id = iter.index + base;
if (WARN_ON_ONCE(id > INT_MAX))
return NULL;
*nextid = iter.index; *nextid = id;
return rcu_dereference_raw(*slot); return rcu_dereference_raw(*slot);
} }
EXPORT_SYMBOL(idr_get_next); EXPORT_SYMBOL(idr_get_next);
void *idr_get_next_ext(struct idr *idr, unsigned long *nextid) /**
* idr_get_next_ul() - Find next populated entry.
* @idr: IDR handle.
* @nextid: Pointer to an ID.
*
* Returns the next populated entry in the tree with an ID greater than
* or equal to the value pointed to by @nextid. On exit, @nextid is updated
* to the ID of the found value. To use in a loop, the value pointed to by
* nextid must be incremented by the user.
*/
void *idr_get_next_ul(struct idr *idr, unsigned long *nextid)
{ {
struct radix_tree_iter iter; struct radix_tree_iter iter;
void __rcu **slot; void __rcu **slot;
unsigned long base = idr->idr_base;
unsigned long id = *nextid;
slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid); id = (id < base) ? 0 : id - base;
slot = radix_tree_iter_find(&idr->idr_rt, &iter, id);
if (!slot) if (!slot)
return NULL; return NULL;
*nextid = iter.index; *nextid = iter.index + base;
return rcu_dereference_raw(*slot); return rcu_dereference_raw(*slot);
} }
EXPORT_SYMBOL(idr_get_next_ext); EXPORT_SYMBOL(idr_get_next_ul);
/** /**
* idr_replace - replace pointer for given id * idr_replace() - replace pointer for given ID.
* @idr: idr handle * @idr: IDR handle.
* @ptr: New pointer to associate with the ID * @ptr: New pointer to associate with the ID.
* @id: Lookup key * @id: ID to change.
* *
* Replace the pointer registered with an ID and return the old value. * Replace the pointer registered with an ID and return the old value.
* This function can be called under the RCU read lock concurrently with * This function can be called under the RCU read lock concurrently with
...@@ -147,18 +286,9 @@ EXPORT_SYMBOL(idr_get_next_ext); ...@@ -147,18 +286,9 @@ EXPORT_SYMBOL(idr_get_next_ext);
* the one being replaced!). * the one being replaced!).
* *
* Returns: the old value on success. %-ENOENT indicates that @id was not * Returns: the old value on success. %-ENOENT indicates that @id was not
* found. %-EINVAL indicates that @id or @ptr were not valid. * found. %-EINVAL indicates that @ptr was not valid.
*/ */
void *idr_replace(struct idr *idr, void *ptr, int id) void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
{
if (id < 0)
return ERR_PTR(-EINVAL);
return idr_replace_ext(idr, ptr, id);
}
EXPORT_SYMBOL(idr_replace);
void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id)
{ {
struct radix_tree_node *node; struct radix_tree_node *node;
void __rcu **slot = NULL; void __rcu **slot = NULL;
...@@ -166,6 +296,7 @@ void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id) ...@@ -166,6 +296,7 @@ void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id)
if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr))) if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr)))
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
id -= idr->idr_base;
entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot); entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE)) if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
...@@ -175,7 +306,7 @@ void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id) ...@@ -175,7 +306,7 @@ void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id)
return entry; return entry;
} }
EXPORT_SYMBOL(idr_replace_ext); EXPORT_SYMBOL(idr_replace);
/** /**
* DOC: IDA description * DOC: IDA description
...@@ -235,7 +366,7 @@ EXPORT_SYMBOL(idr_replace_ext); ...@@ -235,7 +366,7 @@ EXPORT_SYMBOL(idr_replace_ext);
* bitmap, which is excessive. * bitmap, which is excessive.
*/ */
#define IDA_MAX (0x80000000U / IDA_BITMAP_BITS) #define IDA_MAX (0x80000000U / IDA_BITMAP_BITS - 1)
/** /**
* ida_get_new_above - allocate new ID above or equal to a start id * ida_get_new_above - allocate new ID above or equal to a start id
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <linux/bitmap.h> #include <linux/bitmap.h>
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/bug.h>
#include <linux/cpu.h> #include <linux/cpu.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/export.h> #include <linux/export.h>
...@@ -2135,7 +2136,7 @@ int ida_pre_get(struct ida *ida, gfp_t gfp) ...@@ -2135,7 +2136,7 @@ int ida_pre_get(struct ida *ida, gfp_t gfp)
} }
EXPORT_SYMBOL(ida_pre_get); EXPORT_SYMBOL(ida_pre_get);
void __rcu **idr_get_free_cmn(struct radix_tree_root *root, void __rcu **idr_get_free(struct radix_tree_root *root,
struct radix_tree_iter *iter, gfp_t gfp, struct radix_tree_iter *iter, gfp_t gfp,
unsigned long max) unsigned long max)
{ {
......
...@@ -78,7 +78,7 @@ static void free_tcf(struct tc_action *p) ...@@ -78,7 +78,7 @@ static void free_tcf(struct tc_action *p)
static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p) static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
{ {
spin_lock_bh(&idrinfo->lock); spin_lock_bh(&idrinfo->lock);
idr_remove_ext(&idrinfo->action_idr, p->tcfa_index); idr_remove(&idrinfo->action_idr, p->tcfa_index);
spin_unlock_bh(&idrinfo->lock); spin_unlock_bh(&idrinfo->lock);
gen_kill_estimator(&p->tcfa_rate_est); gen_kill_estimator(&p->tcfa_rate_est);
free_tcf(p); free_tcf(p);
...@@ -124,7 +124,7 @@ static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, ...@@ -124,7 +124,7 @@ static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
s_i = cb->args[0]; s_i = cb->args[0];
idr_for_each_entry_ext(idr, p, id) { idr_for_each_entry_ul(idr, p, id) {
index++; index++;
if (index < s_i) if (index < s_i)
continue; continue;
...@@ -181,7 +181,7 @@ static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, ...@@ -181,7 +181,7 @@ static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
if (nla_put_string(skb, TCA_KIND, ops->kind)) if (nla_put_string(skb, TCA_KIND, ops->kind))
goto nla_put_failure; goto nla_put_failure;
idr_for_each_entry_ext(idr, p, id) { idr_for_each_entry_ul(idr, p, id) {
ret = __tcf_idr_release(p, false, true); ret = __tcf_idr_release(p, false, true);
if (ret == ACT_P_DELETED) { if (ret == ACT_P_DELETED) {
module_put(ops->owner); module_put(ops->owner);
...@@ -222,7 +222,7 @@ static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo) ...@@ -222,7 +222,7 @@ static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
struct tc_action *p = NULL; struct tc_action *p = NULL;
spin_lock_bh(&idrinfo->lock); spin_lock_bh(&idrinfo->lock);
p = idr_find_ext(&idrinfo->action_idr, index); p = idr_find(&idrinfo->action_idr, index);
spin_unlock_bh(&idrinfo->lock); spin_unlock_bh(&idrinfo->lock);
return p; return p;
...@@ -274,7 +274,6 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, ...@@ -274,7 +274,6 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
struct tcf_idrinfo *idrinfo = tn->idrinfo; struct tcf_idrinfo *idrinfo = tn->idrinfo;
struct idr *idr = &idrinfo->action_idr; struct idr *idr = &idrinfo->action_idr;
int err = -ENOMEM; int err = -ENOMEM;
unsigned long idr_index;
if (unlikely(!p)) if (unlikely(!p))
return -ENOMEM; return -ENOMEM;
...@@ -284,45 +283,28 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, ...@@ -284,45 +283,28 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
if (cpustats) { if (cpustats) {
p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
if (!p->cpu_bstats) { if (!p->cpu_bstats)
err1:
kfree(p);
return err;
}
p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
if (!p->cpu_qstats) {
err2:
free_percpu(p->cpu_bstats);
goto err1; goto err1;
} p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
if (!p->cpu_qstats)
goto err2;
} }
spin_lock_init(&p->tcfa_lock); spin_lock_init(&p->tcfa_lock);
idr_preload(GFP_KERNEL);
spin_lock_bh(&idrinfo->lock);
/* user doesn't specify an index */ /* user doesn't specify an index */
if (!index) { if (!index) {
idr_preload(GFP_KERNEL); index = 1;
spin_lock_bh(&idrinfo->lock); err = idr_alloc_u32(idr, NULL, &index, UINT_MAX, GFP_ATOMIC);
err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0,
GFP_ATOMIC);
spin_unlock_bh(&idrinfo->lock);
idr_preload_end();
if (err) {
err3:
free_percpu(p->cpu_qstats);
goto err2;
}
p->tcfa_index = idr_index;
} else { } else {
idr_preload(GFP_KERNEL); err = idr_alloc_u32(idr, NULL, &index, index, GFP_ATOMIC);
spin_lock_bh(&idrinfo->lock);
err = idr_alloc_ext(idr, NULL, NULL, index, index + 1,
GFP_ATOMIC);
spin_unlock_bh(&idrinfo->lock);
idr_preload_end();
if (err)
goto err3;
p->tcfa_index = index;
} }
spin_unlock_bh(&idrinfo->lock);
idr_preload_end();
if (err)
goto err3;
p->tcfa_index = index;
p->tcfa_tm.install = jiffies; p->tcfa_tm.install = jiffies;
p->tcfa_tm.lastuse = jiffies; p->tcfa_tm.lastuse = jiffies;
p->tcfa_tm.firstuse = 0; p->tcfa_tm.firstuse = 0;
...@@ -330,9 +312,8 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, ...@@ -330,9 +312,8 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
&p->tcfa_rate_est, &p->tcfa_rate_est,
&p->tcfa_lock, NULL, est); &p->tcfa_lock, NULL, est);
if (err) { if (err)
goto err3; goto err4;
}
} }
p->idrinfo = idrinfo; p->idrinfo = idrinfo;
...@@ -340,6 +321,15 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, ...@@ -340,6 +321,15 @@ int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
INIT_LIST_HEAD(&p->list); INIT_LIST_HEAD(&p->list);
*a = p; *a = p;
return 0; return 0;
err4:
idr_remove(idr, index);
err3:
free_percpu(p->cpu_qstats);
err2:
free_percpu(p->cpu_bstats);
err1:
kfree(p);
return err;
} }
EXPORT_SYMBOL(tcf_idr_create); EXPORT_SYMBOL(tcf_idr_create);
...@@ -348,7 +338,7 @@ void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) ...@@ -348,7 +338,7 @@ void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
struct tcf_idrinfo *idrinfo = tn->idrinfo; struct tcf_idrinfo *idrinfo = tn->idrinfo;
spin_lock_bh(&idrinfo->lock); spin_lock_bh(&idrinfo->lock);
idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index); idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
spin_unlock_bh(&idrinfo->lock); spin_unlock_bh(&idrinfo->lock);
} }
EXPORT_SYMBOL(tcf_idr_insert); EXPORT_SYMBOL(tcf_idr_insert);
...@@ -361,7 +351,7 @@ void tcf_idrinfo_destroy(const struct tc_action_ops *ops, ...@@ -361,7 +351,7 @@ void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
int ret; int ret;
unsigned long id = 1; unsigned long id = 1;
idr_for_each_entry_ext(idr, p, id) { idr_for_each_entry_ul(idr, p, id) {
ret = __tcf_idr_release(p, false, true); ret = __tcf_idr_release(p, false, true);
if (ret == ACT_P_DELETED) if (ret == ACT_P_DELETED)
module_put(ops->owner); module_put(ops->owner);
......
...@@ -381,8 +381,8 @@ static int tcf_block_insert(struct tcf_block *block, struct net *net, ...@@ -381,8 +381,8 @@ static int tcf_block_insert(struct tcf_block *block, struct net *net,
struct tcf_net *tn = net_generic(net, tcf_net_id); struct tcf_net *tn = net_generic(net, tcf_net_id);
int err; int err;
err = idr_alloc_ext(&tn->idr, block, NULL, block_index, err = idr_alloc_u32(&tn->idr, block, &block_index, block_index,
block_index + 1, GFP_KERNEL); GFP_KERNEL);
if (err) if (err)
return err; return err;
block->index = block_index; block->index = block_index;
...@@ -393,7 +393,7 @@ static void tcf_block_remove(struct tcf_block *block, struct net *net) ...@@ -393,7 +393,7 @@ static void tcf_block_remove(struct tcf_block *block, struct net *net)
{ {
struct tcf_net *tn = net_generic(net, tcf_net_id); struct tcf_net *tn = net_generic(net, tcf_net_id);
idr_remove_ext(&tn->idr, block->index); idr_remove(&tn->idr, block->index);
} }
static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
...@@ -434,7 +434,7 @@ static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) ...@@ -434,7 +434,7 @@ static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
{ {
struct tcf_net *tn = net_generic(net, tcf_net_id); struct tcf_net *tn = net_generic(net, tcf_net_id);
return idr_find_ext(&tn->idr, block_index); return idr_find(&tn->idr, block_index);
} }
static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block) static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
......
...@@ -120,7 +120,7 @@ static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack) ...@@ -120,7 +120,7 @@ static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
list_for_each_entry_safe(f, n, &head->flist, link) { list_for_each_entry_safe(f, n, &head->flist, link) {
list_del_rcu(&f->link); list_del_rcu(&f->link);
tcf_unbind_filter(tp, &f->res); tcf_unbind_filter(tp, &f->res);
idr_remove_ext(&head->handle_idr, f->handle); idr_remove(&head->handle_idr, f->handle);
if (tcf_exts_get_net(&f->exts)) if (tcf_exts_get_net(&f->exts))
call_rcu(&f->rcu, basic_delete_filter); call_rcu(&f->rcu, basic_delete_filter);
else else
...@@ -138,7 +138,7 @@ static int basic_delete(struct tcf_proto *tp, void *arg, bool *last, ...@@ -138,7 +138,7 @@ static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
list_del_rcu(&f->link); list_del_rcu(&f->link);
tcf_unbind_filter(tp, &f->res); tcf_unbind_filter(tp, &f->res);
idr_remove_ext(&head->handle_idr, f->handle); idr_remove(&head->handle_idr, f->handle);
tcf_exts_get_net(&f->exts); tcf_exts_get_net(&f->exts);
call_rcu(&f->rcu, basic_delete_filter); call_rcu(&f->rcu, basic_delete_filter);
*last = list_empty(&head->flist); *last = list_empty(&head->flist);
...@@ -185,7 +185,6 @@ static int basic_change(struct net *net, struct sk_buff *in_skb, ...@@ -185,7 +185,6 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,
struct nlattr *tb[TCA_BASIC_MAX + 1]; struct nlattr *tb[TCA_BASIC_MAX + 1];
struct basic_filter *fold = (struct basic_filter *) *arg; struct basic_filter *fold = (struct basic_filter *) *arg;
struct basic_filter *fnew; struct basic_filter *fnew;
unsigned long idr_index;
if (tca[TCA_OPTIONS] == NULL) if (tca[TCA_OPTIONS] == NULL)
return -EINVAL; return -EINVAL;
...@@ -208,34 +207,30 @@ static int basic_change(struct net *net, struct sk_buff *in_skb, ...@@ -208,34 +207,30 @@ static int basic_change(struct net *net, struct sk_buff *in_skb,
if (err < 0) if (err < 0)
goto errout; goto errout;
if (handle) { if (!handle) {
fnew->handle = handle; handle = 1;
if (!fold) { err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, INT_MAX, GFP_KERNEL);
handle, handle + 1, GFP_KERNEL); } else if (!fold) {
if (err) err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
goto errout; handle, GFP_KERNEL);
}
} else {
err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
1, 0x7FFFFFFF, GFP_KERNEL);
if (err)
goto errout;
fnew->handle = idr_index;
} }
if (err)
goto errout;
fnew->handle = handle;
err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr, err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
extack); extack);
if (err < 0) { if (err < 0) {
if (!fold) if (!fold)
idr_remove_ext(&head->handle_idr, fnew->handle); idr_remove(&head->handle_idr, fnew->handle);
goto errout; goto errout;
} }
*arg = fnew; *arg = fnew;
if (fold) { if (fold) {
idr_replace_ext(&head->handle_idr, fnew, fnew->handle); idr_replace(&head->handle_idr, fnew, fnew->handle);
list_replace_rcu(&fold->link, &fnew->link); list_replace_rcu(&fold->link, &fnew->link);
tcf_unbind_filter(tp, &fold->res); tcf_unbind_filter(tp, &fold->res);
tcf_exts_get_net(&fold->exts); tcf_exts_get_net(&fold->exts);
......
...@@ -295,7 +295,7 @@ static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog, ...@@ -295,7 +295,7 @@ static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog,
{ {
struct cls_bpf_head *head = rtnl_dereference(tp->root); struct cls_bpf_head *head = rtnl_dereference(tp->root);
idr_remove_ext(&head->handle_idr, prog->handle); idr_remove(&head->handle_idr, prog->handle);
cls_bpf_stop_offload(tp, prog, extack); cls_bpf_stop_offload(tp, prog, extack);
list_del_rcu(&prog->link); list_del_rcu(&prog->link);
tcf_unbind_filter(tp, &prog->res); tcf_unbind_filter(tp, &prog->res);
...@@ -471,7 +471,6 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, ...@@ -471,7 +471,6 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
struct cls_bpf_prog *oldprog = *arg; struct cls_bpf_prog *oldprog = *arg;
struct nlattr *tb[TCA_BPF_MAX + 1]; struct nlattr *tb[TCA_BPF_MAX + 1];
struct cls_bpf_prog *prog; struct cls_bpf_prog *prog;
unsigned long idr_index;
int ret; int ret;
if (tca[TCA_OPTIONS] == NULL) if (tca[TCA_OPTIONS] == NULL)
...@@ -498,21 +497,18 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, ...@@ -498,21 +497,18 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
} }
if (handle == 0) { if (handle == 0) {
ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index, handle = 1;
1, 0x7FFFFFFF, GFP_KERNEL); ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
if (ret) INT_MAX, GFP_KERNEL);
goto errout; } else if (!oldprog) {
prog->handle = idr_index; ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
} else { handle, GFP_KERNEL);
if (!oldprog) {
ret = idr_alloc_ext(&head->handle_idr, prog, &idr_index,
handle, handle + 1, GFP_KERNEL);
if (ret)
goto errout;
}
prog->handle = handle;
} }
if (ret)
goto errout;
prog->handle = handle;
ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr, ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr,
extack); extack);
if (ret < 0) if (ret < 0)
...@@ -526,7 +522,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, ...@@ -526,7 +522,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW; prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
if (oldprog) { if (oldprog) {
idr_replace_ext(&head->handle_idr, prog, handle); idr_replace(&head->handle_idr, prog, handle);
list_replace_rcu(&oldprog->link, &prog->link); list_replace_rcu(&oldprog->link, &prog->link);
tcf_unbind_filter(tp, &oldprog->res); tcf_unbind_filter(tp, &oldprog->res);
tcf_exts_get_net(&oldprog->exts); tcf_exts_get_net(&oldprog->exts);
...@@ -542,7 +538,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, ...@@ -542,7 +538,7 @@ static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
cls_bpf_free_parms(prog); cls_bpf_free_parms(prog);
errout_idr: errout_idr:
if (!oldprog) if (!oldprog)
idr_remove_ext(&head->handle_idr, prog->handle); idr_remove(&head->handle_idr, prog->handle);
errout: errout:
tcf_exts_destroy(&prog->exts); tcf_exts_destroy(&prog->exts);
kfree(prog); kfree(prog);
......
...@@ -288,7 +288,7 @@ static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, ...@@ -288,7 +288,7 @@ static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
{ {
struct cls_fl_head *head = rtnl_dereference(tp->root); struct cls_fl_head *head = rtnl_dereference(tp->root);
idr_remove_ext(&head->handle_idr, f->handle); idr_remove(&head->handle_idr, f->handle);
list_del_rcu(&f->list); list_del_rcu(&f->list);
if (!tc_skip_hw(f->flags)) if (!tc_skip_hw(f->flags))
fl_hw_destroy_filter(tp, f, extack); fl_hw_destroy_filter(tp, f, extack);
...@@ -334,7 +334,7 @@ static void *fl_get(struct tcf_proto *tp, u32 handle) ...@@ -334,7 +334,7 @@ static void *fl_get(struct tcf_proto *tp, u32 handle)
{ {
struct cls_fl_head *head = rtnl_dereference(tp->root); struct cls_fl_head *head = rtnl_dereference(tp->root);
return idr_find_ext(&head->handle_idr, handle); return idr_find(&head->handle_idr, handle);
} }
static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
...@@ -865,7 +865,6 @@ static int fl_change(struct net *net, struct sk_buff *in_skb, ...@@ -865,7 +865,6 @@ static int fl_change(struct net *net, struct sk_buff *in_skb,
struct cls_fl_filter *fnew; struct cls_fl_filter *fnew;
struct nlattr **tb; struct nlattr **tb;
struct fl_flow_mask mask = {}; struct fl_flow_mask mask = {};
unsigned long idr_index;
int err; int err;
if (!tca[TCA_OPTIONS]) if (!tca[TCA_OPTIONS])
...@@ -896,21 +895,17 @@ static int fl_change(struct net *net, struct sk_buff *in_skb, ...@@ -896,21 +895,17 @@ static int fl_change(struct net *net, struct sk_buff *in_skb,
goto errout; goto errout;
if (!handle) { if (!handle) {
err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, handle = 1;
1, 0x80000000, GFP_KERNEL); err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
if (err) INT_MAX, GFP_KERNEL);
goto errout; } else if (!fold) {
fnew->handle = idr_index; /* user specifies a handle and it doesn't exist */
} err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
handle, GFP_KERNEL);
/* user specifies a handle and it doesn't exist */
if (handle && !fold) {
err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
handle, handle + 1, GFP_KERNEL);
if (err)
goto errout;
fnew->handle = idr_index;
} }
if (err)
goto errout;
fnew->handle = handle;
if (tb[TCA_FLOWER_FLAGS]) { if (tb[TCA_FLOWER_FLAGS]) {
fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
...@@ -966,8 +961,7 @@ static int fl_change(struct net *net, struct sk_buff *in_skb, ...@@ -966,8 +961,7 @@ static int fl_change(struct net *net, struct sk_buff *in_skb,
*arg = fnew; *arg = fnew;
if (fold) { if (fold) {
fnew->handle = handle; idr_replace(&head->handle_idr, fnew, fnew->handle);
idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
list_replace_rcu(&fold->list, &fnew->list); list_replace_rcu(&fold->list, &fnew->list);
tcf_unbind_filter(tp, &fold->res); tcf_unbind_filter(tp, &fold->res);
tcf_exts_get_net(&fold->exts); tcf_exts_get_net(&fold->exts);
...@@ -981,7 +975,7 @@ static int fl_change(struct net *net, struct sk_buff *in_skb, ...@@ -981,7 +975,7 @@ static int fl_change(struct net *net, struct sk_buff *in_skb,
errout_idr: errout_idr:
if (fnew->handle) if (fnew->handle)
idr_remove_ext(&head->handle_idr, fnew->handle); idr_remove(&head->handle_idr, fnew->handle);
errout: errout:
tcf_exts_destroy(&fnew->exts); tcf_exts_destroy(&fnew->exts);
kfree(fnew); kfree(fnew);
......
...@@ -316,19 +316,13 @@ static void *u32_get(struct tcf_proto *tp, u32 handle) ...@@ -316,19 +316,13 @@ static void *u32_get(struct tcf_proto *tp, u32 handle)
return u32_lookup_key(ht, handle); return u32_lookup_key(ht, handle);
} }
/* Protected by rtnl lock */
static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr) static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
{ {
unsigned long idr_index; int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
int err; if (id < 0)
/* This is only used inside rtnl lock it is safe to increment
* without read _copy_ update semantics
*/
err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
1, 0x7FF, GFP_KERNEL);
if (err)
return 0; return 0;
return (u32)(idr_index | 0x800) << 20; return (id | 0x800U) << 20;
} }
static struct hlist_head *tc_u_common_hash; static struct hlist_head *tc_u_common_hash;
...@@ -598,7 +592,7 @@ static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht, ...@@ -598,7 +592,7 @@ static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
rtnl_dereference(n->next)); rtnl_dereference(n->next));
tcf_unbind_filter(tp, &n->res); tcf_unbind_filter(tp, &n->res);
u32_remove_hw_knode(tp, n, extack); u32_remove_hw_knode(tp, n, extack);
idr_remove_ext(&ht->handle_idr, n->handle); idr_remove(&ht->handle_idr, n->handle);
if (tcf_exts_get_net(&n->exts)) if (tcf_exts_get_net(&n->exts))
call_rcu(&n->rcu, u32_delete_key_freepf_rcu); call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
else else
...@@ -625,7 +619,7 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht, ...@@ -625,7 +619,7 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
if (phn == ht) { if (phn == ht) {
u32_clear_hw_hnode(tp, ht, extack); u32_clear_hw_hnode(tp, ht, extack);
idr_destroy(&ht->handle_idr); idr_destroy(&ht->handle_idr);
idr_remove_ext(&tp_c->handle_idr, ht->handle); idr_remove(&tp_c->handle_idr, ht->handle);
RCU_INIT_POINTER(*hn, ht->next); RCU_INIT_POINTER(*hn, ht->next);
kfree_rcu(ht, rcu); kfree_rcu(ht, rcu);
return 0; return 0;
...@@ -747,19 +741,17 @@ static int u32_delete(struct tcf_proto *tp, void *arg, bool *last, ...@@ -747,19 +741,17 @@ static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid) static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
{ {
unsigned long idr_index; u32 index = htid | 0x800;
u32 start = htid | 0x800;
u32 max = htid | 0xFFF; u32 max = htid | 0xFFF;
u32 min = htid;
if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
start, max + 1, GFP_KERNEL)) { index = htid + 1;
if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
min + 1, max + 1, GFP_KERNEL)) GFP_KERNEL))
return max; index = max;
} }
return (u32)idr_index; return index;
} }
static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
...@@ -849,7 +841,7 @@ static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c, ...@@ -849,7 +841,7 @@ static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
if (pins->handle == n->handle) if (pins->handle == n->handle)
break; break;
idr_replace_ext(&ht->handle_idr, n, n->handle); idr_replace(&ht->handle_idr, n, n->handle);
RCU_INIT_POINTER(n->next, pins->next); RCU_INIT_POINTER(n->next, pins->next);
rcu_assign_pointer(*ins, n); rcu_assign_pointer(*ins, n);
} }
...@@ -1010,8 +1002,8 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -1010,8 +1002,8 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
return -ENOMEM; return -ENOMEM;
} }
} else { } else {
err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL, err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
handle, handle + 1, GFP_KERNEL); handle, GFP_KERNEL);
if (err) { if (err) {
kfree(ht); kfree(ht);
return err; return err;
...@@ -1027,7 +1019,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -1027,7 +1019,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
err = u32_replace_hw_hnode(tp, ht, flags, extack); err = u32_replace_hw_hnode(tp, ht, flags, extack);
if (err) { if (err) {
idr_remove_ext(&tp_c->handle_idr, handle); idr_remove(&tp_c->handle_idr, handle);
kfree(ht); kfree(ht);
return err; return err;
} }
...@@ -1067,8 +1059,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -1067,8 +1059,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
return -EINVAL; return -EINVAL;
} }
handle = htid | TC_U32_NODE(handle); handle = htid | TC_U32_NODE(handle);
err = idr_alloc_ext(&ht->handle_idr, NULL, NULL, err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
handle, handle + 1,
GFP_KERNEL); GFP_KERNEL);
if (err) if (err)
return err; return err;
...@@ -1163,7 +1154,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb, ...@@ -1163,7 +1154,7 @@ static int u32_change(struct net *net, struct sk_buff *in_skb,
#endif #endif
kfree(n); kfree(n);
erridr: erridr:
idr_remove_ext(&ht->handle_idr, handle); idr_remove(&ht->handle_idr, handle);
return err; return err;
} }
......
...@@ -153,11 +153,12 @@ void idr_nowait_test(void) ...@@ -153,11 +153,12 @@ void idr_nowait_test(void)
idr_destroy(&idr); idr_destroy(&idr);
} }
void idr_get_next_test(void) void idr_get_next_test(int base)
{ {
unsigned long i; unsigned long i;
int nextid; int nextid;
DEFINE_IDR(idr); DEFINE_IDR(idr);
idr_init_base(&idr, base);
int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0}; int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
...@@ -207,6 +208,7 @@ void idr_checks(void) ...@@ -207,6 +208,7 @@ void idr_checks(void)
assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i); assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
} }
assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC); assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
idr_for_each(&idr, item_idr_free, &idr); idr_for_each(&idr, item_idr_free, &idr);
idr_destroy(&idr); idr_destroy(&idr);
...@@ -214,6 +216,23 @@ void idr_checks(void) ...@@ -214,6 +216,23 @@ void idr_checks(void)
assert(idr_is_empty(&idr)); assert(idr_is_empty(&idr));
idr_set_cursor(&idr, INT_MAX - 3UL);
for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
struct item *item;
unsigned int id;
if (i <= INT_MAX)
item = item_create(i, 0);
else
item = item_create(i - INT_MAX - 1, 0);
id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
assert(id == item->index);
}
idr_for_each(&idr, item_idr_free, &idr);
idr_destroy(&idr);
assert(idr_is_empty(&idr));
for (i = 1; i < 10000; i++) { for (i = 1; i < 10000; i++) {
struct item *item = item_create(i, 0); struct item *item = item_create(i, 0);
assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i); assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
...@@ -226,7 +245,9 @@ void idr_checks(void) ...@@ -226,7 +245,9 @@ void idr_checks(void)
idr_alloc_test(); idr_alloc_test();
idr_null_test(); idr_null_test();
idr_nowait_test(); idr_nowait_test();
idr_get_next_test(); idr_get_next_test(0);
idr_get_next_test(1);
idr_get_next_test(4);
} }
/* /*
...@@ -380,7 +401,7 @@ void ida_check_random(void) ...@@ -380,7 +401,7 @@ void ida_check_random(void)
do { do {
ida_pre_get(&ida, GFP_KERNEL); ida_pre_get(&ida, GFP_KERNEL);
err = ida_get_new_above(&ida, bit, &id); err = ida_get_new_above(&ida, bit, &id);
} while (err == -ENOMEM); } while (err == -EAGAIN);
assert(!err); assert(!err);
assert(id == bit); assert(id == bit);
} }
...@@ -489,7 +510,7 @@ static void *ida_random_fn(void *arg) ...@@ -489,7 +510,7 @@ static void *ida_random_fn(void *arg)
void ida_thread_tests(void) void ida_thread_tests(void)
{ {
pthread_t threads[10]; pthread_t threads[20];
int i; int i;
for (i = 0; i < ARRAY_SIZE(threads); i++) for (i = 0; i < ARRAY_SIZE(threads); i++)
......
...@@ -17,6 +17,4 @@ ...@@ -17,6 +17,4 @@
#define pr_debug printk #define pr_debug printk
#define pr_cont printk #define pr_cont printk
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif /* _KERNEL_H */ #endif /* _KERNEL_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