From patchwork Tue Feb 28 10:15:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= X-Patchwork-Id: 62394 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2932530wrd; Tue, 28 Feb 2023 02:27:39 -0800 (PST) X-Google-Smtp-Source: AK7set8YyNLKgg31hjPHK9XFa2HOWecPbYoCPSsLEEpdrRmBj30D8BZnrl74gdR7haXeILEHfVaS X-Received: by 2002:a05:6402:416:b0:4ac:b95b:f1d7 with SMTP id q22-20020a056402041600b004acb95bf1d7mr3207457edv.15.1677580058844; Tue, 28 Feb 2023 02:27:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677580058; cv=none; d=google.com; s=arc-20160816; b=awi4EZUZBbROM/5C5/sGdRFlY1D5ray6NK95t7au7nJZNn8a0S19ADU8pITPDF8QkW RQWGffQSATvvrO6b0apikFWe0Jvosbuuii5OCZ6VJcAdytHgnxpWhUAzmEuxsXLYbuAG XAokZqI5Lb9hWBsXxLza10Q4E4/viwtPydOrxFIvxXLhiliFbM/O0uRlDmbCz7YtEoBk GUG/WKlqN6znzezXWIsDXSSKtN4c6ipx10a7yi/zkH7GRG6bJZhIzN2J2r2OkoSTvU5Y 92HygSOGxHWSzOJrHXWnXwjSL+lRMZLWG3zZyMpW+YY9h8QIlV7o375Q0mRvEGSE1Lm4 7m6A== 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; bh=dRjsfD+fql4QDvodPdwKkUsSJGHLKJFMN3ykUfwKMmQ=; b=FpViqQoHr0rSVQ2QLOtdpeqURX/CG4w+8RgeBaN3PZMgPHapBlzIx2o58js6gdRQz7 ydMxpu+hQACfq2ajcQLACs6Yr/iVcMwON0rDRa/4WfIH6uJEcvFCdR7pbWaEnNXmvXO/ WI8jMco9HhD054PQW0qT/o4xn4QPK7M5Yoqxko/dVx0vuanoDe7a2aA9osn6E+NJDw2o oMQA82LOR6LKbpB2XCLnUHBxLiPO8UDmQVPGXTsZDA09A72WxlxSamjbJW9RN2WsDvHU zjcd+Fl4+2XJuzs4U0mbEiMEhsIS8y3PAo8vFzXfk2FT4zd3gGw2YjZXX8H428xRQBOo IKSg== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c24-20020aa7c998000000b004af67f48de3si10909955edt.304.2023.02.28.02.27.15; Tue, 28 Feb 2023 02:27:38 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229809AbjB1KQM (ORCPT + 99 others); Tue, 28 Feb 2023 05:16:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229686AbjB1KQL (ORCPT ); Tue, 28 Feb 2023 05:16:11 -0500 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DCD2C19681 for ; Tue, 28 Feb 2023 02:16:06 -0800 (PST) Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pWx1c-00047a-4O; Tue, 28 Feb 2023 11:16:00 +0100 Received: from [2a0a:edc0:0:900:1d::77] (helo=ptz.office.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtp (Exim 4.94.2) (envelope-from ) id 1pWx1a-000qPj-Ug; Tue, 28 Feb 2023 11:15:58 +0100 Received: from ukl by ptz.office.stw.pengutronix.de with local (Exim 4.94.2) (envelope-from ) id 1pWx1a-000zTY-9Q; Tue, 28 Feb 2023 11:15:58 +0100 Date: Tue, 28 Feb 2023 11:15:58 +0100 From: Uwe =?utf-8?q?Kleine-K=C3=B6nig?= To: Munehisa Kamata Cc: thierry.reding@gmail.com, tobetter@gmail.com, linux-pwm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, kernel@pengutronix.de Subject: [PATCH] pwm: Zero-initialize the pwm_state passed to driver's .get_state() Message-ID: <20230228101558.b4dosk54jojfqkgi@pengutronix.de> References: <20230226013722.1802842-1-kamatam@amazon.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230226013722.1802842-1-kamatam@amazon.com> X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759070187568270324?= X-GMAIL-MSGID: =?utf-8?q?1759070187568270324?= This is just to ensure that .usage_power is properly initialized and doesn't contain random stack data. The other members of struct pwm_state should get a value assigned in a successful call to .get_state(). So in the absence of bugs in driver implementations, this is only a safe-guard and no fix. Reported-by: Munehisa Kamata Signed-off-by: Uwe Kleine-König --- drivers/pwm/core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) Hello, On Sat, Feb 25, 2023 at 05:37:21PM -0800, Munehisa Kamata wrote: > Zero-initialize the on-stack structure to avoid unexpected behaviors. Some > drivers may not set or initialize all the values in pwm_state through their > .get_state() callback and therefore some random values may remain there and > be set into pwm->state eventually. > > This actually caused regression on ODROID-N2+ as reported in [1]; kernel > fails to boot due to random panic or hang-up. > > [1] https://forum.odroid.com/viewtopic.php?f=177&t=46360 > > Fixes: c73a3107624d ("pwm: Handle .get_state() failures") > Cc: stable@vger.kernel.org # 6.2 > Signed-off-by: Munehisa Kamata My patch is essentially the same as Munehisa's, just written a bit differently (to maybe make it easier for the compiler to optimize it?) and with an explaining comment. The actual motivation is different so the commit log is considerably different, too. I was unsure how to honor Munehisa's effort, I went with a "Reported-by". Please tell me if you want this to be different. Best regards Uwe diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c index e01147f66e15..533ef5bd3add 100644 --- a/drivers/pwm/core.c +++ b/drivers/pwm/core.c @@ -115,7 +115,14 @@ static int pwm_device_request(struct pwm_device *pwm, const char *label) } if (pwm->chip->ops->get_state) { - struct pwm_state state; + /* + * Zero-initialize state because most drivers are unaware of + * .usage_power. The other members of state are supposed to be + * set by lowlevel drivers. We still initialize the whole + * structure for simplicity even though this might paper over + * faulty implementations of .get_state(). + */ + struct pwm_state state = { 0, }; err = pwm->chip->ops->get_state(pwm->chip, pwm, &state); trace_pwm_get(pwm, &state, err);