From patchwork Fri Aug 26 02:44:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12955505 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 21244ECAAA3 for ; Fri, 26 Aug 2022 02:45:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF1EA80010; Thu, 25 Aug 2022 22:45:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AA15080007; Thu, 25 Aug 2022 22:45:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 942B080010; Thu, 25 Aug 2022 22:45:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 80EED80007 for ; Thu, 25 Aug 2022 22:45:26 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 641D9804D1 for ; Fri, 26 Aug 2022 02:45:26 +0000 (UTC) X-FDA: 79840202652.24.A5C1672 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by imf17.hostedemail.com (Postfix) with ESMTP id 0E8EB4000C for ; Fri, 26 Aug 2022 02:45:25 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id h11-20020a17090a470b00b001fbc5ba5224so267692pjg.2 for ; Thu, 25 Aug 2022 19:45:25 -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; bh=v7ijpUUJZb/iDPXwwv+nfCSTFfI8M4d6OICZ7qDTZBE=; b=cn76eGXganlgoSYlgV2oR77vIYIassdgrhDQYBNoAST9ehRnVjOr2Y1A6JjG/Tsy5I Ppz8xlSx8zrByejMn2V9lfaHXu/5H6GWFlN90v05qIDJ9PKnV21lywriUSObeiwqEBdr YMT3umMkrY/zkFJS+KFqUA7Aomj/mZVs2FyFwatqOsy0dNbG2Z58T90p4Me2mvTNJGSw uo2qaUJELJZ9ZmI92gG80VbIqCPbpCes0ti9wNMqQFSYaEuRSkgFGVkENkvc5HEvLPKP pqjBConTXnhwHmVFJ5cN+k+UvEbjAmcPq7MSnBR2wjL632w0jNq/EG/UghBGxwzwUz8p W3zQ== 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; bh=v7ijpUUJZb/iDPXwwv+nfCSTFfI8M4d6OICZ7qDTZBE=; b=y8tAz+SLqQsKoAaCa7WJf88SzIZN54DRbBT6e5+ILPPaNypKpKX6mcN/yNxB1zyV09 w6qqWN/8fXEXRBhnj5J1eQ+d0R4NFK6xHN2lbMomJTJhSIXDQLYRgRhR7EJBRdwHPmWW m8PvLx4TzXrDefMRM5wme6ly6exQ+y2BADlv63tCtcCUZMxvYn424NkogsI8IYBZIHtC IW5aUMEU0wlK63WT/i53N5QuJRm71CcLgFKRY1LKneSwJpdcEwdTFnRXEwbDULmCgbp/ ed+zNeqI2BSst3R59ykNp4jMDvshSpsXyPK0GO2SV/KQ0+kk1GvBGm8wyEMzeOgxKLAb 6NkQ== X-Gm-Message-State: ACgBeo2XbrDf9dG9SuRW2uwDVWNrKjH4wubot0j7dJPR2oYaL44j3tEb rrw+0vzhz8Pg3EWpGrufP6Y= X-Google-Smtp-Source: AA6agR4yPXRgq0g4xyroom/XnM5eNSLLTGaNkOmL/K+bgD8y2UnupRj5bBLPVT83fXmgj1fvSWheRQ== X-Received: by 2002:a17:90b:483:b0:1fb:137e:4bb9 with SMTP id bh3-20020a17090b048300b001fb137e4bb9mr2064418pjb.188.1661481925121; Thu, 25 Aug 2022 19:45:25 -0700 (PDT) Received: from macbook-pro-3.dhcp.thefacebook.com ([2620:10d:c090:400::5:15dc]) by smtp.gmail.com with ESMTPSA id s6-20020a170902a50600b0016dbe37cebdsm259186plq.246.2022.08.25.19.45.23 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 25 Aug 2022 19:45:24 -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 v4 bpf-next 13/15] bpf: Prepare bpf_mem_alloc to be used by sleepable bpf programs. Date: Thu, 25 Aug 2022 19:44:28 -0700 Message-Id: <20220826024430.84565-14-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220826024430.84565-1-alexei.starovoitov@gmail.com> References: <20220826024430.84565-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661481926; a=rsa-sha256; cv=none; b=uEVS41TyyGnkhYnv5c/O8cFahnh9G6KMew+/3AijNUkJnEeJyI+yUZ6Yvsarr9cas6xToq Azt0AioAhifwGNFAGgkD6S5m0hq0UppLYtHgwa0aeZk18fBFRryufMw5bA2VEQabW/J7K3 muTS+8SEEm3aSbg5f7S/jETJ2Yk+6eM= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cn76eGXg; spf=pass (imf17.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661481926; 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=v7ijpUUJZb/iDPXwwv+nfCSTFfI8M4d6OICZ7qDTZBE=; b=7+/MDMoPbuv3sr+LtmWg7bnUAe9dDcfjZmsAW5LCA3rJwMxefKxAXp9KbSBRUf2ziNu5Kf Et8+BNLUWUFePhz1Jr3LsL/LcvbX8Y7zwoo0p8wvxnXfSYjXJbikJwKmk8y+8xDYN6Q+Yl YRqt4EwK6/BcDOucLYGAM6GVo3XTSTY= X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=cn76eGXg; spf=pass (imf17.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam12 X-Stat-Signature: bd7sy1kc7jj18968mf4wzbcg3djfrzjk X-Rspamd-Queue-Id: 0E8EB4000C X-HE-Tag: 1661481925-230749 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000182, 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 Signed-off-by: Alexei Starovoitov --- kernel/bpf/memalloc.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index 54455a64699b..9caeeaaf9bcb 100644 --- a/kernel/bpf/memalloc.c +++ b/kernel/bpf/memalloc.c @@ -225,6 +225,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; @@ -250,7 +257,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) @@ -453,6 +464,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;