From patchwork Thu Jul 25 00:14:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13741470 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 777C233CA; Thu, 25 Jul 2024 00:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721866467; cv=none; b=LSfc22O9/6NAKRSy+28rp/f6TzdL8ARHI4AZLwmZ0gWLHoEjCcacrpjWrR39g/FqVvPoVBhNZnhlMwCrb1Ky0JyRsQodTTGxP9I3jIWuRVA2kFvfgDutJXX2bHWXKGEtOHIh3BpVH6m4aaso4/iWwpnP4OopTq3xshBu9VSRFGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721866467; c=relaxed/simple; bh=GfmyybgoFneffmyq/GmNA49FTZZjHne2dqt2E61Y14g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WMkyEylVcVZnlGojXlOY/j2El7bVpxSQW1W7ru6O1p6Rc9BMMW0hq/Ic1HuAhzI6w6zWwJMq+Y/AZaGArvzOcrP6oU0+gZ6F5iU94V5UxFVtCmrbuQMJmC83cVpf0uMs8rN0/s3pdcxtor/LGoNJ6+LJwL495PVZP2mKOMafMWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JtxTbZ6H; arc=none smtp.client-ip=209.85.218.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JtxTbZ6H" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-a7979c3ffb1so175566b.2; Wed, 24 Jul 2024 17:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721866464; x=1722471264; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/eG/lRRjkSAeWsEyW9O4j6uH4ueqTUceCM5jXvGupoY=; b=JtxTbZ6HieAPTopuD3HvT6fM95NnBQM4fUXZ0pKwZk5vsPzul8GxpzMzlIF1d9+uCp t5a0E/Xn75wrXdh+sJZQzwmZSCNkxpH6t/57NJ+ggdaT5x98hLsiBlYLbfE/8FdSXSNu SG3xD/GfjSDCqGNzUtCteWjmUuZgI8Z9fYqYSGzdYBpydXzxowSHDuYVopMCmH1JXgpq P1fSxECyNurAFq+sPXULzzjn2s2VH2zo9E43vrH1kQmXrkJjyv3mzNwWgC7pbrrDcA4e s6LjQ7fbZBfejGmZBt1g3Yc8tV+4r9Z+Oo/k57sKPKA1SBIxwl7rNeCYLydi/YasF+ic zMAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721866464; x=1722471264; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/eG/lRRjkSAeWsEyW9O4j6uH4ueqTUceCM5jXvGupoY=; b=hBNSidtCyWFySmZeNJ+bZxExkXoE1tpz6sQEo3jYmPHLxl3P9YF+U/Px6vcL+/Fzfw tAblmIO/bRr91k46ORF1Dc2Ko2IQG075u4eIHxRvOKKLxya5lgneoME2M3CS/oJtcz52 WGYzcNQIbaI5Q4CZVF8TJ1FZPX4t956h/fcHnlE/6BRRBEb/jrnGx8ffKOplAoxnGpiM NeQD4Pm82VvaA1fly6aQZEHatGNCMQBOdJfJ8tizzJSI9jnRIBcKTgFxvv6LP5UAefLj T6J6df/hvLvAYPjAzBiAbZ67o/ThDU3fKbD+ZuzX1tVsomE+N506cy9gaLcydrStggp6 drAQ== X-Forwarded-Encrypted: i=1; AJvYcCXk2Fo7IUa0tkITo4d6A7HVbHtDriD0OMMz78VYJ03WCo+5ci4pAiwtLa5mlRGkM2zL53Cg/UjeGKTpt0G4FpcCIHtCHYD0VxPCCb/pZVMsDc5ZgDw5+rQdc0dLcdOQ3Ap5FPY1gTKI X-Gm-Message-State: AOJu0Yx9UyPuX39nqRqZzod6vmjI4iDv+Cxvorx1A1fbiNaSbEIOhNLg C4wQfd8VUwyxOIvlKOz+EBbFGNvR1v6seTq9nLV3hUUZ7pWMqCjo X-Google-Smtp-Source: AGHT+IGozouLG2EpJhEHUQM4qO07EZGmIH0ARDqjTUpzMJNK1GlInNeZXI7G76ixpIfzP3Pn+DX4cg== X-Received: by 2002:a50:bb49:0:b0:5a4:5df5:12ed with SMTP id 4fb4d7f45d1cf-5ac6396b261mr251094a12.29.1721866463753; Wed, 24 Jul 2024 17:14:23 -0700 (PDT) Received: from teknoraver-mbp.homenet.telecomitalia.it (host-95-232-233-251.retail.telecomitalia.it. [95.232.233.251]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac64eb3a18sm164988a12.75.2024.07.24.17.14.22 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 24 Jul 2024 17:14:23 -0700 (PDT) From: technoboy85@gmail.com To: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Steven Rostedt , Masami Hiramatsu , bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Matteo Croce , Andrii Nakryiko Subject: [PATCH bpf-next v3 1/2] bpf: enable generic kfuncs for BPF_CGROUP_* programs Date: Thu, 25 Jul 2024 02:14:10 +0200 Message-ID: <20240725001411.39614-2-technoboy85@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240725001411.39614-1-technoboy85@gmail.com> References: <20240725001411.39614-1-technoboy85@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Matteo Croce These kfuncs are enabled even in BPF_PROG_TYPE_TRACING, so they should be safe also in BPF_CGROUP_* programs. Acked-by: Andrii Nakryiko Signed-off-by: Matteo Croce --- kernel/bpf/helpers.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index d02ae323996b..0d1d97d968b0 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3052,6 +3052,12 @@ static int __init kfunc_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_DEVICE, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SYSCTL, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT, &generic_kfunc_set); ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, ARRAY_SIZE(generic_dtors), THIS_MODULE); From patchwork Thu Jul 25 00:14:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13741471 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 9D462187F; Thu, 25 Jul 2024 00:14:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721866476; cv=none; b=NZ1CMJbxmLVrAvNzYD68Lz3z0dODT8dhvACFiuhP1jh68sDSIALelRk9tb0szsylS6CQCJ3c/pm1EI1C3KfH2hpOi9L6qXQhK6rqphUMHAQepIgy++/o2xhbSYHO8JL6e7Fu7VYZoFnNOdtrHRObWmv0xThPqIIdHhtad0CjYcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721866476; c=relaxed/simple; bh=UOdItoIE5F1gGb1ybJBcqAUzHmgYuNmDlGm3IUv5vrs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uIcSAsSjtmHjjI78pODlqTM8Hf9JBGUfC5aVH+A5PyMLrgkETAkxECEo0TgTMGxKgERrmzTL6g36z/saEyeXBzavbrE4RnnMEAxZy/2bwoEGVCIxKskJE27nmSSWqEvqcveOweVillZO38les2QSwyLd5yZ9gbDYH9BnF+eyyGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=A90o+xRS; arc=none smtp.client-ip=209.85.208.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A90o+xRS" Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2ef2ed592f6so4015591fa.0; Wed, 24 Jul 2024 17:14:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721866473; x=1722471273; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tJT2GE9iwU5GFa7ZaE2sOFwWPOWEUUQTQrMSdEP0K0E=; b=A90o+xRS9yE10JBkFO4QP0266QKZshOaSL9yv8PC35kwH+0y5egN4gm3pr3lZD5NGI mkVfOHxHIRn+KUagLdl0f8zStMuRBwqst8UTSFuaBlnsMjzs1ueLlRwEziBhKlTrsskh dirUz9VIbwPFZka0yJ+zuDAEs7yqbjDnLYwV5TMYd0RPtGUtdDciRpGUTK/xb3TWQ8uC A5XWw3XZ2tgIJbCFDJ14/dpBO/Rvkm0zTXfqlTpSKgdv+UOKu1UF4zs/lUd1VsPFuMoY YVsFIgv5Fo2ADJ99k7zt9E7ocv7dtHzrHhJ7gH3TPIcCySIQptj9p7aU7QKnbsBLRpaf 3X6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721866473; x=1722471273; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tJT2GE9iwU5GFa7ZaE2sOFwWPOWEUUQTQrMSdEP0K0E=; b=HQd1Bs/oIMmMWKcTOcOQJGs74jFbPiDTk1ZTMQjiwXy2hN9HBJtL503muCfLuHzmSS 6W4owoLHHI1XA4IOO52XyqYAvihmiWBNpzk/c+0mhW16cLYn6pfFdKm/Pjjm4lzO/PC+ 7qreGy/Zb7dlHYtCGj9co9I+h7zFVfwt6VxxsqOFfGNR5SeAVXmxsFcNPNRVxt1/1bcc 48siTR+qpZiufR/73Ma7kHZ5AczZcJbBLTb587bKZLcdpfTlrqLz84z7X4JMP1L9lxKq nS5Cmcucs+RGHY5Hymoag+dQAeuJW/e+2EKZaRNo/WBxuzOswkBGvxWofSYtAblUWCrj 3FOQ== X-Forwarded-Encrypted: i=1; AJvYcCXAVVT0/2wNakWwh51yONrq0dkO1b5lWxlB/5tJSAktQ0lklkIjw2vh2qKC1ZvttY0ueqfG7s3H7Hl1gs5zMwFv3rTH85LEAC9ODkCqqrJ1T3jW9UbemJO+Jnav05BEfFgqu0zSD4C2 X-Gm-Message-State: AOJu0Yy3WQO1in+MDonYdk8Id3oW6sUkOH2S/bk6SDup41SuuF5HAZ3S Xo+CyM2XUmxhUkfme638gCvWF4/+u1+rORuOwrnHqyWzs3R1X2SV X-Google-Smtp-Source: AGHT+IE98euogaxLy09qa2oLk30qQbZkZEvJ4UtQHqkPO0oFkooAI40CvinmfGGYvNLbqVpU4byZuw== X-Received: by 2002:a2e:2ac5:0:b0:2ef:307a:9988 with SMTP id 38308e7fff4ca-2f03dbe94b6mr1118081fa.35.1721866472483; Wed, 24 Jul 2024 17:14:32 -0700 (PDT) Received: from teknoraver-mbp.homenet.telecomitalia.it (host-95-232-233-251.retail.telecomitalia.it. [95.232.233.251]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac64eb3a18sm164988a12.75.2024.07.24.17.14.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 24 Jul 2024 17:14:31 -0700 (PDT) From: technoboy85@gmail.com To: Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Steven Rostedt , Masami Hiramatsu , bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Matteo Croce , Andrii Nakryiko Subject: [PATCH bpf-next v3 2/2] bpf: allow bpf_current_task_under_cgroup() with BPF_CGROUP_* Date: Thu, 25 Jul 2024 02:14:11 +0200 Message-ID: <20240725001411.39614-3-technoboy85@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240725001411.39614-1-technoboy85@gmail.com> References: <20240725001411.39614-1-technoboy85@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net From: Matteo Croce The helper bpf_current_task_under_cgroup() currently is only allowed for tracing programs. Allow its usage also in the BPF_CGROUP_* program types. Move the code from kernel/trace/bpf_trace.c to kernel/bpf/helpers.c, so it compiles also without CONFIG_BPF_EVENTS. This will be used in systemd-networkd to monitor the sysctl writes, and filter it's own writes from others: https://github.com/systemd/systemd/pull/32212 Acked-by: Andrii Nakryiko Signed-off-by: Matteo Croce --- include/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 2 ++ kernel/bpf/helpers.c | 23 +++++++++++++++++++++++ kernel/trace/bpf_trace.c | 23 ----------------------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 7ad37cbdc815..3820fcf360b6 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3202,6 +3202,7 @@ extern const struct bpf_func_proto bpf_sock_hash_update_proto; extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto; extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto; extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto; +extern const struct bpf_func_proto bpf_current_task_under_cgroup_proto; extern const struct bpf_func_proto bpf_msg_redirect_hash_proto; extern const struct bpf_func_proto bpf_msg_redirect_map_proto; extern const struct bpf_func_proto bpf_sk_redirect_hash_proto; diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 8ba73042a239..e7113d700b87 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -2581,6 +2581,8 @@ cgroup_current_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_cgroup_classid: return &bpf_get_cgroup_classid_curr_proto; #endif + case BPF_FUNC_current_task_under_cgroup: + return &bpf_current_task_under_cgroup_proto; default: return NULL; } diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 0d1d97d968b0..8502cfed2926 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2458,6 +2458,29 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, return ret; } +BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) +{ + struct bpf_array *array = container_of(map, struct bpf_array, map); + struct cgroup *cgrp; + + if (unlikely(idx >= array->map.max_entries)) + return -E2BIG; + + cgrp = READ_ONCE(array->ptrs[idx]); + if (unlikely(!cgrp)) + return -EAGAIN; + + return task_under_cgroup_hierarchy(current, cgrp); +} + +const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { + .func = bpf_current_task_under_cgroup, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_ANYTHING, +}; + /** * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index cd098846e251..cc9301054f84 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -798,29 +798,6 @@ const struct bpf_func_proto bpf_task_pt_regs_proto = { .ret_btf_id = &bpf_task_pt_regs_ids[0], }; -BPF_CALL_2(bpf_current_task_under_cgroup, struct bpf_map *, map, u32, idx) -{ - struct bpf_array *array = container_of(map, struct bpf_array, map); - struct cgroup *cgrp; - - if (unlikely(idx >= array->map.max_entries)) - return -E2BIG; - - cgrp = READ_ONCE(array->ptrs[idx]); - if (unlikely(!cgrp)) - return -EAGAIN; - - return task_under_cgroup_hierarchy(current, cgrp); -} - -static const struct bpf_func_proto bpf_current_task_under_cgroup_proto = { - .func = bpf_current_task_under_cgroup, - .gpl_only = false, - .ret_type = RET_INTEGER, - .arg1_type = ARG_CONST_MAP_PTR, - .arg2_type = ARG_ANYTHING, -}; - struct send_signal_irq_work { struct irq_work irq_work; struct task_struct *task;