From patchwork Fri Feb 2 18:44: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: 196007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp655488dyc; Fri, 2 Feb 2024 11:33:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHTp5idlf2LPvxztmvNsbvnHP6g3rEUMPpZbeVw00D9unbpTlQHZ3sIo2p95gGhtmSBtdCx X-Received: by 2002:a05:6a21:3514:b0:19c:71bc:ec82 with SMTP id zc20-20020a056a21351400b0019c71bcec82mr3522570pzb.48.1706902391213; Fri, 02 Feb 2024 11:33:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706902391; cv=pass; d=google.com; s=arc-20160816; b=AQEWKKUtrJPNAAkD4KOlAKfrPA99gjsGx1Ld9OX+YfQZv4pQrFWr92zVX2vVSqGrsI 2lmizaKQxTCH2F8jwvxucTczBlCWq0DJLb1w23pc+jO0vUTSI5BbIZJpTgmQWeCKaT7B UBpyXYcn6ovo2XhbCLFLY1qv8R4bAce87+fZJToaGtMlb54SpF+M4nmfvRFndmYY8972 2Y+6mkkPOD44Pao70CFV1QKHAnM9YysY2n6ECsSCHd3272U1INcoSLsEItgzgFh0GuHb d2PprLHrgHfzYRdSTQqo3vn8E3/od5KVBB0M7sfF8/guNGoJeLJ1wgUCwoxUU3pLxqYk lg4Q== 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=4arN1d1NnrJYcNCC77SrMmzQ3peDQgWS/FWwlKcDITU=; fh=YJh/qigvKc1RoEw0nWJ3EN8dwlaObYcJikP0uqZ2arY=; b=dlHEzWtCGlV32GfZ82KncxVnYK8h/Xc9wseA3NW2ZNO1UE/wplvoOmUgrFGqcFbeE8 b/tA1PN/cDn13bXNP1e/5bN6VUVUiJLsV1LuwXy899UxdOB4tROElXllyjjP9Tsrk6k0 yoI/uq7y/mVxkipckFYT6t4r6Df7Aet6Z2zvoz6Nnbq03MzvTx3t5WUv3S0coZ3Hu9Lk XsoOhsdAYNJ8A4MKH74X9dYWm4y0CXEhselCvh9rjqbmJqPrhpvkucn+tqpQRUln9Eb1 J/62VnohKU9UOIP1eqsMVLMEcuQAdqGPB86QLm1xfLLPPMQZLx7Otj3zgaztWWjz0Wgi sJ5A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=OaYjw1GI; 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-50441-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50441-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=1; AJvYcCXpsbYZFmxGj/2DAn/MfWRlfL3PEYHVB4LG3eQ7mDG7F9/okgzNlJ1wnGYcEUs8b+z/YrrHe+9XaKQ+jJD1u76Nw5zPoA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id b21-20020a056a000a9500b006dbce4a7c80si1955892pfl.397.2024.02.02.11.33.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 11:33:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50441-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=OaYjw1GI; 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-50441-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50441-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id AFF4FB23112 for ; Fri, 2 Feb 2024 19:05:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FFD585931; Fri, 2 Feb 2024 18:45:05 +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="OaYjw1GI" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E04C415E5C3; Fri, 2 Feb 2024 18:44:59 +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=1706899501; cv=none; b=Qgqh8VvzOaxWBwEjI+0z3BAV4VQnXqjRZufNjqfy5vZA3tVSzTuAD3PNmfMlGmDVix09RRPAKGQvTl5sTor1+/uY0g5fw8I9KHI26CIznpmH2NTh2LU306jVft6iBT1XZEvdGi6Wk4pap8pvBPLM517qbsei6oCKopmqzJ6RQ4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706899501; c=relaxed/simple; bh=KenrMU6vAcUiIUu/TZYJLnCdcqT4s32o0HvqsMuQF1Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TKBlbLA02qNu/djRkaXgl25LSqnzaRjJ4uXQWFUxj71pU23YO1H1p1kAwCkjN+nj7dwoWEIuamG8gxZ3k8QOs1J8CNWPeHkdNT1FY1V8zREyoeYUsY8fpSrxeo6jbIWAgQ5WKCHx4aDP62H2Qynz6lSPKFwVufiGxyBoNqcFM6Q= 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=OaYjw1GI; 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.115]) by linux.microsoft.com (Postfix) with ESMTPSA id A4E1320B2001; Fri, 2 Feb 2024 10:44:58 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A4E1320B2001 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706899498; bh=4arN1d1NnrJYcNCC77SrMmzQ3peDQgWS/FWwlKcDITU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OaYjw1GI9ueWPT6T2RPGPiB0HCltl9hnCrLv22VwVho0aRdcYzwynuFFM/PpPlNr+ GIpqdfWfVyFSbPQNOgBAXJEQL4W1BMsO9VC64oa1yxtOUhKTt9N/bivnEAgxJy+8YW FP2IL3Q/CoMHCSuJnZ7vVy53VBPsNUPdJM1LSvL4= 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 v2 1/4] tracing/user_events: Prepare find/delete for same name events Date: Fri, 2 Feb 2024 18:44:46 +0000 Message-Id: <20240202184449.1674-2-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202184449.1674-1-beaub@linux.microsoft.com> References: <20240202184449.1674-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: 1789816881357867845 X-GMAIL-MSGID: 1789816881357867845 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 | 106 +++++++++++++++++-------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 9365ce407426..0480579ba563 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,32 @@ 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; + 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(group->register_table, user, 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 Fri Feb 2 18:44:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 196002 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp640581dyc; Fri, 2 Feb 2024 11:05:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IHLphdwrPLpe5okLVQWRMdduOoVnJWCiKP0EW+cO4Ldaemr698JRKUX9gToNUsWRLtkJ1LF X-Received: by 2002:a67:eb12:0:b0:46b:41fa:bce2 with SMTP id a18-20020a67eb12000000b0046b41fabce2mr3173694vso.30.1706900751536; Fri, 02 Feb 2024 11:05:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706900751; cv=pass; d=google.com; s=arc-20160816; b=Ai4M784DDkVQ03zrLAcKsEu85s2MZMfNBoBQ0FWIsO8G3ZzZ4sKAuJChLnBuD7ActD KXBKFzZXdYI8yv0W8vBGEahPf0RmjmRi2+vQSYneTtpqMuixPIJ2yNHqCRCLR4/dyIiT RJsGXQ79gyKlP9UCiVCtHuoI499bodvWHqRLwXT24oD+38vgVNFqAwkxZ2AyQcOmdUzx lOLtyVDENUj0KfVlmIfJyVmohO5s0hjgszCMwoQ/6Kd/Vu86v8WfqBJvJltgU4hTjcwF gu6jUPtolzBITg4/JPa1hDBwRCmcpyAxf8cv9jiJfYbdUhwgtIyvEqst2+Jb8CMx1kaO E04w== 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=xAvDbcYNQdwBPsYOWD4AHbNAaRlA5aPw4b4jCaAtpio=; fh=DqEDZ85kWeoTCqJ5XM0erXBVAt2luZyiVWbJklwM6CM=; b=PXxDhUFokneHX/OlRV4xrEEiTM0HdkCo2ydWwSNEVh/e0MURgIgiJNGEB+r45IOLfg Xo0IV5JeZ6W9mkewh6Ce6PzsF8MSP2adbQPKz5q0zhGFiLiWtr4D4qTfVfkbC3Z6Djz1 BUVQePaKV5sNXDJYX4qRaEXmai0J5aaFcv9SltDuu5kQlgc5t9tOA9a7WgoYpQRPz4dB xK3a9wiccpzwYhj/j0zYhJQLpmOMQjkcdooQk/tQkYLiBIYSx/uLos4+9nrf4tOpt4Cp a2hYDN3k71vJbP8EAwWRssmlp0DPJSruUC5yxWc5esBVR2hd/GF8CuJ7a5O9m7IaAtkP tf5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=lwJAmpdN; 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-50442-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50442-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=1; AJvYcCVNTUKBvyXSY23CiG3ozr1HJBy4KeByYTxMgqBCGHj8A6IJJ89qS1Bp8/n4CNV4BScGuXSuiYuBoUcMy7OjtxC76Lltxw== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id q5-20020a056102204500b0046b359bcb11si409557vsr.439.2024.02.02.11.05.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 11:05:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50442-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=lwJAmpdN; 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-50442-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50442-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 09B071C2870F for ; Fri, 2 Feb 2024 19:05:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44B4E85955; Fri, 2 Feb 2024 18:45:06 +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="lwJAmpdN" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E06208063F; Fri, 2 Feb 2024 18:44:59 +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=1706899501; cv=none; b=pBvg8P1Moc3Hn6Id464jTGbKBwiEeuzHdtOUgGswD/zcT3kidDolVqpDvNNRwb3wBqOzZSHY2hzT0dCEOH0YCkDXjDmE4bSHZmGSr0MkktGzOMeO9vq6KFX+xTndzRu7UYQMCZRr1fBtJzi+aeGeJig4/gB/qN913hGm7fUl7o0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706899501; c=relaxed/simple; bh=PGA5x1CNboTie0nOdZdTJ56V0iSFqEMT34TyLgt4BHw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CNObBTZ78iXt/hbhw0ptesLHSkzjnwvXAvABdZ9Evcf8ow/znwS/DlU7/PkQdU4UyUqY93coTK56hD7Z91hdbLR1Fgsdk0hyDLyTAdUcvt+M8u5eNeoUe8/RDLNZV9Ozl3mBEQxZTAdJxvAegH7egjb6rCKa/C913/7O1rgVtDY= 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=lwJAmpdN; 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.115]) by linux.microsoft.com (Postfix) with ESMTPSA id C072920B2002; Fri, 2 Feb 2024 10:44:58 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C072920B2002 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706899498; bh=xAvDbcYNQdwBPsYOWD4AHbNAaRlA5aPw4b4jCaAtpio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lwJAmpdNQxVQ5gD0PZWQd5iL+kYYf3sT+2b7zpsVi3s2UvZLFVOwMGa/rEg7qfX05 LCHW8bcNUTx38oeTk5LHCxNnSvdxlSwH7cTeKkYYEvfIyMxP88H/c8Dmt5NGbidd+x TKlH8LwKohK4WhJ2FQOi3ZxHoy/PMzOjvqTty0Wc= 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 v2 2/4] tracing/user_events: Introduce multi-format events Date: Fri, 2 Feb 2024 18:44:47 +0000 Message-Id: <20240202184449.1674-3-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202184449.1674-1-beaub@linux.microsoft.com> References: <20240202184449.1674-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: 1789815162276059856 X-GMAIL-MSGID: 1789815162276059856 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 0480579ba563..608750e4e6df 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; } @@ -2640,7 +2734,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 Fri Feb 2 18:44:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 196004 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp643562dyc; Fri, 2 Feb 2024 11:10:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHx1nRyJt+Hn4pjPHxWjmFzVKFqQN/8wUe0jTLO+30T+DHS/IltHH16hQ0V07D77jYK8ILf X-Received: by 2002:a05:6a20:9719:b0:19c:8447:1c41 with SMTP id hr25-20020a056a20971900b0019c84471c41mr8770646pzc.44.1706901047543; Fri, 02 Feb 2024 11:10:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706901047; cv=pass; d=google.com; s=arc-20160816; b=kGosPE5q+sXj4PvfCFFW4XggVmYpd4u8TKgssXWabsdRtqrBChgE7AZ0tE3Vz5ge1E qGfl1w0ea86TpVWYe8AqZKG17J0hdcCmfuYvkPgxI1SOb6OQeuY9J/qTL2lsX4P5OYQY J0DTuwm2Z0JPrcCeUkaa/npeOsOFqfNniv8m6VkAPXxoH36bi8aNaQGHZNqJjJQnHHtW mOjl041JwAalPs3+woBBszR1LumBbzeU+LjEKrcOEQELDQoPoi8VNPeu1G0sLu3K3qfQ MMvfWIzLUxrPs0vPEg50BDatGmX9NhOJ2wWEB2QMwzbXCaotOWaYVL0MeeQoP4xS3hEX Wvgw== 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=zKKt6k94H8Ff+3OlTPD1kootH4Ys7tp+SJA6p2pu5oU=; b=ZRw2zREBmdZUwB5w0QZkXj23Y4mwec8Ctlq+qtPZmVuoBVbaWLN5dzRhlahi+wG+BH 6dEutZRhJvwWa8V0GRtOsyN3pvKuMwcZ3wT9oHcbJxQoG32R9MrzPHovinBs2H8WZ4mp O67zltkAmi0Dh7fJa1edt2Pfq8n0seIWGB56lNF1m9E+VAi8iepMzoo920gf6LpU2duA Pr3MoIeOxX31SzY7qjxLxAkgkrYbJ/xCRlX58dZxrkNCB0n0vU328QdXNHVinsVKfg2G 2066CEfqKxJM21LJRSdokpQUlQNslSPLKHH6N01qYX3FD/WcG7npU1WrhZ3dKaGYVsVh hgYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=CrihQ6FJ; 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-50443-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50443-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=1; AJvYcCUhE5PSdN+VyQ4+x3LuNw8NTvqYDTedsiA6kFmTxMQMf1qR1wppQKOhBqApgHldIFrLV9Bw5g4i7fjRRJS1PgNn9K73fA== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id p11-20020a63e64b000000b005cd6507bb84si2020986pgj.183.2024.02.02.11.10.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 11:10:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50443-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=CrihQ6FJ; 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-50443-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50443-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 EB85C29501B for ; Fri, 2 Feb 2024 19:05:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EA8E285922; Fri, 2 Feb 2024 18:45:05 +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="CrihQ6FJ" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E05948063D; Fri, 2 Feb 2024 18:44:59 +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=1706899502; cv=none; b=gIDFwzOfrhwOZGpKXl+vYY0G9B0bSnt2npSA8FJgJl6EEz8fo/f6S8UDbV59leZWA4aa3ACSeGYmhh+rXhTNp4IaTshR3Cc7EBPAf4fdLEA3lf1ofBP52l7MVGCCDkcrZOsXtyd4jgfQVW36vstfaGvTsjVR5IkwvWfJs3+gS0E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706899502; c=relaxed/simple; bh=GOTOl32tyEUqyiUqhLrtlUNFRb2MzjMvHRjHDbTATds=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X1DsWpBh1UVXTfFCBfTG9c5Rz8FBxys93p+z5XUyqQ8Z9BH6+skKEJeq5pRscpqR+a8iTQ+zFBU48s3UeIoag6EjOsz+LeogjMFzEw7MUaZHRzepN7YYnpvhoYlMT2rlly1t7P0qPTNmDfOjnEM8ANJKb9ynnEIXm7RzGdWFSlw= 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=CrihQ6FJ; 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.115]) by linux.microsoft.com (Postfix) with ESMTPSA id D8B8520B2003; Fri, 2 Feb 2024 10:44:58 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com D8B8520B2003 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706899498; bh=KUDMkr8RLsAYmhZAS3vUhiwE/jC9o4CeWLT+JtE+tIA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CrihQ6FJTtYTCwYDlu0e3y0carlrbGPga2EFZvSlqG2DlZ7fflirlfAuttNUW8ssp CULnETctYnIY+xC8P7gvi+aHoOs8kUQR6tMfIfjMYS3PcG2lQE3dRyC1qmhe2C5NpU mMsyHvoYtoFHwhkpEs22uCYo3UojXPalmk06C7yQ= 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 v2 3/4] selftests/user_events: Test multi-format events Date: Fri, 2 Feb 2024 18:44:48 +0000 Message-Id: <20240202184449.1674-4-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202184449.1674-1-beaub@linux.microsoft.com> References: <20240202184449.1674-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: 1789815472910723749 X-GMAIL-MSGID: 1789815472910723749 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 Fri Feb 2 18:44:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 196001 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:9bc1:b0:106:209c:c626 with SMTP id op1csp640406dyc; Fri, 2 Feb 2024 11:05:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IG+qeuI4CP3AXynlHjbS21iI6tlqoZaD9AG44QfbOrutIIthVdLp77bYo0eHj/qHtmYiZpJ X-Received: by 2002:a05:6102:38d2:b0:46d:b5d:7c74 with SMTP id k18-20020a05610238d200b0046d0b5d7c74mr1446491vst.16.1706900735460; Fri, 02 Feb 2024 11:05:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706900735; cv=pass; d=google.com; s=arc-20160816; b=PI9GMUnA0Yt5dsk+VDtha6IwxS24JslPkbnzX9WAdibssPr5Tan5GJCfcO8L7xtkyK buGSX06oMW+hbhvIPQlNr+TYvNXxXEQl0eXxa8M0nsNlzl+QPgfTxWcEwFK5so/o1tgw sulagUa/6BfBizdsg6VjWavuXXCJ1CaqcKhy5PGYfSW/nqcGbiaZG+w0Y7loplea0p5y V8Wm0jzDjHrU9bU83oJTAMjEhpwkEUoMy2hPmD+brW4h+4BSYNzj1Wc93nH/NAMzjfbm WQQeNST9KqTpGs981cvwktVKv/hDzDsk6H5vU+od1fmAmvHEjlb8npMB+Aj4OwKNrbbX MEDA== 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=Xkfdqj8XMES2e5YXeM7kBzpoGd3k9cxNDmaZwWD8+Lc=; b=WtSOa/jk/XEfMUesZo5+18PJEDXCumzRM/BcCOHcFQa9IassLFWz4YfM7tgon/8sVB nfHrq4I/FBfr5gBYPk5xXe+VFTPZcOIG1ATVsRBJvxREwrWr9scl8FbGkPncoL2BD1vF Vf6UqCblHrlAbb/nIKJXM3TYB+Yx4UBfTg5OAW4zobWsKrvtEOzBN/EXgC+kHfaHH9HZ GBZTzXWVMxtbR9ldMzJDDoZ45thvQVHqAT8oDUQTPX0aeo33ZxgDvJkov7wWoVfIschK CsVyE8Mk2dsvY0zkmMAXSrYk/rf4aRVsY/2StRmXbqP3GfRSKMhUs7KjNPF7DROHOjEL j5ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=ChIloMUd; 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-50439-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50439-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com X-Forwarded-Encrypted: i=1; AJvYcCWKgc40v9Hup7SBTxtyi6Xptf6v+MlfZieZnmsjgID2fGOjFX/pIm54iLNbkYvfND4uwpSH8k0EWtiNP0qMO2dmiUMZdA== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id k14-20020a67fd8e000000b00466ff89574csi393246vsq.723.2024.02.02.11.05.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 11:05:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-50439-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=ChIloMUd; 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-50439-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-50439-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 B322A1C270A0 for ; Fri, 2 Feb 2024 19:05:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7196983A09; Fri, 2 Feb 2024 18:45: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="ChIloMUd" Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E054980630; Fri, 2 Feb 2024 18:44:59 +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=1706899501; cv=none; b=V2dO+QXrxW7SkDZRK5M3bF8VyAfEEXnE9uBUa1Q0CYsIDSWIE7sFR5NbsuUBFNYZkh6N2i4/4QjajKhgUY3j4CuJfJyibjKg1qhJ4zCBJ7pBMjE1GvbY1PPzyKmY9XfsLjXM2WmveQf6/V5/jufnTWxCGNToHUATa2aSkGalgsQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706899501; c=relaxed/simple; bh=1n+POexB4IxIKN4NogGOvLi6IfL+92Wk9tP/cRbJstg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BepaPCQWKJ6MRy1et/o/j96mJCqxjhZVa2zt37dR49+hrmXxabvbH6x30aYpMBPoVOBHPybyP53KtONM81dkbMzWTmxcrlP9nBipQs+yN2FS+3xGJ6G+79ntX3THJc5ZIRgXWg9qfKhRKvBvHWZy689Qz2p2VjfSAwGjpwMHpok= 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=ChIloMUd; 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.115]) by linux.microsoft.com (Postfix) with ESMTPSA id F24EA20B2004; Fri, 2 Feb 2024 10:44:58 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com F24EA20B2004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1706899499; bh=9VOCGDdI8wf7ppg3CttBYebJqIACHcsfFVDg2ERPXoo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ChIloMUd7Yz15+MUeaAt+evM/yKzUa9w93jRRTjQVtaLOdVIgmvoHP8fjPd+/yO8P Q73voyHyfz2JAKH0aaLznSS4grT74qYZBB5wkychvJe0WsJ79hSy8wLwNgAUkUwat3 ZGSqUd7CAoF7XRQKb/wFZmqyZGEB4VyyUq4ug4ak= 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 v2 4/4] tracing/user_events: Document multi-format flag Date: Fri, 2 Feb 2024 18:44:49 +0000 Message-Id: <20240202184449.1674-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240202184449.1674-1-beaub@linux.microsoft.com> References: <20240202184449.1674-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: 1789815145520488370 X-GMAIL-MSGID: 1789815145520488370 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 -------------