Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1626970pxa; Sun, 23 Aug 2020 10:03:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9Ulgy/zvm1nmIc4hGOUXy3T27hkZru/FtoUi0T6yL4Y/rq6dtExFfGHG7ANByWBDYz5S+ X-Received: by 2002:a17:906:2a04:: with SMTP id j4mr2267263eje.440.1598202190333; Sun, 23 Aug 2020 10:03:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598202190; cv=none; d=google.com; s=arc-20160816; b=EKTkDk6a+fAx5aQcBbn6xWtP9EpHfVQ3EpQoCXqycrSbPm7Oq9xg2rsKpBU12kZT7S uerufc1QCd3Aog1Qa8MlfYOQ62LxegBDi+ZsO9LS4zh7C/mBNReh/BvkOWwfrEhEOBuK vJRaVgf6aukr1xaFczekzWI3x7lK588S1w9G2Ed2LJG4j1qE2thkhyS39p3QtJWixA4/ i/yBEVl+4tcBm029lCZwdeTtgeV7YDnnNbb7RHMHtWk9GkBoOZcebsDzSO5hiwYi+osx Z6btkn9KzWUuOHW8qxmSQ72J8tfvzp22/C6qcrp/EYv6kW+M94hAUofHFFrpQGcbwHxa ESEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7EGajeDiJIxrlPr3Y0NRvAuPtl3AEsFbIm2NTrH6ZEc=; b=e8cYbk6ANwl6mXQUdL2FyfMlcfrXzFzmIZ2I0gSOYcUIc6oGgfr2XjEr6nfHzM2Dpx PHgZK0u/24XRqn0xfl27uBX6yzUFoBVRnmFAbdKa6SbApJNWv/n7aLhp1bHtMquXyJ85 OMu3qKo+l433drWsfzn9tWJlWyJJ6bXK8cpEC/dWo38jPm2ZtwXP6+CmUGZ6pE3vMexq KKQdI/at2wbnGcU0VyCJef0YjXFQDa7wD91n11OmLROcYQkxQOfHlotzylKYNjjv5lFy U4Oq7MMwc6B/bDwzWo42BydEpOr0Y/RQBClCGSiYjlkZdZg9JTmzqyQU1Dzl8DjQfMRl qPSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CwhyfZh5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si1059209ejg.26.2020.08.23.10.02.47; Sun, 23 Aug 2020 10:03:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=CwhyfZh5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726502AbgHWQ54 (ORCPT + 99 others); Sun, 23 Aug 2020 12:57:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727851AbgHWQ4W (ORCPT ); Sun, 23 Aug 2020 12:56:22 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 097F5C061795 for ; Sun, 23 Aug 2020 09:56:20 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id c19so4609217wmd.1 for ; Sun, 23 Aug 2020 09:56:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7EGajeDiJIxrlPr3Y0NRvAuPtl3AEsFbIm2NTrH6ZEc=; b=CwhyfZh5+VOoAaJmKYhren9Fal42PrPyNC7OVekZ4j0KJizvXdmylkSyryY0Oj3Tx7 hXFUiir7tLOxUSsQEQQUpc0kAaT/t8VQizZlo5lTv7UoKwMkw82Zk3/1w3zRXjb63eiN Axl3Gczb/oVreJdGL999ylrFbqrmg6Q7h67e4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7EGajeDiJIxrlPr3Y0NRvAuPtl3AEsFbIm2NTrH6ZEc=; b=jbhYS2PZ54bGPk1mcsFMBwyFhR4ASMVP4D5bE1jy9p8SG6VqPOaWziTtFqKpGUEoqS qk/he0KUEl6FbL9K7XW5uxm1xav4A4Ug2CcmobxZ3dbYJFPdIOAfItRQ42Q2VI9s0cht 7y4dEOopTFTrv63fzewQsgddzdGVQ8Gd5tU8iQpU13Tu376JTxm90iJfs2XJ/ftIKo3A ID9rtMlPYhv5Rn6IwJqYqw+IvEulk1CBeVhHUeSaFAfQKAJ9pqXqkrgoiFDX8gjOdYBj kLgXALuoF0BnlYVDMLSAgstxmPWok933HN+RVJ/XSNKV30OIUTlrehw1GyV3AlhK08AX 5csA== X-Gm-Message-State: AOAM530VD2jpFCp6VfiTV4uVCrRGEo6MXnPxYoYfOXjQSi9vUeBXmlBc wyL9X5dd42V7Jebpa37YJF5IdZK65mokYQ== X-Received: by 2002:a7b:c452:: with SMTP id l18mr2178956wmi.13.1598201778936; Sun, 23 Aug 2020 09:56:18 -0700 (PDT) Received: from kpsingh.zrh.corp.google.com ([81.6.44.51]) by smtp.gmail.com with ESMTPSA id d10sm5425974wrg.3.2020.08.23.09.56.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Aug 2020 09:56:18 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Paul Turner , Jann Horn , Florent Revest Subject: [PATCH bpf-next v9 2/7] bpf: Generalize caching for sk_storage. Date: Sun, 23 Aug 2020 18:56:07 +0200 Message-Id: <20200823165612.404892-3-kpsingh@chromium.org> X-Mailer: git-send-email 2.28.0.297.g1956fa8f8d-goog In-Reply-To: <20200823165612.404892-1-kpsingh@chromium.org> References: <20200823165612.404892-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh Provide the a ability to define local storage caches on a per-object type basis. The caches and caching indices for different objects should not be inter-mixed as suggested in: https://lore.kernel.org/bpf/20200630193441.kdwnkestulg5erii@kafai-mbp.dhcp.thefacebook.com/ "Caching a sk-storage at idx=0 of a sk should not stop an inode-storage to be cached at the same idx of a inode." Acked-by: Martin KaFai Lau Signed-off-by: KP Singh --- include/net/bpf_sk_storage.h | 19 +++++++++++++++++++ net/core/bpf_sk_storage.c | 31 +++++++++++++++---------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h index 5036c94c0503..950c5aaba15e 100644 --- a/include/net/bpf_sk_storage.h +++ b/include/net/bpf_sk_storage.h @@ -3,6 +3,9 @@ #ifndef _BPF_SK_STORAGE_H #define _BPF_SK_STORAGE_H +#include +#include + struct sock; void bpf_sk_storage_free(struct sock *sk); @@ -15,6 +18,22 @@ struct sk_buff; struct nlattr; struct sock; +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_cache { + spinlock_t idx_lock; + u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE]; +}; + +#define DEFINE_BPF_STORAGE_CACHE(name) \ +static struct bpf_local_storage_cache name = { \ + .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \ +} + +u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache); +void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache, + u16 idx); + #ifdef CONFIG_BPF_SYSCALL int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk); struct bpf_sk_storage_diag * diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index f975e2d01207..ec61ee7c7ee4 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -14,6 +14,8 @@ #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) +DEFINE_BPF_STORAGE_CACHE(sk_cache); + struct bpf_local_storage_map_bucket { struct hlist_head list; raw_spinlock_t lock; @@ -78,10 +80,6 @@ struct bpf_local_storage_elem { #define SELEM(_SDATA) \ container_of((_SDATA), struct bpf_local_storage_elem, sdata) #define SDATA(_SELEM) (&(_SELEM)->sdata) -#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 - -static DEFINE_SPINLOCK(cache_idx_lock); -static u64 cache_idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE]; struct bpf_local_storage { struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE]; @@ -521,16 +519,16 @@ static int sk_storage_delete(struct sock *sk, struct bpf_map *map) return 0; } -static u16 cache_idx_get(void) +u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache) { u64 min_usage = U64_MAX; u16 i, res = 0; - spin_lock(&cache_idx_lock); + spin_lock(&cache->idx_lock); for (i = 0; i < BPF_LOCAL_STORAGE_CACHE_SIZE; i++) { - if (cache_idx_usage_counts[i] < min_usage) { - min_usage = cache_idx_usage_counts[i]; + if (cache->idx_usage_counts[i] < min_usage) { + min_usage = cache->idx_usage_counts[i]; res = i; /* Found a free cache_idx */ @@ -538,18 +536,19 @@ static u16 cache_idx_get(void) break; } } - cache_idx_usage_counts[res]++; + cache->idx_usage_counts[res]++; - spin_unlock(&cache_idx_lock); + spin_unlock(&cache->idx_lock); return res; } -static void cache_idx_free(u16 idx) +void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache, + u16 idx) { - spin_lock(&cache_idx_lock); - cache_idx_usage_counts[idx]--; - spin_unlock(&cache_idx_lock); + spin_lock(&cache->idx_lock); + cache->idx_usage_counts[idx]--; + spin_unlock(&cache->idx_lock); } /* Called by __sk_destruct() & bpf_sk_storage_clone() */ @@ -601,7 +600,7 @@ static void bpf_local_storage_map_free(struct bpf_map *map) smap = (struct bpf_local_storage_map *)map; - cache_idx_free(smap->cache_idx); + bpf_local_storage_cache_idx_free(&sk_cache, smap->cache_idx); /* Note that this map might be concurrently cloned from * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone @@ -718,7 +717,7 @@ static struct bpf_map *bpf_local_storage_map_alloc(union bpf_attr *attr) smap->elem_size = sizeof(struct bpf_local_storage_elem) + attr->value_size; - smap->cache_idx = cache_idx_get(); + smap->cache_idx = bpf_local_storage_cache_idx_get(&sk_cache); return &smap->map; } -- 2.28.0.297.g1956fa8f8d-goog