Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2252473ybv; Fri, 14 Feb 2020 14:43:45 -0800 (PST) X-Google-Smtp-Source: APXvYqyqwajx65ENk9utCeLQay5ORJTPwjBd7vA2Ngig9r5RSbb+DFg42HOdr2r3czpD/qPjoa3S X-Received: by 2002:a9d:7f83:: with SMTP id t3mr4100776otp.63.1581720225235; Fri, 14 Feb 2020 14:43:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581720225; cv=none; d=google.com; s=arc-20160816; b=ElVs2eqbkS8+TfQgqY0399DaNhhXcDMhQq0Jug9UDzH5zRMLJdzXngx/Bvufb5ffcY cKn7CmVpiz1ACPg5LtN3yyS7J7H9MuvIwB61RLCmQs6fchZq6sQM8onoUT/IarifIPbJ vYr3EQeZ/QaFpAy5fzFhGpPgmIrXqKMNJEVQ5Mktq9FdSPJbQrNwO5evxzVB4QSKDum7 3JnDFoVijxg1NemRfdtkGLBDF2oGAyP0bgXGbGRo7jEJpgA+bnsTE3AlPUXspdgtDzgL Ekatvz19BLj8pGPrXibYXVyeEXUIg4wLurD4bPLcA6R5qPp2xB/7jP51FQkIJQ7wCzY5 Ye9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=uHN7vdUXj9zg5j6BFSm9FeBemrt4Oks3aUAhhkdikgA=; b=c+jPK0Ewax09NzpytC54hKc+Br7LkwYnW0GcODQQCGt3zj6U6bPa9UfDp1gmVUzx7N xysg1AIq6M9gsNpvrpA1apdIFZClnZAyiS3PBbEsA8qo/cJSrhdXug2O5qu0yc4pcPC7 rjA/2XRjOudfpu8M9H5jOIKiwvhaCqWD8LAuSf6yMTpPphcYfHhJ658OiDumq7fPnmkQ UMiKqQ0Wtck9HA2B8BUtgHtatolkdLlVWkEEw47Rq8lm0AM/PwOIpSk1EHld9t3s3E9s 4Z+N2QMYOUHvXeS9QcRrGAZ1j77UNpsTtLmqpnsBoSJ3Qi5G7d/BjvPMmrhFPqHT94Xi 9UPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IeXW9Ek8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10si3494977otk.195.2020.02.14.14.43.32; Fri, 14 Feb 2020 14:43:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IeXW9Ek8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727794AbgBNWnP (ORCPT + 99 others); Fri, 14 Feb 2020 17:43:15 -0500 Received: from mail-pg1-f201.google.com ([209.85.215.201]:55780 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727566AbgBNWnP (ORCPT ); Fri, 14 Feb 2020 17:43:15 -0500 Received: by mail-pg1-f201.google.com with SMTP id v30so6929108pga.22 for ; Fri, 14 Feb 2020 14:43:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=uHN7vdUXj9zg5j6BFSm9FeBemrt4Oks3aUAhhkdikgA=; b=IeXW9Ek8IIWzbRkroTSuaubhFbW+FB8nYXCtlIy26K7MVDvhCdpSfLETg8Si9wMve2 UiHxMDhpjR9Dl0QaCK8Ds1kdIqYaTlWqA+xmfPOpbrCMXauCY5Y+ai7GExkl/HfSa311 KOX/CueF5MxQbOZroE0U02KBQDrTdv15TAd1Cm8PjniDcK3eEOBwOAQzYCvT4NDKSpR3 1eH+b20qtF0OAJKVSaBsZzr3MynnY33jR1dntOVJntwWWxuPUHmeL01TxDRViqbSGhcV HE1vNcxhg8Ou9tkWYURTl9jJ9ebB3ZbnuSa6UJMll1dgr5TlVS7BNVkDGlpGTQR+l1cq ndAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=uHN7vdUXj9zg5j6BFSm9FeBemrt4Oks3aUAhhkdikgA=; b=jHeqMhQYG0SMTzipbI2LLI/QhFRjmV+HImnOB0ckdVs81Gn3fX1h+H+J0o1MoMQhMp mOvU9AS/eeOcm6NShRWKHeCUAe3sxbTa8Bq7EE87wZ0NgjDqXn24YAF6UuH43zOCpyPH MoRIR9dvVUezeK1fPmqIwC/DzygvUvqW3HSaueXpwx8XJOtL85Lkcpw6TWI+uGeUxRSn sKqz8oj7mqMR4WfP6sdxGw/ZEmomCV5vctjW+nA9TxpQ6GohQlwbnaaGBJoEWBLjx93D HwVkUyI7Zf902q9hEZ7sAtyjnlu2/da6cPFVxF2gCgnygEVJO4uaL0hY/SjHmEI8FvuS 7+0A== X-Gm-Message-State: APjAAAXIW9CsukY0Gb1jjljHHRjo7p11OrgTkQdfdHTfn+mU3UL645m3 ALFybCDibOYqqfx155jl86RquaLOgGJ2 X-Received: by 2002:a63:2254:: with SMTP id t20mr5909022pgm.423.1581720194053; Fri, 14 Feb 2020 14:43:14 -0800 (PST) Date: Fri, 14 Feb 2020 14:43:02 -0800 Message-Id: <20200214224302.229920-1-brianvv@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH bpf] bpf: Do not grab the bucket spinlock by default on htab batch ops From: Brian Vazquez To: Brian Vazquez , Brian Vazquez , Alexei Starovoitov , Daniel Borkmann , "David S . Miller" Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Yonghong Song Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Before: Benchmark Time(ns) CPU(ns) --------------------------------------------- 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 After: Benchmark Time(ns) CPU(ns) --------------------------------------------- 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 Signed-off-by: Brian Vazquez --- kernel/bpf/hashtab.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 2d182c4ee9d99..fdbde28b0fe06 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1260,6 +1260,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, struct hlist_nulls_head *head; struct hlist_nulls_node *n; unsigned long flags; + bool locked = false; struct htab_elem *l; struct bucket *b; int ret = 0; @@ -1319,15 +1320,25 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, dst_val = values; b = &htab->buckets[batch]; head = &b->head; - raw_spin_lock_irqsave(&b->lock, flags); + /* do not grab the lock unless need it (bucket_cnt > 0). */ + if (locked) + raw_spin_lock_irqsave(&b->lock, flags); bucket_cnt = 0; hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) bucket_cnt++; + if (bucket_cnt && !locked) { + locked = true; + goto again_nocopy; + } + if (bucket_cnt > (max_count - total)) { if (total == 0) ret = -ENOSPC; + /* Note that since bucket_cnt > 0 here, it is implicit + * that the locked was grabbed, so release it. + */ raw_spin_unlock_irqrestore(&b->lock, flags); rcu_read_unlock(); this_cpu_dec(bpf_prog_active); @@ -1337,6 +1348,9 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, if (bucket_cnt > bucket_size) { bucket_size = bucket_cnt; + /* Note that since bucket_cnt > 0 here, it is implicit + * that the locked was grabbed, so release it. + */ raw_spin_unlock_irqrestore(&b->lock, flags); rcu_read_unlock(); this_cpu_dec(bpf_prog_active); @@ -1379,7 +1393,10 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, dst_val += value_size; } - raw_spin_unlock_irqrestore(&b->lock, flags); + if (locked) { + raw_spin_unlock_irqrestore(&b->lock, flags); + locked = false; + } /* If we are not copying data, we can go to next bucket and avoid * unlocking the rcu. */ -- 2.25.0.265.gbab2e86ba0-goog