From: Matthew Wilcox <willy@infradead.org>
To: unlisted-recipients:; (no To-header on input)
Cc: Matthew Wilcox <mawilcox@microsoft.com>,
Chris Mi <chrism@mellanox.com>, Jiri Pirko <jiri@mellanox.com>,
"David S . Miller" <davem@davemloft.net>,
Cong Wang <xiyou.wangcong@gmail.com>,
Jamal Hadi Salim <jhs@mojatatu.com>,
Daniel Borkmann <daniel@iogearbox.net>,
Eric Biggers <ebiggers@google.com>,
Lai Jiangshan <laijs@cn.fujitsu.com>, Tejun Heo <tj@kernel.org>,
Rehas Sachdeva <aquannie@gmail.com>,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 08/17] idr: Add idr_alloc_u32 helper
Date: Tue, 28 Nov 2017 13:33:03 -0800 [thread overview]
Message-ID: <20171128213312.28983-9-willy@infradead.org> (raw)
In-Reply-To: <20171128213312.28983-1-willy@infradead.org>
From: Matthew Wilcox <mawilcox@microsoft.com>
All current users of idr_alloc_ext() actually want to allocate a u32 and
it's a little painful for them to use idr_alloc_ext(). This convenience
function makes it simple.
Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
---
include/linux/idr.h | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/include/linux/idr.h b/include/linux/idr.h
index 12514ec0cd28..9b2fd6f408b2 100644
--- a/include/linux/idr.h
+++ b/include/linux/idr.h
@@ -139,6 +139,35 @@ void *idr_get_next_ext(struct idr *idr, unsigned long *nextid);
void *idr_replace(struct idr *, void *, unsigned long id);
void idr_destroy(struct idr *);
+/**
+ * idr_alloc_u32() - Allocate an ID.
+ * @idr: IDR handle.
+ * @ptr: Pointer to be associated with the new ID.
+ * @nextid: The new ID.
+ * @max: The maximum ID to allocate (inclusive).
+ * @gfp: Memory allocation flags.
+ *
+ * Allocates an unused ID in the range [*nextid, max] and updates the @nextid
+ * pointer with the newly assigned ID. Returns -ENOSPC and does not modify
+ * @nextid if there are no unused IDs in the 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: 0 on success, -ENOMEM for memory allocation errors, -ENOSPC if
+ * there are no free IDs in the range.
+ */
+static inline int __must_check idr_alloc_u32(struct idr *idr, void *ptr,
+ u32 *nextid, unsigned long max, gfp_t gfp)
+{
+ unsigned long tmp = *nextid;
+ int ret = idr_alloc_ext(idr, ptr, &tmp, tmp, max + 1, gfp);
+ *nextid = tmp;
+ return ret;
+}
+
static inline void *idr_remove(struct idr *idr, unsigned long id)
{
return radix_tree_delete_item(&idr->idr_rt, id, NULL);
--
2.15.0
next prev parent reply other threads:[~2017-11-28 21:37 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-28 21:32 [PATCH 00/17] IDR patches for 4.15 Matthew Wilcox
2017-11-28 21:32 ` [PATCH 01/17] idr: Fix build Matthew Wilcox
2017-11-28 21:32 ` [PATCH 02/17] radix tree test suite: Remove ARRAY_SIZE Matthew Wilcox
2017-11-28 21:32 ` [PATCH 03/17] idr test suite: Fix ida_test_random() Matthew Wilcox
2017-11-28 21:32 ` [PATCH 04/17] IDR test suite: Check handling negative end correctly Matthew Wilcox
2017-11-28 21:33 ` [PATCH 05/17] idr: Delete idr_remove_ext function Matthew Wilcox
2017-11-28 21:33 ` [PATCH 06/17] idr: Delete idr_replace_ext function Matthew Wilcox
2017-11-28 21:33 ` [PATCH 07/17] idr: Delete idr_find_ext function Matthew Wilcox
2017-11-28 21:33 ` Matthew Wilcox [this message]
2017-11-28 21:33 ` [PATCH 09/17] net sched actions: Convert to use idr_alloc_u32 Matthew Wilcox
2017-11-28 21:33 ` [PATCH 10/17] cls_basic: " Matthew Wilcox
2017-11-28 21:33 ` [PATCH 11/17] cls_bpf: " Matthew Wilcox
2017-11-29 1:08 ` Jakub Kicinski
2017-11-29 16:29 ` Matthew Wilcox
2017-11-29 16:35 ` David Miller
2017-11-28 21:33 ` [PATCH 12/17] cls_flower: Convert to idr_alloc_u32 Matthew Wilcox
2017-11-28 21:33 ` [PATCH 13/17] cls_u32: Reinstate cyclic allocation Matthew Wilcox
2017-11-28 21:33 ` [PATCH 14/17] cls_u32: Convert to idr_alloc_u32 Matthew Wilcox
2017-11-28 21:33 ` [PATCH 15/17] idr: Rename idr_alloc_ext to idr_alloc_ul Matthew Wilcox
2017-11-28 21:33 ` [PATCH 16/17] idr: Rename idr_for_each_entry_ext Matthew Wilcox
2017-11-28 21:33 ` [PATCH 17/17] idr: Warn if old iterators see large IDs Matthew Wilcox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171128213312.28983-9-willy@infradead.org \
--to=willy@infradead.org \
--cc=aquannie@gmail.com \
--cc=chrism@mellanox.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=ebiggers@google.com \
--cc=jhs@mojatatu.com \
--cc=jiri@mellanox.com \
--cc=laijs@cn.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mawilcox@microsoft.com \
--cc=netdev@vger.kernel.org \
--cc=tj@kernel.org \
--cc=xiyou.wangcong@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®