From nobody Thu Jun 06 03:43:41 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Vvqtj17wGz5Mvr1; Thu, 06 Jun 2024 03:43:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Vvqtj0x0Bz4hPg; Thu, 6 Jun 2024 03:43:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717645421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CLOeBszkP+viiOii1Dw1vP0k/JYLJhA5mVauRrtRaMg=; b=kHeWxksqgGegT6E+6+9DeAhzZh36o2+rFJ9lJmzPHBvh/lHBsp79u9uEXEd3nNzfyDNA/g fxaPXKsMveJWz1ZSzTmWVVYWZAbynrGNuaxR+jyzt2iyy/YdkNTpKb6hW0fwD8OOdQVxj5 1kaBwUhG/upA1NlnL7nbKJ+cdeZ7YVbi8y8RZC9ds+ngLTj9JFUurk2AAlSnay9cU15oa8 THs72qzxXHcFQZYZOL1MMmUUyFxmvxbdPormL9m6fuLHvrY8LtkKB1t++UqywUyXTxXRxp 8OAm6klU4N24aiCfwu3ZomvjcpoKfmExeVYMMoYX6JmvHXEscQBKddnCWn/5lw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1717645421; a=rsa-sha256; cv=none; b=PuM6kc/JI/3xnuErFhBgYGdx4yEmvVL/fu/+tnbbvNkDQ3fVZ5X3uebLLML8dsa8hvnkLM MdAaOkP5Hxqfh5Zl2+xCBSOVnniYEb/h4mphqUu0Eoiz4bWou6KBw3bNS6VJMD2LtzKeBe DpRR7X1BwLW4mQ6hBzsinieTIQqYg0DXWnJ2KnMmkmSMvH+GslPydVsOUSczo7gFFZrlYC oSX6pie7ONN7URq2++NRg2DhERxWJiW+I+kPnQZSVHBW1LV9FMnAKQfYYV8/tJdxLM8upk OLXP7ZTmO9+Oz/5vRbgeBHGTgX2rQslOkmGibSlWEZwo6ojYIQ43Oh0mHD354Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1717645421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CLOeBszkP+viiOii1Dw1vP0k/JYLJhA5mVauRrtRaMg=; b=plb2m7udRkzeWVaN6vSOUwE8IF/Pifiojn1oDdxPdzRzxT6uxo6Nj+3RcozSGMm8OkAxoS TxQBvY6FhldEMSf4jugbL7G7gPxPWPOcZxgG5SmDLkVbXaeIKKcxjkKknDVfvWfTlpyESM QEWPmnUN4bg4vykPMizFeF0FNdZwPDMEfDiGChs6rm32jy4914tQ24dNHGxd/kmV9+uSqv JEeT+eXC422EcU3ik2cxYTBkWQcfpPrY6AeFU1mojyEw5Qsbwy54oZtjdHVm/hBRRfGAye zzEPHdt/upSWB65uJgUqgC1oKGl8l6iHe4Wg74wgqu8iHLYfpF9bbWBfq7hHGA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Vvqtj0Xs0zVyc; Thu, 6 Jun 2024 03:43:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 4563hfxO089312; Thu, 6 Jun 2024 03:43:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 4563hfU2089309; Thu, 6 Jun 2024 03:43:41 GMT (envelope-from git) Date: Thu, 6 Jun 2024 03:43:41 GMT Message-Id: <202406060343.4563hfU2089309@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ryan Libby Subject: git: bbf81f46297f - main - pctrie: add combined insert/lookup operations List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: rlibby X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bbf81f46297f91ed6b4dde8877f4260c2d1e03f2 Auto-Submitted: auto-generated The branch main has been updated by rlibby: URL: https://cgit.FreeBSD.org/src/commit/?id=bbf81f46297f91ed6b4dde8877f4260c2d1e03f2 commit bbf81f46297f91ed6b4dde8877f4260c2d1e03f2 Author: Ryan Libby AuthorDate: 2024-06-06 02:17:20 +0000 Commit: Ryan Libby CommitDate: 2024-06-06 02:17:20 +0000 pctrie: add combined insert/lookup operations In several places in code, we do a pctrie lookup followed by a pctrie insert. Provide a few flavors of combined lookup/insert. This may save a portion of the work from walking a large pctrie twice. The general idea is that while we walk the trie during insert, we also do the same kind of tracking work that we do during pctrie_lookup_ge or pctrie_lookup_le, and we pass out a pctrie node from where such a lookup may continue. Reviewed by: dougm (previous version), kib (previous version), markj Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D45394 --- sys/kern/subr_pctrie.c | 205 +++++++++++++++++++++++++++++++++++++++++++++---- sys/sys/pctrie.h | 113 ++++++++++++++++++++++++++- 2 files changed, 298 insertions(+), 20 deletions(-) diff --git a/sys/kern/subr_pctrie.c b/sys/kern/subr_pctrie.c index 76f4ee17a8ca..4017f98c207d 100644 --- a/sys/kern/subr_pctrie.c +++ b/sys/kern/subr_pctrie.c @@ -260,13 +260,32 @@ pctrie_node_size(void) return (sizeof(struct pctrie_node)); } +enum pctrie_insert_neighbor_mode { + PCTRIE_INSERT_NEIGHBOR_NONE, + PCTRIE_INSERT_NEIGHBOR_LT, + PCTRIE_INSERT_NEIGHBOR_GT, +}; + /* - * Looks for where to insert the key-value pair into the trie. Completes the - * insertion if it replaces a null leaf; otherwise, returns insertion location - * to caller. Panics if the key already exists. + * Look for where to insert the key-value pair into the trie. Complete the + * insertion if it replaces a null leaf. Return the insertion location if the + * insertion needs to be completed by the caller; otherwise return NULL. + * + * If the key is already present in the trie, populate *found_out as if by + * pctrie_lookup(). + * + * With mode PCTRIE_INSERT_NEIGHBOR_GT or PCTRIE_INSERT_NEIGHBOR_LT, set + * *neighbor_out to the lowest level node we encounter during the insert lookup + * that is a parent of the next greater or lesser entry. The value is not + * defined if the key was already present in the trie. + * + * Note that mode is expected to be a compile-time constant, and this procedure + * is expected to be inlined into callers with extraneous code optimized out. */ -void * -pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val) +static __always_inline void * +pctrie_insert_lookup_compound(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out, struct pctrie_node **neighbor_out, + enum pctrie_insert_neighbor_mode mode) { uint64_t index; struct pctrie_node *node, *parent; @@ -290,18 +309,44 @@ pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val) pctrie_toleaf(val), PCTRIE_LOCKED); return (NULL); } - if (*pctrie_toval(node) == index) - panic("%s: key %jx is already present", - __func__, (uintmax_t)index); + if (*pctrie_toval(node) == index) { + *found_out = pctrie_toval(node); + return (NULL); + } break; } if (pctrie_keybarr(node, index, &slot)) break; + /* + * Descend. If we're tracking the next neighbor and this node + * contains a neighboring entry in the right direction, record + * it. + */ + if (mode == PCTRIE_INSERT_NEIGHBOR_LT) { + if ((node->pn_popmap & ((1 << slot) - 1)) != 0) + *neighbor_out = node; + } else if (mode == PCTRIE_INSERT_NEIGHBOR_GT) { + if ((node->pn_popmap >> slot) > 1) + *neighbor_out = node; + } parent = node; node = pctrie_node_load(&node->pn_child[slot], NULL, PCTRIE_LOCKED); } + /* + * The caller will split this node. If we're tracking the next + * neighbor, record the old node if the old entry is in the right + * direction. + */ + if (mode == PCTRIE_INSERT_NEIGHBOR_LT) { + if (*pctrie_toval(node) < index) + *neighbor_out = node; + } else if (mode == PCTRIE_INSERT_NEIGHBOR_GT) { + if (*pctrie_toval(node) > index) + *neighbor_out = node; + } + /* * 'node' must be replaced in the tree with a new branch node, with * children 'node' and 'val'. Return the place that points to 'node' @@ -311,6 +356,68 @@ pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val) (smr_pctnode_t *)&ptree->pt_root); } +/* + * Wrap pctrie_insert_lookup_compound to implement a strict insertion. Panic + * if the key already exists, and do not look for neighboring entries. + */ +void * +pctrie_insert_lookup_strict(struct pctrie *ptree, uint64_t *val) +{ + void *parentp; + uint64_t *found; + + found = NULL; + parentp = pctrie_insert_lookup_compound(ptree, val, &found, NULL, + PCTRIE_INSERT_NEIGHBOR_NONE); + if (__predict_false(found != NULL)) + panic("%s: key %jx is already present", __func__, + (uintmax_t)*val); + return (parentp); +} + +/* + * Wrap pctrie_insert_lookup_compound to implement find-or-insert. Do not look + * for neighboring entries. + */ +void * +pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out) +{ + *found_out = NULL; + return (pctrie_insert_lookup_compound(ptree, val, found_out, NULL, + PCTRIE_INSERT_NEIGHBOR_NONE)); +} + +/* + * Wrap pctrie_insert_lookup_compound to implement find or insert and find next + * greater entry. Find a subtree that contains the next entry greater than the + * newly-inserted or to-be-inserted entry. + */ +void * +pctrie_insert_lookup_gt(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out, struct pctrie_node **neighbor_out) +{ + *found_out = NULL; + *neighbor_out = NULL; + return (pctrie_insert_lookup_compound(ptree, val, found_out, + neighbor_out, PCTRIE_INSERT_NEIGHBOR_GT)); +} + +/* + * Wrap pctrie_insert_lookup_compound to implement find or insert and find next + * lesser entry. Find a subtree that contains the next entry less than the + * newly-inserted or to-be-inserted entry. + */ +void * +pctrie_insert_lookup_lt(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out, struct pctrie_node **neighbor_out) +{ + *found_out = NULL; + *neighbor_out = NULL; + return (pctrie_insert_lookup_compound(ptree, val, found_out, + neighbor_out, PCTRIE_INSERT_NEIGHBOR_LT)); +} + /* * Uses new node to insert key-value pair into the trie at given location. */ @@ -422,10 +529,10 @@ pctrie_lookup_unlocked(struct pctrie *ptree, uint64_t index, smr_t smr) * * Requires that access be externally synchronized by a lock. */ -uint64_t * -pctrie_lookup_ge(struct pctrie *ptree, uint64_t index) +static __inline uint64_t * +pctrie_lookup_ge_node(struct pctrie_node *node, uint64_t index) { - struct pctrie_node *node, *succ; + struct pctrie_node *succ; uint64_t *m; int slot; @@ -442,7 +549,6 @@ pctrie_lookup_ge(struct pctrie *ptree, uint64_t index) * "succ". If "succ" is not NULL, then that lookup is guaranteed to * succeed. */ - node = pctrie_root_load(ptree, NULL, PCTRIE_LOCKED); succ = NULL; for (;;) { if (pctrie_isleaf(node)) { @@ -505,23 +611,55 @@ pctrie_lookup_ge(struct pctrie *ptree, uint64_t index) return (pctrie_toval(succ)); } +uint64_t * +pctrie_lookup_ge(struct pctrie *ptree, uint64_t index) +{ + return (pctrie_lookup_ge_node( + pctrie_root_load(ptree, NULL, PCTRIE_LOCKED), index)); +} + +uint64_t * +pctrie_subtree_lookup_gt(struct pctrie_node *node, uint64_t index) +{ + if (node == NULL || index + 1 == 0) + return (NULL); + return (pctrie_lookup_ge_node(node, index + 1)); +} + +#ifdef INVARIANTS +void +pctrie_subtree_lookup_gt_assert(struct pctrie_node *node, uint64_t index, + struct pctrie *ptree, uint64_t *res) +{ + uint64_t *expected; + + if (index + 1 == 0) + expected = NULL; + else + expected = pctrie_lookup_ge(ptree, index + 1); + KASSERT(res == expected, + ("pctrie subtree lookup gt result different from root lookup: " + "ptree %p, index %ju, subtree %p, found %p, expected %p", ptree, + (uintmax_t)index, node, res, expected)); +} +#endif + /* * Returns the value with the greatest index that is less than or equal to the * specified index, or NULL if there are no such values. * * Requires that access be externally synchronized by a lock. */ -uint64_t * -pctrie_lookup_le(struct pctrie *ptree, uint64_t index) +static __inline uint64_t * +pctrie_lookup_le_node(struct pctrie_node *node, uint64_t index) { - struct pctrie_node *node, *pred; + struct pctrie_node *pred; uint64_t *m; int slot; /* - * Mirror the implementation of pctrie_lookup_ge, described above. + * Mirror the implementation of pctrie_lookup_ge_node, described above. */ - node = pctrie_root_load(ptree, NULL, PCTRIE_LOCKED); pred = NULL; for (;;) { if (pctrie_isleaf(node)) { @@ -560,6 +698,39 @@ pctrie_lookup_le(struct pctrie *ptree, uint64_t index) return (pctrie_toval(pred)); } +uint64_t * +pctrie_lookup_le(struct pctrie *ptree, uint64_t index) +{ + return (pctrie_lookup_le_node( + pctrie_root_load(ptree, NULL, PCTRIE_LOCKED), index)); +} + +uint64_t * +pctrie_subtree_lookup_lt(struct pctrie_node *node, uint64_t index) +{ + if (node == NULL || index == 0) + return (NULL); + return (pctrie_lookup_le_node(node, index - 1)); +} + +#ifdef INVARIANTS +void +pctrie_subtree_lookup_lt_assert(struct pctrie_node *node, uint64_t index, + struct pctrie *ptree, uint64_t *res) +{ + uint64_t *expected; + + if (index == 0) + expected = NULL; + else + expected = pctrie_lookup_le(ptree, index - 1); + KASSERT(res == expected, + ("pctrie subtree lookup lt result different from root lookup: " + "ptree %p, index %ju, subtree %p, found %p, expected %p", ptree, + (uintmax_t)index, node, res, expected)); +} +#endif + /* * Remove the specified index from the tree, and return the value stored at * that index. If the index is not present, return NULL. diff --git a/sys/sys/pctrie.h b/sys/sys/pctrie.h index 38b297899592..06b9fca79528 100644 --- a/sys/sys/pctrie.h +++ b/sys/sys/pctrie.h @@ -47,6 +47,16 @@ name##_PCTRIE_LOOKUP_UNLOCKED(struct pctrie *ptree, uint64_t key) \ key, (smr))); \ } \ +#ifdef INVARIANTS +void pctrie_subtree_lookup_gt_assert(struct pctrie_node *node, + uint64_t key, struct pctrie *ptree, uint64_t *res); +void pctrie_subtree_lookup_lt_assert(struct pctrie_node *node, + uint64_t key, struct pctrie *ptree, uint64_t *res); +#else +#define pctrie_subtree_lookup_gt_assert(node, key, ptree, res) +#define pctrie_subtree_lookup_lt_assert(node, key, ptree, res) +#endif + #define PCTRIE_DEFINE(name, type, field, allocfn, freefn) \ \ CTASSERT(sizeof(((struct type *)0)->field) == sizeof(uint64_t)); \ @@ -73,14 +83,14 @@ name##_PCTRIE_PTR2VAL(struct type *ptr) \ return &ptr->field; \ } \ \ -static __inline int \ +static __inline __unused int \ name##_PCTRIE_INSERT(struct pctrie *ptree, struct type *ptr) \ { \ struct pctrie_node *parent; \ void *parentp; \ uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ \ - parentp = pctrie_insert_lookup(ptree, val); \ + parentp = pctrie_insert_lookup_strict(ptree, val); \ if (parentp == NULL) \ return (0); \ parent = allocfn(ptree); \ @@ -90,6 +100,92 @@ name##_PCTRIE_INSERT(struct pctrie *ptree, struct type *ptr) \ return (0); \ } \ \ +static __inline __unused int \ +name##_PCTRIE_FIND_OR_INSERT(struct pctrie *ptree, struct type *ptr, \ + struct type **found_out_opt) \ +{ \ + struct pctrie_node *parent; \ + void *parentp; \ + uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ + uint64_t *found; \ + \ + parentp = pctrie_insert_lookup(ptree, val, &found); \ + if (found != NULL) { \ + if (found_out_opt != NULL) \ + *found_out_opt = name##_PCTRIE_VAL2PTR(found); \ + return (EEXIST); \ + } \ + if (parentp == NULL) \ + return (0); \ + parent = allocfn(ptree); \ + if (__predict_false(parent == NULL)) \ + return (ENOMEM); \ + pctrie_insert_node(parentp, parent, val); \ + return (0); \ +} \ + \ +static __inline __unused int \ +name##_PCTRIE_INSERT_LOOKUP_GE(struct pctrie *ptree, struct type *ptr, \ + struct type **found_out) \ +{ \ + struct pctrie_node *parent, *neighbor; \ + void *parentp; \ + uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ + uint64_t *found; \ + \ + parentp = pctrie_insert_lookup_gt(ptree, val, &found, \ + &neighbor); \ + if (__predict_false(found != NULL)) { \ + *found_out = name##_PCTRIE_VAL2PTR(found); \ + return (EEXIST); \ + } \ + if (parentp != NULL) { \ + parent = allocfn(ptree); \ + if (__predict_false(parent == NULL)) { \ + *found_out = NULL; \ + return (ENOMEM); \ + } \ + if (neighbor == parentp) \ + neighbor = parent; \ + pctrie_insert_node(parentp, parent, val); \ + } \ + found = pctrie_subtree_lookup_gt(neighbor, *val); \ + *found_out = name##_PCTRIE_VAL2PTR(found); \ + pctrie_subtree_lookup_gt_assert(neighbor, *val, ptree, found); \ + return (0); \ +} \ + \ +static __inline __unused int \ +name##_PCTRIE_INSERT_LOOKUP_LE(struct pctrie *ptree, struct type *ptr, \ + struct type **found_out) \ +{ \ + struct pctrie_node *parent, *neighbor; \ + void *parentp; \ + uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \ + uint64_t *found; \ + \ + parentp = pctrie_insert_lookup_lt(ptree, val, &found, \ + &neighbor); \ + if (__predict_false(found != NULL)) { \ + *found_out = name##_PCTRIE_VAL2PTR(found); \ + return (EEXIST); \ + } \ + if (parentp != NULL) { \ + parent = allocfn(ptree); \ + if (__predict_false(parent == NULL)) { \ + *found_out = NULL; \ + return (ENOMEM); \ + } \ + if (neighbor == parentp) \ + neighbor = parent; \ + pctrie_insert_node(parentp, parent, val); \ + } \ + found = pctrie_subtree_lookup_lt(neighbor, *val); \ + *found_out = name##_PCTRIE_VAL2PTR(found); \ + pctrie_subtree_lookup_lt_assert(neighbor, *val, ptree, found); \ + return (0); \ +} \ + \ static __inline __unused struct type * \ name##_PCTRIE_LOOKUP(struct pctrie *ptree, uint64_t key) \ { \ @@ -155,7 +251,14 @@ name##_PCTRIE_REMOVE_LOOKUP(struct pctrie *ptree, uint64_t key) \ return name##_PCTRIE_VAL2PTR(val); \ } -void *pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val); +void *pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out); +void *pctrie_insert_lookup_gt(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out, struct pctrie_node **neighbor_out); +void *pctrie_insert_lookup_lt(struct pctrie *ptree, uint64_t *val, + uint64_t **found_out, struct pctrie_node **neighbor_out); +void *pctrie_insert_lookup_strict(struct pctrie *ptree, + uint64_t *val); void pctrie_insert_node(void *parentp, struct pctrie_node *parent, uint64_t *val); uint64_t *pctrie_lookup(struct pctrie *ptree, uint64_t key); @@ -169,6 +272,10 @@ struct pctrie_node *pctrie_reclaim_resume(struct pctrie_node **pnode); uint64_t *pctrie_remove_lookup(struct pctrie *ptree, uint64_t index, struct pctrie_node **killnode); uint64_t *pctrie_replace(struct pctrie *ptree, uint64_t *newval); +uint64_t *pctrie_subtree_lookup_gt(struct pctrie_node *node, + uint64_t key); +uint64_t *pctrie_subtree_lookup_lt(struct pctrie_node *node, + uint64_t key); size_t pctrie_node_size(void); int pctrie_zone_init(void *mem, int size, int flags);