From nobody Tue Dec 2 02:19:49 2025 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7E5C1A9F86 for ; Thu, 20 Nov 2025 00:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763597735; cv=none; b=Ys68NmF/wTbd3DXbeEcbbCYAC8tByr2C8zMfQrfyKnorRy1zkzOLUGe2spI4CEZn6+dtbwPB3x4Z52++HQKnNx8zTRBgteNh41aa2I0OtAKuzYy1WrrlZzp/NWHCJ7/YBnXkM4JPEX4ZcXH4z/RR+xWszLm6AkzwCO3WxQqHc3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763597735; c=relaxed/simple; bh=kiLZnB7V3oRRKyDtpxL+bmJ91OS8alTGnIrM+Xg3Y10=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dpkgjmuzgjxWxLdsqM9oWUaYsdexwwonUdjo32+lX7XWVkGDPfYN2BoXCIpoEtX8G2FgK8OG7ZdQvZrKGOn0dbEtvuOqqVohBAg4MdSb6mBwI46CXzbXF6377ZrnipgzH+GexpZYmn8wlEYsvBX/neFNlDHPaqMJZLOsSVOnFEg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PVdymMPL; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PVdymMPL" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-47798089d30so41775e9.1 for ; Wed, 19 Nov 2025 16:15:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763597731; x=1764202531; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2vhVpii8LgsowqvzzxuLZsno7LX3ZQOMIJUlQ5i2ZhQ=; b=PVdymMPLRK3rYHMgVA9zo1XHncyicNba6T0wenwrtTxWfhMCp4XvccL6+u4W8ytXTb ocR5l8dFA1CR4phfYE/xnfI8gVI/LuoOZfvlHFu0LKWezunf51XyOT06YZj0bx6ELh2U oPikoXEuQ+P2x8j5mIEBM6+Uc6493o8UEOfkC8O9gOWgGkxKrliq39cKu/9yHVLnuiyY sYPDPeq/miTkO76B9G/xFUV5XYSiubfEl3vAsolQ4fxx2R2YYD+G1BvGDApOPHfhdSh0 9RFJsCcarfogSOeukRqwGdZnN+SJDVoEh+l9RKiRQeEZyx1vWUFSvqlBI6M6ARB2Yijx 9ZFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763597731; x=1764202531; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=2vhVpii8LgsowqvzzxuLZsno7LX3ZQOMIJUlQ5i2ZhQ=; b=PqasvckjAGaWU35oekXcR9kIui/Pa1OE/Sl0dfCZ0WzadctqKG82bhUZdf/o9WCWZY +xjHDaImV91KamI13Hklx/e/HI9W+ZmhsHLcH1WYCV+SM68dIEh/hXdK0CV7TdtYt6Az vb0DUxvtgARpgr6UziLKVHP9ONNph7QJZ4jj5HmpKqqIVENg0pPGCV1lAaTHgsxhb7sL ihFWwOxvxQ7PVFa1fN/ypcPd4inEjgRBWw8JeHUh5q778CkB3bL9MHGZ3YPCY05vVwZ7 9xCpWn/SIIvTrDWOGOdruPXM/oMjXF2cclJb8VLJcL+1DK06rojocS1V9b95rnUXKHMR n35Q== X-Forwarded-Encrypted: i=1; AJvYcCWirK6cnB/5/wCMYAe++ypbctpE0crjiQsU25NS5+n72BkxbKNmdhmV1i5GApC1PwldLSXV7zgny9XPA2g=@vger.kernel.org X-Gm-Message-State: AOJu0YwT5Sp4QY3XEojvJwFgbu6xB83E5pak2n3iBTdOMo69t8X/aLoX f7+JXw6ZlKDpRVMs2TPjMOOIfFRJwtzDTfzuNcXdXULB5zdSY8u/pk1j X-Gm-Gg: ASbGncvXgzqAy08fO0KV2TQ88D0MtAvxFzrYPv8TX78cC9/RFMryeIgJr6+olIgb56F UIWplmGzdV4eGpu7XVDiShD261ZlkaMLJUp0M35KMaESi8A/sKXXJ8XBIY9rwGObKCBi/Ur7fcM zhMroIGEgIktyVJo6+Xkfd8lqaubX7Ye8NJUudReuqX9vZqbrmH1QizM+4sppNEYxYkxidiGIav dfaqtDUgN7hzBNok1rpUZjlVU7UM9+/FD5VwKQ+ShtBXGCoyajmkg//moTw06EQ+HxCBb6pKKIO Dtwj924eCGRYOiiRbrJ3MMawAvL7rzN5zdLoV9FlKaJkpDHNl/DITof7nAhyCjFj7d8LtM6CONN B2g8mrnDcYpQpnJWS7K9TiYGNp97UByhH7GUdUbuwcIW/pc1R4uSX3+7nkMjF7f2tIeA+Q6kn7H 7BiZP0Ik/nmoUerEODCw/v4TfzlozLTUYjtRRZ X-Google-Smtp-Source: AGHT+IF2MRiH1UMlf7EeKR28jq1KCEB+qNGWKKavy66pwwS77hnOtqSMicugRf/j58+WJYPncLzdPQ== X-Received: by 2002:a05:600c:1d1d:b0:477:7a78:3000 with SMTP id 5b1f17b1804b1-477b8da2349mr5176775e9.6.1763597730861; Wed, 19 Nov 2025 16:15:30 -0800 (PST) Received: from localhost ([2a03:2880:31ff:5::]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-477b10804c8sm76579615e9.15.2025.11.19.16.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 16:15:30 -0800 (PST) From: Gustavo Luiz Duarte Date: Wed, 19 Nov 2025 16:14:51 -0800 Subject: [PATCH net-next v3 3/4] netconsole: Dynamic allocation of userdata buffer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251119-netconsole_dynamic_extradata-v3-3-497ac3191707@meta.com> References: <20251119-netconsole_dynamic_extradata-v3-0-497ac3191707@meta.com> In-Reply-To: <20251119-netconsole_dynamic_extradata-v3-0-497ac3191707@meta.com> To: Breno Leitao , Andre Carvalho , Simon Horman , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Gustavo Luiz Duarte X-Mailer: b4 0.13.0 The userdata buffer in struct netconsole_target is currently statically allocated with a size of MAX_USERDATA_ITEMS * MAX_EXTRADATA_ENTRY_LEN (16 * 256 =3D 4096 bytes). This wastes memory when userdata entries are not used or when only a few entries are configured, which is common in typical usage scenarios. It also forces us to keep MAX_USERDATA_ITEMS small to limit the memory wasted. Change the userdata buffer from a static array to a dynamically allocated pointer. The buffer is now allocated on-demand in update_userdata() whenever userdata entries are added, modified, or removed via configfs. The implementation calculates the exact size needed for all current userdata entries, allocates a new buffer of that size, formats the entries into it, and atomically swaps it with the old buffer. This approach provides several benefits: - Memory efficiency: Targets with no userdata use zero bytes instead of 4KB, and targets with userdata only allocate what they need; - Scalability: Makes it practical to increase MAX_USERDATA_ITEMS to a much larger value without imposing a fixed memory cost on every target; - No hot-path overhead: Allocation occurs during configuration (write to configfs), not during message transmission If memory allocation fails during userdata update, -ENOMEM is returned to userspace through the configfs attribute write operation. The sysdata buffer remains statically allocated since it has a smaller fixed size (MAX_SYSDATA_ITEMS * MAX_EXTRADATA_ENTRY_LEN =3D 4 * 256 =3D 1024 bytes) and its content length is less predictable. Signed-off-by: Gustavo Luiz Duarte --- drivers/net/netconsole.c | 100 +++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 70 insertions(+), 30 deletions(-) diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 1bd811714322..0b350f82d915 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -155,7 +155,7 @@ struct netconsole_target { #ifdef CONFIG_NETCONSOLE_DYNAMIC struct config_group group; struct config_group userdata_group; - char userdata[MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS]; + char *userdata; size_t userdata_length; char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS]; =20 @@ -875,45 +875,77 @@ static ssize_t userdatum_value_show(struct config_ite= m *item, char *buf) return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0])); } =20 -static void update_userdata(struct netconsole_target *nt) +/* Navigate configfs and calculate the lentgh of the formatted string + * representing userdata. + * Must be called holding netconsole_subsys.su_mutex + */ +static int calc_userdata_len(struct netconsole_target *nt) { + struct userdatum *udm_item; + struct config_item *item; struct list_head *entry; - int child_count =3D 0; - unsigned long flags; + int len =3D 0; =20 - spin_lock_irqsave(&target_list_lock, flags); + list_for_each(entry, &nt->userdata_group.cg_children) { + item =3D container_of(entry, struct config_item, ci_entry); + udm_item =3D to_userdatum(item); + /* Skip userdata with no value set */ + if (udm_item->value[0]) { + len +=3D snprintf(NULL, 0, " %s=3D%s\n", item->ci_name, + udm_item->value); + } + } + return len; +} =20 - /* Clear the current string in case the last userdatum was deleted */ - nt->userdata_length =3D 0; - nt->userdata[0] =3D 0; +static int update_userdata(struct netconsole_target *nt) +{ + struct userdatum *udm_item; + struct config_item *item; + struct list_head *entry; + char *old_buf =3D NULL; + char *new_buf =3D NULL; + unsigned long flags; + int offset =3D 0; + int len; =20 - list_for_each(entry, &nt->userdata_group.cg_children) { - struct userdatum *udm_item; - struct config_item *item; + /* Calculate required buffer size */ + len =3D calc_userdata_len(nt); =20 - if (child_count >=3D MAX_USERDATA_ITEMS) { - spin_unlock_irqrestore(&target_list_lock, flags); - WARN_ON_ONCE(1); - return; - } - child_count++; + if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS)) + return -ENOSPC; + + /* Allocate new buffer */ + if (len) { + new_buf =3D kmalloc(len + 1, GFP_KERNEL); + if (!new_buf) + return -ENOMEM; + } =20 + /* Write userdata to new buffer */ + list_for_each(entry, &nt->userdata_group.cg_children) { item =3D container_of(entry, struct config_item, ci_entry); udm_item =3D to_userdatum(item); - /* Skip userdata with no value set */ - if (strnlen(udm_item->value, MAX_EXTRADATA_VALUE_LEN) =3D=3D 0) - continue; - - /* This doesn't overflow userdata since it will write - * one entry length (1/MAX_USERDATA_ITEMS long), entry count is - * checked to not exceed MAX items with child_count above - */ - nt->userdata_length +=3D scnprintf(&nt->userdata[nt->userdata_length], - MAX_EXTRADATA_ENTRY_LEN, " %s=3D%s\n", - item->ci_name, udm_item->value); + if (udm_item->value[0]) { + offset +=3D scnprintf(&new_buf[offset], len + 1 - offset, + " %s=3D%s\n", item->ci_name, + udm_item->value); + } } + + WARN_ON_ONCE(offset !=3D len); + + /* Switch to new buffer and free old buffer */ + spin_lock_irqsave(&target_list_lock, flags); + old_buf =3D nt->userdata; + nt->userdata =3D new_buf; + nt->userdata_length =3D offset; spin_unlock_irqrestore(&target_list_lock, flags); + + kfree(old_buf); + + return 0; } =20 static ssize_t userdatum_value_store(struct config_item *item, const char = *buf, @@ -937,7 +969,9 @@ static ssize_t userdatum_value_store(struct config_item= *item, const char *buf, =20 ud =3D to_userdata(item->ci_parent); nt =3D userdata_to_target(ud); - update_userdata(nt); + ret =3D update_userdata(nt); + if (ret < 0) + goto out_unlock; ret =3D count; out_unlock: mutex_unlock(&dynamic_netconsole_mutex); @@ -1193,7 +1227,10 @@ static struct configfs_attribute *netconsole_target_= attrs[] =3D { =20 static void netconsole_target_release(struct config_item *item) { - kfree(to_target(item)); + struct netconsole_target *nt =3D to_target(item); + + kfree(nt->userdata); + kfree(nt); } =20 static struct configfs_item_operations netconsole_target_item_ops =3D { @@ -1874,6 +1911,9 @@ static struct netconsole_target *alloc_param_target(c= har *target_config, static void free_param_target(struct netconsole_target *nt) { netpoll_cleanup(&nt->np); +#ifdef CONFIG_NETCONSOLE_DYNAMIC + kfree(nt->userdata); +#endif kfree(nt); } =20 --=20 2.47.3