Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2158058iof; Tue, 7 Jun 2022 21:41:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxL0ae3Q8s0dV+cj+yOzhkDjIgzyK/OTYmmRWDMJIA8wX+GZl/DFUR6aaNJ7r1sp+YH7x8u X-Received: by 2002:a17:903:11c9:b0:154:be2d:eb9 with SMTP id q9-20020a17090311c900b00154be2d0eb9mr31929044plh.91.1654663304974; Tue, 07 Jun 2022 21:41:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654663304; cv=none; d=google.com; s=arc-20160816; b=isGEnk/L3rrN/BciD1KLr1KZ+ZS+1R9kwq9JT02QPyuYHzVhQX6Q8d5IiPzjdHhvA/ D9AfxZ9xrdgHe3YAL2ZUiMmdy00U6T1FLiI93lNQsRfRn9RtnYn1q348d1KpEVb91d+9 MrvR/g/mtklonH9t7REQnnmwJp0nJD8xh6Jxkvw+eMYVs9worD9OFx1gsBL3ER7oF3As 9rKIyoNGz66MYYepoAfZnQTZrTwdM1gCaAWlGPCywAmwQA88aNAPJurVkQWr51N3UQ2s fucfrmqRyRSahnxKfupTgdQ6t1KnRB1glKDkRhGKpBfi8cPbo7GFPYJjfftEanbeOA4Y YShA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ifDxMatAlmHG6+QO/cX43ydwB+XCFucrKlR7RLoJRyk=; b=QabcHNxHPLlRcLxAxvjNsJy0tgCJyYd6wCIMH9mqS1vmqbSXAkUGLzdGHbiDAriqPi 8S8PSeyAzYqhxqrESpxjhI4pvtoVbLvveod8ahy+VZ5XLSeIrtoyf4t3neHHPvEnPwbB w4mqH9rSCPFmPrH0hAZgRxtcF9wa/suA1sv5q+9LrTPlXGVzI88QIfwdn2YzSZQKe2L/ n13FoVHvseGCVSyPo8ISDFWu2rcxH3QbdwD28WIXOr+tShAKUIRAuHSzGzIeuUMd26UF MB2Wzg6W9pd4KT8yNbZGJlmK9j7dur+jLuC/xkHQWpCc7MRbBxwxfXzHvu4yoU1JH/+C bXbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YwqKVRgD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id mu4-20020a17090b388400b001e88e0c8910si9891649pjb.100.2022.06.07.21.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:41:44 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YwqKVRgD; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1F215447F1B; Tue, 7 Jun 2022 21:12:32 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378949AbiFGV6k (ORCPT + 99 others); Tue, 7 Jun 2022 17:58:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379455AbiFGVC3 (ORCPT ); Tue, 7 Jun 2022 17:02:29 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 380A5121CDF for ; Tue, 7 Jun 2022 11:48:09 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id v19so24141287edd.4 for ; Tue, 07 Jun 2022 11:48:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ifDxMatAlmHG6+QO/cX43ydwB+XCFucrKlR7RLoJRyk=; b=YwqKVRgDSYc7lhb5EOoK6KWe1n5ItyjnVVXQbxtboJkPu8qknjzCCMP5zI8o6wtucr O8oWZgd7TU+bt1TCM4PqWEfgUdm+mh5jPmqqhHyolyQOZ/aXsknjWebnd2MAVlcITrNC GgdK6YYEsFHFxNGQQjzoPMTWGYcAukmaKm82stLPel+yei+sLZ6CmwoT2uIFPwbZJGQm NBzOlIX5gZlg7Ctb8HBAzEh8w+NiH2SS5qle1ugL6jAsUBMX1mdyt2Owjt2s9wNB3t39 izM6HDsttK01IyYzDfonGQ/Zjea48Twd5ku42IdNO/dbD1axrT7c7/jhsyVCmcDMPdmk BPbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ifDxMatAlmHG6+QO/cX43ydwB+XCFucrKlR7RLoJRyk=; b=MzOXTMBjGjFqC3CyAuEElN83pbwHxIa8YItvsZArfdXGLZV7Qyx7Wcl+Kqj/SWIEkT 1FDgvIEsr8aFJVRhCF+ihIWBJQFAvo22TS07DaDNPtxOzcg6ekpzizpEsJc+V/Ik7+hq qVsRvngoE7lHbB5CbLdrKKafQc61uMIsuczvnT+scXD88bZo+PMe4gNNpnmK7LsIuQED LiL65WAA6j8RS6UVcCwT5SGZp4SlK7wepZgEpl/WkMtdk3okIrnBO9HW981s/lquQDJx IXr0VGBPYvzpiCbYIj9n+j38cGpwL8WmxmkbECSuqp6JzRqIHPKlJSel9psUzAtT/h0Z OWLg== X-Gm-Message-State: AOAM532L8YifjDGB3Afn4QSBjkWr13W4LDcFbHKb99tjsROg2Hv7Rkcx k3alBw6Xw0GcGAiKsTJlyA6eEMN+9QXNxRhjbtGnBw== X-Received: by 2002:a05:6402:3688:b0:42d:d3ba:4725 with SMTP id ej8-20020a056402368800b0042dd3ba4725mr35557327edb.212.1654627687938; Tue, 07 Jun 2022 11:48:07 -0700 (PDT) MIME-Version: 1.0 References: <20220607145639.2362750-1-tzungbi@kernel.org> <20220607145639.2362750-9-tzungbi@kernel.org> In-Reply-To: <20220607145639.2362750-9-tzungbi@kernel.org> From: Guenter Roeck Date: Tue, 7 Jun 2022 11:47:56 -0700 Message-ID: Subject: Re: [PATCH v2 08/15] platform/chrome: cros_ec_proto: handle empty payload in getting proto info To: Tzung-Bi Shih Cc: bleung@chromium.org, groeck@chromium.org, chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 On Tue, Jun 7, 2022 at 7:57 AM Tzung-Bi Shih wrote: > > cros_ec_get_proto_info() expects to receive > sizeof(struct ec_response_get_protocol_info) from send_command(). The > payload is valid only if the return value is positive. > > Add Kunit tests for returning 0 in send_command() and handle the case in > cros_ec_get_proto_info(). > That should be two separate patches. > Signed-off-by: Tzung-Bi Shih > --- > No v1. New in the series. > > drivers/platform/chrome/cros_ec_proto.c | 5 + > drivers/platform/chrome/cros_ec_proto_test.c | 132 +++++++++++++++++++ > 2 files changed, 137 insertions(+) > > diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c > index 893b76703da6..6f5be9e5ede4 100644 > --- a/drivers/platform/chrome/cros_ec_proto.c > +++ b/drivers/platform/chrome/cros_ec_proto.c > @@ -314,6 +314,11 @@ static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx) > goto exit; > } > > + if (ret == 0) { > + ret = -EPROTO; > + goto exit; > + } > + I think you can move that into the if() statement above (which already checks for ret >=0), making it a special case of that situation. Thanks, Guenter > info = (struct ec_response_get_protocol_info *)msg->data; > > switch (devidx) { > diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c > index 14a4441a39fc..473714964cf2 100644 > --- a/drivers/platform/chrome/cros_ec_proto_test.c > +++ b/drivers/platform/chrome/cros_ec_proto_test.c > @@ -406,6 +406,71 @@ static void cros_ec_proto_test_query_all_no_pd_return_error(struct kunit *test) > } > } > > +static void cros_ec_proto_test_query_all_no_pd_return0(struct kunit *test) > +{ > + struct cros_ec_proto_test_priv *priv = test->priv; > + struct cros_ec_device *ec_dev = &priv->ec_dev; > + struct ec_xfer_mock *mock; > + int ret; > + > + /* Set some garbage bytes. */ > + ec_dev->max_passthru = 0xbf; > + > + /* For cros_ec_get_proto_info() without passthru. */ > + { > + struct ec_response_get_protocol_info *data; > + > + mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data)); > + KUNIT_ASSERT_PTR_NE(test, mock, NULL); > + > + /* > + * Although it doesn't check the value, provides valid sizes so that > + * cros_ec_query_all() allocates din and dout correctly. > + */ > + data = (struct ec_response_get_protocol_info *)mock->o_data; > + data->max_request_packet_size = 0xbe; > + data->max_response_packet_size = 0xef; > + } > + > + /* For cros_ec_get_proto_info() with passthru. */ > + { > + mock = cros_kunit_ec_xfer_mock_add(test, 0); > + KUNIT_ASSERT_PTR_NE(test, mock, NULL); > + } > + > + cros_ec_proto_test_query_all_pretest(test); > + ret = cros_ec_query_all(ec_dev); > + KUNIT_EXPECT_EQ(test, ret, 0); > + > + /* For cros_ec_get_proto_info() without passthru. */ > + { > + mock = cros_kunit_ec_xfer_mock_next(); > + KUNIT_EXPECT_PTR_NE(test, mock, NULL); > + > + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); > + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO); > + KUNIT_EXPECT_EQ(test, mock->msg.insize, > + sizeof(struct ec_response_get_protocol_info)); > + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); > + } > + > + /* For cros_ec_get_proto_info() with passthru. */ > + { > + mock = cros_kunit_ec_xfer_mock_next(); > + KUNIT_EXPECT_PTR_NE(test, mock, NULL); > + > + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); > + KUNIT_EXPECT_EQ(test, mock->msg.command, > + EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX) | > + EC_CMD_GET_PROTOCOL_INFO); > + KUNIT_EXPECT_EQ(test, mock->msg.insize, > + sizeof(struct ec_response_get_protocol_info)); > + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); > + > + KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0); > + } > +} > + > static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct kunit *test) > { > struct cros_ec_proto_test_priv *priv = test->priv; > @@ -471,6 +536,71 @@ static void cros_ec_proto_test_query_all_legacy_normal_v3_return_error(struct ku > } > } > > +static void cros_ec_proto_test_query_all_legacy_normal_v3_return0(struct kunit *test) > +{ > + struct cros_ec_proto_test_priv *priv = test->priv; > + struct cros_ec_device *ec_dev = &priv->ec_dev; > + struct ec_xfer_mock *mock; > + int ret; > + > + /* For cros_ec_get_proto_info() without passthru. */ > + { > + mock = cros_kunit_ec_xfer_mock_add(test, 0); > + KUNIT_ASSERT_PTR_NE(test, mock, NULL); > + } > + > + /* For cros_ec_host_command_proto_query_v2(). */ > + { > + struct ec_response_hello *data; > + > + mock = cros_kunit_ec_xfer_mock_add(test, sizeof(*data)); > + KUNIT_ASSERT_PTR_NE(test, mock, NULL); > + > + data = (struct ec_response_hello *)mock->o_data; > + data->out_data = 0xa1b2c3d4; > + } > + > + cros_ec_proto_test_query_all_pretest(test); > + ret = cros_ec_query_all(ec_dev); > + KUNIT_EXPECT_EQ(test, ret, 0); > + > + /* For cros_ec_get_proto_info() without passthru. */ > + { > + mock = cros_kunit_ec_xfer_mock_next(); > + KUNIT_EXPECT_PTR_NE(test, mock, NULL); > + > + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); > + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO); > + KUNIT_EXPECT_EQ(test, mock->msg.insize, > + sizeof(struct ec_response_get_protocol_info)); > + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); > + } > + > + /* For cros_ec_host_command_proto_query_v2(). */ > + { > + struct ec_params_hello *data; > + > + mock = cros_kunit_ec_xfer_mock_next(); > + KUNIT_EXPECT_PTR_NE(test, mock, NULL); > + > + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); > + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO); > + KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello)); > + KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(*data)); > + > + data = (struct ec_params_hello *)mock->i_data; > + KUNIT_EXPECT_EQ(test, data->in_data, 0xa0b0c0d0); > + > + KUNIT_EXPECT_EQ(test, ec_dev->proto_version, 2); > + KUNIT_EXPECT_EQ(test, ec_dev->max_request, EC_PROTO2_MAX_PARAM_SIZE); > + KUNIT_EXPECT_EQ(test, ec_dev->max_response, EC_PROTO2_MAX_PARAM_SIZE); > + KUNIT_EXPECT_EQ(test, ec_dev->max_passthru, 0); > + KUNIT_EXPECT_PTR_EQ(test, ec_dev->pkt_xfer, NULL); > + KUNIT_EXPECT_EQ(test, ec_dev->din_size, EC_PROTO2_MSG_BYTES); > + KUNIT_EXPECT_EQ(test, ec_dev->dout_size, EC_PROTO2_MSG_BYTES); > + } > +} > + > static void cros_ec_proto_test_query_all_legacy_xfer_error(struct kunit *test) > { > struct cros_ec_proto_test_priv *priv = test->priv; > @@ -998,7 +1128,9 @@ static struct kunit_case cros_ec_proto_test_cases[] = { > KUNIT_CASE(cros_ec_proto_test_check_result), > KUNIT_CASE(cros_ec_proto_test_query_all_normal), > KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return_error), > + KUNIT_CASE(cros_ec_proto_test_query_all_no_pd_return0), > KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return_error), > + KUNIT_CASE(cros_ec_proto_test_query_all_legacy_normal_v3_return0), > KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error), > KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error), > KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error), > -- > 2.36.1.255.ge46751e96f-goog >