Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2417537iof; Wed, 8 Jun 2022 04:41:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzReiTMxU/W/Li04cmhP43FpSAMmLbzrrE7YmgNR41QygQ9SVWeFvEBr02h3+aXlTSdRZoB X-Received: by 2002:a63:b51:0:b0:3fc:cd1d:884 with SMTP id a17-20020a630b51000000b003fccd1d0884mr29460668pgl.98.1654688484204; Wed, 08 Jun 2022 04:41:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654688484; cv=none; d=google.com; s=arc-20160816; b=kZXwVusnZrz7FA3xG4k0xHEqZ39asBlwHZOtx8j+uoWSDZQK+2jLnAV4CJLbe3IRIK ovzzlKgjIAZcUephYuWYbJzGrRSnK3g0pvEd90oH6mrwVEjHPag79sJY/hWGXBB7Fkre yOG4dfM+jpADZa03nIasJFTenXnFFlO00aW3vJC5apaIRRd5A9gC9lX0FDQfLOiTYV3x b9fzWI23GVFJ2Pgw/T9zt+dEehG7StG0eS+bS0Fmezd3decvLKBwqMpO6Egtx5oypwJ2 PvsEqQ8NYUkL4qVFBRpbaVQbnvfC7K4mzZoEV4nLjClc2fXRAUmH+OT2xU6xeIz0bZP2 u/Hw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dV411FdVZJ0eRxq5KOSC2eBda0K9//f4D1d9RRKzdjw=; b=RXrVh5u+/zGyNbqpm8oFwwKFsQ6agARIFg/9o4wF7CSMf2H9ghePSHES7f7pYyNPra 5Q0uwk2yZjdQgbd9V2vFHAWZa2U6xgMCZ/QWLxWcCCXNJcy7dUyrvhTko4YsgfVuEbcN i7TOGywlCDxuDcwtogoldh+mNgdCMi9pDLj/DJ+TtVJYW7ZheF5U7fw0U67mCc+H/xmI Cv2Mb7dOWmRE27AVVU6heuNiZMcmfwtEEsykoQvpWaM4h//hQuRE9IF6BMpdnIxWVWwg wahwDPpidriag6BxCBfTWV8UkXLO00ZY2K27ZbvV1uPPVjeoH9sMxi9BExR/UIgdZWXd iMgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GsfLoyFm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id g2-20020a170902e38200b00163d77ce6acsi24038573ple.615.2022.06.08.04.41.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 04:41:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GsfLoyFm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5ED4D22DF8F; Wed, 8 Jun 2022 04:09:41 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237851AbiFHLJD (ORCPT + 99 others); Wed, 8 Jun 2022 07:09:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237865AbiFHLIQ (ORCPT ); Wed, 8 Jun 2022 07:08:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0127208B11 for ; Wed, 8 Jun 2022 04:08:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D12F9B826F5 for ; Wed, 8 Jun 2022 11:08:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A03DDC341C5; Wed, 8 Jun 2022 11:08:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654686485; bh=SOuPQn+pA+TZKZrMQUFucIbBb8s6sRe9OeriPuEMDuw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GsfLoyFmpW+QFja1teaME4QJkiYD8SA1umyq9chvRuoU6imv/asJmw3i/Nmyc6dAR 2xouHHmMYgdiXS3Q2k6PgsDrkck2wnDKsigqihEg0Lr+sfnMH/vPW59oeC+nVf8IOw DbrzXCZ5eiSwdpW0+4YabznaEOR25XHGyxjKcFVU86LYouzbTvRck29RtfkHxZTNel 9s/tcYG4lyslhkju0CE2vc8lvOzZzfXk7T1vKnbPmhQqLrPRv+w475ZTTzO0RcgKmx xoedvOiNuIaANdeInTDcnalmjGrhp2ckJVh95kDYlw64Z0ZPtb004J2+TnE++ylo/T BTWAqUCoY5iKQ== From: Tzung-Bi Shih To: bleung@chromium.org, groeck@chromium.org Cc: chrome-platform@lists.linux.dev, tzungbi@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 14/23] platform/chrome: don't use devm variants for `din` and `dout` Date: Wed, 8 Jun 2022 11:07:25 +0000 Message-Id: <20220608110734.2928245-15-tzungbi@kernel.org> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog In-Reply-To: <20220608110734.2928245-1-tzungbi@kernel.org> References: <20220608110734.2928245-1-tzungbi@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Don't use devm variants because the two buffers could be re-allocated multiple times during runtime. Their life cycles aren't quite aligned to the device's. Normally, free the memory if any when the ec_dev gets unregistered in cros_ec_unregister(). No need to free memory if kmalloc() fails. They will be freed eventually in either of the following: - Error handling path in cros_ec_register(). - In cros_ec_unregister(). - Next kmalloc() in cros_ec_query_all(). Signed-off-by: Tzung-Bi Shih --- Changes from v2: - Don't use realloc. Changes from v1: - Don't use devm. - Free in cros_ec_unregister(). drivers/platform/chrome/cros_ec.c | 4 +++ drivers/platform/chrome/cros_ec_proto.c | 29 ++++++++------------ drivers/platform/chrome/cros_ec_proto_test.c | 4 +-- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/drivers/platform/chrome/cros_ec.c b/drivers/platform/chrome/cros_ec.c index 29d3b544dafb..fb8cb8a73295 100644 --- a/drivers/platform/chrome/cros_ec.c +++ b/drivers/platform/chrome/cros_ec.c @@ -285,6 +285,8 @@ int cros_ec_register(struct cros_ec_device *ec_dev) exit: platform_device_unregister(ec_dev->ec); platform_device_unregister(ec_dev->pd); + kfree(ec_dev->din); + kfree(ec_dev->dout); return err; } EXPORT_SYMBOL(cros_ec_register); @@ -302,6 +304,8 @@ void cros_ec_unregister(struct cros_ec_device *ec_dev) if (ec_dev->pd) platform_device_unregister(ec_dev->pd); platform_device_unregister(ec_dev->ec); + kfree(ec_dev->din); + kfree(ec_dev->dout); } EXPORT_SYMBOL(cros_ec_unregister); diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c index 473654f50bca..8a53e989c7e2 100644 --- a/drivers/platform/chrome/cros_ec_proto.c +++ b/drivers/platform/chrome/cros_ec_proto.c @@ -469,7 +469,6 @@ static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev, */ int cros_ec_query_all(struct cros_ec_device *ec_dev) { - struct device *dev = ec_dev->dev; u32 ver_mask = 0; int ret; @@ -492,21 +491,18 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev) } } - devm_kfree(dev, ec_dev->din); - devm_kfree(dev, ec_dev->dout); + kfree(ec_dev->din); + ec_dev->din = NULL; + kfree(ec_dev->dout); + ec_dev->dout = NULL; - ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); - if (!ec_dev->din) { - ret = -ENOMEM; - goto exit; - } + ec_dev->din = kmalloc(ec_dev->din_size, GFP_KERNEL); + if (!ec_dev->din) + return -ENOMEM; - ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); - if (!ec_dev->dout) { - devm_kfree(dev, ec_dev->din); - ret = -ENOMEM; - goto exit; - } + ec_dev->dout = kmalloc(ec_dev->dout_size, GFP_KERNEL); + if (!ec_dev->dout) + return -ENOMEM; /* Probe if MKBP event is supported */ ret = cros_ec_get_host_command_version_mask(ec_dev, @@ -555,10 +551,7 @@ int cros_ec_query_all(struct cros_ec_device *ec_dev) "failed to retrieve wake mask: %d\n", ret); } - ret = 0; - -exit: - return ret; + return 0; } EXPORT_SYMBOL(cros_ec_query_all); diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c index 63071af81c94..ec106d0f5648 100644 --- a/drivers/platform/chrome/cros_ec_proto_test.c +++ b/drivers/platform/chrome/cros_ec_proto_test.c @@ -180,8 +180,8 @@ static void cros_ec_proto_test_query_all_pretest(struct kunit *test) /* * cros_ec_query_all() will free din and dout and allocate them again to fit the usage by - * calling devm_kfree() and devm_kzalloc(). Set them to NULL as they aren't managed by - * ec_dev->dev but allocated statically in struct cros_ec_proto_test_priv + * calling kfree() and kmalloc(). Set them to NULL as they aren't allocated by kmalloc() + * but allocated statically in struct cros_ec_proto_test_priv * (see cros_ec_proto_test_init()). */ ec_dev->din = NULL; -- 2.36.1.255.ge46751e96f-goog