linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: kfatyuip@gmail.com
To: thomas.lendacky@amd.com, john.allen@amd.com,
	herbert@gondor.apana.org.au, davem@davemloft.net
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Kieran Moy <kfatyuip@gmail.com>
Subject: [PATCH] crypto: ccp/sfs - Use DIV_ROUND_UP for set_memory_uc() size calculation
Date: Wed,  1 Oct 2025 15:58:20 +0800	[thread overview]
Message-ID: <20251001075820.185748-1-kfatyuip@gmail.com> (raw)

From: Kieran Moy <kfatyuip@gmail.com>

The SFS driver allocates a 2MB command buffer using snp_alloc_hv_fixed_pages(),
but set_memory_uc() is called with SFS_NUM_PAGES_CMDBUF which assumes 4KB pages.
This mismatch could lead to incomplete cache attribute updates on the buffer if
the payload size is not strictly page-aligned.

Switch to using DIV_ROUND_UP(SFS_MAX_PAYLOAD_SIZE, PAGE_SIZE) to calculate the
number of pages required for the attribute update. This approach follows kernel
coding best practices, improves code robustness, and ensures that all buffer
regions are properly covered regardless of current or future PAGE_SIZE values.

Using DIV_ROUND_UP is also consistent with Linux kernel style for page counting,
which avoids hidden bugs in case the payload size ever changes and is not a
multiple of PAGE_SIZE, or if the kernel is built with a non-default page size.

Signed-off-by: Kieran Moy <kfatyuip@gmail.com>
---
 drivers/crypto/ccp/sfs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/sfs.c b/drivers/crypto/ccp/sfs.c
index 2f4beaafe7ec..3397895160c0 100644
--- a/drivers/crypto/ccp/sfs.c
+++ b/drivers/crypto/ccp/sfs.c
@@ -277,7 +277,8 @@ int sfs_dev_init(struct psp_device *psp)
 	/*
 	* SFS command buffer must be mapped as non-cacheable.
 	*/
-	ret = set_memory_uc((unsigned long)sfs_dev->command_buf, SFS_NUM_PAGES_CMDBUF);
+	ret = set_memory_uc((unsigned long)sfs_dev->command_buf,
+			   DIV_ROUND_UP(SFS_MAX_PAYLOAD_SIZE, PAGE_SIZE));
 	if (ret) {
 		dev_dbg(dev, "Set memory uc failed\n");
 		goto cleanup_cmd_buf;
-- 
2.51.0

             reply	other threads:[~2025-10-01  7:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-01  7:58 kfatyuip [this message]
2025-10-01 14:44 ` [PATCH] crypto: ccp/sfs - Use DIV_ROUND_UP for set_memory_uc() size calculation Ashish Kalra
2025-10-01 17:49   ` kfatyuip

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251001075820.185748-1-kfatyuip@gmail.com \
    --to=kfatyuip@gmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=john.allen@amd.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).