Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2019684pxb; Mon, 22 Feb 2021 17:58:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/FuiExMOoFceakjc9m1/uYwRqY3TDy1CnzqMIZpGeVEKhxoMe+IIOyT786slUeT9AMHdo X-Received: by 2002:a17:906:6942:: with SMTP id c2mr8712592ejs.425.1614045503708; Mon, 22 Feb 2021 17:58:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614045503; cv=none; d=google.com; s=arc-20160816; b=LeUqSZ0JUaqXF1i4MPfnltUcrNmI/geCnF7qEHCoRdI4RZFFRKybInYKMMjEqXNZya zLKofwz6msIrcU8vYPtPFgEW/g6WIRCJW82lrm4xb75fIMpK/dVv4rz8o8EgySp2Dt1R dnM4wcu78hA/Xn5NK8dunBD0hkP/UQOET5Ld5tkwIk1IrtlDwv1tGeJ/WrNPpeRv6whw GDm2c2uXt+ffP/oeNEFNunlGfcjBpfF4BqApDqae5RWrZ9EekWWmRb0kOKMx8itrIPDb 5Z3CDiyAyVXyJGSCE5EJDFMcIpFqpQOO304QHteYzej8Hmcqyxo7jwUaUax/1Joam4Sd QiIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xE5jfbmD7ggvOtV/7xXWeCQmjvzq79ebUAY3GevP7YY=; b=PCl0NaD/npwZh+BhWFIOwy1O1Rbo2ljK51RrjN99Vxc0ZdRxT0qAR8Zg58tPRA5+Vt OpnOiQrMMHnNe2dD/hcopDoWtU6UNhi75KxGbNDTsNRB/suVqBsQe0Q1Xwdjgx0Yu98s ZDzlaNiyHWHRyVcPp1rmVEJwJOjBVPuCk6xIuci6F3soWzzP0kOV2VpWqkCg0NzMuAah 2TbMlcBVo5QVB+CLXhzDj+kIDmNrydlyAEM0B8ccGZwuGeUTuUR4jhGW5udK/saU54Cy 37jj0Is6PyNnzMDP+TG2QeDY6la8Whl8pibQ1ag7z5ejBXMDUcD62VA/C62t0SAsrf9I mgwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b="e9uF/RV3"; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w18si13346565edc.255.2021.02.22.17.58.00; Mon, 22 Feb 2021 17:58:23 -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=@fb.com header.s=facebook header.b="e9uF/RV3"; 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=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231222AbhBWBX5 (ORCPT + 99 others); Mon, 22 Feb 2021 20:23:57 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:20542 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230437AbhBWBW2 (ORCPT ); Mon, 22 Feb 2021 20:22:28 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 11N19TbQ022966 for ; Mon, 22 Feb 2021 17:21:47 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=xE5jfbmD7ggvOtV/7xXWeCQmjvzq79ebUAY3GevP7YY=; b=e9uF/RV3oJY7z4RbCb6z1wJwB7GnXCFZqLH9Mxa1++pKtUf04El/OovNzSPNZqYisUVQ S6TlGDGdcYPxgetHBDhzl3JDvA4ARKsvU0aOH07o/c++/VnRh/DJWn8vFFGPeK4FrupZ JUOlvOl7wqjrlo5I6OmDYg/zlF/IbulIMr0= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 36u14q3fr1-17 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 22 Feb 2021 17:21:46 -0800 Received: from intmgw002.06.ash9.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Mon, 22 Feb 2021 17:21:17 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 083F662E0887; Mon, 22 Feb 2021 17:21:17 -0800 (PST) From: Song Liu To: , , CC: , , , , Song Liu Subject: [PATCH v4 bpf-next 4/6] selftests/bpf: test deadlock from recursive bpf_task_storage_[get|delete] Date: Mon, 22 Feb 2021 17:20:12 -0800 Message-ID: <20210223012014.2087583-5-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20210223012014.2087583-1-songliubraving@fb.com> References: <20210223012014.2087583-1-songliubraving@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-22_08:2021-02-22,2021-02-22 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 adultscore=0 malwarescore=0 bulkscore=0 spamscore=0 suspectscore=0 priorityscore=1501 phishscore=0 impostorscore=0 clxscore=1015 mlxlogscore=938 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102230006 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a test with recursive bpf_task_storage_[get|delete] from fentry programs on bpf_local_storage_lookup and bpf_local_storage_update. Withou= t proper deadlock prevent mechanism, this test would cause deadlock. Signed-off-by: Song Liu --- .../bpf/prog_tests/task_local_storage.c | 23 ++++++ .../selftests/bpf/progs/task_ls_recursion.c | 70 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/task_ls_recursion.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c = b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c index dbb7525cdd567..035c263aab1b1 100644 --- a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c @@ -8,6 +8,7 @@ #include #include "task_local_storage.skel.h" #include "task_local_storage_exit_creds.skel.h" +#include "task_ls_recursion.skel.h" =20 static void test_sys_enter_exit(void) { @@ -60,10 +61,32 @@ static void test_exit_creds(void) task_local_storage_exit_creds__destroy(skel); } =20 +static void test_recursion(void) +{ + struct task_ls_recursion *skel; + int err; + + skel =3D task_ls_recursion__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + return; + + err =3D task_ls_recursion__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + /* trigger sys_enter, make sure it does not cause deadlock */ + syscall(SYS_gettid); + +out: + task_ls_recursion__destroy(skel); +} + void test_task_local_storage(void) { if (test__start_subtest("sys_enter_exit")) test_sys_enter_exit(); if (test__start_subtest("exit_creds")) test_exit_creds(); + if (test__start_subtest("recursion")) + test_recursion(); } diff --git a/tools/testing/selftests/bpf/progs/task_ls_recursion.c b/tool= s/testing/selftests/bpf/progs/task_ls_recursion.c new file mode 100644 index 0000000000000..564583dca7c85 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/task_ls_recursion.c @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, long); +} map_a SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, long); +} map_b SEC(".maps"); + +SEC("fentry/bpf_local_storage_lookup") +int BPF_PROG(on_lookup) +{ + struct task_struct *task =3D bpf_get_current_task_btf(); + + bpf_task_storage_delete(&map_a, task); + bpf_task_storage_delete(&map_b, task); + return 0; +} + +SEC("fentry/bpf_local_storage_update") +int BPF_PROG(on_update) +{ + struct task_struct *task =3D bpf_get_current_task_btf(); + long *ptr; + + ptr =3D bpf_task_storage_get(&map_a, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (ptr) + *ptr +=3D 1; + + ptr =3D bpf_task_storage_get(&map_b, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (ptr) + *ptr +=3D 1; + + return 0; +} + +SEC("tp_btf/sys_enter") +int BPF_PROG(on_enter, struct pt_regs *regs, long id) +{ + struct task_struct *task; + long *ptr; + + task =3D bpf_get_current_task_btf(); + ptr =3D bpf_task_storage_get(&map_a, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (ptr) + *ptr =3D 200; + + ptr =3D bpf_task_storage_get(&map_b, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (ptr) + *ptr =3D 100; + return 0; +} --=20 2.24.1