From patchwork Fri Sep 2 21:10:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12964691 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 282DDECAAA1 for ; Fri, 2 Sep 2022 21:11:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C37438013A; Fri, 2 Sep 2022 17:11:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC01280120; Fri, 2 Sep 2022 17:11:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A39CE8013A; Fri, 2 Sep 2022 17:11:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8E5E180120 for ; Fri, 2 Sep 2022 17:11:51 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6AE274053C for ; Fri, 2 Sep 2022 21:11:51 +0000 (UTC) X-FDA: 79868392422.05.D101492 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf18.hostedemail.com (Postfix) with ESMTP id 299B01C004D for ; Fri, 2 Sep 2022 21:11:51 +0000 (UTC) Received: by mail-pf1-f170.google.com with SMTP id 145so3083490pfw.4 for ; Fri, 02 Sep 2022 14:11:50 -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=7zZUv+wUiLTbSdfVlnRTA9LoEPz96HdCoAnAM60zCkc=; b=NwS+9ZK2eKTx+aaMOQ7GdHd0ONySpdNTqKHMSj9KKgWrBRR8iXt8Df/fwP/LvFm+10 D1EWQA+KOVMQrFsTbYkppw0sjJqjbiJ8xgDT/caLNdqxKR3ulJItoGLuoFfkewGl4GVj WooLniQWrCHe2zNrOzblGJtjrYvUfDWJTZG1kajIikXP6grHGph1/wguaa33stHepzJ+ L2ZiXeHpJwK8aDdwIawyaJ8Ex9mjGH+9bWT/TAU3ij0sM/l57nR4efX65CHBrZzq/NiT 6ZDREUOVgpQFhfiMnmg/RieFkpIfvfPUn9Bw2snvhhpFPoUHuljnJnBJuQFM1RWMjNxA 19EQ== 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=7zZUv+wUiLTbSdfVlnRTA9LoEPz96HdCoAnAM60zCkc=; b=b+w1Bhnd9BIxdbVOcBT6D21xf5YfVfAZaviV7+CazAhSFYRjHxSqFE3VIk7MJqiomt 3XwRA1YhxEDfboww+gr1rOhC3lssxISfec5LTXSvADq31QNRneM0ot87H2/9uoddMRBs muU32zzDdfA49QUb4oQjgBECpPL7eFzUuUvbXFI5oc80oXkLQB63uyxKqqRlcyUtYsvC 58ShV6T/yXAYradywOIu/uHz1XxsoR1dabIPG1Vq/8p27OEytI0MWNg7/hSWN55VdOJN TXhm2FXPkzyLnC98XSiFtB9rtkFo8NN189/4n9f9FGFC29BSn9vSYe9uxoYR8BqbHHRX jtlw== X-Gm-Message-State: ACgBeo199SkI1uD1D1X5aIjZmc0wL4hNMVKUqRhE/7sYablEHadwJ4ZF Gcs1W6ICUPcjLsCk01JtF6c= X-Google-Smtp-Source: AA6agR5uPuVHb59iNf+oddnsd3ubEx/z1NYbz1Rk4wngjEXEteRwU+dblj5OPnu6zTdC6eXF++2rNA== X-Received: by 2002:a63:2048:0:b0:41c:daad:450d with SMTP id r8-20020a632048000000b0041cdaad450dmr31330653pgm.240.1662153110141; Fri, 02 Sep 2022 14:11:50 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::c978]) by smtp.gmail.com with ESMTPSA id c18-20020a170902d49200b0017315b11bb8sm2124526plg.213.2022.09.02.14.11.48 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 02 Sep 2022 14:11:49 -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 13/16] bpf: Prepare bpf_mem_alloc to be used by sleepable bpf programs. Date: Fri, 2 Sep 2022 14:10:55 -0700 Message-Id: <20220902211058.60789-14-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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662153111; 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=7zZUv+wUiLTbSdfVlnRTA9LoEPz96HdCoAnAM60zCkc=; b=Hq+uXo2tK+XiJqHGupBfdXNlLqdJ6HU3SDsn8BfmMRtasMYOXQLKwseSOfUxYHG5230CY0 auvH7Q23GQ6YkY7jmc6/XMcyiZ+aI+AKEKXo5lMD9IGeuy1l2pHzgQT9FsbFTerbh055f1 CozvY1gwlPJigwm+FrF09BYVpIze/WI= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NwS+9ZK2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662153111; a=rsa-sha256; cv=none; b=PVl2QpGLpGnpjeVTfM1hDu80pgZK77aawg0f7nPQFHTcTI+P2LUI92795rPyw3l9fgP0yt hmQQcb/roCflInB6fEiEf22yWxS3FiGtYNkx1ZA8qMQpIXaN66WLlqL5dJap+68/V425yo ajIdPWWFZlzofxg0IAaT3GY75J4E7MQ= X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 299B01C004D Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=NwS+9ZK2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com X-Rspam-User: X-Stat-Signature: ocw1quekbqaqmzf8381akcxx7wus9na3 X-HE-Tag: 1662153111-359330 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 Use call_rcu_tasks_trace() to wait for sleepable progs to finish. Then use call_rcu() to wait for normal progs to finish and finally do free_one() on each element when freeing objects into global memory pool. Acked-by: Kumar Kartikeya Dwivedi Acked-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov --- kernel/bpf/memalloc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index f7b07787581b..8895c016dcdb 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -230,6 +230,13 @@ static void __free_rcu(struct rcu_head *head) atomic_set(&c->call_rcu_in_progress, 0); } +static void __free_rcu_tasks_trace(struct rcu_head *head) +{ + struct bpf_mem_cache *c = container_of(head, struct bpf_mem_cache, rcu); + + call_rcu(&c->rcu, __free_rcu); +} + static void enque_to_free(struct bpf_mem_cache *c, void *obj) { struct llist_node *llnode = obj; @@ -255,7 +262,11 @@ static void do_call_rcu(struct bpf_mem_cache *c) * from __free_rcu() and from drain_mem_cache(). */ __llist_add(llnode, &c->waiting_for_gp); - call_rcu(&c->rcu, __free_rcu); + /* Use call_rcu_tasks_trace() to wait for sleepable progs to finish. + * Then use call_rcu() to wait for normal progs to finish + * and finally do free_one() on each element. + */ + call_rcu_tasks_trace(&c->rcu, __free_rcu_tasks_trace); } static void free_bulk(struct bpf_mem_cache *c) @@ -457,6 +468,7 @@ void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) /* c->waiting_for_gp list was drained, but __free_rcu might * still execute. Wait for it now before we free 'c'. */ + rcu_barrier_tasks_trace(); rcu_barrier(); free_percpu(ma->cache); ma->cache = NULL; @@ -471,6 +483,7 @@ void bpf_mem_alloc_destroy(struct bpf_mem_alloc *ma) } if (c->objcg) obj_cgroup_put(c->objcg); + rcu_barrier_tasks_trace(); rcu_barrier(); free_percpu(ma->caches); ma->caches = NULL;