Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1313960rdb; Fri, 1 Dec 2023 12:36:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IHoj/NNIyA943Kceznn1efbYKXJulMPLi2RA+zDHd0YkWzdHiUI2RhxEfc03oTruQTMegEt X-Received: by 2002:a05:6358:8a6:b0:16b:c414:ae2 with SMTP id m38-20020a05635808a600b0016bc4140ae2mr130848rwj.8.1701462978594; Fri, 01 Dec 2023 12:36:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462978; cv=none; d=google.com; s=arc-20160816; b=m+sc2T5PAa8+xjWMS9cCKtAuQEN0xDUvHiuuV6tmyabUQiaZZOHkXmPakSlUXgmUAJ bdvwGTccXBG9CCfkCCubwmZjTayuxZKXxeXZWuh8SS2L1abWu9EKt9KoHWAFkHKR6oxV KbvCF1OLmt6ZLMI2L4FeqD93btQaIU0G55d3d2YIcEDz6R2Ga+xW97ba3xMdHycm4syx l8CfxtbEnhhKnpF1oM750dLnyOb85FigMDV3rh+ToWQ75SacnNFsGZuVzVh/0FrQQ8Es lYu8zr8OwxWOTNNdji+GhTFLDAf177cV9Vp2n361L8G8XrYjOBSE/BO2vu+YiqHeNcyg F/Bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i3vFuRTwQHBTKl7jarH+dzkS/sxU8PltPN/jY3lVGdc=; fh=4dHeqYK0KsKuK2/CBHfyswcAhow44JbSy5AgKi54e+I=; b=lnTEFul6GhPPAOHYsXkAjm0tLqV/vplijcxqVOtOnTVL46WYp1xp3dpkRq+8Wyuzy0 z7EeU0u6vg/Z4e4gTLRIal8hJKNS2Dd4yUZ0d9/7kO8QhTiaBS/QwK54jAMR/Jv2yg2V 3Iaa5+0NH/aeJEfjhmYyMZ7q/BFcNRoLJQ5GppT/gPdCLfBjOaLp/CcJT1Ad68+N7ILJ tcPtj/ht3qmsODeIM/WYHbd2/LuE7BFI+jrM29eIwN5vIybEDa7OmXcw4zTptII1UZ3a 1E1qQ+CgNDG4vbF3fLFSHuiJETuBkLNLTuDMjZALR+WtDXuqVnYRsWXtp6yFSlzVVqfu iwbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nuIUSC2K; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id f2-20020a056a00228200b006910a45a234si3875001pfe.202.2023.12.01.12.36.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:36:18 -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=@kernel.org header.s=k20201202 header.b=nuIUSC2K; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 806B281DC61C; Fri, 1 Dec 2023 12:35:58 -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 S1379608AbjLAUfp (ORCPT + 99 others); Fri, 1 Dec 2023 15:35:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379597AbjLAUfn (ORCPT ); Fri, 1 Dec 2023 15:35:43 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0452F10E4 for ; Fri, 1 Dec 2023 12:35:50 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 346A3C433C9; Fri, 1 Dec 2023 20:35:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701462949; bh=/TnZS0HuOiZtqdpeKk51Z32DjLXO4zdmRL4p1KDlMX0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nuIUSC2KcV7FM+30c0UfS9RFJL5UdaqyTsZOK2FqD//8T/b276NdoI8m+YF20LoNM 5wAuKZj+wKVShIfyEbd0MimM68YPFpDzxA10NbkGhZcV2ocC944n6CGWfkSL8q0iRd P0ntSTShaDcjjILKCAngWAUTu3Z8lG7OJJIIJAEGqQ+hbcWsVNKpTFQ1GmjZUJNGa9 6A3jugUlzCutWP3a2Wuuw1n8V7ee7oqzm41Jgxnh9KN6/EJKgcPnt31wN3mj2dmaLr i/EORjhhBN18rLz0bPJ7Fpvs8AlrLISUe7+BPUT/l/k+0HU9p/LC0AA3kX2yocc8m2 J7z/uvgG7yMDQ== Date: Fri, 1 Dec 2023 12:39:15 -0800 From: Bjorn Andersson To: Maria Yu Cc: linus.walleij@linaro.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@quicinc.com, linux-arm-msm@vger.kernel.org Subject: Re: [PATCH] pinctrl: Add lock to ensure the state atomization Message-ID: <6jlui5h7d2rs37sdvvwmii55mwhm5dzfo2m62hwt53mkx4z32a@aw5kcghe4bik> References: <20231201152931.31161-1-quic_aiquny@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231201152931.31161-1-quic_aiquny@quicinc.com> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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]); Fri, 01 Dec 2023 12:35:58 -0800 (PST) On Fri, Dec 01, 2023 at 11:29:31PM +0800, Maria Yu wrote: > Currently pinctrl_select_state is an export symbol and don't have > effective re-entrance protect design. And possible of pinctrl state > changed during pinctrl_commit_state handling. Add per pinctrl lock to > ensure the old state and new state transition atomization. > Move dev error print message right before old_state pinctrl_select_state > and out of lock protection to avoid console related driver call > pinctrl_select_state recursively. I'm uncertain about the validity of having client code call this api in a racy manner. I'm likely just missing something here... It would be nice if this scenario was described in a little bit more detail. The recursive error print sounds like a distinct problem of its own, that warrants being introduced in a patch of its own. But as with the other part, I'm not able to spot a code path in the upstream kernel where this hppens, so please properly describe the scenario where touching the console would result back in another pinctrl_select_state(). Thanks, Bjorn > > Fixes: 4198a9b57106 ("pinctrl: avoid reload of p state in list iteration") > Signed-off-by: Maria Yu > --- > drivers/pinctrl/core.c | 11 +++++++++-- > drivers/pinctrl/core.h | 2 ++ > 2 files changed, 11 insertions(+), 2 deletions(-) > > diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c > index f2977eb65522..a19c286bf82e 100644 > --- a/drivers/pinctrl/core.c > +++ b/drivers/pinctrl/core.c > @@ -1066,6 +1066,7 @@ static struct pinctrl *create_pinctrl(struct device *dev, > p->dev = dev; > INIT_LIST_HEAD(&p->states); > INIT_LIST_HEAD(&p->dt_maps); > + spin_lock_init(&p->lock); > > ret = pinctrl_dt_to_map(p, pctldev); > if (ret < 0) { > @@ -1262,9 +1263,12 @@ static void pinctrl_link_add(struct pinctrl_dev *pctldev, > static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state) > { > struct pinctrl_setting *setting, *setting2; > - struct pinctrl_state *old_state = READ_ONCE(p->state); > + struct pinctrl_state *old_state; > int ret; > + unsigned long flags; > > + spin_lock_irqsave(&p->lock, flags); > + old_state = p->state; > if (old_state) { > /* > * For each pinmux setting in the old state, forget SW's record > @@ -1329,11 +1333,11 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state) > } > > p->state = state; > + spin_unlock_irqrestore(&p->lock, flags); > > return 0; > > unapply_new_state: > - dev_err(p->dev, "Error applying setting, reverse things back\n"); > > list_for_each_entry(setting2, &state->settings, node) { > if (&setting2->node == &setting->node) > @@ -1349,6 +1353,9 @@ static int pinctrl_commit_state(struct pinctrl *p, struct pinctrl_state *state) > pinmux_disable_setting(setting2); > } > > + spin_unlock_irqrestore(&p->lock, flags); > + > + dev_err(p->dev, "Error applying setting, reverse things back\n"); > /* There's no infinite recursive loop here because p->state is NULL */ > if (old_state) > pinctrl_select_state(p, old_state); > diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h > index 530370443c19..86fc41393f7b 100644 > --- a/drivers/pinctrl/core.h > +++ b/drivers/pinctrl/core.h > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > #include > > #include > @@ -91,6 +92,7 @@ struct pinctrl { > struct pinctrl_state *state; > struct list_head dt_maps; > struct kref users; > + spinlock_t lock; > }; > > /** > > base-commit: 994d5c58e50e91bb02c7be4a91d5186292a895c8 > -- > 2.17.1 > >