From patchwork Thu Jan 4 21:57:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185158 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5891836dyb; Thu, 4 Jan 2024 14:01:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGMXswgvKgt0oxh9qAsgR/l6JZc188uLoj4e25qjjxSS2IcrKQj4fr/DN40kNd0nu7qViiM X-Received: by 2002:a05:6358:2c8e:b0:175:707d:e937 with SMTP id l14-20020a0563582c8e00b00175707de937mr197898rwm.50.1704405670124; Thu, 04 Jan 2024 14:01:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704405670; cv=none; d=google.com; s=arc-20160816; b=jz1bYPgbtbCfLcl54wTIkpLTV1ClI2086r7tE8ipMSNZ3V0dOIt1wLHEhAC0jI2fWa EJ71HUSpZAZBerSsum1p9QOK0JCqwQrqdglDx5+o3OHGSMi+Ql9vaFwTaKVlW0VXnDGG sdTUfTCC7HKc4voEzzIm472engm8A7XgJuNRe0si0Nfsk7wCXX4JTmpan8z6NEevCFnc HcuTTNlG34EgumLMyXHeqLM1GAEjrcCMzp2uscLkwE7UOMEuS0FMSVv8tJD+gokCQiWC 3XRkSxqnR4SGB37Tp4I/PkH/qXIlrNyHWG3blJgbUR1JUVfSNYuE7XgwinZuLeogrKdM rYrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=BBKNxJdU71KTHcJLsF4+Dg8KGYR/XUYxLER780f9pWE=; fh=/+b36Kyncow8zkAf4U206kHBsn7X4TQclH3EtkME/8M=; b=xurPUE/HCTqMFFcvGKvRO1e2o4waiBIF1xQheKDHJoiGoCcbVK+qG6moOweb3U1i31 3XnSFHLdbytnkNa4SmeubHUONwcO/Oi7+Afdh+CBdJ7YiEVhTCzutHokA8Rj+JcdBVae ou9vCF4PJrUHX6RnW/7mmfo2ubnzXJKl1tXjLS5GDImsACFlNKvrH05yPIc3qSQKhPg3 Oc1DeiuO8NxLsJo3BUNJgW1pVOBHtxt3raDf0C4z1pxNWKk20D9EFk3Cctu8GbUjqv7l 5BuB89oEz0WSX8sr5WFmt0Elqu1MR3SrnVUPm0pzHemmdY3bpMMnbEd0tRcL6PZAciZ2 WlcQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-17264-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17264-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w186-20020a6362c3000000b005ce0474353csi234880pgb.238.2024.01.04.14.01.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:01:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17264-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; spf=pass (google.com: domain of linux-kernel+bounces-17264-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17264-ouuuleilei=gmail.com@vger.kernel.org" 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 60FB0288454 for ; Thu, 4 Jan 2024 22:00:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0B4F2D792; Thu, 4 Jan 2024 21:59:43 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 111622C851; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A76E5C433C8; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLVlg-00000000vgE-0dzN; Thu, 04 Jan 2024 17:00:48 -0500 Message-ID: <20240104220048.016261289@goodmis.org> User-Agent: quilt/0.67 Date: Thu, 04 Jan 2024 16:57:12 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH 1/4] eventfs: Have eventfs_iterate() stop immediately if ei->is_freed is set References: <20240104215711.924088454@goodmis.org> 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: 1787198879801468111 X-GMAIL-MSGID: 1787198879801468111 From: "Steven Rostedt (Google)" If ei->is_freed is set in eventfs_iterate(), it means that the directory that is being iterated on is in the process of being freed. Just exit the loop immediately when that is ever detected, and separate out the return of the entry->callback() from ei->is_freed. Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/event_inode.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 72912b5f9a90..0aca6910efb3 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -788,11 +788,12 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) name = entry->name; mutex_lock(&eventfs_mutex); - /* If ei->is_freed, then the event itself may be too */ - if (!ei->is_freed) - r = entry->callback(name, &mode, &cdata, &fops); - else - r = -1; + /* If ei->is_freed then just bail here, nothing more to do */ + if (ei->is_freed) { + mutex_unlock(&eventfs_mutex); + goto out; + } + r = entry->callback(name, &mode, &cdata, &fops); mutex_unlock(&eventfs_mutex); if (r <= 0) continue; From patchwork Thu Jan 4 21:57:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185156 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5891570dyb; Thu, 4 Jan 2024 14:00:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IFXld3vqcu7JCPKYcXAvByqsjcdh9uaIaHbJ4ErQYOGYmneVkJQI1YCpTMl2gsrEWTD+h6k X-Received: by 2002:a05:6214:240a:b0:680:d22d:7bb9 with SMTP id fv10-20020a056214240a00b00680d22d7bb9mr1471586qvb.65.1704405646798; Thu, 04 Jan 2024 14:00:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704405646; cv=none; d=google.com; s=arc-20160816; b=xpfHeU7YeyzfTXNdY1zmV/NsARZ673kjMVz+5fIqZQ/bVY7Fe5k7PkP5AiUqSF/Tux BOwuKx0NZlQGzE8LKh3+pKe5yD14saNfRCWziujqb5Sg0Of0C+yEiFN3CEsbjZ3M4TPC qtnLAPYTXOniE/qGP8dcUQOzh8KH9jYxuQnn5bWRgb9C79uf3iXLnwhP0s2nj/NPsLuK vVuplKtKzz0iwptWOe23efvkcbBk9Vplv04T9h5L+t1lD0SiXSs6umUnxkms48037DsV rAIS3ewWkCaTu6aTza094wLgxoFNr5hugdCJx1HKsVptMYsNL+riLBUHjiEl7aficyRq oXbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=KI8HvX4lhQOABjoOULNgrVn6ncqeU3JFeqk0jr68UuI=; fh=/+b36Kyncow8zkAf4U206kHBsn7X4TQclH3EtkME/8M=; b=tJnHfvSZYMfcOK1BA9/mVIjio/Cq49RhKGzurOQD9Z8BcBi6qt7inqJVGhzMyG3RD6 DX3VQ++pi4YIfydqNcoqfP5XX2dgVRfCENKmCj/jCRebSHfhGcBDqX8F+N9vBaNjVa0z nxRiOBTqrVlkga0XG3z7Uvge3df3cfzRkdbgvMqbC1fznW5Ew8lT0PA+Cm8c4JetIAoN FtzA/UxogvKbp5t7YHhvsbcxkG64LE0L+hTh6vsz1oyX1qDYBofmMzxQbIF1YltVoYj3 wharOWr+R8wDdgKIzIjcAZJY/fmVfTXldtm9+BGrS2oUJ8IodpDuCZj9lWl3jfK0+HEC +a7A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-17265-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17265-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p6-20020a0c8c86000000b0067f8d43804dsi382600qvb.453.2024.01.04.14.00.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:00:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17265-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; spf=pass (google.com: domain of linux-kernel+bounces-17265-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17265-ouuuleilei=gmail.com@vger.kernel.org" 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 95EAA1C20ABC for ; Thu, 4 Jan 2024 22:00:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AEA572D796; Thu, 4 Jan 2024 21:59:43 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 111932C854; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AAC44C433C7; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLVlg-00000000vgh-1JSa; Thu, 04 Jan 2024 17:00:48 -0500 Message-ID: <20240104220048.172295263@goodmis.org> User-Agent: quilt/0.67 Date: Thu, 04 Jan 2024 16:57:13 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH 2/4] eventfs: Do ctx->pos update for all iterations in eventfs_iterate() References: <20240104215711.924088454@goodmis.org> 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: 1787198855407299872 X-GMAIL-MSGID: 1787198855407299872 From: "Steven Rostedt (Google)" The ctx->pos was only updated when it added an entry, but the "skip to current pos" check (c--) happened for every loop regardless of if the entry was added or not. This inconsistency caused readdir to be incorrect. It was due to: for (i = 0; i < ei->nr_entries; i++) { if (c > 0) { c--; continue; } mutex_lock(&eventfs_mutex); /* If ei->is_freed then just bail here, nothing more to do */ if (ei->is_freed) { mutex_unlock(&eventfs_mutex); goto out; } r = entry->callback(name, &mode, &cdata, &fops); mutex_unlock(&eventfs_mutex); [..] ctx->pos++; } But this can cause the iterator to return a file that was already read. That's because of the way the callback() works. Some events may not have all files, and the callback can return 0 to tell eventfs to skip the file for this directory. for instance, we have: # ls /sys/kernel/tracing/events/ftrace/function format hist hist_debug id inject and # ls /sys/kernel/tracing/events/sched/sched_switch/ enable filter format hist hist_debug id inject trigger Where the function directory is missing "enable", "filter" and "trigger". That's because the callback() for events has: static int event_callback(const char *name, umode_t *mode, void **data, const struct file_operations **fops) { struct trace_event_file *file = *data; struct trace_event_call *call = file->event_call; [..] /* * Only event directories that can be enabled should have * triggers or filters, with the exception of the "print" * event that can have a "trigger" file. */ if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) { if (call->class->reg && strcmp(name, "enable") == 0) { *mode = TRACE_MODE_WRITE; *fops = &ftrace_enable_fops; return 1; } if (strcmp(name, "filter") == 0) { *mode = TRACE_MODE_WRITE; *fops = &ftrace_event_filter_fops; return 1; } } if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) || strcmp(trace_event_name(call), "print") == 0) { if (strcmp(name, "trigger") == 0) { *mode = TRACE_MODE_WRITE; *fops = &event_trigger_fops; return 1; } } [..] return 0; } Where the function event has the TRACE_EVENT_FL_IGNORE_ENABLE set. This means that the entries array elements for "enable", "filter" and "trigger" when called on the function event will have the callback return 0 and not 1, to tell eventfs to skip these files for it. Because the "skip to current ctx->pos" check happened for all entries, but the ctx->pos++ only happened to entries that exist, it would confuse the reading of a directory. Which would cause: # ls /sys/kernel/tracing/events/ftrace/function/ format hist hist hist_debug hist_debug id inject inject The missing "enable", "filter" and "trigger" caused ls to show "hist", "hist_debug" and "inject" twice. Update the ctx->pos for every iteration to keep its update and the "skip" update consistent. This also means that on error, the ctx->pos needs to be decremented if it was incremented without adding something. Link: https://lore.kernel.org/all/20240104150500.38b15a62@gandalf.local.home/ Fixes: 493ec81a8fb8e ("eventfs: Stop using dcache_readdir() for getdents()") Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/event_inode.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 0aca6910efb3..c73fb1f7ddbc 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -760,6 +760,8 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) continue; } + ctx->pos++; + if (ei_child->is_freed) continue; @@ -767,13 +769,12 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) dentry = create_dir_dentry(ei, ei_child, ei_dentry); if (!dentry) - goto out; + goto out_dec; ino = dentry->d_inode->i_ino; dput(dentry); if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) - goto out; - ctx->pos++; + goto out_dec; } for (i = 0; i < ei->nr_entries; i++) { @@ -784,6 +785,8 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) continue; } + ctx->pos++; + entry = &ei->entries[i]; name = entry->name; @@ -791,7 +794,7 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) /* If ei->is_freed then just bail here, nothing more to do */ if (ei->is_freed) { mutex_unlock(&eventfs_mutex); - goto out; + goto out_dec; } r = entry->callback(name, &mode, &cdata, &fops); mutex_unlock(&eventfs_mutex); @@ -800,19 +803,23 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); if (!dentry) - goto out; + goto out_dec; ino = dentry->d_inode->i_ino; dput(dentry); if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) - goto out; - ctx->pos++; + goto out_dec; } ret = 1; out: srcu_read_unlock(&eventfs_srcu, idx); return ret; + + out_dec: + /* Incremented ctx->pos without adding something, reset it */ + ctx->pos--; + goto out; } /** From patchwork Thu Jan 4 21:57:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5891633dyb; Thu, 4 Jan 2024 14:00:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IGf8fUdBdEo1AIA3CxQ+SJEIZAx6oPwgYI9fpAKt/Oq5oV7bo/lPIO55AugU311iXuaD0sZ X-Received: by 2002:a05:620a:5595:b0:77f:3db5:7b69 with SMTP id vq21-20020a05620a559500b0077f3db57b69mr1432305qkn.32.1704405652056; Thu, 04 Jan 2024 14:00:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704405652; cv=none; d=google.com; s=arc-20160816; b=fNkiwH0isWPh+tVKh6f2T+JcYORU8xg4NelrUvXskeBDsh01d9UFVw5yZg/n9gKJvC kjb8Kv2lDqdVFjb2OVuTuEmSZpA73LjD8W6SkpD24AjFS2ecC+Wz5oiKmcDRTGluAN+I NJHmJdwvX0FMoTq5JQAAc9Hl006QmLWYMjA+XWb0Ic5LXdMNvHZq4tNv8vUfpbhmj+tF jsX1brncDVlU8guRwzDWgmBauI01aaZqckEkJML1SBvYeEVOJnR2Zh96xW0KEXjfZKmd U9KolIAqHCFrWD3+jPWbZDFtiOVG6X3KcwwomsrZKeyh3UZmO+9x7K7/2ZXotRgj5piC 435Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=EbuEc+3+/dM6pxEHoHz7TMraZJmVVWSnEJNirB5z+G0=; fh=/+b36Kyncow8zkAf4U206kHBsn7X4TQclH3EtkME/8M=; b=gIA6b5GKj26jejf7WS9cuM4oyqSbOoOT04PyIkbUuFBdR6+xnM62S/OFSSZhrMlISF 0WtX9YF6EbCzvvIapDfuyoomzlfvqyOkwPcd5JwbyvWCgdiYkaKyG+DClJ7vxV+n5G08 zpgB/no5OULeGJcFFCqVEx4uAeamLBFNWYozVBb9l0u9P7R2YRAIvslxDUx2JDMlQDtI +fCQ1zE0YUNMoP5u3/X5ub9cKE2/SPhDeJGQRCRLER9XvWooqDmGYTwk0lccpgrpB5iw tn9oQtSwVw5mt3OJLvcaHYm7mOQpZ9VMoWcAgBA0YCM5csNAYaLTq3h5rxwNX/Mi+Yw1 anrQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-17268-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17268-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 18-20020a05620a049200b00781795d5badsi377634qkr.14.2024.01.04.14.00.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:00:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17268-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; spf=pass (google.com: domain of linux-kernel+bounces-17268-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17268-ouuuleilei=gmail.com@vger.kernel.org" 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 D2E591C22791 for ; Thu, 4 Jan 2024 22:00:51 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D2A172D7AB; Thu, 4 Jan 2024 21:59:43 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B7272C863; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6B59C433CC; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLVlg-00000000vhD-1zwl; Thu, 04 Jan 2024 17:00:48 -0500 Message-ID: <20240104220048.333115095@goodmis.org> User-Agent: quilt/0.67 Date: Thu, 04 Jan 2024 16:57:14 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH 3/4] eventfs: Read ei->entries before ei->children in eventfs_iterate() References: <20240104215711.924088454@goodmis.org> 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: 1787198861310805795 X-GMAIL-MSGID: 1787198861310805795 From: "Steven Rostedt (Google)" In order to apply a shortcut to skip over the current ctx->pos immediately, by using the ei->entries array, the reading of that array should be first. Moving the array reading before the linked list reading will make the shortcut change diff nicer to read. Link: https://lore.kernel.org/all/CAHk-=wiKwDUDv3+jCsv-uacDcHDVTYsXtBR9=6sGM5mqX+DhOg@mail.gmail.com/ Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/event_inode.c | 46 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index c73fb1f7ddbc..a1934e0eea3b 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -752,8 +752,8 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) * Need to create the dentries and inodes to have a consistent * inode number. */ - list_for_each_entry_srcu(ei_child, &ei->children, list, - srcu_read_lock_held(&eventfs_srcu)) { + for (i = 0; i < ei->nr_entries; i++) { + void *cdata = ei->data; if (c > 0) { c--; @@ -762,23 +762,32 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) ctx->pos++; - if (ei_child->is_freed) - continue; + entry = &ei->entries[i]; + name = entry->name; - name = ei_child->name; + mutex_lock(&eventfs_mutex); + /* If ei->is_freed then just bail here, nothing more to do */ + if (ei->is_freed) { + mutex_unlock(&eventfs_mutex); + goto out_dec; + } + r = entry->callback(name, &mode, &cdata, &fops); + mutex_unlock(&eventfs_mutex); + if (r <= 0) + continue; - dentry = create_dir_dentry(ei, ei_child, ei_dentry); + dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); if (!dentry) goto out_dec; ino = dentry->d_inode->i_ino; dput(dentry); - if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) + if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) goto out_dec; } - for (i = 0; i < ei->nr_entries; i++) { - void *cdata = ei->data; + list_for_each_entry_srcu(ei_child, &ei->children, list, + srcu_read_lock_held(&eventfs_srcu)) { if (c > 0) { c--; @@ -787,27 +796,18 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) ctx->pos++; - entry = &ei->entries[i]; - name = entry->name; - - mutex_lock(&eventfs_mutex); - /* If ei->is_freed then just bail here, nothing more to do */ - if (ei->is_freed) { - mutex_unlock(&eventfs_mutex); - goto out_dec; - } - r = entry->callback(name, &mode, &cdata, &fops); - mutex_unlock(&eventfs_mutex); - if (r <= 0) + if (ei_child->is_freed) continue; - dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); + name = ei_child->name; + + dentry = create_dir_dentry(ei, ei_child, ei_dentry); if (!dentry) goto out_dec; ino = dentry->d_inode->i_ino; dput(dentry); - if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) + if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) goto out_dec; } ret = 1; From patchwork Thu Jan 4 21:57:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185159 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp5891902dyb; Thu, 4 Jan 2024 14:01:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGc7KlsfAJQTZiL8+XUEwb0JWf96S3Svd7GrCfS4qm4ztQCH13/YGZ7Qp9widJMf2uP34Ab X-Received: by 2002:a05:6a00:cc4:b0:6d9:e2b7:4e2 with SMTP id b4-20020a056a000cc400b006d9e2b704e2mr1789424pfv.11.1704405675272; Thu, 04 Jan 2024 14:01:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704405675; cv=none; d=google.com; s=arc-20160816; b=NPh8mpM8j8+aUfKvlk1ecEPssw1zWn/6p6zDtNYv20xEZQMxLhBDRDxScxjeYRVvUQ CyFJNauTA9KkSx7mJCd0vhW4jUIYaUIjz3/M1bsfU4hnW9vRrTbO/RHt5+1kn4dQChCF VOmg0LFK0TbqZyXq0D3WzIbyJ6POpUu63+DLS71cd448z20shttrJUtWTzSDolZsh0Fd 0E2I6e4chrIW1cHm7w1e5H1DoT2rmL1qWFXRSQnSYT1fu0td2PC52JtL91LWtIP4UCSd xmG1CpJC8bEoLj6L/OAb0aHRFPAzjR4uRvvuIgUeARMtKou2xQ6KNymdelAc8/bWJhkb xuLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=1wkIgLefPB1DLu6IXZcYjRPI4gvfvalD4n2bF1HWtcc=; fh=/+b36Kyncow8zkAf4U206kHBsn7X4TQclH3EtkME/8M=; b=t/g+AzL69N7jaYal029sqlm00D8KVIijw2qWsOwtlngaa4HjUxswmvJebGXk8kHNW9 LIWHE2ilc7409pqyCERzmDAAlCrOxPqLfYWcD1HP2vbNydiEAuQELtDB4Vlocco+LS6B NIhHaJW/YLr5dJRZC3RMaz4sX4xiMi8oP1VBK7Ff8dXxA1J3XY/qZ8w50cNY9ldAdb+6 mMZNYuRPCrr3j3s29Yf4m73I4P8lAERhryC6evVyIy6nfGOYFxPxMduN4HkmaxNZ6Nt4 caZLDHF2erIH3ds7mZ32Fgr+v2hCm24kN+IFPtCDjsPKNSWTAfDeBS/sbUky1rNa/gXl rPaA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-17266-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17266-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ff23-20020a056a002f5700b006d9a20bc8d5si192743pfb.394.2024.01.04.14.01.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:01:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17266-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; spf=pass (google.com: domain of linux-kernel+bounces-17266-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17266-ouuuleilei=gmail.com@vger.kernel.org" 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 38671288685 for ; Thu, 4 Jan 2024 22:00:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C9E432D7A7; Thu, 4 Jan 2024 21:59:43 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37B932C861; Thu, 4 Jan 2024 21:59:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1CB9C43395; Thu, 4 Jan 2024 21:59:41 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLVlg-00000000vhh-2eTY; Thu, 04 Jan 2024 17:00:48 -0500 Message-ID: <20240104220048.494956957@goodmis.org> User-Agent: quilt/0.67 Date: Thu, 04 Jan 2024 16:57:15 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH 4/4] eventfs: Shortcut eventfs_iterate() by skipping entries already read References: <20240104215711.924088454@goodmis.org> 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: 1787198885448210640 X-GMAIL-MSGID: 1787198885448210640 From: "Steven Rostedt (Google)" As the ei->entries array is fixed for the duration of the eventfs_inode, it can be used to skip over already read entries in eventfs_iterate(). That is, if ctx->pos is greater than zero, there's no reason in doing the loop across the ei->entries array for the entries less than ctx->pos. Instead, start the lookup of the entries at the current ctx->pos. Link: https://lore.kernel.org/all/CAHk-=wiKwDUDv3+jCsv-uacDcHDVTYsXtBR9=6sGM5mqX+DhOg@mail.gmail.com/ Suggested-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- fs/tracefs/event_inode.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index a1934e0eea3b..fdff53d5a1f8 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -746,21 +746,15 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) if (!ei || !ei_dentry) goto out; - ret = 0; - /* * Need to create the dentries and inodes to have a consistent * inode number. */ - for (i = 0; i < ei->nr_entries; i++) { - void *cdata = ei->data; - - if (c > 0) { - c--; - continue; - } + ret = 0; - ctx->pos++; + /* Start at 'c' to jump over already read entries */ + for (i = c; i < ei->nr_entries; i++, ctx->pos++) { + void *cdata = ei->data; entry = &ei->entries[i]; name = entry->name; @@ -769,7 +763,7 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) /* If ei->is_freed then just bail here, nothing more to do */ if (ei->is_freed) { mutex_unlock(&eventfs_mutex); - goto out_dec; + goto out; } r = entry->callback(name, &mode, &cdata, &fops); mutex_unlock(&eventfs_mutex); @@ -778,14 +772,17 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) dentry = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); if (!dentry) - goto out_dec; + goto out; ino = dentry->d_inode->i_ino; dput(dentry); if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) - goto out_dec; + goto out; } + /* Subtract the skipped entries above */ + c -= min((unsigned int)c, (unsigned int)ei->nr_entries); + list_for_each_entry_srcu(ei_child, &ei->children, list, srcu_read_lock_held(&eventfs_srcu)) {