Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp631582pxb; Thu, 12 Nov 2020 12:12:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJxrkNZwX1ZFOw2v7uiaK3y4eKBCk+x8N/HGD8CSf82hn1Nu+HQ+QcH/MzXSceh8yj+qdem/ X-Received: by 2002:a17:906:1458:: with SMTP id q24mr1010910ejc.541.1605211964743; Thu, 12 Nov 2020 12:12:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605211964; cv=none; d=google.com; s=arc-20160816; b=iBtYZrO/dr/y6+ASPaD3R5djosri0T8oF2poIGTA0J2YNwZUNqUoU6uOVOjuPbguVG bmE/GVBb9iPKzHdD4uOFIZYUkP/eAUbPgaNAmhtYlcRfBo7B6yqSiMn5lIQ2upRniRqQ sL00vh22pdUoUH4qVl6tfBjVtnZSbY3s4x/hgTvF6Cfl4EV7zWu6k0W7L3P5giZoIRJw GDlRrUHlBBaZUYwOnQ9x1TBlwHF7g6YICIgePbhaYW4rfCxw39sp7H5c11ICaLr63G2E 7oKCbeLqXjI6Jkp40E9UlkeATbRRRmdUEA4Kw4QgrMFeN1OzQisO7LieOIec1M8ciMOE WWIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=y94nwo1CEggXD4knbaMbqIkUnYdaP7pApPBKsxsL/8U=; b=hdIF/S1raLMcfY88QAjHdzvGSrdBphyiLoJGwt2gusGVr8stdbgZ5T8cMA4QH3aubF +u7znRfU9rdwqTO9R9HKR+33AA/+OMhzri3SebULya+tMy3VRM57FxCaz0677pSYPjhV rxapJTuV8/I41mCiREawdTw6UX43bvuEPNWggqdymAia6zJduYElnTw1sdo06vehKwml I0rerhcleQQEzJVOKmwLHz0fYdJLX6epWqYESrUtDyDvEM+l40C9oD+rpdmvo0GWv0hf KVvWZT58bk7TzjymDJhMIa0aAhdg/EwDMVMSsjftkH0J2jCzB6J74IQNTJ0yxK1MKIMK XYvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ctMlC2wc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l30si4524799edl.99.2020.11.12.12.12.21; Thu, 12 Nov 2020 12:12:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ctMlC2wc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727138AbgKLUKJ (ORCPT + 99 others); Thu, 12 Nov 2020 15:10:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727097AbgKLUKJ (ORCPT ); Thu, 12 Nov 2020 15:10:09 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27370C0613D1 for ; Thu, 12 Nov 2020 12:10:09 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id l1so7334416wrb.9 for ; Thu, 12 Nov 2020 12:10:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=y94nwo1CEggXD4knbaMbqIkUnYdaP7pApPBKsxsL/8U=; b=ctMlC2wcU5DQ1hCfy3zQObIY743O1hB6k3yheXjapLwE3074ggej2sU/Pg7B3yVv8V 8CvceXjn5sQ8lx1kDb1Hqtxd0azQf2J6E12jPzEchymNcLT2o+wvmTGDpxiRlf0Wy3uF FxGNLgnOXfuTCk5s4n8tYG/ND11qxSr5RcVZU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=y94nwo1CEggXD4knbaMbqIkUnYdaP7pApPBKsxsL/8U=; b=o+5aujZ/q2gJMXGj/t4T+FLCXwc1pWC22q0HWo/HRu3ZK3ANxdoVCDCNEtJq4WPyqJ TkBAmSe2WhMq4NXrAsfn23s53WQfJfQDDdoxKxWVG0wUOgtKfMQozYjEr3iF0GCZqe6Z 87MXJIcdSZD8EnCaUOFmxELz74TQZJ5o1TyruJJ8ymyUqKhn6vrxnWo3wFb2r+lPAZa1 4/Dw51sLPrnc/7BgN6aa2yLzGWqrKv4GCD6+oqDP/kZnNKDW6A0eUAAsZSlY8EDH+6oC CPjYqGW+s522JnqteYHCI5vBzB4A6SEc+obawNlZfnPBHXFSmWQYp/o+mggVGrfg7IZ+ BIcg== X-Gm-Message-State: AOAM5336Rnu+hJbf8TGkCsEv92OQ9Nk2an1h3qw1pFKdyt9hzL7koYLh A1DqTRqOmpJxIWlTButv/pvlXQ== X-Received: by 2002:a5d:548b:: with SMTP id h11mr1461329wrv.306.1605211807921; Thu, 12 Nov 2020 12:10:07 -0800 (PST) Received: from revest.zrh.corp.google.com ([2a00:79e0:42:204:f693:9fff:fef4:a569]) by smtp.gmail.com with ESMTPSA id g138sm7767970wme.39.2020.11.12.12.10.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Nov 2020 12:10:07 -0800 (PST) From: Florent Revest To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, kafai@fb.com, yhs@fb.com, andrii@kernel.org, kpsingh@chromium.org, jackmanb@chromium.org, linux-kernel@vger.kernel.org, Florent Revest Subject: [PATCH] bpf: Expose a bpf_sock_from_file helper to tracing programs Date: Thu, 12 Nov 2020 21:09:44 +0100 Message-Id: <20201112200944.2726451-1-revest@chromium.org> X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Florent Revest eBPF programs can already check whether a file is a socket using file->f_op == &socket_file_ops but they can not convert file->private_data into a struct socket with BTF information. For that, we need a new helper that is essentially just a wrapper for sock_from_file. sock_from_file can set an err value but this is only set to -ENOTSOCK when the return value is NULL so it's useless superfluous information. Signed-off-by: Florent Revest --- include/uapi/linux/bpf.h | 7 +++++++ kernel/trace/bpf_trace.c | 22 ++++++++++++++++++++++ scripts/bpf_helpers_doc.py | 4 ++++ tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 40 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 162999b12790..6c96bf9c1f94 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -3787,6 +3787,12 @@ union bpf_attr { * *ARG_PTR_TO_BTF_ID* of type *task_struct*. * Return * Pointer to the current task. + * + * struct socket *bpf_sock_from_file(struct file *file) + * Description + * If the given file contains a socket, returns the associated socket. + * Return + * A pointer to a struct socket on success, or NULL on failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3948,6 +3954,7 @@ union bpf_attr { FN(task_storage_get), \ FN(task_storage_delete), \ FN(get_current_task_btf), \ + FN(sock_from_file), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 3530120fa280..d040d3ec8313 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1255,6 +1255,26 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_sock_from_file, struct file *, file) +{ + int err; + + return (unsigned long) sock_from_file(file, &err); +} + +BTF_ID_LIST(bpf_sock_from_file_btf_ids) +BTF_ID(struct, socket) +BTF_ID(struct, file) + +const struct bpf_func_proto bpf_sock_from_file_proto = { + .func = bpf_sock_from_file, + .gpl_only = true, + .ret_type = RET_PTR_TO_BTF_ID_OR_NULL, + .ret_btf_id = &bpf_sock_from_file_btf_ids[0], + .arg1_type = ARG_PTR_TO_BTF_ID, + .arg1_btf_id = &bpf_sock_from_file_btf_ids[1], +}; + const struct bpf_func_proto * bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1349,6 +1369,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_per_cpu_ptr_proto; case BPF_FUNC_bpf_this_cpu_ptr: return &bpf_this_cpu_ptr_proto; + case BPF_FUNC_sock_from_file: + return &bpf_sock_from_file_proto; default: return NULL; } diff --git a/scripts/bpf_helpers_doc.py b/scripts/bpf_helpers_doc.py index 6769caae142f..99068ec40315 100755 --- a/scripts/bpf_helpers_doc.py +++ b/scripts/bpf_helpers_doc.py @@ -434,6 +434,8 @@ class PrinterHelpers(Printer): 'struct xdp_md', 'struct path', 'struct btf_ptr', + 'struct socket', + 'struct file', ] known_types = { '...', @@ -477,6 +479,8 @@ class PrinterHelpers(Printer): 'struct task_struct', 'struct path', 'struct btf_ptr', + 'struct socket', + 'struct file', } mapped_types = { 'u8': '__u8', diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 162999b12790..6c96bf9c1f94 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -3787,6 +3787,12 @@ union bpf_attr { * *ARG_PTR_TO_BTF_ID* of type *task_struct*. * Return * Pointer to the current task. + * + * struct socket *bpf_sock_from_file(struct file *file) + * Description + * If the given file contains a socket, returns the associated socket. + * Return + * A pointer to a struct socket on success, or NULL on failure. */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -3948,6 +3954,7 @@ union bpf_attr { FN(task_storage_get), \ FN(task_storage_delete), \ FN(get_current_task_btf), \ + FN(sock_from_file), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper -- 2.29.2.222.g5d2a92d10f8-goog