Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2164261pxb; Mon, 22 Feb 2021 23:20:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJxSlpn+uiJeQiEkZuIz+ryZgGViGr2qDhsPHf5SEVP4/75wlogVriFImzWAQTofDP9YYz5+ X-Received: by 2002:aa7:dd4c:: with SMTP id o12mr25885189edw.180.1614064825937; Mon, 22 Feb 2021 23:20:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1614064825; cv=pass; d=google.com; s=arc-20160816; b=mpnIDwz/S0gFv/2LXJgrEOpc/oEoi8nA+E/JSO/jqoJAyOUJxqKXnAzumwHmJMzoO4 l6KGjgWvc/bGssAjzAUSW0jfEeZO4SYV1Xxot2SeZBKghBCy0gCLafyZqRc8tjWLzvY7 VGlBq0096DJ9qMKyio3CgeBMGJvd+e5xZEHRsCuk8x8I9XGvvifiudPiforQfjpvrURI gjaC4BsMz63qiYXZUvC5XtvdDvqkmUeKvqKScwAmKshwUhx3ce3cjPIsii6emJ77uI1L 2ZKoQhR0imGiRRhZQ7OnjSVnqe7bMy8LqhyBQHUn7C1afgpduyE+ZF4YjmH+vqUc290A 8Oww== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:cc:to:from :dkim-signature; bh=jj7lI8sSvxh4aY5HOz5aTxd6c+6ItJ0k8SLNpsPGTDI=; b=Ub5jM4gjcUkaFHC4O2BSUwn7ABO07t7UBACyF85aPACzWuVqv5XonY+d0J7hIwIpRZ mqVkMIepGrijuMxZ5UGgXqEXJ+7qeeosL3mpSzoPl1qVSIwuPDf2+mEGMfpg2p6yEG5R OPXYDwty4Y/bEbYOHdLi237C5+LFq0yDfBoG6WFEwSTpZne4q9lIhoFjr128r4BvNU/v +ABvJ5YlJpGROSFIvNwqS/Vhq9gjRXGn/cDwz4mVrW6EgpiOpjutY70Raus0WTxhv2/o fHk6+wBrBkpTRsS3E7EIHVlAvYzlWQN8hS89tSicrQaL58An3Fd2sVv3Zowzz7/nbigp X7jg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=pZNOssBA; arc=pass (i=1 spf=pass spfdomain=fb.com dkim=pass dkdomain=fb.com dmarc=pass fromdomain=fb.com); 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 y9si13158550edt.598.2021.02.22.23.20.00; Mon, 22 Feb 2021 23:20:25 -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=pZNOssBA; arc=pass (i=1 spf=pass spfdomain=fb.com dkim=pass dkdomain=fb.com dmarc=pass fromdomain=fb.com); 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 S231638AbhBWHRj (ORCPT + 99 others); Tue, 23 Feb 2021 02:17:39 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:3714 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231974AbhBWHRX (ORCPT ); Tue, 23 Feb 2021 02:17:23 -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 11N7DjEb004128; Mon, 22 Feb 2021 23:16:22 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=facebook; bh=jj7lI8sSvxh4aY5HOz5aTxd6c+6ItJ0k8SLNpsPGTDI=; b=pZNOssBAIrRVVtLNw9Fc/c8sJt8YNo9930DpbYf56w/zqlJ7VhfnIgxBsvwM6K2uo8at rrvQFanvzOO0qSzsULehJHvZHDRFp5q7pzkM95d//XKCVUEjBEqQG3bbyfl4JdiX6nAH 1ycVxBOMDl6ScsAjh/JvB0Ltl1qFQqFMS1k= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 36u14q4q4c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Mon, 22 Feb 2021 23:16:22 -0800 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (100.104.31.183) by o365-in.thefacebook.com (100.104.35.175) 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 23:16:21 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HlWH5PMVBdC/V6g3qCim8u+pvyZl06LJZUuCNgp0dURJwCfIjoFFJfFjWXXPZDOwF5C97WP8SJnaEEft7KqEP8cnzISDSkt8YPQZfSNGUbDuD8hRy5T89/qWYJqGgo9SFJC7fO0vnI2HUdHbRXynGBc8rrkUXZagkQUIPuDT3HFqMijP5x6JLfe+llZ+/DRqQjA4ES6Qto34oLcU9o0rjIAXvW5pFD0JhLlAgF1qGS5S6P8JprM7yWMzAq1Jy6yPASBKpFKpGNluyLJqUH4Z4mWlRkfmQfpOc2R7G/DGClrDT0GROnlazNGmfPC3+L3qXym1Gw54qMDJMzRcgzraMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jj7lI8sSvxh4aY5HOz5aTxd6c+6ItJ0k8SLNpsPGTDI=; b=h39egWLCs0sbstxmfUcdIZoa8bsX2xFgKhnBQcc8ClIF31blSzmJytNJ3QWXonxQFOvzX0H6R3/HtzIUv5s6c8aE8SGd9eu0YeMZ1paW2txTvJpTN4kWQLwCGU/o4XITXZNfZe5XZY2CBXfHEzCPJmqsDQJEIGzeeyF2P850sd7KnIqVyN8PmWPvRIgxLb/CD8cqIhzAyzEPW/SswEImdPd1jEG2ZN8MEKIvg58kRKYXKBhZqtDTRsY7H2U5oYUAXUWTfH5iLpA+4DxaSTrNTiW238UdbVwUjLmgu6Wej+/6d0MTkusfuGFR9zjcqk4swRctlGzH+AVc++e3MzBKMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none Received: from BYAPR15MB2999.namprd15.prod.outlook.com (2603:10b6:a03:fa::12) by SJ0PR15MB4711.namprd15.prod.outlook.com (2603:10b6:a03:37c::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.30; Tue, 23 Feb 2021 07:16:18 +0000 Received: from BYAPR15MB2999.namprd15.prod.outlook.com ([fe80::8cd0:7956:3c0b:7910]) by BYAPR15MB2999.namprd15.prod.outlook.com ([fe80::8cd0:7956:3c0b:7910%6]) with mapi id 15.20.3846.042; Tue, 23 Feb 2021 07:16:18 +0000 From: Song Liu To: Andrii Nakryiko CC: bpf , Networking , open list , Alexei Starovoitov , "Daniel Borkmann" , Kernel Team , "Peter Ziljstra" Subject: Re: [PATCH v4 bpf-next 2/6] bpf: prevent deadlock from recursive bpf_task_storage_[get|delete] Thread-Topic: [PATCH v4 bpf-next 2/6] bpf: prevent deadlock from recursive bpf_task_storage_[get|delete] Thread-Index: AQHXCYIynt1KuoobvEWNWRDMRSL2HaplRT0AgAAPSwA= Date: Tue, 23 Feb 2021 07:16:18 +0000 Message-ID: <6A4F1927-AF73-4AC8-AE44-5878ACEDF944@fb.com> References: <20210223012014.2087583-1-songliubraving@fb.com> <20210223012014.2087583-3-songliubraving@fb.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: Apple Mail (2.3654.60.0.2.21) authentication-results: gmail.com; dkim=none (message not signed) header.d=none;gmail.com; dmarc=none action=none header.from=fb.com; x-originating-ip: [2620:10d:c091:480::1:8f65] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 948c1cd3-10d6-400d-537d-08d8d7caea40 x-ms-traffictypediagnostic: SJ0PR15MB4711: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-oob-tlc-oobclassifiers: OLM:9508; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: JRr/Zm49jDaI5g7CQAMi4y2yFBxebVDa1yxEqEOluQ6/MuhPQVELFzWVvQl0i5ys5j+dRmNkxKQvU4lcMF8MURL7BkFR+gZKk2PIzpXTB0WikM/SWMTjlnM1nFhoKAGax/oBD096Lrjb83X9gA3W+h+rwKohXcJ1LdHO32K8iSqK1ktGVCrbVz7DTbYI/xJv1vtrDTQVpIpFhWriWPatFbG9iHHngK/37GYaG7lQ1eiR6fhbstY8UjyaB3QO4KQfeDT37QBpFnTg9Mjien9qgMYst/ogpvnY4pAaQ2I9A8nVEZ6B54/6EiTxZP6ga4Nkrvh2HKjwW1CKvIaN0FajdHXUm/yhVeve1Rq0mcRczGhn6FJFyXIeem5STidJmKCALCM0FP+zXrRCyuzd4/AzCl6aG6GKsVU0RoBUZa7TXw09Sw+L1WfsT92V85GNb85/P64LPqjRGiU04YIGru6ooaIRQbxh454DCO5di1g7XK41gTaEiK47YBCkSgSlqj8pykj8WNuDDxl82ORdtFJFBMez/Sv9fyDQIPHlnZTymhSxk5eSi5BBjMV08mrPYU8XQ91vVMMl79V1Quqh8OC/yw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR15MB2999.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(396003)(366004)(376002)(346002)(39860400002)(136003)(186003)(66556008)(6512007)(86362001)(6486002)(33656002)(8676002)(66946007)(2616005)(91956017)(71200400001)(2906002)(76116006)(478600001)(4326008)(8936002)(66446008)(64756008)(66476007)(6916009)(316002)(83380400001)(5660300002)(53546011)(54906003)(36756003)(6506007)(45980500001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?r5tetsnduT4uYlcgIo6EL71gIB08AXaZ8jvN8zhGVP+lBKeMM+7XJbsZYSjc?= =?us-ascii?Q?4jPcS9ewPrPc9Gw98Ml7vZ46Y8jdfYzkUjzKZ39Dfut/7zPTNTrZ8iL+tqm/?= =?us-ascii?Q?3erhrTkaxL4nxWOrxtX85y8Bu3gMypftIIwBTWyDJw1s+igfRJqg+xmpKqUg?= =?us-ascii?Q?xXezGjnp5yAi4dGM6mNuD323rgb/YzTuefuUqWQp95vQnf3cAJeQJfOw/pgC?= =?us-ascii?Q?G4njI5T3dafM0CDYpsdiMg5auEDUHgrmVk5QgM46CPcvrqTWsivyvTvnuWOr?= =?us-ascii?Q?pXJax0VyvqK9vxuq52pviBwEucjwg7GKuK6/5CltW2VYppgigw0zsPEC6XT8?= =?us-ascii?Q?58JSU5gqVHUMdfQFUyZYxgmWCcBo4hPVEhlga+hc7Yl7oDEvlHjB1dGPmSbW?= =?us-ascii?Q?aFrRpzlhJcWPCIhI4wGu/ydf3FhHwIBs3SUTk7yMZuzb1ezaEyKKnCkh18w7?= =?us-ascii?Q?vcHhT5i7vPTK58pPf8zEtYIjy2MpbZ6AuFDsbi7p9DhdCuvYtBqbdSHk5vSi?= =?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charset="us-ascii" Content-ID: <56FFB24331712B43B00CE1763F75904D@namprd15.prod.outlook.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BYAPR15MB2999.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 948c1cd3-10d6-400d-537d-08d8d7caea40 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Feb 2021 07:16:18.4885 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: vgLu5HY+WNU6SB+RVQyuH843rr8lXAj4mr1FEVglW5RVBYscNvt3aeyIO9SIzHiEspQcavQXKv8IuRgxXOFH6g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR15MB4711 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-23_02:2021-02-22,2021-02-23 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=999 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102230059 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On Feb 22, 2021, at 10:21 PM, Andrii Nakryiko = wrote: >=20 > On Mon, Feb 22, 2021 at 5:23 PM Song Liu wrote: >>=20 >> 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. >>=20 >> Prevent such deadlock with a per cpu counter, bpf_task_storage_busy, whi= ch >> is similar to bpf_prog_active. We need this counter to be global, becaus= e >> the two locks here belong to two different objects: bpf_local_storage_ma= p >> and bpf_local_storage. If we pick one of them as the owner of the counte= r, >> 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. >>=20 >> Signed-off-by: Song Liu >> --- >> kernel/bpf/bpf_task_storage.c | 57 ++++++++++++++++++++++++++++++----- >> 1 file changed, 50 insertions(+), 7 deletions(-) >>=20 >=20 > [...] >=20 >> @@ -109,7 +136,9 @@ static void *bpf_pid_task_storage_lookup_elem(struct= bpf_map *map, void *key) >> goto out; >> } >>=20 >> + bpf_task_storage_lock(); >> sdata =3D 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; >> } >>=20 >> + bpf_task_storage_lock(); >> sdata =3D bpf_local_storage_update( >> task, (struct bpf_local_storage_map *)map, value, map_fla= gs); >=20 > 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=20 separate patch? Thanks, Song >=20 >> + bpf_task_storage_unlock(); >>=20 >> err =3D PTR_ERR_OR_ZERO(sdata); >> out: >> @@ -185,7 +216,9 @@ static int bpf_pid_task_storage_delete_elem(struct b= pf_map *map, void *key) >> goto out; >> } >>=20 >> + bpf_task_storage_lock(); >> err =3D task_storage_delete(task, map); >> + bpf_task_storage_unlock(); >> out: >> put_pid(pid); >> return err; >=20 > [...]