Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2177322pxb; Mon, 22 Feb 2021 23:47:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJxUrBgTUZS+H6e9kSnGt3UqJmwkQzPHuoAoRo88CaSe40AW2OeDvt/kg4RPS0SVM8dW26h9 X-Received: by 2002:aa7:cdd2:: with SMTP id h18mr24218517edw.38.1614066470011; Mon, 22 Feb 2021 23:47:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614066470; cv=none; d=google.com; s=arc-20160816; b=zW4GZxi3cBiYb2bC/K9L5vhnD+Hl6wpIFJ7yWx8V3cFAR0Qyz41tGzjMQto613zG7Q saXgpDIAzsSe7uLxnjjM4IyLLNQ2k+GpHmBKUAt5lFU+LOfYVvpZGL+sWH+JFGMkZw5o 445FpgkEIMvERZf+8YqRpS9UCkdu/bT7n119RzseM9X7eZiWZfKEa8e7iWHUoIbcPC+i KL7c2g5FFP3yxeCvO5dn431A5+lS+ZzU2WSOeBfjELzGvidmWGqdVXtg4/VgpbY+aEuT YJeETVVcoDA/GR3b3CCRXHSnKavUFS3nZ/DUYjE46awcVlw44j+YqtrWmz9ztJFnr/Eo PzfQ== 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=ZbrJy9F31IyOyKc6QRCuclFArja7riXdxECoDwKq/m4=; b=0PUdozC+JiCm1P7t3TMlFM4Q+x2MKtK6TtXFrB53+FsTKhr4f36W80fR+l2yn4hs9e K9FGn0wBE7aYdm0b5jyGPZg4orNKSOyEf0y7nAAzFtQAiwT9ALKJLa7DkHEoZAY1eEqk EMUx9YOJJmOLRg8TX5xLCQ1hmUWdFKkCb3D/YrwFbghfJ5s5x0QKT0D2YyriAlx5ZSAp EEG3dQIybL+XCMz2whaefAaMinVWUpEi0MPHSBxmkv60CCjPvvypDRbHI9FeJ5PN0Y1b rQNonbs35bHhUQFYRuPGFpW24woj0HUxovG+3VqxCcMw96CnLswP593sRh3lmlAMbi4U UiZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kAS+penU; 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 yd25si13646001ejb.451.2021.02.22.23.47.22; Mon, 22 Feb 2021 23:47:50 -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=kAS+penU; 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 S231915AbhBWHUS (ORCPT + 99 others); Tue, 23 Feb 2021 02:20:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231840AbhBWHUM (ORCPT ); Tue, 23 Feb 2021 02:20:12 -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 9B49CC061574; Mon, 22 Feb 2021 23:19:32 -0800 (PST) Received: by mail-yb1-xb2e.google.com with SMTP id f4so15497814ybk.11; Mon, 22 Feb 2021 23:19:32 -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=ZbrJy9F31IyOyKc6QRCuclFArja7riXdxECoDwKq/m4=; b=kAS+penUa37P4RUd7+QrbldH1BOQ9P2ymVbT5d7WvMt8LPaXToK7YR09KpEL96x5iH Aa6YQDtzdpxS24rH4c30RL6CF0chlrE72YML+61YkdyiG2UOXDbZX57M6p+bi+QVYO0Z 55dTjsGmSRiMfi/CzyLzG05XfvMEUz+aV89SPozdzSCaL+mKKV//V+ST8AjwdoCFOLix cr/IBF6xSaIMe8IPhMX6lboCp4b55+PerW0B5B1tCFRQ4OmjM2esDrgKkwXq6GNNOWcu zvxnFYylwFWHtlk6CmlCWC86+30ptXQuCFS+vG1BCpxReP6MXb9MpOD7PmqAJTGCK/Mh p0kQ== 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=ZbrJy9F31IyOyKc6QRCuclFArja7riXdxECoDwKq/m4=; b=il1Mas5x2s6JEIJfd75LCEaQUoAdZhmUB6NwH7ZtJ57YlhIY+2NDGbo8uGH0yc0up5 p/Wvdxz/urxByoC1HdG52rI/uwgxxnJE9v4rBNcZJohlt929xLqrtTpwxfw9lICRigkL kH1m9JwNMRIU6SQCiLsDrmnrTYnB6+a/K+FiSfuXFJDAFH6Rwsc0c5p4I29csxLuM2l/ gcusYeHMyW/yVtd3gpMaLdODqdr6Jj+HpGOLioq9zIP2w9VH9Jd39X01eHySUAqJ6U2T mcUFPoHnV6Zb8VUGvU1o2iW1PAdxCu4QxpZ1KwUIF6no0epbhUbiq0qriZSWRURZP8Km kbnQ== X-Gm-Message-State: AOAM533EwxwGAqdrcFaEUd0OaSo80p1B9oFzTzvhT9y9SJ5qu3tlt4PA irzED00aJJOkee5QoP8+SUsYJ6YT1f5+eOapfqk= X-Received: by 2002:a25:37c4:: with SMTP id e187mr39545653yba.347.1614064772003; Mon, 22 Feb 2021 23:19:32 -0800 (PST) MIME-Version: 1.0 References: <20210223012014.2087583-1-songliubraving@fb.com> <20210223012014.2087583-3-songliubraving@fb.com> <6A4F1927-AF73-4AC8-AE44-5878ACEDF944@fb.com> In-Reply-To: <6A4F1927-AF73-4AC8-AE44-5878ACEDF944@fb.com> From: Andrii Nakryiko Date: Mon, 22 Feb 2021 23:19:21 -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 11:16 PM Song Liu wrote: > > > > > On Feb 22, 2021, at 10:21 PM, Andrii Nakryiko wrote: > > > > 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.c uses casting in multiple places. How about we fix it in a > separate patch? > Sure, let's fix all uses in a separate patch. My point is that this makes an assumption (that struct bpf_map map field is always the very first one) which is not enforced and not documented in struct bpf_local_storage_map. > Thanks, > Song > > > > >> + 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; > > > > [...] >