During kernel module encoding, the hash table and LRU chain methods are often required for resource management.
You can add a knpostion identifier in the data structure of the resource node,
After both operations are completed, enable this bit to ensure operation consistency.
I wrote some simple code:
82 void resource_remove (resource_node_t * node)
83 {
84 ne_u32 hash_pos;
85 re_do_list * lru_list;
86
87 lru_list = per_cpu (lru_list_pcpu, node-> CPU );
88 nos_spin_lock_bh (& lru_list [node-> lru_idx]. Lock );
89 If (likely (node_alive = nos_atomic_read (& node-> knpostion ))){
90 nos_atomic_set (& node-> knpostion, node_dead );
91 ne_list_del (& node-> lru_list );
92 nos_spin_unlock_bh (& lru_list [node-> lru_idx]. Lock );
93 /*
94 * del from hash
95 */
96 hash_pos = resource_get_hash (node). hash_pos;
97 nos_spin_lock_bh (& (resource_hashtable + hash_pos)-> lock );
98 ne_list_del (& node-> list );
99 resource_put (node );
100 nos_spin_unlock_bh (& (resource_hashtable + hash_pos)-> lock );
101} else {
102 nos_spin_unlock_bh (& lru_list [node-> lru_idx]. Lock );
103}
104}
63 void resource_insert (resource_node_t * node)
64 {
65 ne_u32 hash_pos;
66 re_do_list * lru_list;
67 hash_pos = resource_get_hash (node). hash_pos;
68 nos_spin_lock_bh (& (resource_hashtable + hash_pos)-> lock );
69 ne_list_add_tail (& node-> list, & (resource_hashtable + hash_pos)-> list );
70 nos_spin_unlock_bh (& (resource_hashtable + hash_pos)-> lock );
71
72 node-> create_jiffies = jiffies;
73
74 lru_list = per_cpu (lru_list_pcpu, node-> CPU );
75 nos_spin_lock_bh (& lru_list [node-> lru_idx]. Lock );
76 ne_list_add_tail (& node-> lru_list, & lru_list [node-> lru_idx]. List );
77 nos_atomic_set (& node-> knpostion, node_alive );
78 resource_hold (node );
79 nos_spin_unlock_bh (& lru_list [node-> lru_idx]. Lock );
80}
Lock-free policies when operating the same resource in multiple ways