Received: by 10.223.164.202 with SMTP id h10csp1261325wrb; Fri, 17 Nov 2017 17:31:28 -0800 (PST) X-Google-Smtp-Source: AGs4zMYYDibxftDrOgPPGcT3urPOWr4bCfV0NDQi9z5P5dRtu+UPbn6JGljtzmC0p8Fx7ttSPt+f X-Received: by 10.84.149.102 with SMTP id b35mr6942704plh.331.1510968687922; Fri, 17 Nov 2017 17:31:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510968687; cv=none; d=google.com; s=arc-20160816; b=euEtAy2g6uQekMMyHDfnrS1Dh/CfXplKgA/oq5ahmimuFWwopvHyoPU4a8+oVqAB3G 8GmQzXZURND60itdAJInAw/UnS5xoGN0tLfRfr6kB5TOK7rZkmmjNgobvGhohzQ8P2D4 B3baMG9qsswPzArAng57p6bj/jXBQyvgBJX7ZZw5uhGMxFj0gXfHUfvaOKfMqhtpv92P Q6lVaIhMdt9fMzraRi71t+ofxDPTB3CvGUyTwvvMh0U3SdMnOzqFDMbWuNBvKabMUPhO v6qJd3z6Qti0iM+WVMn2EH4yHELu6GtGUJPpz8ju/PXKWUQZH1Pq59Ey7wbh/bwYqC01 Az3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=/xZ+xdxCmKjNGEQmfg98zrHbzr6GBRz/LJMbBzz29o0=; b=HvxTuqx64UKmmdXnkZSc0c7AHgZ7s9IIzRLoWE1iKxHRL/x6wjF9xzMHpu8ypJLePB 50K+5vrLu1IdjNP/0BRHGcYOCv5y4OXDyJo6W/PgSKHpdeoVUTdAItg3W1LXFBeEFmSh tYVmYPM+PmT6kim1Q+d1g9zV2o5G8OKKXKSGryl3WYC6P5rn64GwrFJCdBZpZtQ30nba 1W7He4LeJ2g3JOK6vrz0PkHoyQItQtycquR6c+FWcHcTWu5zRUQEqId/MB4X9C5XR9Sv eyjJM05nlP8i5wOl1IV+ie5ab8VWpGRdtNxIhkY6qcwJniBFgywBAy4REeHySJ8AjCtT 71PA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@toxicpanda-com.20150623.gappssmtp.com header.s=20150623 header.b=WWfpdja5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f3si3653403pgn.37.2017.11.17.17.31.15; Fri, 17 Nov 2017 17:31:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@toxicpanda-com.20150623.gappssmtp.com header.s=20150623 header.b=WWfpdja5; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760075AbdKQSBs (ORCPT + 93 others); Fri, 17 Nov 2017 13:01:48 -0500 Received: from mail-qk0-f193.google.com ([209.85.220.193]:37503 "EHLO mail-qk0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754589AbdKQSAR (ORCPT ); Fri, 17 Nov 2017 13:00:17 -0500 Received: by mail-qk0-f193.google.com with SMTP id 136so332120qkd.4 for ; Fri, 17 Nov 2017 10:00:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/xZ+xdxCmKjNGEQmfg98zrHbzr6GBRz/LJMbBzz29o0=; b=WWfpdja5+1ZQGBnv+sGhPr7QDSeKKJlVTre4fMGjyt94ztN78Iq0Xe17mr/SfofZWT Z5Ml4Qc3wgBgS+14N/TFAeA7Ug7ofESpFC05IoULQzmimoRvviV/TPOLyDxLbQcf3jgF iqtrof+zffBv1uLv3UU8PrM9Y5CVVTVQNgQmmIGPbOrvRvt85Ag8DrzAjHfquRuYUiAy yfzibAqbIXLKkrwVO5Uzh9sTENrpHittBTgA54qB6563v+JKH9zQPJENIgy9OljXg5Cm 22CDxTNpthLYAwVZftFbFHhl/zKtSK4c+S0BGjri/1lWvBzMiWNzFKy95gO3p5jxqO6T vMXA== 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:in-reply-to :references; bh=/xZ+xdxCmKjNGEQmfg98zrHbzr6GBRz/LJMbBzz29o0=; b=a2Zv5e2DY+mZr5K7A+jrb0uSYzbiLbczreLjRaz++G5RXK48phDVDsQeysDp++eEav UZ2udtAOe5wM7kVzsMG5D3JR+e2HDGDBIwHKKAg+F0itkTIZB/CX67ORX/gNJ6hjsWWl Nib1Kz3iAKfaxmUiKdILXtgAawK38BrlGScGOX2UpifsULffu6nSe5opXhgsWah4in37 5W+8maJOxVEdQwVbjujLfM6sFMosbY90ayf0S200RB15aJnqk8i0n5ClYRhrsPf/JVpw Et/Sgrj/0K7qzUZLtMO844M+gxvh6ycNCm3QjavRRkAhU5VyPDXiDCk3QiGocLDSzHR0 6E8Q== X-Gm-Message-State: AJaThX6nrCiMtkVf7ldxW7FjSlF3wVyPeIzr9bdv9k8BBtkXg/yCyXYw 7OWK6KT8MV/0ojWMHJ80FsmIbQ== X-Received: by 10.55.69.84 with SMTP id s81mr9242140qka.77.1510941615466; Fri, 17 Nov 2017 10:00:15 -0800 (PST) Received: from localhost (cpe-2606-A000-4381-1201-225-22FF-FEB3-E51A.dyn6.twc.com. [2606:a000:4381:1201:225:22ff:feb3:e51a]) by smtp.gmail.com with ESMTPSA id a132sm2806736qkg.43.2017.11.17.10.00.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Nov 2017 10:00:14 -0800 (PST) From: Josef Bacik To: rostedt@goodmis.org, mingo@redhat.com, davem@davemloft.net, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, ast@kernel.org, kernel-team@fb.com, daniel@iogearbox.net, linux-btrfs@vger.kernel.org Cc: Josef Bacik Subject: [PATCH 4/4] samples/bpf: add a test for bpf_override_return Date: Fri, 17 Nov 2017 13:00:07 -0500 Message-Id: <1510941607-3997-5-git-send-email-josef@toxicpanda.com> X-Mailer: git-send-email 2.7.5 In-Reply-To: <1510941607-3997-1-git-send-email-josef@toxicpanda.com> References: <1510941607-3997-1-git-send-email-josef@toxicpanda.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Josef Bacik This adds a basic test for bpf_override_return to verify it works. We override the main function for mounting a btrfs fs so it'll return -ENOMEM and then make sure that trying to mount a btrfs fs will fail. Acked-by: Alexei Starovoitov Signed-off-by: Josef Bacik --- samples/bpf/Makefile | 4 ++++ samples/bpf/test_override_return.sh | 15 +++++++++++++++ samples/bpf/tracex7_kern.c | 16 ++++++++++++++++ samples/bpf/tracex7_user.c | 28 ++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 ++++++- tools/testing/selftests/bpf/bpf_helpers.h | 3 ++- 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100755 samples/bpf/test_override_return.sh create mode 100644 samples/bpf/tracex7_kern.c create mode 100644 samples/bpf/tracex7_user.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index ea2b9e6135f3..83d06bc1f710 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -14,6 +14,7 @@ hostprogs-y += tracex3 hostprogs-y += tracex4 hostprogs-y += tracex5 hostprogs-y += tracex6 +hostprogs-y += tracex7 hostprogs-y += test_probe_write_user hostprogs-y += trace_output hostprogs-y += lathist @@ -58,6 +59,7 @@ tracex3-objs := bpf_load.o $(LIBBPF) tracex3_user.o tracex4-objs := bpf_load.o $(LIBBPF) tracex4_user.o tracex5-objs := bpf_load.o $(LIBBPF) tracex5_user.o tracex6-objs := bpf_load.o $(LIBBPF) tracex6_user.o +tracex7-objs := bpf_load.o $(LIBBPF) tracex7_user.o load_sock_ops-objs := bpf_load.o $(LIBBPF) load_sock_ops.o test_probe_write_user-objs := bpf_load.o $(LIBBPF) test_probe_write_user_user.o trace_output-objs := bpf_load.o $(LIBBPF) trace_output_user.o @@ -100,6 +102,7 @@ always += tracex3_kern.o always += tracex4_kern.o always += tracex5_kern.o always += tracex6_kern.o +always += tracex7_kern.o always += sock_flags_kern.o always += test_probe_write_user_kern.o always += trace_output_kern.o @@ -153,6 +156,7 @@ HOSTLOADLIBES_tracex3 += -lelf HOSTLOADLIBES_tracex4 += -lelf -lrt HOSTLOADLIBES_tracex5 += -lelf HOSTLOADLIBES_tracex6 += -lelf +HOSTLOADLIBES_tracex7 += -lelf HOSTLOADLIBES_test_cgrp2_sock2 += -lelf HOSTLOADLIBES_load_sock_ops += -lelf HOSTLOADLIBES_test_probe_write_user += -lelf diff --git a/samples/bpf/test_override_return.sh b/samples/bpf/test_override_return.sh new file mode 100755 index 000000000000..e68b9ee6814b --- /dev/null +++ b/samples/bpf/test_override_return.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +rm -f testfile.img +dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1 +DEVICE=$(losetup --show -f testfile.img) +mkfs.btrfs -f $DEVICE +mkdir tmpmnt +./tracex7 $DEVICE +if [ $? -eq 0 ] +then + echo "SUCCESS!" +else + echo "FAILED!" +fi +losetup -d $DEVICE diff --git a/samples/bpf/tracex7_kern.c b/samples/bpf/tracex7_kern.c new file mode 100644 index 000000000000..1ab308a43e0f --- /dev/null +++ b/samples/bpf/tracex7_kern.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include "bpf_helpers.h" + +SEC("kprobe/open_ctree") +int bpf_prog1(struct pt_regs *ctx) +{ + unsigned long rc = -12; + + bpf_override_return(ctx, rc); + return 0; +} + +char _license[] SEC("license") = "GPL"; +u32 _version SEC("version") = LINUX_VERSION_CODE; diff --git a/samples/bpf/tracex7_user.c b/samples/bpf/tracex7_user.c new file mode 100644 index 000000000000..8a52ac492e8b --- /dev/null +++ b/samples/bpf/tracex7_user.c @@ -0,0 +1,28 @@ +#define _GNU_SOURCE + +#include +#include +#include +#include "libbpf.h" +#include "bpf_load.h" + +int main(int argc, char **argv) +{ + FILE *f; + char filename[256]; + char command[256]; + int ret; + + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + + if (load_bpf_file(filename)) { + printf("%s", bpf_log_buf); + return 1; + } + + snprintf(command, 256, "mount %s tmpmnt/", argv[1]); + f = popen(command, "r"); + ret = pclose(f); + + return ret ? 0 : 1; +} diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 4a4b6e78c977..3756dde69834 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -673,6 +673,10 @@ union bpf_attr { * @buf: buf to fill * @buf_size: size of the buf * Return : 0 on success or negative error code + * + * int bpf_override_return(pt_regs, rc) + * @pt_regs: pointer to struct pt_regs + * @rc: the return value to set */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -732,7 +736,8 @@ union bpf_attr { FN(xdp_adjust_meta), \ FN(perf_event_read_value), \ FN(perf_prog_read_value), \ - FN(getsockopt), + FN(getsockopt), \ + FN(override_return), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index abfa4c5c8527..086733298d5e 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -81,7 +81,8 @@ static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags, static int (*bpf_perf_prog_read_value)(void *ctx, void *buf, unsigned int buf_size) = (void *) BPF_FUNC_perf_prog_read_value; - +static int (*bpf_override_return)(void *ctx, unsigned long rc) = + (void *) BPF_FUNC_override_return; /* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions -- 2.7.5 From 1585462348764328479@xxx Thu Nov 30 04:05:21 +0000 2017 X-GM-THRID: 1585462348764328479 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread