From patchwork Wed Feb 14 17:50:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 201080 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1388771dyb; Wed, 14 Feb 2024 09:52:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUTK08mS1E0OifHMwZZSm/ALnryF5Q4wpgYkBNoy+mjeaAcpTKqyc6gfre9ZyF1smim2/MCg6CUVz2YUoLhq4kwoU59QA== X-Google-Smtp-Source: AGHT+IGSJEy1r9yo+jWAtG3ei1HMiBojtmVEqF53C88O1ZXuvfQHLM0cTxtkQ4V4Uyvs0E6NmZNX X-Received: by 2002:a17:906:af99:b0:a34:d426:1c0a with SMTP id mj25-20020a170906af9900b00a34d4261c0amr2545826ejb.24.1707933130468; Wed, 14 Feb 2024 09:52:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707933130; cv=pass; d=google.com; s=arc-20160816; b=mRgddqpk+QfNLcAXD97c2R0n+IJkkaUYvpLvFwrNAPNGobJnb8qds4Kt0dmHbGYJRQ PdUpdlITJ8UkcsJ5m9HIXOGMXoZO2K1nt8zBJMvoDJ63yRZFGi++4bG0ez3xB0Z51eNz 4AcYmJm59ijZEYDN6Inj4adYdAW2kPbeGA4IszD8OdsiUOjaB5dKd41X8xjsYiXv7+kz wwX5JhCs/teOloio/MGe2mpsEGfnuNvysrUG6TjHJEoAibnHRyJaUMDiOKlmWhMY2mGj UE8Nr6d5mgcMMOgP2Sqnxuj4P+4EhfnuqtVACyvbdnpCc8eLtl1yBrW4aqGsFD42DpS4 aFDA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-filter; bh=o0FV3/sKobbMl5jl+jtxhaHSWtCwgSqiq8T2gL2Rh8U=; fh=/Abyi3RYet0/6pcrhLul2982g9XjAeBwnQClJtb2xFs=; b=NyW6ZtlZO5DKeIbYBCrtenHaw8G6CbmlnguSK5rDk9RanCZ3Cz0SQJbw8TKhxXC/Iz QsvoIW0xsRykc2ZjKfji7qRgM/JUYcWFioh4kUsrGIGC8kW2+OaZC4P7Wvh7Ju0vBv40 SqhBt03kjY37PrfpJCZE8ieBhKjPbmuzmDhpZWOynv+7a1CLSJV8qBi9joK9igCbRDNK UtaJUjS+BWa3r+M7U6rbIwNSkLMmDxL+fmtbLnORY0mqk5DAkbBgGW4oZHbhjAgZu5hi 8rm/aBTaTfbJmFeGqtcpuAevguFKTwOl4XwUvAprFfC57juPGNuhqaIbixI1v4Sj7UQP zXHw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=UxpFCZSW; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65720-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=2; AJvYcCXShdi5lEje3erK+g5Nk376ZCTHYjcfQTRFRWyLIirUOpduFMWQll7xz5IKtf6QR38IMNc6Q+86oyph9ASfcwF6K1YlNQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id dx4-20020a170906a84400b00a3d6ceeb8ebsi233253ejb.894.2024.02.14.09.52.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:52:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=UxpFCZSW; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65720-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65720-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 7D7081F25E93 for ; Wed, 14 Feb 2024 17:51:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 253CD1292F8; Wed, 14 Feb 2024 17:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="UxpFCZSW" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5DADC1272C8; Wed, 14 Feb 2024 17:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; cv=none; b=j1twgf7EnBQRMTyet/Q/Z6Lys8iX5le39yd8ddyAwxkbwIsyKcb0SMSkMognR31tPKk+dEOxuAJpvX0HFdTiyR4cd5j3Jlvwi7LNzBjZnx79u26Sj/Pvn6NoClKk793jC2t5Df6Q0n4bx0tgcOQcq2+1OEYhoTH/Zm903cZV7p8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; c=relaxed/simple; bh=LhLcIfGwLgQ6jhRoGqWd9yow1npiE7O06A4Z+YTURrU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DBADaQLzn41PgyDrj9+hwraubheOUFOiVIwSTWUhk0aHpw3fhutnET8wyjHSd9M9gxnKNUkLRabvZAmXpBGhme8s2RTvgd0ygkpdU7bFX8Y4DF7iRBUsHAHq1Tyezdn+bPWcxZFhUt5yae67LkJHCwWsr0u6GgWjtDwIJAv2oFc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=UxpFCZSW; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: from localhost.localdomain (unknown [4.155.48.117]) by linux.microsoft.com (Postfix) with ESMTPSA id 0DD6220B2001; Wed, 14 Feb 2024 09:50:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 0DD6220B2001 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1707933056; bh=o0FV3/sKobbMl5jl+jtxhaHSWtCwgSqiq8T2gL2Rh8U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UxpFCZSW3qIUZ5Uhx7Jua3aJGTc9uWX+hJXyXZdLdHojFXjbqPneDndx8wuhttWSt DfKTfl6OI4ZnUVsJ23G5G9JhDcxdo1GnbhK4fFh0KjI6r9zy8nJxk4hzeG8LRKNIFn 87IrFvyGxHs5QL3BF57UZj9kvk/Xws0ausSLg8uU= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com Subject: [PATCH v3 1/4] tracing/user_events: Prepare find/delete for same name events Date: Wed, 14 Feb 2024 17:50:43 +0000 Message-Id: <20240214175046.240-2-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214175046.240-1-beaub@linux.microsoft.com> References: <20240214175046.240-1-beaub@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897690038013078 X-GMAIL-MSGID: 1790897690038013078 The current code for finding and deleting events assumes that there will never be cases when user_events are registered with the same name, but different formats. In the future this scenario will exist to ensure user programs can be updated or modify their events and run different versions of their programs side-by-side without being blocked. This change does not yet allow for multi-format events. If user_events are registered with the same name but different arguments the programs see the same return values as before. This change simply makes it possible to easily accomodate for this in future changes. Update find_user_event() to take in argument parameters and register flags to accomodate future multi-format event scenarios. Have find validate argument matching and return error pointers to cover address in use cases, or allocation errors. Update callers to handle error pointer logic. Move delete_user_event() to use hash walking directly now that find has changed. Delete all events found that match the register name, stop if an error occurs and report back to the user. Update user_fields_match() to cover list_empty() scenarios instead of each callsite doing it now that find_user_event() uses it directly. Signed-off-by: Beau Belgrave --- kernel/trace/trace_events_user.c | 107 +++++++++++++++++-------------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 9365ce407426..dda58681247e 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -202,6 +202,8 @@ static struct user_event_mm *user_event_mm_get(struct user_event_mm *mm); static struct user_event_mm *user_event_mm_get_all(struct user_event *user); static void user_event_mm_put(struct user_event_mm *mm); static int destroy_user_event(struct user_event *user); +static bool user_fields_match(struct user_event *user, int argc, + const char **argv); static u32 user_event_key(char *name) { @@ -1493,17 +1495,24 @@ static int destroy_user_event(struct user_event *user) } static struct user_event *find_user_event(struct user_event_group *group, - char *name, u32 *outkey) + char *name, int argc, const char **argv, + u32 flags, u32 *outkey) { struct user_event *user; u32 key = user_event_key(name); *outkey = key; - hash_for_each_possible(group->register_table, user, node, key) - if (!strcmp(EVENT_NAME(user), name)) + hash_for_each_possible(group->register_table, user, node, key) { + if (strcmp(EVENT_NAME(user), name)) + continue; + + if (user_fields_match(user, argc, argv)) return user_event_get(user); + return ERR_PTR(-EADDRINUSE); + } + return NULL; } @@ -1860,6 +1869,9 @@ static bool user_fields_match(struct user_event *user, int argc, struct list_head *head = &user->fields; int i = 0; + if (argc == 0) + return list_empty(head); + list_for_each_entry_reverse(field, head, link) { if (!user_field_match(field, argc, argv, &i)) return false; @@ -1880,10 +1892,8 @@ static bool user_event_match(const char *system, const char *event, match = strcmp(EVENT_NAME(user), event) == 0 && (!system || strcmp(system, USER_EVENTS_SYSTEM) == 0); - if (match && argc > 0) + if (match) match = user_fields_match(user, argc, argv); - else if (match && argc == 0) - match = list_empty(&user->fields); return match; } @@ -1922,11 +1932,11 @@ static int user_event_parse(struct user_event_group *group, char *name, char *args, char *flags, struct user_event **newuser, int reg_flags) { - int ret; - u32 key; struct user_event *user; + char **argv = NULL; int argc = 0; - char **argv; + int ret; + u32 key; /* Currently don't support any text based flags */ if (flags != NULL) @@ -1935,41 +1945,34 @@ static int user_event_parse(struct user_event_group *group, char *name, if (!user_event_capable(reg_flags)) return -EPERM; + if (args) { + argv = argv_split(GFP_KERNEL, args, &argc); + + if (!argv) + return -ENOMEM; + } + /* Prevent dyn_event from racing */ mutex_lock(&event_mutex); - user = find_user_event(group, name, &key); + user = find_user_event(group, name, argc, (const char **)argv, + reg_flags, &key); mutex_unlock(&event_mutex); - if (user) { - if (args) { - argv = argv_split(GFP_KERNEL, args, &argc); - if (!argv) { - ret = -ENOMEM; - goto error; - } + if (argv) + argv_free(argv); - ret = user_fields_match(user, argc, (const char **)argv); - argv_free(argv); - - } else - ret = list_empty(&user->fields); - - if (ret) { - *newuser = user; - /* - * Name is allocated by caller, free it since it already exists. - * Caller only worries about failure cases for freeing. - */ - kfree(name); - } else { - ret = -EADDRINUSE; - goto error; - } + if (IS_ERR(user)) + return PTR_ERR(user); + + if (user) { + *newuser = user; + /* + * Name is allocated by caller, free it since it already exists. + * Caller only worries about failure cases for freeing. + */ + kfree(name); return 0; -error: - user_event_put(user, false); - return ret; } user = kzalloc(sizeof(*user), GFP_KERNEL_ACCOUNT); @@ -2052,25 +2055,33 @@ static int user_event_parse(struct user_event_group *group, char *name, } /* - * Deletes a previously created event if it is no longer being used. + * Deletes previously created events if they are no longer being used. */ static int delete_user_event(struct user_event_group *group, char *name) { - u32 key; - struct user_event *user = find_user_event(group, name, &key); + struct user_event *user; + struct hlist_node *tmp; + u32 key = user_event_key(name); + int ret = -ENOENT; - if (!user) - return -ENOENT; + /* Attempt to delete all event(s) with the name passed in */ + hash_for_each_possible_safe(group->register_table, user, tmp, node, key) { + if (strcmp(EVENT_NAME(user), name)) + continue; - user_event_put(user, true); + if (!user_event_last_ref(user)) + return -EBUSY; - if (!user_event_last_ref(user)) - return -EBUSY; + if (!user_event_capable(user->reg_flags)) + return -EPERM; - if (!user_event_capable(user->reg_flags)) - return -EPERM; + ret = destroy_user_event(user); - return destroy_user_event(user); + if (ret) + goto out; + } +out: + return ret; } /* From patchwork Wed Feb 14 17:50:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 201082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1389409dyb; Wed, 14 Feb 2024 09:53:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWXGMMq0xHkb8uA6oM5daBle/+9eGS2TEjFZw+DCpYDkCJaR+slDknk6pdVwUtnb+Tkmyi00B/yjbCwQ6013jL/LdYjiw== X-Google-Smtp-Source: AGHT+IGBHrpEL96h2FWa/7mFiOhbiXbvG67AJYgcVoe7/H+SH9KtNmzB3FbHQSG2eguVx+2E8IIv X-Received: by 2002:a05:6214:301d:b0:68e:f594:30ca with SMTP id ke29-20020a056214301d00b0068ef59430camr4442773qvb.23.1707933207607; Wed, 14 Feb 2024 09:53:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707933207; cv=pass; d=google.com; s=arc-20160816; b=B+hv9wJxBtSjDrxNK+RJtdBgE0Q9kyh0VOpgXb9uhRYPaLFh1ipwsUiSlOhCd25mnt 0rOHBE9UTDhCj07Rf0KwoBIcQvPgPCPo223G5UN8KTsAWUQ7dQvwKXX2+WdKtiaQhmLr UT3JbTbmef+RweNXcSa3hRSWT2ZMR7b4+j79VBDeDUq7KwFvrnFZw4rW3fDpOCAcTHrm B331jKLGTHbYcI855U5QbZdTrToKyDyL7FKwjd7McsoYr9Q2A8tfE1o2+3pHtH+nV6pa xevXZnMPRKyS6HBAw2I6okv1OORDxtntSdgver1brtj8y8lcx4tZPdhGqejtFzkiTLsv 9lCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-filter; bh=rCUIa9r7RCQbT2ME+ULhEAx0/ULy4rYNOneRGyRu2VM=; fh=93OdfzLinM7+3Nv00VAKW2Ij0jZ0IxRgg+a5ehTtjOk=; b=gZUPCqRkCjydZn0N2EApjGs7NX0XHFDv31UfQ4Oj9/Yjs/Tj9rjTfr3BvZV/JqZyFq ytaToDwujOk1kuCXjanfGb4vWUPYA4HfQlxwYzbYUYTQ+qAbmhmmIX3LXJkWMey7sgXH IXUX1EVT8wQtORdmNL7LrkLq4w8Qm5g/5AjsM2W+M/R5QRf1vzaCcd5QVZfzg5Jxp/lk TQD71/iMDGrdg9P6zeTI2CuzK9i6UhdE7G51LScWPBOkXLJRJPKgJp1eQppNej+GQ8qS bnIirKigIHZKQUgAn71dNh04jOL5rbwjeHkCeDLTLFAHP4lTb+H9F+5obwJMiMB2ZSBP eTOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=PxLYqSpw; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65719-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65719-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=2; AJvYcCWXdY5mG6fkydNa9rp7g5VxD8CJCxiY6i3EEySF2wY4cefA4AricfsW9Pk+TsjZ98PPHlQkdF3uR6wk8vD4exyY8fEDng== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id gw13-20020a0562140f0d00b00683699d4df8si5891119qvb.139.2024.02.14.09.53.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:53:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65719-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=PxLYqSpw; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65719-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65719-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2C9D71C2705C for ; Wed, 14 Feb 2024 17:52:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D96A6129A74; Wed, 14 Feb 2024 17:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="PxLYqSpw" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84AD284FCB; Wed, 14 Feb 2024 17:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; cv=none; b=swJiM2wC3pFuB7lttPvPINuDWqWN+KRZJlzBc2NUr8LwZ05jMi1OUpDRUALpV3zgFpYXbmr9AH+BOEgj42eWWOaa5DkA5Pgyu5R5cJABHA3LPP2PwfLoXksssY46cHOLpNnCpWqGH8j2m+ssNq1QaP8jPCqR2zILP1Bjt5cFDn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; c=relaxed/simple; bh=eh7Znbuws0FpzTqXB/htjVXzfh7M7J0jPIJS1UmMUDU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N9RTAKSYU0evUQNdaKxlVXp3NtU0L/KnOu42YnJ+atV4rA8z4lWOV4RZYRsvmJxo7139p0p+ieHKCzFq8h7aY1XFrR4CA20PfGsHuuwh66hO9a530iqwzwTBnqUvDuyjwoz7tW24MvjiDoMWLRyU8vm8m7GF0a6BQV5SznSOhSg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=PxLYqSpw; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: from localhost.localdomain (unknown [4.155.48.117]) by linux.microsoft.com (Postfix) with ESMTPSA id 2703D20B2002; Wed, 14 Feb 2024 09:50:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2703D20B2002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1707933056; bh=rCUIa9r7RCQbT2ME+ULhEAx0/ULy4rYNOneRGyRu2VM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PxLYqSpw5JdpvlocHypm61AscLQdzr7hR8SkFvoeFd8lSUrYXe4sknikoPsSdw0jy yi5IDnjbtW6SfSme1niNXYr0rcpO0WnPnND3/wB0LlV9RH0dak8LulychTMBc1vjS4 JNvmi6/SRZsaC431v2DaPocDKPH2XMxJtsHeE/Sg= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com Subject: [PATCH v3 2/4] tracing/user_events: Introduce multi-format events Date: Wed, 14 Feb 2024 17:50:44 +0000 Message-Id: <20240214175046.240-3-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214175046.240-1-beaub@linux.microsoft.com> References: <20240214175046.240-1-beaub@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897771248595227 X-GMAIL-MSGID: 1790897771248595227 Currently user_events supports 1 event with the same name and must have the exact same format when referenced by multiple programs. This opens an opportunity for malicous or poorly thought through programs to create events that others use with different formats. Another scenario is user programs wishing to use the same event name but add more fields later when the software updates. Various versions of a program may be running side-by-side, which is prevented by the current single format requirement. Add a new register flag (USER_EVENT_REG_MULTI_FORMAT) which indicates the user program wishes to use the same user_event name, but may have several different formats of the event in the future. When this flag is used, create the underlying tracepoint backing the user_event with a unique name per-version of the format. It's important that existing ABI users do not get this logic automatically, even if one of the multi format events matches the format. This ensures existing programs that create events and assume the tracepoint name will match exactly continue to work as expected. Add logic to only check multi-format events with other multi-format events and single-format events to only check single-format events during find. Change system name of the multi-format event tracepoint to ensure that multi-format events are isolated completely from single-format events. This prevents single-format names from conflicting with multi-format events if they end with the same suffix as the multi-format events. Add a register_name (reg_name) to the user_event struct which allows for split naming of events. We now have the name that was used to register within user_events as well as the unique name for the tracepoint. Upon registering events ensure matches based on first the reg_name, followed by the fields and format of the event. This allows for multiple events with the same registered name to have different formats. The underlying tracepoint will have a unique name in the format of {reg_name}.{unique_id}. For example, if both "test u32 value" and "test u64 value" are used with the USER_EVENT_REG_MULTI_FORMAT the system would have 2 unique tracepoints. The dynamic_events file would then show the following: u:test u64 count u:test u32 count The actual tracepoint names look like this: test.0 test.1 Both would be under the new user_events_multi system name to prevent the older ABI from being used to squat on multi-formatted events and block their use. Deleting events via "!u:test u64 count" would only delete the first tracepoint that matched that format. When the delete ABI is used all events with the same name will be attempted to be deleted. If per-version deletion is required, user programs should either not use persistent events or delete them via dynamic_events. Signed-off-by: Beau Belgrave --- include/uapi/linux/user_events.h | 6 +- kernel/trace/trace_events_user.c | 118 +++++++++++++++++++++++++++---- 2 files changed, 111 insertions(+), 13 deletions(-) diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_events.h index f74f3aedd49c..a03de03dccbc 100644 --- a/include/uapi/linux/user_events.h +++ b/include/uapi/linux/user_events.h @@ -12,6 +12,7 @@ #include #define USER_EVENTS_SYSTEM "user_events" +#define USER_EVENTS_MULTI_SYSTEM "user_events_multi" #define USER_EVENTS_PREFIX "u:" /* Create dynamic location entry within a 32-bit value */ @@ -22,8 +23,11 @@ enum user_reg_flag { /* Event will not delete upon last reference closing */ USER_EVENT_REG_PERSIST = 1U << 0, + /* Event will be allowed to have multiple formats */ + USER_EVENT_REG_MULTI_FORMAT = 1U << 1, + /* This value or above is currently non-ABI */ - USER_EVENT_REG_MAX = 1U << 1, + USER_EVENT_REG_MAX = 1U << 2, }; /* diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index dda58681247e..53980982a575 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -34,7 +34,8 @@ /* Limit how long of an event name plus args within the subsystem. */ #define MAX_EVENT_DESC 512 -#define EVENT_NAME(user_event) ((user_event)->tracepoint.name) +#define EVENT_NAME(user_event) ((user_event)->reg_name) +#define EVENT_TP_NAME(user_event) ((user_event)->tracepoint.name) #define MAX_FIELD_ARRAY_SIZE 1024 /* @@ -54,10 +55,13 @@ * allows isolation for events by various means. */ struct user_event_group { - char *system_name; - struct hlist_node node; - struct mutex reg_mutex; + char *system_name; + char *system_multi_name; + struct hlist_node node; + struct mutex reg_mutex; DECLARE_HASHTABLE(register_table, 8); + /* ID that moves forward within the group for multi-event names */ + u64 multi_id; }; /* Group for init_user_ns mapping, top-most group */ @@ -78,6 +82,7 @@ static unsigned int current_user_events; */ struct user_event { struct user_event_group *group; + char *reg_name; struct tracepoint tracepoint; struct trace_event_call call; struct trace_event_class class; @@ -127,6 +132,8 @@ struct user_event_enabler { #define ENABLE_BIT(e) ((int)((e)->values & ENABLE_VAL_BIT_MASK)) +#define EVENT_MULTI_FORMAT(f) ((f) & USER_EVENT_REG_MULTI_FORMAT) + /* Used for asynchronous faulting in of pages */ struct user_event_enabler_fault { struct work_struct work; @@ -330,6 +337,7 @@ static void user_event_put(struct user_event *user, bool locked) static void user_event_group_destroy(struct user_event_group *group) { kfree(group->system_name); + kfree(group->system_multi_name); kfree(group); } @@ -348,6 +356,21 @@ static char *user_event_group_system_name(void) return system_name; } +static char *user_event_group_system_multi_name(void) +{ + char *system_name; + int len = sizeof(USER_EVENTS_MULTI_SYSTEM) + 1; + + system_name = kmalloc(len, GFP_KERNEL); + + if (!system_name) + return NULL; + + snprintf(system_name, len, "%s", USER_EVENTS_MULTI_SYSTEM); + + return system_name; +} + static struct user_event_group *current_user_event_group(void) { return init_group; @@ -367,6 +390,11 @@ static struct user_event_group *user_event_group_create(void) if (!group->system_name) goto error; + group->system_multi_name = user_event_group_system_multi_name(); + + if (!group->system_multi_name) + goto error; + mutex_init(&group->reg_mutex); hash_init(group->register_table); @@ -1482,6 +1510,11 @@ static int destroy_user_event(struct user_event *user) hash_del(&user->node); user_event_destroy_validators(user); + + /* If we have different names, both must be freed */ + if (EVENT_NAME(user) != EVENT_TP_NAME(user)) + kfree(EVENT_TP_NAME(user)); + kfree(user->call.print_fmt); kfree(EVENT_NAME(user)); kfree(user); @@ -1504,12 +1537,24 @@ static struct user_event *find_user_event(struct user_event_group *group, *outkey = key; hash_for_each_possible(group->register_table, user, node, key) { + /* + * Single-format events shouldn't return multi-format + * events. Callers expect the underlying tracepoint to match + * the name exactly in these cases. Only check like-formats. + */ + if (EVENT_MULTI_FORMAT(flags) != EVENT_MULTI_FORMAT(user->reg_flags)) + continue; + if (strcmp(EVENT_NAME(user), name)) continue; if (user_fields_match(user, argc, argv)) return user_event_get(user); + /* Scan others if this is a multi-format event */ + if (EVENT_MULTI_FORMAT(flags)) + continue; + return ERR_PTR(-EADDRINUSE); } @@ -1889,8 +1934,12 @@ static bool user_event_match(const char *system, const char *event, struct user_event *user = container_of(ev, struct user_event, devent); bool match; - match = strcmp(EVENT_NAME(user), event) == 0 && - (!system || strcmp(system, USER_EVENTS_SYSTEM) == 0); + match = strcmp(EVENT_NAME(user), event) == 0; + + if (match && system) { + match = strcmp(system, user->group->system_name) == 0 || + strcmp(system, user->group->system_multi_name) == 0; + } if (match) match = user_fields_match(user, argc, argv); @@ -1923,6 +1972,39 @@ static int user_event_trace_register(struct user_event *user) return ret; } +static int user_event_set_tp_name(struct user_event *user) +{ + lockdep_assert_held(&user->group->reg_mutex); + + if (EVENT_MULTI_FORMAT(user->reg_flags)) { + char *multi_name; + int len; + + len = snprintf(NULL, 0, "%s.%llx", user->reg_name, + user->group->multi_id) + 1; + + multi_name = kzalloc(len, GFP_KERNEL_ACCOUNT); + + if (!multi_name) + return -ENOMEM; + + snprintf(multi_name, len, "%s.%llx", user->reg_name, + user->group->multi_id); + + user->call.name = multi_name; + user->tracepoint.name = multi_name; + + /* Inc to ensure unique multi-event name next time */ + user->group->multi_id++; + } else { + /* Non Multi-format uses register name */ + user->call.name = user->reg_name; + user->tracepoint.name = user->reg_name; + } + + return 0; +} + /* * Parses the event name, arguments and flags then registers if successful. * The name buffer lifetime is owned by this method for success cases only. @@ -1985,7 +2067,13 @@ static int user_event_parse(struct user_event_group *group, char *name, INIT_LIST_HEAD(&user->validators); user->group = group; - user->tracepoint.name = name; + user->reg_name = name; + user->reg_flags = reg_flags; + + ret = user_event_set_tp_name(user); + + if (ret) + goto put_user; ret = user_event_parse_fields(user, args); @@ -1999,11 +2087,14 @@ static int user_event_parse(struct user_event_group *group, char *name, user->call.data = user; user->call.class = &user->class; - user->call.name = name; user->call.flags = TRACE_EVENT_FL_TRACEPOINT; user->call.tp = &user->tracepoint; user->call.event.funcs = &user_event_funcs; - user->class.system = group->system_name; + + if (EVENT_MULTI_FORMAT(user->reg_flags)) + user->class.system = group->system_multi_name; + else + user->class.system = group->system_name; user->class.fields_array = user_event_fields_array; user->class.get_fields = user_event_get_fields; @@ -2025,8 +2116,6 @@ static int user_event_parse(struct user_event_group *group, char *name, if (ret) goto put_user_lock; - user->reg_flags = reg_flags; - if (user->reg_flags & USER_EVENT_REG_PERSIST) { /* Ensure we track self ref and caller ref (2) */ refcount_set(&user->refcnt, 2); @@ -2050,6 +2139,11 @@ static int user_event_parse(struct user_event_group *group, char *name, user_event_destroy_fields(user); user_event_destroy_validators(user); kfree(user->call.print_fmt); + + /* Caller frees reg_name on error, but not multi-name */ + if (EVENT_NAME(user) != EVENT_TP_NAME(user)) + kfree(EVENT_TP_NAME(user)); + kfree(user); return ret; } @@ -2641,7 +2735,7 @@ static int user_seq_show(struct seq_file *m, void *p) hash_for_each(group->register_table, i, user, node) { status = user->status; - seq_printf(m, "%s", EVENT_NAME(user)); + seq_printf(m, "%s", EVENT_TP_NAME(user)); if (status != 0) seq_puts(m, " #"); From patchwork Wed Feb 14 17:50:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 201081 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1389291dyb; Wed, 14 Feb 2024 09:53:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVnbOGoN0V6zk59pLEiur6345py6D0wqQqLQA74WRuYvY6QltHjBIjQCEUxG7kF9uL8r4qDnD5tuaUwfpeATp0iqyNolw== X-Google-Smtp-Source: AGHT+IGQ3K5xi9iqitGYDOJhe/bvAyXBrAbrIJqdtgfACeFfC+HjG1Cjw5kNyIOCQ6xQbjBm68HJ X-Received: by 2002:a05:620a:1133:b0:785:ec1f:df6f with SMTP id p19-20020a05620a113300b00785ec1fdf6fmr3539016qkk.9.1707933194274; Wed, 14 Feb 2024 09:53:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707933194; cv=pass; d=google.com; s=arc-20160816; b=LLTYJaEp/9Ob03WYiHsYMOfGJyjxI8MuJ+CvGqQ1O0UW4IsA2+Mxe/6xJQDPxCbeq9 QhBzFEwBc5VSyr6Ir62sY2Qm8FTsa/KIEKwa435IrH0oT9dYGP6h5tDmDI/t3UP951+J LPxs4UOpkjEwbr50gtJfY37anRHUfBWSdPHp/zO/+7od+UtkeXTVn6RlR2S6PJnZfH5u FLkBXyNsvThHtvP9jiHmkeUcUam68vg0YT3y6996n8aRTm97243ri12PzfsmsduZ+HAK jyHHOdTK02krYFP3DZcjWzBWRZzNfaOvIJJ6FEfZuPclJAOhMKXFhsSy6yw7m9u+xP38 q5qw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-filter; bh=KUDMkr8RLsAYmhZAS3vUhiwE/jC9o4CeWLT+JtE+tIA=; fh=D5DfrMzg0ZZU1DIrVUUu3TNsvUtblKBaNg2HganCEBI=; b=Ow/JrVJkfLVWu4Dg0HtDTCiHg+ohGkGvu04+jTLx+1xIc9kXUfaq60Wqfw0vweVMRg uti7znsEktqHl1n+QeVdFHD8voVP72Uer5nwjkS4Ap4eEaawTenMisjp6aZzVk1vgcRl oJ4aTF6jsapwUhMLI3YRcI7RhMwhRmQXAqL/o7pmWQtFC28wA8ZRu8P6qTMd5hPWpYPG YsMjsgraEVQZBnDQp/IvPfUakcvqYGeQ+6+oPcFqTu+aJA142MUM+8UG8srY5zG1KOpX TuMbnKjnjujctZzVLu96Uv+uB19en67hYmXHQq+qC5PpI2EJyOnaTT+2PQtZo8Zxk+VM k6Ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=gVENyiOr; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65718-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65718-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=2; AJvYcCX/pHR0dfa+4/4QMqkCRaNW4sp8VwGSDt0PXEIMtrludwDKP/bWTrPW1RI7wKj9PVWS4VWi1qv5szJtujDRYDGVrASZnQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id l21-20020a05620a0c1500b007872d50caf6si1403021qki.334.2024.02.14.09.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:53:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65718-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=gVENyiOr; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65718-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65718-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id DFE951C2A273 for ; Wed, 14 Feb 2024 17:51:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 859E31292DC; Wed, 14 Feb 2024 17:51:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="gVENyiOr" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AAEDA1272CD; Wed, 14 Feb 2024 17:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; cv=none; b=dl1nLdwr2bvsA6jjILXLSG4SHAAoATtcKOBCuXHPPRGStgUWVOore7ya5j/OOajafegBZTxhH0NFfoUPXrPJuM4efoMVJgu8uhrRkOf+btFFZ/aXNbTRmGrp9Q2C3+MyUOAVNIiSny+ny9AHOr5qI6XyxHRMGAxPgNdmP9FZDmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933058; c=relaxed/simple; bh=GOTOl32tyEUqyiUqhLrtlUNFRb2MzjMvHRjHDbTATds=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R28Cgutl5ekkq3aLn7NJ225WQW/+otQVycYQfPIegh67/3AWXozSsYFDWO6eCr7x/f7ZgsK5hUhhznH/TXqFw+G6/+jwquPgW1UOaZUuv39d8EhhtSAvGHbYsurXBJAPw2FQzgazCtevvYooX68/iD42tRBlT53uTPiEKYEqFKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=gVENyiOr; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: from localhost.localdomain (unknown [4.155.48.117]) by linux.microsoft.com (Postfix) with ESMTPSA id 4047720B2003; Wed, 14 Feb 2024 09:50:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4047720B2003 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1707933056; bh=KUDMkr8RLsAYmhZAS3vUhiwE/jC9o4CeWLT+JtE+tIA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gVENyiOrjYlmF2qT+1crlHDeN0fp2G9pX29HMYxt/sPGR1WHI9BxyFEV7U2ikRuiN w8jLFyWmmSKCJnfMj5K60PfsHFJkv0hJVnHkkPqK4SYPmnccMhyauAGNa2MmJ5nBEe K8shV3qMFgSVLuAiSDvTwfdEbqmTY6X1dVDi3PxI= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com Subject: [PATCH v3 3/4] selftests/user_events: Test multi-format events Date: Wed, 14 Feb 2024 17:50:45 +0000 Message-Id: <20240214175046.240-4-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214175046.240-1-beaub@linux.microsoft.com> References: <20240214175046.240-1-beaub@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897757097397854 X-GMAIL-MSGID: 1790897757097397854 User_events now has multi-format events which allow for the same register name, but with different formats. When this occurs, different tracepoints are created with unique names. Add a new test that ensures the same name can be used for two different formats. Ensure they are isolated from each other and that name and arg matching still works if yet another register comes in with the same format as one of the two. Signed-off-by: Beau Belgrave --- .../testing/selftests/user_events/abi_test.c | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/tools/testing/selftests/user_events/abi_test.c b/tools/testing/selftests/user_events/abi_test.c index cef1ff1af223..7288a05136ba 100644 --- a/tools/testing/selftests/user_events/abi_test.c +++ b/tools/testing/selftests/user_events/abi_test.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include "../kselftest_harness.h" @@ -23,6 +25,62 @@ const char *data_file = "/sys/kernel/tracing/user_events_data"; const char *enable_file = "/sys/kernel/tracing/events/user_events/__abi_event/enable"; +const char *multi_dir_glob = "/sys/kernel/tracing/events/user_events_multi/__abi_event.*"; + +static int wait_for_delete(char *dir) +{ + struct stat buf; + int i; + + for (i = 0; i < 10000; ++i) { + if (stat(dir, &buf) == -1 && errno == ENOENT) + return 0; + + usleep(1000); + } + + return -1; +} + +static int find_multi_event_dir(char *unique_field, char *out_dir, int dir_len) +{ + char path[256]; + glob_t buf; + int i, ret; + + ret = glob(multi_dir_glob, GLOB_ONLYDIR, NULL, &buf); + + if (ret) + return -1; + + ret = -1; + + for (i = 0; i < buf.gl_pathc; ++i) { + FILE *fp; + + snprintf(path, sizeof(path), "%s/format", buf.gl_pathv[i]); + fp = fopen(path, "r"); + + if (!fp) + continue; + + while (fgets(path, sizeof(path), fp) != NULL) { + if (strstr(path, unique_field)) { + fclose(fp); + /* strscpy is not available, use snprintf */ + snprintf(out_dir, dir_len, "%s", buf.gl_pathv[i]); + ret = 0; + goto out; + } + } + + fclose(fp); + } +out: + globfree(&buf); + + return ret; +} static bool event_exists(void) { @@ -74,6 +132,39 @@ static int event_delete(void) return ret; } +static int reg_enable_multi(void *enable, int size, int bit, int flags, + char *args) +{ + struct user_reg reg = {0}; + char full_args[512] = {0}; + int fd = open(data_file, O_RDWR); + int len; + int ret; + + if (fd < 0) + return -1; + + len = snprintf(full_args, sizeof(full_args), "__abi_event %s", args); + + if (len > sizeof(full_args)) { + ret = -E2BIG; + goto out; + } + + reg.size = sizeof(reg); + reg.name_args = (__u64)full_args; + reg.flags = USER_EVENT_REG_MULTI_FORMAT | flags; + reg.enable_bit = bit; + reg.enable_addr = (__u64)enable; + reg.enable_size = size; + + ret = ioctl(fd, DIAG_IOCSREG, ®); +out: + close(fd); + + return ret; +} + static int reg_enable_flags(void *enable, int size, int bit, int flags) { struct user_reg reg = {0}; @@ -207,6 +298,49 @@ TEST_F(user, bit_sizes) { ASSERT_NE(0, reg_enable(&self->check, 128, 0)); } +TEST_F(user, multi_format) { + char first_dir[256]; + char second_dir[256]; + struct stat buf; + + /* Multiple formats for the same name should work */ + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 0, + 0, "u32 multi_first")); + + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 1, + 0, "u64 multi_second")); + + /* Same name with same format should also work */ + ASSERT_EQ(0, reg_enable_multi(&self->check, sizeof(int), 2, + 0, "u64 multi_second")); + + ASSERT_EQ(0, find_multi_event_dir("multi_first", + first_dir, sizeof(first_dir))); + + ASSERT_EQ(0, find_multi_event_dir("multi_second", + second_dir, sizeof(second_dir))); + + /* Should not be found in the same dir */ + ASSERT_NE(0, strcmp(first_dir, second_dir)); + + /* First dir should still exist */ + ASSERT_EQ(0, stat(first_dir, &buf)); + + /* Disabling first register should remove first dir */ + ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, wait_for_delete(first_dir)); + + /* Second dir should still exist */ + ASSERT_EQ(0, stat(second_dir, &buf)); + + /* Disabling second register should remove second dir */ + ASSERT_EQ(0, reg_disable(&self->check, 1)); + /* Ensure bit 1 and 2 are tied together, should not delete yet */ + ASSERT_EQ(0, stat(second_dir, &buf)); + ASSERT_EQ(0, reg_disable(&self->check, 2)); + ASSERT_EQ(0, wait_for_delete(second_dir)); +} + TEST_F(user, forks) { int i; From patchwork Wed Feb 14 17:50:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 201083 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp1390983dyb; Wed, 14 Feb 2024 09:56:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVo5TvCoWD81XTDPfgR/Gk0CMUwtplMP70C1KX1/0YNwQxDfmgWMzlqBljnDXDr9nw+C5tyVfQTNpSKLceV1RoI8Rxx0g== X-Google-Smtp-Source: AGHT+IEFKEpFZ4u8kKx+VfiOvSTT/TMy0qNdGNwmoiPVYnJhPVgP6JWCLtIKzPpQ7PwtJylMBJ9E X-Received: by 2002:a17:902:8485:b0:1d9:8ddf:5fa0 with SMTP id c5-20020a170902848500b001d98ddf5fa0mr3452954plo.62.1707933386913; Wed, 14 Feb 2024 09:56:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707933386; cv=pass; d=google.com; s=arc-20160816; b=YuRilg2+7ENHL7T0HBXhmwLc0Q7qjYVcGRFGR3KMvu9z2EyYV9f6MLeDAezBEvIGlR JCv5cuOpZ+KfuDnvd9934ggWfxByEK2h9XsCeEa4+2VhtgtErbokBU4MTfDVk0DU4zAz RF2g5ck5KsffPSS4sY1AcEk5mstKRfZ9FXuujnaWWZXVUxoZ9/PeltXCWCpnCZJvZDDI loa06fYM8pGD7Nn5XhdTlPxgQCLh6PHDxLegyDgxBh2ZryUvv02Y0RDG64r9lKh9J8oW I1IEmG5UyKt4zihOxGdCVzM6VhzcMbs7IwnpNfmUmtjqI+O3kTwBvr7Pz1aw7rFn88oO Y0Fg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-filter; bh=9VOCGDdI8wf7ppg3CttBYebJqIACHcsfFVDg2ERPXoo=; fh=Czm9w8TuXaaSr0jwsCtpA7zlJi0qlQbciAYHiFPztps=; b=bcE44TIQZAGFWwI3MzmEYgqYhphL/uCNz00lNZ6O6KYI9/XuS6F7DTm8qw5G5ui7nd T0l/Jyd5IZ4mKB1j4EgNUTUh3fSivPV9e3457uBE12SnjU3CksiXmYFomZoe4G88xwL5 OUlXOHErjs04AbdSFn1dQS+Y1zG7dBB6FBfnNw8YZFbd4MMM163gfXVMp52OTn2SN0vX bUZSgsZyF+CgOv9VFXKVRK2PcvidveMNgaLkaG/ahEUrrfpyx1lfX1YtkaSyGa2h4N8+ obO1PyhCE1QEmNvawZxuHPuT90JAx4nO1R53a9WEqz4ZUt6bTMPGwELZ7bvAIuO/t/Bq kfhw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=tGzDrNLn; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65722-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65722-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=2; AJvYcCX4x7xxrAYu9zo9YtbJ25nM+9CI6ss7422ogXJdsk0dAttkzo82yUWroqB07yNfTNHLwpvDwKC2/Ks2HBRySFr3IcQN8w== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 2-20020a170902c14200b001da3197457asi4017560plj.650.2024.02.14.09.56.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 09:56:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65722-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=tGzDrNLn; arc=pass (i=1 spf=pass spfdomain=linux.microsoft.com dkim=pass dkdomain=linux.microsoft.com dmarc=pass fromdomain=linux.microsoft.com); spf=pass (google.com: domain of linux-kernel+bounces-65722-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65722-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A8C3C284C3C for ; Wed, 14 Feb 2024 17:52:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88E3F129A9B; Wed, 14 Feb 2024 17:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="tGzDrNLn" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C25C086644; Wed, 14 Feb 2024 17:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933059; cv=none; b=iEtBrxkauEcGE4/aBoeuZJoDgnXKE5bjOxGHtKvXwWI6bhr3/Z+j47FGHC3iTsxewTKx738ton7zsNQmUPVwGli6/epmdRD/AZaorHtG2EWMaCxz2rj/WJKd1m2pAxsToxy3P8L2I3kwYGOJtcu77w3kPAg8kScqlF/lLlWwfe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707933059; c=relaxed/simple; bh=1n+POexB4IxIKN4NogGOvLi6IfL+92Wk9tP/cRbJstg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tjjRrzwr7f8WjTxKaxv0IEGH6UErVg2QLfZVuY5NoyR4jY+2Eyd18DkEU8mcxyyZa/6M6gBviZP0vJH1YIe+7IlSB0rjLDEeiEft7GqX/NzAOMdEosmYYOnLKTWCVtF39DATXBYo5g2l7N9kROWz+LQtJaFkMZF5l+dP97btDD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=tGzDrNLn; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Received: from localhost.localdomain (unknown [4.155.48.117]) by linux.microsoft.com (Postfix) with ESMTPSA id 59AB320B2004; Wed, 14 Feb 2024 09:50:56 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 59AB320B2004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1707933056; bh=9VOCGDdI8wf7ppg3CttBYebJqIACHcsfFVDg2ERPXoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tGzDrNLnvdm7wkDwXHHGty2XSx7A6lxBHg7yWMvzCC0UqI+U8U4e2wbJGTtOg3XB/ RyKh3ul3asNW9w2nYqGIo8RnajGdnLV/4H3desJ8oatH+EB4pm/PlXaOW8DHN5EOkq e6pXtt2g9uuDLxBsAmBTsGpD4kImHeeEYObSBQVc= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, mathieu.desnoyers@efficios.com Subject: [PATCH v3 4/4] tracing/user_events: Document multi-format flag Date: Wed, 14 Feb 2024 17:50:46 +0000 Message-Id: <20240214175046.240-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240214175046.240-1-beaub@linux.microsoft.com> References: <20240214175046.240-1-beaub@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790897959393594936 X-GMAIL-MSGID: 1790897959393594936 User programs can now ask user_events to handle the synchronization of multiple different formats for an event with the same name via the new USER_EVENT_REG_MULTI_FORMAT flag. Add a section for USER_EVENT_REG_MULTI_FORMAT that explains the intended purpose and caveats of using it. Explain how deletion works in these cases and how to use /sys/kernel/tracing/dynamic_events for per-version deletion. Signed-off-by: Beau Belgrave --- Documentation/trace/user_events.rst | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Documentation/trace/user_events.rst b/Documentation/trace/user_events.rst index d8f12442aaa6..1d5a7626e6a6 100644 --- a/Documentation/trace/user_events.rst +++ b/Documentation/trace/user_events.rst @@ -92,6 +92,24 @@ The following flags are currently supported. process closes or unregisters the event. Requires CAP_PERFMON otherwise -EPERM is returned. ++ USER_EVENT_REG_MULTI_FORMAT: The event can contain multiple formats. This + allows programs to prevent themselves from being blocked when their event + format changes and they wish to use the same name. When this flag is used the + tracepoint name will be in the new format of "name.unique_id" vs the older + format of "name". A tracepoint will be created for each unique pair of name + and format. This means if several processes use the same name and format, + they will use the same tracepoint. If yet another process uses the same name, + but a different format than the other processes, it will use a different + tracepoint with a new unique id. Recording programs need to scan tracefs for + the various different formats of the event name they are interested in + recording. The system name of the tracepoint will also use "user_events_multi" + instead of "user_events". This prevents single-format event names conflicting + with any multi-format event names within tracefs. The unique_id is output as + a hex string. Recording programs should ensure the tracepoint name starts with + the event name they registered and has a suffix that starts with . and only + has hex characters. For example to find all versions of the event "test" you + can use the regex "^test\.[0-9a-fA-F]+$". + Upon successful registration the following is set. + write_index: The index to use for this file descriptor that represents this @@ -106,6 +124,9 @@ or perf record -e user_events:[name] when attaching/recording. **NOTE:** The event subsystem name by default is "user_events". Callers should not assume it will always be "user_events". Operators reserve the right in the future to change the subsystem name per-process to accommodate event isolation. +In addition if the USER_EVENT_REG_MULTI_FORMAT flag is used the tracepoint name +will have a unique id appended to it and the system name will be +"user_events_multi" as described above. Command Format ^^^^^^^^^^^^^^ @@ -156,7 +177,11 @@ to request deletes than the one used for registration due to this. to the event. If programs do not want auto-delete, they must use the USER_EVENT_REG_PERSIST flag when registering the event. Once that flag is used the event exists until DIAG_IOCSDEL is invoked. Both register and delete of an -event that persists requires CAP_PERFMON, otherwise -EPERM is returned. +event that persists requires CAP_PERFMON, otherwise -EPERM is returned. When +there are multiple formats of the same event name, all events with the same +name will be attempted to be deleted. If only a specific version is wanted to +be deleted then the /sys/kernel/tracing/dynamic_events file should be used for +that specific format of the event. Unregistering -------------