From patchwork Mon May 2 23:09:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12834873 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18B08C433F5 for ; Mon, 2 May 2022 23:09:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229556AbiEBXNL (ORCPT ); Mon, 2 May 2022 19:13:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiEBXNK (ORCPT ); Mon, 2 May 2022 19:13:10 -0400 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96DEF2F386 for ; Mon, 2 May 2022 16:09:39 -0700 (PDT) Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.17.1.5/8.17.1.5) with ESMTP id 242LsRMZ014107 for ; Mon, 2 May 2022 16:09:38 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=facebook; bh=cOQEdGfjFBc0fQXLFHIZS2Djpy7PTKGsd2sNcXs+11A=; b=XHVaaNr99juL9TBA7/qYV/PS1876K8h/dgqZmWUidbhuQriPqwilC+teMp7Rm1vnlzrw BfwicAQ8qhGVEfTyKErErXlf5DPMpfF4lqyyms+X1LNtjkvFd1yxMf4NynVQFCsQe/BJ A/P2hDk4d9tGlXobBaKQJhG2plxQsu8rIjo= Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam07lp2047.outbound.protection.outlook.com [104.47.56.47]) by m0001303.ppops.net (PPS) with ESMTPS id 3fs0sucuq2-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 02 May 2022 16:09:38 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vc486X38OgcLVeLCemoPEqy2ER0SSjFDVSh6BQDb1XkiVugAZ/cNBjXn8TWQAOVAUYpdgzCcS93fF2YuULy0+Z3U/qc5G00/ScESiKueoNGLkBoPoA6IVwErUgUXZJdxsG66GyjG9RlsEnPzrj2edOXLekKD3jNQjh55zwxz2Fu4S2o/bLvY1Gz7AA/71ENaInUf1Kvn8W+he0dSrkFKW3v5IxXknoVb3YoqFDCEYDqQU7GYYcBFCRozNkv8wqBc1GzdDQt36G7iZcYSr1pogmEzup7eMHJVC2gedxbD6p/ZKA3Ltpy56Y7S8FfUreLS8o7wj+/8aJ62BktnMKQf6g== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cOQEdGfjFBc0fQXLFHIZS2Djpy7PTKGsd2sNcXs+11A=; b=WdgVpjliGnzvw6DzXrHqkOQOXV6n7ApVz6eF7Y3d3SghfCPfeKaOXbFIYtgPBH/0wU8YmMhC7rB1ll6+F+/uuHy/Dam5DVN6B7ts0x8ZOecZ8U4GrGtKS0GhC0RH+DquUH6APfAzyxrxiZIGTCg1xjATolyzPMTjtgx0eMnJzpFTLjkqI8V9TDSNdLigoVwdd6miWvGpXZ4ZDYBpYaLZF+Bv2t2ntJhDMKIXWODsbh7Pmtl7XG+Qb8gUmVDvt2JHvvqxseMjGztRw52RXGpFcW4ccjXxAJNDY2SxlFunkfvpVe3cLWJ62zh6ZcAxWPs8VJDp7m0BVNQaPFkFiTANzQ== 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 SJ0PR15MB5154.namprd15.prod.outlook.com (2603:10b6:a03:423::6) by MWHPR15MB1903.namprd15.prod.outlook.com (2603:10b6:301:59::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.28; Mon, 2 May 2022 23:09:36 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::b5e0:1df4:e09d:6b5b]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::b5e0:1df4:e09d:6b5b%5]) with mapi id 15.20.5186.028; Mon, 2 May 2022 23:09:36 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v2 3/5] bpf: allow sleepable uprobe programs to attach Thread-Topic: [PATCH bpf-next v2 3/5] bpf: allow sleepable uprobe programs to attach Thread-Index: AQHYXnmw6LimKPj6okKMdit4AKAiIA== Date: Mon, 2 May 2022 23:09:35 +0000 Message-ID: <611d4629dc959f9d327693180b0d106dcefe949f.1651532419.git.delyank@fb.com> References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 36ddf96c-6815-46c6-2df8-08da2c90d376 x-ms-traffictypediagnostic: MWHPR15MB1903:EE_ x-microsoft-antispam-prvs: x-fb-source: Internal x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: FJ8JIty3RU0yLHXfPRA7eDiAiJQfMx/QdB8TqDUcdEwQi+ZBnLckLPcbLspktLv65kM2A5grOFPTEPRTPMRzCa9LP1Q3fHDhiuLseAt6xWhsC19AX/FYLAU6KyzyZB3Vqe9aNSen9Q3vtyyTFKjaTeuvBWYvvkbXJEbwYV9cSvb/3ay1PPf9qHuqBXW9j9eew/jzx2fnA1PGuQpGjez+I3BozSiPLkxsfaInhHqw4yvHbv/zjVnuhilTBaST9zbJk5TwjTy7R4GBttb+lXaKEU8vut3EdTNl78ZYRXRFZ0nmSbHQ8Zps7+TI4HszrjIQdRUHS58/FKAD6xN9S1rII9ffeIhtpsjsWGstnUAsaCscorki150/TvmFjI3v/W9wlpIdgIini906k8qXwf3jLThi05SXWtHAEwq78f7+V1SN+89m+6UmxgmLNxWQwY11wN5GChKi3QAlT5imWcLhFWDXx3lFF9UYpt7nKeRJYvbBSoS5HSoxMeLD7MCJo4EInSqbLFYjb2dzLM86s5V04d7I03v7bwBzIMR4rVACfJz+BXEwrVH3Z4OSh8P91xvGjLr76edMuIxTjYEAU9fAnXOOrFfLqwu8fZCVrG/T/1bcmcd2hhYn+3JzJFMY49Iwd8olBZr2mscxrdzA4TQ2vcIZ4vfcYjRMQ5BjrTU8rlTGqkALDCqbcw1CXycVnbxuXGWRciRyC5dA62T2ZbMN9A== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR15MB5154.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(2616005)(36756003)(6512007)(83380400001)(186003)(2906002)(66446008)(8676002)(76116006)(86362001)(5660300002)(71200400001)(66946007)(6506007)(8936002)(316002)(66476007)(66556008)(64756008)(38070700005)(6486002)(122000001)(508600001)(110136005)(38100700002);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?vW9rPiZ7uq+l92C+Rjgy6+S?= =?iso-8859-1?q?Ph/vAt7Q95pzA9BaaotQN4s9WObvZzVDM5u4z3kDyU3l4eoH1pwNtI05oxRc?= =?iso-8859-1?q?1hFREGH/XEJ5MSQKmJyGAeq536IVqw5v9V+LO4MYdYKFHIn7ScDxcBUy53zw?= =?iso-8859-1?q?p6W1nv3CxcRReDsvKMqkOMG9ScDbXziO37wLz9rhcyAaQLm1iZPZDLyDbooV?= =?iso-8859-1?q?KGz34azqZY5QvoR0B0II+xfTjGa455R32jiyb7DjqJg+Lekbdj5jHvH2+PKl?= =?iso-8859-1?q?sDyimMl9bQB3ShDPEFuuyiNi0HEjoLg/z8damXDpG3ZANzVjMNjOfIMKQQyt?= =?iso-8859-1?q?DUIuenC1EnPgyUXtpOP1Xa/Ye7Bzjv55X4TOFJnl1isYI0l2lZ4dK+ykqkbr?= =?iso-8859-1?q?xu1+HDO7nSXPLodDLrdflNZfRhJK66N0OAHAziVY3zuy99NLZENzS41uFVF+?= =?iso-8859-1?q?BFhwINm4IPYE+iDgtT5qwrhZehDP3wDHbVqIe6sGX/K8LDnfCX4+K2eGFiLK?= =?iso-8859-1?q?WjS3+FdJmzauYxHV9ovhYAOa+6JZdXKnu6BIimcH6ZujY44zTHitySKdP4Ud?= =?iso-8859-1?q?oaTD8G46wK0EYQosYGU2ikD09ZkBdW+C74mXw/qcnbkoEJ56lAxbVJFt+ZdD?= =?iso-8859-1?q?GJ8FmSMYdK/sX+uU/yTEexNI4DS0NLcrySsg9wU0u/vAch9s4zNufZI5KmBZ?= =?iso-8859-1?q?YiUe5lG1EvsKYUcRYBMlnBu9J/xv+kmnoyyof3nyiCTqG4o1d2avpXW8kAzN?= =?iso-8859-1?q?GKBdGnm5CTyTc2uVrS5RWlDTyJ7kybpSfE6IXDhoc3s1WwnHCHOX2NSq2QoG?= =?iso-8859-1?q?HRvW3wFFArKUxSP3Fw9xmQin2audxSvUZrMb8g+75774t9ZmpISYQbw4653q?= =?iso-8859-1?q?et/6Y42Ld90ziF4fX48Sw/Txswu6noAplmfU5AMvFLtN0gkmyxDcysNpjOA4?= =?iso-8859-1?q?Hpg4tZrZeqXpAdbcJyiNvgM67TXWHdqidgmO7xOfZIuJbJgB76RzQ39p+Kry?= =?iso-8859-1?q?CKFcnfnn5MvBBt3gI1wiQd/NHx3QY6/0AURDPKRUDZP553ZWzjVb5S6rJJPs?= =?iso-8859-1?q?E3ma5qQLBdR5g5diVseLMCYGzergH8PR3SOrIHqXTl/Luhwxc5icmse4JsVn?= =?iso-8859-1?q?BM3iSBaj6ARq2M6+CpzhV/r3b4unR1FV3e0FVapUFztwW3z6/WeajHDGb81c?= =?iso-8859-1?q?H2Lq/3sf7+w5CbSbd2vAInMSrVJKjYfONYk5GKI2+ijCsSGPyXuTcWPDXYRB?= =?iso-8859-1?q?gCXQqsDtYg31IDLM+MnU+HdQGkFeEwLgr+MXNkF82ni8JWSwDGcZzLWKoRMa?= =?iso-8859-1?q?VclRILjpklvDkjt8OlUUlxdSVsFTgAnJ6ZigtfVYGC49149w5nj7j1T2TqVk?= =?iso-8859-1?q?cJT9X5w22UpWm/LSG9nRYBfqQV/ehLZS3e7HZzQmwj2XRbMBvpDATFrYnZUG?= =?iso-8859-1?q?o54UQUq5XVtf2TrZJZPRUe+QX3F7vHxCkXbkw8am6h217lCmAqtuhKUnRqMU?= =?iso-8859-1?q?1bWY5FO1LkNRS3n7aBsqnS/S6gXvhdyYUrSMonWWD4vwSxl1xLSkR2V3dwyF?= =?iso-8859-1?q?Iw602ohVV37wZjKIHzi2O3KxwsNlLX15xRJMDQ2ZUYe+R1pEfEgj1GuHfaz5?= =?iso-8859-1?q?j07JyjzylYvdSl7lRcoLBsqzkgGEoYt8PkevGbdPWuELDl+vOslxoDsmOVNR?= =?iso-8859-1?q?uu3eUQhfsHx25XhGPXghbIpGoX+KTxPWdzICUrToPmLtjJpbvLtzMn0MJCCt?= =?iso-8859-1?q?62AmY6YnruguGOmfsSWVxi/QEY3L4qgj366+G43Y8oKc69srNr6YV08uuIfP?= =?iso-8859-1?q?Jt7ufeUNAW5FzTyVbqI1PB/UO9P3V?= MIME-Version: 1.0 X-OriginatorOrg: fb.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SJ0PR15MB5154.namprd15.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36ddf96c-6815-46c6-2df8-08da2c90d376 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 May 2022 23:09:36.0372 (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: akjbHCtb8h1hZOoU+vUZl3ZwDIrWDIw6Q6CepF+oy7drsD2j9g2AY5Mo4a8kqvzJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR15MB1903 X-Proofpoint-GUID: Zoy16mtMueSGGrAulwo_-NPxBkHxmloa X-Proofpoint-ORIG-GUID: Zoy16mtMueSGGrAulwo_-NPxBkHxmloa X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-02_08,2022-05-02_03,2022-02-23_01 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net uprobe and kprobe programs have the same program type, KPROBE, which is currently not allowed to load sleepable programs. To avoid adding a new UPROBE type, we instead allow sleepable KPROBE programs to load and defer the is-it-actually-a-uprobe-program check to attachment time, where we're already validating the corresponding perf_event. A corollary of this patch is that you can now load a sleepable kprobe program but cannot attach it. Signed-off-by: Delyan Kratunov Acked-by: Andrii Nakryiko --- kernel/bpf/verifier.c | 4 ++-- kernel/events/core.c | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 813f6ee80419..82b37a1a8a33 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14571,8 +14571,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) } if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && - prog->type != BPF_PROG_TYPE_LSM) { - verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); + prog->type != BPF_PROG_TYPE_LSM && prog->type != BPF_PROG_TYPE_KPROBE) { + verbose(env, "Only fentry/fexit/fmod_ret, lsm, and kprobe/uprobe programs can be sleepable\n"); return -EINVAL; } diff --git a/kernel/events/core.c b/kernel/events/core.c index 23bb19716ad3..bd09c4152a3b 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10069,26 +10069,30 @@ static inline bool perf_event_is_tracing(struct perf_event *event) int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie) { - bool is_kprobe, is_tracepoint, is_syscall_tp; + bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp; if (!perf_event_is_tracing(event)) return perf_event_set_bpf_handler(event, prog, bpf_cookie); - is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; + is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; + is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; is_syscall_tp = is_syscall_trace_event(event->tp_event); - if (!is_kprobe && !is_tracepoint && !is_syscall_tp) + if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp) /* bpf programs can only be attached to u/kprobe or tracepoint */ return -EINVAL; - if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || + if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) || (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) return -EINVAL; + if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable && !is_uprobe) + /* only uprobe programs are allowed to be sleepable */ + return -EINVAL; + /* Kprobe override only works for kprobes, not uprobes. */ - if (prog->kprobe_override && - !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) + if (prog->kprobe_override && !is_kprobe) return -EINVAL; if (is_tracepoint || is_syscall_tp) {