From patchwork Tue Jul 23 01:28:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13739269 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (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 4CBC9130AC8; Tue, 23 Jul 2024 01:28:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721698127; cv=none; b=cHF16ncLRr+Cvzwxt2r/1nCJZTwjNgKXcVnZYJcNLiPhrXIry8APWJyhQYM1c861QZA3ZBROEMnlFEWY5jDNy2shaaEPqh5DS5/gD1ntB4NJn+kfvgeo+N/vK0lo+1Dfmx+/yV36+ECGSi6lLT1VMERTk1FvUfO+BaKhcgJxsaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721698127; c=relaxed/simple; bh=SQ2IEf6uLbp6ip3VzFb1HBH6eJI/KPdxvaQkO5d7/N4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KoDposVd6xDO2b6vCv11ralbMkmPYIHo8H/WhHWJvUELwcWmPzkYOB2bP/Mf8rbZzEng2d8xEDUtw3TgCo9vkWVnpXnROTi2rrlHFOE0cBDtGkM8uI3R9s4YywqSNqJtHzmL46Mz9fHyrBWa4ZP76OWS6hj1DOnK4daK72HuXp8= 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=kAzNvmQh; arc=none smtp.client-ip=209.85.208.180 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="kAzNvmQh" Received: by mail-lj1-f180.google.com with SMTP id 38308e7fff4ca-2eecd2c6432so72488501fa.3; Mon, 22 Jul 2024 18:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721698123; x=1722302923; 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=4neOAOvc/P0MwJqYTyLb2Lt2JFbT6AmM7NbvKiw+YxI=; b=kAzNvmQhWpIigg/UZDCXisK2hZLzuBxOS/IWc0hu2x+ofkJKjl3TV7246fli50T+x4 XBWbxjURcJCRIYM6Owhg3K/0nFYwF1JQm0hDqabYz3s0wrRvKF1ugpSssbe8Fsy7kX7x w5voVcPiMjbIVln9cDfaNLTJxoa8GhWZK4wF820X1Hpe0zxAjBEdLhVYrEHxDbpb3OaT rBurU+0wys94SrYyy9eC6Edj2XJJGErPrQsMPngIph54jMBwmSc5GnC/PUo2ARmPMcnD NQTeq6OMzltnaTQqFtCWK0bTCYtT6xNov4OZ1PfX9ekfAjuxZSm1YGMO1pmknS8jKcVR oyag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721698123; x=1722302923; 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=4neOAOvc/P0MwJqYTyLb2Lt2JFbT6AmM7NbvKiw+YxI=; b=jIaGjQ0gqxhZxLydMHFk5PRduxe+PucU0NBU4zgHHQwqAQqajN82qQH1ACxyey/GfR MHlUy6urE431K5m5UzL+mdGDv8vhd8JLGq2n5REHpjieM2ZRaG9aPW1WW6EqBeSZT9kv zUxgETbMfpZppsn9x7ixOCxxYo8GbcZeGgrQ+8DDjAf7DWUYk33JlpsNX5iXgrkaVmO4 uZw6ZvSYEaesmClf/cps4/Y6x1hCFvDiIONQ6smm5HdQJ5MuHpREpo4Vbh2qSl2qHiYY Y/+tlwFG12w9Hml/pcCPZqmFITvS7exgyaBje9DLYHRBBA9d7KTxXAeZoaY07yvFPiQP TZiA== X-Forwarded-Encrypted: i=1; AJvYcCV61in7AIJHeO2Cx/zbUhQNohqpKBxkndP3OYDavsybOQTHheFoyiq0/i3k2WJK2jeGHvC23oR9gcloEWkkREcxrUZlh+Enr2mBkzJzrQonaSV0J7Kfb/kFAfPbLiLd8bTM3N0EefF6 X-Gm-Message-State: AOJu0Yy+F122ak56BzSsY0xUHeMmJbvMvUxVAbSmnagGmnIQSOHADZzn JtWCnz/IMq5WhulPhnWSsDwsz8JAHjnp2i1iS0tVop5wS9qSV7Jk X-Google-Smtp-Source: AGHT+IGdZz22bCtlmaWj0785V4Ked6hpxrQ8/xVksAey/DHb1sLF5mQABKu/Nk03eDuJlaMebbr8zQ== X-Received: by 2002:a2e:84ca:0:b0:2ee:8f3d:e68d with SMTP id 38308e7fff4ca-2ef1685d099mr78962171fa.44.1721698123225; Mon, 22 Jul 2024 18:28:43 -0700 (PDT) Received: from lenovo.teknoraver.net (net-93-66-31-10.cust.vodafonedsl.it. [93.66.31.10]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7a8f3a47d2sm31435366b.81.2024.07.22.18.28.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 18:28:42 -0700 (PDT) From: technoboy85@gmail.com To: 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 v2 1/2] bpf: enable generic kfuncs for BPF_CGROUP_* programs Date: Tue, 23 Jul 2024 03:28:26 +0200 Message-ID: <20240723012827.13280-2-technoboy85@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723012827.13280-1-technoboy85@gmail.com> References: <20240723012827.13280-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. Signed-off-by: Matteo Croce Acked-by: Andrii Nakryiko --- kernel/bpf/helpers.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index b5f0adae8293..23b782641077 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -3051,6 +3051,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 Jul 23 01:28:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 13739270 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 049CA1369AE; Tue, 23 Jul 2024 01:28:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721698137; cv=none; b=JmBXAfvsIdU9a5rn0NHAYJl84velw0Hb563SOogv+9q6qYStidPnN75k7fS+KLiVwkYn9kANOqIWoAAurySHeb8CwX/6bJ02t8nG7JF8Zd+BCev+yeyLPWHfdeEHpPJVLH7Dw01OB6BJGtMYtSNDIKh8H75+0cUHtJ/tniJR9Qk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721698137; c=relaxed/simple; bh=VLL35boxqFuOc40XPfXlJjwMZlRdjvQpA8ziQfPKO5E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R+tl2ZnbG5duaCrTNrwcqfZlmEtBm1JCLNROby6Nb7BJqERVuSinwcqYm8s1ftvOtM8DoVlv4ggcftLB7d8PSX77f4cGpgK/CwSaEIl9cEp39nqUnk26ZwRkAFrjCZze38cNLSrU+uyObTV8b8owcykkTmwPy+hfuhVTuWXuxdc= 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=cs6NL04t; arc=none smtp.client-ip=209.85.128.54 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="cs6NL04t" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4272738eb9eso36931235e9.3; Mon, 22 Jul 2024 18:28:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721698134; x=1722302934; 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=0JpvM6gskhIkYk6iEhzrjUnhABg12qwE+NcmnGPaHaU=; b=cs6NL04tYGP+UTf9Cr7s+AD5tNfJOV0E5uH25r7MBtjanEzsdlsRAb9dma4n4sDoNF X08bKRV1r8E2oLRXBkAkxI+V4/CG9k7ufhFRO+HwGe3OYMzPftRJY6nWoBYDPTmVmNuQ Hj8o1omGXxB3+23N35plTdLMgA3LEfo5A3vUL/lp5jfihwfBGL3dcg6GAJYQSpDfPv4B 7sQdfvyZ+LlgyqDdpLSA2HYbzGVHhJJLnuww58qaO+Js/1357RatCkiAf2t6E1dpNaPB 7b1qmUf5Ddn1/oZ1CTVQBGI8DbtKzMfrs/ysAp7kpR02ufJKGzV0ru2X0w7ZgSSJNAEY ciOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721698134; x=1722302934; 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=0JpvM6gskhIkYk6iEhzrjUnhABg12qwE+NcmnGPaHaU=; b=HiSnEN/COBdnPlf66Z/Qs76LVb7L8bAkr68hBZf62Opor3LVAnofTexvtc4/D2rcAn 9H2RtoPuWc7POV2RqG/JcP468RHIOgfWZorYUTnX7qHFjPNYQ/1I5ZO4WxeM0VVGtbkN MXzJhDlCKG2s+8Em+HvcVUifqHlFbE81pgsEhDIl+iMCnUNQDVnSx4834xr54WDjku5z qF4s5PBoa7MYJuB+T4foyWP6QMAKKTBObnEUyrkMOJBu/G5fFajpTY94R7+gzCAVy/VL beUeRIZ24a09PDmnB77QfMqAdm7vQ7uqB5Fu88AYSYQruPLPf+iWF9Fh0BI7vOKizzPV vqBQ== X-Forwarded-Encrypted: i=1; AJvYcCUW1jiGvduMkyWslAg/R7ok+cTbSi1CybIs4mYn4FjJnCY8G3JBCUEESgEziY8BHt7p+9Jvd9pAF3hY8uIffOQSKYprbMx9cF05R0Xp8/0K5P+AWOXF7Dv4Rh6MrSJDom0YLyrAab8O X-Gm-Message-State: AOJu0YyODHcqnp04InJHnEb+WvOnnqMqR7HQrxVlK0ccPI+CRB+6nDqO G7MTpp3YiCoBbOQ0GT39f0Apg3ZtO0Rzugiba1Ab9mrMSJPnn3UxzJ6Erg== X-Google-Smtp-Source: AGHT+IFJhpYdZ1TYDVvD5UDDmF+Iy8jQVDWtEy8GHnJDXw4rkaRFQqKQebTYHV3pM0whCnYhXUi/IQ== X-Received: by 2002:a05:6000:c49:b0:367:9522:5e6b with SMTP id ffacd0b85a97d-369dee4de69mr918088f8f.45.1721698133956; Mon, 22 Jul 2024 18:28:53 -0700 (PDT) Received: from lenovo.teknoraver.net (net-93-66-31-10.cust.vodafonedsl.it. [93.66.31.10]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7a8f3a47d2sm31435366b.81.2024.07.22.18.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 18:28:53 -0700 (PDT) From: technoboy85@gmail.com To: 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 v2 2/2] bpf: allow bpf_current_task_under_cgroup() with BPF_CGROUP_* Date: Tue, 23 Jul 2024 03:28:27 +0200 Message-ID: <20240723012827.13280-3-technoboy85@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240723012827.13280-1-technoboy85@gmail.com> References: <20240723012827.13280-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 Acked-by: Andrii Nakryiko --- include/linux/bpf.h | 1 + kernel/bpf/cgroup.c | 2 ++ kernel/bpf/helpers.c | 23 +++++++++++++++++++++++ kernel/trace/bpf_trace.c | 27 ++------------------------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4f1d4a97b9d1..4000fd161dda 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3188,6 +3188,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 23b782641077..eaa3ce14028a 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2457,6 +2457,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..ea5cdd122024 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; @@ -1548,8 +1525,6 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_get_numa_node_id_proto; case BPF_FUNC_perf_event_read: return &bpf_perf_event_read_proto; - case BPF_FUNC_current_task_under_cgroup: - return &bpf_current_task_under_cgroup_proto; case BPF_FUNC_get_prandom_u32: return &bpf_get_prandom_u32_proto; case BPF_FUNC_probe_write_user: @@ -1578,6 +1553,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_cgrp_storage_get_proto; case BPF_FUNC_cgrp_storage_delete: return &bpf_cgrp_storage_delete_proto; + case BPF_FUNC_current_task_under_cgroup: + return &bpf_current_task_under_cgroup_proto; #endif case BPF_FUNC_send_signal: return &bpf_send_signal_proto;