From patchwork Tue Jun 14 23:10:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Delyan Kratunov X-Patchwork-Id: 12881696 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 5EEDBC43334 for ; Tue, 14 Jun 2022 23:10:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240106AbiFNXKz (ORCPT ); Tue, 14 Jun 2022 19:10:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240108AbiFNXKv (ORCPT ); Tue, 14 Jun 2022 19:10:51 -0400 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA8C652B0D for ; Tue, 14 Jun 2022 16:10:46 -0700 (PDT) Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 25EMcqrw006248 for ; Tue, 14 Jun 2022 16:10:46 -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=VZNTSQJbvvTtEpRIh+e++fhF/w6C3GTaVRh+vwDGskA=; b=EzbzSXeUCQedV6h9bomitBcS8SEcbZqDGbctIe00EUsRag8JNrSIvnP4wy6Z8XSsMP+C ufSPiubadkcVzTcmt7piWlU8X+awwIHu0nZVYebnVJicd2kjoqbItzmDrzGTlHG0GeCw hpbcR34UyI5RGypAsjzfJNZ5bAZ/7YbQxc8= Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2169.outbound.protection.outlook.com [104.47.59.169]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3gpht16dwf-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 14 Jun 2022 16:10:46 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AQEUYzG52OJcFaBP0cz22sxBM/2l3ESg9wvGl3bn+wcq9vTKRwv7jukrKfZFH0egINjM0gZkDaZwXxk3KRaCxDP0k8WJN7Gx9MalW2pSQOSpngd1ot7FSv1XocQ/5z8Mwvr3Imck0TDUmqTf1tHADw76FELHaaqu789C+YU+YG3Ac2xSRmGhGV6ItFqG33t1MwDe2NWeQu3qBVuf1gCFQl6rzfwqr8zae9UNTUBCU7P22z0f5WwN7/W+fIDxTDJleX2drHgShyJCoj/CBQQMTOlCFjkwdb1VqbvzPrnfuzaBctaVvn/4sYZN2TuyOUyVl/0+2AgnZvZCekFB6wDAJQ== 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=VZNTSQJbvvTtEpRIh+e++fhF/w6C3GTaVRh+vwDGskA=; b=X5Adqx+eWx8TqC6/6RLlF/SfZMNZ8nDqsb9xFdnvM78XJN5nhWoH6DozYl4a9rZf24xoie9qe80cwloX+zwBRETaAE2kk7wwFuxdqMZrf74zVOHZDkZr93G/DnODdbCKA3D6UKzXGy9MNFuwkwF3ShqJuem5uu2quzH+HNogx8kKJFyvCTJOoZAbSxV3b2e0bqO2tl42cwwBOqrZvuO3ZrtU8a2uWXZSeYjK21tLXoDTpKD//fbL0RU1MV1k4Kjgar1XFeFWIKUzygjXdwPcg3HMGXX0FhKY/sTf2+guQd9pJ2zPEP273P58c2QP+vB04llze9UrZNSggJcBcgNt3g== 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 MW5PR15MB5220.namprd15.prod.outlook.com (2603:10b6:303:1a0::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.20; Tue, 14 Jun 2022 23:10:43 +0000 Received: from SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::8910:e73d:9868:600b]) by SJ0PR15MB5154.namprd15.prod.outlook.com ([fe80::8910:e73d:9868:600b%9]) with mapi id 15.20.5332.016; Tue, 14 Jun 2022 23:10:43 +0000 From: Delyan Kratunov To: "daniel@iogearbox.net" , "ast@kernel.org" , "andrii@kernel.org" , "bpf@vger.kernel.org" Subject: [PATCH bpf-next v4 3/5] bpf: allow sleepable uprobe programs to attach Thread-Topic: [PATCH bpf-next v4 3/5] bpf: allow sleepable uprobe programs to attach Thread-Index: AQHYgEP56LrMDqiRekeDa7Qk18mVcg== Date: Tue, 14 Jun 2022 23:10:43 +0000 Message-ID: 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: 48f7cb0b-22e1-465e-4d5f-08da4e5b1b8d x-ms-traffictypediagnostic: MW5PR15MB5220: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: ZRlLaWa27XK/+/WfVySwsVuNleWGi8KjHNW28jiu2/eN+WdjvyK0YzOpapt612tzTxPesFtw2CuowH1ruGGeMQEuIIQVqtHNat/5W5hf+BhHehlg7ohPctt7dh/xGxWiRRro/7anAAT6gi+qjDxj46FSOcIs9I+RMQS1WhYE7ARZA93dZSLgLYvSEEiOHbi/2bZUVuje72dU4aC2tQldXqk6wadPbrJJcFUVDkDP+2R5heUSrg2oWSAcuvapoHYbytMHN7Gm0veCmOaw9pxwzUsajUohJiL0wNzEWXPo/pyQ1ICDk85SXNVTa7Wh6JEJxu7lheVNG3y7miIU9dXJ9G43zOvxXVz+zeQ/LzSG/P9QqwNaYJi/3NUvlygQPQqScOphJCHmMn1GQUBuhcGgDRn0TFiiLp9fbtTZm96//L5F9qe9/R6YdVr8rU4EXNpk9Hz34apnYutD81X1sIhenZV6ijHaAm6ttDBbiCvfuR4fNdpPgr7V4IaYHvACSFOf5dXQSFGJ0nGAAzV023JmEecNa0NClTw5DC/jzFyzkPbvIcLKe5AI1NwhTv9pVVJm+k2XEkapvM5kmPVopVUqlFPPMkXDGfQAMd1/graQk+5YBFnFyUP3dhfwphYD/tgJ9aN4uKSnkz5ehW5orHNKFXJBAhucq77Anoz5Ymn+wbZf1IPIrFRKu+Vtg4dMlamLn4jXJaWo6jujPs8wUkSg5w== 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:(13230016)(4636009)(366004)(186003)(8936002)(5660300002)(122000001)(38100700002)(83380400001)(6506007)(6486002)(36756003)(508600001)(38070700005)(6512007)(71200400001)(66946007)(316002)(66476007)(2616005)(2906002)(64756008)(66446008)(76116006)(8676002)(91956017)(66556008)(86362001)(110136005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?0Za+tulDjfcIAN1tc/FsBG4?= =?iso-8859-1?q?3LAXn6ZHvu+RrUJMjdcYA/slpEbV1sCdEHrVJM0OaxvNCTEPLAwTorXwkgZd?= =?iso-8859-1?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-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: 48f7cb0b-22e1-465e-4d5f-08da4e5b1b8d X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Jun 2022 23:10:43.7269 (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: 0SNOo9ii3AhpYV/IRBznKQLOqeeycfjm2Cu97FeagusG3HHTL13W1IuM7qowA/Y/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR15MB5220 X-Proofpoint-GUID: HE9qvUEyOt2qPn2Bp-6bDALoLGOYCC0J X-Proofpoint-ORIG-GUID: HE9qvUEyOt2qPn2Bp-6bDALoLGOYCC0J X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.874,Hydra:6.0.517,FMLib:17.11.64.514 definitions=2022-06-14_10,2022-06-13_01,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, instead allow sleepable KPROBE programs to load and defer the is-it-actually-a-uprobe-program check to attachment time, where there's already validation of the corresponding perf_event. A corollary of this patch is that you can now load a sleepable kprobe program but cannot attach it. Acked-by: Andrii Nakryiko Signed-off-by: Delyan Kratunov --- 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 2d2872682278..eadc23a8452c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14829,8 +14829,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 950b25c3f210..deee6815bdd3 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) {