From nobody Thu Apr 2 15:36:11 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E810B376477 for ; Thu, 5 Mar 2026 08:54:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772700898; cv=none; b=KQ1DnNSllZijgfN+9R4Y01vlUD1IvI6LAOVv77WUL6I7FCHyUi9HtHcwfaG42ySvuO9x7iMiUQAz1U9QMYyHyQX4vKIV0brn59l4A+Iy/IsWcl5mfRPrgY6fO861NRGa1BKHko3jhYCgooU6R7CNAf/wvKeNhDlsDzVssMvwTUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772700898; c=relaxed/simple; bh=9P5dpYxQuWcUjFjd+RUZceybv8gUSEaT/oC+QYdlkBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qZVLf7HSR9GhZYlefMno3eJA+tEXu8whVy29zNBaJAY9QtibQlWqifvZoy0f+9ldE1ewiXYz7jLnndk07Yo1ek/1qFIcjyowNEAjAFC6GvysGiTdZr6xYqGPJixaUangVw4qw6v84MCCAxuAARrtk9Yk8i8nI03c8gomPaOXUPg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=IxKE+Sl9; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IxKE+Sl9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1772700897; x=1804236897; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9P5dpYxQuWcUjFjd+RUZceybv8gUSEaT/oC+QYdlkBE=; b=IxKE+Sl9n9Zc7jBQsOKGI/hUERfRLaEnVvAckCTd9tn2wjJl9lYWFuOQ Qu69mfJOCEd6ELVvBwKq5qYQSycboTRtSxo31Vs7kDugkZj5apQy5ggkf qS4UxnDXxavpKXedJXnKudNlymc5AwX1/8CxCMwlKTyCegUxmXs2MQonP chVAGPs3zF1CIFCvX5UQKzh+mFNID27G6JCKC7VSj7WwV6Ubg+kmklW9p 7wQ/nOaxE0UYlaZEdrA8B7OtBmyWHP5QtOPdVaaVjbswkRKq9jU9UR7GU 6H4gw7+8wpH2ysMB91WXYWphU2a7t9QvoImFdOkDeh13hVNRBWX+ibaIs Q==; X-CSE-ConnectionGUID: TA9sUSvSTqmq2mCq7gwMnA== X-CSE-MsgGUID: Kjx5gHUcTBSSf7fd27p9ZQ== X-IronPort-AV: E=McAfee;i="6800,10657,11719"; a="73687434" X-IronPort-AV: E=Sophos;i="6.21,325,1763452800"; d="scan'208";a="73687434" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2026 00:54:53 -0800 X-CSE-ConnectionGUID: Sbs7cjivR3Snq2Necac6cw== X-CSE-MsgGUID: 6fib0Z6YR8Cbez5VDyURew== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,325,1763452800"; d="scan'208";a="216425988" Received: from black.igk.intel.com ([10.91.253.5]) by fmviesa007.fm.intel.com with ESMTP; 05 Mar 2026 00:54:51 -0800 Received: by black.igk.intel.com (Postfix, from userid 1003) id 5B93999; Thu, 05 Mar 2026 09:54:50 +0100 (CET) From: Andy Shevchenko To: Andy Shevchenko , linux-kernel@vger.kernel.org, driver-core@lists.linux.dev Cc: Mark Brown , Greg Kroah-Hartman , "Rafael J. Wysocki" , Danilo Krummrich Subject: [PATCH v6 2/3] regcache: Allocate and free reg_defaults on the same level Date: Thu, 5 Mar 2026 09:53:01 +0100 Message-ID: <20260305085449.3184020-3-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260305085449.3184020-1-andriy.shevchenko@linux.intel.com> References: <20260305085449.3184020-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently reg_defaults buffer may be allocated on two different levels when the user provided them and we duplicate it in regcache_init() or when user wants us to read back from HW in regcache_hw_init(). This inconsistency makes code harder to follow and maintain. Allocate and free reg_defaults on the same level in regcache_init() to improve the readability and maintenance efforts. Signed-off-by: Andy Shevchenko --- drivers/base/regmap/regcache.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c index b73de70bbf3f..e9d95aa63938 100644 --- a/drivers/base/regmap/regcache.c +++ b/drivers/base/regmap/regcache.c @@ -56,17 +56,12 @@ static int regcache_count_cacheable_registers(struct re= gmap *map) return count; } =20 -static int regcache_hw_init(struct regmap *map, int count) +static int regcache_hw_init(struct regmap *map) { int ret; unsigned int reg, val; void *tmp_buf; =20 - map->num_reg_defaults =3D count; - map->reg_defaults =3D kmalloc_objs(struct reg_default, count); - if (!map->reg_defaults) - return -ENOMEM; - if (!map->reg_defaults_raw) { bool cache_bypass =3D map->cache_bypass; dev_dbg(map->dev, "No cache defaults, reading back from HW\n"); @@ -74,10 +69,8 @@ static int regcache_hw_init(struct regmap *map, int coun= t) /* Bypass the cache access till data read from HW */ map->cache_bypass =3D true; tmp_buf =3D kmalloc(map->cache_size_raw, GFP_KERNEL); - if (!tmp_buf) { - ret =3D -ENOMEM; - goto err_free; - } + if (!tmp_buf) + return -ENOMEM; ret =3D regmap_raw_read(map, 0, tmp_buf, map->cache_size_raw); map->cache_bypass =3D cache_bypass; @@ -110,7 +103,7 @@ static int regcache_hw_init(struct regmap *map, int cou= nt) if (ret !=3D 0) { dev_err(map->dev, "Failed to read %x: %d\n", reg, ret); - goto err_free; + return ret; } } =20 @@ -120,16 +113,10 @@ static int regcache_hw_init(struct regmap *map, int c= ount) } =20 return 0; - -err_free: - kfree(map->reg_defaults); - - return ret; } =20 static void regcache_hw_exit(struct regmap *map) { - kfree(map->reg_defaults); if (map->cache_free) kfree(map->reg_defaults_raw); } @@ -209,13 +196,18 @@ int regcache_init(struct regmap *map, const struct re= gmap_config *config) if (map->cache_bypass) return 0; =20 + map->num_reg_defaults =3D count; + map->reg_defaults =3D kmalloc_objs(struct reg_default, count); + if (!map->reg_defaults) + return -ENOMEM; + /* Some devices such as PMICs don't have cache defaults, * we cope with this by reading back the HW registers and * crafting the cache defaults by hand. */ - ret =3D regcache_hw_init(map, count); + ret =3D regcache_hw_init(map); if (ret < 0) - return ret; + goto err_free_reg_defaults; } =20 if (!map->max_register_is_set && map->num_reg_defaults_raw) { @@ -253,6 +245,8 @@ int regcache_init(struct regmap *map, const struct regm= ap_config *config) } err_free: regcache_hw_exit(map); +err_free_reg_defaults: + kfree(map->reg_defaults); =20 return ret; } @@ -273,6 +267,8 @@ void regcache_exit(struct regmap *map) map->cache_ops->exit(map); map->unlock(map->lock_arg); } + + kfree(map->reg_defaults); } =20 /** --=20 2.50.1