Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2142385pxb; Mon, 22 Feb 2021 22:32:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYuQRM76ntvwXQYkTEiRYQqW/GR7QhCVbl3/Y4dmUIfkilAYxV3OwJlkJkmlyxlIOu2JlA X-Received: by 2002:a17:906:4f8a:: with SMTP id o10mr5769366eju.484.1614061955657; Mon, 22 Feb 2021 22:32:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614061955; cv=none; d=google.com; s=arc-20160816; b=Co3YBqWsPAzVlkpgILwtTbdg5Qs+dOWHJMOM46YGhIEjW0sJzmRPP2yt9KJpxKyUIv 0jW2SGTUT+9axlawjWqkys9ERPHt8Fddrfxh+3d4RgJvMGhpEiYUXD2wvkXEfFR4kvdk zgJWh31Cq87Hc8KzfPje0ZJHH93TMNNkvykU+0lvDFJV6gAHBf0QIr1lF4QQwVK/78mu TGFDMYm5KpKiiRGje4lIfG6+C0HEgciH00rHZxeIToPrtK8WnrpmUMnlRs1mRdF7DUSw DCo6ZgualGRIA1yadoZCepWN3BeFXC5nvVNbAdiJ293wjr8dH2cPrErggT3uJv6jyUKg emKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=aN0nEKFhckdZG5EVeID2xP188+9jE1AxS3UZDyML5Vk=; b=ZjjWbUk/oYWiPnMcyEdO7l5THslgp7TLucdsCkyo+FYASV5T9sNi07Tj8KehAvTYV6 CIIiIHG1Y/wvYbtktMa0LkLlP4HkoYZ/rcvADrgD9WG6N2h55d+r7/jEYyl/qzsH8Fqq D1B9atB4LNC+d3AyWFhyYOwI80ukJInUryW4oUAF4lz+9SUQ0LpF08cTiBqEORSOdlxL z3r4DnY14cHKpZnCCyK2vahjNtCRn0Tf7Q/dI0MsmwsFx103yO1dKH3fiHt6p8qKUtjn P3bz8AKz2rEV94R1CSm7htjEfPr5bL900sUxllM241WqFXlWt6zovu+xpI80lWWzYcdV QXeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="LVe9/bYY"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h26si11070686ejk.153.2021.02.22.22.32.11; Mon, 22 Feb 2021 22:32:35 -0800 (PST) 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=@gmail.com header.s=20161025 header.b="LVe9/bYY"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230371AbhBWG1e (ORCPT + 99 others); Tue, 23 Feb 2021 01:27:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231740AbhBWGWY (ORCPT ); Tue, 23 Feb 2021 01:22:24 -0500 Received: from mail-yb1-xb2e.google.com (mail-yb1-xb2e.google.com [IPv6:2607:f8b0:4864:20::b2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1002C061574; Mon, 22 Feb 2021 22:21:43 -0800 (PST) Received: by mail-yb1-xb2e.google.com with SMTP id x19so15424762ybe.0; Mon, 22 Feb 2021 22:21:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=aN0nEKFhckdZG5EVeID2xP188+9jE1AxS3UZDyML5Vk=; b=LVe9/bYYFRbcktdhUrB8khXJiLQbsmf5LBj8OoRu5fC1dhe+whay/vuW8QYBR3iXTR uWcTeEw8W2gogkvgIMDwTOoSahyrlZLv/LBada2TWdIOve3Qz/8YjiWzEm3g4cTeQH9N BXXl/g2yCd9CJXRcfcsfcxhAAvZhxHq8ARABaxZpBglxj1c65nE4IxFTune2GDh+6Lm8 vyEoSQPcdgV1ETSYsO75hBX4iMRXv5UicQKdVhduwoxPhNTwBtb4logKvbJcfpMGIo7U UmltnNBEr9G93vd4Xd28UW0u5P1F3c/jK722TZ3b+b4JyQ42Uh6L7Gs36UMgnWODh4So NadA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=aN0nEKFhckdZG5EVeID2xP188+9jE1AxS3UZDyML5Vk=; b=NjorKyXiA/NJX7Ttx7x4txUb1T4avfnmh4gJ5lEX1CL9ZrqL2WmlSAGKJSdqnElqYk L5rP1bKBXx8eMnCGNa+bKelOTt3WTl/Y06xPBjk7weAKzLGnzpjZcwA4O13TGLnC6eeZ nB6Ta/hTaA+BnMY67OyGywM437YWHgCnRwCrxLLUGUy5c3vi/ZYU8F6QXF2Jy+9suuHu hDZ0irT8GEhPP34euIyPLdeArV51Q5oLW1t1CqN7C8CjN8xT9tOP3kP1RNpyyjzDIQiz vhoMOnUkSUZD2nNmzrXy9qRe4VEijTUqxg1rTs1Gm9tVoFuWS7DCCmgPznEqd81NqAtB oQoQ== X-Gm-Message-State: AOAM531BWqElWk8GWAIZ0BT4gt9B+3bz5S9dH4VjQ8aa6hMDtz2vKOgl 0Q2PABwZHtFlZV1bb1ROGtUXw2heHZdLp44sJorEOMP2 X-Received: by 2002:a25:bd12:: with SMTP id f18mr14582916ybk.403.1614061303271; Mon, 22 Feb 2021 22:21:43 -0800 (PST) MIME-Version: 1.0 References: <20210223012014.2087583-1-songliubraving@fb.com> <20210223012014.2087583-3-songliubraving@fb.com> In-Reply-To: <20210223012014.2087583-3-songliubraving@fb.com> From: Andrii Nakryiko Date: Mon, 22 Feb 2021 22:21:32 -0800 Message-ID: Subject: Re: [PATCH v4 bpf-next 2/6] bpf: prevent deadlock from recursive bpf_task_storage_[get|delete] To: Song Liu Cc: bpf , Networking , open list , Alexei Starovoitov , Daniel Borkmann , Kernel Team , Peter Ziljstra Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Feb 22, 2021 at 5:23 PM Song Liu wrote: > > BPF helpers bpf_task_storage_[get|delete] could hold two locks: > bpf_local_storage_map_bucket->lock and bpf_local_storage->lock. Calling > these helpers from fentry/fexit programs on functions in bpf_*_storage.c > may cause deadlock on either locks. > > Prevent such deadlock with a per cpu counter, bpf_task_storage_busy, which > is similar to bpf_prog_active. We need this counter to be global, because > the two locks here belong to two different objects: bpf_local_storage_map > and bpf_local_storage. If we pick one of them as the owner of the counter, > it is still possible to trigger deadlock on the other lock. For example, > if bpf_local_storage_map owns the counters, it cannot prevent deadlock > on bpf_local_storage->lock when two maps are used. > > Signed-off-by: Song Liu > --- > kernel/bpf/bpf_task_storage.c | 57 ++++++++++++++++++++++++++++++----- > 1 file changed, 50 insertions(+), 7 deletions(-) > [...] > @@ -109,7 +136,9 @@ static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key) > goto out; > } > > + bpf_task_storage_lock(); > sdata = task_storage_lookup(task, map, true); > + bpf_task_storage_unlock(); > put_pid(pid); > return sdata ? sdata->data : NULL; > out: > @@ -141,8 +170,10 @@ static int bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key, > goto out; > } > > + bpf_task_storage_lock(); > sdata = bpf_local_storage_update( > task, (struct bpf_local_storage_map *)map, value, map_flags); this should probably be container_of() instead of casting > + bpf_task_storage_unlock(); > > err = PTR_ERR_OR_ZERO(sdata); > out: > @@ -185,7 +216,9 @@ static int bpf_pid_task_storage_delete_elem(struct bpf_map *map, void *key) > goto out; > } > > + bpf_task_storage_lock(); > err = task_storage_delete(task, map); > + bpf_task_storage_unlock(); > out: > put_pid(pid); > return err; [...]