From patchwork Wed Jan 31 18:49:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194911 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2107315dyb; Wed, 31 Jan 2024 11:15:54 -0800 (PST) X-Google-Smtp-Source: AGHT+IG4J6Wk4moQnPCvk2lcyPEZIrhQ8hQ4AQffxf+idGUchcNvV1Jhnwa6RZha9pT7/0nu9ltg X-Received: by 2002:a17:902:e541:b0:1d8:f200:6ae2 with SMTP id n1-20020a170902e54100b001d8f2006ae2mr3363071plf.30.1706728554605; Wed, 31 Jan 2024 11:15:54 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706728554; cv=pass; d=google.com; s=arc-20160816; b=qq/rV2ua1K2CYT2fz2BjaNBAgXvFfdr9us9O3i4ZtUyfu0cXbb9X06djWL6u4TU55E 1Wmnoff7sIcZtzZGZCGE43LaIJ8sizZpsVSRS8wQCt/RcD7KYrXPoj0O5GrmYWGj9T67 TwQV0QciYtnoeJouwSvHSE4+splAbeoBJv8QaallMjdmcUleHcUK5NG5Y83lJJTE6uZ7 9Y9GyGP/FqVT67QMYQDlHXMKPxcnmoYKIRLA1uUPG8J4cD5rlbMRlO/CISc4X3ox4uGi FltiPox/MbTbGSaG85eM0jvffFLoygTMH++jcjMIRQMcPFF8vwMP/scLpUHk4+Bmp7rq BT3w== ARC-Message-Signature: i=2; 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=BLEdrGS2Da0/DL+Bn5JBrytdx7X4xFks9AkFRO0h+xQ=; fh=wTjzPxNcwW1GffzqGN/XcZB7GpDbkM3wWPgqd9vPLQA=; b=PXf5WR63VaHH0aJg22/u6a62nd86jz7zqJyxRFwheRXqPCWsrFHshPVpmDnUdm32ht nW0yFYmIbgqPUjcwFkny6YeMGlWmCjJMaWy5UqcwUGTYRu0S0Tv5iOpdmeAsDv4PX2YX N9oAhUoP1MTKOS0tzSPTo00MSEYLRskhwk4Je3k4qwkKaVJgR/mzph82IMyRBZhsNlt2 WhTrVfoYo53lkbUkR/SfYROc7qD+7hfMjRxzDsMChVK4YwZVugPlciv7qa2VEhjQMh8L mqi+d9f7LKMFGgspnf8zidIxqIuSFUDIFchKzWBag9cFLkE3Iu05XYL6ZAKWjOFLbiiP odfg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46988-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46988-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCUp/KeiFURK5JrPrtCDSL3qLkO6I41+7sXpt15XkrSHqBJf7ag6kbjLAhyuZw429M97ZNP/dvxDTPOKyf3Fy6drEB8f4w== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q15-20020a170902dacf00b001d88bce4b30si541856plx.317.2024.01.31.11.15.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 11:15:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46988-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46988-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46988-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 0B88CB274DB for ; Wed, 31 Jan 2024 18:56:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C1C5312FF7C; Wed, 31 Jan 2024 18:54:59 +0000 (UTC) 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 C79D938FA1; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727297; cv=none; b=VmkwwH+MDQoSEsw7ZnFrsyp0lsfw7PKc3U0IQkmTBPDAteA8ky815gfdDfO0xie6MUbUH0PUIug14czJvIeGyK9mLaXbErtkx1rCFdtZFPluNPC3//upCW2OIruXifMpZImTe3ORMOgaIOLFPWkQlK2vm9dvGPMj1xFyoA1ntc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727297; c=relaxed/simple; bh=pfpMUH3tWGcM1lGwQK3NyStqZwiqHr8qawaLbZJl8tw=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=T6AxM+xZwbHEwWwMTbX//V46Ogcnw9Rcs58K0Ere9mGIo046PaIMN36JPk+Cxnzf9ZCCNYXZuOf9KzU4PgzSqFN/P1NVQc2xXuE2c0k4l6LK3iQW0wZ21GvAwr46TCfL+i0wnto4wal8o5/l7s8QLehvkfQkAsByVY30nbhmTMY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C98CC433F1; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjs-000000055Ps-1v8J; Wed, 31 Jan 2024 13:55:12 -0500 Message-ID: <20240131185512.315825944@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:19 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot Subject: [PATCH v2 1/7] tracefs: Zero out the tracefs_inode when allocating it References: <20240131184918.945345370@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: 1789634601251459504 X-GMAIL-MSGID: 1789634601251459504 From: "Steven Rostedt (Google)" eventfs uses the tracefs_inode and assumes that it's already initialized to zero. That is, it doesn't set fields to zero (like ti->private) after getting its tracefs_inode. This causes bugs due to stale values. Just initialize the entire structure to zero on allocation so there isn't any more surprises. This is a partial fix to access to ti->private. The assignment still needs to be made before the dentry is instantiated. Cc: stable@vger.kernel.org Fixes: 5790b1fb3d672 ("eventfs: Remove eventfs_file and just use eventfs_inode") Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202401291043.e62e89dc-oliver.sang@intel.com Suggested-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Changes since last version: https://lore.kernel.org/all/20240130230612.377a1933@gandalf.local.home/ - Moved vfs_inode to top of tracefs_inode structure so that the rest can be initialized with memset_after() as the vfs_inode portion is already cleared with a memset() itself in inode_init_once(). fs/tracefs/inode.c | 6 ++++-- fs/tracefs/internal.h | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index e1b172c0e091..888e42087847 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -38,8 +38,6 @@ static struct inode *tracefs_alloc_inode(struct super_block *sb) if (!ti) return NULL; - ti->flags = 0; - return &ti->vfs_inode; } @@ -779,7 +777,11 @@ static void init_once(void *foo) { struct tracefs_inode *ti = (struct tracefs_inode *) foo; + /* inode_init_once() calls memset() on the vfs_inode portion */ inode_init_once(&ti->vfs_inode); + + /* Zero out the rest */ + memset_after(ti, 0, vfs_inode); } static int __init tracefs_init(void) diff --git a/fs/tracefs/internal.h b/fs/tracefs/internal.h index 91c2bf0b91d9..7d84349ade87 100644 --- a/fs/tracefs/internal.h +++ b/fs/tracefs/internal.h @@ -11,9 +11,10 @@ enum { }; struct tracefs_inode { + struct inode vfs_inode; + /* The below gets initialized with memset_after(ti, 0, vfs_inode) */ unsigned long flags; void *private; - struct inode vfs_inode; }; /* From patchwork Wed Jan 31 18:49:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194901 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2094800dyb; Wed, 31 Jan 2024 10:56:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IGKTIuI8pNg9dZTGz1QGA0WY4A4oHJn2TIa1Zz/4b0l4nv7iU/isTrGIlH+OgiYU/ekZoHt X-Received: by 2002:aa7:91ca:0:b0:6de:1b57:ca8c with SMTP id z10-20020aa791ca000000b006de1b57ca8cmr2434133pfa.23.1706727415737; Wed, 31 Jan 2024 10:56:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706727415; cv=pass; d=google.com; s=arc-20160816; b=QQTruNgDyKH5O1bwFkAe5j3w9zMwqd0NxC7h5Pm8san0/efvLpJKZaNTKmZCw5BOv2 EdNP3TqMBWqraH8ysMkDOPEHSyGMSXCmjZZAvz2l/OClsSi1/ZtJhb5lj708nSqUznOR 1yzHiQcNJnROmIE7neX0F/DEH4svpDkz3DYkp6Hfjc3iYHa9h40yIDutVmYMEXOliRkt bQSSQGBdTL8Uk8sgpY9ip8neVZFUuj5+9MWksBlTdKmuJWe192UxoNWf0d7UmgkI1vVD /RrAgXRmPd0dTMkzl1euCM948z7HzevjhGfWFoAvE2augdbUv/tZTb6XTiPDoWbU3TWb tMCQ== ARC-Message-Signature: i=2; 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=s4r0o6G8SaT+rQppo9CI/mZyE+VqCeHQrA5gsQDbtho=; fh=5zt4BK7uRnrjTfz1VEGqL9FSJx1MNzRoJg2nhzVqnxw=; b=Jamk2kPLNb4mPWxyKhJ8h6mPy64R0W848/XAtjMHOjWEj6BaRtkRZ37YsKUu1zb1It nv9cyQ7OgyZENg1f2M0P6zKN4Ve5vTibDpNkOaGjzKh1ioFbdt9RX0xX/c1tOApRS6wa nZJVwTiA9tplQVQXvfNg1xX7DGUdlqreO3godGelEaB0RmyLHfPL4KmfaZHAUHP2KiJe cO29tG8DRm/+7O28CmV2t2Xvxfa5Z4Bk/PHaHp/Usecz/uLWkLwWXV4upuTCYfJkDeoz D31h6Yiq95mBfW8xn8p90k15U0brF3s+stzd2EiLESKBypKrhhgTKoAtV25lG0ibfN80 B5fA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46990-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46990-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCUax/URML9HPR+0v6DThp23FfHDnayLjW6NmMCpYp7Kmp+u55e6B0TH+/OXNQN6h+3fHEjwZn9XnXDPFYkIy/5whQPA5Q== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a14-20020a056a000c8e00b006de212f1c59si7185361pfv.101.2024.01.31.10.56.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 10:56:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46990-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46990-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46990-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 7D5762851DF for ; Wed, 31 Jan 2024 18:56:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1AA9E13541D; Wed, 31 Jan 2024 18:55:00 +0000 (UTC) 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 126083A8FE; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=QNyL46sDnBnrpgal219thiF4Hw4JOvSzIMwBjGrnEAmA2VHnjHQe05JrqA6druQj6Eh/YRbOmkZhUvhZcw8r34DGokCsQ/rAdveXpbAkzSasqOJdfb0CdM0CB/RCfyFMXVElS7rJH6PjLOFEYQScxJUn2C40TdoKb3ywpanh9Mo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=7Rdww8PD/OZDuJxNvZnG5j/I+jvbRavpqIHcKsYavJQ=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=O8KbXwxcDuDNVuzyThFh293AFGLq0ovmBcfZ1iFkezRYTECvHntNpB//rASDyXktw+I//quNIEFhjG1fzLTeXvupAIZOsoulwnPPJ/yjmw5mIFtP+wa9gG5XZLJEe/yi5WjjHO9teI0/tzszT0iCOIp9DilZtnFeTtzQJtEFRTc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3D7AC433A6; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjs-000000055QM-2apb; Wed, 31 Jan 2024 13:55:12 -0500 Message-ID: <20240131185512.478449628@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:20 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot Subject: [PATCH v2 2/7] eventfs: Initialize the tracefs inode properly References: <20240131184918.945345370@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: 1789633406682114754 X-GMAIL-MSGID: 1789633406682114754 From: Linus Torvalds The tracefs-specific fields in the inode were not initialized before the inode was exposed to others through the dentry with 'd_instantiate()'. Move the field initializations up to before the d_instantiate. Cc: stable@vger.kernel.org Fixes: 5790b1fb3d672 ("eventfs: Remove eventfs_file and just use eventfs_inode") Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202401291043.e62e89dc-oliver.sang@intel.com Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-2-torvalds@linux-foundation.org - Since another patch zeroed out the entire tracefs_inode, there's no need to initialize any of its fields to NULL. fs/tracefs/event_inode.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 1c3dd0ad4660..824b1811e342 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -370,6 +370,8 @@ static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent ti = get_tracefs(inode); ti->flags |= TRACEFS_EVENT_INODE; + /* Only directories have ti->private set to an ei, not files */ + ti->private = ei; inc_nlink(inode); d_instantiate(dentry, inode); @@ -515,7 +517,6 @@ create_file_dentry(struct eventfs_inode *ei, int idx, static void eventfs_post_create_dir(struct eventfs_inode *ei) { struct eventfs_inode *ei_child; - struct tracefs_inode *ti; lockdep_assert_held(&eventfs_mutex); @@ -525,9 +526,6 @@ static void eventfs_post_create_dir(struct eventfs_inode *ei) srcu_read_lock_held(&eventfs_srcu)) { ei_child->d_parent = ei->dentry; } - - ti = get_tracefs(ei->dentry->d_inode); - ti->private = ei; } /** From patchwork Wed Jan 31 18:49:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194912 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2107436dyb; Wed, 31 Jan 2024 11:16:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHiprRu3J0wSFN8UOpssSQe+4ryDQl4cP+H0uv127eNx43UvMy6MNfziPJADVFzAiVYoQDg X-Received: by 2002:a05:6870:5309:b0:214:2dd0:6c78 with SMTP id j9-20020a056870530900b002142dd06c78mr3065349oan.25.1706728566953; Wed, 31 Jan 2024 11:16:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706728566; cv=pass; d=google.com; s=arc-20160816; b=u3ipBGyDYuZRYdwRa/L/UK2hUlrXTgYSQbY5OJmGOfhdcj6W+92+WdMLNqRACd3pCN 4IY5iwyACOc2ifiXyr72zZplesFtI72FYm1GsKTb/sW/psJR/BryO2dUdRmhKRGWNVog 4hMspDjFS3yf3XLxqANCd3nQiDMsDo8og7CmNGmETokryFOWF0R21i3NE4cBHTPim/p2 hA0yFV5yqy12/PL8h50JSSWbLpQg7arbWGy5vXAWzHVIRmFhvpKeI3TVf/n+wOXndeFx sMhWalcOdiC+SVQD2LyR8UwrDVEZkKZitxDtb+/zobe3S0ZBN6Gc+2vf23YPFkVoS7D6 bFNQ== ARC-Message-Signature: i=2; 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=lQxtUAB98YvH689MmRbXqsDi5zOdnFTKnh+wJRPHUk8=; fh=5x+SgJ1py+lN3ONkpLcGOZCqg3EhQuvgDRP1YwNBNT8=; b=GFwB76u0R2Ebg+PM2quGrdtnTz+RYkBghcoy9IUhA4CZLODBD9rAd4Agx2bKHv0Ajd o2eYCqSz+Zu5hmkpN1/CUn5w3lizPsXxUy3ATrmzi5OuMUPTvGi6HWEIybQx58JSFjbF a3b2AG4riUTNG7e1AbtcbGvt9th0NTmEYYCj2eWrGTlnpneT/XdI10JcyyfET7LrdGg3 huuNsuGVOlkC/mZEYoJKfG010Elo62lgLN/p98o64ZMPYBryl9X7sfwxRyLXoecoJV29 fj1zvYLncCgBKahygR6Z3wrOQtL36SLW+JwGli65wVJ6dy1pkC/oIkQT4ry1PlbrI/JK xFvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46989-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46989-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCUmM3PRCpsnkQv26kq80z0tUc2Z9ZwSZ+qRmCV8xI8jfvbRgBPb+2dbz6mF8D7PRyOBBW66+yM0PWPPJqMnwQAQCSmwFA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id s67-20020a635e46000000b005b942de1e92si10008398pgb.443.2024.01.31.11.16.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 11:16:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46989-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46989-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46989-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 40DADB28145 for ; Wed, 31 Jan 2024 18:56:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1AE49135A5C; Wed, 31 Jan 2024 18:55:00 +0000 (UTC) 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 125DB3A1C8; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=Yn8JZn0Iwabe8Ua9fcT4G+5b163vjldSYfz9J6h7+6l+S97SoTRco+xh0hLR4jmiO7+4SUu9ZvZI/FF+0DBx0eypI/waU6z2l1BApJzCJcAlUSu8jelHj2GJTvOIVdb+SuIl21sdEuxR9TwTncE90ehDUCro8FYMn28hunHd8B8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=GDc+aLW5Fd1txUftp1mWm1xo+fkkQ873V74up1OwR9E=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=TqUKGMLpJifjlVUUvG6T6Ycl2hxA5mCAb8CTKYgTEKluOaEPpwp8likqt3fCbPQ/ilqjl3FljVBDk5SkOn1+F4QqdefV1n/DlZ9DlxAmRYxdpOtJday0gWbKD6z28xAEmZ7Po3JDkvExewGyMeAiXht3B2YWfJ4hwOqcwoIBTnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC093C433B2; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjs-000000055Qq-3Goq; Wed, 31 Jan 2024 13:55:12 -0500 Message-ID: <20240131185512.638645365@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:21 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH v2 3/7] tracefs: Avoid using the ei->dentry pointer unnecessarily References: <20240131184918.945345370@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: 1789634614046123260 X-GMAIL-MSGID: 1789634614046123260 From: Linus Torvalds The eventfs_find_events() code tries to walk up the tree to find the event directory that a dentry belongs to, in order to then find the eventfs inode that is associated with that event directory. However, it uses an odd combination of walking the dentry parent, looking up the eventfs inode associated with that, and then looking up the dentry from there. Repeat. But the code shouldn't have back-pointers to dentries in the first place, and it should just walk the dentry parenthood chain directly. Similarly, 'set_top_events_ownership()' looks up the dentry from the eventfs inode, but the only reason it wants a dentry is to look up the superblock in order to look up the root dentry. But it already has the real filesystem inode, which has that same superblock pointer. So just pass in the superblock pointer using the information that's already there, instead of looking up extraneous data that is irrelevant. Link: https://lore.kernel.org/linux-trace-kernel/202401291043.e62e89dc-oliver.sang@intel.com/ Cc: stable@vger.kernel.org Fixes: c1504e510238 ("eventfs: Implement eventfs dir creation functions") Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Original patch: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-1-torvalds@linux-foundation.org fs/tracefs/event_inode.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 824b1811e342..e9819d719d2a 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -156,33 +156,30 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry, return ret; } -static void update_top_events_attr(struct eventfs_inode *ei, struct dentry *dentry) +static void update_top_events_attr(struct eventfs_inode *ei, struct super_block *sb) { - struct inode *inode; + struct inode *root; /* Only update if the "events" was on the top level */ if (!ei || !(ei->attr.mode & EVENTFS_TOPLEVEL)) return; /* Get the tracefs root inode. */ - inode = d_inode(dentry->d_sb->s_root); - ei->attr.uid = inode->i_uid; - ei->attr.gid = inode->i_gid; + root = d_inode(sb->s_root); + ei->attr.uid = root->i_uid; + ei->attr.gid = root->i_gid; } static void set_top_events_ownership(struct inode *inode) { struct tracefs_inode *ti = get_tracefs(inode); struct eventfs_inode *ei = ti->private; - struct dentry *dentry; /* The top events directory doesn't get automatically updated */ if (!ei || !ei->is_events || !(ei->attr.mode & EVENTFS_TOPLEVEL)) return; - dentry = ei->dentry; - - update_top_events_attr(ei, dentry); + update_top_events_attr(ei, inode->i_sb); if (!(ei->attr.mode & EVENTFS_SAVE_UID)) inode->i_uid = ei->attr.uid; @@ -235,8 +232,10 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry) mutex_lock(&eventfs_mutex); do { - /* The parent always has an ei, except for events itself */ - ei = dentry->d_parent->d_fsdata; + // The parent is stable because we do not do renames + dentry = dentry->d_parent; + // ... and directories always have d_fsdata + ei = dentry->d_fsdata; /* * If the ei is being freed, the ownership of the children @@ -246,12 +245,11 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry) ei = NULL; break; } - - dentry = ei->dentry; + // Walk upwards until you find the events inode } while (!ei->is_events); mutex_unlock(&eventfs_mutex); - update_top_events_attr(ei, dentry); + update_top_events_attr(ei, dentry->d_sb); return ei; } From patchwork Wed Jan 31 18:49:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194904 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2095315dyb; Wed, 31 Jan 2024 10:57:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBsQoMZZScpPY+dmMRCJ6qgkTPDT7opDxV1+UqBb9XuyEaTBAltG84Y9veYz0gTBfi+4S0 X-Received: by 2002:a19:8c0a:0:b0:511:1b80:9583 with SMTP id o10-20020a198c0a000000b005111b809583mr261053lfd.10.1706727479350; Wed, 31 Jan 2024 10:57:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706727479; cv=pass; d=google.com; s=arc-20160816; b=tHdPx+41Xju+QQznpZSIP9nF1Z+k72EcGA136xTSYDpPW87p50GMsY5cp+1BzMR5VT OO2x88lNt/fczBOXURv3uMO5QwPVnM+p/2Qeu9LTjggYJdyuhTsL7Qboah0e+ViJreiO gUNoSISjTyQohMFf4aULbScFVmJ3Ka8Mao0v4BWxBz34n9wFNYqF8xzNqBg0B+Y+WS+j wf0tRsCxfpPCAMCpxsvYnjaHeJS8yeSLGPfNIv9yI9MR/aJMuGlWaSl+uHk53T8wecZR 4dXvJELQsgvz/hDgjd90FeP9QI2+Y2vpWzcacROKxxN7WJ4vLRjvit3qOiL+Ik2NHBd6 qWqA== ARC-Message-Signature: i=2; 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=fL2YouFLgH7I82bZVmMFYG+nD7CdV1zqPUlkJ6qjCEw=; fh=AYuAOo1z5aTZydBRft9Rwj2GbLnswXYT5J94xLlA5Jw=; b=izeviepdtad5/06tdTPrYtQhwFeieIbtcm0Jahbyhd75cGq2FiH1O/dpyhEvo5qZz5 iau+CLLU17JMWHpfLcQAVyWnO1qMzEijxUtfKhmWm3qEOGzk5MsG5zSVWuaWKCf2aOwp 3t+FTFG99OY7z9+fY9nu03vl/+djQC5vLvArKA4e02ZUZduxgL5T/q67AhuKlH3nKAQW t4Gezb1463gglq8qAWYH64/j2lUt9lRR2xlr01LawLVuvGT4LO8ErcjH6H10XbaTEe9j bIBhU6dJslXje2tZtiQZ/JoSht3IyRuLXeBVb/fI7bI5/SdC6JkCuCozc6ELtgQR70Uu SMhQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46992-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46992-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCV0Gc7o2k42OaEZINzt+qAozfdcHpQ2PKNDX+EBLzhRh0Mu9TRlVmrFCqjN/fFFYtwCHSeWSh4YW4ZBABpOJRHFefMVnQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id t7-20020a1709060c4700b00a3550c66072si4539793ejf.959.2024.01.31.10.57.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 10:57:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46992-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46992-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46992-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 C3E6B1F27B64 for ; Wed, 31 Jan 2024 18:57:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8A85C13BEA4; Wed, 31 Jan 2024 18:55:04 +0000 (UTC) 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 8BB7C3FB17; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=BAYy97ifRs0NZXugeBopta7uQXCnhHgiBnr7vUffjvoUsHZyeXS6VAOXZ1Y2R79oPRnOQvlupq0DYq4nM1D5b+xHDIu59/XaaMoyDYVxMp0dJ4VsyLiMAJXlQl9Yhgsd84iCAJvau9H7bL28kUE1cH5Hif2Mp2MYFQFUyZHMxwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=hGyxi7rkpGN3rfWPDjLksvDJVIhdaDvsVYxgi+1VBlw=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Y1F1ION4gVzmrWHTwGQB+nyBrjMGF/N5HzrTewUtnh4OhbsL3UIlnS3GgUI8brLQ9cw/DUAYl1WU5l6MGlOCSe3IEkiMCL7ypVBMh6qzY1DTfVZfYEMqP1GJQfgVWj5D3UpANtA3ZVTtaz691mQnuvGbKRJXZCC4Pxk0xnqNng0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6B96C43390; Wed, 31 Jan 2024 18:54:57 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjs-000000055RK-3x34; Wed, 31 Jan 2024 13:55:12 -0500 Message-ID: <20240131185512.799813912@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:22 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH v2 4/7] tracefs: dentry lookup crapectomy References: <20240131184918.945345370@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: 1789633473451553875 X-GMAIL-MSGID: 1789633473451553875 From: Linus Torvalds The dentry lookup for eventfs files was very broken, and had lots of signs of the old situation where the filesystem names were all created statically in the dentry tree, rather than being looked up dynamically based on the eventfs data structures. You could see it in the naming - how it claimed to "create" dentries rather than just look up the dentries that were given it. You could see it in various nonsensical and very incorrect operations, like using "simple_lookup()" on the dentries that were passed in, which only results in those dentries becoming negative dentries. Which meant that any other lookup would possibly return ENOENT if it saw that negative dentry before the data was then later filled in. You could see it in the immense amount of nonsensical code that didn't actually just do lookups. Link: https://lore.kernel.org/linux-trace-kernel/202401291043.e62e89dc-oliver.sang@intel.com/ Cc: stable@vger.kernel.org Fixes: c1504e510238 ("eventfs: Implement eventfs dir creation functions") Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-3-torvalds@linux-foundation.org - Fixed the lookup case of not found dentry, to return an error. This was added in a later patch when it should have been in this one. - Removed the calls to eventfs_{start,end,failed}_creating() fs/tracefs/event_inode.c | 285 ++++++++------------------------------- fs/tracefs/inode.c | 69 ---------- fs/tracefs/internal.h | 3 - 3 files changed, 58 insertions(+), 299 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index e9819d719d2a..4878f4d578be 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -230,7 +230,6 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry) { struct eventfs_inode *ei; - mutex_lock(&eventfs_mutex); do { // The parent is stable because we do not do renames dentry = dentry->d_parent; @@ -247,7 +246,6 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry) } // Walk upwards until you find the events inode } while (!ei->is_events); - mutex_unlock(&eventfs_mutex); update_top_events_attr(ei, dentry->d_sb); @@ -280,11 +278,10 @@ static void update_inode_attr(struct dentry *dentry, struct inode *inode, } /** - * create_file - create a file in the tracefs filesystem - * @name: the name of the file to create. + * lookup_file - look up a file in the tracefs filesystem + * @dentry: the dentry to look up * @mode: the permission that the file should have. * @attr: saved attributes changed by user - * @parent: parent dentry for this file. * @data: something that the caller will want to get to later on. * @fop: struct file_operations that should be used for this file. * @@ -292,13 +289,13 @@ static void update_inode_attr(struct dentry *dentry, struct inode *inode, * directory. The inode.i_private pointer will point to @data in the open() * call. */ -static struct dentry *create_file(const char *name, umode_t mode, +static struct dentry *lookup_file(struct dentry *dentry, + umode_t mode, struct eventfs_attr *attr, - struct dentry *parent, void *data, + void *data, const struct file_operations *fop) { struct tracefs_inode *ti; - struct dentry *dentry; struct inode *inode; if (!(mode & S_IFMT)) @@ -307,15 +304,9 @@ static struct dentry *create_file(const char *name, umode_t mode, if (WARN_ON_ONCE(!S_ISREG(mode))) return NULL; - WARN_ON_ONCE(!parent); - dentry = eventfs_start_creating(name, parent); - - if (IS_ERR(dentry)) - return dentry; - inode = tracefs_get_inode(dentry->d_sb); if (unlikely(!inode)) - return eventfs_failed_creating(dentry); + return ERR_PTR(-ENOMEM); /* If the user updated the directory's attributes, use them */ update_inode_attr(dentry, inode, attr, mode); @@ -329,32 +320,29 @@ static struct dentry *create_file(const char *name, umode_t mode, ti = get_tracefs(inode); ti->flags |= TRACEFS_EVENT_INODE; - d_instantiate(dentry, inode); + + d_add(dentry, inode); fsnotify_create(dentry->d_parent->d_inode, dentry); - return eventfs_end_creating(dentry); + return dentry; }; /** - * create_dir - create a dir in the tracefs filesystem + * lookup_dir_entry - look up a dir in the tracefs filesystem + * @dentry: the directory to look up * @ei: the eventfs_inode that represents the directory to create - * @parent: parent dentry for this file. * - * This function will create a dentry for a directory represented by + * This function will look up a dentry for a directory represented by * a eventfs_inode. */ -static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent) +static struct dentry *lookup_dir_entry(struct dentry *dentry, + struct eventfs_inode *pei, struct eventfs_inode *ei) { struct tracefs_inode *ti; - struct dentry *dentry; struct inode *inode; - dentry = eventfs_start_creating(ei->name, parent); - if (IS_ERR(dentry)) - return dentry; - inode = tracefs_get_inode(dentry->d_sb); if (unlikely(!inode)) - return eventfs_failed_creating(dentry); + return ERR_PTR(-ENOMEM); /* If the user updated the directory's attributes, use them */ update_inode_attr(dentry, inode, &ei->attr, @@ -371,11 +359,14 @@ static struct dentry *create_dir(struct eventfs_inode *ei, struct dentry *parent /* Only directories have ti->private set to an ei, not files */ ti->private = ei; + dentry->d_fsdata = ei; + ei->dentry = dentry; // Remove me! + inc_nlink(inode); - d_instantiate(dentry, inode); + d_add(dentry, inode); inc_nlink(dentry->d_parent->d_inode); fsnotify_mkdir(dentry->d_parent->d_inode, dentry); - return eventfs_end_creating(dentry); + return dentry; } static void free_ei(struct eventfs_inode *ei) @@ -425,7 +416,7 @@ void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry) } /** - * create_file_dentry - create a dentry for a file of an eventfs_inode + * lookup_file_dentry - create a dentry for a file of an eventfs_inode * @ei: the eventfs_inode that the file will be created under * @idx: the index into the d_children[] of the @ei * @parent: The parent dentry of the created file. @@ -438,157 +429,21 @@ void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry) * address located at @e_dentry. */ static struct dentry * -create_file_dentry(struct eventfs_inode *ei, int idx, - struct dentry *parent, const char *name, umode_t mode, void *data, +lookup_file_dentry(struct dentry *dentry, + struct eventfs_inode *ei, int idx, + umode_t mode, void *data, const struct file_operations *fops) { struct eventfs_attr *attr = NULL; struct dentry **e_dentry = &ei->d_children[idx]; - struct dentry *dentry; - - WARN_ON_ONCE(!inode_is_locked(parent->d_inode)); - mutex_lock(&eventfs_mutex); - if (ei->is_freed) { - mutex_unlock(&eventfs_mutex); - return NULL; - } - /* If the e_dentry already has a dentry, use it */ - if (*e_dentry) { - dget(*e_dentry); - mutex_unlock(&eventfs_mutex); - return *e_dentry; - } - - /* ei->entry_attrs are protected by SRCU */ if (ei->entry_attrs) attr = &ei->entry_attrs[idx]; - mutex_unlock(&eventfs_mutex); - - dentry = create_file(name, mode, attr, parent, data, fops); - - mutex_lock(&eventfs_mutex); - - if (IS_ERR_OR_NULL(dentry)) { - /* - * When the mutex was released, something else could have - * created the dentry for this e_dentry. In which case - * use that one. - * - * If ei->is_freed is set, the e_dentry is currently on its - * way to being freed, don't return it. If e_dentry is NULL - * it means it was already freed. - */ - if (ei->is_freed) { - dentry = NULL; - } else { - dentry = *e_dentry; - dget(dentry); - } - mutex_unlock(&eventfs_mutex); - return dentry; - } - - if (!*e_dentry && !ei->is_freed) { - *e_dentry = dentry; - dentry->d_fsdata = ei; - } else { - /* - * Should never happen unless we get here due to being freed. - * Otherwise it means two dentries exist with the same name. - */ - WARN_ON_ONCE(!ei->is_freed); - dentry = NULL; - } - mutex_unlock(&eventfs_mutex); - - return dentry; -} - -/** - * eventfs_post_create_dir - post create dir routine - * @ei: eventfs_inode of recently created dir - * - * Map the meta-data of files within an eventfs dir to their parent dentry - */ -static void eventfs_post_create_dir(struct eventfs_inode *ei) -{ - struct eventfs_inode *ei_child; - - lockdep_assert_held(&eventfs_mutex); - - /* srcu lock already held */ - /* fill parent-child relation */ - list_for_each_entry_srcu(ei_child, &ei->children, list, - srcu_read_lock_held(&eventfs_srcu)) { - ei_child->d_parent = ei->dentry; - } -} - -/** - * create_dir_dentry - Create a directory dentry for the eventfs_inode - * @pei: The eventfs_inode parent of ei. - * @ei: The eventfs_inode to create the directory for - * @parent: The dentry of the parent of this directory - * - * This creates and attaches a directory dentry to the eventfs_inode @ei. - */ -static struct dentry * -create_dir_dentry(struct eventfs_inode *pei, struct eventfs_inode *ei, - struct dentry *parent) -{ - struct dentry *dentry = NULL; - - WARN_ON_ONCE(!inode_is_locked(parent->d_inode)); - - mutex_lock(&eventfs_mutex); - if (pei->is_freed || ei->is_freed) { - mutex_unlock(&eventfs_mutex); - return NULL; - } - if (ei->dentry) { - /* If the eventfs_inode already has a dentry, use it */ - dentry = ei->dentry; - dget(dentry); - mutex_unlock(&eventfs_mutex); - return dentry; - } - mutex_unlock(&eventfs_mutex); + dentry->d_fsdata = ei; // NOTE: ei of _parent_ + lookup_file(dentry, mode, attr, data, fops); - dentry = create_dir(ei, parent); - - mutex_lock(&eventfs_mutex); - - if (IS_ERR_OR_NULL(dentry) && !ei->is_freed) { - /* - * When the mutex was released, something else could have - * created the dentry for this e_dentry. In which case - * use that one. - * - * If ei->is_freed is set, the e_dentry is currently on its - * way to being freed. - */ - dentry = ei->dentry; - if (dentry) - dget(dentry); - mutex_unlock(&eventfs_mutex); - return dentry; - } - - if (!ei->dentry && !ei->is_freed) { - ei->dentry = dentry; - eventfs_post_create_dir(ei); - dentry->d_fsdata = ei; - } else { - /* - * Should never happen unless we get here due to being freed. - * Otherwise it means two dentries exist with the same name. - */ - WARN_ON_ONCE(!ei->is_freed); - dentry = NULL; - } - mutex_unlock(&eventfs_mutex); + *e_dentry = dentry; // Remove me return dentry; } @@ -607,79 +462,55 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) { - const struct file_operations *fops; - const struct eventfs_entry *entry; struct eventfs_inode *ei_child; struct tracefs_inode *ti; struct eventfs_inode *ei; - struct dentry *ei_dentry = NULL; - struct dentry *ret = NULL; - struct dentry *d; const char *name = dentry->d_name.name; - umode_t mode; - void *data; - int idx; - int i; - int r; + struct dentry *result = NULL; ti = get_tracefs(dir); if (!(ti->flags & TRACEFS_EVENT_INODE)) - return NULL; - - /* Grab srcu to prevent the ei from going away */ - idx = srcu_read_lock(&eventfs_srcu); + return ERR_PTR(-EIO); - /* - * Grab the eventfs_mutex to consistent value from ti->private. - * This s - */ mutex_lock(&eventfs_mutex); - ei = READ_ONCE(ti->private); - if (ei && !ei->is_freed) - ei_dentry = READ_ONCE(ei->dentry); - mutex_unlock(&eventfs_mutex); - - if (!ei || !ei_dentry) - goto out; - data = ei->data; + ei = ti->private; + if (!ei || ei->is_freed) + goto enoent; - list_for_each_entry_srcu(ei_child, &ei->children, list, - srcu_read_lock_held(&eventfs_srcu)) { + list_for_each_entry(ei_child, &ei->children, list) { if (strcmp(ei_child->name, name) != 0) continue; - ret = simple_lookup(dir, dentry, flags); - if (IS_ERR(ret)) - goto out; - d = create_dir_dentry(ei, ei_child, ei_dentry); - dput(d); + if (ei_child->is_freed) + goto enoent; + lookup_dir_entry(dentry, ei, ei_child); goto out; } - for (i = 0; i < ei->nr_entries; i++) { - entry = &ei->entries[i]; - if (strcmp(name, entry->name) == 0) { - void *cdata = data; - 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; - mutex_unlock(&eventfs_mutex); - if (r <= 0) - continue; - ret = simple_lookup(dir, dentry, flags); - if (IS_ERR(ret)) - goto out; - d = create_file_dentry(ei, i, ei_dentry, name, mode, cdata, fops); - dput(d); - break; - } + for (int i = 0; i < ei->nr_entries; i++) { + void *data; + umode_t mode; + const struct file_operations *fops; + const struct eventfs_entry *entry = &ei->entries[i]; + + if (strcmp(name, entry->name) != 0) + continue; + + data = ei->data; + if (entry->callback(name, &mode, &data, &fops) <= 0) + goto enoent; + + lookup_file_dentry(dentry, ei, i, mode, data, fops); + goto out; } + + enoent: + /* Don't cache negative lookups, just return an error */ + result = ERR_PTR(-ENOENT); + out: - srcu_read_unlock(&eventfs_srcu, idx); - return ret; + mutex_unlock(&eventfs_mutex); + return result; } /* diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 888e42087847..5c84460feeeb 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -495,75 +495,6 @@ struct dentry *tracefs_end_creating(struct dentry *dentry) return dentry; } -/** - * eventfs_start_creating - start the process of creating a dentry - * @name: Name of the file created for the dentry - * @parent: The parent dentry where this dentry will be created - * - * This is a simple helper function for the dynamically created eventfs - * files. When the directory of the eventfs files are accessed, their - * dentries are created on the fly. This function is used to start that - * process. - */ -struct dentry *eventfs_start_creating(const char *name, struct dentry *parent) -{ - struct dentry *dentry; - int error; - - /* Must always have a parent. */ - if (WARN_ON_ONCE(!parent)) - return ERR_PTR(-EINVAL); - - error = simple_pin_fs(&trace_fs_type, &tracefs_mount, - &tracefs_mount_count); - if (error) - return ERR_PTR(error); - - if (unlikely(IS_DEADDIR(parent->d_inode))) - dentry = ERR_PTR(-ENOENT); - else - dentry = lookup_one_len(name, parent, strlen(name)); - - if (!IS_ERR(dentry) && dentry->d_inode) { - dput(dentry); - dentry = ERR_PTR(-EEXIST); - } - - if (IS_ERR(dentry)) - simple_release_fs(&tracefs_mount, &tracefs_mount_count); - - return dentry; -} - -/** - * eventfs_failed_creating - clean up a failed eventfs dentry creation - * @dentry: The dentry to clean up - * - * If after calling eventfs_start_creating(), a failure is detected, the - * resources created by eventfs_start_creating() needs to be cleaned up. In - * that case, this function should be called to perform that clean up. - */ -struct dentry *eventfs_failed_creating(struct dentry *dentry) -{ - dput(dentry); - simple_release_fs(&tracefs_mount, &tracefs_mount_count); - return NULL; -} - -/** - * eventfs_end_creating - Finish the process of creating a eventfs dentry - * @dentry: The dentry that has successfully been created. - * - * This function is currently just a place holder to match - * eventfs_start_creating(). In case any synchronization needs to be added, - * this function will be used to implement that without having to modify - * the callers of eventfs_start_creating(). - */ -struct dentry *eventfs_end_creating(struct dentry *dentry) -{ - return dentry; -} - /* Find the inode that this will use for default */ static struct inode *instance_inode(struct dentry *parent, struct inode *inode) { diff --git a/fs/tracefs/internal.h b/fs/tracefs/internal.h index 7d84349ade87..09037e2c173d 100644 --- a/fs/tracefs/internal.h +++ b/fs/tracefs/internal.h @@ -80,9 +80,6 @@ struct dentry *tracefs_start_creating(const char *name, struct dentry *parent); struct dentry *tracefs_end_creating(struct dentry *dentry); struct dentry *tracefs_failed_creating(struct dentry *dentry); struct inode *tracefs_get_inode(struct super_block *sb); -struct dentry *eventfs_start_creating(const char *name, struct dentry *parent); -struct dentry *eventfs_failed_creating(struct dentry *dentry); -struct dentry *eventfs_end_creating(struct dentry *dentry); void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry); #endif /* _TRACEFS_INTERNAL_H */ From patchwork Wed Jan 31 18:49:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2096090dyb; Wed, 31 Jan 2024 10:59:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEsUcv4mc2b2/7gUh3jrqcMyyMB+MUpdv9uFgmTtgkP0FMCvpEgCsL9053L/13I18Ju4fbG X-Received: by 2002:a05:6a20:9591:b0:19e:3ae2:eff2 with SMTP id iu17-20020a056a20959100b0019e3ae2eff2mr857971pzb.32.1706727582056; Wed, 31 Jan 2024 10:59:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706727582; cv=pass; d=google.com; s=arc-20160816; b=wp8lo9ABc+7Do/RyUTz4zWgdYJmlH3QYw40dwCxynchFpEgMENCAnfbxScylwVqDHX HJ/bkx+M4jmCEn0cFP4ZnWs3U2ItnKb0k+gxa+NUhy44Qkmz+CcHs8QDsMQQejv9XePY i4nbWwkhba541N9g5qvMhueuTYiz8JK+lHdJdyzUUVOnIsQk969RZ0s/rSrRtsQDhxB2 VIKR1KZ1MV//8ZJ+o77Y6Gg4Y9NSvo4FuwPPGrGC6Z3+qpThmtCGjMhZ9rGzvs2nrzl+ Q7Gnd3WI+urwzZn+bLnHGs0Nkx/jXunl9LrKT8fZBeNFHoWbq8ZS4p+zHbyW2BXKZ3V3 AmQg== ARC-Message-Signature: i=2; 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=Y/Z1i7KKyGeglSC2joikdWawCAxxaeMwyoZXZt4ik18=; fh=d0dr3kBjFWcIY4wLX+VuApF5Zn4oGdjVDQxXlAbAQ40=; b=Qc87+E100Stxl/9ZOSUSDI6kfboZx0VnJo57fgoX+nvVv/wtQ0MQNEdfLvqPJmQMNk 6eYRxERE1qPU/xleJq6KzYdC8aFOCbiR4M0T/KBJh/Xi11xu79Kolss/02damM4c6Vu3 u1CGRTkkyKkiOoxksWxApV1aFDcX9qrLZ3CpmqaI52hlgHw2PhtDQjBlotkgqi3sRFKh IRA6zg6kdvhZu2SeFlnLih1QchfKK5gAzeaYFOUjfPgd2uTGj9b2LK/wviHK6ptvhMIT GHSD6pTywFtLsg2vtezjAvjbgIc4aJTnGcgxsLfpMg5yoS5iW2sdnJ+tB6EUs4lH+IYW 57ZQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46993-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46993-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXNASfzV0b0CrDi+l2/jnhouzUXyjRcPgONLlGDYGWSBdeS0T07frdCG4FpNM/0zKNk6m4fIMVcYe0VPpmGiOsOwDxclQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id o14-20020a17090ac70e00b00295b3ecc46bsi1795303pjt.168.2024.01.31.10.59.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 10:59:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46993-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46993-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46993-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 12E90284DB3 for ; Wed, 31 Jan 2024 18:57:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B3A5313AA33; Wed, 31 Jan 2024 18:55:01 +0000 (UTC) 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 A369838FA0; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=NEs6IleJ5snY2LbZIDGbZXJHdome6f4cKx8bTlWb/m/nRmF90fvfXj4H0O5IqISJbI+J0NunuVYAtxhG6clmVqMS3cQqmNoXnJByke/ZgWMwDBJOsKSLGAJqaBH2cpvcX1Hh3WQX1XRRbzaE7DRqP2hqAGQijMcyZ/8bxuoTCIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=ryuvbz2YC+7XgFZae3ZU4jkaJ3KY5fCxQDuCm/pUEeI=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=QJiG8RDtGXy2q5lEeyGyyUy5+ygjOo4FX2aoEoC0jipKI1uZY1+qr0Wr4M+GBbaY+7KFIdDT9FtYdYuvX4dslI+af5VBe009cyiv8tnu8FbllbUgZD6+f+kIEMT4TqrdE7gMEV0uj1smOD0y0qU4QWldF7mRTMkyRMCE5fsjp2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2A4DFC41679; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjt-000000055Ro-0RDp; Wed, 31 Jan 2024 13:55:13 -0500 Message-ID: <20240131185512.961772428@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:23 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH v2 5/7] eventfs: Remove unused d_parent pointer field References: <20240131184918.945345370@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: 1789633580802444901 X-GMAIL-MSGID: 1789633580802444901 From: Linus Torvalds It's never used Link: https://lore.kernel.org/linux-trace-kernel/202401291043.e62e89dc-oliver.sang@intel.com/ Cc: stable@vger.kernel.org Fixes: c1504e510238 ("eventfs: Implement eventfs dir creation functions") Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Original-patch: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-4-torvalds@linux-foundation.org fs/tracefs/event_inode.c | 4 +--- fs/tracefs/internal.h | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 4878f4d578be..0289ec787367 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -686,10 +686,8 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode INIT_LIST_HEAD(&ei->list); mutex_lock(&eventfs_mutex); - if (!parent->is_freed) { + if (!parent->is_freed) list_add_tail(&ei->list, &parent->children); - ei->d_parent = parent->dentry; - } mutex_unlock(&eventfs_mutex); /* Was the parent freed? */ diff --git a/fs/tracefs/internal.h b/fs/tracefs/internal.h index 09037e2c173d..932733a2696a 100644 --- a/fs/tracefs/internal.h +++ b/fs/tracefs/internal.h @@ -36,7 +36,6 @@ struct eventfs_attr { * @name: the name of the directory to create * @children: link list into the child eventfs_inode * @dentry: the dentry of the directory - * @d_parent: pointer to the parent's dentry * @d_children: The array of dentries to represent the files when created * @entry_attrs: Saved mode and ownership of the @d_children * @attr: Saved mode and ownership of eventfs_inode itself @@ -51,7 +50,6 @@ struct eventfs_inode { const char *name; struct list_head children; struct dentry *dentry; /* Check is_freed to access */ - struct dentry *d_parent; struct dentry **d_children; struct eventfs_attr *entry_attrs; struct eventfs_attr attr; From patchwork Wed Jan 31 18:49:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194902 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2094904dyb; Wed, 31 Jan 2024 10:57:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IHS0XETwEj34ePMaJ7WoREJZFj0OrxzZE+oIlX8+DVs34YihJTp99y6j+5MKF2Ze+oMFai/ X-Received: by 2002:a05:622a:50d:b0:42b:e666:5cf5 with SMTP id l13-20020a05622a050d00b0042be6665cf5mr182723qtx.19.1706727430188; Wed, 31 Jan 2024 10:57:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706727430; cv=pass; d=google.com; s=arc-20160816; b=a9xnq6Akwv0ADzYkS/0BbA8BRvjlIIhAZNFjOdHhIEScvVYA/kLnHox5Dh9SJCdLCp ZI5srev3lNhtcT583MOLnXNXOWsGRnRxrjK6FyzuW2Hu0MXOC7vODDclGzXZiUH8/Kkh mI2EINPWlynA51sDgMLCTDpzlwXwiaGHAKl0sTpRU1h8K9joixBbQEJvQ7ASHpFiDCqV dxMM/fm1MkrJEtlHtZ7y48sOIqZiKV7r5BhEAhnwmyxoZE8cGHouGu7OeTofKwMm9zWC xnJNIUlxWnSctyRXWn3IBIXzj07lJUTXXKIu1KzDZB++BmR9z518R8o0jMzNJjn3imTu 8KjQ== ARC-Message-Signature: i=2; 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=qewHzN30CBK4eQVXU8GvXDZoxGE6ZpGuDogPOlAAf4I=; fh=Vmajbll7HNkJSQot1t0Jrxw7YRGziKeZg/Jy5fmaHo0=; b=0kVYCI9a8Bzxa8rgtbaEZH+k8lf6B43CkKa8P46/qL1Bl/t3Ldp5KNxCvSp7NkvuWu IstO4nZ1JL5IPTKY+ORj0c1FzwDPKsKdj3VOmi8kvtfEYGlqYtoaj27jMdcfpJNHGw+2 hNAB6GFlM2iKohH5sXItPGcZDAR7cJNwRsS7GC2jkLqCi/kAyABaG6zDtgz0yEfyTOWW IL4i4nlnBiSNdQa5lalz0dyHgGmlUi/ckX3ZViOdD8Aiep3kSOaTTRTDYMJwib0mO0s7 ElL5kHLN6paTISgGWqeMy79r0bWSWzxyWwROc4p0TI3MpKubaXbor38Vd8bl2ARRHXSP YuFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46991-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46991-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCWnmGOg6VcuTn78AyNM28cK/I3AIV7/clAxX+HL88iSYwhVunYsTnpJpXpN4PW89HNrror4SDUWRUcAL1OQm2o7FCED9g== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d15-20020ac85d8f000000b0042aa5c657e7si7477306qtx.449.2024.01.31.10.57.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 10:57:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46991-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; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46991-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46991-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 0223E1C22A23 for ; Wed, 31 Jan 2024 18:57:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6DA41386D6; Wed, 31 Jan 2024 18:55:00 +0000 (UTC) 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 5E12E3EA96; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=YagUh+JHNe8k94F7NDoPk36aEj43tchKDVYICh4CtOpAeb0d88VQQwuyRAjPQOX0/lJjc2it1EmDJZXRnPpJUP5NsaY4bF4SSpR8oKMPizUKvdMexsGM1gGX5Z9h4EQ9QynK/t7ZEKlXJ0CcTsljZJuoKoKYILLt63MQ3RLHBEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=q343WLfpufl7W/ZraSQs5ordYtlVQ0quLjuxzkJRZAM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=OMBb+6BOD7b0uHZahDt8hAJeFiXa1gLsj15ae3fo8QL/2iuPf/RMHfHAw12pFekQ+Effpf3RdYrFeFGz7uz1c5/LjweF/44wLoVs1s7PRF1AUid//N2BBWnLMrwrBwxYUk6B0e/gFKYBQfOakQRK+jj2LrfNxVA+9ecwkQq+6uw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34D08C41606; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjt-000000055SJ-15QJ; Wed, 31 Jan 2024 13:55:13 -0500 Message-ID: <20240131185513.124644253@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:24 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH v2 6/7] eventfs: Clean up dentry ops and add revalidate function References: <20240131184918.945345370@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: 1789633421979697287 X-GMAIL-MSGID: 1789633421979697287 From: Linus Torvalds In order for the dentries to stay up-to-date with the eventfs changes, just add a 'd_revalidate' function that checks the 'is_freed' bit. Also, clean up the dentry release to actually use d_release() rather than the slightly odd d_iput() function. We don't care about the inode, all we want to do is to get rid of the refcount to the eventfs data added by dentry->d_fsdata. It would probably be cleaner to make eventfs its own filesystem, or at least set its own dentry ops when looking up eventfs files. But as it is, only eventfs dentries use d_fsdata, so we don't really need to split these things up by use. Another thing that might be worth doing is to make all eventfs lookups mark their dentries as not worth caching. We could do that with d_delete(), but the DCACHE_DONTCACHE flag would likely be even better. As it is, the dentries are all freeable, but they only tend to get freed at memory pressure rather than more proactively. But that's a separate issue. Link: https://lore.kernel.org/linux-trace-kernel/202401291043.e62e89dc-oliver.sang@intel.com/ Cc: stable@vger.kernel.org Fixes: c1504e510238 ("eventfs: Implement eventfs dir creation functions") Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Original-patch: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-6-torvalds@linux-foundation.org fs/tracefs/event_inode.c | 5 ++--- fs/tracefs/inode.c | 27 ++++++++++++++++++--------- fs/tracefs/internal.h | 3 ++- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 0289ec787367..0213a3375d53 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -378,13 +378,12 @@ static void free_ei(struct eventfs_inode *ei) } /** - * eventfs_set_ei_status_free - remove the dentry reference from an eventfs_inode - * @ti: the tracefs_inode of the dentry + * eventfs_d_release - dentry is going away * @dentry: dentry which has the reference to remove. * * Remove the association between a dentry from an eventfs_inode. */ -void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry) +void eventfs_d_release(struct dentry *dentry) { struct eventfs_inode *ei; int i; diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 5c84460feeeb..d65ffad4c327 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -377,21 +377,30 @@ static const struct super_operations tracefs_super_operations = { .show_options = tracefs_show_options, }; -static void tracefs_dentry_iput(struct dentry *dentry, struct inode *inode) +/* + * It would be cleaner if eventfs had its own dentry ops. + * + * Note that d_revalidate is called potentially under RCU, + * so it can't take the eventfs mutex etc. It's fine - if + * we open a file just as it's marked dead, things will + * still work just fine, and just see the old stale case. + */ +static void tracefs_d_release(struct dentry *dentry) { - struct tracefs_inode *ti; + if (dentry->d_fsdata) + eventfs_d_release(dentry); +} - if (!dentry || !inode) - return; +static int tracefs_d_revalidate(struct dentry *dentry, unsigned int flags) +{ + struct eventfs_inode *ei = dentry->d_fsdata; - ti = get_tracefs(inode); - if (ti && ti->flags & TRACEFS_EVENT_INODE) - eventfs_set_ei_status_free(ti, dentry); - iput(inode); + return !(ei && ei->is_freed); } static const struct dentry_operations tracefs_dentry_operations = { - .d_iput = tracefs_dentry_iput, + .d_revalidate = tracefs_d_revalidate, + .d_release = tracefs_d_release, }; static int trace_fill_super(struct super_block *sb, void *data, int silent) diff --git a/fs/tracefs/internal.h b/fs/tracefs/internal.h index 932733a2696a..4b50a0668055 100644 --- a/fs/tracefs/internal.h +++ b/fs/tracefs/internal.h @@ -78,6 +78,7 @@ struct dentry *tracefs_start_creating(const char *name, struct dentry *parent); struct dentry *tracefs_end_creating(struct dentry *dentry); struct dentry *tracefs_failed_creating(struct dentry *dentry); struct inode *tracefs_get_inode(struct super_block *sb); -void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry); + +void eventfs_d_release(struct dentry *dentry); #endif /* _TRACEFS_INTERNAL_H */ From patchwork Wed Jan 31 18:49:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steven Rostedt X-Patchwork-Id: 194903 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp2095268dyb; Wed, 31 Jan 2024 10:57:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvsBXKAz7H3sOR8nUIFsVPIBWyx3a3xl8rc1ty9aftjax2lrKSp9SWJmDFuNGN7Mmokeyy X-Received: by 2002:a17:906:c58:b0:a36:239:77a with SMTP id t24-20020a1709060c5800b00a360239077amr1824813ejf.23.1706727475320; Wed, 31 Jan 2024 10:57:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706727475; cv=pass; d=google.com; s=arc-20160816; b=nM2MI7KV4SuTAJSZmpTkwBfN41JNGxrYWw+RuEFh1pXpK3d5DMyIUooEd4le4IaTl3 7yrhqK94ckTB+oevQr3Qw6uhGUW+QJ3h84RnEFrF7ejtZz0OTsL4CkpON2VVSj1FhT4H LQGbV5XofbzuJO8os2FWLieuvXzPwG2yGuPm97tvv2i31BlzUxp7K98MDJcZ9NQBV2wD ElpAcBwmENmfrthvkG786SVpNk8ArN7iXyOAwmi+mQqcwCX3C98OxEExQw4gqb0rClRW Is4SsEdhG8S+nOgqp0sN9hRuopCFx7VNtFQ9OJLe7COTHtSte73QZssYBjjQRiheyqeR hNng== ARC-Message-Signature: i=2; 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=zMB8G1f6ar0bdbAj0CSRMKHm8j2Vy1CvHOjiVqWgnj0=; fh=zWNhV9+WiXu1T3dN3Uh5GCHvT0rQrow9G3MEl2kyxag=; b=tGCNdlzYGQtv+zbZUdrn6kXjTx16Ipe7U2oO65ZXT8R285qWNLY4pHDA4AJ3idZS/x ldIPumT8hqygPH3mudMtmbKQ5ba3VB8YDl2I/ErWZ4vpDORKlZky6KKE2v8rzasMFB7r +lYkAwZF3ZyLfaQ1LZ0AVygKHfw4zdoPeYPoPi02p+/jnljffJTAQvKbUUE1I+gwwvAi qjS3ejYgy7XPSAtxxgO72pYDFcZaq4Q5vsaFqmt4Cow6Wm9oN28ndw2KAvu1AvXM0ly8 TsKhvhx0ooyRs1o/z4V/Q2yaU14uBicVYe4uB+CKiY36qW/jmDvxPGPlrmaB919s4PYD 8emg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46994-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46994-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXV8BQsVTEp4MaONMogh45kCH/QRrffyxL2v/8UNnn8FnAvEKgKPHeYpPlRvyQSo4g7DsrU6zyqcldrnd7zyv8vEv/3ag== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u16-20020a170906069000b00a31b755546asi5860901ejb.488.2024.01.31.10.57.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 10:57:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-46994-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-46994-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-46994-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 B75F51F2773A for ; Wed, 31 Jan 2024 18:57:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BFAF13BE93; Wed, 31 Jan 2024 18:55:04 +0000 (UTC) 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 B2F2A3FE23; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; cv=none; b=geS+iBYe2YKKFpOS8Npk1KDEaGpvfmPDabQl6L/6C0hxlJ1pUA49rLpnS/vymtC7uksbtx8rFOrt51RuncmNb3BL6XD6j+Z/QUxo9/lBjNaw4vtJ5WDLU8DMJzuxEsoqrHzamYyK9oTQLr2MvRoNiGAwAQJlkvqF7VxmwHUArs0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706727298; c=relaxed/simple; bh=b6EnHVWY5z0zH/Z7gqswLCroF+3CnlRaFbq66GvEVjs=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=g5sHxGgvs8Vkub/juyjeJN/ZmaOe5b6Sud9tEWevPSer/qsA1SPpoknnU6bclOcKasDC3MAL5rSEB69PRuW/0h7hkbgsBaCO6DITX19YAr1B/wZFGkhOHBMHe0GJ+DgJasasLFWZmh9hnijweO6Cn07ScrBGRyYqk1x9y3QtHLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61B39C32786; Wed, 31 Jan 2024 18:54:58 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1rVFjt-000000055Sn-1mGl; Wed, 31 Jan 2024 13:55:13 -0500 Message-ID: <20240131185513.280463000@goodmis.org> User-Agent: quilt/0.67 Date: Wed, 31 Jan 2024 13:49:25 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Christian Brauner , Al Viro , Ajay Kaher , Greg Kroah-Hartman , stable@vger.kernel.org Subject: [PATCH v2 7/7] eventfs: Get rid of dentry pointers without refcounts References: <20240131184918.945345370@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: 1789633469174143786 X-GMAIL-MSGID: 1789633469174143786 From: Linus Torvalds The eventfs inode had pointers to dentries (and child dentries) without actually holding a refcount on said pointer. That is fundamentally broken, and while eventfs tried to then maintain coherence with dentries going away by hooking into the '.d_iput' callback, that doesn't actually work since it's not ordered wrt lookups. There were two reasonms why eventfs tried to keep a pointer to a dentry: - the creation of a 'events' directory would actually have a stable dentry pointer that it created with tracefs_start_creating(). And it needed that dentry when tearing it all down again in eventfs_remove_events_dir(). This use is actually ok, because the special top-level events directory dentries are actually stable, not just a temporary cache of the eventfs data structures. - the 'eventfs_inode' (aka ei) needs to stay around as long as there are dentries that refer to it. It then used these dentry pointers as a replacement for doing reference counting: it would try to make sure that there was only ever one dentry associated with an event_inode, and keep a child dentry array around to see which dentries might still refer to the parent ei. This gets rid of the invalid dentry pointer use, and renames the one valid case to a different name to make it clear that it's not just any random dentry. The magic child dentry array that is kind of a "reverse reference list" is simply replaced by having child dentries take a ref to the ei. As does the directory dentries. That makes the broken use case go away. Link: https://lore.kernel.org/linux-trace-kernel/202401291043.e62e89dc-oliver.sang@intel.com/ Cc: stable@vger.kernel.org Fixes: c1504e510238 ("eventfs: Implement eventfs dir creation functions") Signed-off-by: Linus Torvalds Signed-off-by: Steven Rostedt (Google) --- Changes since v1: https://lore.kernel.org/linux-trace-kernel/20240130190355.11486-5-torvalds@linux-foundation.org - Put back the kstrdup_const() - use kfree_rcu(ei, rcu); - Replace simple_recursive_removal() with d_invalidate(). fs/tracefs/event_inode.c | 247 ++++++++++++--------------------------- fs/tracefs/internal.h | 7 +- 2 files changed, 77 insertions(+), 177 deletions(-) diff --git a/fs/tracefs/event_inode.c b/fs/tracefs/event_inode.c index 0213a3375d53..31cbe38739fa 100644 --- a/fs/tracefs/event_inode.c +++ b/fs/tracefs/event_inode.c @@ -62,6 +62,35 @@ enum { #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1) +/* + * eventfs_inode reference count management. + * + * NOTE! We count only references from dentries, in the + * form 'dentry->d_fsdata'. There are also references from + * directory inodes ('ti->private'), but the dentry reference + * count is always a superset of the inode reference count. + */ +static void release_ei(struct kref *ref) +{ + struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref); + kfree(ei->entry_attrs); + kfree_const(ei->name); + kfree_rcu(ei, rcu); +} + +static inline void put_ei(struct eventfs_inode *ei) +{ + if (ei) + kref_put(&ei->kref, release_ei); +} + +static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei) +{ + if (ei) + kref_get(&ei->kref); + return ei; +} + static struct dentry *eventfs_root_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags); @@ -289,7 +318,8 @@ static void update_inode_attr(struct dentry *dentry, struct inode *inode, * directory. The inode.i_private pointer will point to @data in the open() * call. */ -static struct dentry *lookup_file(struct dentry *dentry, +static struct dentry *lookup_file(struct eventfs_inode *parent_ei, + struct dentry *dentry, umode_t mode, struct eventfs_attr *attr, void *data, @@ -302,7 +332,7 @@ static struct dentry *lookup_file(struct dentry *dentry, mode |= S_IFREG; if (WARN_ON_ONCE(!S_ISREG(mode))) - return NULL; + return ERR_PTR(-EIO); inode = tracefs_get_inode(dentry->d_sb); if (unlikely(!inode)) @@ -321,9 +351,12 @@ static struct dentry *lookup_file(struct dentry *dentry, ti = get_tracefs(inode); ti->flags |= TRACEFS_EVENT_INODE; + // Files have their parent's ei as their fsdata + dentry->d_fsdata = get_ei(parent_ei); + d_add(dentry, inode); fsnotify_create(dentry->d_parent->d_inode, dentry); - return dentry; + return NULL; }; /** @@ -359,22 +392,29 @@ static struct dentry *lookup_dir_entry(struct dentry *dentry, /* Only directories have ti->private set to an ei, not files */ ti->private = ei; - dentry->d_fsdata = ei; - ei->dentry = dentry; // Remove me! + dentry->d_fsdata = get_ei(ei); inc_nlink(inode); d_add(dentry, inode); inc_nlink(dentry->d_parent->d_inode); fsnotify_mkdir(dentry->d_parent->d_inode, dentry); - return dentry; + return NULL; } -static void free_ei(struct eventfs_inode *ei) +static inline struct eventfs_inode *alloc_ei(const char *name) { - kfree_const(ei->name); - kfree(ei->d_children); - kfree(ei->entry_attrs); - kfree(ei); + struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL); + + if (!ei) + return NULL; + + ei->name = kstrdup_const(name, GFP_KERNEL); + if (!ei->name) { + kfree(ei); + return NULL; + } + kref_init(&ei->kref); + return ei; } /** @@ -385,39 +425,13 @@ static void free_ei(struct eventfs_inode *ei) */ void eventfs_d_release(struct dentry *dentry) { - struct eventfs_inode *ei; - int i; - - mutex_lock(&eventfs_mutex); - - ei = dentry->d_fsdata; - if (!ei) - goto out; - - /* This could belong to one of the files of the ei */ - if (ei->dentry != dentry) { - for (i = 0; i < ei->nr_entries; i++) { - if (ei->d_children[i] == dentry) - break; - } - if (WARN_ON_ONCE(i == ei->nr_entries)) - goto out; - ei->d_children[i] = NULL; - } else if (ei->is_freed) { - free_ei(ei); - } else { - ei->dentry = NULL; - } - - dentry->d_fsdata = NULL; - out: - mutex_unlock(&eventfs_mutex); + put_ei(dentry->d_fsdata); } /** * lookup_file_dentry - create a dentry for a file of an eventfs_inode * @ei: the eventfs_inode that the file will be created under - * @idx: the index into the d_children[] of the @ei + * @idx: the index into the entry_attrs[] of the @ei * @parent: The parent dentry of the created file. * @name: The name of the file to create * @mode: The mode of the file. @@ -434,17 +448,11 @@ lookup_file_dentry(struct dentry *dentry, const struct file_operations *fops) { struct eventfs_attr *attr = NULL; - struct dentry **e_dentry = &ei->d_children[idx]; if (ei->entry_attrs) attr = &ei->entry_attrs[idx]; - dentry->d_fsdata = ei; // NOTE: ei of _parent_ - lookup_file(dentry, mode, attr, data, fops); - - *e_dentry = dentry; // Remove me - - return dentry; + return lookup_file(ei, dentry, mode, attr, data, fops); } /** @@ -465,7 +473,7 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, struct tracefs_inode *ti; struct eventfs_inode *ei; const char *name = dentry->d_name.name; - struct dentry *result = NULL; + struct dentry *result; ti = get_tracefs(dir); if (!(ti->flags & TRACEFS_EVENT_INODE)) @@ -482,7 +490,7 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, continue; if (ei_child->is_freed) goto enoent; - lookup_dir_entry(dentry, ei, ei_child); + result = lookup_dir_entry(dentry, ei, ei_child); goto out; } @@ -499,7 +507,7 @@ static struct dentry *eventfs_root_lookup(struct inode *dir, if (entry->callback(name, &mode, &data, &fops) <= 0) goto enoent; - lookup_file_dentry(dentry, ei, i, mode, data, fops); + result = lookup_file_dentry(dentry, ei, i, mode, data, fops); goto out; } @@ -659,25 +667,10 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode if (!parent) return ERR_PTR(-EINVAL); - ei = kzalloc(sizeof(*ei), GFP_KERNEL); + ei = alloc_ei(name); if (!ei) return ERR_PTR(-ENOMEM); - ei->name = kstrdup_const(name, GFP_KERNEL); - if (!ei->name) { - kfree(ei); - return ERR_PTR(-ENOMEM); - } - - if (size) { - ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL); - if (!ei->d_children) { - kfree_const(ei->name); - kfree(ei); - return ERR_PTR(-ENOMEM); - } - } - ei->entries = entries; ei->nr_entries = size; ei->data = data; @@ -691,7 +684,7 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode /* Was the parent freed? */ if (list_empty(&ei->list)) { - free_ei(ei); + put_ei(ei); ei = NULL; } return ei; @@ -726,28 +719,20 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry if (IS_ERR(dentry)) return ERR_CAST(dentry); - ei = kzalloc(sizeof(*ei), GFP_KERNEL); + ei = alloc_ei(name); if (!ei) - goto fail_ei; + goto fail; inode = tracefs_get_inode(dentry->d_sb); if (unlikely(!inode)) goto fail; - if (size) { - ei->d_children = kcalloc(size, sizeof(*ei->d_children), GFP_KERNEL); - if (!ei->d_children) - goto fail; - } - - ei->dentry = dentry; + // Note: we have a ref to the dentry from tracefs_start_creating() + ei->events_dir = dentry; ei->entries = entries; ei->nr_entries = size; ei->is_events = 1; ei->data = data; - ei->name = kstrdup_const(name, GFP_KERNEL); - if (!ei->name) - goto fail; /* Save the ownership of this directory */ uid = d_inode(dentry->d_parent)->i_uid; @@ -778,7 +763,7 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry inode->i_op = &eventfs_root_dir_inode_operations; inode->i_fop = &eventfs_file_operations; - dentry->d_fsdata = ei; + dentry->d_fsdata = get_ei(ei); /* directory inodes start off with i_nlink == 2 (for "." entry) */ inc_nlink(inode); @@ -790,72 +775,11 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry return ei; fail: - kfree(ei->d_children); - kfree(ei); - fail_ei: + put_ei(ei); tracefs_failed_creating(dentry); return ERR_PTR(-ENOMEM); } -static LLIST_HEAD(free_list); - -static void eventfs_workfn(struct work_struct *work) -{ - struct eventfs_inode *ei, *tmp; - struct llist_node *llnode; - - llnode = llist_del_all(&free_list); - llist_for_each_entry_safe(ei, tmp, llnode, llist) { - /* This dput() matches the dget() from unhook_dentry() */ - for (int i = 0; i < ei->nr_entries; i++) { - if (ei->d_children[i]) - dput(ei->d_children[i]); - } - /* This should only get here if it had a dentry */ - if (!WARN_ON_ONCE(!ei->dentry)) - dput(ei->dentry); - } -} - -static DECLARE_WORK(eventfs_work, eventfs_workfn); - -static void free_rcu_ei(struct rcu_head *head) -{ - struct eventfs_inode *ei = container_of(head, struct eventfs_inode, rcu); - - if (ei->dentry) { - /* Do not free the ei until all references of dentry are gone */ - if (llist_add(&ei->llist, &free_list)) - queue_work(system_unbound_wq, &eventfs_work); - return; - } - - /* If the ei doesn't have a dentry, neither should its children */ - for (int i = 0; i < ei->nr_entries; i++) { - WARN_ON_ONCE(ei->d_children[i]); - } - - free_ei(ei); -} - -static void unhook_dentry(struct dentry *dentry) -{ - if (!dentry) - return; - /* - * Need to add a reference to the dentry that is expected by - * simple_recursive_removal(), which will include a dput(). - */ - dget(dentry); - - /* - * Also add a reference for the dput() in eventfs_workfn(). - * That is required as that dput() will free the ei after - * the SRCU grace period is over. - */ - dget(dentry); -} - /** * eventfs_remove_rec - remove eventfs dir or file from list * @ei: eventfs_inode to be removed. @@ -868,8 +792,6 @@ static void eventfs_remove_rec(struct eventfs_inode *ei, int level) { struct eventfs_inode *ei_child; - if (!ei) - return; /* * Check recursion depth. It should never be greater than 3: * 0 - events/ @@ -881,28 +803,12 @@ static void eventfs_remove_rec(struct eventfs_inode *ei, int level) return; /* search for nested folders or files */ - list_for_each_entry_srcu(ei_child, &ei->children, list, - lockdep_is_held(&eventfs_mutex)) { - /* Children only have dentry if parent does */ - WARN_ON_ONCE(ei_child->dentry && !ei->dentry); + list_for_each_entry(ei_child, &ei->children, list) eventfs_remove_rec(ei_child, level + 1); - } - ei->is_freed = 1; - - for (int i = 0; i < ei->nr_entries; i++) { - if (ei->d_children[i]) { - /* Children only have dentry if parent does */ - WARN_ON_ONCE(!ei->dentry); - unhook_dentry(ei->d_children[i]); - } - } - - unhook_dentry(ei->dentry); - - list_del_rcu(&ei->list); - call_srcu(&eventfs_srcu, &ei->rcu, free_rcu_ei); + list_del(&ei->list); + put_ei(ei); } /** @@ -913,22 +819,12 @@ static void eventfs_remove_rec(struct eventfs_inode *ei, int level) */ void eventfs_remove_dir(struct eventfs_inode *ei) { - struct dentry *dentry; - if (!ei) return; mutex_lock(&eventfs_mutex); - dentry = ei->dentry; eventfs_remove_rec(ei, 0); mutex_unlock(&eventfs_mutex); - - /* - * If any of the ei children has a dentry, then the ei itself - * must have a dentry. - */ - if (dentry) - simple_recursive_removal(dentry, NULL); } /** @@ -941,7 +837,11 @@ void eventfs_remove_events_dir(struct eventfs_inode *ei) { struct dentry *dentry; - dentry = ei->dentry; + dentry = ei->events_dir; + if (!dentry) + return; + + ei->events_dir = NULL; eventfs_remove_dir(ei); /* @@ -951,5 +851,6 @@ void eventfs_remove_events_dir(struct eventfs_inode *ei) * sticks around while the other ei->dentry are created * and destroyed dynamically. */ + d_invalidate(dentry); dput(dentry); } diff --git a/fs/tracefs/internal.h b/fs/tracefs/internal.h index 4b50a0668055..1886f1826cd8 100644 --- a/fs/tracefs/internal.h +++ b/fs/tracefs/internal.h @@ -35,8 +35,7 @@ struct eventfs_attr { * @entries: the array of entries representing the files in the directory * @name: the name of the directory to create * @children: link list into the child eventfs_inode - * @dentry: the dentry of the directory - * @d_children: The array of dentries to represent the files when created + * @events_dir: the dentry of the events directory * @entry_attrs: Saved mode and ownership of the @d_children * @attr: Saved mode and ownership of eventfs_inode itself * @data: The private data to pass to the callbacks @@ -45,12 +44,12 @@ struct eventfs_attr { * @nr_entries: The number of items in @entries */ struct eventfs_inode { + struct kref kref; struct list_head list; const struct eventfs_entry *entries; const char *name; struct list_head children; - struct dentry *dentry; /* Check is_freed to access */ - struct dentry **d_children; + struct dentry *events_dir; struct eventfs_attr *entry_attrs; struct eventfs_attr attr; void *data;