From patchwork Tue Aug 13 12:20:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13761934 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 9255917D8A6; Tue, 13 Aug 2024 12:21:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723551684; cv=none; b=tMeT6eax25v2O4M9todjMObivLkS1QCZqtrUMoSYrjhPrTYTNG1vPaDyBFw7/fqXLbRUZokNx7BbutkeULJJ3nTVPLcVtjH1WeiqEOpuJZeyscHA9EUEvaAbTXGmHoQXCoEffMZUt3HGcpSMh0lvWtHpjjI8QIZybHXWX/rW/tI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723551684; c=relaxed/simple; bh=lSxUx8QJC4EPhMBsEVy0vDjpMEg1x5VyoHxhEZZ5+ZQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Mjz8Oq5C1Jz6yo5fHWBiywNED+WruHAes8jmKZAsrcbH9+NMo6ZCsVIFIH4yBdhmrywvwKEC/DhzXKaurA1nWFkZjUAaEZgQ85Q0zEEXVpqVtJ9cd7fE8o/o7DnMtd/PFNEm+/WD524bsjcAMExTbdin8QZdagLcNhYAuMt5N5M= 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=Hxvv26L9; arc=none smtp.client-ip=209.85.208.43 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="Hxvv26L9" Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5bb8e62570fso6501856a12.1; Tue, 13 Aug 2024 05:21:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723551681; x=1724156481; 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=L3qivM8JDv5nFGvOqtv1aQ6yzpE06yGKDeScqPR2KII=; b=Hxvv26L9f+IIR1Ynfs3/WrBpkFh67OvdGzy96DjMcXJrRYLxUVWRVfZHrcsq4hw1g0 ARi06RlJloDde1Dw9vGd0b/RYeYmZzAAy1BtSUebvVuQSUiTCagiemTwrIhzi8FyYyED u71bmKNpMZBSAQXUV9HWOha/jgIh4dzfRA1wYIaSrqX5XWezIi6Xh0Y6b0u40H/x1wYC z36H2HHVekIlyiraQBrleSf8rDRLgb3UxOp4Ibi/ALFt8emnJNqZY7Cpoq5KwzuodX+E 5tRbFO4oXhtyhSNIRNz05zJ0GcZtNbh2ZnHhmei1A1M1yAdauinwT4GCY1SsT7VebDlX vKFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723551681; x=1724156481; 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=L3qivM8JDv5nFGvOqtv1aQ6yzpE06yGKDeScqPR2KII=; b=uBU5uR9OhOQw8Dl7j7Eoo+bXyo/Cx4HnNoRH5vkU2aJzPxKk7GB9plErIhyVLM/GeI KWBGsF2zvLEctsiXvvlYMTQbETAx3r6xp9lxed88s7AplLZcva5lmxEMY8hGjIlPwzqg A3RxJDE1Zqchg29ZdhIFBF3MrvkXwYwzX1vEYPLIDEcZK3xdFv6GODvq1pTZ5d2H5UJG 7pPRtX39zogODgSGYiEErYRsXCt59oAvR6zMg6ondnv1rmZFxvTv5vYQMUuLKT/pSqVL RzBdNQAZx5JV+bGbwO0QyFfis+ZzvaAWVach4XSNmPSBmaAkoZeY+UxJdf5/xik017iR OrsQ== X-Forwarded-Encrypted: i=1; AJvYcCUAMVi5EriBZ9etKR72/41hO4dcJEm+AB98Q0J5sXP6Vlk9c7YzuI1tV69UHYFh9PkoZR4=@vger.kernel.org, AJvYcCX+8phSJT4puPxOdczcU22yE23IrKkqh+ux80aybvHc9KSLotC13cuJl8j2t+buXg3oznJSV0O3EKkAqF5ntoJvp06I@vger.kernel.org X-Gm-Message-State: AOJu0YztwDduTqmq/u2B2OlAMBxakxHnbaCD4pehenF3CexnKFMqFQL4 CjJCAOucMASzPB043k7KkC3rtIR1qy/wSVYmQP1lm1nSAtjRNDwL5G82Ml6J X-Google-Smtp-Source: AGHT+IHPNXjvvrwsIrxty2hZgZxlI/dmhNdb2l6vEok6ER+BgSM0sWONVhLpWSV9CFoEgpXfmhRlvg== X-Received: by 2002:a17:907:5cb:b0:a7a:8cfb:655b with SMTP id a640c23a62f3a-a80ed2d48b0mr237778766b.64.1723551680665; Tue, 13 Aug 2024 05:21:20 -0700 (PDT) Received: from lenovo.homenet.telecomitalia.it (host-79-17-17-86.retail.telecomitalia.it. [79.17.17.86]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f41849cesm65358766b.199.2024.08.13.05.21.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 05:21:20 -0700 (PDT) From: Matteo Croce 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 Subject: [PATCH bpf-next v4 1/2] bpf: enable generic kfuncs for BPF_CGROUP_* programs Date: Tue, 13 Aug 2024 14:20:59 +0200 Message-ID: <20240813122100.181246-2-technoboy85@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240813122100.181246-1-technoboy85@gmail.com> References: <20240813122100.181246-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. In enum btf_kfunc_hook, rename BTF_KFUNC_HOOK_CGROUP_SKB to a more generic BTF_KFUNC_HOOK_CGROUP, since it's used for all the cgroup related program types. Signed-off-by: Matteo Croce --- kernel/bpf/btf.c | 8 ++++++-- kernel/bpf/helpers.c | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 95426d5b634e..08d094875f00 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -212,7 +212,7 @@ enum btf_kfunc_hook { BTF_KFUNC_HOOK_TRACING, BTF_KFUNC_HOOK_SYSCALL, BTF_KFUNC_HOOK_FMODRET, - BTF_KFUNC_HOOK_CGROUP_SKB, + BTF_KFUNC_HOOK_CGROUP, BTF_KFUNC_HOOK_SCHED_ACT, BTF_KFUNC_HOOK_SK_SKB, BTF_KFUNC_HOOK_SOCKET_FILTER, @@ -8312,8 +8312,12 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) case BPF_PROG_TYPE_SYSCALL: return BTF_KFUNC_HOOK_SYSCALL; case BPF_PROG_TYPE_CGROUP_SKB: + case BPF_PROG_TYPE_CGROUP_SOCK: + case BPF_PROG_TYPE_CGROUP_DEVICE: case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: - return BTF_KFUNC_HOOK_CGROUP_SKB; + case BPF_PROG_TYPE_CGROUP_SOCKOPT: + case BPF_PROG_TYPE_CGROUP_SYSCTL: + return BTF_KFUNC_HOOK_CGROUP; case BPF_PROG_TYPE_SCHED_ACT: return BTF_KFUNC_HOOK_SCHED_ACT; case BPF_PROG_TYPE_SK_SKB: 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 Tue Aug 13 12:21:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13761935 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (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 1D1611991C2; Tue, 13 Aug 2024 12:21:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723551698; cv=none; b=sLjVZ0H9FSINgTdh/40Mn1k1xm5Qdi0H+HSA2Z+i16XZqu3T/p8yNiEWlSPmXOVLJvED6ldQyTmXpo4QfcgeNekV0GGa1jzn6RUqrWAjgiMQgndRzKKkqiUthom0MscsDUIfklHtgO0q7rxHV65n+ZXEong5+SKLc27Xy/SL88Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723551698; c=relaxed/simple; bh=nul9oNFo5MlT8L54l8FRJ+VtGDNyl0aThkFSBNHbOKU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cpgAzrvW/KhUw1CJGs9+gqpUWjZDh+4U+xxPh+OWMoa70BQD/R+c+DHI60/uBYOueSXK4PDgBWJrB6l8z5iLh2lBXGGtWbhfRGiriz0k41War0Vb4QI17Wk/QKOrMq201dSsxLkXOzhvlO2nYbiZby8d45GAcTNc9meQC4f/wBU= 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=KWvRj6rZ; arc=none smtp.client-ip=209.85.218.41 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="KWvRj6rZ" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a7a9185e1c0so409703266b.1; Tue, 13 Aug 2024 05:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723551695; x=1724156495; 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=YKwcvjI1BwRV/PUGETAH1O0LycydQJQiwQrCJX2yD5s=; b=KWvRj6rZtWmFXTTBuD6pnETCNYE3rjbax2Qsf+vFxkjp+EgeWReVzWitEwUqrGE2Ui V1UhpZ7FpMVgcpUc7CkUvANXDWhag5m+7B9YiRaA2CAcae7pZ94KMfat9KHEk3oyY6o1 aPBlUMBoA+65u7q3I/+RAV37r46ljx35bxifrB7y4RqWTZBD6am4srwKnXgaqJMyIR84 48NlJi/HyeguPtMUxZWlSEXBMmSGYVPvALyNFNS9IMNT2Ljq1xH7bM2dbxBIhvbOPpi9 feW206L6vHpajrjNAiqBUpb1lgBQ7iMZBnSvzdJWVQjdHKm27h82EAtQiAeDc/pwRhVu ecQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723551695; x=1724156495; 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=YKwcvjI1BwRV/PUGETAH1O0LycydQJQiwQrCJX2yD5s=; b=tToZ3b/jdn7Pc/3AWgd3jQqMYHU7CXPlLG3MBRFQDL1Mz9GkaUk25uU/hVk/v+HN3w jdWhTu0YSt4DKv6U73OaSlIDyvzB82/MQbwvZNuweII8pvZictJ4bAAoawo4E4FLktTO d4KmKH+D9qQdTdx4r863HSphI/WxOP9Q236TkFuGx1f2Brtu7lNAkBg+8IsZd5LK6hMq o6GwOE+2Nme/PUTkvMtIy6+KddyT/kEZsLnEVpjPCEcapvurBi9L0xfakTXBo0jwa8my s/xFupezHwpYyQMTtXV7zN1CxIHDrdTmcTfrhhIA2GIp15/O+rJ+8ZykSRJYmfwGoDkJ rVYQ== X-Forwarded-Encrypted: i=1; AJvYcCWt703tcS+v7K5YbZPy5FsewBNm/79PQ3bw8PN4wW63ToZt4p9EiWmUz3aWoAXDEJfsEXhU20hSZpWT8lLapV9Lq3metX6cPN3vL1ZKLIBQpEf+XwZ+sIKqnWSi8upJZhA42di403DO X-Gm-Message-State: AOJu0YyspVef4hSkM6wEzpi/vq8aXXKe+Q3bEtNunjCgaCO91eD5Lq9T 0kaLedX5eD9IpVTpiDkPxNX0dmpKowFzPpQZpet82iFAe4ViTSeY X-Google-Smtp-Source: AGHT+IHSPYKEoBsb0fISxSo3sHBmT1rvSbClTaG3HWR9l0IPSzLQ0Fhf12Udo8xgjVy2f2Goy6qZoA== X-Received: by 2002:a17:907:9412:b0:a80:d64f:6734 with SMTP id a640c23a62f3a-a80ed2d256dmr230346766b.60.1723551695037; Tue, 13 Aug 2024 05:21:35 -0700 (PDT) Received: from lenovo.homenet.telecomitalia.it (host-79-17-17-86.retail.telecomitalia.it. [79.17.17.86]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a80f41849cesm65358766b.199.2024.08.13.05.21.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Aug 2024 05:21:34 -0700 (PDT) From: Matteo Croce 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 Subject: [PATCH bpf-next v4 2/2] bpf: allow bpf_current_task_under_cgroup() with BPF_CGROUP_* Date: Tue, 13 Aug 2024 14:21:00 +0200 Message-ID: <20240813122100.181246-3-technoboy85@gmail.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240813122100.181246-1-technoboy85@gmail.com> References: <20240813122100.181246-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 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 b9425e410bcb..f0192c173ed8 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3206,6 +3206,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 d557bb11e0ff..e4e1f5d8b2a6 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -797,29 +797,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;