From patchwork Thu Jul 18 11:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Yikai X-Patchwork-Id: 13736424 Received: from APC01-SG2-obe.outbound.protection.outlook.com (mail-sgaapc01on2050.outbound.protection.outlook.com [40.107.215.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F3CC136E0E; Thu, 18 Jul 2024 11:53:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.215.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721303588; cv=fail; b=n2+gtBIikeaiVpTVSXJqSpoJMb6fXFcFES8oXd+RTDerN6xr7MDV6XZHrHRLP9IqoPtylkN8p/8jzRiMk1uu3Sre3HNxSU7OGc/yr9P5Q0ATs9seZrb+aE4qrv/M9ASI5RiKdc/jOHH31fSvCkU4sYRjguAWAplJh6dV+JFzW+M= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721303588; c=relaxed/simple; bh=AmacMSQYZ1CIgXDwF6411MztTDsZbl53fFoYAh/Uh50=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=ONucHUaYaXLM39i2YrQkQgAiwj15jIcPkTFXPaGTsZNn6KNTSSp8Fk5IVCDTdeaTL9DlHUiAoGENpiWdQtOO7sW+3+lnuMuN5MGZRI3da4NgiwLpV60piOnwoo+UuGTNSLbV4ZdX0VzB+t/OMqmTa04RAaY5p0kDfuSBYkXTfmU= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=jLQsHj7h; arc=fail smtp.client-ip=40.107.215.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="jLQsHj7h" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xtX7tUQM50yQanyG/rM8tta4MDJGa5iIQKlLkmlyInC1SePEU7EG3DPI/TA31sLODfdBbbwo4VHSNvFU3+MIPCc/vyECmlPn81sw5E2H1in5TY0etnvxOna7D8zK9AVAL64HXju2lFynNdJh1KfPA0FkKXlO1eA3WT/JB+8tGimPTrLNKvhQuaW3CfWLo2Qsd01Y9BIs8wyu1qhncV6Y1ufz7RJuJLsXCp5B5TIc0+6eqV0tZI4BGtjFGAey5OArhnTeBcyrlLxAOMupcx7Vhokg1oUXlUZ2ErNUKvelSd8LjPe75sDEh09yYpcE8bWi2IYfLjf479IhXJePsK1dSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XMgfvdG1IZifbSPrt7P8WxcsOKbDbgQXUspwl0xYOhY=; b=RFv6qdvagVXv1S+L7X9RMP/Esf/AP97lq4o/yqdsY7Bj3x2uOkJV1GBT819GeI1UGdgu64kZ0LZhQd2qa9hp94UWs/8wvQ0tihTQnpvzW+pgkcVWltzXPQ4361VfJ1NUZU9eM6X+ycJpXZse2/M/h/rYsdgyQjBrHR1Z1/PGa6unDR0924vmSlTuciNkEHV3epJGGiuBZTussUxjrTSR2vDMRc32Awuw355Xd6IHVNuVIefNNVEaUWgwHUK+81B2YF19WlEdZ/0SZcJL3Zq5haK8qROZrMcGQI1LgQBHexcKIucHUxLA4jm586QEASnmdpLDkGGXq2xvsQUOICFIRA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XMgfvdG1IZifbSPrt7P8WxcsOKbDbgQXUspwl0xYOhY=; b=jLQsHj7h/GlU9EkqV5lU+DK9dWM2pXukFU5foaOLIuC8Z085Xh5OIc91cvyU53USlZN0HEkG8KZuCpjhWsyh4dYmn1wZAUIQeAg21FBJDgcD1jatpwwN3Nfk7Vl7RVnoFodGAFx7p32kah77Cps7Hte46YMB5qRNi/cJq5MhAcd3Hp+yBbhPEJSX1yatXbCyry2W2a1yJAlt22/7LOfMcw+7wHDK7861SFvo8oz/HYsApksDaiMK7QvNVTRLf5hd1B8UVpu5QNxHw2Z8Zxzv/OOgxwhRnf39kbKxckcklv4K145aV5yvDZQ+bXj5L79xmFniUScnirZy33v3DeW6/w== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from SEYPR06MB5913.apcprd06.prod.outlook.com (2603:1096:101:da::16) by TYUPR06MB6219.apcprd06.prod.outlook.com (2603:1096:400:352::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.16; Thu, 18 Jul 2024 11:53:02 +0000 Received: from SEYPR06MB5913.apcprd06.prod.outlook.com ([fe80::f049:a716:8200:c4a1]) by SEYPR06MB5913.apcprd06.prod.outlook.com ([fe80::f049:a716:8200:c4a1%4]) with mapi id 15.20.7784.016; Thu, 18 Jul 2024 11:53:02 +0000 From: Lin Yikai To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan , Lin Yikai , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: opensource.kernel@vivo.com Subject: [PATCH bpf-next v1 1/3] bpf: Add bpf_file_d_path helper Date: Thu, 18 Jul 2024 19:51:43 +0800 Message-Id: <20240718115153.1967859-2-yikai.lin@vivo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240718115153.1967859-1-yikai.lin@vivo.com> References: <20240718115153.1967859-1-yikai.lin@vivo.com> X-ClientProxiedBy: SI2P153CA0033.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::21) To SEYPR06MB5913.apcprd06.prod.outlook.com (2603:1096:101:da::16) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SEYPR06MB5913:EE_|TYUPR06MB6219:EE_ X-MS-Office365-Filtering-Correlation-Id: 615feb20-b6cb-4ab8-8f10-08dca7202d7b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|1800799024|366016|7416014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: eH3bll8Q1auprBq9UcTe8xtc2M000lp4VLch3iOwskJTW9MsFrrbHJUFRMzOGrdOV1S1DAhj65Lgo9D1SqNwcL/JdDAtvoOJKp49QqGgOYssJOgJE7UgsHgY6WB0p1vDqGxMsg/v4C2BK3BpLfwicpLS4/Wc6Yd9j76Im37leE62aR91O3Ilrp/sBxXjO4GQ9VemiAKfD/LwAXQXc4sb6WcUF1cxd2BJmV2OOyxrfvKC8BwIf7iHO4C3ZFPgDbxCExTJyrDP6o8PtfQv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sgDb7TFIA7uOV+T/7NNw+63X3VoajjcuOSBS3qEpBkx/VRthd38C/f/+kJprnUtqp/Mxsa80j4b88OOkvV+we/DwfV7U= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5913.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(52116014)(1800799024)(366016)(7416014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5gTDV2GkMRoHKd5WV4kaY0vKV0/KDIbY1Z7XyV4ijsQSw5uS/aEl11ifV9Q09H+5+xqL9h17QPEqL/OinfHM692M0JPcaQRMTrFUfP9ZSHlUVIphQiwRkl3LF2qkVyVgXOdGV8ezhdd/U+9SJ2lxepidV8IMZ6pJp5lIrIgpM8BJsQqee+saQvuvLyKGYtF7nEu9NLVoIbR8AHGVso0G3xw8kr7bO9fom3WUqXEt9Na+/oFKCQ0XdDzv+vborO1gqfuUVy8NMKddszbfFmNqHj3q5L0HWaAtGoN40jVNWAA+Pyr9A9NXrMZPg4iO3NYLn7b01c7WDx0PG9wG7SocMkGNo9PkbrBjWZA73FrbX9G1h/PJBIrviZCN6XSg3jNU9Z3s5G3CZQwNgRdBKVX3RubsmaqZ38KK9BPeOGN1Vp/V0b32cMSl8lKDmPNli51dHa6NhweXEKl3oG1SXNOAM2K1hopQz0A9zboe+aHMZoFtukliOWvOnNgsuO/igxi6DeshrQJIf8EmBDvv4uyGV7XLtLlsQxx1jwhw51ginrnITEtcbONxwA3WFJXttL/fhySGWKsqZLiqrgpMzSNDq+l3mtyfytibE0f9jGe63jc5SnXxQjlR++ZOoHIwHsqUVPhfshTcdloDcnYYV3fzcyuBUMVeIeWa2FqYN6wVZqQuvdcXeN55q2LyuxA5BadMl4CrGxES4xGPS+2DzE4ekCEyfSSbQ4YFekQ+IHKNtzIqcMGCAV6Wm7DB1ANW9n7ZS8Wnuy6i+WR777sI48+VSe9KufDQRpVU3B6hj66x89N/dycYLI+ksnkbUHance8XGbOzoT5u0dbkpJMDy2RLMcCI5sfsqvkfMRdOqdOZQMvO5v7vJUMHa/qo4TPn5GWgCK0OgLCTdCAP/xgBxrwororoePx8YdqKVbY9AsNx9tDEH8x/gpjquAqDMT97zYPGFC1JHNFKvmpbCygDUoIrnD43ubVCwhFKJWLJZgLTk60GL0QZJocsm2Ie4iViEBt7NOB4LwQmiy0G38FNZyQCWRIHgzSYhnuVrgv1K1GYoYvH3W1tD7Nu06Yy7/zelq77amtf2ta66ZAkUP15AyMG1xARfSms2wVu4Gr/Gck29lGqIsjmDBZeJJhZYC8pBSKKJOgJqpd7jGfpGskVxbTe5+LmMt8unCHN5FpwiXh01wEPdtclHcU1jx6W2V8VG3NyevBlZ4ARHE87786ZV40pTr4gXLf6ydCEIfT/pUADUnSrSR3nBQ2teRj7W5g6mS4k7fuvM4Ygqco0Q6GZmgrHqKkl/BBa1K0VJrVxPhxgzwGxTM+ga/U6WUWR2tsvfhpXm8E/E5clA0NKvTwb71bmbKzJdlHv+odpxuOt+IlR3puV4xikh+4gDozfMWAQmVRrTb7lsixhrHw0UyLQCQiyL4ffeF+ThL/gBIOWupHkKGTi0MlfjZAd71bcN298+bdjwSECMsKfffACEW9o+/Js+xyk8jX6bqX+obCSMRY/GlHFnPDCOYDTooD5pFnYDCV72zkb7Dp0p13Sq7IIZuo4YtorsbvDXSzvl+gLzcTbgMqScOX3QV3uA8cFmjnkTRrt X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 615feb20-b6cb-4ab8-8f10-08dca7202d7b X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5913.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jul 2024 11:53:02.5436 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yisgghx2IDcYeLsEYefNgLNN3K8/RSjUwHWCooWqWNH6IjyYSBtz/wzhm0Q0To6UkcMUY2GAokW3oxFD/n2nBA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYUPR06MB6219 Add the "bpf_file_d_path" helper function to retrieve the path from a struct file object. But there is no need to include vmlinux.h or reference the definition of struct file. Additionally, update the bpf.h tools uapi header. Signed-off-by: Lin Yikai --- include/uapi/linux/bpf.h | 20 ++++++++++++++++++++ kernel/trace/bpf_trace.c | 34 ++++++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 35bcf52dbc65..7e5cec61a877 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5792,6 +5792,25 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * long bpf_file_d_path(void *file, char *dst, u32 size) + * Description + * Return full path for the given *file* object. + * + * In order to solve issues where certain eBPF programs can not include + * the definition of struct file or vmlinux.h + * due to their complexity and conflicts on some operating system, + * the variable *file* here is declared as type void* + * instead of the traditional struct file*. + * It will be forcibly converted into type struct file* later. + * + * If the path is larger than *size*, then only *size* + * bytes will be copied to *dst* + * + * Return + * On success, the strictly positive length of the string, + * including the trailing NULL character. On error, a negative + * value. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6006,6 +6025,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(file_d_path, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index cd098846e251..70fde7f20e97 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1257,6 +1257,38 @@ static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_3(bpf_file_d_path, void *, file, char*, dst, u32, size) +{ + long len; + struct file copy; + char *ptr; + + if (!size) + return 0; + + len = copy_from_kernel_nofault(©, (struct file *)file, sizeof(struct file)); + if (len < 0) + return len; + + ptr = d_path(&(copy.f_path), dst, size); + if (IS_ERR(ptr)) { + len = PTR_ERR(ptr); + } else { + len = dst + size - ptr; + memmove(dst, ptr, len); + } + return len; +} + +const struct bpf_func_proto bpf_file_d_path_proto = { + .func = bpf_file_d_path, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_ANYTHING, + .arg2_type = ARG_PTR_TO_MEM, + .arg3_type = ARG_CONST_SIZE_OR_ZERO, +}; + #ifdef CONFIG_KEYS __bpf_kfunc_start_defs(); @@ -1629,6 +1661,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) return &bpf_find_vma_proto; case BPF_FUNC_trace_vprintk: return bpf_get_trace_vprintk_proto(); + case BPF_FUNC_file_d_path: + return &bpf_file_d_path_proto; default: return bpf_base_func_proto(func_id, prog); } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 35bcf52dbc65..7e5cec61a877 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5792,6 +5792,25 @@ union bpf_attr { * 0 on success. * * **-ENOENT** if the bpf_local_storage cannot be found. + * + * long bpf_file_d_path(void *file, char *dst, u32 size) + * Description + * Return full path for the given *file* object. + * + * In order to solve issues where certain eBPF programs can not include + * the definition of struct file or vmlinux.h + * due to their complexity and conflicts on some operating system, + * the variable *file* here is declared as type void* + * instead of the traditional struct file*. + * It will be forcibly converted into type struct file* later. + * + * If the path is larger than *size*, then only *size* + * bytes will be copied to *dst* + * + * Return + * On success, the strictly positive length of the string, + * including the trailing NULL character. On error, a negative + * value. */ #define ___BPF_FUNC_MAPPER(FN, ctx...) \ FN(unspec, 0, ##ctx) \ @@ -6006,6 +6025,7 @@ union bpf_attr { FN(user_ringbuf_drain, 209, ##ctx) \ FN(cgrp_storage_get, 210, ##ctx) \ FN(cgrp_storage_delete, 211, ##ctx) \ + FN(file_d_path, 212, ##ctx) \ /* */ /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't From patchwork Thu Jul 18 11:51:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lin Yikai X-Patchwork-Id: 13736425 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2080.outbound.protection.outlook.com [40.107.117.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B84513B780; Thu, 18 Jul 2024 11:53:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.80 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721303599; cv=fail; b=XyF4pvHprbijTgTSi2PUZQ2eqocyjp1o2N2nuxKP3BuyS+ug6bJ24r3xDbRb2MomfFOnAkUvo/98YTwSDwRtHK2qHdQ94rY8tW2cWdw5z9c5+qGMXHveu1JDinByg/PTVtmPqOy3eYDmMmQZA3fL7Rrb5A6PS9nc3fkoAJOWpLc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721303599; c=relaxed/simple; bh=Unb6v1BTZG4AH6DIbwS/DWO0y2UGDc9pn5mTtSE1zO0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=QIi5rgEW15HeOZHMQoQEkyxbTaHXhooPMHm0H35M8aKfkBXnfytWytbS/lji/yMETIJ8W62hu9JzeRGOJ+Wg3mXa4enypdOAQbldL4O96O8FK1jZhcYznla6sgmscVyoVFuvX/9roO1GQtIXcWXWBojTephg+eX06Z4DMItcpMg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com; spf=pass smtp.mailfrom=vivo.com; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b=bjY0t3/G; arc=fail smtp.client-ip=40.107.117.80 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=vivo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=vivo.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=vivo.com header.i=@vivo.com header.b="bjY0t3/G" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hyYN/+PC4l+vXGnIFa4RPJj8laQ3F8VAD51VSM8+EenNUx+mU4Np2xZVHOL/I9thRZ08/iJGClRL4LG26RYLuXKRfiB1SzL7C4JWxCqUzE3kqLRDIbXtxrRif3qRQLsbZYwmWHfUHAfTjUWO4QDP5VlbP3KZL4IivAWm1EpYiiEQxKduckmomd+QikGuMVvEkafL8TtD2hHCk6SU/S1hrjzMQgcMxL/IYCOIP8qhMZu98I00yKyncc0bGmxCZxi3ISqhIp47H9BgZzWfJyXfYUE+pX4pXabh9/QFTZvrBiue2dGVKVxI5KURRiaGJVqQg9tsTkvVEnMqCuw7BEkncA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=FwsFm2w9kA3VbpErR1R5yMie6b+3mDrA9uzO+BwSfOY=; b=c5SzE1J7ugTvOd0W3mzHMjIV1WnMlb5GDx4pvm00fWip2K0RKKCc6FTYBK1BV4iYRJ1fgzkJkbeDwpleAM2ESENUgNBvthAkB7se2gXElsJqVe9YNZzZSD2UDyuel+u8x8fS5wmD00SbfKUh+TY3axfbfC4V6pZMT/WYIWvZYJF29p+bV0O8x8bsqE7ly0uWQ1XPJbIKdHd3CrZ632V4pi5fcaVVOjba4I5OoiNirmaQ5FBTycpYwla3U9h3uoH5IWbHkYqGPUWyRnE8ykGQf4aHR8fJf1+ajPGEOgvBlVTBFb2R0oYns1+LX01P051cL+w++FQ2OF+exe6RE1NjVw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FwsFm2w9kA3VbpErR1R5yMie6b+3mDrA9uzO+BwSfOY=; b=bjY0t3/G4ltwYgWHNPFa1zpy39MmMGf3rsu3W2uXs52cgjgqtqN6O6GZAZ+DF/eGZXheSuJKw/Zvzalcr2gCGQNkmIS8/mZPfErG2jU5sijLPGPtsl2V95BuMgPVJT0WJW/8N735RTI3SF2ugZwWsV9kqLOzylyJEyIrBJiEi4+U3F3Lq5JBSb+TUvAl6iizdEOEbDDKRYehhGoLVtr3XRqqReqWfmb8xqNVMFtGAACSMt8ksngaEls/DLfyebcGdUwcA3+ozBqHB2Zx7eWH/Wg4LOjxT/6urysSRjCj8snzbXv/ZCl08WCCvzwWkctiqnF9RycvHj7pCmq885SLyg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from SEYPR06MB5913.apcprd06.prod.outlook.com (2603:1096:101:da::16) by TYUPR06MB6219.apcprd06.prod.outlook.com (2603:1096:400:352::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.16; Thu, 18 Jul 2024 11:53:15 +0000 Received: from SEYPR06MB5913.apcprd06.prod.outlook.com ([fe80::f049:a716:8200:c4a1]) by SEYPR06MB5913.apcprd06.prod.outlook.com ([fe80::f049:a716:8200:c4a1%4]) with mapi id 15.20.7784.016; Thu, 18 Jul 2024 11:53:14 +0000 From: Lin Yikai To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Mykola Lysenko , Shuah Khan , Lin Yikai , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: opensource.kernel@vivo.com Subject: [PATCH bpf-next v1 2/3] selftests/bpf:Adding test for bpf_file_d_path helper Date: Thu, 18 Jul 2024 19:51:44 +0800 Message-Id: <20240718115153.1967859-3-yikai.lin@vivo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240718115153.1967859-1-yikai.lin@vivo.com> References: <20240718115153.1967859-1-yikai.lin@vivo.com> X-ClientProxiedBy: SI2P153CA0033.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::21) To SEYPR06MB5913.apcprd06.prod.outlook.com (2603:1096:101:da::16) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SEYPR06MB5913:EE_|TYUPR06MB6219:EE_ X-MS-Office365-Filtering-Correlation-Id: cbb0247d-416e-4616-3599-08dca72034d2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|1800799024|366016|7416014|376014|921020|38350700014; X-Microsoft-Antispam-Message-Info: tUUIcQw+6m6QnMXXU7fRZ0WXiYCRkvt2gqcSICEy/xa379KZ8UdG0HT9hzsSqR7/W+mfNEltuvfoXQ/hImaAXRSk0UBaBCcQNZDjoSOBWTKEbIT0Bm3SBKVmBYqO/SQEnFdVGTRnN+mxKPW/oXJ/WZTx2uqH1j1HS+VJU15BR0fXLAFVuKMfatCf6OXi4CbLWBlHpfpCWd0xr8X45fnsuAtqi9za7GoN1zmTGhCJ0SXWRiCPg506YBNyJldAGlAjdHwntVYKPgxfuoMYBU2TlAseY/+rseVaRqPB7tD0pH431x7io8KJ/VDWH2aKMLQpPialsUXcYOb6T/R7IIew2eyljWpogF1XCVlcqMdupJvjh7cyAfDnLQKdv1VzeDndZ9AntZiYodLpOQcUZlin8K/KN0rgvPxryVsFjThOsPJTtKl493Sgp+i1O/+m1PjbYIuD1IxKu0Y47btwtm8tQRxOwF7c6ApJIryz/uAVJNK/Hl0kG9TGJXC/oc8531GlRqcDoNkV24I7ph36cS2UxlC7NF7F8utlmRXZCbBVu32lKVA85WYEmZW2tZno6OEXsuMqkPNea3Mk35KVe9rO5HV2hMzVVo20lunq7d7+gioysktWyDh48VOLunogHq0XFtIyYVcgBeLxlYE2afgp76G0VL7soyfgubUDBAlSamhjQvCq4/pgAKm9KvncwFOoZ3dDSom2T9wXcnzqIugWGoygQTJaiwH83qHmB0AR8atxXJ7UEpgFULSgM8rp4y/Ho6n18vaqCLJD1QhRdv4HbfJdamihVvRLhR2PTn1by/DBw4DXpCQLvxp+w6zW4sp+381HyH3twYIqEWnGeYqJH2Kx/S3wOQUQWY2QUoUE3zyprpnqLCBm/UPvHUZy3eQcycm2034/EqiUf63QQ/Lrx9Ov85ItyhdU9SnI/U5NOTm1dxRDiHPs6r6ZdAVcglhtNxwGZ4tICayrRTVxiNnRLtXQfHCaiMFUMjLEbXvmD0SRPPVjrsDmZ7AyGYphmdCrZETm5Joi9OPZt9ZrzOPUwXWe5yTU3NoOGiR4wc0UOhANsxcdBpMg77D+vypDsjntLp17Uiey09RAO0C3O9qQk8s6SltLteOqbs5Vd9bioIGzutLaudqubO/BUxvZce7jT58oqnWjOS4bps8NzDHBbpEq6g3Af47UgrkAD4prLdLL0tKn69cG0S71zcZfZnyzePXR68mZfaovHrh2cM+Kua+IUKQrRDlMYPG7Q+BG5iqpMy/NVn7TLwQH/haj9fhat1/6PTQARMJ/9u34dcCD/v38PtL4G3KqNkpJc7pCTaonLsyGwMvqHRvUlUZEmsTSJNJmnFZaHquwCVWrj2swtDj3o1Wiaq9ns9j9+HztyzpKyWBwFasxdH9+PSFQxlpNzQHHxg7QMjKnPxq6ycHNtmbYa/fSehPcu81og0464qU= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SEYPR06MB5913.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(52116014)(1800799024)(366016)(7416014)(376014)(921020)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3YQE/z2FqtEPgcb9/l++oj9EcrCnF6h9RAm4PieLrTFcVf8KENI6tEdW7510wCmmpB7qmgK15j0xBdNJrn0xfTrrr4z2v5nugFU5FeYGNq+1kjdUUSxCgUfxVYNFVdoq5H2eaWsD+nhJCOElmDJwJDbRFk6SMByzY80CWj7KgRYoLrqj/n+AgSUZArqPbA+GA7DGUEw4UQlW3B5nYtIMDtmvdh2QkEwgKuLuKy9DpFDuNgQ+Mq1IgIiBbaFmTTqzN/vU+5EF/Gj84pDSBi7rdaVj81FzNth0UbWWTdIaCn/8Z7tPF080Ga7ncqDikLNx1tzLbw/ZAQmi+E7/CK57teNcbpvtz8OE0fvXiE8aj4v/R9aVykLNlffAFYt662YVYDbUt+HaVtnp8XEdImzRdWboXp78ZsI4C8C7n1jKCUuF/moBr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vrb1Gg21nfn14xn7IRNaoyxQWsVhSNvcXFvrqjtMBseqwKw6dpuJh3Es9mFtHYHTTokMxsmFGJByJ/mbZTxsoA X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: cbb0247d-416e-4616-3599-08dca72034d2 X-MS-Exchange-CrossTenant-AuthSource: SEYPR06MB5913.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jul 2024 11:53:14.8468 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YCSep7h0tmGammT1W6OaXy2y26Q3XUMveUdUl8hUDUF+QBRNwgp5rwCCSkfmjqC3YpryhIQNLfQ3G5wJ+i+XVQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYUPR06MB6219 Hi, Adding test for bpf_file_d_path helper. It passed the test in my environment using QEMU (./test_progs -t file_d_path) Below are some partial results: ''' + [ -x /etc/rcS.d/S50-startup ] + /etc/rcS.d/S50-startup ./test_progs -t file_d_path Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED ''' Signed-off-by: Lin Yikai --- .../selftests/bpf/prog_tests/file_d_path.c | 115 ++++++++++++++++++ .../selftests/bpf/progs/test_file_d_path.c | 32 +++++ 2 files changed, 147 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/file_d_path.c create mode 100644 tools/testing/selftests/bpf/progs/test_file_d_path.c diff --git a/tools/testing/selftests/bpf/prog_tests/file_d_path.c b/tools/testing/selftests/bpf/prog_tests/file_d_path.c new file mode 100644 index 000000000000..ba76d9467f3e --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/file_d_path.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +#include "test_file_d_path.skel.h" + +/* Compatible with older versions of glibc begin */ +#ifndef __NR_close_range +#ifdef __alpha__ +#define __NR_close_range 546 +#else +#define __NR_close_range 436 +#endif +#endif + +#define close_fd(fd) syscall(__NR_close_range, fd, fd, 0) +/* Compatible with older versions of glibc end */ + + +#define MAX_PATH_LEN 256 +#define TEST_FILES_NUM 2 + +static int duration; + +static struct { + __u32 cnt; + char paths[TEST_FILES_NUM][MAX_PATH_LEN]; +} record; + +static int set_pathname(int fd, pid_t pid) +{ + char buf[MAX_PATH_LEN]; + + snprintf(buf, MAX_PATH_LEN, "/proc/%d/fd/%d", pid, fd); + return readlink(buf, record.paths[record.cnt++], MAX_PATH_LEN); +} + +static int trigger_filp_close(pid_t pid) +{ + int ret = -1; + const char *comm_path = "/proc/self/comm"; + int commfd = -1; + const char *tmp_path = "/tmp/test_bpf_file_d_path.txt"; + int tmpfd = -1; + + /* open file */ + commfd = open(comm_path, O_RDONLY); + if (CHECK(commfd < 0, "test_file_d_path", "open %s failed\n", comm_path)) + goto fd_close; + + tmpfd = open(tmp_path, O_CREAT | O_RDONLY, 0644); + if (CHECK(tmpfd < 0, "test_file_d_path", "open %s failed\n", tmp_path)) + goto fd_close; + remove(tmp_path); + + /* record file */ + memset(&record, 0, sizeof(record)); + ret = set_pathname(commfd, pid); + if (CHECK(ret < 0, "test_file_d_path", "set_pathname failed for commfd\n")) + goto fd_close; + ret = set_pathname(tmpfd, pid); + if (CHECK(ret < 0, "test_file_d_path", "set_pathname failed for tmpfd\n")) + goto fd_close; + + ret = 0; + /* close file */ +fd_close: + if (commfd != -1) + close_fd(commfd); + if (tmpfd != -1) + close_fd(tmpfd); + return ret; +} + +static void test_base(void) +{ + int err = -1; + struct test_file_d_path__bss *bss; + struct test_file_d_path *skel; + + skel = test_file_d_path__open_and_load(); + if (CHECK(!skel, "open_and_load", "load file_d_path skeleton failed\n")) + goto cleanup; + + err = test_file_d_path__attach(skel); + if (CHECK(err, "attach", "attach file_d_path failed: %s\n", strerror(errno))) + goto cleanup; + + bss = skel->bss; + bss->monitor_pid = getpid(); + + err = trigger_filp_close(bss->monitor_pid); + if (err < 0) + goto cleanup; + + if (CHECK(bss->bpf_called_cnt != TEST_FILES_NUM, + "bpf_called_cnt", + "prog called times diff from with the expectations\n")) + goto cleanup; + + for (int i = 0; i < TEST_FILES_NUM; i++) { + CHECK(strncmp(record.paths[i], bss->bpf_paths_close[i], MAX_PATH_LEN), + "bpf_paths_close", + "the paths diff from the expectations: id=[%d], path: %s vs %s\n", + i, record.paths[i], bss->bpf_paths_close[i]); + } + +cleanup: + test_file_d_path__destroy(skel); +} + +void test_file_d_path(void) +{ + test_base(); +} diff --git a/tools/testing/selftests/bpf/progs/test_file_d_path.c b/tools/testing/selftests/bpf/progs/test_file_d_path.c new file mode 100644 index 000000000000..8db2bcd1179f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_file_d_path.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" +#include +#include + +#define MAX_PATH_LEN 256 +#define TEST_FILES_NUM 2 + +pid_t monitor_pid = 0; + +__u32 bpf_called_cnt = 0; +char bpf_paths_close[TEST_FILES_NUM][MAX_PATH_LEN] = {0}; + +SEC("kprobe/filp_close") +int test_bpf_file_to_path(struct pt_regs *regs) +{ + void *file = NULL; + pid_t cur_pid = bpf_get_current_pid_tgid() >> 32; + + if (cur_pid != monitor_pid) + return 0; + + if (bpf_called_cnt >= TEST_FILES_NUM) + return 0; + + file = (void *)PT_REGS_PARM1(regs); + bpf_file_d_path(file, bpf_paths_close[bpf_called_cnt++], MAX_PATH_LEN); + + return 0; +} + +char _license[] SEC("license") = "GPL";