From patchwork Fri Jan 5 18:00:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185477 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp6377251dyb; Fri, 5 Jan 2024 10:01:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IHM7oLQCbuPbgqKv/2qH8gK6MobGg1Ykg9Gn5dXhMdQdSW1Gq/7Ug12NHZyczH0h+oNd8o+ X-Received: by 2002:a05:6402:2038:b0:553:a3ec:3be1 with SMTP id ay24-20020a056402203800b00553a3ec3be1mr765281edb.163.1704477716427; Fri, 05 Jan 2024 10:01:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704477716; cv=none; d=google.com; s=arc-20160816; b=XUNscZAJnB2KigCrmFppMKeYvpFEslqNq7kj8LBVXHyBmAjtmOOSFAqlPDsEDAtWTN O2IcopaU6h7F2/GYyvT2EaVCv31gaEV+ftGmI2DVyGjot9ZMiT5GYEBxDzrhNf0uhd/B 5eVGwWlh1VE1g+I1/LzqwEsETbyARMbtN+CX/iyBAwZyu8G5Ey3+o37/guYjchXz3/rm GkShLO1T4fQ4CIYlx8XAH/U2KvJOBk7igUFLc1Y4m+slYzLrKw0uFjHepJQCp0aRKT2n VYJoA8Z9Qdxhj7Pz6kz7M5krt1rL1eT3vEBCUYuOfQiFC+LgaN/W2CQjqVAELchQOHEx JkOA== 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=NTI+YfePI3I5G/EEG+DFn0HC3ZD6RLqAYp3NGlSyhTs=; fh=vkFQlI0+F6MeP6c8OufhDbPZhDE+E+f9sPZRB/bqb3w=; b=MV4lIz0mTzcM8PIMxegSQ4zJ19t5phG7c5RjvojdDWpcogH8gqJ/DMm2hdlSqAjvx/ c0iRnPZ52dg14CsHhgbiR6lSFx6u9+JneMPoM5Z6av/L7Z1wf3nUiZYP6xFndy5royG3 tGmwmTONSE4cZqMNB9OCH2hpqEhjSBBpm2fy4/HBXkGBFb4invUr/2dNJWpVKH1JlYHI FQY56PBObOXJ4EEUOcWiXjjM9TWKFE/LgXBjENYLjnyVjXixUaWCHURamjZL2loTdca8 OeGEc5GCKSJIzEEq+hTPILVQuOKiiEkm8J928eNLjKh67q6KrKkyaKeOlYSzQMcEdmh3 N65Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18160-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18160-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dd13-20020a056402312d00b00553ae9870f4si849959edb.191.2024.01.05.10.01.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:01:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18160-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18160-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18160-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 am.mirrors.kernel.org (Postfix) with ESMTPS id AB50D1F2456C for ; Fri, 5 Jan 2024 18:01:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 35623358B1; Fri, 5 Jan 2024 18:00:12 +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 AD68C328D1 for ; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50BA1C433C7; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLoVT-000000012Uu-0E2Y; Fri, 05 Jan 2024 13:01:19 -0500 Message-ID: <20240105180118.917102213@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 05 Jan 2024 13:00:52 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , Greg Kroah-Hartman Subject: [for-next][PATCH 1/4] eventfs: Have eventfs_iterate() stop immediately if ei->is_freed is set References: <20240105180051.741934288@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: 1787274425839309497 X-GMAIL-MSGID: 1787274425839309497 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. Link: https://lore.kernel.org/linux-trace-kernel/20240104220048.016261289@goodmis.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Linus Torvalds Cc: Al Viro Cc: Christian Brauner Cc: Greg Kroah-Hartman 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 Fri Jan 5 18:00:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185478 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp6377374dyb; Fri, 5 Jan 2024 10:02:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IFV50TCKqIC2QT+HtbSUZ6DV/03esULQK2TRzkd8Jg4XfWZFfi0CUH81d+p239F8qHzz3xr X-Received: by 2002:a17:906:22d5:b0:a28:c191:598d with SMTP id q21-20020a17090622d500b00a28c191598dmr1186533eja.37.1704477725094; Fri, 05 Jan 2024 10:02:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704477725; cv=none; d=google.com; s=arc-20160816; b=c3irZSJLSA0Vhgy7YOHKlnBVwnCFG5e8BZ7AdGQT/QaRRfpRZTEg0wJKstjksjUYJP +Vx6SGT3nmMAklgBGjdGrMt/IML4ALL43rMz7V9XLNBMIzNMdZyZES73gVK74s2YDsp3 00HB5gtWL2hXiDb+LnSXkYS8FCaqsCY95oHCOpKvRk8JThfP8ONPz8rkGkHRpaV85bnS NR7DYyLBOnv01O25FjGuFOSOUbWqN1RbKEneBp4erp6ouj7Wg2a6PteLTvFsT1NJua6Y OMRxXH2bB3hoA3YJeM6bgcxm+AXIwWYHQlEzDUr61BFCZBwuWQ/vonnqydjmIWB9+smW KfQg== 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=fg+hEXau8DfTKdhyRQQBLzmnbO1X05EA4yTKwoimzPI=; fh=vkFQlI0+F6MeP6c8OufhDbPZhDE+E+f9sPZRB/bqb3w=; b=cpG6MXGn2jzrI6UuZXB8BfCkWbrMqYXld7UPFdgM9ioRUZ5SwVQp7ncaVfj6wRG8gI o05TmSt0FeY/O0O/YBBickYYYle0yn8ApGI9McwbObUm4v8jmQqON6nfBafucehVMzy0 35Tvk9QVmBcM8R04/ekQcQe1JJNbPIJNGAgPfExt/OFgP3zROGZSJkaTEiVplgWvR+11 xwdMksihuPo/JC6xPnFO3AfaFzbFGELJa19OzcteHs3q0l2PvcvpHNp+DNLjV+SBMwF1 +q6bH08IyL9tWOFv75afqvG2zD9tc/+mrAKq46djGehlBgTkySr6Y78wisn4+kKQm1pJ AqIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18161-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18161-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id md10-20020a170906ae8a00b00a28f283b473si770970ejb.657.2024.01.05.10.02.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:02:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18161-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18161-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18161-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 am.mirrors.kernel.org (Postfix) with ESMTPS id CFB7C1F2483F for ; Fri, 5 Jan 2024 18:01:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AB1735EF1; Fri, 5 Jan 2024 18:00:12 +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 06EE434571 for ; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B779C433CC; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLoVT-000000012VP-0tUB; Fri, 05 Jan 2024 13:01:19 -0500 Message-ID: <20240105180119.073136397@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 05 Jan 2024 13:00:53 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , Greg Kroah-Hartman Subject: [for-next][PATCH 2/4] eventfs: Do ctx->pos update for all iterations in eventfs_iterate() References: <20240105180051.741934288@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: 1787274435095477664 X-GMAIL-MSGID: 1787274435095477664 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/ Link: https://lore.kernel.org/linux-trace-kernel/20240104220048.172295263@goodmis.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Linus Torvalds Cc: Al Viro Cc: Christian Brauner Cc: Greg Kroah-Hartman 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 Fri Jan 5 18:00:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185479 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp6377429dyb; Fri, 5 Jan 2024 10:02:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBfgW/Chf7U4FMgdjWOjxnxBxjCsHUUDJU4hIh4SujdVwmdjthTheKyQtbiOhbtBg3+Umt X-Received: by 2002:ac2:5dd0:0:b0:50e:76ab:8234 with SMTP id x16-20020ac25dd0000000b0050e76ab8234mr1269007lfq.114.1704477728341; Fri, 05 Jan 2024 10:02:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704477728; cv=none; d=google.com; s=arc-20160816; b=BpTnewH04+MoFYGcOpA6xzcDvOvOP1EYwjimPKx7fvu7DT0azsqNUhr/Qai2Bb1P/l AV+QCCWMnJ8QJLlDDIo2PNaGi9O5kDzbSJFLwyfsLnRY4CvnfCI/Fd2+Dfujt/uXdaht /gTQj0R/CfoE0H4gv7h7pKfN1ANR6zuWj7LFQ0S44XIZp6vw+ybpKwmp9L0OPIi0aN0t mgLaqaCFTC5LTSpwVDX0XUjXfypxsNGj25xOCjBx/rTuELRxw1ehnR2xAibFBkAyMlEw EjKwCn1AquX4YhScs/JE8DdJ4qnPDisPF2Q4Ej+fBr/sK6+R4Ke3gVL4Wt7iri5uCJv5 FTIg== 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=q6IIQubvHP2/0fYPFjXXZ459bT9+HrQfgqO6YdkJkUI=; fh=vkFQlI0+F6MeP6c8OufhDbPZhDE+E+f9sPZRB/bqb3w=; b=wexi+U1Up5f1TYl+cikfpZdiNQSmobUq27PIlMXfq/gjQ0ZilTOs3dkqG6/oBz/diC ZkSFrAMotkFBauFHNnQ1qczktjpw47M7eQOm1vTEfz0D2gtH718H93drYdteV3CnLSMP MGvVHJCgHAHbW/nzwlV1yX1aar/2KMri2UdcFU5DzOPVK8V7EsMba1dyvEakFGmpkLip kXiMaqbYkfw5+JvQ+PYex8DvnmNFc+y/UyX5O5IGwu89yTGnbbbJgjJD70bgMivMJR29 lhKzPJPwe3v5bnt4wGE/WPpDHbrgaQwTe2T1zghGhXvLmMH3S8jDF9l4ssofupOqdL1V FQiQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18162-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18162-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id md10-20020a170906ae8a00b00a28f283b473si770970ejb.657.2024.01.05.10.02.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:02:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18162-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18162-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18162-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 295141F24B71 for ; Fri, 5 Jan 2024 18:01:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DBDE335F02; Fri, 5 Jan 2024 18:00:12 +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 5FE8834CEA for ; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA708C433CD; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLoVT-000000012Vt-1Y3M; Fri, 05 Jan 2024 13:01:19 -0500 Message-ID: <20240105180119.231810370@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 05 Jan 2024 13:00:54 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Linus Torvalds , Al Viro , Christian Brauner , Greg Kroah-Hartman Subject: [for-next][PATCH 3/4] eventfs: Read ei->entries before ei->children in eventfs_iterate() References: <20240105180051.741934288@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: 1787274438489937190 X-GMAIL-MSGID: 1787274438489937190 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/ Link: https://lore.kernel.org/linux-trace-kernel/20240104220048.333115095@goodmis.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Linus Torvalds Cc: Al Viro Cc: Christian Brauner Cc: Greg Kroah-Hartman 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 Fri Jan 5 18:00:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 185480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp6377590dyb; Fri, 5 Jan 2024 10:02:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IF5EnMxnDhJtQQH5S5U45Vnp3LlSR1fqcytLgok+8oZqip4kPeEyGuyRTwA27JlZWgSb2q+ X-Received: by 2002:a05:6a20:3c93:b0:198:fa9d:d58c with SMTP id b19-20020a056a203c9300b00198fa9dd58cmr2504648pzj.72.1704477738817; Fri, 05 Jan 2024 10:02:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704477738; cv=none; d=google.com; s=arc-20160816; b=k0SMATBkNSLmAs0QpHipleiMP7lNvByVrwBmKUv/f+fqCygWFih/UvkNoLv1EPSbW+ d5Pst9Xng7jj7JuzIUoa80O4z9j+ek8Wp5pcMFScYx368kqzEjI9264NRuT3gkXw0P9V ORGzM/zPFeaZzdIj+j4Nrew0OsxiPBcG/AwLDusu8Uw7Wwaf+7t6brnBSMptetNqJmFK IkxLbklWSAdzX3qTlKwtuy+QMLRypuUTcWCwueQHvRAW43bLKFAXg+WTZ4orIs0UongI 4MXapZD2naaJKPhyP+nny0lJ+NideNMJdhTeIiGl4SDOfQviESM7UQkbRNsovhc13sm8 2bhg== 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=TYEnDQSfA0BWYMt46jjP4QB5A7Q64UNnp6Ht5hrsb3A=; fh=wfNyI3/0KUXQJuliuroo31KnPhj2VPJ8bBNWGHjTLnI=; b=MceFYFMIbIZjQPQ0Nsj0M5V756cCR6BrlhzSUZqsNCmB98la5oBltdoqs6o4HQkoxO CMWk960wTtsxRPuNcAK2kgmuqIPGbH9Tc0Kb44uMFIljdLVCEQ0JQwNGn3DxpfDhCPZv GWto648CiDbuNsrxLjnZbd0oEuuGWFMNQ7a5tGAmimUNvq6AFIebHtR9JhaTZET+a8a8 AbrkcnrXGxtWk7nHgZEub9P9G212+pHv5Iy9IbdnYVM2XhiOuDQiKteRjq7uovPLWoQR 2dAQkpVZi+lCcvs3+OOqdjeu15vM2Zr3wT2jvgI+BTory2vxS2Nay6pBj4u4JnLgjUQN Jxrg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-18163-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18163-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ei49-20020a056a0080f100b006d9b21cd3e1si1596265pfb.280.2024.01.05.10.02.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:02:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18163-ouuuleilei=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; spf=pass (google.com: domain of linux-kernel+bounces-18163-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18163-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id CDA5CB22571 for ; Fri, 5 Jan 2024 18:01:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DBF0D35F03; Fri, 5 Jan 2024 18:00:12 +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 5FEC434CEC for ; Fri, 5 Jan 2024 18:00:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA13BC433C7; Fri, 5 Jan 2024 18:00:10 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rLoVT-000000012WN-2E3n; Fri, 05 Jan 2024 13:01:19 -0500 Message-ID: <20240105180119.388998534@goodmis.org> User-Agent: quilt/0.67 Date: Fri, 05 Jan 2024 13:00:55 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Al Viro , Christian Brauner , Greg Kroah-Hartman , Linus Torvalds Subject: [for-next][PATCH 4/4] eventfs: Shortcut eventfs_iterate() by skipping entries already read References: <20240105180051.741934288@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: 1787274449555155739 X-GMAIL-MSGID: 1787274449555155739 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/ Link: https://lore.kernel.org/linux-trace-kernel/20240104220048.494956957@goodmis.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Al Viro Cc: Christian Brauner Cc: Greg Kroah-Hartman 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)) {