From patchwork Tue Dec 15 11:20:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 556930 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id D21961402D6 for ; Tue, 15 Dec 2015 22:23:16 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=sXRF7k11; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965101AbbLOLV0 (ORCPT ); Tue, 15 Dec 2015 06:21:26 -0500 Received: from mail-qg0-f42.google.com ([209.85.192.42]:33617 "EHLO mail-qg0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964936AbbLOLVY (ORCPT ); Tue, 15 Dec 2015 06:21:24 -0500 Received: by mail-qg0-f42.google.com with SMTP id v16so3697207qge.0; Tue, 15 Dec 2015 03:21:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fouUEaCGfpnFpvVEBQ2HyeTmngAQMVXd8dO4MLBcajA=; b=sXRF7k11N06FsIfPgLLKHVqN9cOEXtQNLzcRXIwLfMCnlDyDqqm+oxKkh29iJCY5NU 5/MT7s+HoMJAXK483XUEERar5coJxVwxA0L+M/KKLJX5Eq5R56cQONvXqEyeYjetpFsI BUzxCgbTMEADuWuj/GayuZWggD8m83Z8pf43PzhUEc07y5avC7z3XyVoLfxHRiGmXMic k5BtYqamuBLEn3ScXOEZ8K+NKfn19XKgbX1puiYuNJHCglvoIWV+RwFc+n+jRp5v6aO4 GqFLLjMfNVSddnfDVpzHz1aG1myp0QiknPYdullbnNYtBWRybTtpgvDmCsxVWNp6pBO0 57LA== X-Received: by 10.140.180.20 with SMTP id b20mr25069015qha.49.1450178483917; Tue, 15 Dec 2015 03:21:23 -0800 (PST) Received: from localhost (56.34.213.162.lcy-01.canonistack.canonical.com. [162.213.34.56]) by smtp.gmail.com with ESMTPSA id l124sm271456qhc.24.2015.12.15.03.21.21 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 15 Dec 2015 03:21:22 -0800 (PST) From: Ming Lei To: linux-kernel@vger.kernel.org, Alexei Starovoitov Cc: "David S. Miller" , netdev@vger.kernel.org, Ming Lei Subject: [PATCH 1/6] bpf: hash: use atomic count Date: Tue, 15 Dec 2015 19:20:59 +0800 Message-Id: <1450178464-27721-2-git-send-email-tom.leiming@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1450178464-27721-1-git-send-email-tom.leiming@gmail.com> References: <1450178464-27721-1-git-send-email-tom.leiming@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Preparing for removing global per-hashtable lock, so the counter need to be defined as aotmic_t first. Signed-off-by: Ming Lei --- kernel/bpf/hashtab.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 34777b3..2615388 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -18,7 +18,7 @@ struct bpf_htab { struct bpf_map map; struct hlist_head *buckets; raw_spinlock_t lock; - u32 count; /* number of elements in this hashtable */ + atomic_t count; /* number of elements in this hashtable */ u32 n_buckets; /* number of hash buckets */ u32 elem_size; /* size of each element in bytes */ }; @@ -106,7 +106,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) INIT_HLIST_HEAD(&htab->buckets[i]); raw_spin_lock_init(&htab->lock); - htab->count = 0; + atomic_set(&htab->count, 0); return &htab->map; @@ -256,7 +256,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, l_old = lookup_elem_raw(head, l_new->hash, key, key_size); - if (!l_old && unlikely(htab->count >= map->max_entries)) { + if (!l_old && unlikely(atomic_read(&htab->count) >= map->max_entries)) { /* if elem with this 'key' doesn't exist and we've reached * max_entries limit, fail insertion of new elem */ @@ -284,7 +284,7 @@ static int htab_map_update_elem(struct bpf_map *map, void *key, void *value, hlist_del_rcu(&l_old->hash_node); kfree_rcu(l_old, rcu); } else { - htab->count++; + atomic_inc(&htab->count); } raw_spin_unlock_irqrestore(&htab->lock, flags); @@ -319,7 +319,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key) if (l) { hlist_del_rcu(&l->hash_node); - htab->count--; + atomic_dec(&htab->count); kfree_rcu(l, rcu); ret = 0; } @@ -339,7 +339,7 @@ static void delete_all_elements(struct bpf_htab *htab) hlist_for_each_entry_safe(l, n, head, hash_node) { hlist_del_rcu(&l->hash_node); - htab->count--; + atomic_dec(&htab->count); kfree(l); } }