Re: Issue with hash_for_each_possible() in Kernel Hashtable Implementation

Abdullah Al Sharji <[email protected]> Fri, 31 Jan 2025 05:35:53 +0400
Newsgroups gmane.linux.newbie
Message-ID <[email protected]>
Im not very sure what the error is related to, I tried a whole number of 
things, such as changing the arguments and their types. However, I 
really don't know what why I keep getting this error. I'd appreciate any 
direction.

On 1/31/25 05:31, Abdullah Al Sharji wrote:
> I'm developing a Linux kernel feature that tracks per-process network 
> bandwidth usage using a hashtable (DEFINE_HASHTABLE()). My function 
> logs network activity per PID using the struct sock structure and 
> associates it with incoming and outgoing packet statistics.
>
> The macro hash_for_each_possible has an error: expression must have a 
> constant valueC/C++(28)
>
> "pointer type mismatch in container_of()"); ((typeof(*(entry)) 
> *)(__mptr -__builtin_offsetof(typeof(*(entry)), hlist))); }) 
> :((void*)0); }))
>
>
> #include<linux/net_bandwidth.h>
> DEFINE_HASHTABLE(pid_bw_table, 8);
> DEFINE_SPINLOCK(pid_bw_lock);
> voidpid_bw_entry(structsock*sk, u64rx, u64tx)
> {
> if(!sk->sk_socket||!sk->sk_socket->file)
> return;
> structpid_bw_entry*entry;
> pid_tsk_pid=sk->sk_socket->file->f_owner.pid;
> spin_lock(&pid_bw_lock);
> hash_for_each_possible(pid_bw_table, entry, hlist, sk_pid) {
> if(entry->pid==sk_pid) {
> entry->rx_bytes+=rx;
> entry->tx_bytes+=tx;
> spin_unlock(&pid_bw_lock);
> return;
> }
> }
> entry=kmalloc(sizeof(structpid_bw_entry), GFP_KERNEL);
> if(!entry) {
> spin_unlock(&pid_bw_lock);
> return;
> }
> entry->pid=sk_pid;
> strncpy(entry->comm, current->comm, sizeof(entry->comm));
> entry->rx_bytes=rx;
> entry->tx_bytes=tx;
> hash_add(pid_bw_table, &entry->hlist, (unsignedlong)sk_pid);
> spin_unlock(&pid_bw_lock);
> }
> This is my header file:
> #ifndef_LINUX_NET_BANDWIDTH_H
> #define_LINUX_NET_BANDWIDTH_H
> #include<linux/types.h>
> #include<linux/spinlock.h>
> #include<linux/hashtable.h>
> #include<linux/slab.h>
> #include<linux/file.h>
> #include<linux/skbuff.h>
> #include<net/sock.h>
> structpid_bw_entry{
> pid_tpid;
> charcomm[16];
> u64rx_bytes;
> u64tx_bytes;
> structhlist_nodehlist;
> };
> externstructhlist_headpid_bw_table[1<<8];
> externspinlock_tpid_bw_lock;
> voidpid_bw_entry(structsock*sk, u64rx, u64tx);
> #endif/* _LINUX_NET_BANDWIDTH_H */