Received: by 2002:a05:7412:2a91:b0:fc:a2b0:25d7 with SMTP id u17csp757135rdh; Wed, 14 Feb 2024 10:20:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW9PdHpVVTBbCLmTkeIQPaPo78aq88O6RWVGH+jRhlGE37/RPBYHt6y2jD+TnSNzeh0ohJgbIX76JxYe9Gx+IMnLDyj+qidZa788SPLNA== X-Google-Smtp-Source: AGHT+IFR9iolne1Z2xnBl1wmptINz6oOWIpHeOHEnb0QWPsGjSYhwbvidH0iLuMIWdYN7+kNCzL4 X-Received: by 2002:a05:6a20:94c6:b0:19e:a85d:166 with SMTP id ht6-20020a056a2094c600b0019ea85d0166mr4902896pzb.9.1707934843878; Wed, 14 Feb 2024 10:20:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707934843; cv=pass; d=google.com; s=arc-20160816; b=WqvV7NLLTeOGqTJnVaXMWnimKIlCl9JMeeZBz5bkpEvB5E9oUw+3wux3l1YKVTSxSf 1cfhKAM+DdouCMrPey8eS4E8+Q0FyaMn4XGy3xKufjLPzlKv/eAO8CnsYSF8p4++78dv tU6yTM7qrcHUYj/0Famh/KG5+9TVk/t3zCHasuyl9vEzq0AEeL3ayzzE+Oog6mTtDkEC nE050dH+LSkfqkWD7Yp58K9omJ12cU2MHOnnrpIMdL9ISIR6MeX1SLtXN6f6yubDgFKu ASGXwJ6fOsG6d661739xX2H6De//lXFx69kTmsIAiBvn4Bw4KpN5+Lb6MP3CbyqvgVcq nphQ== 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=bxEMWakODMr7yKY9jwSuyHTInUdikjyWeEEvtIg8lyI=; b=LE9Ldy6fTc8pGaUYTiolO9u05lMSnRAEEUHHKzODHM8pWAmb126yGX2+nijthdgADF 7mUfHQZgeLp6hEKxXuWxdYh1lH5sKLPU5mqGH3DJuqVI5nzsO+lOHDj13uIGhL0KvCJb hmcbmPITAzqp2hllMPWuHoiGZ15t+NEZfE6LcHJxAiI/YOfEsI+mBNJcmtTPQg3rrye3 qlaC4wONvD+vKkvT4qGC2PFJD1+yZ70tzyobsOa2IE+V8Xq+5kjxfxO6oNkh+r95Uq8O QG81VGe4v7/k0NSnAcRb8Qwnbumt4O9vMtOzoc+bNps5r7kVBS2F2+Rm0xjMc4uuCk52 +tAA==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65720-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=2; AJvYcCXqm2CChLWserSG8DGwYkoZU9LkXHDZNj9PSxRR2H7EOdl7UD1wF15o5lKGOY0RhJIcoI97b/91J/MH6dJrZELJ3du1c3YeG5/m2/2AMw== Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id z27-20020aa7991b000000b006e0a347f6ebsi8214714pff.90.2024.02.14.10.20.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 10:20:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-65720-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-65720-linux.lists.archive=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 3743EB21318 for ; Wed, 14 Feb 2024 17:51:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BDF0512838C; Wed, 14 Feb 2024 17:50:59 +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 Content-Transfer-Encoding: 8bit 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; } /* -- 2.34.1