Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2198390pxa; Mon, 3 Aug 2020 09:49:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyirEbH+TR5khqTIk3nsfCk5lnuhXTLNjQgBp8bi5f1Lg8eqQq/72az8riMOi2rnta0QaAx X-Received: by 2002:a17:906:a3d9:: with SMTP id ca25mr17733806ejb.164.1596473339842; Mon, 03 Aug 2020 09:48:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596473339; cv=none; d=google.com; s=arc-20160816; b=ovFvVUlqhjMLwz9tBF6Co5b18z3yOqWPPp29J8MpD9jajiq64G6mf5XWy6ne4CV80k YsvGTtkMuueWaTGTdUXlU2Gaj+o7WnRu7MohyVDK7669n6pheKhZcYP5pBz8iiL1UWt4 Px1/I3t3DfB4hRi3UV6UjIp70LYJ9vCPxw2/b5mppT4ktau2EQM5eabSaLfulwxhTxTb Kg2ueAodZr/oC+b+OIeUPNZl7XRRMODdLzUIxxg91CXmk7lOzloNznc+elJpjaN0U1X6 axTBVxBUETDc7JwwGV/vpHqci1j5Nh41XrPJY9bwNYoffti0HW/JpZkrYzaBYfAfrZMy 2fOg== 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=jWq9xcPzW6zE3flBBT8XSejw7BvbFb4qcCI1YOHOcdw=; b=M0VtHHttcUkeahZqYYJrHW4clPmc0fz/iSuO5PzTq7NI6QtIdYeC5/vuqa86RlLCYt vdQIP+3CKLtELSobyeI8fhAqYbFtl77Ifpgd9mq5SDszQc4dPiQeYCcpa+F8ufO8nfUw zGOjU6WMk/kx0Bvp5/G+/vLZapHGdWDGKJ5sYkj54pouM2IgmPB2rpM4jswOHyuAL1wG JPYhnfFkcZJ1/FZaQtQfpl7USv/olXDvZwhg11n0I6+natvZhVYbOMzhUfT5H/XMM7S3 f9Vpj2GCvZ4MZnKlUnwbh6xebsJgIpAUx+9MDPWSmSfyDozykpDDjpMMPnobJTrSKefm Ex5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=fUmAv+zL; 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 dp1si16212526ejc.173.2020.08.03.09.48.37; Mon, 03 Aug 2020 09:48:59 -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=fUmAv+zL; 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 S1726942AbgHCQrE (ORCPT + 99 others); Mon, 3 Aug 2020 12:47:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727012AbgHCQrD (ORCPT ); Mon, 3 Aug 2020 12:47:03 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 547CCC06179E for ; Mon, 3 Aug 2020 09:47:03 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id l4so39268507ejd.13 for ; Mon, 03 Aug 2020 09:47:03 -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=jWq9xcPzW6zE3flBBT8XSejw7BvbFb4qcCI1YOHOcdw=; b=fUmAv+zLS1DA3w/OwtMY93vb/ZKmDkFHEvUEl2PqV1CTegCFfqEvHgGaU2UGLIoBmC l/foLmE8WU+DoaU+6MdB5meXgquG3eW+OpeycdMjWJwAeeIlQjFLwaBQ2ZFEg3aiJrPQ CmdW0HOFfAfEDA/P48OTzlt/GCUTrOKcMUODA= 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=jWq9xcPzW6zE3flBBT8XSejw7BvbFb4qcCI1YOHOcdw=; b=OGSglg+lZioN7KpVvoZT56S+tKn9bLFHXZF3w9W846QXSUG1R98espBV/83WDJNic2 Tt7vI2jhFJwfjPkYilE3zxye5NS/XioJ8hfspBa2e8WUl6wJ1DWpTvOr7l20UsPyXxmq FfOPJTzzS0qEmT3juWrTEFArPGSKVn+ZBoz53Sy3qUtclPNOS7gt7fiFNwpFGfaVEq9Z gVuJ8B4ULlDEU9dVSQMTDGvZPgk7KFmPlcqRUpSkwT+ihOu1JHdg6dkbiSMGDkzzzsAq +eaUOInVGRFqzAx40D/IekDNHoiGSxAJuQ2Nc4UvxVB4K9zciXxanIxx+TYuaADdl9Dq wJIQ== X-Gm-Message-State: AOAM5323KnzDYEK+J/JNuHjYOZL+avLaSi7YMFgZJ0xkgiE7Qo4OIlW8 jKlRnK5yleS4eE6k97aW4u5oTVdSB5HnJA== X-Received: by 2002:a17:906:fc26:: with SMTP id ov38mr16574597ejb.99.1596473221690; Mon, 03 Aug 2020 09:47:01 -0700 (PDT) Received: from kpsingh.zrh.corp.google.com ([81.6.44.51]) by smtp.gmail.com with ESMTPSA id j7sm16385654ejb.64.2020.08.03.09.47.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 09:47:00 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Paul Turner , Jann Horn , Florent Revest Subject: [PATCH bpf-next v8 2/7] bpf: Generalize caching for sk_storage. Date: Mon, 3 Aug 2020 18:46:50 +0200 Message-Id: <20200803164655.1924498-3-kpsingh@chromium.org> X-Mailer: git-send-email 2.28.0.163.g6104cc2f0b6-goog In-Reply-To: <20200803164655.1924498-1-kpsingh@chromium.org> References: <20200803164655.1924498-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." 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 a5cc218834ee..99dde7b74767 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]; @@ -517,16 +515,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 */ @@ -534,18 +532,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() */ @@ -597,7 +596,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 @@ -713,7 +712,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.163.g6104cc2f0b6-goog