From patchwork Mon Aug 17 15:55:37 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shibo Zhu <3499129952@qq.com> X-Patchwork-Id: 2287109 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=mnOgN14Y; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=O2PXTRUu; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hNy9d1jl6z1xsW for ; Tue, 18 Aug 2026 01:56:01 +1000 (AEST) 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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Date:Subject:To:From: Message-ID:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=4CcQZfxzKMtoWP2rY9bALUOq1MbqSxwInGp7jzn9InY=; b=mnOgN14YY5hnOe NWOFl6+xpjAuUPIhkgmZmplnSL1tfEMqgLXe0clidvK1AuI56Ekze9sL20JOb94nbsFU5WgDbNNg8 B22OCHiV3MLK1sFwwJ7bqMyWBZ6xc/RdVro7lfNi4Y/B4R/47ecS5ecZkESjcJambPaiK7k2u5FTr hYFjcz7NFTkHWD0HjcCcNmnKR6mPJ9fNeDn3J2VagukxYjt2UnZOGfP8tDGga6ERYRucZXRIEm+G+ r+DX2cjneoU1N5dPXkxEjrh3vDJ0QzlCPCvFdgWEniaj8TSvXOIgRrYblvU6ZWTh+pmXLr0b4veJI DijYnV/KG/MrpqPB/rMw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wvzgh-00000006SRP-17Yw; Mon, 17 Aug 2026 15:55:47 +0000 Received: from out162-62-58-211.mail.qq.com ([162.62.58.211]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wvzge-00000006SQZ-0vMO for opensbi@lists.infradead.org; Mon, 17 Aug 2026 15:55:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1786982139; bh=1Y/RooDRHcYbU8HxGfMb2V8ZET4ojeMkqsAcWNnKDmI=; h=From:To:Subject:Date; b=O2PXTRUuUmgZ88PGS/HJ9y0O0SR/zraIjya7liaPfOpOKpfr8V2t1PkMJrEuvlWBu SPeTHRFDh4EIiJOxvfdb4UcWGePyEEGfL7VP5wewfVjYsVYbQVPjjwvEaBmzTWohrt yAASaNePUXo5GnMtEpAJAMFd2Y9LO7rG2z0uvThg= Received: from zs-ASUS-TUF-Gaming-F16-FX607JV-FX607JV.. ([124.79.168.208]) by newxmesmtplogicsvrszc43-0.qq.com (NewEsmtp) with SMTP id DE62164D; Mon, 17 Aug 2026 23:55:38 +0800 X-QQ-mid: xmsmtpt1786982138tejc102sa Message-ID: X-QQ-XMAILINFO: Me8mHstNM5a8474/1RRJR5VMcpWvqAXOEzc2y/aL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xzsU2MFSTbtpZaxALnJCbzBc9 YPEIHyiDhC8aPl+TKro1Teyg2S9C0NjSkWx5cGDEWt3mNQPX8= X-QQ-XMRINFO: Nq+8W0+stu50tPAe92KXseR0ZZmBTk3gLg== From: Shibo Zhu <3499129952@qq.com> To: opensbi@lists.infradead.org Subject: [PATCH] lib: sbi_mpxy: Fix shared memory size rounding Date: Mon, 17 Aug 2026 23:55:37 +0800 X-OQ-MSGID: <20260817155537.115260-1-3499129952@qq.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260817_085544_789070_F5E3419C X-CRM114-Status: GOOD ( 15.89 ) X-Spam-Score: -1.8 (-) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: sbi_mpxy_register_channel() currently performs ceiling division by PAGE_SIZE but stores the resulting page count in mpxy_shmem_size. All consumers interpret mpxy_shmem_size as a byte count, so channel [...] Content analysis details: (-1.8 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [162.62.58.211 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [3499129952(at)qq.com] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit [3499129952(at)qq.com] 0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [162.62.58.211 listed in wl.mailspike.net] 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders -0.0 HELO_STATIC_HOST Relay HELO'd using static hostname -0.0 DMARC_PASS DMARC pass policy X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org sbi_mpxy_register_channel() currently performs ceiling division by PAGE_SIZE but stores the resulting page count in mpxy_shmem_size. All consumers interpret mpxy_shmem_size as a byte count, so channels requiring more than one page produce an undersized shared memory value. Use ROUNDUP() to keep the rounded value in bytes. Add SBIUnit coverage for page boundaries and ensure smaller channels do not reduce the shared memory size. Fixes: ec09918426a5 ("lib: sbi: Update MPXY framework and SBI extension as per latest spec") Signed-off-by: Shibo Zhu <3499129952@qq.com> Reviewed-by: Anup Patel --- lib/sbi/sbi_mpxy.c | 8 +++-- lib/sbi/tests/objects.mk | 2 ++ lib/sbi/tests/sbi_mpxy_test.c | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 lib/sbi/tests/sbi_mpxy_test.c diff --git a/lib/sbi/sbi_mpxy.c b/lib/sbi/sbi_mpxy.c index 19f59f3a..ed0e7807 100644 --- a/lib/sbi/sbi_mpxy.c +++ b/lib/sbi/sbi_mpxy.c @@ -257,8 +257,8 @@ int sbi_mpxy_register_channel(struct sbi_mpxy_channel *channel) /* Update shared memory size if required */ if (mpxy_shmem_size < channel->attrs.msg_data_maxlen) { - mpxy_shmem_size = channel->attrs.msg_data_maxlen; - mpxy_shmem_size = (mpxy_shmem_size + (PAGE_SIZE - 1)) / PAGE_SIZE; + mpxy_shmem_size = ROUNDUP(channel->attrs.msg_data_maxlen, + PAGE_SIZE); } sbi_list_add_tail(&channel->head, &mpxy_channel_list); @@ -803,3 +803,7 @@ int sbi_mpxy_get_notification_events(u32 channel_id, unsigned long *events_len) return SBI_SUCCESS; } + +#ifdef CONFIG_SBIUNIT +#include "tests/sbi_mpxy_test.c" +#endif diff --git a/lib/sbi/tests/objects.mk b/lib/sbi/tests/objects.mk index 40c441e1..888b4412 100644 --- a/lib/sbi/tests/objects.mk +++ b/lib/sbi/tests/objects.mk @@ -19,6 +19,8 @@ libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_math_test.o carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += ecall_test_suite libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_ecall_test.o +carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += mpxy_test_suite + carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitops_test_suite libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_bitops_test.o diff --git a/lib/sbi/tests/sbi_mpxy_test.c b/lib/sbi/tests/sbi_mpxy_test.c new file mode 100644 index 00000000..d96975b4 --- /dev/null +++ b/lib/sbi/tests/sbi_mpxy_test.c @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +#include + +static u32 mpxy_test_unused_channel_id(void) +{ + u32 channel_id = 0; + + while (sbi_mpxy_find_channel_any(channel_id)) + channel_id++; + + return channel_id; +} + +static void mpxy_shmem_size_test(struct sbiunit_test_case *test) +{ + static const struct { + u32 msg_data_maxlen; + unsigned long expected; + } cases[] = { + { 4095, PAGE_SIZE }, + { 4096, PAGE_SIZE }, + { 4097, 2 * PAGE_SIZE }, + { 8192, 2 * PAGE_SIZE }, + { 8193, 3 * PAGE_SIZE }, + }; + struct sbi_mpxy_channel channels[array_size(cases) + 1] = { 0 }; + unsigned long saved_size = mpxy_shmem_size; + unsigned long previous = PAGE_SIZE; + u32 i; + + mpxy_shmem_size = PAGE_SIZE; + + for (i = 0; i < array_size(cases); i++) { + channels[i].channel_id = mpxy_test_unused_channel_id(); + channels[i].owner_domain = sbi_domain_thishart_ptr(); + channels[i].attrs.msg_data_maxlen = cases[i].msg_data_maxlen; + + SBIUNIT_ASSERT_EQ(test, + sbi_mpxy_register_channel(&channels[i]), SBI_OK); + SBIUNIT_EXPECT_EQ(test, sbi_mpxy_get_shmem_size(), + cases[i].expected); + SBIUNIT_EXPECT(test, sbi_mpxy_get_shmem_size() >= previous); + previous = sbi_mpxy_get_shmem_size(); + } + + /* Registering a smaller channel must not decrease the shared memory. */ + channels[i].channel_id = mpxy_test_unused_channel_id(); + channels[i].owner_domain = sbi_domain_thishart_ptr(); + channels[i].attrs.msg_data_maxlen = 4095; + SBIUNIT_ASSERT_EQ(test, sbi_mpxy_register_channel(&channels[i]), SBI_OK); + SBIUNIT_EXPECT_EQ(test, sbi_mpxy_get_shmem_size(), previous); + + for (i = 0; i < array_size(channels); i++) + sbi_list_del_init(&channels[i].head); + mpxy_shmem_size = saved_size; +} + +static struct sbiunit_test_case mpxy_tests[] = { + SBIUNIT_TEST_CASE(mpxy_shmem_size_test), + SBIUNIT_END_CASE, +}; + +SBIUNIT_TEST_SUITE(mpxy_test_suite, mpxy_tests);