Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp2015031rwn; Fri, 9 Sep 2022 07:25:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR43nwZFwZ4Y/hW6pNTF1mXByYe8xlxBXBJhAUi8gyiGuWUx5AaAi/D+awtmzUhe+1zYgXbI X-Received: by 2002:a05:6a00:3492:b0:540:b30d:8396 with SMTP id cp18-20020a056a00349200b00540b30d8396mr6907037pfb.81.1662733505085; Fri, 09 Sep 2022 07:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662733505; cv=none; d=google.com; s=arc-20160816; b=QKS2wQjW0UPj+w8z4U0Oz4sz6ZhsfeApMIo2rLGFsF2C0JrYgI2Sr8STu2rCVRiqmQ 9AzZIWbIdQI0tg23HfI24zgwRRhPXsQmN5A+isoGVxAhP30BnvX8wFcksS9K47s/WdKj nRtY7SYxxQ2OJDvHHQVU2gH9xA25vIYz+8FHqOhVQ2bn4DUm0plYljQNeil3xY739Dq5 R5tz0lOt+S9Gk7+K++A0zMuRpShe3MOVL5OATa9xXKR7iTJeRcSUqs3dWZLp5qVlpDq8 IfPzsMv3n1rieHV1s9eXkFH3JRaIdOaEBHhuxbsTwZg9RdWceHFW6yqtFzfO2LyiRhqr +cwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=xtYXvAW96OQptFLFPBfzBp6CkQBMVtL5E6E3SMyjjMI=; b=SFKa/NQsDqeYc0EF7aCizeYXC52S9YTz7KzPwbtcUGxrJBzUxOZx+rmMuqw+NwEEdb UN9CoGEk8xVn0wTPUIUPeROCFN3POpYPDTG4VuPklBjLFaL6Ght921vOJ0oEc/aUNzih 3kSH8zHprKKISof9AGrEOkGdXo7YmsCTwtKOVZ8WlGyECofZXTxUtk6l91nmfQwBID3X s0m/5M13Ra85lgtl9TjLrLb8jvzp/TLbLEZ53PQshHwYZCF72TRV/vvD70+GSaC9khp9 QfG7j3Bz9+uzBsyzCfxC8hxTCD9v662oDvc8dJdl0k0OtqHCc0QLEAciBnKxiPR2WrBf HkIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="vdy/G2MT"; dkim=neutral (no key) header.i=@linutronix.de header.b=fTF8Pomu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q5-20020a170902dac500b00172fa1d47d4si716515plx.509.2022.09.09.07.24.52; Fri, 09 Sep 2022 07:25:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="vdy/G2MT"; dkim=neutral (no key) header.i=@linutronix.de header.b=fTF8Pomu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232294AbiIIOCU (ORCPT + 99 others); Fri, 9 Sep 2022 10:02:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232102AbiIIOAu (ORCPT ); Fri, 9 Sep 2022 10:00:50 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0F1C9F74A; Fri, 9 Sep 2022 07:00:37 -0700 (PDT) Date: Fri, 09 Sep 2022 14:00:34 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662732035; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xtYXvAW96OQptFLFPBfzBp6CkQBMVtL5E6E3SMyjjMI=; b=vdy/G2MTRekHKXwz6DugsS+FL6tH23QGhp05T2nNnERqgkwcbsqQ0sd7Ef/Z4Kfg/gQWi5 7J65ZcI0aMnWOik+yrz4o8QTEwt2KgG6XowJOsdxRgpF6uJGsSZ30j/ToMF4t9nYV2wa7E fll2iaix1JmsYWvhUYFdZSV/UJVto7UqnFeta275xvVW/G5tXnd+Cijb0jYScV8TIKqGAQ Nt60SXh4C67y2HMS/EwHc8KwvR+DhnPsVO6Y05/Qi3WQM/JwmiaTSyCzHB/KAXDOS79s3h BpauKSUImO8HDa9u8ixFGkKUg4X3xaqbPOGEdULRHrr3PJ18LR5O3xOzSbM9OQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662732035; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xtYXvAW96OQptFLFPBfzBp6CkQBMVtL5E6E3SMyjjMI=; b=fTF8PomuCYNpro0Qm8eh00U8Q+bsuVWYANf8wD9jtzXNB7a5UJZlK0gXDvhUeOUmIOClS8 YWfgs5cIdfMXK0Dw== From: "tip-bot2 for Tejun Heo" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/psi] kernfs: Simply by replacing kernfs_deref_open_node() with of_on() Cc: Imran Khan , Chengming Zhou , Tejun Heo , "Greg Kroah-Hartman" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220828050440.734579-2-tj@kernel.org> References: <20220828050440.734579-2-tj@kernel.org> MIME-Version: 1.0 Message-ID: <166273203440.401.9865519901772848692.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/psi branch of tip: Commit-ID: 3db48aca879db475844182a24d1760ee3d230627 Gitweb: https://git.kernel.org/tip/3db48aca879db475844182a24d1760ee3d230627 Author: Tejun Heo AuthorDate: Sat, 27 Aug 2022 19:04:32 -10:00 Committer: Greg Kroah-Hartman CommitterDate: Thu, 01 Sep 2022 18:08:43 +02:00 kernfs: Simply by replacing kernfs_deref_open_node() with of_on() kernfs_node->attr.open is an RCU pointer to kernfs_open_node. However, RCU dereference is currently only used in kernfs_notify(). Everywhere else, either we're holding the lock which protects it or know that the kernfs_open_node is pinned becaused we have a pointer to a kernfs_open_file which is hanging off of it. kernfs_deref_open_node() is used for the latter case - accessing kernfs_open_node from kernfs_open_file. The lifetime and visibility rules are simple and clear here. To someone who can access a kernfs_open_file, its kernfs_open_node is pinned and visible through of->kn->attr.open. Replace kernfs_deref_open_node() which simpler of_on(). The former takes both @kn and @of and RCU deref @kn->attr.open while sanity checking with @of. The latter takes @of and uses protected deref on of->kn->attr.open. As the return value can't be NULL, remove the error handling in the callers too. This shouldn't cause any functional changes. Cc: Imran Khan Tested-by: Chengming Zhou Reviewed-by: Chengming Zhou Signed-off-by: Tejun Heo Link: https://lore.kernel.org/r/20220828050440.734579-2-tj@kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/kernfs/file.c | 56 ++++++++++------------------------------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index b3ec343..32b16fe 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -57,31 +57,17 @@ static inline struct mutex *kernfs_open_file_mutex_lock(struct kernfs_node *kn) } /** - * kernfs_deref_open_node - Get kernfs_open_node corresponding to @kn. - * - * @of: associated kernfs_open_file instance. - * @kn: target kernfs_node. - * - * Fetch and return ->attr.open of @kn if @of->list is non empty. - * If @of->list is not empty we can safely assume that @of is on - * @kn->attr.open->files list and this guarantees that @kn->attr.open - * will not vanish i.e. dereferencing outside RCU read-side critical - * section is safe here. - * - * The caller needs to make sure that @of->list is not empty. + * of_on - Return the kernfs_open_node of the specified kernfs_open_file + * @of: taret kernfs_open_file */ -static struct kernfs_open_node * -kernfs_deref_open_node(struct kernfs_open_file *of, struct kernfs_node *kn) +static struct kernfs_open_node *of_on(struct kernfs_open_file *of) { - struct kernfs_open_node *on; - - on = rcu_dereference_check(kn->attr.open, !list_empty(&of->list)); - - return on; + return rcu_dereference_protected(of->kn->attr.open, + !list_empty(&of->list)); } /** - * kernfs_deref_open_node_protected - Get kernfs_open_node corresponding to @kn + * kernfs_deref_open_node_locked - Get kernfs_open_node corresponding to @kn * * @kn: target kernfs_node. * @@ -96,7 +82,7 @@ kernfs_deref_open_node(struct kernfs_open_file *of, struct kernfs_node *kn) * The caller needs to make sure that kernfs_open_file_mutex is held. */ static struct kernfs_open_node * -kernfs_deref_open_node_protected(struct kernfs_node *kn) +kernfs_deref_open_node_locked(struct kernfs_node *kn) { return rcu_dereference_protected(kn->attr.open, lockdep_is_held(kernfs_open_file_mutex_ptr(kn))); @@ -207,12 +193,8 @@ static void kernfs_seq_stop(struct seq_file *sf, void *v) static int kernfs_seq_show(struct seq_file *sf, void *v) { struct kernfs_open_file *of = sf->private; - struct kernfs_open_node *on = kernfs_deref_open_node(of, of->kn); - - if (!on) - return -EINVAL; - of->event = atomic_read(&on->event); + of->event = atomic_read(&of_on(of)->event); return of->kn->attr.ops->seq_show(sf, v); } @@ -235,7 +217,6 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) struct kernfs_open_file *of = kernfs_of(iocb->ki_filp); ssize_t len = min_t(size_t, iov_iter_count(iter), PAGE_SIZE); const struct kernfs_ops *ops; - struct kernfs_open_node *on; char *buf; buf = of->prealloc_buf; @@ -257,14 +238,7 @@ static ssize_t kernfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) goto out_free; } - on = kernfs_deref_open_node(of, of->kn); - if (!on) { - len = -EINVAL; - mutex_unlock(&of->mutex); - goto out_free; - } - - of->event = atomic_read(&on->event); + of->event = atomic_read(&of_on(of)->event); ops = kernfs_ops(of->kn); if (ops->read) @@ -584,7 +558,7 @@ static int kernfs_get_open_node(struct kernfs_node *kn, struct mutex *mutex = NULL; mutex = kernfs_open_file_mutex_lock(kn); - on = kernfs_deref_open_node_protected(kn); + on = kernfs_deref_open_node_locked(kn); if (on) { list_add_tail(&of->list, &on->files); @@ -629,7 +603,7 @@ static void kernfs_unlink_open_file(struct kernfs_node *kn, mutex = kernfs_open_file_mutex_lock(kn); - on = kernfs_deref_open_node_protected(kn); + on = kernfs_deref_open_node_locked(kn); if (!on) { mutex_unlock(mutex); return; @@ -839,7 +813,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) return; mutex = kernfs_open_file_mutex_lock(kn); - on = kernfs_deref_open_node_protected(kn); + on = kernfs_deref_open_node_locked(kn); if (!on) { mutex_unlock(mutex); return; @@ -874,11 +848,7 @@ void kernfs_drain_open_files(struct kernfs_node *kn) */ __poll_t kernfs_generic_poll(struct kernfs_open_file *of, poll_table *wait) { - struct kernfs_node *kn = kernfs_dentry_node(of->file->f_path.dentry); - struct kernfs_open_node *on = kernfs_deref_open_node(of, kn); - - if (!on) - return EPOLLERR; + struct kernfs_open_node *on = of_on(of); poll_wait(of->file, &on->poll, wait);