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; } /*