From patchwork Mon Jul 4 15:42:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Leach X-Patchwork-Id: 12905530 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5B797C433EF for ; Mon, 4 Jul 2022 15:44:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=rZpj1nruNSdzVLU01P8E5Jb5nB95as/iiCwziRObtPc=; b=fDF1bPO/xyPGAP 9xHoVUsEbrbw+T3nEZmiwumJ9mmqObvm42V30uKKvfkzbquP/+inu377NVcJIAcCM32sTWXdoMFnd F2+fr6Lyo2UPKtFLuZfEB+vP4PIIWsMmAOYI33I29QSVUmK6d1AZ4qcQuemPUbzdraInc8rWKsj9t MhYlvGYtlYLbBfAxZodsPLFXmoa+lnmTf7VW3jKiFzYF2c2+ShvFlD2lym/panqcpSXx4wCk9pDo3 IdcNQwu0UNmkwgN8vgWsOhN/dI4DbMEVpM61hqL/dWnw1vLu11iFZJunHniBFX66CXCegXQO5Mojb zt704oQOCqLtaMeMsS9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8OEI-009lsK-7e; Mon, 04 Jul 2022 15:43:18 +0000 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o8ODw-009liW-AY for linux-arm-kernel@lists.infradead.org; Mon, 04 Jul 2022 15:42:58 +0000 Received: by mail-wr1-x434.google.com with SMTP id cl1so14041237wrb.4 for ; Mon, 04 Jul 2022 08:42:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kVN6AgfRQ9JB5xb8lTlVzU3nqEsQK95Vgz2t3dnIbI4=; b=ckrJIPJAwHonM+IdF9BVRdc90bYnnfjbzbzju65B1KKFYa86FIKxjcOMzgLDhZrgfn lQCOT7a/eRpKZNQLSZdgZFLd2CKqoyKnal9I6wA4s0Q5lo/YFLjl9FRRgyiWblFu7u0l olP3hDmCQyOej4mYLBf3JXxSmwAIe8g7uBUj2WEYUoJShN3eWmr7krp36q8tl+6+6l1k h10yiVsRyi5B5TY5beZk7k/Yak3ce0h+1b3WLb2+793FbxdPEBxF0sjGHt/1u5rgO0fX jY41sbXkT/H3wflu4/f/jlsSa/yGFvAy6WX6CxHv1wezjNp2HyHTSEXSWNVaNVerpO81 kk5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kVN6AgfRQ9JB5xb8lTlVzU3nqEsQK95Vgz2t3dnIbI4=; b=a4N3qYqJDNj/1syR+8/Ecf24/ULoVlrNf59nYvmQbr42kSIgdlag5L4uI13EcAE+nC b3UUsqoeE6ZJJebMxatCu/KQ8aMiL5qgv+zEdreT3QKLxUvxKFoQ4sx9BMxGyRiMIO06 POEvciorQInQQLEIL9whc9SvbG43lpd4Em4auQSAFWiDGsZv+K+Wgyi07RF7jDw/71wk kfIkJHSHJhUYpeL2AZl3sH2NHWa4TJkd2iui/LILnl6jRcLuO+YiD7INKLBye+p9oRc9 6+ZTuIQs0QXVxAdZbgSwd5kjyAxZWwIlp569ug8oypzVawTM555f1L7cW9MRfCUkWpT4 R2eA== X-Gm-Message-State: AJIora9GUb2krVvbW6HEHcXbc+PQhCN2yQXxahg2f+IVI1JZhkF3G4zY T9Lzyp9rlpjliFNWNTm/2WKfJ1TpmgLDlQ== X-Google-Smtp-Source: AGRyM1uKhJamkuobuces0fTri6XuS3VttQB16ma2+ZCWseG8v2023ndZovt8Tx3S96NngTmG1hNtpg== X-Received: by 2002:a5d:6d46:0:b0:21b:933d:7950 with SMTP id k6-20020a5d6d46000000b0021b933d7950mr27576848wri.679.1656949372566; Mon, 04 Jul 2022 08:42:52 -0700 (PDT) Received: from linaro.org ([2a00:23c5:6809:2201:a9e8:9fdd:fa2d:1c72]) by smtp.gmail.com with ESMTPSA id j6-20020adfff86000000b0021d6b851ca1sm2763749wrr.111.2022.07.04.08.42.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 08:42:52 -0700 (PDT) From: Mike Leach To: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org, linux-kernel@vger.kernel.org Cc: mathieu.poirier@linaro.org, suzuki.poulose@arm.com, leo.yan@linaro.org, acme@kernel.org, james.clark@arm.com, Mike Leach Subject: [PATCH v4 1/7] coresight: configfs: Update memory allocation / free for configfs elements Date: Mon, 4 Jul 2022 16:42:43 +0100 Message-Id: <20220704154249.11501-2-mike.leach@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220704154249.11501-1-mike.leach@linaro.org> References: <20220704154249.11501-1-mike.leach@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220704_084256_459795_69A31266 X-CRM114-Status: GOOD ( 24.62 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Previously, the objects backing the configfs directories and files were created using devm managed memory on the coresight device. Now we are adding configfs load/unload, configurations can be loaded many times over the lifetime of the device, so it is more appropriate to use normally allocated and freed memory. Signed-off-by: Mike Leach --- .../coresight/coresight-syscfg-configfs.c | 119 +++++++++++++----- 1 file changed, 88 insertions(+), 31 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c index 433ede94dd63..6e8c8db52d39 100644 --- a/drivers/hwtracing/coresight/coresight-syscfg-configfs.c +++ b/drivers/hwtracing/coresight/coresight-syscfg-configfs.c @@ -14,7 +14,7 @@ static inline struct config_item_type *cscfg_create_ci_type(void) { struct config_item_type *ci_type; - ci_type = devm_kzalloc(cscfg_device(), sizeof(*ci_type), GFP_KERNEL); + ci_type = kzalloc(sizeof(*ci_type), GFP_KERNEL); if (ci_type) ci_type->ct_owner = THIS_MODULE; @@ -175,6 +175,19 @@ static struct config_item_type cscfg_config_preset_type = { .ct_attrs = cscfg_config_preset_attrs, }; + +/* walk list of presets and free the previously allocated memory */ +static void cscfg_destroy_preset_groups(struct config_group *cfg_view_group) +{ + struct cscfg_fs_preset *cfg_fs_preset; + struct config_group *p_group; + + list_for_each_entry(p_group, &cfg_view_group->default_groups, default_groups) { + cfg_fs_preset = container_of(p_group, struct cscfg_fs_preset, group); + kfree(cfg_fs_preset); + } +} + static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) { int preset_num; @@ -186,11 +199,12 @@ static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) return 0; for (preset_num = 1; preset_num <= config_desc->nr_presets; preset_num++) { - cfg_fs_preset = devm_kzalloc(cscfg_device(), - sizeof(struct cscfg_fs_preset), GFP_KERNEL); + cfg_fs_preset = kzalloc(sizeof(struct cscfg_fs_preset), GFP_KERNEL); - if (!cfg_fs_preset) + if (!cfg_fs_preset) { + cscfg_destroy_preset_groups(&cfg_view->group); return -ENOMEM; + } snprintf(name, CONFIGFS_ITEM_NAME_LEN, "preset%d", preset_num); cfg_fs_preset->preset_num = preset_num; @@ -204,14 +218,10 @@ static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view) static struct config_group *cscfg_create_config_group(struct cscfg_config_desc *config_desc) { - struct cscfg_fs_config *cfg_view; - struct device *dev = cscfg_device(); + struct cscfg_fs_config *cfg_view = NULL; int err; - if (!dev) - return ERR_PTR(-EINVAL); - - cfg_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_config), GFP_KERNEL); + cfg_view = kzalloc(sizeof(struct cscfg_fs_config), GFP_KERNEL); if (!cfg_view) return ERR_PTR(-ENOMEM); @@ -220,12 +230,21 @@ static struct config_group *cscfg_create_config_group(struct cscfg_config_desc * /* add in a preset dir for each preset */ err = cscfg_add_preset_groups(cfg_view); - if (err) + if (err) { + kfree(cfg_view); return ERR_PTR(err); - + } return &cfg_view->group; } +static void cscfg_destroy_config_group(struct config_group *group) +{ + struct cscfg_fs_config *cfg_view = container_of(group, struct cscfg_fs_config, group); + + cscfg_destroy_preset_groups(&cfg_view->group); + kfree(cfg_view); +} + /* attributes for features view */ static ssize_t cscfg_feat_description_show(struct config_item *item, char *page) @@ -314,6 +333,17 @@ static struct config_item_type cscfg_param_view_type = { .ct_attrs = cscfg_param_view_attrs, }; +/* walk the list of default groups - which were set as param items and remove */ +static void cscfg_destroy_params_group_items(struct config_group *params_group) +{ + struct cscfg_fs_param *param_item; + struct config_group *p_group; + + list_for_each_entry(p_group, ¶ms_group->default_groups, default_groups) { + param_item = container_of(p_group, struct cscfg_fs_param, group); + kfree(param_item); + } +} /* * configfs has far less functionality provided to add attributes dynamically than sysfs, * and the show and store fns pass the enclosing config_item so the actual attribute cannot @@ -322,15 +352,16 @@ static struct config_item_type cscfg_param_view_type = { static int cscfg_create_params_group_items(struct cscfg_feature_desc *feat_desc, struct config_group *params_group) { - struct device *dev = cscfg_device(); struct cscfg_fs_param *param_item; int i; /* parameter items - as groups with default_value attribute */ for (i = 0; i < feat_desc->nr_params; i++) { - param_item = devm_kzalloc(dev, sizeof(struct cscfg_fs_param), GFP_KERNEL); - if (!param_item) + param_item = kzalloc(sizeof(struct cscfg_fs_param), GFP_KERNEL); + if (!param_item) { + cscfg_destroy_params_group_items(params_group); return -ENOMEM; + } param_item->feat_desc = feat_desc; param_item->param_idx = i; config_group_init_type_name(¶m_item->group, @@ -343,27 +374,22 @@ static int cscfg_create_params_group_items(struct cscfg_feature_desc *feat_desc, static struct config_group *cscfg_create_feature_group(struct cscfg_feature_desc *feat_desc) { - struct cscfg_fs_feature *feat_view; - struct config_item_type *params_group_type; + struct cscfg_fs_feature *feat_view = NULL; + struct config_item_type *params_group_type = NULL; struct config_group *params_group = NULL; - struct device *dev = cscfg_device(); - int item_err; - - if (!dev) - return ERR_PTR(-EINVAL); + int err = -ENOMEM; - feat_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_feature), GFP_KERNEL); + feat_view = kzalloc(sizeof(struct cscfg_fs_feature), GFP_KERNEL); if (!feat_view) return ERR_PTR(-ENOMEM); if (feat_desc->nr_params) { - params_group = devm_kzalloc(dev, sizeof(struct config_group), GFP_KERNEL); + params_group = kzalloc(sizeof(struct config_group), GFP_KERNEL); if (!params_group) - return ERR_PTR(-ENOMEM); - + goto exit_err_free_mem; params_group_type = cscfg_create_ci_type(); if (!params_group_type) - return ERR_PTR(-ENOMEM); + goto exit_err_free_mem; } feat_view->feat_desc = feat_desc; @@ -373,11 +399,36 @@ static struct config_group *cscfg_create_feature_group(struct cscfg_feature_desc if (params_group) { config_group_init_type_name(params_group, "params", params_group_type); configfs_add_default_group(params_group, &feat_view->group); - item_err = cscfg_create_params_group_items(feat_desc, params_group); - if (item_err) - return ERR_PTR(item_err); + err = cscfg_create_params_group_items(feat_desc, params_group); + if (err) + goto exit_err_free_mem; } return &feat_view->group; + +exit_err_free_mem: + kfree(feat_view); + kfree(params_group_type); + kfree(params_group); + return ERR_PTR(err); +} + +static void cscfg_destroy_feature_group(struct config_group *feat_group) +{ + struct cscfg_fs_feature *feat_view; + struct config_group *params_group = NULL; + + feat_view = container_of(feat_group, struct cscfg_fs_feature, group); + + /* params group is the first item on the default group list */ + if (!list_empty(&feat_group->default_groups)) { + params_group = list_first_entry(&feat_group->default_groups, + struct config_group, default_groups); + cscfg_destroy_params_group_items(params_group); + /* free the item type, then the group */ + kfree(params_group->cg_item.ci_type); + kfree(params_group); + } + kfree(feat_view); } static struct config_item_type cscfg_configs_type = { @@ -403,6 +454,8 @@ int cscfg_configfs_add_config(struct cscfg_config_desc *config_desc) err = configfs_register_group(&cscfg_configs_grp, new_group); if (!err) config_desc->fs_group = new_group; + else + cscfg_destroy_config_group(new_group); return err; } @@ -410,6 +463,7 @@ void cscfg_configfs_del_config(struct cscfg_config_desc *config_desc) { if (config_desc->fs_group) { configfs_unregister_group(config_desc->fs_group); + cscfg_destroy_config_group(config_desc->fs_group); config_desc->fs_group = NULL; } } @@ -434,9 +488,11 @@ int cscfg_configfs_add_feature(struct cscfg_feature_desc *feat_desc) new_group = cscfg_create_feature_group(feat_desc); if (IS_ERR(new_group)) return PTR_ERR(new_group); - err = configfs_register_group(&cscfg_features_grp, new_group); + err = configfs_register_group(&cscfg_features_grp, new_group); if (!err) feat_desc->fs_group = new_group; + else + cscfg_destroy_feature_group(new_group); return err; } @@ -444,6 +500,7 @@ void cscfg_configfs_del_feature(struct cscfg_feature_desc *feat_desc) { if (feat_desc->fs_group) { configfs_unregister_group(feat_desc->fs_group); + cscfg_destroy_feature_group(feat_desc->fs_group); feat_desc->fs_group = NULL; } }