From patchwork Thu Sep 1 16:15:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 12962896 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 E4715ECAAD1 for ; Thu, 1 Sep 2022 16:16:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 854226B00A9; Thu, 1 Sep 2022 12:16:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7FF298000C; Thu, 1 Sep 2022 12:16:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 679008000B; Thu, 1 Sep 2022 12:16:41 -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 55A8D6B00A9 for ; Thu, 1 Sep 2022 12:16:41 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 316F11A01D5 for ; Thu, 1 Sep 2022 16:16:41 +0000 (UTC) X-FDA: 79864019802.04.AB64E8F Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf04.hostedemail.com (Postfix) with ESMTP id E30804004D for ; Thu, 1 Sep 2022 16:16:40 +0000 (UTC) Received: by mail-pj1-f53.google.com with SMTP id m10-20020a17090a730a00b001fa986fd8eeso2931729pjk.0 for ; Thu, 01 Sep 2022 09:16:40 -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=42e36wx7XXKoNRKIF9mVOArDC422WoW66dv9/nAsqMQ=; b=mEyjFDXtAvOlKfUWKYjrinEOWqFoQlAn3j+ZAMCU0tYmuUaQh3eq4Gze2eLJ369IRz nyeIQmdP7+dprrgfnAvrx30leEeMSmCN4Lrneqq7t7lEk8doeNLsV5xpp0HGxTkmBFvp CNUGH8KNInPDESJ6RdgFJpSf/sUycGWv1RZZ4oljJuad1tZlbQSi32D9Qs1IUzGHTEJi Ww9Thux9nyc+ke7zAO7lkLoEelNDn+ZE5t2wt70H4U/zFMcpfc9blv7tHJIcoVn35zCY qEl1SOf3Drg+71f+mYeZkmi+HueO9olNdFACZ/kYvl6KjAUT4S0K5mx+ogSzqcUww2XK l9tg== 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=42e36wx7XXKoNRKIF9mVOArDC422WoW66dv9/nAsqMQ=; b=W2ScDu79fRo32xitQ7iF+wSF1R/+awEVOEYUqWiSrI9x3/vyLpCOvvACEdOjAkGDBV UE90bxU1GwCmjiV3XxxwJI5buQ9Rfo8BLLMJ5v/kPudllU/+wMBWOAdCawRSWXTfEc74 X+Wz4qomVt1uLLwZNKr3B6jq5Yf+wtVr8eI0NoR4WzybJOJgpzq0VPpW2Wz5x/dCA0vs tYUYUvvo11KNnZb7C/2aMUmKImHdCiJmFW60GCyqRw6mlQeXR2yiyRI7Tku+n81SvTMl icsxTYNTJocoaqqM5Tu2zujokfCBLFPDR4XR/y83ukAIqPbTw9TwKtShbpUA0AF51K2G Uk2Q== X-Gm-Message-State: ACgBeo1M5P86C9hxvRO29djQSO7IQtMYccTwX3d1AS87DJk8FkKCrnY4 47se4pDOrn3OnbEx2o/PpbQ= X-Google-Smtp-Source: AA6agR78xK3PbbrN1He9C+ueQ4NagXOAMJTErV01ObA/z2a5A07FWyxOdPTRRTZfgo4z9Aj3X3A58Q== X-Received: by 2002:a17:903:32cc:b0:174:e627:4909 with SMTP id i12-20020a17090332cc00b00174e6274909mr17798812plr.67.1662048999937; Thu, 01 Sep 2022 09:16:39 -0700 (PDT) Received: from localhost.localdomain ([2620:10d:c090:500::3:4dc5]) by smtp.gmail.com with ESMTPSA id mh16-20020a17090b4ad000b001f8aee0d826sm3498191pjb.53.2022.09.01.09.16.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 01 Sep 2022 09:16:39 -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 v5 bpf-next 13/15] bpf: Prepare bpf_mem_alloc to be used by sleepable bpf programs. Date: Thu, 1 Sep 2022 09:15:45 -0700 Message-Id: <20220901161547.57722-14-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220901161547.57722-1-alexei.starovoitov@gmail.com> References: <20220901161547.57722-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=1662049000; 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=42e36wx7XXKoNRKIF9mVOArDC422WoW66dv9/nAsqMQ=; b=5wVGLJnKLvTLfydkCSK9lIfb0ULc4x2+8A/ba7+1WplE6esKGEL3aLMR8tpAl3LzRNUMCV VSCA9q2mV6iSCDmLIs3ZOya0yfTzIQpZW5CSnS5rU9jK7u4RPNIpA8LL07YWm6E2BSC8ZL wOaPrV/q1ni2F3MYSga18vCwcAbDDYk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mEyjFDXt; spf=pass (imf04.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662049000; a=rsa-sha256; cv=none; b=EaKhzfuL0r1EAbKvAaAoAj9+ZAP7bZK5wQer2zpwiC4d1CjNMbtVBks2O/34lugrhT4oYH fLYDg8vh2Efr9huU9lzilHQ2ADU+Bnyo2Ch3gYCAtJfSDQSEk9UkFkh8FJq2PWWAJaLO4u lRUSYNjs/ImE6tKdg8dOBwE1mTWmLs4= Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=mEyjFDXt; spf=pass (imf04.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam11 X-Stat-Signature: 78cfdzwmricmdqznbra73xtwp75gbu1x X-Rspamd-Queue-Id: E30804004D X-Rspam-User: X-HE-Tag: 1662049000-541801 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 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c index 967ccd02ecb8..a66bca8caddf 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;