From patchwork Thu Oct 17 15:24:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoine Tenart X-Patchwork-Id: 13840224 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2D6081DED4D for ; Thu, 17 Oct 2024 15:24:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178669; cv=none; b=Q7oPO2dBCUAHhlZ1uLR3C0IrU9h4z60g4AkXYoh81Qsm0rOk1AWMl/GehMZ9RDGKxwaQI24taOsqJHpFTwHJ1aXDwTxcpDPt6P74XzZMqv3YkjWWWA162cKiuY2sgFKrDJA4BUMONHegYO3t7ptKsICYUq20ELmZHxx+aoa+SJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178669; c=relaxed/simple; bh=OykKxKwP0lrkTqzkAJtCbXaqTh7JZLgsk0rcLfNzoow=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u9y/nHShY7Ag5iAti8gBxjRicjLg2NmdxuVaj/bmyYxEUBDjzBmSw2PgsRjO+I8RxfIT85V6e5YtKp618fSMP5wPWQgIKfqijj/INOC8MkaUsTodEec1r1O0uW/hfnJZWRO7IRAljNGM9lIVhzfEle4dipyucUr1NC/xyGTabyw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=o7kwpetA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="o7kwpetA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AD9EC4CEC3; Thu, 17 Oct 2024 15:24:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729178668; bh=OykKxKwP0lrkTqzkAJtCbXaqTh7JZLgsk0rcLfNzoow=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o7kwpetA0lX81KRkcc3HZzVPO2iHq3m6YcUqeD9+yiEzPIviJ5YSbA7YUW+FlwoN9 Gn+powb5iWFi73S3d5v3zxYiBvdNxa1/wRU5cSraYgFd4snggOxPyOC/zJmQk4j0sF x3SZS5l3a+4GGNTxAXMGpI62miL2mfrkIzi4yL1FbpqCg3sQ/2gKUXuHmGKTpZx7NL /ygYxm6a1qDoEm92ktKcMg0Zh3kKspEFhSzqOqVQe6DWacNoMTL1EquWb6Wh9CeTg9 lAO3TciNpEEj5uAZkwAiOia1H0vL29Pz49r20ZUYBHIvwmlyobjflkKQ5QgatKyNHE 7JLUHyoqkkfaQ== From: Antoine Tenart To: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, edumazet@google.com Cc: Antoine Tenart , netdev@vger.kernel.org Subject: [PATCH net-next 1/3] net: sysctl: remove always-true condition Date: Thu, 17 Oct 2024 17:24:17 +0200 Message-ID: <20241017152422.487406-2-atenart@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241017152422.487406-1-atenart@kernel.org> References: <20241017152422.487406-1-atenart@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Before adding a new line at the end of the temporary buffer in dump_cpumask, a length check is performed to ensure there is space for it. len = min(sizeof(kbuf) - 1, *lenp); len = scnprintf(kbuf, len, ...); if (len < *lenp) kbuf[len++] = '\n'; Note that the check is currently logically wrong, the written length is compared against the output buffer, not the temporary one. However this has no consequence as this is always true, even if fixed: scnprintf includes a null char at the end of the buffer but the returned length do not include it and there is always space for overriding it with a newline. Remove the condition. Signed-off-by: Antoine Tenart Reviewed-by: Simon Horman --- net/core/sysctl_net_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c index b60fac380cec..e7c0121dfaa1 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -69,8 +69,10 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, return; } - if (len < *lenp) - kbuf[len++] = '\n'; + /* scnprintf writes a trailing null char not counted in the returned + * length, override it with a newline. + */ + kbuf[len++] = '\n'; memcpy(buffer, kbuf, len); *lenp = len; *ppos += len; From patchwork Thu Oct 17 15:24:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoine Tenart X-Patchwork-Id: 13840225 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2FA101DED76 for ; Thu, 17 Oct 2024 15:24:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178672; cv=none; b=NhW50ziqbAc+O70liMyLh9ZlRGBYDlxKRwLJ3k0ZnPUJSHKuTb+jSsjTgR5Z1+4k+HO8//OmjUnZHvUoTQ3Pcqf2Qsh/zCh8n2nebdslwYUdMv7bgckQmyUj3HI137XYjAuuXM1bOUnw7HzkPo7rm3NqxyHaZejZhWbYWawzBr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178672; c=relaxed/simple; bh=vddhhLfJFoNR/Pudv6TWiHyZGFLfBiLpicAF55+Y+o4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uh8XkWB7SYULYHoUfLaZnoJGE/7iS0i5gU1f/4FnhQLbuku0P8uH2fB9wyo+P0z+8u8yYuLbXwXDww1YKJEe0WtQFbqaqXvlD6iC+JwK1iDURTQm2IgSaF3wmbpMYqaOEQEsd2mfyv0yoIA3ODGjOJAX/yrA4oLKjYrJOk/oOdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gCaGz+jo; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gCaGz+jo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 699E3C4CECD; Thu, 17 Oct 2024 15:24:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729178671; bh=vddhhLfJFoNR/Pudv6TWiHyZGFLfBiLpicAF55+Y+o4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gCaGz+jomWGz32HCS5UZGFVM9ueBGgbjE3iUCl6OpgBEC5Zppy+KIytIgOIqsQJgI SgM5SITMfkmDPYpaG76O7KccLKKlaOOtfOH3Cnzgi3aIKyzXc0m1n3OJ34/VGcplVr WdJXtC0FqpeYEqJxgE4/fDSgwzqvWPovGuv2ok+HaILqnqZJ0scRRxlCyVcw6DrALJ MNhLv8fhW7OeHpfcZu9b+uffubr+9sj715sNQCcYzkCjHMtR0kXYZHNsbs+kO0CzmO romxaIp5ZP651pap3QPioIXDa+lxa7ZDu6mFbzNwpOmsSewAseRr09CFlAmWzo0en1 soIoyGmsbIB+A== From: Antoine Tenart To: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, edumazet@google.com Cc: Antoine Tenart , netdev@vger.kernel.org Subject: [PATCH net-next 2/3] net: sysctl: do not reserve an extra char in dump_cpumask temporary buffer Date: Thu, 17 Oct 2024 17:24:18 +0200 Message-ID: <20241017152422.487406-3-atenart@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241017152422.487406-1-atenart@kernel.org> References: <20241017152422.487406-1-atenart@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org When computing the length we'll be able to use out of the buffers, one char is removed from the temporary one to make room for a newline. It should be removed from the output buffer length too, but in reality this is not needed as the later call to scnprintf makes sure a null char is written at the end of the buffer which we override with the newline. Signed-off-by: Antoine Tenart --- net/core/sysctl_net_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c index e7c0121dfaa1..8dc07f7b1772 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -62,7 +62,7 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, return; } - len = min(sizeof(kbuf) - 1, *lenp); + len = min(sizeof(kbuf), *lenp); len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask)); if (!len) { *lenp = 0; From patchwork Thu Oct 17 15:24:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antoine Tenart X-Patchwork-Id: 13840226 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C9D421DEFD2 for ; Thu, 17 Oct 2024 15:24:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178674; cv=none; b=PdQNGym+Oi1kbW1ha8yjN6BlbxyzjgKF6UHpeUbrxXZEvUxnEYtrvx3CCe1rmdBCocZiBSiPgKM/d/OokTwrMgg//ApYgR4GG+Gqq9QJQhLmn0rhbJ8099NHXJEiXfiSfFVh5clsYOjq3+GltMDZAIKeT7odrgFC7ru83Vbtfv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729178674; c=relaxed/simple; bh=Bb+72Sy2zMBwPYXqRH2QU/V370T/6jS4805nMXZEIAw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eL88kJ8fFVm/j9zSzqOFtYPHb/y3FdKvQ6Z0kfftoykNUlboge56PEekhmsAiXiFQ43UbpVs+fWbrDxbi617ZXlXpcbc9KEObX6C4Dkztt3VfDG44n4fjLh9v/XMyc13EQIAVLChACYYxKiFCjnCjTdDnjd0jWGM7WndHrkhD8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J8d/GOt/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="J8d/GOt/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 056ABC4CEC3; Thu, 17 Oct 2024 15:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729178674; bh=Bb+72Sy2zMBwPYXqRH2QU/V370T/6jS4805nMXZEIAw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J8d/GOt/R+2jirYIgCOBd2ZyvTM8QMC587spTQGMR1kI1rfsS4JkHiZ2ZJgnowQIt ewAaG7wFkjfjGm7GzApZ9bNYTnHrXOQgzUtxZ2ijDKFYUsGuKFDdu9Lk0ASHE01v3U J2cySFGzavavGvIylffcPXo+50nuRdjfOoO/fJV/TpXv2dq0+j3y+WuHk7e55liqK7 Syc+34oJfw1iQl3Re9OrOI8vUBy9/JW1qXHfNQM0Yy07al6J15jy0/E9kx+A9zbYS7 EbrhlR/SjGo4muRAvWDslkfFa86N+T2y9qzWNcIdtJA3eB9UdeRg35z5FYi89QZOMQ QHLGfSQDXPAwQ== From: Antoine Tenart To: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, edumazet@google.com Cc: Antoine Tenart , netdev@vger.kernel.org Subject: [PATCH net-next 3/3] net: sysctl: allow dump_cpumask to handle higher numbers of CPUs Date: Thu, 17 Oct 2024 17:24:19 +0200 Message-ID: <20241017152422.487406-4-atenart@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241017152422.487406-1-atenart@kernel.org> References: <20241017152422.487406-1-atenart@kernel.org> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org This fixes the output of rps_default_mask and flow_limit_cpu_bitmap when the CPU count is > 448, as it was truncated. The underlying values are actually stored correctly when writing to these sysctl but displaying them uses a fixed length temporary buffer in dump_cpumask. This buffer can be too small if the CPU count is > 448. Fix this by dynamically allocating the buffer in dump_cpumask, using a guesstimate of what we need. Signed-off-by: Antoine Tenart Reviewed-by: Simon Horman Reviewed-by: Simon Horman --- net/core/sysctl_net_core.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c index 8dc07f7b1772..cb8d32e5c14e 100644 --- a/net/core/sysctl_net_core.c +++ b/net/core/sysctl_net_core.c @@ -51,22 +51,32 @@ int sysctl_devconf_inherit_init_net __read_mostly; EXPORT_SYMBOL(sysctl_devconf_inherit_init_net); #if IS_ENABLED(CONFIG_NET_FLOW_LIMIT) || IS_ENABLED(CONFIG_RPS) -static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, - struct cpumask *mask) +static int dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, + struct cpumask *mask) { - char kbuf[128]; + char *kbuf; int len; if (*ppos || !*lenp) { *lenp = 0; - return; + return 0; + } + + /* CPUs are displayed as a hex bitmap + a comma between each groups of 8 + * nibbles (except the last one which has a newline instead). + * Guesstimate the buffer size at the group granularity level. + */ + len = min(DIV_ROUND_UP(nr_cpumask_bits, 32) * (8 + 1), *lenp); + kbuf = kmalloc(len, GFP_KERNEL); + if (!kbuf) { + *lenp = 0; + return -ENOMEM; } - len = min(sizeof(kbuf), *lenp); len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask)); if (!len) { *lenp = 0; - return; + goto free_buf; } /* scnprintf writes a trailing null char not counted in the returned @@ -76,6 +86,10 @@ static void dump_cpumask(void *buffer, size_t *lenp, loff_t *ppos, memcpy(buffer, kbuf, len); *lenp = len; *ppos += len; + +free_buf: + kfree(kbuf); + return 0; } #endif @@ -119,8 +133,8 @@ static int rps_default_mask_sysctl(const struct ctl_table *table, int write, if (err) goto done; } else { - dump_cpumask(buffer, lenp, ppos, - net->core.rps_default_mask ? : cpu_none_mask); + err = dump_cpumask(buffer, lenp, ppos, + net->core.rps_default_mask ? : cpu_none_mask); } done: @@ -249,7 +263,7 @@ static int flow_limit_cpu_sysctl(const struct ctl_table *table, int write, } rcu_read_unlock(); - dump_cpumask(buffer, lenp, ppos, mask); + ret = dump_cpumask(buffer, lenp, ppos, mask); } done: