Re: [PATCH bpf] bpf: Do not grab the bucket spinlock by default on htab batch ops

From: Yonghong Song
Date: Tue Feb 18 2020 - 11:35:02 EST

On 2/18/20 7:56 AM, Daniel Borkmann wrote:
On 2/18/20 4:43 PM, Yonghong Song wrote:
On 2/14/20 2:43 PM, Brian Vazquez wrote:
Grabbing the spinlock for every bucket even if it's empty, was causing
significant perfomance cost when traversing htab maps that have only a
few entries. This patch addresses the issue by checking first the
bucket_cnt, if the bucket has some entries then we go and grab the
spinlock and proceed with the batching.

Tested with a htab of size 50K and different value of populated entries.

ÂÂ ---------------------------------------------
ÂÂ BM_DumpHashMap/1ÂÂÂÂÂÂ 2759655ÂÂÂÂÂÂÂ 2752033
ÂÂ BM_DumpHashMap/10ÂÂÂÂÂ 2933722ÂÂÂÂÂÂÂ 2930825
ÂÂ BM_DumpHashMap/200ÂÂÂÂ 3171680ÂÂÂÂÂÂÂ 3170265
ÂÂ BM_DumpHashMap/500ÂÂÂÂ 3639607ÂÂÂÂÂÂÂ 3635511
ÂÂ BM_DumpHashMap/1000ÂÂÂ 4369008ÂÂÂÂÂÂÂ 4364981
ÂÂ BM_DumpHashMap/5kÂÂÂÂ 11171919ÂÂÂÂÂÂ 11134028
ÂÂ BM_DumpHashMap/20kÂÂÂ 69150080ÂÂÂÂÂÂ 69033496
ÂÂ BM_DumpHashMap/39kÂÂ 190501036ÂÂÂÂÂ 190226162

ÂÂ ---------------------------------------------
ÂÂ BM_DumpHashMap/1ÂÂÂÂÂÂÂ 202707ÂÂÂÂÂÂÂÂ 200109
ÂÂ BM_DumpHashMap/10ÂÂÂÂÂÂ 213441ÂÂÂÂÂÂÂÂ 210569
ÂÂ BM_DumpHashMap/200ÂÂÂÂÂ 478641ÂÂÂÂÂÂÂÂ 472350
ÂÂ BM_DumpHashMap/500ÂÂÂÂÂ 980061ÂÂÂÂÂÂÂÂ 967102
ÂÂ BM_DumpHashMap/1000ÂÂÂ 1863835ÂÂÂÂÂÂÂ 1839575
ÂÂ BM_DumpHashMap/5kÂÂÂÂÂ 8961836ÂÂÂÂÂÂÂ 8902540
ÂÂ BM_DumpHashMap/20kÂÂÂ 69761497ÂÂÂÂÂÂ 69322756
ÂÂ BM_DumpHashMap/39kÂÂ 187437830ÂÂÂÂÂ 186551111

Fixes: 057996380a42 ("bpf: Add batch ops to all htab bpf map")
Cc: Yonghong Song <yhs@xxxxxx>
Signed-off-by: Brian Vazquez <brianvv@xxxxxxxxxx>

Acked-by: Yonghong Song <yhs@xxxxxx>

I must probably be missing something, but how is this safe? Presume we
traverse in the walk with bucket_cnt = 0. Meanwhile a different CPU added
entries to this bucket since not locked. Same reader on the other CPU with
bucket_cnt = 0 then starts to traverse the second
hlist_nulls_for_each_entry_safe() unlocked e.g. deleting entries?

Thanks for pointing this out. Yes, you are correct. If bucket_cnt is 0
and buck->lock is not held, we should skip the
hlist_nulls_for_each_entry_safe(l, n, head, hash_node) {
as another cpu may traverse the bucket in parallel by adding/deleting the elements.