From patchwork Thu Nov 9 21:22:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Berg X-Patchwork-Id: 163606 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp722034vqs; Thu, 9 Nov 2023 13:38:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IEWjnedNOUnNT/RNXU6w3UE0MfXNzk2nHoUCFlTI/wTExE66PlIrIuW0M48Fujli49c3Vfv X-Received: by 2002:a17:90a:4585:b0:27d:222c:f5eb with SMTP id v5-20020a17090a458500b0027d222cf5ebmr2762460pjg.11.1699565921151; Thu, 09 Nov 2023 13:38:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699565921; cv=none; d=google.com; s=arc-20160816; b=lF7q/A9F9QqKDNYQt4m2IGZDB2hcjVt/ecN4C+5fbb4eBBFaUbXy9jNtxMq+67fUl0 3x05V3TyWMT06Ghbe4I5X29MJ+T3fThHKYeH0ySh7Cvdldlj42DvQ8SetzmdYaWSVM2H sTLcTT6DFNfqLmGiIT6IegiYRymhccl/XCKyLaR0F/3LxvIfyKXg56cZXPhKAQDUyPjt /ehDD+rPIoqvoEVa1V6gWGJc9a0+7pC8DJ7UUpGYt1R5mRyOGkNseSUqLMNueiCyfKL1 ocZLN1WA2j9GLuhn4S46B7SiU4A5NDf5cAtJqX/W3jo2LEsW6iRcrFuSekfL29V+VpZj UnbA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/3x7Uzb2xVsM+qPa/s6ahfiXYwxF7KgqrBu++ys4yuQ=; fh=fr6hIKaSb7E+0K08IHhjy1pQvdN8X98xxmR7pnwhgag=; b=fbzgi+ffrQ6DvPgSWj8yu8vH2p1/ByLV04ABgg8SM937V+vl46NzmhANo23HIacYmj +zU12gHnFZ9z6x/afJ/7KmPMtI4G0nrlcJxeZeTYYHZQQCpkKhbUCAGOXLNfDDGaKdm6 H0h3UuftR8gqduE6ddz6uWmZ6dbuILhlgNpQMVn6yhP13/IaHJX8SzFje03zMJcYpzN+ iAxrbixK/4e9TJ3wGZKEIjel7T7J42wCLI3CizabPF9zJ7yMpagNxe07pYRaKZU8LKRQ YmnOfBv6XbjFF15MUCwZoxOz79giQU+nR8zRmiWzbmMR/FrqiAHtJuGX0wyqu15SncrT pDYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=KUznzj3v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id ob3-20020a17090b390300b00280a4a15cddsi2718509pjb.94.2023.11.09.13.38.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 13:38:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@sipsolutions.net header.s=mail header.b=KUznzj3v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 D523D8392E1D; Thu, 9 Nov 2023 13:38:39 -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 S234801AbjKIVic (ORCPT + 30 others); Thu, 9 Nov 2023 16:38:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345262AbjKIViY (ORCPT ); Thu, 9 Nov 2023 16:38:24 -0500 Received: from sipsolutions.net (s3.sipsolutions.net [IPv6:2a01:4f8:242:246e::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC8103C30; Thu, 9 Nov 2023 13:38:21 -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: References:In-Reply-To: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; bh=/3x7Uzb2xVsM+qPa/s6ahfiXYwxF7KgqrBu++ys4yuQ=; t=1699565902; x=1700775502; b=KUznzj3vFood3m4qndr+0Z80B26ACVzpDau6qD8FaPG4vyw azKhDI/pUxt5lc/NGA8irE4Cdnrw/ZAKAYuDRtt/4LNJgBMdL7oUzJR63AubskA9uGPAROo6IiMtZ 2Pn2Ojmw4PDyF3Tk4HntXkax+8MUv/rlavzyBtENs1n4AIWe8VZbXjjnRJahgDfpzayaKJDoM3paN pI6zRcnH6kpYDuQpjETJP7CHdl28yUMPzH8wuWhX/5irb0MW2ZmzYLoahgox/UARVARH8j7OEbTxq JESXEl0lQkQPNbO9TvcPAQbyoVXoKbr9ioprsH+ibNFakONbac0H8PbJtr+U5W5Q==; Received: by sipsolutions.net with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97) (envelope-from ) id 1r1CjC-00000001znF-3teS; Thu, 09 Nov 2023 22:38:19 +0100 From: Johannes Berg To: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, Greg Kroah-Hartman , "Rafael J. Wysocki" , Nicolai Stange , Ben Greear , Johannes Berg Subject: [RFC PATCH 3/6] debugfs: add API to allow debugfs operations cancellation Date: Thu, 9 Nov 2023 22:22:55 +0100 Message-ID: <20231109222251.e6ade2190ef4.If54cd017d5734024e7bee5e4a237e17244050480@changeid> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231109212251.213873-7-johannes@sipsolutions.net> References: <20231109212251.213873-7-johannes@sipsolutions.net> 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 13:38:39 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782124035395618556 X-GMAIL-MSGID: 1782124035395618556 From: Johannes Berg In some cases there might be longer-running hardware accesses in debugfs files, or attempts to acquire locks, and we want to still be able to quickly remove the files. Introduce a cancellations API to use inside the debugfs handler functions to be able to cancel such operations on a per-file basis. Signed-off-by: Johannes Berg --- fs/debugfs/file.c | 82 +++++++++++++++++++++++++++++++++++++++++ fs/debugfs/inode.c | 23 +++++++++++- fs/debugfs/internal.h | 5 +++ include/linux/debugfs.h | 19 ++++++++++ 4 files changed, 128 insertions(+), 1 deletion(-) diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c index e499d38b1077..f6993c068322 100644 --- a/fs/debugfs/file.c +++ b/fs/debugfs/file.c @@ -109,6 +109,8 @@ int debugfs_file_get(struct dentry *dentry) lockdep_init_map(&fsd->lockdep_map, fsd->lock_name ?: "debugfs", &fsd->key, 0); #endif + INIT_LIST_HEAD(&fsd->cancellations); + spin_lock_init(&fsd->cancellations_lock); } /* @@ -151,6 +153,86 @@ void debugfs_file_put(struct dentry *dentry) } EXPORT_SYMBOL_GPL(debugfs_file_put); +/** + * debugfs_enter_cancellation - enter a debugfs cancellation + * @file: the file being accessed + * @cancellation: the cancellation object, the cancel callback + * inside of it must be initialized + * + * When a debugfs file is removed it needs to wait for all active + * operations to complete. However, the operation itself may need + * to wait for hardware or completion of some asynchronous process + * or similar. As such, it may need to be cancelled to avoid long + * waits or even deadlocks. + * + * This function can be used inside a debugfs handler that may + * need to be cancelled. As soon as this function is called, the + * cancellation's 'cancel' callback may be called, at which point + * the caller should proceed to call debugfs_leave_cancellation() + * and leave the debugfs handler function as soon as possible. + * Note that the 'cancel' callback is only ever called in the + * context of some kind of debugfs_remove(). + * + * This function must be paired with debugfs_leave_cancellation(). + */ +void debugfs_enter_cancellation(struct file *file, + struct debugfs_cancellation *cancellation) +{ + struct debugfs_fsdata *fsd; + struct dentry *dentry = F_DENTRY(file); + + INIT_LIST_HEAD(&cancellation->list); + + if (WARN_ON(!d_is_reg(dentry))) + return; + + if (WARN_ON(!cancellation->cancel)) + return; + + fsd = READ_ONCE(dentry->d_fsdata); + if (WARN_ON(!fsd || + ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) + return; + + spin_lock(&fsd->cancellations_lock); + list_add(&cancellation->list, &fsd->cancellations); + spin_unlock(&fsd->cancellations_lock); + + /* if we're already removing wake it up to cancel */ + if (d_unlinked(dentry)) + complete(&fsd->active_users_drained); +} +EXPORT_SYMBOL_GPL(debugfs_enter_cancellation); + +/** + * debugfs_leave_cancellation - leave cancellation section + * @file: the file being accessed + * @cancellation: the cancellation previously registered with + * debugfs_enter_cancellation() + * + * See the documentation of debugfs_enter_cancellation(). + */ +void debugfs_leave_cancellation(struct file *file, + struct debugfs_cancellation *cancellation) +{ + struct debugfs_fsdata *fsd; + struct dentry *dentry = F_DENTRY(file); + + if (WARN_ON(!d_is_reg(dentry))) + return; + + fsd = READ_ONCE(dentry->d_fsdata); + if (WARN_ON(!fsd || + ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) + return; + + spin_lock(&fsd->cancellations_lock); + if (!list_empty(&cancellation->list)) + list_del(&cancellation->list); + spin_unlock(&fsd->cancellations_lock); +} +EXPORT_SYMBOL_GPL(debugfs_leave_cancellation); + /* * Only permit access to world-readable files when the kernel is locked down. * We also need to exclude any file that has ways to write or alter it as root diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c index a4c77aafb77b..2cbcc49a8826 100644 --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -247,6 +247,7 @@ static void debugfs_release_dentry(struct dentry *dentry) lockdep_unregister_key(&fsd->key); kfree(fsd->lock_name); #endif + WARN_ON(!list_empty(&fsd->cancellations)); } kfree(fsd); @@ -754,8 +755,28 @@ static void __debugfs_file_removed(struct dentry *dentry) lock_map_acquire(&fsd->lockdep_map); lock_map_release(&fsd->lockdep_map); - if (!refcount_dec_and_test(&fsd->active_users)) + /* if we hit zero, just wait for all to finish */ + if (!refcount_dec_and_test(&fsd->active_users)) { wait_for_completion(&fsd->active_users_drained); + return; + } + + /* if we didn't hit zero, try to cancel any we can */ + while (refcount_read(&fsd->active_users)) { + struct debugfs_cancellation *c; + + spin_lock(&fsd->cancellations_lock); + while ((c = list_first_entry_or_null(&fsd->cancellations, + typeof(*c), list))) { + list_del_init(&c->list); + spin_unlock(&fsd->cancellations_lock); + c->cancel(dentry, c->cancel_data); + spin_lock(&fsd->cancellations_lock); + } + spin_unlock(&fsd->cancellations_lock); + + wait_for_completion(&fsd->active_users_drained); + } } static void remove_one(struct dentry *victim) diff --git a/fs/debugfs/internal.h b/fs/debugfs/internal.h index c7d61cfc97d2..5f279abd9351 100644 --- a/fs/debugfs/internal.h +++ b/fs/debugfs/internal.h @@ -8,6 +8,7 @@ #ifndef _DEBUGFS_INTERNAL_H_ #define _DEBUGFS_INTERNAL_H_ #include +#include struct file_operations; @@ -29,6 +30,10 @@ struct debugfs_fsdata { struct lock_class_key key; char *lock_name; #endif + + /* lock for the cancellations list */ + spinlock_t cancellations_lock; + struct list_head cancellations; }; }; }; diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h index ea2d919fd9c7..c9c65b132c0f 100644 --- a/include/linux/debugfs.h +++ b/include/linux/debugfs.h @@ -171,6 +171,25 @@ ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, size_t count, loff_t *ppos); +/** + * struct debugfs_cancellation - cancellation data + * @list: internal, for keeping track + * @cancel: callback to call + * @cancel_data: extra data for the callback to call + */ +struct debugfs_cancellation { + struct list_head list; + void (*cancel)(struct dentry *, void *); + void *cancel_data; +}; + +void __acquires(cancellation) +debugfs_enter_cancellation(struct file *file, + struct debugfs_cancellation *cancellation); +void __releases(cancellation) +debugfs_leave_cancellation(struct file *file, + struct debugfs_cancellation *cancellation); + #else #include