From patchwork Fri Sep 2 21:10:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12964680 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27C00ECAAA1 for ; Fri, 2 Sep 2022 21:11:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B73EC8012F; Fri, 2 Sep 2022 17:11:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFBFD80120; Fri, 2 Sep 2022 17:11:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 928FB8012F; Fri, 2 Sep 2022 17:11:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 7CCC380120 for ; Fri, 2 Sep 2022 17:11:10 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5778E1605EF for ; Fri, 2 Sep 2022 21:11:10 +0000 (UTC) X-FDA: 79868390700.02.7E80601 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf02.hostedemail.com (Postfix) with ESMTP id E9C3E80068 for ; Fri, 2 Sep 2022 21:11:09 +0000 (UTC) Received: by mail-pl1-f177.google.com with SMTP id jm11so2970884plb.13 for ; Fri, 02 Sep 2022 14:11:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Ga0Nwt5Zi5Wm2Kkt3SbRcQrpwa6IAGB0M+wlcmjeY9w=; b=LT2FmVcwiQsltTeEp0hTE44aqRgd79vDL/FHer/61JBuCVzyYkfJwZf0D7Yc/BnbGN AxxHbrL4rhdcmr0YrHsDZDGgeYLpN92GHZa5Z8Hz2VPhg4yri592fHoEIWW0HvsuX6cc ayFKfJZDDSnB9CHQAxXQf8Wt55+nJgowZEIMu0aaym1TyZ9rBPLiN8fAZ6kqCorFtAiH QRngBbH5C+RlnH+wmUmESGzMFkkXQTxx5cY6s3ssUcemkDerW/OoCoaRVmCEUCPqeUOO SmcuJ1gtVens6ZxWYvP2JOJcnHxa3OMGyi3OukHYiMBs+E14gJTcJsI9g8GIWTRjC5fd cBlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Ga0Nwt5Zi5Wm2Kkt3SbRcQrpwa6IAGB0M+wlcmjeY9w=; b=PcGElxlLCQhtcbKH3FvjMSHrkHL4zkn6YwvtOFKkeRiIrTEw7+YOfjLL/136xdJw19 cF9Kfi+mrpA3bAktFSjmmZqyH7Bq/+hkQE/xLy0q7cJJ3A1RGmucid3mGokRulXrOM8Z QHwjAURZ7k/K9UId2b+9/YkzZjYxe3hE9k5k0IYyjDWnXJ/u1MxxtLBsNF4MLTNcrPD+ u3HoFxtP42BhUP3C5LRvxv4tvJ3EpLWzcPUgsQ8AA9IvYwDtjvSQ7TlvX37GjET7oErQ YyPtoiG+0Tzpr8SCR16FcKRRBFmAvyrOrk8xJPA2EpuAZe6mdkjNP8qXjIhwc5oZtCm7 sWZQ== X-Gm-Message-State: ACgBeo23nEyYJYLfgfLnrq2neDClIRFZ21PcerRVHk1DWYbfgcaGIvo0 vL3BXgAQvnmhZwGSuhs2k4Q= X-Google-Smtp-Source: AA6agR4leFQrfvhSUXDaX2SlYFzlzN6r/IBW2pDg7LziTKf/ZUbDHT0ZBiUqih6w333ieveTbAth4g== X-Received: by 2002:a17:902:e5c3:b0:175:534:1735 with SMTP id u3-20020a170902e5c300b0017505341735mr21332665plf.87.1662153069480; Fri, 02 Sep 2022 14:11:09 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::c978]) by smtp.gmail.com with ESMTPSA id v8-20020a17090a520800b001fda0505eaasm1945582pjh.1.2022.09.02.14.11.08 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 02 Sep 2022 14:11:09 -0700 (PDT) From: Alexei Starovoitov To: davem@davemloft.net Cc: daniel@iogearbox.net, andrii@kernel.org, tj@kernel.org, memxor@gmail.com, delyank@fb.com, linux-mm@kvack.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v6 bpf-next 02/16] bpf: Convert hash map to bpf_mem_alloc. Date: Fri, 2 Sep 2022 14:10:44 -0700 Message-Id: <20220902211058.60789-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220902211058.60789-1-alexei.starovoitov@gmail.com> References: <20220902211058.60789-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662153070; a=rsa-sha256; cv=none; b=qMeNq1QvwrhGPoEJ0gYtKbLZDypwmcfSob6Cm7E40a+4kW4L6VvVo9YZ/2Yew0DmW5JHGG Hzi9DZV4iUxYWHhpW2LenA8xQhxVeFW0+uGJEBiNItXKYU/pX7jWZY0iq2h3MfJpcwbtkS r2VFCyh4o9AWHYf+myZX2RNVQ91zc9M= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=LT2FmVcw; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662153069; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ga0Nwt5Zi5Wm2Kkt3SbRcQrpwa6IAGB0M+wlcmjeY9w=; b=AN83Ig5LxBy04gnveuFF4LdLQ6YEmCSZlHiKKpap1exgEVi3Bnl7OEm26nEpzTqgrVZegk D3A/jT8CSV8AoGnuhL8+Sfelkt8WZD1dxiqO+tgY0fFcGbe7cbp5HLqPd8Hh2NomMf1EYn dQ+coo6xTXFtRQs7AIF3OxUMqh8UImQ= X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E9C3E80068 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=LT2FmVcw; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspam-User: X-Stat-Signature: m94ca1rxdbtc8atxfdxshm7acngm1rwg X-HE-Tag: 1662153069-990727 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Alexei Starovoitov Convert bpf hash map to use bpf memory allocator. Acked-by: Kumar Kartikeya Dwivedi Acked-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov --- kernel/bpf/hashtab.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index eb1263f03e9b..508e64351f87 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -14,6 +14,7 @@ #include "percpu_freelist.h" #include "bpf_lru_list.h" #include "map_in_map.h" +#include #define HTAB_CREATE_FLAG_MASK \ (BPF_F_NO_PREALLOC | BPF_F_NO_COMMON_LRU | BPF_F_NUMA_NODE | \ @@ -92,6 +93,7 @@ struct bucket { struct bpf_htab { struct bpf_map map; + struct bpf_mem_alloc ma; struct bucket *buckets; void *elems; union { @@ -576,6 +578,10 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) if (err) goto free_prealloc; } + } else { + err = bpf_mem_alloc_init(&htab->ma, htab->elem_size); + if (err) + goto free_map_locked; } return &htab->map; @@ -586,6 +592,7 @@ static struct bpf_map *htab_map_alloc(union bpf_attr *attr) for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++) free_percpu(htab->map_locked[i]); bpf_map_area_free(htab->buckets); + bpf_mem_alloc_destroy(&htab->ma); free_htab: lockdep_unregister_key(&htab->lockdep_key); bpf_map_area_free(htab); @@ -862,7 +869,7 @@ static void htab_elem_free(struct bpf_htab *htab, struct htab_elem *l) if (htab->map.map_type == BPF_MAP_TYPE_PERCPU_HASH) free_percpu(htab_elem_get_ptr(l, htab->map.key_size)); check_and_free_fields(htab, l); - kfree(l); + bpf_mem_cache_free(&htab->ma, l); } static void htab_elem_free_rcu(struct rcu_head *head) @@ -986,9 +993,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, l_new = ERR_PTR(-E2BIG); goto dec_count; } - l_new = bpf_map_kmalloc_node(&htab->map, htab->elem_size, - GFP_NOWAIT | __GFP_NOWARN, - htab->map.numa_node); + l_new = bpf_mem_cache_alloc(&htab->ma); if (!l_new) { l_new = ERR_PTR(-ENOMEM); goto dec_count; @@ -1007,7 +1012,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key, pptr = bpf_map_alloc_percpu(&htab->map, size, 8, GFP_NOWAIT | __GFP_NOWARN); if (!pptr) { - kfree(l_new); + bpf_mem_cache_free(&htab->ma, l_new); l_new = ERR_PTR(-ENOMEM); goto dec_count; } @@ -1429,6 +1434,10 @@ static void delete_all_elements(struct bpf_htab *htab) { int i; + /* It's called from a worker thread, so disable migration here, + * since bpf_mem_cache_free() relies on that. + */ + migrate_disable(); for (i = 0; i < htab->n_buckets; i++) { struct hlist_nulls_head *head = select_bucket(htab, i); struct hlist_nulls_node *n; @@ -1439,6 +1448,7 @@ static void delete_all_elements(struct bpf_htab *htab) htab_elem_free(htab, l); } } + migrate_enable(); } static void htab_free_malloced_timers(struct bpf_htab *htab) @@ -1502,6 +1512,7 @@ static void htab_map_free(struct bpf_map *map) bpf_map_free_kptr_off_tab(map); free_percpu(htab->extra_elems); bpf_map_area_free(htab->buckets); + bpf_mem_alloc_destroy(&htab->ma); for (i = 0; i < HASHTAB_MAP_LOCK_COUNT; i++) free_percpu(htab->map_locked[i]); lockdep_unregister_key(&htab->lockdep_key);