Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 25bed649 authored by Manikanta Sivapala's avatar Manikanta Sivapala Committed by Gerrit - the friendly Code Review server
Browse files

msm: vidc: Convert realloc to resource managed malloc to avoid memleak.



Whenever the probe is deferred, the resources that are allocated by
devm_kzalloc() will automatically get freed, but that are
allocated by krealloc() has to be freed explicitly.
In video driver currently memory related to regulator is allocated
using krealloc. Such memory is causing memory leak when probe is deferred.

Convert krealloc to devm_kzalloc to avoid memleak during probe defer.

Change-Id: I6653a6c204fb219a4a72e14904ff1c044a57b976
Signed-off-by: default avatarManikanta Sivapala <msivap@codeaurora.org>
parent f0331928
Loading
Loading
Loading
Loading
+35 −24
Original line number Diff line number Diff line
@@ -103,13 +103,9 @@ static inline void msm_vidc_free_regulator_table(
		struct regulator_info *rinfo =
			&res->regulator_set.regulator_tbl[c];

		kfree(rinfo->name);
		rinfo->name = NULL;
	}

	/* The regulator table is one the few allocs that aren't managed, hence
	 * free it manually */
	kfree(res->regulator_set.regulator_tbl);
	res->regulator_set.regulator_tbl = NULL;
	res->regulator_set.count = 0;
}
@@ -422,6 +418,7 @@ static int msm_vidc_load_regulator_table(
	struct regulator_set *regulators = &res->regulator_set;
	struct device_node *domains_parent_node = NULL;
	struct property *domains_property = NULL;
	int reg_count = 0;

	regulators->count = 0;
	regulators->regulator_tbl = NULL;
@@ -431,50 +428,64 @@ static int msm_vidc_load_regulator_table(
		const char *search_string = "-supply";
		char *supply;
		bool matched = false;
		struct device_node *regulator_node = NULL;
		struct regulator_info *rinfo = NULL;
		void *temp = NULL;

		/* 1) check if current property is possibly a regulator */
		/* check if current property is possibly a regulator */
		supply = strnstr(domains_property->name, search_string,
				strlen(domains_property->name) + 1);
		matched = supply && (*(supply + strlen(search_string)) == '\0');
		if (!matched)
			continue;

		/* 2) make sure prop isn't being misused */
		regulator_node = of_parse_phandle(domains_parent_node,
				domains_property->name, 0);
		if (IS_ERR(regulator_node)) {
			dprintk(VIDC_WARN, "%s is not a phandle\n",
					domains_property->name);
			continue;
		reg_count++;
	}

		/* 3) expand our table */
		temp = krealloc(regulators->regulator_tbl,
	regulators->regulator_tbl = devm_kzalloc(&pdev->dev,
			sizeof(*regulators->regulator_tbl) *
				(regulators->count + 1), GFP_KERNEL);
		if (!temp) {
			reg_count, GFP_KERNEL);

	if (!regulators->regulator_tbl) {
		rc = -ENOMEM;
		dprintk(VIDC_ERR,
			"Failed to alloc memory for regulator table\n");
		goto err_reg_tbl_alloc;
	}

		regulators->regulator_tbl = temp;
	for_each_property_of_node(domains_parent_node, domains_property) {
		const char *search_string = "-supply";
		char *supply;
		bool matched = false;
		struct device_node *regulator_node = NULL;
		struct regulator_info *rinfo = NULL;

		/* check if current property is possibly a regulator */
		supply = strnstr(domains_property->name, search_string,
				strlen(domains_property->name) + 1);
		matched = supply && (supply[strlen(search_string)] == '\0');
		if (!matched)
			continue;

		/* make sure prop isn't being misused */
		regulator_node = of_parse_phandle(domains_parent_node,
				domains_property->name, 0);
		if (IS_ERR(regulator_node)) {
			dprintk(VIDC_WARN, "%s is not a phandle\n",
					domains_property->name);
			continue;
		}
		regulators->count++;

		/* 4) populate regulator info */
		/* populate regulator info */
		rinfo = &regulators->regulator_tbl[regulators->count - 1];
		rinfo->name = kstrndup(domains_property->name,
				supply - domains_property->name, GFP_KERNEL);
		rinfo->name = devm_kzalloc(&pdev->dev,
			(supply - domains_property->name) + 1, GFP_KERNEL);
		if (!rinfo->name) {
			rc = -ENOMEM;
			dprintk(VIDC_ERR,
					"Failed to alloc memory for regulator name\n");
			goto err_reg_name_alloc;
		}
		strlcpy(rinfo->name, domains_property->name,
			(supply - domains_property->name) + 1);

		rinfo->has_hw_power_collapse = of_property_read_bool(
			regulator_node, "qcom,support-hw-trigger");