From patchwork Thu Nov 9 15:06:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 163412 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp497319vqs; Thu, 9 Nov 2023 07:07:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjB0vZFzn7/B19COPRDT00CSpW0bx+Jf5qKlZ+6KF176jY62ZqAW9AU4/4tDcA4ozQTca4 X-Received: by 2002:a05:6a20:4294:b0:182:11b2:b990 with SMTP id o20-20020a056a20429400b0018211b2b990mr6091803pzj.12.1699542438396; Thu, 09 Nov 2023 07:07:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699542438; cv=none; d=google.com; s=arc-20160816; b=MYrri6njqOcHcH6vHj4vN0SibMJxllxHFyBduCMAEV/J46Etc5rOalM7H6qV7rLZZH rMQ8ZUGG21WNyCOVNYGkAWqtpgxMvaH3ySHNgGMTdvtIuujXojiRwFGhVPQn+XQvuxsB B9oVDJjSsVndPsJ0rgtQ8hemvIUA2SzgnEMR9x/S6J3kPmBBAm0uqIZLrVtpg+wjHxgC nVF7HQrp7VQB2NDhpNXjG7JD91Sqp0B46zmxHwkrsm2smFLxpgzshyh4XtKsxUcUqzmP v+5GfgWbFcj7mKp8WmWXJBJkTc7SF+bxwokx4NWPlqZrLDor8Hyq/eBcSJd1iNwCxO/Q cBXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=Eb8MG9TBz4s7MRmJjfR+Bq5zgXzHMYOhLNojz4j8puU=; fh=rdbF92JMeMxiLZzu5202iuBrKirLFgsZK/ul3mO2aN8=; b=wN6SmCO8Q60lPGtD6EPAeBFPYCI5kCYxPTO8xJHagaum3MALFLnlE6kPlNyCvB5t6g JBQfl1e95xVHOW6BYIhhF9kdG5um3REVgy5f7+/y9OENQzCeAHdAEFclh/XFlL+qcpUO 1zD8bTHZoE6KdPUcjhi+K8RwzV/xCq4kPdvmSQN82lHKeADArsOYsnI1U6vLoSTvJCI1 FEqds1g+U/BzwHjgkKH12tppdaYlwuzb77I3bFikjB1ceAncIVej5OFKefDKMFuBJS9I fOfSEk+N+jUr/JFw1EkhIAQqJogxlhY5vmTuvXuB/vaYgu3tb94V7lpdB6K5wjTMXPT9 JRoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=h+o57ad7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=REJECT dis=NONE) header.from=sipsolutions.net Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p9-20020a056a000b4900b006c4ad151a62si1583811pfo.266.2023.11.09.07.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 07:07:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=h+o57ad7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=REJECT dis=NONE) header.from=sipsolutions.net Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F1A7A831334D; Thu, 9 Nov 2023 07:07:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234424AbjKIPHE (ORCPT + 31 others); Thu, 9 Nov 2023 10:07:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231659AbjKIPHD (ORCPT ); Thu, 9 Nov 2023 10:07:03 -0500 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:242:246e::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7EDE35A9; Thu, 9 Nov 2023 07:07:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sipsolutions.net; s=mail; h=Content-Transfer-Encoding:MIME-Version: Message-ID:Date:Subject:Cc:To:From:Content-Type:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-To:Resent-Cc: Resent-Message-ID:In-Reply-To:References; bh=Eb8MG9TBz4s7MRmJjfR+Bq5zgXzHMYOhLNojz4j8puU=; t=1699542421; x=1700752021; b=h+o57ad7VbE6V682ZrJM0u1AUYYwmG8MchFwi4gU61IZ3YXuKF/dUGPTBikNAtfEnvdZgXKbdZB y7KClNzpRbxZ0StFxJwxUdb2u7Nq314og0M35JNQbgHwtWkZClX7+p73p3fdKpKAlzPFD5ja1/4v/ +MPaOvrA1wwRn5Oac5LYw98K63kl6YRc7idaXTHtB4Kfepy+pfedzCUan0qb3CBRARbNh7V+syPxO JnARg9UXy8gRULkqWekMoOOC0nvTg9CEsa9UH5Lei1dwWVlQngE1YEziZL0483pwZK7aZfTZq4EI5 Vl+OkJtnfclWJ2o0GWKuvJK4dbrPiNgJpEDw==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1r16cO-00000001pgv-0fKp; Thu, 09 Nov 2023 16:06:52 +0100 From: Johannes Berg To: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman , "Rafael J. Wysocki" , Nicolai Stange , Johannes Berg Subject: [PATCH] debugfs: only clean up d_fsdata for d_is_reg() Date: Thu, 9 Nov 2023 16:06:40 +0100 Message-ID: <20231109160639.514a2568f1e7.I64fe5615568e87f9ae2d7fb2ac4e5fa96924cb50@changeid> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Thu, 09 Nov 2023 07:07:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782099411790222827 X-GMAIL-MSGID: 1782099411790222827 From: Johannes Berg debugfs_create_automount() can store a function pointer in d_fsdata, and for directories it may be NULL. The commit 7c8d469877b1 ("debugfs: add support for more elaborate ->d_fsdata") ignored that, and while freeing NULL is just fine, if an automount is ever removed we'd attempt to kfree() the function pointer. This currently never happens since the only user (tracing) will never remove the automount dir. Later patches changed the logic here again to store the real fops, and store the allocation only after a debugfs file reference is obtained via debugfs_file_get(). Remove debugfs_release_dentry() so we won't attempt to do anything common with the different uses of d_fsdata, and put the freeing of the allocated data where it's last possibly used, in __debugfs_file_removed(), which is only called for regular files. Also check in debugfs_file_get() that it gets only called on regular files, just to make things clearer. Signed-off-by: Johannes Berg --- fs/debugfs/file.c | 3 +++ fs/debugfs/inode.c | 14 +++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c index 1f971c880dde..1a20c7db8e11 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c @@ -84,6 +84,9 @@ int debugfs_file_get(struct dentry *dentry) struct debugfs_fsdata *fsd; void *d_fsd; + if (WARN_ON(!d_is_reg(dentry))) + return -EINVAL; + d_fsd = READ_ONCE(dentry->d_fsdata); if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) { fsd = d_fsd; diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c index 3f81f73c241a..3f00cd40c81d 100644 --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -235,14 +235,6 @@ static const struct super_operations debugfs_super_operations = { .free_inode = debugfs_free_inode, }; -static void debugfs_release_dentry(struct dentry *dentry) -{ - void *fsd = dentry->d_fsdata; - - if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) - kfree(dentry->d_fsdata); -} - static struct vfsmount *debugfs_automount(struct path *path) { debugfs_automount_t f; @@ -252,7 +244,6 @@ static struct vfsmount *debugfs_automount(struct path *path) static const struct dentry_operations debugfs_dops = { .d_delete = always_delete_dentry, - .d_release = debugfs_release_dentry, .d_automount = debugfs_automount, }; @@ -734,6 +725,11 @@ static void __debugfs_file_removed(struct dentry *dentry) return; if (!refcount_dec_and_test(&fsd->active_users)) wait_for_completion(&fsd->active_users_drained); + + /* this no longer matters */ + dentry->d_fsdata = NULL; + + kfree(fsd); } static void remove_one(struct dentry *victim)