From patchwork Tue Nov 14 15:26:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 164991 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6358:a59:b0:164:83eb:24d7 with SMTP id 25csp1999849rwb; Tue, 14 Nov 2023 07:27:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFleDwp+EndpmAnL5fgZR5e2Nsw/LFEB/dm2gODmTk3ohJ+oKi/G3V/4HeAHUS13V9QCLxS X-Received: by 2002:a17:90b:17cb:b0:27d:28ad:3086 with SMTP id me11-20020a17090b17cb00b0027d28ad3086mr7879456pjb.2.1699975653033; Tue, 14 Nov 2023 07:27:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699975653; cv=none; d=google.com; s=arc-20160816; b=NOYfXszjK8mm6eMvQ9mp1+x2tATn0f5cPYpsv7rJdBJmYhrB9/LsDQt91WcTMESYtx Rb7n8dZG4VXTEEGrc1BoELGQU6QslP7RYt0N3B4+8DO/gioMvNq2lLSZs0HoFyH0t0cK Q47PcN/GCMWhTWjjSCupA28wZ8pfhMO1QKXk0UEYA8xELpOVCCBbyPM9hrRzm9yZgSx3 WYwVjypV81jlQoloeDSfDv3beNIRAsaGJB/SaQ6/EeQ+iehVhVrt6stDr6IaJ1UhiZtE ZUudrDvPAXoXLU32z/s9+aDgZB9FqYwBf7el4Fd7TLdQhlRomUd1iyeOJLEvZLXdtSab rLOA== 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=PAfd5PzRAlov3r0lVkTLqmgUGDb3Mw2H4g0bqbIXqTI=; fh=inKJ5kKAsUU4c8hT0Ljb/CSuao2S4RjDzYxLdUfmU1Y=; b=DVK1kWI8L0/hlpXSrF7C5HnmTz1KzbXawPwKmjsuATGu3K16qcm43m828bPess1rrq 9Pjd3EqEJSTunTOWZHTzn6jwgeBtmB0u/NAmd44UWocWg0Nln9qBsJRVDoArTGqmNzSX 9gZ6/9qmEGHDcfPS4WXMoUwBGSF2STIJRQQ1+PsFX49IEomu4WlVuxZ8NjfvtEQXw/Ss 1HqhtX9e7X//UdqGVfnujqlyspw+VlqjNuswhr5/40PVJyFjBAOkwCn0Rw0hNH10ZDTS +ym9IFc75I3HimF9X1HtJE87PewYKF5D/W+ChSYhNvTfIP4rZGQwDm7h1cw7gtMZkdlD 14dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=AeY77Kka; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id mp15-20020a17090b190f00b00280414d7a6bsi7940174pjb.190.2023.11.14.07.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Nov 2023 07:27:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=AeY77Kka; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id BEF5880310DF; Tue, 14 Nov 2023 07:27:30 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233558AbjKNP1O (ORCPT + 29 others); Tue, 14 Nov 2023 10:27:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232193AbjKNP1N (ORCPT ); Tue, 14 Nov 2023 10:27:13 -0500 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 562CF11D; Tue, 14 Nov 2023 07:27:08 -0800 (PST) Received: by mail.gandi.net (Postfix) with ESMTPA id 27B7F40003; Tue, 14 Nov 2023 15:27:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1699975626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PAfd5PzRAlov3r0lVkTLqmgUGDb3Mw2H4g0bqbIXqTI=; b=AeY77KkaNxILwfvBQ+kEBEvz0KK9Z5DDt3bQrhdRFzAnavgsj3ZiNXEmtDL2jNFyVn0Uji ZIlF5WNhfAy5kbNEYTp1CAZfDXy0llqUsS2EF34Z0+zsvHuEO5sY+PWXLXCfvHHgNAqUW3 l8+U14CPDchb2jWrukR5pJZI8P9B/ygJOknh5ltEE/XLiiDstUzpoDImXbend1nlc/VnCg fET4sYdKUHjpo9peHV3jtthzI2SlJqryHZMsUNFzpFaV7PZKeUZt4BIJUS4X0bQ5/bpeLR 9dwM07NnJH9I0KGROhUaivO4ZSEMBjv8Jzcx4j9IT4cHI5Zuqq4c6AmZK7ahHA== From: Herve Codina To: Saravana Kannan , Petr Mladek , Steven Rostedt , Andy Shevchenko , Rasmus Villemoes , Sergey Senozhatsky , Sakari Ailus , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v3 1/1] lib/vsprintf: Fix %pfwf when current node refcount == 0 Date: Tue, 14 Nov 2023 16:26:55 +0100 Message-ID: <20231114152655.409331-1-herve.codina@bootlin.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email 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 (lipwig.vger.email [0.0.0.0]); Tue, 14 Nov 2023 07:27:30 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782550509176312298 X-GMAIL-MSGID: 1782553670264917879 A refcount issue can appeared in __fwnode_link_del() due to the pr_debug() call: WARNING: CPU: 0 PID: 901 at lib/refcount.c:25 refcount_warn_saturate+0xe5/0x110 Call Trace: ... of_node_get+0x1e/0x30 of_fwnode_get+0x28/0x40 fwnode_full_name_string+0x34/0x90 fwnode_string+0xdb/0x140 ... vsnprintf+0x17b/0x630 ... __fwnode_link_del+0x25/0xa0 fwnode_links_purge+0x39/0xb0 of_node_release+0xd9/0x180 ... Indeed, an fwnode (of_node) is being destroyed and so, of_node_release() is called because the of_node refcount reached 0. From of_node_release() several function calls are done and lead to a pr_debug() calls with %pfwf to print the fwnode full name. The issue is not present if we change %pfwf to %pfwP. To print the full name, %pfwf iterates over the current node and its parents and obtain/drop a reference to all nodes involved. In order to allow to print the full name (%pfwf) of a node while it is being destroyed, do not obtain/drop a reference to this current node. Fixes: a92eb7621b9f ("lib/vsprintf: Make use of fwnode API to obtain node names and separators") Cc: stable@vger.kernel.org Signed-off-by: Herve Codina Reviewed-by: Sakari Ailus Reviewed-by: Andy Shevchenko --- Changes v2 -> v3 - Fix typo in comment ("ie parents node" -> "i.e. parent nodes") - Add 'Reviewed-by: Sakari Ailus ' - Add 'Reviewed-by: Andy Shevchenko ' Changes v1 -> v2 - Avoid handling current node out of the loop. Instead obtain/drop references in the loop based on the depth value. - Remove some of the backtrace lines in the commit log. lib/vsprintf.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index afb88b24fa74..2aa408441cd3 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -2110,15 +2110,20 @@ char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, /* Loop starting from the root node to the current node. */ for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { - struct fwnode_handle *__fwnode = - fwnode_get_nth_parent(fwnode, depth); + /* + * Only get a reference for other nodes (i.e. parent nodes). + * fwnode refcount may be 0 here. + */ + struct fwnode_handle *__fwnode = depth ? + fwnode_get_nth_parent(fwnode, depth) : fwnode; buf = string(buf, end, fwnode_get_name_prefix(__fwnode), default_str_spec); buf = string(buf, end, fwnode_get_name(__fwnode), default_str_spec); - fwnode_handle_put(__fwnode); + if (depth) + fwnode_handle_put(__fwnode); } return buf;