From nobody Sun Dec 22 02:05:50 2024 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5055F14A82 for ; Sat, 21 Dec 2024 06:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734761444; cv=none; b=hHeKyMZk9BDgZhTt1lwCO8YfpFP1fyXcJPgNLmFGVxM6lPTrX4Z7XXHGHdYfZZ1XL/sn14CoJr82/CvRBmPr07cI/SP694dZgYtq58KYkDeMMfB8+pgxIPBiApSp9tsO/QenVNF02IQSbn7ziN5ycEJPkF2OcaMJtaMhiqgLb1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734761444; c=relaxed/simple; bh=PN6V9tT+jUJNcrg2/IifXhf5UUd8c1DCmYD9pBGAcpo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=OacHCrYQVaDkz0yMKiZ1DFRR1qe9LmzhZ3hgv/SE0oNhJ3NEYvPTQLZVWgz2jqnenivGEfVijitEJTwMaa4jdBa4Ue1aFZeKKZPEOZd9PWTw/tPQ0wbD5O3kjbTbxCNYV06weZ5xUUXAyoXVeCbjbyo2EcDZIrPI1AWE/cNxWzg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=RiUumem9; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="RiUumem9" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-215464b0432so3035415ad.0 for ; Fri, 20 Dec 2024 22:10:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1734761441; x=1735366241; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0MUN4Uw+ym76sEm1sJgHgtu0fyPy5FHyQ5TXTAPskQw=; b=RiUumem9U/bc/3gMigSyflmW1Gv58XZV+g2R8yjeJwnDCExFrK/XDgdHZUO43+/Hyl qCzcG2KMHoPDnRJken+u8taDjHH36kUC6b0T7DRk4vi3eaKXM/3ELXRLTjzHPCntDT/S ipBhwSw3pbcnPJkrn9ZBmhx3B6YUkU3AAaMx/T+HuWJ9m22uWehjZSKVh0RXJs0hdIK7 vjMEDMsH+ZlKau6w5MeebIKkpgCk6mIB3JLe8TC3qyypcnE4FOXnqibphhtIRKBDaWqG tiQVjx9rh7f3W/WCza4angWbYDr6ykW9Z+ITnkV2lZyR8zoxZMvHzUp/bvvY2/IHGBTl 5e0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734761441; x=1735366241; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0MUN4Uw+ym76sEm1sJgHgtu0fyPy5FHyQ5TXTAPskQw=; b=MvXy96DEtvzs/rdMfrvJkSTPX8BlA/xRsZ+sIfWqspgd7OgIjbkYyvayzzf3YSf5ol BnJhdqp0VFkwIzt2nkqPyg2jEERbyIFVOru8Ap/1aoOPO1lXSlilvtecaoNmghiVRMbt 31n6L6NcoHtTtlHBAVrOJEjt/hCkBXnroftBH58b2IfwG8YyKbVvDt/zdwevzeQ/yN3b b/1GTqmRUPkddhAOQ5+eYgrJV/aU8YMahBftIfsKxHfFCvcPFdWisNwgMKNuccGW5NhV v7CxeOx0b2nmabEy4fWTP3LHHxGtPodKXvZHB9rg2ZR3jy/MGHLi0Ult6SvC7+3H5Ybv 8Lyg== X-Forwarded-Encrypted: i=1; AJvYcCXQU5axk9Z5yzHIvpfpOzc37669EpEvXxW2BTgNvdEwbLSUbMGlsvsH7hqQtoRnBdhXocmh7l10yf2e7Ng=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2hgr9JHrirvkaGyftO/mUGRyBlqgWWBH/Ii4HSYtkkLgYL49k iN8hTbtYfxZwEqLG+4p0JBc9PuoXf9s3pr696maMBvR3m/pR9qWaZn6AFYQDUMI= X-Gm-Gg: ASbGnct1Ixvvc3EoqTnghAxa1ijVJwYHGZbUFjP5cf2fGpfqLGwBxijEI6B5sWYrZXo SP2aQgkVkBTyedcJQtw+P8/zSWEaTVhPpUzuqza2En/DYR2x4f9tDeFD717lLZoC8Eynwj3dHGH 3cdM6hk2U8XRL3ulXPXan00NmyeBgk8myCHXDuS7M6U5t0Oae6Wwo3bw9V8Xs7yTbHCmEh8MuyH sPE+DSIOiOP0aIRXD0GFvQqbLOgp0rIjfvTXtS5LwTVZyORhk3Cxd8EureWa6j2eRFSr1hja3Zj Fz0jvUsn8dnSbu2s/pQ= X-Google-Smtp-Source: AGHT+IH5nR5nisAREPuE59xSMd/Jd77yqnRaPq0stclmZn8R/ib/DK7nVCd3VKnc2yo9CrnKmZ3TyA== X-Received: by 2002:a17:902:d510:b0:216:2e45:f6e1 with SMTP id d9443c01a7336-219e6f2739dmr27660845ad.15.1734761441601; Fri, 20 Dec 2024 22:10:41 -0800 (PST) Received: from C02DV8HUMD6R.bytedance.net ([139.177.225.251]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9629d0sm38292615ad.41.2024.12.20.22.10.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2024 22:10:41 -0800 (PST) From: Abel Wu To: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , David Vernet Cc: Abel Wu , bpf@vger.kernel.org (open list:BPF [STORAGE & CGROUPS]), linux-kernel@vger.kernel.org (open list) Subject: [PATCH bpf v2] bpf: Fix deadlock when freeing cgroup storage Date: Sat, 21 Dec 2024 14:10:16 +0800 Message-Id: <20241221061018.37717-1-wuyun.abel@bytedance.com> X-Mailer: git-send-email 2.37.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The following commit bc235cdb423a ("bpf: Prevent deadlock from recursive bpf_task_storage_[get|d= elete]") first introduced deadlock prevention for fentry/fexit programs attaching on bpf_task_storage helpers. That commit also employed the logic in map free path in its v6 version. Later bpf_cgrp_storage was first introduced in c4bcfb38a95e ("bpf: Implement cgroup storage available to non-cgroup-attach= ed bpf progs") which faces the same issue as bpf_task_storage, instead of its busy counter, NULL was passed to bpf_local_storage_map_free() which opened a window to cause deadlock: (acquiring local_storage->lock) _raw_spin_lock_irqsave+0x3d/0x50 bpf_local_storage_update+0xd1/0x460 bpf_cgrp_storage_get+0x109/0x130 bpf_prog_a4d4a370ba857314_cgrp_ptr+0x139/0x170 ? __bpf_prog_enter_recur+0x16/0x80 bpf_trampoline_6442485186+0x43/0xa4 cgroup_storage_ptr+0x9/0x20 (holding local_storage->lock) bpf_selem_unlink_storage_nolock.constprop.0+0x135/0x160 bpf_selem_unlink_storage+0x6f/0x110 bpf_local_storage_map_free+0xa2/0x110 bpf_map_free_deferred+0x5b/0x90 process_one_work+0x17c/0x390 worker_thread+0x251/0x360 kthread+0xd2/0x100 ret_from_fork+0x34/0x50 ret_from_fork_asm+0x1a/0x30 Progs: - A: SEC("fentry/cgroup_storage_ptr") - cgid (BPF_MAP_TYPE_HASH) Record the id of the cgroup the current task belonging to in this hash map, using the address of the cgroup as the map key. - cgrpa (BPF_MAP_TYPE_CGRP_STORAGE) If current task is a kworker, lookup the above hash map using function parameter @owner as the key to get its corresponding cgroup id which is then used to get a trusted pointer to the cgroup through bpf_cgroup_from_id(). This trusted pointer can then be passed to bpf_cgrp_storage_get() to finally trigger the deadlock issue. - B: SEC("tp_btf/sys_enter") - cgrpb (BPF_MAP_TYPE_CGRP_STORAGE) The only purpose of this prog is to fill Prog A's hash map by calling bpf_cgrp_storage_get() for as many userspace tasks as possible. Steps to reproduce: - Run A; - while (true) { Run B; Destroy B; } Fix this issue by passing its busy counter to the free procedure so it can be properly incremented before storage/smap locking. Fixes: c4bcfb38a95e ("bpf: Implement cgroup storage available to non-cgroup= -attached bpf progs") Signed-off-by: Abel Wu --- kernel/bpf/bpf_cgrp_storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c index 20f05de92e9c..7996fcea3755 100644 --- a/kernel/bpf/bpf_cgrp_storage.c +++ b/kernel/bpf/bpf_cgrp_storage.c @@ -154,7 +154,7 @@ static struct bpf_map *cgroup_storage_map_alloc(union b= pf_attr *attr) =20 static void cgroup_storage_map_free(struct bpf_map *map) { - bpf_local_storage_map_free(map, &cgroup_cache, NULL); + bpf_local_storage_map_free(map, &cgroup_cache, &bpf_cgrp_storage_busy); } =20 /* *gfp_flags* is a hidden argument provided by the verifier */ --=20 2.37.3