Assorted rhashtable improvements
Subsystems (13)
cover Assorted rhashtable improvements
Hi,
the following is my current set of rhashtable improvements. Some have been seen before, some have been improved, others are new.
They include:
working list-nulls support
stability improvements for rhashtable_walk bit-spin-locks for simplicity and reduced cache footprint
during modification
optional per-cpu locks to improve scalability for modificiation various cleanups
If I get suitable acks I will send more focused subsets to Davem for inclusion.
I had said previously that I thought there was a way to provide stable walking of an rhl table in the face of concurrent insert/delete. Having tried, I no longer think this can be done without substantial impact to lookups and/or other operations. The idea of attaching a marker to the list is incompatible with the normal rules for working with rcu-protected lists ("attaching" might be manageable. "moving" or "removing" is the problematic part).
The last patch is the one I'm least certain of. It seems like a good idea to improve the chance of a walk avoiding any rehash, but it cannot provide a solid guarantee without risking a denial-of-service. My compromise is to guarantee no rehashes caused by shrinkage, and discourage rehashes caused by growth. I'm not yet sure if that is sufficiently valuable, but I thought I would include the patch in the RFC anyway.
Thanks,
NeilBrown
---
NeilBrown (18):
rhashtable: silence RCU warning in rhashtable_test.
rhashtable: split rhashtable.h
rhashtable: remove nulls_base and related code.
rhashtable: detect when object movement might have invalidated a lookup
rhashtable: simplify INIT_RHT_NULLS_HEAD()
rhashtable: simplify nested_table_alloc() and rht_bucket_nested_insert()
rhashtable: use cmpxchg() to protect ->future_tbl.
rhashtable: clean up dereference of ->future_tbl.
rhashtable: use cmpxchg() in nested_table_alloc()
rhashtable: remove rhashtable_walk_peek()
rhashtable: further improve stability of rhashtable_walk
rhashtable: add rhashtable_walk_prev()
rhashtable: don't hold lock on first table throughout insertion.
rhashtable: allow rht_bucket_var to return NULL.
rhashtable: use bit_spin_locks to protect hash bucket.
rhashtable: allow percpu element counter
rhashtable: rename rht_for_each*continue as *from.
rhashtable: add rhashtable_walk_delay_rehash()
.clang-format | 8 MAINTAINERS | 2 drivers/net/ethernet/chelsio/cxgb4/cxgb4.h | 1 drivers/staging/lustre/lustre/fid/fid_request.c | 2 drivers/staging/lustre/lustre/fld/fld_request.c | 1 drivers/staging/lustre/lustre/include/lu_object.h | 1 include/linux/ipc.h | 2 include/linux/ipc_namespace.h | 2 include/linux/mroute_base.h | 2 include/linux/percpu_counter.h | 4 include/linux/rhashtable-types.h | 147 ++++++ include/linux/rhashtable.h | 537 +++++++++++---------- include/net/inet_frag.h | 2 include/net/netfilter/nf_flow_table.h | 2 include/net/sctp/structs.h | 2 include/net/seg6.h | 2 include/net/seg6_hmac.h | 2 ipc/msg.c | 1 ipc/sem.c | 1 ipc/shm.c | 1 ipc/util.c | 2 lib/rhashtable.c | 481 +++++++++++-------- lib/test_rhashtable.c | 22 + net/bridge/br_fdb.c | 1 net/bridge/br_vlan.c | 1 net/bridge/br_vlan_tunnel.c | 1 net/ipv4/inet_fragment.c | 1 net/ipv4/ipmr.c | 2 net/ipv4/ipmr_base.c | 1 net/ipv6/ip6mr.c | 2 net/ipv6/seg6.c | 1 net/ipv6/seg6_hmac.c | 1 net/netfilter/nf_tables_api.c | 1 net/sctp/input.c | 1 net/sctp/socket.c | 1 35 files changed, 760 insertions(+), 481 deletions(-) create mode 100644 include/linux/rhashtable-types.h
--
Signature
PATCH 1/18 rhashtable: silence RCU warning in rhashtable_test.
print_ht in rhashtable_test calls rht_dereference() with neither RCU protection or the mutex. This triggers an RCU warning. So take the mutex to silence the warning.
--- lib/test_rhashtable.c | 3 +++ 1 file changed, 3 insertions(+)
lib/test_rhashtable.c +3 -0
@@ -499,6 +499,8 @@ static unsigned int __init print_ht(struct rhltable *rhlt)unsigned int i, cnt =0 ; ht = &rhlt->ht; +/* Take the mutex to avoid RCU warning */ +mutex_lock (&ht->mutex); tbl =rht_dereference (ht->tbl, ht);for (i =0 ; i < tbl->size; i++) {struct rhash_head *pos, *next;
@@ -532,6 +534,7 @@ static unsigned int __init print_ht(struct rhltable *rhlt) } }printk (KERN_ERR "\n---- ht: ----%s\n-------------\n" , buff); +mutex_unlock (&ht->mutex);return cnt; }
PATCH 2/18 rhashtable: split rhashtable.h
In reply to NeilBrownDue to the use of rhashtables in net namespaces, rhashtable.h is included in lots of the kernel, so a small changes can required a large recompilation. This makes development painful.
This patch splits out rhashtable-types.h which just includes the major type declarations, and does not include (non-trivial) inline code. rhashtable.h is no longer included by anything in the include/ directory.
Common include files only include rhashtable-types.h so a large recompilation is only triggered when that changes.
--- MAINTAINERS | 2 drivers/net/ethernet/chelsio/cxgb4/cxgb4.h | 1 drivers/staging/lustre/lustre/fid/fid_request.c | 2 drivers/staging/lustre/lustre/fld/fld_request.c | 1 drivers/staging/lustre/lustre/include/lu_object.h | 1 include/linux/ipc.h | 2 include/linux/ipc_namespace.h | 2 include/linux/mroute_base.h | 2 include/linux/rhashtable-types.h | 139 +++++++++++++++++++++ include/linux/rhashtable.h | 127 ------------------- include/net/inet_frag.h | 2 include/net/netfilter/nf_flow_table.h | 2 include/net/sctp/structs.h | 2 include/net/seg6.h | 2 include/net/seg6_hmac.h | 2 ipc/msg.c | 1 ipc/sem.c | 1 ipc/shm.c | 1 ipc/util.c | 1 lib/rhashtable.c | 1 net/ipv4/inet_fragment.c | 1 net/ipv4/ipmr.c | 1 net/ipv4/ipmr_base.c | 1 net/ipv6/ip6mr.c | 1 net/ipv6/seg6.c | 1 net/ipv6/seg6_hmac.c | 1 net/netfilter/nf_tables_api.c | 1 net/sctp/input.c | 1 net/sctp/socket.c | 1 29 files changed, 169 insertions(+), 134 deletions(-) create mode 100644 include/linux/rhashtable-types.h
MAINTAINERS +2 -0
@@ -12012,7 +12012,9 @@ M: Herbert Xu <[email protected]> L: [email protected] S: Maintained F: lib/rhashtable.c +F: lib/test_rhashtable.c F: include/linux/rhashtable.h +F: include/linux/rhashtable-types.h RICOH R5C592 MEMORYSTICK DRIVER M: Maxim Levitsky <[email protected]>
drivers/net/ethernet/chelsio/cxgb4/cxgb4.h +1 -0
@@ -46,6 +46,7 @@#include <linux/spinlock.h> #include <linux/timer.h> #include <linux/vmalloc.h> +#include <linux/rhashtable.h> #include <linux/etherdevice.h> #include <linux/net_tstamp.h> #include <linux/ptp_clock_kernel.h>
drivers/staging/lustre/lustre/fid/fid_request.c +1 -1
@@ -40,7 +40,7 @@#define DEBUG_SUBSYSTEM S_FID#include <linux/module.h> - +#include <linux/rhashtable.h> #include <obd.h> #include <obd_class.h> #include <obd_support.h>
drivers/staging/lustre/lustre/fld/fld_request.c +1 -0
@@ -40,6 +40,7 @@#define DEBUG_SUBSYSTEM S_FLD#include <linux/module.h> +#include <linux/rhashtable.h> #include <asm/div64.h> #include <obd.h>
drivers/staging/lustre/lustre/include/lu_object.h +1 -0
@@ -37,6 +37,7 @@#include <stdarg.h> #include <linux/percpu_counter.h> #include <linux/libcfs/libcfs.h> +#include <linux/rhashtable-types.h> #include <uapi/linux/lustre/lustre_idl.h> #include <lu_ref.h>
include/linux/ipc.h +1 -1
@@ -4,7 +4,7 @@#include <linux/spinlock.h> #include <linux/uidgid.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> #include <uapi/linux/ipc.h> #include <linux/refcount.h>
include/linux/ipc_namespace.h +1 -1
@@ -9,7 +9,7 @@#include <linux/nsproxy.h> #include <linux/ns_common.h> #include <linux/refcount.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> struct user_namespace ;
include/linux/mroute_base.h +1 -1
@@ -2,7 +2,7 @@#define __LINUX_MROUTE_BASE_H#include <linux/netdevice.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> #include <linux/spinlock.h> #include <net/net_namespace.h> #include <net/sock.h>
include/linux/rhashtable-types.h new file +139 -0
139 lines not on this page · raw message
include/linux/rhashtable.h +2 -125
161 lines not on this page · raw message
include/net/inet_frag.h +1 -1
@@ -2,7 +2,7 @@#ifndef __NET_FRAG_H__#define __NET_FRAG_H__ -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> struct netns_frags {/* sysctls */
include/net/netfilter/nf_flow_table.h +1 -1
@@ -4,7 +4,7 @@#include <linux/in.h> #include <linux/in6.h> #include <linux/netdevice.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> #include <linux/rcupdate.h> #include <net/dst.h>
include/net/sctp/structs.h +1 -1
@@ -48,7 +48,7 @@#define __sctp_structs_h__#include <linux/ktime.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> #include <linux/socket.h> /* linux/in.h needs this!! */ #include <linux/in.h> /* We get struct sockaddr_in. */ #include <linux/in6.h> /* We get struct in6_addr */
include/net/seg6.h +1 -1
@@ -18,7 +18,7 @@#include <linux/ipv6.h> #include <net/lwtunnel.h> #include <linux/seg6.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> static inline void update_csum_diff4 (struct sk_buff *skb,__be32 from,__be32 to)
include/net/seg6_hmac.h +1 -1
@@ -22,7 +22,7 @@#include <linux/route.h> #include <net/seg6.h> #include <linux/seg6_hmac.h> -#include <linux/rhashtable.h> +#include <linux/rhashtable-types.h> #define SEG6_HMAC_MAX_DIGESTSIZE 160#define SEG6_HMAC_RING_SIZE 256
ipc/msg.c +1 -0
@@ -38,6 +38,7 @@#include <linux/rwsem.h> #include <linux/nsproxy.h> #include <linux/ipc_namespace.h> +#include <linux/rhashtable.h> #include <asm/current.h> #include <linux/uaccess.h>
ipc/sem.c +1 -0
@@ -84,6 +84,7 @@#include <linux/nsproxy.h> #include <linux/ipc_namespace.h> #include <linux/sched/wake_q.h> +#include <linux/rhashtable.h> #include <linux/uaccess.h> #include "util.h"
ipc/shm.c +1 -0
@@ -43,6 +43,7 @@#include <linux/nsproxy.h> #include <linux/mount.h> #include <linux/ipc_namespace.h> +#include <linux/rhashtable.h> #include <linux/uaccess.h>
ipc/util.c +1 -0
@@ -63,6 +63,7 @@#include <linux/rwsem.h> #include <linux/memory.h> #include <linux/ipc_namespace.h> +#include <linux/rhashtable.h> #include <asm/unistd.h>
lib/rhashtable.c +1 -0
@@ -28,6 +28,7 @@#include <linux/rhashtable.h> #include <linux/err.h> #include <linux/export.h> +#include <linux/rhashtable.h> #define HASH_DEFAULT_SIZE 64UL#define HASH_MIN_SIZE 4U
net/ipv4/inet_fragment.c +1 -0
@@ -20,6 +20,7 @@#include <linux/skbuff.h> #include <linux/rtnetlink.h> #include <linux/slab.h> +#include <linux/rhashtable.h> #include <net/sock.h> #include <net/inet_frag.h>
net/ipv4/ipmr.c +1 -0
@@ -60,6 +60,7 @@#include <linux/netfilter_ipv4.h> #include <linux/compat.h> #include <linux/export.h> +#include <linux/rhashtable.h> #include <net/ip_tunnels.h> #include <net/checksum.h> #include <net/netlink.h>
net/ipv4/ipmr_base.c +1 -0
@@ -2,6 +2,7 @@* Common logic shared by IPv4 [ipmr] and IPv6 [ip6mr] implementation */ +#include <linux/rhashtable.h> #include <linux/mroute_base.h> /* Sets everything common except 'dev', since that is done under locking */
net/ipv6/ip6mr.c +1 -0
@@ -32,6 +32,7 @@#include <linux/seq_file.h> #include <linux/init.h> #include <linux/compat.h> +#include <linux/rhashtable.h> #include <net/protocol.h> #include <linux/skbuff.h> #include <net/raw.h>
net/ipv6/seg6.c +1 -0
@@ -17,6 +17,7 @@#include <linux/net.h> #include <linux/in6.h> #include <linux/slab.h> +#include <linux/rhashtable.h> #include <net/ipv6.h> #include <net/protocol.h>
net/ipv6/seg6_hmac.c +1 -0
@@ -22,6 +22,7 @@#include <linux/icmpv6.h> #include <linux/mroute6.h> #include <linux/slab.h> +#include <linux/rhashtable.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv6.h>
net/netfilter/nf_tables_api.c +1 -0
@@ -14,6 +14,7 @@#include <linux/skbuff.h> #include <linux/netlink.h> #include <linux/vmalloc.h> +#include <linux/rhashtable.h> #include <linux/netfilter.h> #include <linux/netfilter/nfnetlink.h> #include <linux/netfilter/nf_tables.h>
net/sctp/input.c +1 -0
@@ -56,6 +56,7 @@#include <net/sctp/sm.h> #include <net/sctp/checksum.h> #include <net/net_namespace.h> +#include <linux/rhashtable.h> /* Forward declarations for internal helpers. */ static int sctp_rcv_ootb (struct sk_buff *);
net/sctp/socket.c +1 -0
@@ -66,6 +66,7 @@#include <linux/slab.h> #include <linux/file.h> #include <linux/compat.h> +#include <linux/rhashtable.h> #include <net/ip.h> #include <net/icmp.h>
Re: [PATCH 02/18] rhashtable: split rhashtable.h
Quoted 5 lines · NeilBrown
Due to the use of rhashtables in net namespaces,
rhashtable.his included in lots of the kernel, so a small changes can required a large recompilation. This makes development painful.
Quoted 8 lines
This patch splits out
rhashtable-types.hwhich just includes the major type declarations, and does not include (non-trivial) inline code.rhashtable.his no longer included by anything in theinclude/directory.
Common include files only includerhashtable-types.hso a large recompilation is only triggered when that changes.Signed-off-by NeilBrown <[email protected]>
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 3/18 rhashtable: remove nulls_base and related code.
In reply to NeilBrownThis "feature" is unused, undocumented, and untested and so doesn't really belong. Next patch will introduce support to detect when a search gets diverted down a different chain, which the common purpose of nulls markers.
This patch actually fixes a bug too. The table resizing allows a table to grow to 2^31 buckets, but the hash is truncated to 27 bits - any growth beyond 2^27 is wasteful an ineffective.
This patch results in NULLS_MARKER(0) being used for all chains, and leaves the use of rht_is_a_null() to test for it.
--- include/linux/rhashtable-types.h | 2 -- include/linux/rhashtable.h | 33 +++------------------------------ lib/rhashtable.c | 8 -------- lib/test_rhashtable.c | 5 +---- 4 files changed, 4 insertions(+), 44 deletions(-)
include/linux/rhashtable-types.h +0 -2
@@ -50,7 +50,6 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,* @min_size: Minimum size while shrinking * @locks_mul: Number of bucket locks to allocate per cpu (default: 32) * @automatic_shrinking: Enable automatic shrinking of tables -* @nulls_base: Base value to generate nulls marker * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) * @obj_hashfn: Function to hash object * @obj_cmpfn: Function to compare key with object
@@ -64,7 +63,6 @@ struct rhashtable_params {u16 min_size;bool automatic_shrinking;u8 locks_mul; -u32 nulls_base;rht_hashfn_t hashfn;rht_obj_hashfn_t obj_hashfn;rht_obj_cmpfn_t obj_cmpfn;
include/linux/rhashtable.h +3 -30
@@ -28,25 +28,8 @@#include <linux/rhashtable-types.h> /* * The end of the chain is marked with a special nulls marks which has -* the following format: -* -* +-------+-----------------------------------------------------+-+ -* | Base | Hash |1| -* +-------+-----------------------------------------------------+-+ -* -* Base (4 bits) : Reserved to distinguish between multiple tables. -* Specified via &struct rhashtable_params.nulls_base. -* Hash (27 bits): Full hash (unmasked) of first element added to bucket -* 1 (1 bit) : Nulls marker (always set) -* -* The remaining bits of the next pointer remain unused for now. +* the least significant bit set. */ -#define RHT_BASE_BITS 4 -#define RHT_HASH_BITS 27 -#define RHT_BASE_SHIFT RHT_HASH_BITS - -/* Base bits plus 1 bit for nulls marker */ -#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)/* Maximum chain length before rehash *
@@ -92,24 +75,14 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; -static inline unsigned long rht_marker (const struct rhashtable *ht,u32 hash) -{ -return NULLS_MARKER (ht->p.nulls_base + hash); -} -#define INIT_RHT_NULLS_HEAD (ptr, ht, hash) \ - ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) + ((ptr) = (typeof(ptr)) NULLS_MARKER(0))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {return ((unsigned long ) ptr &1 ); } -static inline unsigned long rht_get_nulls_value (const struct rhash_head *ptr) -{ -return ((unsigned long ) ptr) >>1 ; -} -static inline void *rht_obj (const struct rhashtable *ht,const struct rhash_head *he) {
@@ -119,7 +92,7 @@ static inline void *rht_obj(const struct rhashtable *ht,static inline unsigned int rht_bucket_index (const struct bucket_table *tbl,unsigned int hash) { -return (hash >>RHT_HASH_RESERVED_SPACE ) & (tbl->size -1 ); +return hash & (tbl->size -1 ); } static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
lib/rhashtable.c +0 -8
@@ -995,7 +995,6 @@ static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)* .key_offset = offsetof(struct test_obj, key), * .key_len = sizeof(int), * .hashfn = jhash, -* .nulls_base = (1U << RHT_BASE_SHIFT), * }; * * Configuration Example 2: Variable length keys
@@ -1029,9 +1028,6 @@ int rhashtable_init(struct rhashtable *ht, (params->obj_hashfn && !params->obj_cmpfn))return -EINVAL ; -if (params->nulls_base && params->nulls_base < (1U <<RHT_BASE_SHIFT )) -return -EINVAL ; -memset (ht,0 ,sizeof (*ht));mutex_init (&ht->mutex);spin_lock_init (&ht->lock);
@@ -1096,10 +1092,6 @@ int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params) {int err; -/* No rhlist NULLs marking for now. */ -if (params->nulls_base) -return -EINVAL ; - err =rhashtable_init (&hlt->ht, params); hlt->ht.rhlist = true;return err;
lib/test_rhashtable.c +1 -4
@@ -83,7 +83,7 @@ static u32 my_hashfn(const void *data, u32 len, u32 seed) {const struct test_obj_rhl *obj = data; -return (obj->value.id %10 ) <<RHT_HASH_RESERVED_SPACE ; +return (obj->value.id %10 ); }static int my_cmpfn (struct rhashtable_compare_arg *arg,const void *obj)
@@ -99,7 +99,6 @@ static struct rhashtable_params test_rht_params = { .key_offset = offsetof(struct test_obj, value), .key_len = sizeof(struct test_obj_val), .hashfn = jhash, - .nulls_base = (3U << RHT_BASE_SHIFT), };static struct rhashtable_params test_rht_params_dup = {
@@ -294,8 +293,6 @@ static int __init test_rhltable(unsigned int entries)if (!obj_in_table) goto out_free; -/* nulls_base not supported in rhlist interface */ - test_rht_params.nulls_base =0 ; err =rhltable_init (&rhlt, &test_rht_params);if (WARN_ON (err)) goto out_free;
Re: [PATCH 03/18] rhashtable: remove nulls_base and related code.
Quoted 5 lines · NeilBrown
This "feature" is unused, undocumented, and untested and so doesn't really belong. Next patch will introduce support to detect when a search gets diverted down a different chain, which the common purpose of nulls markers.
Quoted 8 lines
This patch actually fixes a bug too. The table resizing allows a table to grow to 2^31 buckets, but the hash is truncated to 27 bits - any growth beyond 2^27 is wasteful an ineffective.
This patch results in
NULLS_MARKER(0)being used for all chains, and leaves the use ofrht_is_a_null()to test for it.Signed-off-by NeilBrown <[email protected]>
Hi Herbert,
You've acked a few patches that depends on this one, but not this patch itself. If you could ack this one, I could submit a collection of patches for inclusion (after the merge window closes I guess) and then have fewer outstanding.
This assumes you are in-principle happy with the alternative approach I took to handling list-nulls. I got the impression that it was only some small details holding that back.
Thanks,
NeilBrown
Quoted 156 lines
--- include/linux/rhashtable-types.h | 2 -- include/linux/rhashtable.h | 33 +++------------------------------ lib/rhashtable.c | 8 -------- lib/test_rhashtable.c | 5 +---- 4 files changed, 4 insertions(+), 44 deletions(-)include/linux/rhashtable-types.h +0
-2@@ -50,7 +50,6 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,* @min_size: Minimum size while shrinking * @locks_mul: Number of bucket locks to allocate per cpu (default: 32) * @automatic_shrinking: Enable automatic shrinking of tables -* @nulls_base: Base value to generate nulls marker * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) * @obj_hashfn: Function to hash object * @obj_cmpfn: Function to compare key with object @@ -64,7 +63,6 @@ struct rhashtable_params {u16 min_size;bool automatic_shrinking;u8 locks_mul; -u32 nulls_base;rht_hashfn_t hashfn;rht_obj_hashfn_t obj_hashfn;rht_obj_cmpfn_t obj_cmpfn;include/linux/rhashtable.h +3
-30@@ -28,25 +28,8 @@#include <linux/rhashtable-types.h> /* * The end of the chain is marked with a special nulls marks which has -* the following format: -* -* +-------+-----------------------------------------------------+-+ -* | Base | Hash |1| -* +-------+-----------------------------------------------------+-+ -* -* Base (4 bits) : Reserved to distinguish between multiple tables. -* Specified via &struct rhashtable_params.nulls_base. -* Hash (27 bits): Full hash (unmasked) of first element added to bucket -* 1 (1 bit) : Nulls marker (always set) -* -* The remaining bits of the next pointer remain unused for now. +* the least significant bit set. */ -#define RHT_BASE_BITS 4 -#define RHT_HASH_BITS 27 -#define RHT_BASE_SHIFT RHT_HASH_BITS - -/* Base bits plus 1 bit for nulls marker */ -#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)/* Maximum chain length before rehash * @@ -92,24 +75,14 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; -static inline unsigned long rht_marker (const struct rhashtable *ht,u32 hash) -{ -return NULLS_MARKER (ht->p.nulls_base + hash); -} -#define INIT_RHT_NULLS_HEAD (ptr, ht, hash) \ - ((ptr) = (typeof(ptr)) rht_marker(ht, hash)) + ((ptr) = (typeof(ptr)) NULLS_MARKER(0))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {return ((unsigned long ) ptr &1 ); } -static inline unsigned long rht_get_nulls_value (const struct rhash_head *ptr) -{ -return ((unsigned long ) ptr) >>1 ; -} -static inline void *rht_obj (const struct rhashtable *ht,const struct rhash_head *he) {@@ -119,7 +92,7 @@ static inline void *rht_obj(const struct rhashtable *ht,static inline unsigned int rht_bucket_index (const struct bucket_table *tbl,unsigned int hash) { -return (hash >>RHT_HASH_RESERVED_SPACE ) & (tbl->size -1 ); +return hash & (tbl->size -1 ); } static inline unsigned int rht_key_get_hash(struct rhashtable *ht,lib/rhashtable.c +0
-8@@ -995,7 +995,6 @@ static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)* .key_offset = offsetof(struct test_obj, key), * .key_len = sizeof(int), * .hashfn = jhash, -* .nulls_base = (1U << RHT_BASE_SHIFT), * }; * * Configuration Example 2: Variable length keys @@ -1029,9 +1028,6 @@ int rhashtable_init(struct rhashtable *ht, (params->obj_hashfn && !params->obj_cmpfn))return -EINVAL ; -if (params->nulls_base && params->nulls_base < (1U <<RHT_BASE_SHIFT )) -return -EINVAL ; -memset (ht,0 ,sizeof (*ht));mutex_init (&ht->mutex);spin_lock_init (&ht->lock);@@ -1096,10 +1092,6 @@ int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params) {int err; -/* No rhlist NULLs marking for now. */ -if (params->nulls_base) -return -EINVAL ; - err =rhashtable_init (&hlt->ht, params); hlt->ht.rhlist = true;return err;lib/test_rhashtable.c +1
-4@@ -83,7 +83,7 @@ static u32 my_hashfn(const void *data, u32 len, u32 seed) {const struct test_obj_rhl *obj = data; -return (obj->value.id %10 ) <<RHT_HASH_RESERVED_SPACE ; +return (obj->value.id %10 ); }static int my_cmpfn (struct rhashtable_compare_arg *arg,const void *obj)@@ -99,7 +99,6 @@ static struct rhashtable_params test_rht_params = { .key_offset = offsetof(struct test_obj, value), .key_len = sizeof(struct test_obj_val), .hashfn = jhash, - .nulls_base = (3U << RHT_BASE_SHIFT), };static struct rhashtable_params test_rht_params_dup = {@@ -294,8 +293,6 @@ static int __init test_rhltable(unsigned int entries)if (!obj_in_table) goto out_free; -/* nulls_base not supported in rhlist interface */ - test_rht_params.nulls_base =0 ; err =rhltable_init (&rhlt, &test_rht_params);if (WARN_ON (err)) goto out_free;
Re: [PATCH 03/18] rhashtable: remove nulls_base and related code.
Quoted 16 lines · NeilBrown
Quoted 5 lines · NeilBrown
This "feature" is unused, undocumented, and untested and so doesn't really belong. Next patch will introduce support to detect when a search gets diverted down a different chain, which the common purpose of nulls markers.
Quoted 8 lines
This patch actually fixes a bug too. The table resizing allows a table to grow to 2^31 buckets, but the hash is truncated to 27 bits - any growth beyond 2^27 is wasteful an ineffective.
This patch results in
NULLS_MARKER(0)being used for all chains, and leaves the use ofrht_is_a_null()to test for it.Signed-off-by NeilBrown <[email protected]>
Quoted 8 lines
Hi Herbert,
You've acked a few patches that depends on this one, but not this patch itself. If you could ack this one, I could submit a collection of patches for inclusion (after the merge window closes I guess) and then have fewer outstanding.
This assumes you are in-principle happy with the alternative approach I took to handling list-nulls. I got the impression that it was only some small details holding that back.
You can add my ack to this patch:
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 4/18 rhashtable: detect when object movement might have invalidated a lookup
In reply to NeilBrownSome users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects using SLAB_TYPESAFE_BY_RCU which can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.
To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
The unique NULLS_MARKER is derived from the address of the head of the chain.
If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain. insert_slow() already does that, insert_fast() does not. So this patch changes insert_fast to always insert at the head of the chain.
Note that such a user must do their own double-checking of the object found by rhashtable_lookup_fast() after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.
--- include/linux/rhashtable.h | 35 +++++++++++++++++++++++------------ lib/rhashtable.c | 8 +++++--- 2 files changed, 28 insertions(+), 15 deletions(-)
include/linux/rhashtable.h +23 -12
@@ -75,8 +75,10 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +#define RHT_NULLS_MARKER (ptr) \ + ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))#define INIT_RHT_NULLS_HEAD (ptr, ht, hash) \ - ((ptr) = (typeof(ptr)) NULLS_MARKER(0)) + ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {
@@ -471,6 +473,7 @@ static inline struct rhash_head *__rhashtable_lookup( .ht = ht, .key = key, }; +struct rhash_head __rcu *const *head;struct bucket_table *tbl;struct rhash_head *he;unsigned int hash;
@@ -478,13 +481,19 @@ static inline struct rhash_head *__rhashtable_lookup( tbl =rht_dereference_rcu (ht->tbl, ht); restart: hash =rht_key_hashfn (ht, tbl, key, params); -rht_for_each_rcu (he, tbl, hash) { -if (params.obj_cmpfn ? - params.obj_cmpfn (&arg,rht_obj (ht, he)) : -rhashtable_compare (&arg,rht_obj (ht, he))) -continue ; -return he; - } + head =rht_bucket (tbl, hash); +do { +rht_for_each_rcu_continue (he, *head, tbl, hash) { +if (params.obj_cmpfn ? + params.obj_cmpfn (&arg,rht_obj (ht, he)) : +rhashtable_compare (&arg,rht_obj (ht, he))) +continue ; +return he; + } +/* An object might have been moved to a different hash chain, +* while we walk along it - better check and retry. +*/ + }while (he !=RHT_NULLS_MARKER (head));/* Ensure we see any new tables. */ smp_rmb ();
@@ -580,6 +589,7 @@ static inline void *__rhashtable_insert_fast( .ht = ht, .key = key, }; +struct rhash_head __rcu **headp;struct rhash_head __rcu **pprev;struct bucket_table *tbl;struct rhash_head *head;
@@ -603,12 +613,13 @@ static inline void *__rhashtable_insert_fast( } elasticity =RHT_ELASTICITY ; - pprev =rht_bucket_insert (ht, tbl, hash); + headp =rht_bucket_insert (ht, tbl, hash); + pprev = headp; data =ERR_PTR (-ENOMEM );if (!pprev) goto out; -rht_for_each_continue (head, *pprev, tbl, hash) { +rht_for_each_continue (head, *headp, tbl, hash) {struct rhlist_head *plist;struct rhlist_head *list;
@@ -648,7 +659,7 @@ static inline void *__rhashtable_insert_fast(if (unlikely (rht_grow_above_100 (ht, tbl))) goto slow_path; - head =rht_dereference_bucket (*pprev, tbl, hash); + head =rht_dereference_bucket (*headp, tbl, hash);RCU_INIT_POINTER (obj->next, head);if (rhlist) {
@@ -658,7 +669,7 @@ static inline void *__rhashtable_insert_fast(RCU_INIT_POINTER (list->next,NULL ); } -rcu_assign_pointer (*pprev, obj); +rcu_assign_pointer (*headp, obj);atomic_inc (&ht->nelems);if (rht_grow_above_75 (ht, tbl))
lib/rhashtable.c +5 -3
@@ -1174,8 +1174,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull = - (struct rhash_head __rcu *)NULLS_MARKER (0 ); +static struct rhash_head __rcu *rhnull;unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
@@ -1193,8 +1192,11 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, subhash >>= shift; } -if (!ntbl) +if (!ntbl) { +if (!rhnull) +INIT_RHT_NULLS_HEAD (rhnull,NULL ,0 );return &rhnull; + }return &ntbl[subhash].bucket;
Herbert Xu
maintainerRe: [PATCH 04/18] rhashtable: detect when object movement might have invalidated a lookup
Quoted 7 lines · NeilBrown
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.
Quoted 7 lines
To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.
Yes I thinks makes a lot more sense than the existing rhashtable nulls code. The current rhashtable nulls code harkens back to the time of the old rhashtable implementation where the same chain existed in two different tables and that is no longer the case.
Quoted 2 lines
If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure
This is not currently required by TCP/UDP. I'd rather not add extra constraints that aren't actually used. The only requirement for nulls is to allow sockets to float from the listening table to the established table within one RCU grace period. There is no shuttling back and forth, i.e., the only exit path for a socket in the established table is to be freed at the end of the RCU grace period.
Adding Eric/Dave to the cc list in case they do/will need such a functionality.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH 04/18] rhashtable: detect when object movement might have invalidated a lookup
Quoted 21 lines · Herbert Xu
Quoted 7 lines · NeilBrown
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.Quoted 7 lines
To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.Yes I thinks makes a lot more sense than the existing rhashtable nulls code. The current rhashtable nulls code harkens back to the time of the old rhashtable implementation where the same chain existed in two different tables and that is no longer the case.
Quoted 5 lines
Quoted 2 lines
If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure
This is not currently required by TCP/UDP. I'd rather not add extra constraints that aren't actually used.
I assume you are referring here to the change to insert at the head of the chain, rather than at the end.
This is important for me to be able to use SLAB_TYPESAFE_BY_RCU for the kmem_cache. Without that, I need a new rcu-free callback for every different kmem_cache.
When using SLAB_TYPESAFE_BY_RCU, there is no way to know where a freed and re-allocated object might be used again - it could be used in the same table with the same hash value.
Thanks,
NeilBrown
PATCH RESEND 1/1 rhashtable: detect when object movement might have invalidated a lookup
In reply to Herbert XuSome users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects using SLAB_TYPESAFE_BY_RCU which can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.
To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
The unique NULLS_MARKER is derived from the address of the head of the chain.
If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain. insert_slow() already does that, insert_fast() does not. So this patch changes insert_fast to always insert at the head of the chain.
Note that such a user must do their own double-checking of the object found by rhashtable_lookup_fast() after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.
---
I'm resending this unchanged. Herbert wasn't sure if we needed all the functionality provided. I explained that it was useful when SLAB_TYPESAFE_BY_RCU slabs are used. No further discussion happened.
Thanks,
NeilBrown
include/linux/rhashtable.h | 35 +++++++++++++++++++++++------------ lib/rhashtable.c | 8 +++++--- 2 files changed, 28 insertions(+), 15 deletions(-)
include/linux/rhashtable.h +23 -12
@@ -75,8 +75,10 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +#define RHT_NULLS_MARKER (ptr) \ + ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))#define INIT_RHT_NULLS_HEAD (ptr) \ - ((ptr) = (typeof(ptr)) NULLS_MARKER(0)) + ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {
@@ -471,6 +473,7 @@ static inline struct rhash_head *__rhashtable_lookup( .ht = ht, .key = key, }; +struct rhash_head __rcu *const *head;struct bucket_table *tbl;struct rhash_head *he;unsigned int hash;
@@ -478,13 +481,19 @@ static inline struct rhash_head *__rhashtable_lookup( tbl =rht_dereference_rcu (ht->tbl, ht); restart: hash =rht_key_hashfn (ht, tbl, key, params); -rht_for_each_rcu (he, tbl, hash) { -if (params.obj_cmpfn ? - params.obj_cmpfn (&arg,rht_obj (ht, he)) : -rhashtable_compare (&arg,rht_obj (ht, he))) -continue ; -return he; - } + head =rht_bucket (tbl, hash); +do { +rht_for_each_rcu_continue (he, *head, tbl, hash) { +if (params.obj_cmpfn ? + params.obj_cmpfn (&arg,rht_obj (ht, he)) : +rhashtable_compare (&arg,rht_obj (ht, he))) +continue ; +return he; + } +/* An object might have been moved to a different hash chain, +* while we walk along it - better check and retry. +*/ + }while (he !=RHT_NULLS_MARKER (head));/* Ensure we see any new tables. */ smp_rmb ();
@@ -580,6 +589,7 @@ static inline void *__rhashtable_insert_fast( .ht = ht, .key = key, }; +struct rhash_head __rcu **headp;struct rhash_head __rcu **pprev;struct bucket_table *tbl;struct rhash_head *head;
@@ -603,12 +613,13 @@ static inline void *__rhashtable_insert_fast( } elasticity =RHT_ELASTICITY ; - pprev =rht_bucket_insert (ht, tbl, hash); + headp =rht_bucket_insert (ht, tbl, hash); + pprev = headp; data =ERR_PTR (-ENOMEM );if (!pprev) goto out; -rht_for_each_continue (head, *pprev, tbl, hash) { +rht_for_each_continue (head, *headp, tbl, hash) {struct rhlist_head *plist;struct rhlist_head *list;
@@ -648,7 +659,7 @@ static inline void *__rhashtable_insert_fast(if (unlikely (rht_grow_above_100 (ht, tbl))) goto slow_path; - head =rht_dereference_bucket (*pprev, tbl, hash); + head =rht_dereference_bucket (*headp, tbl, hash);RCU_INIT_POINTER (obj->next, head);if (rhlist) {
@@ -658,7 +669,7 @@ static inline void *__rhashtable_insert_fast(RCU_INIT_POINTER (list->next,NULL ); } -rcu_assign_pointer (*pprev, obj); +rcu_assign_pointer (*headp, obj);atomic_inc (&ht->nelems);if (rht_grow_above_75 (ht, tbl))
lib/rhashtable.c +5 -3
@@ -1164,8 +1164,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull = - (struct rhash_head __rcu *)NULLS_MARKER (0 ); +static struct rhash_head __rcu *rhnull;unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
@@ -1183,8 +1182,11 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, subhash >>= shift; } -if (!ntbl) +if (!ntbl) { +if (!rhnull) +INIT_RHT_NULLS_HEAD (rhnull,NULL ,0 );return &rhnull; + }return &ntbl[subhash].bucket;
Signature · 1 line
2.14.0.rc0.dirty
Re: [PATCH resend] rhashtable: detect when object movement might have invalidated a lookup
From: NeilBrown <[email protected]>
Date: Fri, 06 Jul 2018 17:08:35 +1000
Quoted 24 lines
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>
Applied to net-next.
David Miller
maintainerRe: [PATCH resend] rhashtable: detect when object movement might have invalidated a lookup
From: David Miller <[email protected]>
Date: Wed, 11 Jul 2018 22:46:58 -0700 (PDT)
Quoted 27 lines
From: NeilBrown <[email protected]>
Date: Fri, 06 Jul 2018 17:08:35 +1000Quoted 24 lines
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.
Quoted 8 lines
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>Applied to net-next.
Actually, reverted, it doesn't even compile.
lib/rhashtable.c: In function ‘rht_bucket_nested’:lib/rhashtable.c:1187:39: error: macro "INIT_RHT_NULLS_HEAD" passed 3 arguments, but takes just 1
INIT_RHT_NULLS_HEAD(rhnull, NULL, 0);
^
lib/rhashtable.c:1187:4: error: ‘INIT_RHT_NULLS_HEAD’ undeclared (first use in this function); did you mean ‘INIT_LIST_HEAD’?
INIT_RHT_NULLS_HEAD(rhnull, NULL, 0);
^~~~~~~~~~~~~~~~~~~
INIT_LIST_HEADlib/rhashtable.c:1187:4: note: each undeclared identifier is reported only once for each function it appears in
Re: [PATCH resend] rhashtable: detect when object movement might have invalidated a lookup
Quoted 43 lines · David Miller
From: David Miller <[email protected]>
Date: Wed, 11 Jul 2018 22:46:58 -0700 (PDT)Quoted 27 lines
From: NeilBrown <[email protected]>
Date: Fri, 06 Jul 2018 17:08:35 +1000Quoted 24 lines
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.Quoted 8 lines
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>Applied to net-next.
Actually, reverted, it doesn't even compile.
lib/rhashtable.c: In function ‘rht_bucket_nested’:lib/rhashtable.c:1187:39: error: macro "INIT_RHT_NULLS_HEAD" passed 3 arguments, but takes just 1
Quoted 7 lines
INIT_RHT_NULLS_HEAD(rhnull, NULL, 0); ^ lib/rhashtable.c:1187:4: error: ‘INIT_RHT_NULLS_HEAD’ undeclared (first use in this function); did you mean ‘INIT_LIST_HEAD’? INIT_RHT_NULLS_HEAD(rhnull, NULL, 0); ^~~~~~~~~~~~~~~~~~~ INIT_LIST_HEAD
lib/rhashtable.c:1187:4: note: each undeclared identifier is reported only once for each function it appears in
How embarrassing :-(
I had tested a series, but not each individual patch. Sorry about that. I'll post the correct patch.
Thanks,
NeilBrown
PATCH 1/1 rhashtable: detect when object movement might have invalidated a lookup
In reply to David MillerSome users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects using SLAB_TYPESAFE_BY_RCU which can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.
To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
The unique NULLS_MARKER is derived from the address of the head of the chain.
If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain. insert_slow() already does that, insert_fast() does not. So this patch changes insert_fast to always insert at the head of the chain.
Note that such a user must do their own double-checking of the object found by rhashtable_lookup_fast() after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.
--- include/linux/rhashtable.h | 35 +++++++++++++++++++++++------------ lib/rhashtable.c | 8 +++++--- 2 files changed, 28 insertions(+), 15 deletions(-)
include/linux/rhashtable.h +23 -12
@@ -75,8 +75,10 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +#define RHT_NULLS_MARKER (ptr) \ + ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))#define INIT_RHT_NULLS_HEAD (ptr) \ - ((ptr) = (typeof(ptr)) NULLS_MARKER(0)) + ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {
@@ -471,6 +473,7 @@ static inline struct rhash_head *__rhashtable_lookup( .ht = ht, .key = key, }; +struct rhash_head __rcu *const *head;struct bucket_table *tbl;struct rhash_head *he;unsigned int hash;
@@ -478,13 +481,19 @@ static inline struct rhash_head *__rhashtable_lookup( tbl =rht_dereference_rcu (ht->tbl, ht); restart: hash =rht_key_hashfn (ht, tbl, key, params); -rht_for_each_rcu (he, tbl, hash) { -if (params.obj_cmpfn ? - params.obj_cmpfn (&arg,rht_obj (ht, he)) : -rhashtable_compare (&arg,rht_obj (ht, he))) -continue ; -return he; - } + head =rht_bucket (tbl, hash); +do { +rht_for_each_rcu_continue (he, *head, tbl, hash) { +if (params.obj_cmpfn ? + params.obj_cmpfn (&arg,rht_obj (ht, he)) : +rhashtable_compare (&arg,rht_obj (ht, he))) +continue ; +return he; + } +/* An object might have been moved to a different hash chain, +* while we walk along it - better check and retry. +*/ + }while (he !=RHT_NULLS_MARKER (head));/* Ensure we see any new tables. */ smp_rmb ();
@@ -580,6 +589,7 @@ static inline void *__rhashtable_insert_fast( .ht = ht, .key = key, }; +struct rhash_head __rcu **headp;struct rhash_head __rcu **pprev;struct bucket_table *tbl;struct rhash_head *head;
@@ -603,12 +613,13 @@ static inline void *__rhashtable_insert_fast( } elasticity =RHT_ELASTICITY ; - pprev =rht_bucket_insert (ht, tbl, hash); + headp =rht_bucket_insert (ht, tbl, hash); + pprev = headp; data =ERR_PTR (-ENOMEM );if (!pprev) goto out; -rht_for_each_continue (head, *pprev, tbl, hash) { +rht_for_each_continue (head, *headp, tbl, hash) {struct rhlist_head *plist;struct rhlist_head *list;
@@ -648,7 +659,7 @@ static inline void *__rhashtable_insert_fast(if (unlikely (rht_grow_above_100 (ht, tbl))) goto slow_path; - head =rht_dereference_bucket (*pprev, tbl, hash); + head =rht_dereference_bucket (*headp, tbl, hash);RCU_INIT_POINTER (obj->next, head);if (rhlist) {
@@ -658,7 +669,7 @@ static inline void *__rhashtable_insert_fast(RCU_INIT_POINTER (list->next,NULL ); } -rcu_assign_pointer (*pprev, obj); +rcu_assign_pointer (*headp, obj);atomic_inc (&ht->nelems);if (rht_grow_above_75 (ht, tbl))
lib/rhashtable.c +5 -3
@@ -1164,8 +1164,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull = - (struct rhash_head __rcu *)NULLS_MARKER (0 ); +static struct rhash_head __rcu *rhnull;unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
@@ -1183,8 +1182,11 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, subhash >>= shift; } -if (!ntbl) +if (!ntbl) { +if (!rhnull) +INIT_RHT_NULLS_HEAD (rhnull);return &rhnull; + }return &ntbl[subhash].bucket;
Signature · 1 line
2.14.0.rc0.dirty
Herbert Xu
maintainerRe: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 24 lines · NeilBrown
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>
I still don't understand why we need this feature. The only existing user of this (which currently doesn't use rhashtable) does not readd the reused entry to the same table. IOW the flow is always from table A to table B. After which the entry will be properly freed rather than reused.
So who is going to use this?
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 32 lines · Herbert Xu
Quoted 24 lines · NeilBrown
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>
Quoted 7 lines
I still don't understand why we need this feature. The only existing user of this (which currently doesn't use rhashtable) does not readd the reused entry to the same table. IOW the flow is always from table A to table B. After which the entry will be properly freed rather than reused.
So who is going to use this?
I want it so I can use SLAB_TYPESAFE_BY_RCU slab caches in lustre. lustre isn't in mainline any more so I cannot point at the code but the concept is simple.
Without this, you need to use rcu_free to free any object added to an rhashtable.
When kmalloc is used, kfree_rcu() can be used, which is fairly painless. When a kmem_cache is used, you need to provide your own rcu free function, which is clumsy.
With SLAB_TYPESAFE_BY_RCU, that isn't needed. The object can be freed immediately, providing you can cope with it being re-used (as the same type) immediately. Part of handling that is coping with the possibility that it might be inserted into the same hash table, and possibly the same chain, immediately it is freed.
lustre has 6 different resizable hashtables which I want to convert to use rhashtable.
I currently need call_rcu handlers for 3 for these. I want those 3 to use SLAB_TYPESAFE_BY_RCU instead so they can use kmem_cache_free() directly. For this, I need rhashtable to be safe if an object is deleted and immediately re-inserted into the same hash chain.
Thanks,
NeilBrown
Herbert Xu
maintainerRe: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 4 lines · NeilBrown
kmem_cache_free()directly. For this, I need rhashtable to be safe if an object is deleted and immediately re-inserted into the same hash chain.
This means that
rcu_read_lock(); A = rhashtable_lookup(); use(A); rcu_read_unlock();
A can turn into object B when it is used. That is just too strange for words. Can we see some actual code on how this works?
For comparison, the existing net code where this happens A doesn't actually change and it simply moves from one hashtable to another.
I'm relucant to add semantics that would restrain on how rhashtable works unless we have real and valid use-cases for it.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 8 lines · Herbert Xu
Quoted 4 lines · NeilBrown
kmem_cache_free()directly. For this, I need rhashtable to be safe if an object is deleted and immediately re-inserted into the same hash chain.This means that
Quoted 7 lines
rcu_read_lock(); A = rhashtable_lookup(); use(A); rcu_read_unlock();A can turn into object B when it is used. That is just too strange for words. Can we see some actual code on how this works?
Look in Documenation/RCU/rculist_nulls.txt.
The very first example is a typical lookup for a nulls list. The above sample code would read:
rcu_read_lock();
begin:
A = rhashtable_lookup(table, key);
if (IS_ERR(A)) {
rcu_read_unlock();
goto not_found;
}
if (!try_get_ref(A))
goto again;
if (A->key != key) {
put_ref(A);
goto again;
}
rcu_read_unlock();
use(A);If you particularly need to see real code rather than sample code, I can have something for you in a day or so.
Thanks,
NeilBrown
Quoted 12 lines
For comparison, the existing net code where this happens A doesn't actually change and it simply moves from one hashtable to another.
I'm relucant to add semantics that would restrain on how rhashtable works unless we have real and valid use-cases for it.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerRe: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 4 lines · NeilBrown
Look in
Documenation/RCU/rculist_nulls.txt.
The very first example is a typical lookup for a nulls list. The above sample code would read:
OK, but how will this work with rhlist? It would be very bad to have a feature that works for rhashtable but fails in strange ways when you use rhlist.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 9 lines · Herbert Xu
Quoted 4 lines · NeilBrown
Look in
Documenation/RCU/rculist_nulls.txt.
The very first example is a typical lookup for a nulls list. The above sample code would read:OK, but how will this work with rhlist? It would be very bad to have a feature that works for rhashtable but fails in strange ways when you use rhlist.
It should be easy enough to handle in rhlist too.
When inserting a new object, we put it at the start of the chain, and if there was already a list with the same key, it gets moved to the new object.
A walk could see some objects repeatedly when this happens, but that is already possible.
Thanks,
NeilBrown
Quoted 6 lines
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
David Miller
maintainerRe: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
In reply to NeilBrownFrom: NeilBrown <[email protected]>
Date: Mon, 16 Jul 2018 09:57:11 +1000
Quoted 23 lines
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>
Neil I have to be honest with you.
During this whole ordeal I was under the impression that this was all going to be used for something in-tree. But now I see that you want to use all of this stuff for lustre which is out of tree.
It would be extremely hard for me to accept adding this kind of complexity and weird semantics to an already extremely complicated and delicate piece of infrastructure if something in-tree would use it.
But for something out-of-tree? I'm sorry, no way.
Re: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 26 lines · David Miller
From: NeilBrown <[email protected]>
Date: Mon, 16 Jul 2018 09:57:11 +1000Quoted 23 lines
Some users of rhashtable might need to change the key
of an object and move it to a different location in the table. Other users might want to allocate objects usingSLAB_TYPESAFE_BY_RCUwhich can result in the same memory allocation being used for a different (type-compatible) purpose and similarly end up in a different hash-chain.To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.The unique
NULLS_MARKERis derived from the address of the head of the chain.If an object is removed and re-added to the same hash chain, we won't notice by looking that the NULLS marker. In this case we must be sure that it was not re-added *after* its original location, or a lookup may incorrectly fail. The easiest solution is to ensure it is inserted at the start of the chain.
insert_slow()already does that,insert_fast()does not. So this patch changesinsert_fastto always insert at the head of the chain.
Quoted 8 lines
Quoted 6 lines
Note that such a user must do their own double-checking of the object found by
rhashtable_lookup_fast()after ensuring mutual exclusion which anything that might change the key, such as successfully taking a new reference.Signed-off-by NeilBrown <[email protected]>Neil I have to be honest with you.
Thank you.
Quoted 11 lines
During this whole ordeal I was under the impression that this was all going to be used for something in-tree. But now I see that you want to use all of this stuff for lustre which is out of tree.
It would be extremely hard for me to accept adding this kind of complexity and weird semantics to an already extremely complicated and delicate piece of infrastructure if something in-tree would use it.
But for something out-of-tree? I'm sorry, no way.
That's unfortunate, but I can live with it. null-list support is just a nice-to-have for me.
I'll resend the patch with the unwanted complexity removed.
Does this ruling also apply to the bit-spin-lock changes and the per-cpu-counter changes that I have proposed?
These improve scalability when updates dominate. Not having these in mainline would mean I need to carry a separate rhashtables implementation for lustre, which means code diversion which isn't healthy in the long run.
(Note that, in my mind, lustre is only temporarily out-of-tree. It is coming back, hopefully this year).
Thanks,
NeilBrown
David Miller
maintainerRe: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
From: NeilBrown <[email protected]>
Date: Fri, 20 Jul 2018 16:30:34 +1000
Quoted 6 lines
Does this ruling also apply to the bit-spin-lock changes and the per-cpu-counter changes that I have proposed? These improve scalability when updates dominate. Not having these in mainline would mean I need to carry a separate rhashtables implementation for lustre, which means code diversion which isn't healthy in the long run.
If it helps existing rhashtable users generally, then it is fine, since it will actually be tested by upstream users.
Thanks.
Re: [PATCH - revised] rhashtable: detect when object movement might have invalidated a lookup
Quoted 10 lines · David Miller
From: NeilBrown <[email protected]>
Date: Fri, 20 Jul 2018 16:30:34 +1000Quoted 6 lines
Does this ruling also apply to the bit-spin-lock changes and the per-cpu-counter changes that I have proposed? These improve scalability when updates dominate. Not having these in mainline would mean I need to carry a separate rhashtables implementation for lustre, which means code diversion which isn't healthy in the long run.
Quoted 2 lines
If it helps existing rhashtable users generally, then it is fine, since it will actually be tested by upstream users.
Good, thanks.
NeilBrown
PATCH 1/1 rhashtable: detect when object movement between tables might have invalidated a lookup
In reply to David MillerSome users of rhashtables might need to move an object from one table to another - this appears to be the reason for the incomplete usage of NULLS markers.
To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
The unique NULLS_MARKER is derived from the address of the head of the chain. As this cannot be derived at load-time the static rhnull in rht_bucket_nexted() need to be initialised at run time.
Any caller of a lookup function must be prepared for the possibility that the object returned is in a different table - it might have been there for some time.
Note that this does NOT provide support for other uses for NULLS_MARKERs such as allocating with SLAB_TYPESAFE_BY_RCU or changing the key of an object and re-inserting it in the table. These could only be done safely if new objects were inserted at the *start* of a hash chain, and that is not currently the case.
---
This is a simplified version of a previous patch.
It provides NULLS_MARKER support only for the specific use case which is currently thought be valuable to in-tree users of rhashtables.
Thanks,
NeilBrown
include/linux/rhashtable.h | 25 +++++++++++++++++-------- lib/rhashtable.c | 3 +-- 2 files changed, 18 insertions(+), 10 deletions(-)
include/linux/rhashtable.h +17 -8
@@ -75,8 +75,10 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +#define RHT_NULLS_MARKER (ptr) \ + ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))#define INIT_RHT_NULLS_HEAD (ptr) \ - ((ptr) = (typeof(ptr)) NULLS_MARKER(0)) + ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {
@@ -471,6 +473,7 @@ static inline struct rhash_head *__rhashtable_lookup( .ht = ht, .key = key, }; +struct rhash_head __rcu *const *head;struct bucket_table *tbl;struct rhash_head *he;unsigned int hash;
@@ -478,13 +481,19 @@ static inline struct rhash_head *__rhashtable_lookup( tbl =rht_dereference_rcu (ht->tbl, ht); restart: hash =rht_key_hashfn (ht, tbl, key, params); -rht_for_each_rcu (he, tbl, hash) { -if (params.obj_cmpfn ? - params.obj_cmpfn (&arg,rht_obj (ht, he)) : -rhashtable_compare (&arg,rht_obj (ht, he))) -continue ; -return he; - } + head =rht_bucket (tbl, hash); +do { +rht_for_each_rcu_continue (he, *head, tbl, hash) { +if (params.obj_cmpfn ? + params.obj_cmpfn (&arg,rht_obj (ht, he)) : +rhashtable_compare (&arg,rht_obj (ht, he))) +continue ; +return he; + } +/* An object might have been moved to a different hash chain, +* while we walk along it - better check and retry. +*/ + }while (he !=RHT_NULLS_MARKER (head));/* Ensure we see any new tables. */ smp_rmb ();
lib/rhashtable.c +1 -2
@@ -1175,8 +1175,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull = - (struct rhash_head __rcu *)NULLS_MARKER (0 ); +static struct rhash_head __rcu *rhnull =RHT_NULLS_MARKER (&rhnull);unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
Signature · 1 line
2.14.0.rc0.dirty
David Miller
maintainerRe: [PATCH net-next] rhashtable: detect when object movement between tables might have invalidated a lookup
From: NeilBrown <[email protected]>
Date: Mon, 23 Jul 2018 11:56:14 +1000
Quoted 8 lines
Some users of rhashtables might need to move an object from one table to another - this appears to be the reason for the incomplete usage of NULLS markers.
To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
...
Quoted 4 lines
This is a simplified version of a previous patch.
It providesNULLS_MARKERsupport only for the specific use case which is currently thought be valuable to in-tree users of rhashtables.
Neil, this doesn't even compile:
In file included from lib/rhashtable.c:28:lib/rhashtable.c: In function ‘rht_bucket_nested’:./include/linux/rhashtable.h:79:2: error: initializer element is not computable at load time
((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
^lib/rhashtable.c:1178:43: note: in expansion of macro ‘RHT_NULLS_MARKER’
static struct rhash_head __rcu *rhnull = RHT_NULLS_MARKER(&rhnull);
^~~~~~~~~~~~~~~~make[1]: *** [scripts/Makefile.build:318: lib/rhashtable.o] Error 1
make: *** [Makefile:1653: lib/rhashtable.o] Error 2
I imagine you have a mix of other changes or whatever in your tree, so I'll give you the benefit of the doubt.
But this is the second time this has happened with your rhashtable changes, so...
Re: [PATCH net-next] rhashtable: detect when object movement between tables might have invalidated a lookup
Quoted 27 lines · David Miller
From: NeilBrown <[email protected]>
Date: Mon, 23 Jul 2018 11:56:14 +1000Quoted 8 lines
Some users of rhashtables might need to move an object from one table to another - this appears to be the reason for the incomplete usage of NULLS markers.
To support these, we store a unique
NULLS_MARKERat the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated....
Quoted 4 lines
This is a simplified version of a previous patch.
It providesNULLS_MARKERsupport only for the specific use case which is currently thought be valuable to in-tree users of rhashtables.Neil, this doesn't even compile:
In file included from
lib/rhashtable.c:28:lib/rhashtable.c: In function ‘rht_bucket_nested’:./include/linux/rhashtable.h:79:2: error: initializer element is not computable at load time
((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
^lib/rhashtable.c:1178:43: note: in expansion of macro ‘RHT_NULLS_MARKER’static struct rhash_head __rcu *rhnull = RHT_NULLS_MARKER(&rhnull); ^~~~~~~~~~~~~~~~
Quoted 8 lines
make[1]: *** [
scripts/Makefile.build:318:lib/rhashtable.o] Error 1
make: *** [Makefile:1653:lib/rhashtable.o] Error 2I imagine you have a mix of other changes or whatever in your tree, so I'll give you the benefit of the doubt.
But this is the second time this has happened with your rhashtable changes, so...
Your displeasure is understandable.
I had fixed this, but hadn't refreshed the patch - though I had updated the patch description to explain the change I had to make - second sentence of:
The unique NULLS_MARKER is derived from the address of the head of the chain. As this cannot be derived at load-time the static rhnull in rht_bucket_nexted() need to be initialised at run time.
This is what I meant to send - it does compile.
Thanks,
NeilBrown
From: NeilBrown <[email protected]>
Date: Mon, 25 Jun 2018 08:09:16 +1000
Subject: [PATCH] rhashtable: detect when object movement between tables might
have invalidated a lookup
Some users of rhashtables might need to move an object from one table to another - this appears to be the reason for the incomplete usage of NULLS markers.
To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search is repeated.
The unique NULLS_MARKER is derived from the address of the head of the chain. As this cannot be derived at load-time the static rhnull in rht_bucket_nexted() need to be initialised at run time.
Any caller of a lookup function must be prepared for the possibility that the object returned is in a different table - it might have been there for some time.
Note that this does NOT provide support for other uses for NULLS_MARKERs such as allocating with SLAB_TYPESAFE_BY_RCU or changing the key of an object and re-inserting it in the table. These could only be done safely if new objects were inserted at the *start* of a hash chain, and that is not currently the case.
--- include/linux/rhashtable.h | 25 +++++++++++++++++-------- lib/rhashtable.c | 8 +++++--- 2 files changed, 22 insertions(+), 11 deletions(-)
include/linux/rhashtable.h +17 -8
@@ -75,8 +75,10 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +#define RHT_NULLS_MARKER (ptr) \ + ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))#define INIT_RHT_NULLS_HEAD (ptr) \ - ((ptr) = (typeof(ptr)) NULLS_MARKER(0)) + ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr) {
@@ -471,6 +473,7 @@ static inline struct rhash_head *__rhashtable_lookup( .ht = ht, .key = key, }; +struct rhash_head __rcu *const *head;struct bucket_table *tbl;struct rhash_head *he;unsigned int hash;
@@ -478,13 +481,19 @@ static inline struct rhash_head *__rhashtable_lookup( tbl =rht_dereference_rcu (ht->tbl, ht); restart: hash =rht_key_hashfn (ht, tbl, key, params); -rht_for_each_rcu (he, tbl, hash) { -if (params.obj_cmpfn ? - params.obj_cmpfn (&arg,rht_obj (ht, he)) : -rhashtable_compare (&arg,rht_obj (ht, he))) -continue ; -return he; - } + head =rht_bucket (tbl, hash); +do { +rht_for_each_rcu_continue (he, *head, tbl, hash) { +if (params.obj_cmpfn ? + params.obj_cmpfn (&arg,rht_obj (ht, he)) : +rhashtable_compare (&arg,rht_obj (ht, he))) +continue ; +return he; + } +/* An object might have been moved to a different hash chain, +* while we walk along it - better check and retry. +*/ + }while (he !=RHT_NULLS_MARKER (head));/* Ensure we see any new tables. */ smp_rmb ();
lib/rhashtable.c +5 -3
@@ -1175,8 +1175,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull = - (struct rhash_head __rcu *)NULLS_MARKER (0 ); +static struct rhash_head __rcu *rhnull;unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
@@ -1194,8 +1193,11 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, subhash >>= shift; } -if (!ntbl) +if (!ntbl) { +if (!rhnull) +INIT_RHT_NULLS_HEAD (rhnull);return &rhnull; + }return &ntbl[subhash].bucket;
Signature · 1 line
2.14.0.rc0.dirty
PATCH 5/18 rhashtable: simplify INIT_RHT_NULLS_HEAD()
In reply to NeilBrownThe 'ht' and 'hash' arguments to INIT_RHT_NULLS_HEAD() are no longer used - so drop them. This allows us to also from the nhash argument from nested_table_alloc().
--- include/linux/rhashtable.h | 2 +- lib/rhashtable.c | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-)
include/linux/rhashtable.h +1 -1
@@ -77,7 +77,7 @@ struct bucket_table {#define RHT_NULLS_MARKER (ptr) \ ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1)) -#define INIT_RHT_NULLS_HEAD (ptr, ht, hash) \ +#define INIT_RHT_NULLS_HEAD (ptr) \ ((ptr) = RHT_NULLS_MARKER(&(ptr)))static inline bool rht_is_a_nulls (const struct rhash_head *ptr)
lib/rhashtable.c +7 -10
@@ -116,8 +116,7 @@ static void bucket_table_free_rcu(struct rcu_head *head)static union nested_table *nested_table_alloc (struct rhashtable *ht,union nested_table __rcu **prev, -unsigned int shifted, -unsigned int nhash) +unsigned int shifted) {union nested_table *ntbl;int i;
@@ -130,8 +129,7 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht,if (ntbl && shifted) {for (i =0 ; i <PAGE_SIZE /sizeof (ntbl[0 ].bucket); i++) -INIT_RHT_NULLS_HEAD (ntbl[i].bucket, ht, - (i << shifted) | nhash); +INIT_RHT_NULLS_HEAD (ntbl[i].bucket); }rcu_assign_pointer (*prev, ntbl);
@@ -157,7 +155,7 @@ static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,return NULL ;if (!nested_table_alloc (ht, (union nested_table __rcu **)tbl->buckets, -0 ,0 )) { +0 )) {kfree (tbl);return NULL ; }
@@ -207,7 +205,7 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, tbl->hash_rnd =get_random_u32 ();for (i =0 ; i < nbuckets; i++) -INIT_RHT_NULLS_HEAD (tbl->buckets[i], ht, i); +INIT_RHT_NULLS_HEAD (tbl->buckets[i]);return tbl; }
@@ -1194,7 +1192,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,if (!ntbl) {if (!rhnull) -INIT_RHT_NULLS_HEAD (rhnull,NULL ,0 ); +INIT_RHT_NULLS_HEAD (rhnull);return &rhnull; }
@@ -1219,7 +1217,7 @@ struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, nhash = index; shifted = tbl->nest; ntbl =nested_table_alloc (ht, &ntbl[index].table, - size <= (1 << shift) ? shifted :0 , nhash); + size <= (1 << shift) ? shifted :0 );while (ntbl && size > (1 << shift)) { index = hash & ((1 << shift) -1 );
@@ -1228,8 +1226,7 @@ struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, nhash |= index << shifted; shifted += shift; ntbl =nested_table_alloc (ht, &ntbl[index].table, - size <= (1 << shift) ? shifted :0 , - nhash); + size <= (1 << shift) ? shifted :0 ); }if (!ntbl)
Re: [PATCH 05/18] rhashtable: simplify INIT_RHT_NULLS_HEAD()
Quoted 3 lines · NeilBrown
The 'ht' and 'hash' arguments to
INIT_RHT_NULLS_HEAD()are no longer used - so drop them. This allows us to also from the nhash argument fromnested_table_alloc().
The second sentence is missing a word.
Quoted 1 line
Signed-off-by NeilBrown <[email protected]>
Otherwise,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 6/18 rhashtable: simplify nested_table_alloc() and rht_bucket_nested_insert()
In reply to NeilBrownNow that we don't use the hash value or shift in nested_table_alloc() there is room for simplification.
We only need to pass a "is this a leaf" flag to nested_table_alloc(), and don't need to track as much information in rht_bucket_nested_insert().
Note there is another minor cleanup in nested_table_alloc() here. The number of elements in a page of "union nested_tables" is most naturally
PAGE_SIZE / sizeof(ntbl[0])
The previous code had
PAGE_SIZE / sizeof(ntbl[0].bucket)
which happens to be the correct value only because the bucket uses all the space in the union.
--- lib/rhashtable.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
lib/rhashtable.c +6 -12
@@ -116,7 +116,7 @@ static void bucket_table_free_rcu(struct rcu_head *head)static union nested_table *nested_table_alloc (struct rhashtable *ht,union nested_table __rcu **prev, -unsigned int shifted) +bool leaf) {union nested_table *ntbl;int i;
@@ -127,8 +127,8 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht, ntbl =kzalloc (PAGE_SIZE ,GFP_ATOMIC ); -if (ntbl && shifted) { -for (i =0 ; i <PAGE_SIZE /sizeof (ntbl[0 ].bucket); i++) +if (ntbl && leaf) { +for (i =0 ; i <PAGE_SIZE /sizeof (ntbl[0 ]); i++)INIT_RHT_NULLS_HEAD (ntbl[i].bucket); }
@@ -155,7 +155,7 @@ static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,return NULL ;if (!nested_table_alloc (ht, (union nested_table __rcu **)tbl->buckets, -0 )) { + false)) {kfree (tbl);return NULL ; }
@@ -1209,24 +1209,18 @@ struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;union nested_table *ntbl; -unsigned int shifted; -unsigned int nhash; ntbl = (union nested_table *)rcu_dereference_raw (tbl->buckets[0 ]); hash >>= tbl->nest; - nhash = index; - shifted = tbl->nest; ntbl =nested_table_alloc (ht, &ntbl[index].table, - size <= (1 << shift) ? shifted :0 ); + size <= (1 << shift));while (ntbl && size > (1 << shift)) { index = hash & ((1 << shift) -1 ); size >>= shift; hash >>= shift; - nhash |= index << shifted; - shifted += shift; ntbl =nested_table_alloc (ht, &ntbl[index].table, - size <= (1 << shift) ? shifted :0 ); + size <= (1 << shift)); }if (!ntbl)
Re: [PATCH 06/18] rhashtable: simplify nested_table_alloc() and rht_bucket_nested_insert()
Quoted 11 lines · NeilBrown
Now that we don't use the hash value or shift in
nested_table_alloc()there is room for simplification.
We only need to pass a "is this a leaf" flag tonested_table_alloc(), and don't need to track as much information inrht_bucket_nested_insert().Note there is another minor cleanup in
nested_table_alloc()here. The number of elements in a page of "unionnested_tables" is most naturally
PAGE_SIZE/sizeof(ntbl[0])
Quoted 8 lines
The previous code had
PAGE_SIZE/sizeof(ntbl[0].bucket)which happens to be the correct value only because the bucket uses all the space in the union.
Signed-off-by NeilBrown <[email protected]>
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 7/18 rhashtable: use cmpxchg() to protect ->future_tbl.
In reply to NeilBrownRather than borrowing one of the bucket locks to protect ->future_tbl updates, use cmpxchg(). This gives more freedom to change how bucket locking is implemented.
--- lib/rhashtable.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
lib/rhashtable.c +4 -11
@@ -297,21 +297,14 @@ static int rhashtable_rehash_attach(struct rhashtable *ht, struct bucket_table *old_tbl, struct bucket_table *new_tbl) { -/* Protect future_tbl using the first bucket lock. */ - spin_lock_bh(old_tbl->locks); - -/* Did somebody beat us to it? */ -if (rcu_access_pointer (old_tbl->future_tbl)) { -spin_unlock_bh (old_tbl->locks); -return -EEXIST ; - } -/* Make insertions go into the new, empty table right away. Deletions * and lookups will be attempted in both tables until we synchronize. +* As cmpxchg() provides strong barriers, we do not need +* rcu_assign_pointer(). */ -rcu_assign_pointer (old_tbl->future_tbl, new_tbl); -spin_unlock_bh (old_tbl->locks); +if (cmpxchg (&old_tbl->future_tbl,NULL , new_tbl) !=NULL ) +return -EEXIST ;return 0 ; }
Re: [PATCH 07/18] rhashtable: use cmpxchg() to protect ->future_tbl.
Quoted 6 lines · NeilBrown
Rather than borrowing one of the bucket locks to protect ->future_tbl updates, use
cmpxchg(). This gives more freedom to change how bucket locking is implemented.Signed-off-by NeilBrown <[email protected]>
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 8/18 rhashtable: clean up dereference of ->future_tbl.
In reply to NeilBrownUsing rht_dereference_bucket() to dereference ->future_tbl looks like a type error, and could be confusing. Using rht_dereference_rcu() to test a pointer for NULL adds an unnecessary barrier - rcu_access_pointer() is preferred for NULL tests when no lock is held.
This uses 3 different ways to access ->future_tbl.
- if we know the mutex is held, use rht_dereference()
- if we don't hold the mutex, and are only testing for NULL, use rcu_access_pointer()
- otherwise (using RCU protection for true dereference), use rht_dereference_rcu().
Note that this includes a simplification of the call to rhashtable_last_table() - we don't do an extra dereference before the call any more.
--- include/linux/rhashtable.h | 2 +- lib/rhashtable.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-)
include/linux/rhashtable.h +1 -1
@@ -605,7 +605,7 @@ static inline void *__rhashtable_insert_fast( lock =rht_bucket_lock (tbl, hash);spin_lock_bh (lock); -if (unlikely (rht_dereference_bucket (tbl->future_tbl, tbl, hash))) { +if (unlikely (rcu_access_pointer (tbl->future_tbl))) { slow_path:spin_unlock_bh (lock);rcu_read_unlock ();
lib/rhashtable.c +4 -5
@@ -226,8 +226,7 @@ static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,static int rhashtable_rehash_one (struct rhashtable *ht,unsigned int old_hash) {struct bucket_table *old_tbl =rht_dereference (ht->tbl, ht); -struct bucket_table *new_tbl =rhashtable_last_table (ht, -rht_dereference_rcu (old_tbl->future_tbl, ht)); +struct bucket_table *new_tbl =rhashtable_last_table (ht, old_tbl);struct rhash_head __rcu **pprev =rht_bucket_var (old_tbl, old_hash);int err = -EAGAIN ;struct rhash_head *head, *next, *entry;
@@ -467,7 +466,7 @@ static int rhashtable_insert_rehash(struct rhashtable *ht, fail:/* Do not fail the insert if someone else did a rehash. */ -if (likely (rcu_dereference_raw (tbl->future_tbl))) +if (likely (rcu_access_pointer (tbl->future_tbl)))return 0 ;/* Schedule async rehash to retry allocation in process context. */
@@ -540,7 +539,7 @@ static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,if (PTR_ERR (data) != -EAGAIN &&PTR_ERR (data) != -ENOENT )return ERR_CAST (data); - new_tbl =rcu_dereference (tbl->future_tbl); + new_tbl =rht_dereference_rcu (tbl->future_tbl, ht);if (new_tbl)return new_tbl;
@@ -599,7 +598,7 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,break ;spin_unlock_bh (lock); - tbl =rcu_dereference (tbl->future_tbl); + tbl =rht_dereference_rcu (tbl->future_tbl, ht); } data =rhashtable_lookup_one (ht, tbl, hash, key, obj);
Re: [PATCH 08/18] rhashtable: clean up dereference of ->future_tbl.
Quoted 13 lines · NeilBrown
Using
rht_dereference_bucket()to dereference ->future_tbl looks like a type error, and could be confusing. Usingrht_dereference_rcu()to test a pointer for NULL adds an unnecessary barrier -rcu_access_pointer()is preferred for NULL tests when no lock is held.This uses 3 different ways to access ->future_tbl.
- if we know the mutex is held, userht_dereference()
- if we don't hold the mutex, and are only testing for NULL, usercu_access_pointer()
- otherwise (using RCU protection for true dereference), userht_dereference_rcu().
Quoted 5 lines
Note that this includes a simplification of the call to
rhashtable_last_table()- we don't do an extra dereference before the call any more.Signed-off-by NeilBrown <[email protected]>
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
PATCH 9/18 rhashtable: use cmpxchg() in nested_table_alloc()
In reply to NeilBrownnested_table_alloc() relies on the fact that there is at most one spinlock allocated for every slot in the top level nested table, so it is not possible for two threads to try to allocate the same table at the same time.
A future patch will change the locking and invalidate this assumption. So change the code to protect against a race using cmpxchg() - if it loses, it frees the table that it allocated.
--- lib/rhashtable.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/rhashtable.c +5 -3
@@ -132,9 +132,11 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht,INIT_RHT_NULLS_HEAD (ntbl[i].bucket); } -rcu_assign_pointer (*prev, ntbl); - -return ntbl; +if (cmpxchg (prev,NULL , ntbl) ==NULL ) +return ntbl; +/* Raced with another thread. */ +kfree (ntbl); +return rcu_dereference (*prev); } static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
PATCH 10/18 rhashtable: remove rhashtable_walk_peek()
In reply to NeilBrownThis function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.
This function is not currently used and is not worth keeping, so remove it.
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
--- include/linux/rhashtable.h | 1 - lib/rhashtable.c | 34 ---------------------------------- 2 files changed, 35 deletions(-)
include/linux/rhashtable.h +0 -1
@@ -247,7 +247,6 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter) }void *rhashtable_walk_next (struct rhashtable_iter *iter); -void *rhashtable_walk_peek (struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); void rhashtable_free_and_destroy(struct rhashtable *ht,
lib/rhashtable.c +0 -34
@@ -891,40 +891,6 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) }EXPORT_SYMBOL_GPL (rhashtable_walk_next); -/** -* rhashtable_walk_peek - Return the next object but don't advance the iterator -* @iter: Hash table iterator -* -* Returns the next object or NULL when the end of the table is reached. -* -* Returns -EAGAIN if resize event occurred. Note that the iterator -* will rewind back to the beginning and you may continue to use it. -*/ -void *rhashtable_walk_peek (struct rhashtable_iter *iter) -{ -struct rhlist_head *list = iter->list; -struct rhashtable *ht = iter->ht; -struct rhash_head *p = iter->p; - -if (p) -return rht_obj (ht, ht->rhlist ? &list->rhead : p); - -/* No object found in current iter, find next one in the table. */ - -if (iter->skip) { -/* A nonzero skip value points to the next entry in the table -* beyond that last one that was found. Decrement skip so -* we find the current value. __rhashtable_walk_find_next -* will restore the original value of skip assuming that -* the table hasn't changed. -*/ - iter->skip--; - } - -return __rhashtable_walk_find_next (iter); -} -EXPORT_SYMBOL_GPL (rhashtable_walk_peek); -/** * rhashtable_walk_stop - Finish a hash table walk * @iter: Hash table iterator
Herbert Xu
maintainerRe: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 6 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.
Quoted 8 lines
This function is not currently used and is not worth keeping, so remove it.
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>
Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my new rhashtable_walk_prev() will use it like rhasthable_talk_prev() ?: rhashtable_walk_next() which is close to what rhashtable_walk_peek() does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.
So provide the most understandable/maintainable solution, I think we should provide rhashtable_walk_prev() as a separate interface.
Thanks,
NeilBronw
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 36 lines · NeilBrown
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my newrhashtable_walk_prev()will use it likerhasthable_talk_prev()?:rhashtable_walk_next()which is close to whatrhashtable_walk_peek()does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.
Quoted 3 lines
So provide the most understandable/maintainable solution, I think we should provide
rhashtable_walk_prev()as a separate interface.
I'm still missing why requiring two API operations instead of one is simpler or easier to document. Also, I disagree that rhashtable_walk_peek does two things-- it just does one which is to return the current element in the walk without advancing to the next one. The fact that the iterator may or may not move is immaterial in the API, that is an implementation detail. In fact, it's conceivable that we might completely reimplement this someday such that the iterator works completely differently implementation semantics but the API doesn't change. Also the naming in your proposal is confusing, we'd have operations to get the previous, and the next next object-- so the user may ask where's the API to get the current object in the walk? The idea that we get it by first trying to get the previous object, and then if that fails getting the next object seems counterintuitive.
Tom
Tom
Quoted 2 lines
Thanks,
NeilBronw
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 40 lines · Tom Herbert
Quoted 36 lines · NeilBrown
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my newrhashtable_walk_prev()will use it likerhasthable_talk_prev()?:rhashtable_walk_next()which is close to whatrhashtable_walk_peek()does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.Quoted 3 lines
So provide the most understandable/maintainable solution, I think we should provide
rhashtable_walk_prev()as a separate interface.
Quoted 14 lines
I'm still missing why requiring two API operations instead of one is simpler or easier to document. Also, I disagree that
rhashtable_walk_peekdoes two things-- it just does one which is to return the current element in the walk without advancing to the next one. The fact that the iterator may or may not move is immaterial in the API, that is an implementation detail. In fact, it's conceivable that we might completely reimplement this someday such that the iterator works completely differently implementation semantics but the API doesn't change. Also the naming in your proposal is confusing, we'd have operations to get the previous, and the next next object-- so the user may ask where's the API to get the current object in the walk? The idea that we get it by first trying to get the previous object, and then if that fails getting the next object seems counterintuitive.
To respond to your points out of order:
- I accept that "rhashtable_walk_prev" is not a perfect name. It suggests a stronger symmetry with rhasthable_walk_next than actually exist. I cannot think of a better name, but I think the description "Return the previously returned object if it is still in the table" is clear and simple and explains the name. I'm certainly open to suggestions for a better name.
- I don't think it is meaningful to talk about a "current" element in a table where asynchronous insert/remove is to be expected. The best we can hope for is a "current location" is the sequence of objects in the table - a location which is after some objects and before all others. rhashtable_walk_next() returns the next object after the current location, and advances the location pointer past that object.
rhashtable_walk_prev() *doesn't* return the previous object in the table. It returns the previously returned object. ("previous" in time, but not in space, if you like).
- rhashtable_walk_peek() currently does one of two different things. It either returns the previously returned object (iter->p) if that is still in the table, or it find the next object, steps over it, and returns it.
- I would like to suggest that when an API acts on a iterator object, the question of whether or not the iterator is advanced *must* be a fundamental question, not one that might change from time to time.
Maybe a useful way forward would be for you to write documentation for the rhashtable_walk_peek() interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.
Thanks,
NeilBrown
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 82 lines · NeilBrown
Quoted 40 lines · Tom Herbert
Quoted 36 lines · NeilBrown
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my newrhashtable_walk_prev()will use it likerhasthable_talk_prev()?:rhashtable_walk_next()which is close to whatrhashtable_walk_peek()does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.Quoted 3 lines
So provide the most understandable/maintainable solution, I think we should provide
rhashtable_walk_prev()as a separate interface.Quoted 14 lines
I'm still missing why requiring two API operations instead of one is simpler or easier to document. Also, I disagree that
rhashtable_walk_peekdoes two things-- it just does one which is to return the current element in the walk without advancing to the next one. The fact that the iterator may or may not move is immaterial in the API, that is an implementation detail. In fact, it's conceivable that we might completely reimplement this someday such that the iterator works completely differently implementation semantics but the API doesn't change. Also the naming in your proposal is confusing, we'd have operations to get the previous, and the next next object-- so the user may ask where's the API to get the current object in the walk? The idea that we get it by first trying to get the previous object, and then if that fails getting the next object seems counterintuitive.To respond to your points out of order:
- I accept that "
rhashtable_walk_prev" is not a perfect name. It suggests a stronger symmetry withrhasthable_walk_nextthan actually exist. I cannot think of a better name, but I think the description "Return the previously returned object if it is still in the table" is clear and simple and explains the name. I'm certainly open to suggestions for a better name.- I don't think it is meaningful to talk about a "current" element in a table where asynchronous insert/remove is to be expected. The best we can hope for is a "current location" is the sequence of objects in the table - a location which is after some objects and before all others.
rhashtable_walk_next()returns the next object after the current location, and advances the location pointer past that object.
rhashtable_walk_prev()*doesn't* return the previous object in the table. It returns the previously returned object. ("previous" in time, but not in space, if you like).-
rhashtable_walk_peek()currently does one of two different things. It either returns the previously returned object (iter->p) if that is still in the table, or it find the next object, steps over it, and returns it.
Quoted 9 lines
- I would like to suggest that when an API acts on a iterator object, the question of whether or not the iterator is advanced *must* be a fundamental question, not one that might change from time to time.
Maybe a useful way forward would be for you to write documentation for the
rhashtable_walk_peek()interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.
Here's how it's documented currently:
"rhashtable_walk_peek - Return the next object but don't advance the iterator"
I don't see what is incorrect about that. Peek returns the next object in the walk, however does not move the iterator past that object, so sucessive calls to peek return the same object. In other words it's a way to inspect the next object but not "consume" it. This is what is needed when netlink returns in the middle of a walk. The last object retrieved from the table may not have been processed completely, so it needs to be the first one processed on the next invocation to netlink.
This is also easily distinguishable from
"rhashtable_walk_next - Return the next object and advance the iterator"
Where the only difference is that peek and walk is that, walk advances the iterator and peek does not. Hence why "peek" is a descriptive name for what is happening.
Tom
Quoted 2 lines
Thanks,
NeilBrown
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 105 lines · Tom Herbert
Quoted 82 lines · NeilBrown
Quoted 40 lines · Tom Herbert
Quoted 36 lines · NeilBrown
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my newrhashtable_walk_prev()will use it likerhasthable_talk_prev()?:rhashtable_walk_next()which is close to whatrhashtable_walk_peek()does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.Quoted 3 lines
So provide the most understandable/maintainable solution, I think we should provide
rhashtable_walk_prev()as a separate interface.Quoted 14 lines
I'm still missing why requiring two API operations instead of one is simpler or easier to document. Also, I disagree that
rhashtable_walk_peekdoes two things-- it just does one which is to return the current element in the walk without advancing to the next one. The fact that the iterator may or may not move is immaterial in the API, that is an implementation detail. In fact, it's conceivable that we might completely reimplement this someday such that the iterator works completely differently implementation semantics but the API doesn't change. Also the naming in your proposal is confusing, we'd have operations to get the previous, and the next next object-- so the user may ask where's the API to get the current object in the walk? The idea that we get it by first trying to get the previous object, and then if that fails getting the next object seems counterintuitive.To respond to your points out of order:
- I accept that "
rhashtable_walk_prev" is not a perfect name. It suggests a stronger symmetry withrhasthable_walk_nextthan actually exist. I cannot think of a better name, but I think the description "Return the previously returned object if it is still in the table" is clear and simple and explains the name. I'm certainly open to suggestions for a better name.- I don't think it is meaningful to talk about a "current" element in a table where asynchronous insert/remove is to be expected. The best we can hope for is a "current location" is the sequence of objects in the table - a location which is after some objects and before all others.
rhashtable_walk_next()returns the next object after the current location, and advances the location pointer past that object.
rhashtable_walk_prev()*doesn't* return the previous object in the table. It returns the previously returned object. ("previous" in time, but not in space, if you like).-
rhashtable_walk_peek()currently does one of two different things. It either returns the previously returned object (iter->p) if that is still in the table, or it find the next object, steps over it, and returns it.Quoted 9 lines
- I would like to suggest that when an API acts on a iterator object, the question of whether or not the iterator is advanced *must* be a fundamental question, not one that might change from time to time.
Maybe a useful way forward would be for you to write documentation for the
rhashtable_walk_peek()interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.Here's how it's documented currently:
"
rhashtable_walk_peek- Return the next object but don't advance the iterator"I don't see what is incorrect about that. Peek returns the next object in the walk, however does not move the iterator past that object, so sucessive calls to peek return the same object. In other words it's a way to inspect the next object but not "consume" it. This is what is needed when netlink returns in the middle of a walk. The last object retrieved from the table may not have been processed completely, so it needs to be the first one processed on the next invocation to netlink.
Quoted 8 lines
This is also easily distinguishable from
"
rhashtable_walk_next- Return the next object and advance the iterator"Where the only difference is that peek and walk is that, walk advances the iterator and peek does not. Hence why "peek" is a descriptive name for what is happening.
btw, we are using rhashtable_walk_peek with ILA code that hasn't been upstreamed yet. I'll (re)post the patches shortly, this demonstates why we need the peek functionality. If you think that rhashtable_walk_peek is nothing more than an inline that does "return rhashtable_walk_prev(iter) ? : rhashtable_walk_next(iter);" then maybe we could redefine rhashtable_walk_peek to be that. But, then I'll ask what the use case is for rhashtable_walk_prev as a standalone function? We created rhashtable_walk_peek for the netlink walk problem and I don't think any of the related use cases would ever call rhashtable_walk_prev without the rhashtable_walk_next fallback.
Tom
Quoted 4 lines
Tom
Quoted 2 lines
Thanks,
NeilBrown
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 115 lines · Tom Herbert
Quoted 105 lines · Tom Herbert
Quoted 82 lines · NeilBrown
Quoted 40 lines · Tom Herbert
Quoted 36 lines · NeilBrown
Quoted 10 lines · Herbert Xu
Quoted 9 lines · NeilBrown
This function has a somewhat confused behavior that is not properly described by the documentation.
Sometimes is returns the previous object, sometimes it returns the next one.
Sometimes it changes the iterator, sometimes it doesn't.This function is not currently used and is not worth keeping, so remove it.
Quoted 8 lines
Quoted 5 lines
A future patch will introduce a new function with a simpler interface which can meet the same need that this was added for.
Signed-off-by NeilBrown <[email protected]>Please keep Tom Herbert in the loop. IIRC he had an issue with this patch.
Yes you are right - sorry for forgetting to add Tom.
My understanding of where this issue stands is that Tom raised issue and asked for clarification, I replied, nothing further happened.
It summary, my position is that:
- most users of my newrhashtable_walk_prev()will use it likerhasthable_talk_prev()?:rhashtable_walk_next()which is close to whatrhashtable_walk_peek()does
- I know of no use-case that could not be solved if we only had the combined operation
- BUT it is hard to document the combined operation, as it really does two things. If it is hard to document, then it might be hard to understand.Quoted 3 lines
So provide the most understandable/maintainable solution, I think we should provide
rhashtable_walk_prev()as a separate interface.Quoted 14 lines
I'm still missing why requiring two API operations instead of one is simpler or easier to document. Also, I disagree that
rhashtable_walk_peekdoes two things-- it just does one which is to return the current element in the walk without advancing to the next one. The fact that the iterator may or may not move is immaterial in the API, that is an implementation detail. In fact, it's conceivable that we might completely reimplement this someday such that the iterator works completely differently implementation semantics but the API doesn't change. Also the naming in your proposal is confusing, we'd have operations to get the previous, and the next next object-- so the user may ask where's the API to get the current object in the walk? The idea that we get it by first trying to get the previous object, and then if that fails getting the next object seems counterintuitive.To respond to your points out of order:
- I accept that "
rhashtable_walk_prev" is not a perfect name. It suggests a stronger symmetry withrhasthable_walk_nextthan actually exist. I cannot think of a better name, but I think the description "Return the previously returned object if it is still in the table" is clear and simple and explains the name. I'm certainly open to suggestions for a better name.- I don't think it is meaningful to talk about a "current" element in a table where asynchronous insert/remove is to be expected. The best we can hope for is a "current location" is the sequence of objects in the table - a location which is after some objects and before all others.
rhashtable_walk_next()returns the next object after the current location, and advances the location pointer past that object.
rhashtable_walk_prev()*doesn't* return the previous object in the table. It returns the previously returned object. ("previous" in time, but not in space, if you like).-
rhashtable_walk_peek()currently does one of two different things. It either returns the previously returned object (iter->p) if that is still in the table, or it find the next object, steps over it, and returns it.Quoted 9 lines
- I would like to suggest that when an API acts on a iterator object, the question of whether or not the iterator is advanced *must* be a fundamental question, not one that might change from time to time.
Maybe a useful way forward would be for you to write documentation for the
rhashtable_walk_peek()interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.Here's how it's documented currently:
"
rhashtable_walk_peek- Return the next object but don't advance the iterator"I don't see what is incorrect about that. Peek returns the next object in the walk, however does not move the iterator past that object, so sucessive calls to peek return the same object. In other words it's a way to inspect the next object but not "consume" it. This is what is needed when netlink returns in the middle of a walk. The last object retrieved from the table may not have been processed completely, so it needs to be the first one processed on the next invocation to netlink.
Quoted 8 lines
This is also easily distinguishable from
"
rhashtable_walk_next- Return the next object and advance the iterator"Where the only difference is that peek and walk is that, walk advances the iterator and peek does not. Hence why "peek" is a descriptive name for what is happening.
Quoted 10 lines
btw, we are using
rhashtable_walk_peekwith ILA code that hasn't been upstreamed yet. I'll (re)post the patches shortly, this demonstates why we need the peek functionality. If you think thatrhashtable_walk_peekis nothing more than an inline that does "returnrhashtable_walk_prev(iter)? :rhashtable_walk_next(iter);" then maybe we could redefinerhashtable_walk_peekto be that. But, then I'll ask what the use case is forrhashtable_walk_prevas a standalone function? We createdrhashtable_walk_peekfor the netlink walk problem and I don't think any of the related use cases would ever callrhashtable_walk_prevwithout therhashtable_walk_nextfallback.
I think I did describe my particular use-case for rhashtable_walk_prev() before, but it probably got buried in other things.
Lustre has a debugfs file which lists all the cached pages in all the objects which are stored in a hash table.
It might get part-way through listing the cached pages for one object, and then have to take a break (rhashtable_walk_stop()) until more output space is available.
When it restarts, it needs to pick up where it left off.
If the same object is still in the cache, it needs to start at the page address that it was up to. If the object is gone and it has to move on to the next object, then it needs to start at page zero.
So I would do something like:
rhashtable_walk_start(hashtab, &state->riter);
obj = rhashtable_walk_prev(&state->riter);
addr = state->addr;
if (!obj) {
obj = rhashtable_walk_next(state->riter);
addr = 0;}
then repeatedly format(obj,addr) and incrment addr or (if past end-of-object) call rhashtable_walk_next and addr=0.
then
addr->addr = addr - 1;
rhashtable_walk_stop();
Thanks,
NeilBrown
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
In reply to Tom HerbertQuoted 7 lines · Tom Herbert
Quoted 6 lines
Maybe a useful way forward would be for you to write documentation for the
rhashtable_walk_peek()interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.
Quoted 5 lines
Here's how it's documented currently:
"
rhashtable_walk_peek- Return the next object but don't advance the iterator"I don't see what is incorrect about that.
rhashtable_walk_next is documented:
* rhashtable_walk_next - Return the next object and advance the iterator
So it seems reasonable to assume that you get the same object, no matter which one you call. Yet this is not (necessarily) the case.
Quoted 7 lines
Peek returns the next object
in the walk, however does not move the iterator past that object, so sucessive calls to peek return the same object. In other words it's a way to inspect the next object but not "consume" it. This is what is needed when netlink returns in the middle of a walk. The last object retrieved from the table may not have been processed completely, so it needs to be the first one processed on the next invocation to netlink.
I completely agree with this last sentence.
We typically need to process the last object retrieved. This could also be described as the previously retrieved object.
So rhashtable_walk_last() and rhashtable_walk_prev() might both be suitable names, though each is open to misinterpretation.
I fail to see how the "last object retrieved" could be the same as "the next object" which rhashtable_walk_peek claims to return.
Quoted 8 lines
This is also easily distinguishable from
"
rhashtable_walk_next- Return the next object and advance the iterator"Where the only difference is that peek and walk is that, walk advances the iterator and peek does not. Hence why "peek" is a descriptive name for what is happening.
Maybe if we step back and go for a totally different API.
We could change rhashtable_walk_start() to return the object that was current (most recently returned) when rhashtable_walk_stop() was called, if it is still in the table, otherwise it returns NULL (as it would the first time it was called).
This loses the option for rhashtable_walk_start() to return -EAGAIN, but I would rather than rhashtable_walk_next() were the only thing to return that.
The only time it really makes sense to call rhashtable_walk_peek() is immediately after rhashtable_walk_start(), and this change would make that fact clear in the API.
Thanks,
NeilBrown
A message that is not in the archive (sent off-list or not yet received): CALx6S36Ce-rXQMzmFYZVPGD10Bo6udvRAHiZ5gWwnzVwoTVv0w@mail.gmail.com
Re: [PATCH 10/18] rhashtable: remove rhashtable_walk_peek()
Quoted 41 lines · Tom Herbert
Quoted 32 lines · NeilBrown
Quoted 10 lines · Tom Herbert
Quoted 6 lines
Maybe a useful way forward would be for you to write documentation for the
rhashtable_walk_peek()interface which correctly describes what it does and how it is used. Given that, I can implement that interface with the stability improvements that I'm working on.Here's how it's documented currently:
"
rhashtable_walk_peek- Return the next object but don't advance theiterator"
Quoted 2 lines
I don't see what is incorrect about that.
rhashtable_walk_nextis documented:*
rhashtable_walk_next- Return the next object and advance the iteratorSo it seems reasonable to assume that you get the same object, no matter which one you call. Yet this is not (necessarily) the case.
Quoted 7 lines
Peek returns the next object
in the walk, however does not move the iterator past that object, so sucessive calls to peek return the same object. In other words it's a way to inspect the next object but not "consume" it. This is what is needed when netlink returns in the middle of a walk. The last object retrieved from the table may not have been processed completely, so it needs to be the first one processed on the next invocation to netlink.Quoted 6 lines
I completely agree with this last sentence.
We typically need to process the last object retrieved. This could also be described as the previously retrieved object.
Sorhashtable_walk_last()andrhashtable_walk_prev()might both be suitable names, though each is open to misinterpretation.
Quoted 3 lines
rhashtable_walk_lastis better, but still could have the connotation that it returns the last element in the list or table. How aboutrhashtable_walk_last_seenorrhashtable_walk_last_iter?
I'd be quite happy with rhashtable_walk_last_seen().
I also be happy to keep rhasthable_walk_peek() but to implement it as
rhashtable_walk_last_seen() ?: rhashtable_walk_next()
I'll send patches to that effect some time this week.
Thanks,
NeilBrown
RFC PATCH 1/1 rhashtable: add rhashtable_walk_last_seen()
rhashtable_walk_last_seen() returns the object returned by the previous rhashtable_walk_next(), providing it is still in the table (or was during this grace period).
This works even if rhashtable_walk_stop() and rhashtable_talk_start() have been called since the last rhashtable_walk_next().
If there have been no calls to rhashtable_walk_next(), or if the object is gone from the table, then NULL is returned.
This can usefully be used in a seq_file ->start() function. If the pos is the same as was returned by the last ->next() call, then rhashtable_walk_last_seen() can be used to re-establish the current location in the table. If it returns NULL, then rhashtable_walk_next() should be used.
--- include/linux/rhashtable.h | 1 + lib/rhashtable.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/linux/rhashtable.h +1 -0
@@ -248,6 +248,7 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter)void *rhashtable_walk_next (struct rhashtable_iter *iter);void *rhashtable_walk_peek (struct rhashtable_iter *iter); +void *rhashtable_walk_last_seen (struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); void rhashtable_free_and_destroy(struct rhashtable *ht,
lib/rhashtable.c +30 -0
@@ -949,6 +949,36 @@ void *rhashtable_walk_peek(struct rhashtable_iter *iter) }EXPORT_SYMBOL_GPL (rhashtable_walk_peek); +/** +* rhashtable_walk_last_seen - Return the previously returned object, if available +* @iter: Hash table iterator +* +* If rhashtable_walk_next() has previously been called and the object +* it returned is still in the hash table, that object is returned again, +* otherwise %NULL is returned. +* +* If the recent rhashtable_walk_next() call was since the most recent +* rhashtable_walk_start() call then the returned object may not, strictly +* speaking, still be in the table. It will be safe to dereference. +* +* Note that the iterator is not changed. +*/ +void *rhashtable_walk_last_seen (struct rhashtable_iter *iter) +{ +struct rhashtable *ht = iter->ht; +struct rhash_head *p = iter->p; + +if (!p) +return NULL ; +if (!iter->p_is_unsafe || ht->rhlist) +return p; +rht_for_each_rcu (p, iter->walker.tbl, iter->slot) +if (p == iter->p) +return p; +return NULL ; +} +EXPORT_SYMBOL_GPL (rhashtable_walk_last_seen); +/** * rhashtable_walk_stop - Finish a hash table walk * @iter: Hash table iterator
Signature · 1 line
2.14.0.rc0.dirty
RFC PATCH 1/1 rhashtable: implement rhashtable_walk_peek() using rhashtable_walk_last_seen()
rhashtable_walk_last_seen() does most of the work that rhashtable_walk_peek() needs done, so use it.
Also update the documentation for rhashtable_walk_peek() to clarify the expected use case.
--- lib/rhashtable.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-)
lib/rhashtable.c +20 -22
@@ -916,36 +916,34 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter)EXPORT_SYMBOL_GPL (rhashtable_walk_next);/** -* rhashtable_walk_peek - Return the next object but don't advance the iterator +* rhashtable_walk_peek - Return the next object to use in an interrupted walk * @iter: Hash table iterator * -* Returns the next object or NULL when the end of the table is reached. +* Returns the "current" object or NULL when the end of the table is reached. +* When an rhashtable_walk is interrupted with rhashtable_walk_stop(), +* it is often because an object was found that could not be processed +* immediately, possible because there is no more space to encode details +* of the object (e.g. when producing a seq_file from the table). +* When the walk is restarted, the same object needs to be processed again, +* if possible. The object might have been removed from the table while +* the walk was paused, so it might not be available. In that case, the +* normal "next" object should be treated as "current". * -* Returns -EAGAIN if resize event occurred. Note that the iterator +* To support this common case, rhashtable_walk_peek() returns the +* appropriate object to process after an interrupted walk, either the +* one that was most recently returned, or if that doesn't exist - the +* next one. +* +* Returns -EAGAIN if resize event occurred. In that case the iterator * will rewind back to the beginning and you may continue to use it. */ void *rhashtable_walk_peek (struct rhashtable_iter *iter) { -struct rhlist_head *list = iter->list; -struct rhashtable *ht = iter->ht; -struct rhash_head *p = iter->p; +void *ret =rhashtable_walk_last_seen (iter); -if (p) -return rht_obj (ht, ht->rhlist ? &list->rhead : p); - -/* No object found in current iter, find next one in the table. */ - -if (iter->skip) { -/* A nonzero skip value points to the next entry in the table -* beyond that last one that was found. Decrement skip so -* we find the current value. __rhashtable_walk_find_next -* will restore the original value of skip assuming that -* the table hasn't changed. -*/ - iter->skip--; - } - -return __rhashtable_walk_find_next (iter); +if (!ret) + ret =rhashtable_walk_next (iter); +return ret; }EXPORT_SYMBOL_GPL (rhashtable_walk_peek);
Signature · 1 line
2.14.0.rc0.dirty
A message that is not in the archive (sent off-list or not yet received): CALx6S35GgUOd0dPgv7P96wNNTv5pN7fij0pcAoccqcSWZhvY7Q@mail.gmail.com
RFC PATCH v2 1/1 rhashtable: implement rhashtable_walk_peek() using rhashtable_walk_last_seen()
rhashtable_walk_last_seen() does most of the work that rhashtable_walk_peek() needs done, so use it and put
it in a "static inline".
Also update the documentation for rhashtable_walk_peek() to clarify the expected use case.
---
v2 as static-inline - suggested by Tom.
Thanks,
NeilBrown
include/linux/rhashtable.h | 29 ++++++++++++++++++++++++++++- lib/rhashtable.c | 34 ---------------------------------- 2 files changed, 28 insertions(+), 35 deletions(-)
include/linux/rhashtable.h +28 -1
@@ -247,10 +247,37 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter) }void *rhashtable_walk_next (struct rhashtable_iter *iter); -void *rhashtable_walk_peek (struct rhashtable_iter *iter);void *rhashtable_walk_last_seen (struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); +/** +* rhashtable_walk_peek - Return the next object to use in an interrupted walk +* @iter: Hash table iterator +* +* Returns the "current" object or NULL when the end of the table is reached. +* When an rhashtable_walk is interrupted with rhashtable_walk_stop(), +* it is often because an object was found that could not be processed +* immediately, possible because there is no more space to encode details +* of the object (e.g. when producing a seq_file from the table). +* When the walk is restarted, the same object needs to be processed again, +* if possible. The object might have been removed from the table while +* the walk was paused, so it might not be available. In that case, the +* normal "next" object should be treated as "current". +* +* To support this common case, rhashtable_walk_peek() returns the +* appropriate object to process after an interrupted walk, either the +* one that was most recently returned, or if that doesn't exist - the +* next one. +* +* Returns -EAGAIN if resize event occurred. In that case the iterator +* will rewind back to the beginning and you may continue to use it. +*/ +static inline void *rhashtable_walk_peek (struct rhashtable_iter *iter) +{ +return rhashtable_walk_last_seen (iter) ?: +rhashtable_walk_next (iter); +} +void rhashtable_free_and_destroy (struct rhashtable *ht,void (*free_fn)(void *ptr,void *arg),void *arg);
lib/rhashtable.c +0 -34
@@ -915,40 +915,6 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) }EXPORT_SYMBOL_GPL (rhashtable_walk_next); -/** -* rhashtable_walk_peek - Return the next object but don't advance the iterator -* @iter: Hash table iterator -* -* Returns the next object or NULL when the end of the table is reached. -* -* Returns -EAGAIN if resize event occurred. Note that the iterator -* will rewind back to the beginning and you may continue to use it. -*/ -void *rhashtable_walk_peek (struct rhashtable_iter *iter) -{ -struct rhlist_head *list = iter->list; -struct rhashtable *ht = iter->ht; -struct rhash_head *p = iter->p; - -if (p) -return rht_obj (ht, ht->rhlist ? &list->rhead : p); - -/* No object found in current iter, find next one in the table. */ - -if (iter->skip) { -/* A nonzero skip value points to the next entry in the table -* beyond that last one that was found. Decrement skip so -* we find the current value. __rhashtable_walk_find_next -* will restore the original value of skip assuming that -* the table hasn't changed. -*/ - iter->skip--; - } - -return __rhashtable_walk_find_next (iter); -} -EXPORT_SYMBOL_GPL (rhashtable_walk_peek); -/** * rhashtable_walk_last_seen - Return the previously returned object, if available * @iter: Hash table iterator
Signature · 1 line
2.14.0.rc0.dirty
Re: [PATCH RFC v2] rhashtable: implement rhashtable_walk_peek() using rhashtable_walk_last_seen()
Quoted 8 lines · NeilBrown
rhashtable_walk_last_seen()does most of the work thatrhashtable_walk_peek()needs done, so use it and put
it in a "static inline".
Also update the documentation forrhashtable_walk_peek()to clarify the expected use case.Signed-off-by NeilBrown <[email protected]>
Quoted 102 lines
---
v2 as static-inline - suggested by Tom.
Thanks,
NeilBrowninclude/linux/rhashtable.h | 29 ++++++++++++++++++++++++++++- lib/rhashtable.c | 34 ---------------------------------- 2 files changed, 28 insertions(+), 35 deletions(-)include/linux/rhashtable.h +28
-1@@ -247,10 +247,37 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter) }void *rhashtable_walk_next (struct rhashtable_iter *iter); -void *rhashtable_walk_peek (struct rhashtable_iter *iter);void *rhashtable_walk_last_seen (struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); +/** +* rhashtable_walk_peek - Return the next object to use in an interrupted walk +* @iter: Hash table iterator +* +* Returns the "current" object or NULL when the end of the table is reached. +* When an rhashtable_walk is interrupted with rhashtable_walk_stop(), +* it is often because an object was found that could not be processed +* immediately, possible because there is no more space to encode details +* of the object (e.g. when producing a seq_file from the table). +* When the walk is restarted, the same object needs to be processed again, +* if possible. The object might have been removed from the table while +* the walk was paused, so it might not be available. In that case, the +* normal "next" object should be treated as "current". +* +* To support this common case, rhashtable_walk_peek() returns the +* appropriate object to process after an interrupted walk, either the +* one that was most recently returned, or if that doesn't exist - the +* next one. +* +* Returns -EAGAIN if resize event occurred. In that case the iterator +* will rewind back to the beginning and you may continue to use it. +*/ +static inline void *rhashtable_walk_peek (struct rhashtable_iter *iter) +{ +return rhashtable_walk_last_seen (iter) ?: +rhashtable_walk_next (iter); +} +void rhashtable_free_and_destroy (struct rhashtable *ht,void (*free_fn)(void *ptr,void *arg),void *arg);lib/rhashtable.c +0
-35@@ -915,40 +915,6 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) }EXPORT_SYMBOL_GPL (rhashtable_walk_next); -/** -* rhashtable_walk_peek - Return the next object but don't advance the iterator -* @iter: Hash table iterator -* -* Returns the next object or NULL when the end of the table is reached. -* -* Returns -EAGAIN if resize event occurred. Note that the iterator -* will rewind back to the beginning and you may continue to use it. -*/ -void *rhashtable_walk_peek (struct rhashtable_iter *iter) -{ -struct rhlist_head *list = iter->list; -struct rhashtable *ht = iter->ht; -struct rhash_head *p = iter->p; - -if (p) -return rht_obj (ht, ht->rhlist ? &list->rhead : p); - -/* No object found in current iter, find next one in the table. */ - -if (iter->skip) { -/* A nonzero skip value points to the next entry in the table -* beyond that last one that was found. Decrement skip so -* we find the current value. __rhashtable_walk_find_next -* will restore the original value of skip assuming that -* the table hasn't changed. -*/ - iter->skip--; - } - -return __rhashtable_walk_find_next (iter); -} -EXPORT_SYMBOL_GPL (rhashtable_walk_peek); -/** * rhashtable_walk_last_seen - Return the previously returned object, if available * @iter: Hash table iterator -- 2.14.0.rc0.dirty
Re: [PATCH RFC v2] rhashtable: implement rhashtable_walk_peek() using rhashtable_walk_last_seen()
Quoted 11 lines · Tom Herbert
Quoted 8 lines · NeilBrown
rhashtable_walk_last_seen()does most of the work thatrhashtable_walk_peek()needs done, so use it and put
it in a "static inline".
Also update the documentation forrhashtable_walk_peek()to clarify the expected use case.Signed-off-by NeilBrown <[email protected]>Acked-by Tom Herbert <[email protected]>
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH RFC v2] rhashtable: implement rhashtable_walk_peek() using rhashtable_walk_last_seen()
Quoted 9 lines · Herbert Xu
Quoted 8 lines · Tom Herbert
Quoted 7 lines · NeilBrown
rhashtable_walk_last_seen()does most of the work thatrhashtable_walk_peek()needs done, so use it and put
it in a "static inline".
Also update the documentation forrhashtable_walk_peek()to clarify the expected use case.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by NeilBrown <[email protected]>Acked-by Tom Herbert <[email protected]>Acked-by Herbert Xu <[email protected]>
Thanks to your both.
I assume this is an implicit Ack for
rhashtable: add rhashtable_walk_last_seen()
I'll send out a patchset of all the acked patches shortly after the merge window closes, and then see where we are up to.
Thanks,
NeilBrown
PATCH 11/18 rhashtable: further improve stability of rhashtable_walk
In reply to NeilBrownIf the sequence:
obj = rhashtable_walk_next(iter); rhashtable_walk_stop(iter); rhashtable_remove_fast(ht, &obj->head, params); rhashtable_walk_start(iter);
races with another thread inserting or removing an object on the same hash chain, a subsequent rhashtable_walk_next() is not guaranteed to get the "next" object. It is possible that an object could be repeated, or missed.
This can be made more reliable by keeping the objects in a hash chain sorted by memory address. A subsequent rhashtable_walk_next() call can reliably find the correct position in the list, and thus find the 'next' object.
It is not possible to take this approach with an rhltable as keeping the hash chain in order is not so easy. When the first object with a given key is removed, it is replaced in the chain with the next object with the same key, and the address of that object may not be correctly ordered.
I have not yet found any way to achieve the same stability with rhltables, that doesn't have a major impact on lookup or insert. No code currently in Linux would benefit from such extra stability.
With this patch:
- a new object is always inserted after the last object with a smaller address, or at the start. This preserves the property, important when allowing objects to be removed and re-added, that an object is never inserted *after* a position that it previously held in the list.
- when rhashtable_walk_start() is called, it records that 'p' is not 'safe', meaning that it cannot be dereferenced. The revalidation that was previously done here is moved to rhashtable_walk_next()
- when rhashtable_walk_next() is called while p is not NULL and not safe, it walks the chain looking for the first object with an address greater than p and returns that. If there is none, it moves to the next hash chain.
--- include/linux/rhashtable-types.h | 1 include/linux/rhashtable.h | 10 ++++- lib/rhashtable.c | 82 +++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 31 deletions(-)
include/linux/rhashtable-types.h +1 -0
@@ -126,6 +126,7 @@ struct rhashtable_iter {struct rhashtable_walker walker;unsigned int slot;unsigned int skip; +bool p_is_unsafe;bool end_of_table; };
include/linux/rhashtable.h +8 -2
@@ -627,7 +627,12 @@ static inline void *__rhashtable_insert_fast( (params.obj_cmpfn ? params.obj_cmpfn(&arg, rht_obj(ht, head)) : rhashtable_compare(&arg, rht_obj(ht, head)))) { - pprev = &head->next; +if (rhlist) { + pprev = &head->next; + }else { +if (head < obj) + headp = &head->next; + }continue ; }
@@ -1123,7 +1128,8 @@ static inline int rhashtable_walk_init(struct rhashtable *ht,* Note that if you restart a walk after rhashtable_walk_stop you * may see the same object twice. Also, you may miss objects if * there are removals in between rhashtable_walk_stop and the next -* call to rhashtable_walk_start. +* call to rhashtable_walk_start. Note that this is different to +* rhashtable_walk_enter() which misses objects. * * For a completely stable walk you should construct your own data * structure outside the hash table.
lib/rhashtable.c +53 -29
127 lines not on this page · raw message
PATCH 12/18 rhashtable: add rhashtable_walk_prev()
In reply to NeilBrownrhashtable_walk_prev() returns the object returned by
the previous rhashtable_walk_next(), providing it is still in the table (or was during this grace period).
This works even if rhashtable_walk_stop() and rhashtable_talk_start() have been called since the last rhashtable_walk_next().
If there have been no calls to rhashtable_walk_next(), or if the object is gone from the table, then NULL is returned.
This can usefully be used in a seq_file ->start() function. If the pos is the same as was returned by the last ->next() call, then rhashtable_walk_prev() can be used to re-establish the current location in the table. If it returns NULL, then rhashtable_walk_next() should be used.
--- include/linux/rhashtable.h | 1 + lib/rhashtable.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+)
include/linux/rhashtable.h +1 -0
@@ -247,6 +247,7 @@ static inline void rhashtable_walk_start(struct rhashtable_iter *iter) }void *rhashtable_walk_next (struct rhashtable_iter *iter); +void *rhashtable_walk_prev (struct rhashtable_iter *iter); void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU); void rhashtable_free_and_destroy(struct rhashtable *ht,
lib/rhashtable.c +31 -0
@@ -915,6 +915,37 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) }EXPORT_SYMBOL_GPL (rhashtable_walk_next); +/** +* rhashtable_walk_prev - Return the previously returned object, if available +* @iter: Hash table iterator +* +* If rhashtable_walk_next() has previously been called and the object +* it returned is still in the hash table, that object is returned again, +* otherwise %NULL is returned. +* +* If the recent rhashtable_walk_next() call was since the most recent +* rhashtable_walk_start() call then the returned object may not, strictly +* speaking, still be in the table. It will be safe to dereference. +* +* Note that the iterator is not changed and in particular it does not +* step backwards. +*/ +void *rhashtable_walk_prev (struct rhashtable_iter *iter) +{ +struct rhashtable *ht = iter->ht; +struct rhash_head *p = iter->p; + +if (!p) +return NULL ; +if (!iter->p_is_unsafe || ht->rhlist) +return p; +rht_for_each_rcu (p, iter->walker.tbl, iter->slot) +if (p == iter->p) +return p; +return NULL ; +} +EXPORT_SYMBOL_GPL (rhashtable_walk_prev); +/** * rhashtable_walk_stop - Finish a hash table walk * @iter: Hash table iterator
PATCH 13/18 rhashtable: don't hold lock on first table throughout insertion.
In reply to NeilBrownrhashtable_try_insert() currently hold a lock on the bucket in the first table, while also locking buckets in subsequent tables. This is unnecessary and looks like a hold-over from some earlier version of the implementation.
As insert and remove always lock a bucket in each table in turn, and as insert only inserts in the final table, there cannot be any races that are not covered by simply locking a bucket in each table in turn.
When an insert call reaches that last table it can be sure that there is no match entry in any other table as it has searched them all, and insertion never happens anywhere but in the last table. The fact that code tests for the existence of future_tbl while holding a lock on the relevant bucket ensures that two threads inserting the same key will make compatible decisions about which is the "last" table.
This simplifies the code and allows the ->rehash field to be discarded.
We still need a way to ensure that a dead bucket_table is never re-linked by rhashtable_walk_stop(). This can be achieved by calling call_rcu() inside the locked region, and checking ->rcu.func in rhashtable_walk_stop(). If it is not NULL, then the bucket table is empty and dead.
--- include/linux/rhashtable.h | 13 ------------- lib/rhashtable.c | 44 +++++++++++--------------------------------- 2 files changed, 11 insertions(+), 46 deletions(-)
include/linux/rhashtable.h +0 -13
@@ -63,7 +63,6 @@struct bucket_table {unsigned int size;unsigned int nest; -unsigned int rehash;u32 hash_rnd;unsigned int locks_mask;spinlock_t *locks;
@@ -774,12 +773,6 @@ static inline int rhltable_insert(* @obj: pointer to hash head inside object * @params: hash table parameters * -* Locks down the bucket chain in both the old and new table if a resize -* is in progress to ensure that writers can't remove from the old table -* and can't insert to the new table during the atomic operation of search -* and insertion. Searches for duplicates in both the old and new table if -* a resize is in progress. -* * This lookup function may only be used for fixed key hash table (key_len * parameter set). It will BUG() if used inappropriately. *
@@ -835,12 +828,6 @@ static inline void *rhashtable_lookup_get_insert_fast(* @obj: pointer to hash head inside object * @params: hash table parameters * -* Locks down the bucket chain in both the old and new table if a resize -* is in progress to ensure that writers can't remove from the old table -* and can't insert to the new table during the atomic operation of search -* and insertion. Searches for duplicates in both the old and new table if -* a resize is in progress. -* * Lookups may occur in parallel with hashtable mutations and resizing. * * Will trigger an automatic deferred table resizing if residency in the
lib/rhashtable.c +11 -33
86 lines not on this page · raw message
PATCH 14/18 rhashtable: allow rht_bucket_var to return NULL.
In reply to NeilBrownRather than returning a pointer a static nulls, rht_bucket_var() now returns NULL if the bucket doesn't exist.
This will make the next patch, which stores a bitlock in the bucket pointer, somewhat cleaner.
--- include/linux/rhashtable.h | 11 +++++++++-- lib/rhashtable.c | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-)
include/linux/rhashtable.h +9 -2
@@ -256,6 +256,8 @@ void rhashtable_destroy(struct rhashtable *ht);struct rhash_head __rcu **rht_bucket_nested (const struct bucket_table *tbl,unsigned int hash); +struct rhash_head __rcu **__rht_bucket_nested (const struct bucket_table *tbl, +unsigned int hash);struct rhash_head __rcu **rht_bucket_nested_insert (struct rhashtable *ht,struct bucket_table *tbl,unsigned int hash);
@@ -285,7 +287,7 @@ static inline struct rhash_head __rcu *const *rht_bucket(static inline struct rhash_head __rcu **rht_bucket_var (struct bucket_table *tbl,unsigned int hash) { -return unlikely (tbl->nest) ?rht_bucket_nested (tbl, hash) : +return unlikely (tbl->nest) ?__rht_bucket_nested (tbl, hash) : &tbl->buckets[hash]; }
@@ -888,6 +890,8 @@ static inline int __rhashtable_remove_fast_one(spin_lock_bh (lock); pprev =rht_bucket_var (tbl, hash); +if (!pprev) + goto out;rht_for_each_continue (he, *pprev, tbl, hash) {struct rhlist_head *list;
@@ -932,6 +936,7 @@ static inline int __rhashtable_remove_fast_one(break ; } +out:spin_unlock_bh (lock);if (err >0 ) {
@@ -1040,6 +1045,8 @@ static inline int __rhashtable_replace_fast(spin_lock_bh (lock); pprev =rht_bucket_var (tbl, hash); +if (!pprev) + goto out;rht_for_each_continue (he, *pprev, tbl, hash) {if (he != obj_old) { pprev = &he->next;
@@ -1051,7 +1058,7 @@ static inline int __rhashtable_replace_fast( err =0 ;break ; } - +out:spin_unlock_bh (lock);return err;
lib/rhashtable.c +19 -7
@@ -240,8 +240,10 @@ static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash) goto out; err = -ENOENT ; +if (!pprev) + goto out; -rht_for_each (entry, old_tbl, old_hash) { +rht_for_each_continue (entry, *pprev, old_tbl, old_hash) { err =0 ; next =rht_dereference_bucket (entry->next, old_tbl, old_hash);
@@ -498,6 +500,8 @@ static void *rhashtable_lookup_one(struct rhashtable *ht, elasticity =RHT_ELASTICITY ; pprev =rht_bucket_var (tbl, hash); +if (!pprev) +return ERR_PTR (-ENOENT );rht_for_each_continue (head, *pprev, tbl, hash) {struct rhlist_head *list;struct rhlist_head *plist;
@@ -1161,11 +1165,10 @@ void rhashtable_destroy(struct rhashtable *ht) }EXPORT_SYMBOL_GPL (rhashtable_destroy); -struct rhash_head __rcu **rht_bucket_nested (const struct bucket_table *tbl, -unsigned int hash) +struct rhash_head __rcu **__rht_bucket_nested (const struct bucket_table *tbl, +unsigned int hash) {const unsigned int shift =PAGE_SHIFT -ilog2 (sizeof (void *)); -static struct rhash_head __rcu *rhnull;unsigned int index = hash & ((1 << tbl->nest) -1 );unsigned int size = tbl->size >> tbl->nest;unsigned int subhash = hash;
@@ -1184,14 +1187,23 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, }if (!ntbl) { -if (!rhnull) -INIT_RHT_NULLS_HEAD (rhnull); -return &rhnull; +return NULL ; }return &ntbl[subhash].bucket; } +EXPORT_SYMBOL_GPL (__rht_bucket_nested); + +struct rhash_head __rcu **rht_bucket_nested (const struct bucket_table *tbl, +unsigned int hash) +{ +static struct rhash_head __rcu *rhnull; + +if (!rhnull) +INIT_RHT_NULLS_HEAD (rhnull); +return __rht_bucket_nested (tbl, hash) ?: &rhnull; +}EXPORT_SYMBOL_GPL (rht_bucket_nested); struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
PATCH 15/18 rhashtable: use bit_spin_locks to protect hash bucket.
In reply to NeilBrownThis patch changes rhashtables to use a bit_spin_lock (BIT(1)) the bucket pointer to lock the hash chain for that bucket.
The benefits of a bit spin_lock are:
- no need to allocate a separate array of locks.
- no need to have a configuration option to guide the choice of the size of this array
- locking cost if often a single test-and-set in a cache line that will have to be loaded anyway. When inserting at, or removing from, the head of the chain, the unlock is free - writing the new address in the bucket head implicitly clears the lock bit.
- even when lockings costs 2 updates (lock and unlock), they are in a cacheline that needs to be read anyway.
The cost of using a bit spin_lock is a little bit of code complexity, which I think is quite manageable.
Bit spin_locks are sometimes inappropriate because they are not fair - if multiple CPUs repeatedly contend of the same lock, one CPU can easily be starved. This is not a credible situation with rhashtable. Multiple CPUs may want to repeatedly add or remove objects, but they will typically do so at different buckets, so they will attempt to acquire different locks.
As we have more bit-locks than we previously had spinlocks (by at least a factor of two) we can expect slightly less contention to go with the slightly better cache behavior and reduced memory consumption.
--- include/linux/rhashtable-types.h | 2 include/linux/rhashtable.h | 190 +++++++++++++++++++++++++------------- ipc/util.c | 1 lib/rhashtable.c | 118 ++++++++++++------------ net/bridge/br_fdb.c | 1 net/bridge/br_vlan.c | 1 net/bridge/br_vlan_tunnel.c | 1 net/ipv4/ipmr.c | 1 net/ipv6/ip6mr.c | 1 9 files changed, 184 insertions(+), 132 deletions(-)
include/linux/rhashtable-types.h +0 -2
@@ -48,7 +48,6 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,* @head_offset: Offset of rhash_head in struct to be hashed * @max_size: Maximum size while expanding * @min_size: Minimum size while shrinking -* @locks_mul: Number of bucket locks to allocate per cpu (default: 32) * @automatic_shrinking: Enable automatic shrinking of tables * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) * @obj_hashfn: Function to hash object
@@ -62,7 +61,6 @@ struct rhashtable_params {unsigned int max_size;u16 min_size;bool automatic_shrinking; -u8 locks_mul;rht_hashfn_t hashfn;rht_obj_hashfn_t obj_hashfn;rht_obj_cmpfn_t obj_cmpfn;
include/linux/rhashtable.h +123 -67
364 lines not on this page · raw message
ipc/util.c +0 -1
@@ -105,7 +105,6 @@ static const struct rhashtable_params ipc_kht_params = { .head_offset = offsetof(struct kern_ipc_perm, khtnode), .key_offset = offsetof(struct kern_ipc_perm, key), .key_len = FIELD_SIZEOF(struct kern_ipc_perm, key), - .locks_mul = 1, .automatic_shrinking = true, };
lib/rhashtable.c +61 -57
256 lines not on this page · raw message
net/bridge/br_fdb.c +0 -1
@@ -33,7 +33,6 @@ static const struct rhashtable_params br_fdb_rht_params = { .key_offset = offsetof(struct net_bridge_fdb_entry, key), .key_len = sizeof(struct net_bridge_fdb_key), .automatic_shrinking = true, - .locks_mul = 1, };static struct kmem_cache *br_fdb_cache__read_mostly ;
net/bridge/br_vlan.c +0 -1
@@ -21,7 +21,6 @@ static const struct rhashtable_params br_vlan_rht_params = { .key_offset = offsetof(struct net_bridge_vlan, vid), .key_len = sizeof(u16), .nelem_hint = 3, - .locks_mul = 1, .max_size = VLAN_N_VID, .obj_cmpfn = br_vlan_cmp, .automatic_shrinking = true,
net/bridge/br_vlan_tunnel.c +0 -1
@@ -34,7 +34,6 @@ static const struct rhashtable_params br_vlan_tunnel_rht_params = { .key_offset = offsetof(struct net_bridge_vlan, tinfo.tunnel_id), .key_len = sizeof(__be64), .nelem_hint = 3, - .locks_mul = 1, .obj_cmpfn = br_vlan_tunid_cmp, .automatic_shrinking = true, };
net/ipv4/ipmr.c +0 -1
@@ -371,7 +371,6 @@ static const struct rhashtable_params ipmr_rht_params = { .key_offset = offsetof(struct mfc_cache, cmparg), .key_len = sizeof(struct mfc_cache_cmp_arg), .nelem_hint = 3, - .locks_mul = 1, .obj_cmpfn = ipmr_hash_cmp, .automatic_shrinking = true, };
net/ipv6/ip6mr.c +0 -1
@@ -340,7 +340,6 @@ static const struct rhashtable_params ip6mr_rht_params = { .key_offset = offsetof(struct mfc6_cache, cmparg), .key_len = sizeof(struct mfc6_cache_cmp_arg), .nelem_hint = 3, - .locks_mul = 1, .obj_cmpfn = ip6mr_hash_cmp, .automatic_shrinking = true, };
Herbert Xu
maintainerRe: [PATCH 15/18] rhashtable: use bit_spin_locks to protect hash bucket.
Quoted 30 lines · NeilBrown
This patch changes rhashtables to use a
bit_spin_lock(BIT(1)) the bucket pointer to lock the hash chain for that bucket.The benefits of a bit
spin_lockare:
- no need to allocate a separate array of locks.
- no need to have a configuration option to guide the choice of the size of this array
- locking cost if often a single test-and-set in a cache line that will have to be loaded anyway. When inserting at, or removing from, the head of the chain, the unlock is free - writing the new address in the bucket head implicitly clears the lock bit.
- even when lockings costs 2 updates (lock and unlock), they are in a cacheline that needs to be read anyway.The cost of using a bit
spin_lockis a little bit of code complexity, which I think is quite manageable.Bit
spin_locksare sometimes inappropriate because they are not fair - if multiple CPUs repeatedly contend of the same lock, one CPU can easily be starved. This is not a credible situation with rhashtable. Multiple CPUs may want to repeatedly add or remove objects, but they will typically do so at different buckets, so they will attempt to acquire different locks.As we have more bit-locks than we previously had spinlocks (by at least a factor of two) we can expect slightly less contention to go with the slightly better cache behavior and reduced memory consumption.
Signed-off-by NeilBrown <[email protected]>
...
Quoted 10 lines
include/linux/rhashtable.h +6
-0@@ -74,6 +71,61 @@ struct bucket_table {struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; }; +/* +* We lock a bucket by setting BIT(1) in the pointer - this is always +* zero in real pointers and in the nulls marker. +* bit_spin_locks do not handle contention well, but the whole point +* of the hashtable design is to achieve minimum per-bucket contention. +* A nested hash table might not have a bucket pointer. In that case
include/linux/rhashtable.h struct bucket_table { · line 89
Quoted 10 lines
include/linux/rhashtable.h +10
-0+* we cannot get a lock. For remove and replace the bucket cannot be +* interesting and doesn't need locking. +* For insert we allocate the bucket if this is the last bucket_table, +* and then take the lock. +* Sometimes we unlock a bucket by writing a new pointer there. In that +* case we don't need to unlock, but we do need to reset state such as +* local_bh. For that we have rht_unlocked(). This doesn't include +* the memory barrier that bit_spin_unlock() provides, but rcu_assign_pointer() +* will have provided that. +*/
Yes the concept looks good to me. But I would like to hear from Eric/Dave as to whether this would be acceptable for existing network hash tables such as the ones in inet.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH 15/18] rhashtable: use bit_spin_locks to protect hash bucket.
Quoted 3 lines · Herbert Xu
Yes the concept looks good to me. But I would like to hear from Eric/Dave as to whether this would be acceptable for existing network hash tables such as the ones in inet.
What about lockdep support ?
Re: [PATCH 15/18] rhashtable: use bit_spin_locks to protect hash bucket.
Quoted 8 lines · Eric Dumazet
Quoted 3 lines · Herbert Xu
Yes the concept looks good to me. But I would like to hear from Eric/Dave as to whether this would be acceptable for existing network hash tables such as the ones in inet.
What about lockdep support ?
bitlocks don't have native lockdep support.
I would be fairly easy to add lockdep support to rht_{lock,unlock,unlocked} if you think it is worthwhile. It could only really help if a hash-function or cmp-function took a lock, but it is not a great cost so we may as well just do it. I'll try to have a patch in the next day or so.
Thanks,
NeilBrown
PATCH 1/1 rhashtables: add lockdep tracking to bucket bit-spin-locks.
Native bit_spin_locks are not tracked by lockdep.
The bit_spin_locks used for rhashtable buckets are local to the rhashtable implementation, so there is little opportunity for the sort of misuse that lockdep might detect. However locks are held while a hash function or compare function is called, and if one of these took a lock,
a misbehaviour is possible.
As it is quite easy to add lockdep support this unlikely possibility see to be enough justification.
So create a lockdep class for bucket bit_spin_lock as attach through a lockdep_map in each bucket_table.
With the 'nested' annotation in rhashtable_rehash_one(), lockdep correctly reports a possible problem as this lock it taken while another bucket lock (in another table) is held. This confirms that the added support works.
With the correct nested annotation in place, lockdep reports no problems.
--- include/linux/rhashtable.h | 40 +++++++++++++++++++++++++++------------- lib/rhashtable.c | 17 ++++++++++------- 2 files changed, 37 insertions(+), 20 deletions(-)
include/linux/rhashtable.h +27 -13
@@ -70,6 +70,8 @@ struct bucket_table {struct bucket_table __rcu *future_tbl; +struct lockdep_map dep_map; +struct rhash_head __rcu *buckets[]____cacheline_aligned_in_smp ; };
@@ -90,20 +92,32 @@ struct bucket_table {* will have provided that. */ -static inline void rht_lock (struct rhash_head **bucket) +static inline void rht_lock (struct bucket_table *tbl,struct rhash_head **bucket) +{ +local_bh_disable (); +bit_spin_lock (1 , (unsigned long *)bucket); +lock_map_acquire (&tbl->dep_map); +} + +static inline void rht_lock_nested (struct bucket_table *tbl, +struct rhash_head **bucket, +unsigned int subclass) {local_bh_disable ();bit_spin_lock (1 , (unsigned long *)bucket); +lock_acquire_exclusive (&tbl->dep_map, subclass,0 ,NULL , _THIS_IP_); } -static inline void rht_unlock (struct rhash_head **bucket) +static inline void rht_unlock (struct bucket_table *tbl,struct rhash_head **bucket) { +lock_map_release (&tbl->dep_map);bit_spin_unlock (1 , (unsigned long *)bucket);local_bh_enable (); } -static inline void rht_unlocked (void ) +static inline void rht_unlocked (struct bucket_table *tbl) { +lock_map_release (&tbl->dep_map);preempt_enable ();__release (bitlock);local_bh_enable ();
@@ -742,11 +756,11 @@ static inline void *__rhashtable_insert_fast(if (!headp) goto out; lock = pprev = headp; -rht_lock (lock); +rht_lock (tbl, lock);if (unlikely (rcu_access_pointer (tbl->future_tbl))) { slow_path: -rht_unlock (lock); +rht_unlock (tbl, lock);rcu_read_unlock ();return rhashtable_insert_slow (ht, key, obj); }
@@ -825,10 +839,10 @@ static inline void *__rhashtable_insert_fast(/* Assigning to *headp unlocked the chain, so we * don't need to do it again. */ -rht_unlocked (); +rht_unlocked (tbl); }else { out: -rht_unlock (lock); +rht_unlock (tbl, lock); }rcu_read_unlock ();
@@ -1033,7 +1047,7 @@ static inline int __rhashtable_remove_fast_one(if (!pprev)return -ENOENT ; lock = pprev; -rht_lock (lock); +rht_lock (tbl, lock);rht_for_each_from (he,rht_ptr (*pprev), tbl, hash) {struct rhlist_head *list;
@@ -1078,13 +1092,13 @@ static inline int __rhashtable_remove_fast_one(rcu_assign_pointer (*pprev, obj);if (lock == pprev) {/* That rcu_assign_pointer() unlocked the chain */ -rht_unlocked (); +rht_unlocked (tbl); goto unlocked; }break ; } -rht_unlock (lock); +rht_unlock (tbl, lock); unlocked:if (err >0 ) {if (params.percpu_count)
@@ -1195,7 +1209,7 @@ static inline int __rhashtable_replace_fast(return -ENOENT ; lock = pprev; -rht_lock (lock); +rht_lock (tbl, lock);rht_for_each_from (he,rht_ptr (*pprev), tbl, hash) {if (he != obj_old) {
@@ -1208,13 +1222,13 @@ static inline int __rhashtable_replace_fast( err =0 ;if (pprev == lock) {/* We just unlocked the chain by assigning to *pprev */ -rht_unlocked (); +rht_unlocked (tbl); goto unlocked; }break ; } -rht_unlock (lock); +rht_unlock (tbl, lock); unlocked:return err;
lib/rhashtable.c +10 -7
@@ -174,6 +174,7 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,struct bucket_table *tbl =NULL ;size_t size;int i; +static struct lock_class_key __key; size =sizeof (*tbl) + nbuckets *sizeof (tbl->buckets[0 ]);if (gfp !=GFP_KERNEL )
@@ -190,6 +191,8 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,if (tbl ==NULL )return NULL ; +lockdep_init_map (&tbl->dep_map,"rhashtable_bucket" , &__key,0 ); + tbl->size = size;INIT_LIST_HEAD (&tbl->walkers);
@@ -249,7 +252,7 @@ static int rhashtable_rehash_one(struct rhashtable *ht, inspos = &new_tbl->buckets[new_hash]; lock = inspos; -rht_lock (lock); +rht_lock_nested (new_tbl, lock,SINGLE_DEPTH_NESTING ); head =rht_ptr (rht_dereference_bucket (*inspos, new_tbl, new_hash));while (!rht_is_a_nulls (head) && head < entry) {
@@ -260,9 +263,9 @@ static int rhashtable_rehash_one(struct rhashtable *ht,rcu_assign_pointer (*inspos, entry);if (inspos != lock) -rht_unlock (lock); +rht_unlock (new_tbl, lock);else -rht_unlocked (); +rht_unlocked (new_tbl);/* Need to preserved the bit lock. */ if (rht_is_locked (*pprev))
@@ -283,14 +286,14 @@ static int rhashtable_rehash_chain(struct rhashtable *ht,if (!pprev)return 0 ; -rht_lock (pprev); +rht_lock (old_tbl, pprev);while (!(err =rhashtable_rehash_one (ht, pprev, old_hash))) ;if (err == -ENOENT ) err =0 ; -rht_unlock (pprev); +rht_unlock (old_tbl, pprev);return err; }
@@ -658,7 +661,7 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key, new_tbl =rht_dereference_rcu (tbl->future_tbl, ht); data =ERR_PTR (-EAGAIN ); }else { -rht_lock (pprev); +rht_lock (tbl, pprev); data =rhashtable_lookup_one (ht, pprev, tbl, hash, key, obj); new_tbl = rhashtable_insert_one(ht, pprev, tbl,
@@ -666,7 +669,7 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,if (PTR_ERR (new_tbl) != -EEXIST ) data =ERR_CAST (new_tbl); -rht_unlock (pprev); +rht_unlock (tbl, pprev); } }while (!IS_ERR_OR_NULL (new_tbl));
Signature · 1 line
2.14.0.rc0.dirty
Re: [PATCH 15a/18] rhashtables: add lockdep tracking to bucket bit-spin-locks.
Quoted 10 lines · NeilBrown
Native
bit_spin_locksare not tracked by lockdep.The
bit_spin_locksused for rhashtable buckets are local to the rhashtable implementation, so there is little opportunity for the sort of misuse that lockdep might detect. However locks are held while a hash function or compare function is called, and if one of these took a lock,
a misbehaviour is possible.
Quoted 2 lines
As it is quite easy to add lockdep support this unlikely possibility see to be enough justification.
nit: s/see/seems/
Quoted 12 lines
So create a lockdep class for bucket
bit_spin_lockas attach through alockdep_mapin eachbucket_table.With the 'nested' annotation in
rhashtable_rehash_one(), lockdep correctly reports a possible problem as this lock it taken while another bucket lock (in another table) is held. This confirms that the added support works.
With the correct nested annotation in place, lockdep reports no problems.Signed-off-by NeilBrown <[email protected]>
Re: [PATCH 15a/18] rhashtables: add lockdep tracking to bucket bit-spin-locks.
Quoted 11 lines · Simon Horman
Quoted 10 lines · NeilBrown
Native
bit_spin_locksare not tracked by lockdep.The
bit_spin_locksused for rhashtable buckets are local to the rhashtable implementation, so there is little opportunity for the sort of misuse that lockdep might detect. However locks are held while a hash function or compare function is called, and if one of these took a lock,
a misbehaviour is possible.
Quoted 5 lines
Quoted 2 lines
As it is quite easy to add lockdep support this unlikely possibility see to be enough justification.
nit: s/see/seems/
Thanks :-) I've made that change for when I formally submit.
Thanks,
NeilBrown
Quoted 12 lines
Quoted 12 lines
So create a lockdep class for bucket
bit_spin_lockas attach through alockdep_mapin eachbucket_table.With the 'nested' annotation in
rhashtable_rehash_one(), lockdep correctly reports a possible problem as this lock it taken while another bucket lock (in another table) is held. This confirms that the added support works.
With the correct nested annotation in place, lockdep reports no problems.Signed-off-by NeilBrown <[email protected]>
PATCH 16/18 rhashtable: allow percpu element counter
In reply to NeilBrownWhen an rhashtable is receiving a high rate of updates on multiple CPUs, the atomic update to the nelems counter can become a measurable contention point.
We can use a per-cpu counter instead of an atomic_t. This reduces the contention on updates, but reduces the precision for grow/shrink decisions. This results in a performance trade-off between updates and lookups. Updates should be faster due to reduced contention, lookups could be slower if a resize is delayed. This trade-off will only be appropriate on rhashtables which have a high proportion of updates compared to lookups.
A fast read of a percpu counter can have an error of as much as 32 times the number of CPUs. A more precise count can be extracted, but this takes longer. It seems sensible to use the fast read to schedule a rehash, and the slow read to choose the new size of the table. This has the drawback that if the fast read is sufficiently higher than the slow read, we might repeatedly schedule a rehash which never happens. To avoid this, we record the most recently noticed difference between the two reads and use this to adjust the estimate returned by the fast read. If the rehash thread determines that a rehash is not needed, it will update the recored error so that updaters will not schedule the thread again until there is substantially more growth.
When the fast-read shows that the table is over 100% full, we need to double check with a slow read at that point too, and update the recorded error to ensure we only resize when it is really appropriate, but don't keep performing slow tests to see exactly when it is appropriate.
The error we keep (in pnelems_error) is 1.5 time the absolute difference between percpu_counter_read_positive() and percpu_counter_sum_positive(). The extra 50% reduces the frequency with which we have to do the slow read, at a cost of allowing the occupancy rate to grow well above 1 (but usually less than 2).
This patch includes a change to include/linux/percpu_counter.h to make some functions accept a const pointer.
It also adds a module parameter to test_rhashtable.ko to allow testing of percpu counters.
--- include/linux/percpu_counter.h | 4 + include/linux/rhashtable-types.h | 8 ++ include/linux/rhashtable.h | 126 +++++++++++++++++++++++++++++++++----- lib/rhashtable.c | 70 +++++++++++++++++---- lib/test_rhashtable.c | 14 ++++ 5 files changed, 189 insertions(+), 33 deletions(-)
include/linux/percpu_counter.h +2 -2
@@ -66,7 +66,7 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc)return __percpu_counter_sum (fbc); } -static inline s64 percpu_counter_read (struct percpu_counter *fbc) +static inline s64 percpu_counter_read (const struct percpu_counter *fbc) {return fbc->count; }
@@ -76,7 +76,7 @@ static inline s64 percpu_counter_read(struct percpu_counter *fbc)* number for some counter which should never be negative. * */ -static inline s64 percpu_counter_read_positive (struct percpu_counter *fbc) +static inline s64 percpu_counter_read_positive (const struct percpu_counter *fbc) {s64 ret = fbc->count;
include/linux/rhashtable-types.h +8 -0
@@ -13,6 +13,7 @@#include <linux/compiler.h> #include <linux/mutex.h> #include <linux/workqueue.h> +#include <linux/percpu_counter.h> struct rhash_head {struct rhash_head __rcu *next;
@@ -49,6 +50,7 @@ typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,* @max_size: Maximum size while expanding * @min_size: Minimum size while shrinking * @automatic_shrinking: Enable automatic shrinking of tables +* @percpu_count: Use a per-cpu counter instead of atomic_t * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash) * @obj_hashfn: Function to hash object * @obj_cmpfn: Function to compare key with object
@@ -61,6 +63,7 @@ struct rhashtable_params {unsigned int max_size;u16 min_size;bool automatic_shrinking; +bool percpu_count;rht_hashfn_t hashfn;rht_obj_hashfn_t obj_hashfn;rht_obj_cmpfn_t obj_cmpfn;
@@ -77,6 +80,9 @@ struct rhashtable_params {* @mutex: Mutex to protect current/future table swapping * @lock: Spin lock to protect walker list * @nelems: Number of elements in table +* @pnelems: Alternate per-cpu nelems counter +* @pnelems_error: absolute difference between approximate and actual +* percpu count at last check: 1.5 * abs("read" - "sum"). */ struct rhashtable {struct bucket_table __rcu *tbl;
@@ -88,6 +94,8 @@ struct rhashtable {struct mutex mutex;spinlock_t lock;atomic_t nelems; +struct percpu_counter pnelems; +unsigned int pnelems_error; };/**
include/linux/rhashtable.h +109 -17
191 lines not on this page · raw message
lib/rhashtable.c +58 -12
132 lines not on this page · raw message
lib/test_rhashtable.c +12 -2
@@ -57,6 +57,10 @@ static bool enomem_retry = false;module_param (enomem_retry, bool,0 );MODULE_PARM_DESC (enomem_retry,"Retry insert even if -ENOMEM was returned (default: off)" ); +static bool percpu_count = false; +module_param (percpu_count, bool,0 ); +MODULE_PARM_DESC (percpu_count,"Use less-precise percpu counter for counting elements (default: off)" ); +struct test_obj_val {int id;int tid;
@@ -180,6 +184,7 @@ static void test_bucket_stats(struct rhashtable *ht, unsigned int entries)unsigned int err, total =0 , chain_len =0 ;struct rhashtable_iter hti;struct rhash_head *pos; +unsigned int nelems; err =rhashtable_walk_init (ht, &hti,GFP_KERNEL );if (err) {
@@ -206,10 +211,14 @@ static void test_bucket_stats(struct rhashtable *ht, unsigned int entries)rhashtable_walk_stop (&hti);rhashtable_walk_exit (&hti); +if (ht->p.percpu_count) + nelems =percpu_counter_sum (&ht->pnelems); +else + nelems =atomic_read (&ht->nelems);pr_info (" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n" , - total,atomic_read (&ht->nelems), entries, chain_len); + total, nelems, entries, chain_len); -if (total !=atomic_read (&ht->nelems) || total != entries) +if (total != nelems || total != entries)pr_warn ("Test failed: Total count mismatch ^^^" ); }
@@ -705,6 +714,7 @@ static int __init test_rht_init(void) test_rht_params.automatic_shrinking = shrinking; test_rht_params.max_size = max_size ? :roundup_pow_of_two (entries); test_rht_params.nelem_hint = size; + test_rht_params.percpu_count = percpu_count; objs =vzalloc ((test_rht_params.max_size +1 ) *sizeof (struct test_obj ));if (!objs)
PATCH 17/18 rhashtable: rename rht_for_each*continue as *from.
In reply to NeilBrownThe pattern set by list.h is that for_each..continue() iterators start at the next entry after the given one, while for_each..next() iterators start at the given entry.
The rht_for_each*continue() iterates are documented as though the start at the 'next' entry, but actually start at the given entry, and they are used expecting that behaviour.
So fix the documentation and change the names to *from for consistency with list.h
--- .clang-format | 8 ++++---- include/linux/rhashtable.h | 38 +++++++++++++++++++------------------- lib/rhashtable.c | 4 ++-- 3 files changed, 25 insertions(+), 25 deletions(-)
.clang-format +4 -4
@@ -332,14 +332,14 @@ ForEachMacros: - 'rhl_for_each_entry_rcu' - 'rhl_for_each_rcu' - 'rht_for_each' - - 'rht_for_each_continue' + - 'rht_for_each_from' - 'rht_for_each_entry' - - 'rht_for_each_entry_continue' + - 'rht_for_each_entry_from' - 'rht_for_each_entry_rcu' - - 'rht_for_each_entry_rcu_continue' + - 'rht_for_each_entry_rcu_from' - 'rht_for_each_entry_safe' - 'rht_for_each_rcu' - - 'rht_for_each_rcu_continue' + - 'rht_for_each_rcu_from' - '__rq_for_each_bio' - 'rq_for_each_segment' - 'scsi_for_each_prot_sg'
include/linux/rhashtable.h +19 -19
@@ -418,13 +418,13 @@ static inline struct rhash_head __rcu **rht_bucket_insert( }/** -* rht_for_each_continue - continue iterating over hash chain +* rht_for_each_from - iterate over hash chain from given head * @pos: the &struct rhash_head to use as a loop cursor. -* @head: the previous &struct rhash_head to continue from +* @head: the &struct rhash_head to start from * @tbl: the &struct bucket_table * @hash: the hash value / bucket index */ -#define rht_for_each_continue (pos, head, tbl, hash) \ +#define rht_for_each_from (pos, head, tbl, hash) \ for (pos = rht_dereference_bucket(head, tbl, hash); \ !rht_is_a_nulls(pos); \ pos = rht_dereference_bucket((pos)->next, tbl, hash))
@@ -436,18 +436,18 @@ static inline struct rhash_head __rcu **rht_bucket_insert(* @hash: the hash value / bucket index */ #define rht_for_each (pos, tbl, hash) \ - rht_for_each_continue(pos, rht_ptr(*rht_bucket(tbl, hash)), tbl, hash) + rht_for_each_from(pos, rht_ptr(*rht_bucket(tbl, hash)), tbl, hash)/** -* rht_for_each_entry_continue - continue iterating over hash chain +* rht_for_each_entry_from - iterate over hash chain from given head * @tpos: the type * to use as a loop cursor. * @pos: the &struct rhash_head to use as a loop cursor. -* @head: the previous &struct rhash_head to continue from +* @head: the &struct rhash_head to start from * @tbl: the &struct bucket_table * @hash: the hash value / bucket index * @member: name of the &struct rhash_head within the hashable struct. */ -#define rht_for_each_entry_continue (tpos, pos, head, tbl, hash, member) \ +#define rht_for_each_entry_from (tpos, pos, head, tbl, hash, member) \ for (pos = rht_dereference_bucket(head, tbl, hash); \ (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \ pos = rht_dereference_bucket((pos)->next, tbl, hash))
@@ -461,7 +461,7 @@ static inline struct rhash_head __rcu **rht_bucket_insert(* @member: name of the &struct rhash_head within the hashable struct. */ #define rht_for_each_entry (tpos, pos, tbl, hash, member) \ - rht_for_each_entry_continue(tpos, pos, rht_ptr(*rht_bucket(tbl, hash)), \ + rht_for_each_entry_from(tpos, pos, rht_ptr(*rht_bucket(tbl, hash)), \ tbl, hash, member)/**
@@ -487,9 +487,9 @@ static inline struct rhash_head __rcu **rht_bucket_insert( rht_dereference_bucket(pos->next, tbl, hash) : NULL)/** -* rht_for_each_rcu_continue - continue iterating over rcu hash chain +* rht_for_each_rcu_from - iterate over rcu hash chain from given head * @pos: the &struct rhash_head to use as a loop cursor. -* @head: the previous &struct rhash_head to continue from +* @head: the &struct rhash_head to start from * @tbl: the &struct bucket_table * @hash: the hash value / bucket index *
@@ -497,7 +497,7 @@ static inline struct rhash_head __rcu **rht_bucket_insert(* the _rcu mutation primitives such as rhashtable_insert() as long as the * traversal is guarded by rcu_read_lock(). */ -#define rht_for_each_rcu_continue (pos, head, tbl, hash) \ +#define rht_for_each_rcu_from (pos, head, tbl, hash) \ for (({barrier(); }), \ pos = rht_ptr(rht_dereference_bucket_rcu(head, tbl, hash)); \ !rht_is_a_nulls(pos); \
@@ -521,10 +521,10 @@ static inline struct rhash_head __rcu **rht_bucket_insert( pos = rcu_dereference_raw(pos->next))/** -* rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain +* rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head * @tpos: the type * to use as a loop cursor. * @pos: the &struct rhash_head to use as a loop cursor. -* @head: the previous &struct rhash_head to continue from +* @head: the &struct rhash_head to start from * @tbl: the &struct bucket_table * @hash: the hash value / bucket index * @member: name of the &struct rhash_head within the hashable struct.
@@ -533,7 +533,7 @@ static inline struct rhash_head __rcu **rht_bucket_insert(* the _rcu mutation primitives such as rhashtable_insert() as long as the * traversal is guarded by rcu_read_lock(). */ -#define rht_for_each_entry_rcu_continue (tpos, pos, head, tbl, hash, member) \ +#define rht_for_each_entry_rcu_from (tpos, pos, head, tbl, hash, member) \ for (({barrier(); }), \ pos = rht_dereference_bucket_rcu(head, tbl, hash); \ (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
@@ -552,7 +552,7 @@ static inline struct rhash_head __rcu **rht_bucket_insert(* traversal is guarded by rcu_read_lock(). */ #define rht_for_each_entry_rcu (tpos, pos, tbl, hash, member) \ - rht_for_each_entry_rcu_continue(tpos, pos, \ + rht_for_each_entry_rcu_from(tpos, pos, \ rht_ptr(*rht_bucket(tbl, hash)), \ tbl, hash, member)
@@ -609,7 +609,7 @@ static inline struct rhash_head *__rhashtable_lookup( hash =rht_key_hashfn (ht, tbl, key, params); head =rht_bucket (tbl, hash); do { - rht_for_each_rcu_continue(he, *head, tbl, hash) { + rht_for_each_rcu_from(he, *head, tbl, hash) { if (params.obj_cmpfn ? params.obj_cmpfn(&arg, rht_obj(ht, he)) : rhashtable_compare(&arg, rht_obj(ht, he)))
@@ -744,7 +744,7 @@ static inline void *__rhashtable_insert_fast( } -rht_for_each_continue (head,rht_ptr (*headp), tbl, hash) { +rht_for_each_from (head,rht_ptr (*headp), tbl, hash) {struct rhlist_head *plist;struct rhlist_head *list;
@@ -1027,7 +1027,7 @@ static inline int __rhashtable_remove_fast_one( lock = pprev;rht_lock (lock); -rht_for_each_continue (he,rht_ptr (*pprev), tbl, hash) { +rht_for_each_from (he,rht_ptr (*pprev), tbl, hash) {struct rhlist_head *list; list = container_of(he, struct rhlist_head, rhead);
@@ -1189,7 +1189,7 @@ static inline int __rhashtable_replace_fast( lock = pprev;rht_lock (lock); -rht_for_each_continue (he,rht_ptr (*pprev), tbl, hash) { +rht_for_each_from (he,rht_ptr (*pprev), tbl, hash) {if (he != obj_old) { pprev = &he->next;continue ;
lib/rhashtable.c +2 -2
@@ -232,7 +232,7 @@ static int rhashtable_rehash_one(struct rhashtable *ht, err = -ENOENT ; -rht_for_each_continue (entry,rht_ptr (*pprev), old_tbl, old_hash) { +rht_for_each_from (entry,rht_ptr (*pprev), old_tbl, old_hash) { err =0 ; next =rht_dereference_bucket (entry->next, old_tbl, old_hash);
@@ -527,7 +527,7 @@ static void *rhashtable_lookup_one(struct rhashtable *ht,int elasticity; elasticity =RHT_ELASTICITY ; -rht_for_each_continue (head,rht_ptr (*pprev), tbl, hash) { +rht_for_each_from (head,rht_ptr (*pprev), tbl, hash) {struct rhlist_head *list;struct rhlist_head *plist;
PATCH 18/18 rhashtable: add rhashtable_walk_delay_rehash()
In reply to NeilBrownThis function allows an rhashtable walk to complete without any restarts as long as it progresses reasonably quickly.
Any current rehash is waited for, then a counter is incremented to stop any further rehash from happening until the 100% occupancy mark is reached.
This particularly ensures that restarts won't happen due to the hash table shrinking as shrinking is delayed indefinitely.
--- include/linux/rhashtable-types.h | 3 ++ include/linux/rhashtable.h | 14 ++++++++-- lib/rhashtable.c | 54 +++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 4 deletions(-)
include/linux/rhashtable-types.h +3 -0
@@ -76,6 +76,7 @@ struct rhashtable_params {* @max_elems: Maximum number of elements in table * @p: Configuration parameters * @rhlist: True if this is an rhltable +* @rehash_delayers: number of walkers asking for rehash to be delayed. * @run_work: Deferred worker to expand/shrink asynchronously * @mutex: Mutex to protect current/future table swapping * @lock: Spin lock to protect walker list
@@ -90,6 +91,7 @@ struct rhashtable {unsigned int max_elems;struct rhashtable_params p;bool rhlist; +atomic_t rehash_delayers;struct work_struct run_work;struct mutex mutex;spinlock_t lock;
@@ -134,6 +136,7 @@ struct rhashtable_iter {unsigned int skip;bool p_is_unsafe;bool end_of_table; +bool delay_rehash; }; int rhashtable_init(struct rhashtable *ht,
include/linux/rhashtable.h +11 -3
@@ -233,7 +233,8 @@ static inline bool __rht_grow_above_75(const struct rhashtable *ht, unsigned int nelems) {return nelems > (tbl->size /4 *3 ) && - (!ht->p.max_size || tbl->size < ht->p.max_size); + (!ht->p.max_size || tbl->size < ht->p.max_size) && +atomic_read (&ht->rehash_delayers) ==0 ; }/** * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
@@ -263,7 +264,8 @@ static inline bool __rht_shrink_below_30(const struct rhashtable *ht, {/* Shrink table beneath 30% load */ return nelems < (tbl->size *3 /10 ) && - tbl->size > ht->p.min_size; + tbl->size > ht->p.min_size && +atomic_read (&ht->rehash_delayers) ==0 ; }/** * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
@@ -285,9 +287,14 @@ static inline bool rht_shrink_below_30(const struct rhashtable *ht,return __rht_shrink_below_30 (ht, tbl, nelems); } +/** +* rht_grow_above_100 - returns true if nelems > table-size +* @ht: hash table +* @tbl: current table +*/ static inline bool __rht_grow_above_100 (const struct rhashtable *ht,const struct bucket_table *tbl, -unsigned int nelems) +const unsigned int nelems) {return nelems > tbl->size && (!ht->p.max_size || tbl->size < ht->p.max_size);
@@ -357,6 +364,7 @@ void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,void rhashtable_walk_enter (struct rhashtable *ht,struct rhashtable_iter *iter);void rhashtable_walk_exit (struct rhashtable_iter *iter); +void rhashtable_walk_delay_rehash (struct rhashtable_iter *iter); int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);static inline void rhashtable_walk_start (struct rhashtable_iter *iter)
lib/rhashtable.c +53 -1
88 lines not on this page · raw message