Received: by 2002:a25:ca44:0:0:0:0:0 with SMTP id a65csp1963021ybg; Thu, 30 Jul 2020 07:12:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2V/y9Hj29dCi2xRj44v5Encs2FXEATVbNO5SWMlE9HceHzD8H6FINBIvloa9Mt1MzGLBr X-Received: by 2002:a17:906:2851:: with SMTP id s17mr2884000ejc.347.1596118322000; Thu, 30 Jul 2020 07:12:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596118321; cv=none; d=google.com; s=arc-20160816; b=JjNwzVombK4m9yHvW6aivr95himWCyZpu5rUtYZ9pYWac42zwxfSbZbSCojoUAYxkF HNMLvqlZr+SlGAol6zoqLcjqOhZTH/AbRiYQJYkmuU80zFsFWkPMY/3v5Q25V2ZSdCkt vDJTYADnRHvppbkT2MxXJr55/AMCPXZiYyDGBV3xFl+UGJBnzuYdVt1hqD5F4CaQh8/P pQTdNl4WuhlWtrw17VROsh5+pUsyVC9IVpqddth+qWbe2lUnzO9TEcmXEinQj/JxrbmH n1mu3VtD4atXQGqfCFv9kBblJ4M4pk8WP4Mv62J5V4Avn6MHRNEEyDT03u+g7e9XWMog jekA== 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=wg0kUSEGrXJMBnS7RgbBCsvy47iqb2tGJq2KYVpzp4k=; b=xvtgBHl5Na01b9b2WjEfMPU8Ql4hOUu/1tfzGI+6qaZFXRbmflMpole8B7gzfmbrQ6 aN8RhhIMSCteI5F3REiLv5MeLQ3F7JJB3JsXNe6XrQKEQdiHoVvIA1wAT8XDmXskOzfk qAw79dYpUyPHbtM3xggX38AMSmd8A4j/U/QR+bMFc96iLalABJXum8UwoYR4Ti5ugOP+ uAn5N/OHzicoUW41drclUU55ZDpjTABcXLZTnWgT2jIZ1amm/IzM9AaMhtjiy8k4hIOP verRQYReRMYele2JiaJCfRv8D/rJnOQeYB4qHXAGAtbrbRSNmO5mgKPE5cBrD+jz6RI3 Ystg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jvJv6yv6; 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 oy7si3216092ejb.534.2020.07.30.07.11.36; Thu, 30 Jul 2020 07:12:01 -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=jvJv6yv6; 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 S1729599AbgG3OIP (ORCPT + 99 others); Thu, 30 Jul 2020 10:08:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729299AbgG3OH0 (ORCPT ); Thu, 30 Jul 2020 10:07:26 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C96D1C06179E for ; Thu, 30 Jul 2020 07:07:25 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id q76so5128125wme.4 for ; Thu, 30 Jul 2020 07:07:25 -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=wg0kUSEGrXJMBnS7RgbBCsvy47iqb2tGJq2KYVpzp4k=; b=jvJv6yv6xw2Jt9XFRvVkb3uVw/3MnlSFZoSU57KuYryVkW5Qt16DX5ASNRWVB1imJd LCuiiB4KD795O/OGiWAohtAJgpioTiLJpJv2SBRIz0XpaOEPNrorUeMMAGTUQD1zDT8E ftT4KTIUlr287/LdTtbJh78IKSbyJb4hm4laI= 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=wg0kUSEGrXJMBnS7RgbBCsvy47iqb2tGJq2KYVpzp4k=; b=qTlh+zeOYBZEdbg63nYgJOG2/WqDXPPgGZJb5JQjx1g5cUwFZauxcKOVIHS+rzSu6z a9KoxbNQU9lzDW8Pds2CFL/CbeqP/vNGybgo52CslpwDEHQTEK5D+N4T9snlZjpkI+8d 7P2dOWo+IX7RBGpsFVhDh4zLgWEAoEmiLyffV7WO9X0+aTgVyS3uWdzxC0UkdKrphu+E 9ZDoiAkUoNaMaMhIJEJxnCUqvgXCGHJd8qi0ih9KEVwzVGzfzfNjwOVkz5VGgprYDzL+ zrgCKRTV6wJlAorMLHyvcCsrHTO+5Nh40ysOw6YIrFGJplKdN6wuFOzgBGms7tfekucU 1W8w== X-Gm-Message-State: AOAM532ttW8NIw7fRMZP21EpCNYOpNu98E4Qg9ZUjNNN4eJJZ7ukyR1z ywx8CDK1Witto/6obGtKFT2fScKhevrNQw== X-Received: by 2002:a1c:5f41:: with SMTP id t62mr14230359wmb.134.1596118044164; Thu, 30 Jul 2020 07:07:24 -0700 (PDT) Received: from kpsingh.zrh.corp.google.com ([81.6.44.51]) by smtp.gmail.com with ESMTPSA id a10sm19088599wmd.3.2020.07.30.07.07.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2020 07:07:23 -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 v7 2/7] bpf: Generalize caching for sk_storage. Date: Thu, 30 Jul 2020 16:07:11 +0200 Message-Id: <20200730140716.404558-3-kpsingh@chromium.org> X-Mailer: git-send-email 2.28.0.rc0.142.g3c755180ce-goog In-Reply-To: <20200730140716.404558-1-kpsingh@chromium.org> References: <20200730140716.404558-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.rc0.142.g3c755180ce-goog