[PATCH net 2/4] nfc: st95hf: use DMA-safe buffers for SPI transfers

From: Elkin Cruz

Date: Wed Nov 25 2026 - 17:25:08 EST


st95hf_spi_send(), st95hf_spi_recv_response(), and
st95hf_spi_recv_echo_res() pass stack-allocated buffers from core.c and
spi.c directly to spi_sync(), violating kernel DMA safety requirements
on DMA-capable SPI controllers and CONFIG_VMAP_STACK kernels.

Use kmemdup() in st95hf_spi_send(), spi_write_then_read() in
st95hf_spi_recv_echo_res(), and DMA-cacheline-isolated cmd_buf and
rx_buf members (via __dma_from_device_group_begin/end) in
struct st95hf_spi_context for st95hf_spi_recv_response().

This issue was found while trying out new Clang static analysis checks for
kernel bug classes. The finding was triaged and fixed, and this commit
description was drafted with the help of AI, then verified by code
inspection and compile-tested against linux-next (not reproduced and not
runtime-tested on hardware).

Fixes: cab47333f0f7 ("NFC: Add STMicroelectronics ST95HF driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
drivers/nfc/st95hf/spi.c | 52 +++++++++++++++++++++-------------------
drivers/nfc/st95hf/spi.h | 10 ++++++++
2 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/drivers/nfc/st95hf/spi.c b/drivers/nfc/st95hf/spi.c
index a6599d65a3bf..8bd7eccc34d3 100644
--- a/drivers/nfc/st95hf/spi.c
+++ b/drivers/nfc/st95hf/spi.c
@@ -14,13 +14,18 @@ int st95hf_spi_send(struct st95hf_spi_context *spicontext,
int datalen,
enum req_type reqtype)
{
- struct spi_message m;
- int result = 0;
struct spi_device *spidev = spicontext->spidev;
struct spi_transfer tx_transfer = {
- .tx_buf = buffertx,
.len = datalen,
};
+ unsigned char *tx_buf;
+ struct spi_message m;
+ int result = 0;
+
+ tx_buf = kmemdup(buffertx, datalen, GFP_KERNEL);
+ if (!tx_buf)
+ return -ENOMEM;
+ tx_transfer.tx_buf = tx_buf;

mutex_lock(&spicontext->spi_lock);

@@ -35,6 +40,7 @@ int st95hf_spi_send(struct st95hf_spi_context *spicontext,
spi_message_add_tail(&tx_transfer, &m);

result = spi_sync(spidev, &m);
+ kfree(tx_buf);
if (result) {
dev_err(&spidev->dev, "error: sending cmd to st95hf using SPI = %d\n",
result);
@@ -69,12 +75,11 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
unsigned char *receivebuff,
size_t max_len)
{
- unsigned char readdata_cmd = ST95HF_COMMAND_RECEIVE;
struct spi_device *spidev = spicontext->spidev;
struct spi_transfer tx_takedata;
struct spi_transfer t[2] = {
- { .tx_buf = &readdata_cmd, .len = 1 },
- { .rx_buf = receivebuff, .len = 2, .cs_change = 1 },
+ { .len = 1 },
+ { .len = 2, .cs_change = 1 },
};
struct spi_message m;
int len = 0;
@@ -85,6 +90,10 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,

mutex_lock(&spicontext->spi_lock);

+ spicontext->cmd_buf = ST95HF_COMMAND_RECEIVE;
+ t[0].tx_buf = &spicontext->cmd_buf;
+ t[1].rx_buf = spicontext->rx_buf;
+
/* First spi transfer to know the length of valid data */
spi_message_init(&m);
spi_message_add_tail(&t[0], &m);
@@ -102,12 +111,13 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,
len = 2;

/* Support of long frame */
- if (receivebuff[0] & 0x60)
- len += (((receivebuff[0] & 0x60) >> 5) << 8) | receivebuff[1];
+ if (spicontext->rx_buf[0] & 0x60)
+ len += (((spicontext->rx_buf[0] & 0x60) >> 5) << 8) |
+ spicontext->rx_buf[1];
else
- len += receivebuff[1];
+ len += spicontext->rx_buf[1];

- if (len > max_len) {
+ if (len > max_len || len > ST95HF_MAX_RESPONSE_BUFFER_SIZE) {
dev_err(&spidev->dev, "spi_recv_resp, invalid response len = %d\n",
len);
memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
@@ -120,21 +130,23 @@ int st95hf_spi_recv_response(struct st95hf_spi_context *spicontext,

/* Now make a transfer to read only relevant bytes */
memset(&tx_takedata, 0x0, sizeof(struct spi_transfer));
- tx_takedata.rx_buf = &receivebuff[2];
+ tx_takedata.rx_buf = &spicontext->rx_buf[2];
tx_takedata.len = len - 2;

spi_message_init(&m);
spi_message_add_tail(&tx_takedata, &m);

ret = spi_sync(spidev, &m);
-
- mutex_unlock(&spicontext->spi_lock);
if (ret) {
dev_err(&spidev->dev, "spi_recv_resp, data read error = %d\n",
ret);
+ mutex_unlock(&spicontext->spi_lock);
return ret;
}

+ memcpy(receivebuff, spicontext->rx_buf, len);
+ mutex_unlock(&spicontext->spi_lock);
+
return len;
}
EXPORT_SYMBOL_GPL(st95hf_spi_recv_response);
@@ -143,21 +155,11 @@ int st95hf_spi_recv_echo_res(struct st95hf_spi_context *spicontext,
unsigned char *receivebuff)
{
unsigned char readdata_cmd = ST95HF_COMMAND_RECEIVE;
- struct spi_transfer t[2] = {
- {.tx_buf = &readdata_cmd, .len = 1,},
- {.rx_buf = receivebuff, .len = 1,},
- };
- struct spi_message m;
struct spi_device *spidev = spicontext->spidev;
- int ret = 0;
+ int ret;

mutex_lock(&spicontext->spi_lock);
-
- spi_message_init(&m);
- spi_message_add_tail(&t[0], &m);
- spi_message_add_tail(&t[1], &m);
- ret = spi_sync(spidev, &m);
-
+ ret = spi_write_then_read(spidev, &readdata_cmd, 1, receivebuff, 1);
mutex_unlock(&spicontext->spi_lock);

if (ret)
diff --git a/drivers/nfc/st95hf/spi.h b/drivers/nfc/st95hf/spi.h
index 840f55f82899..6fbace4412d9 100644
--- a/drivers/nfc/st95hf/spi.h
+++ b/drivers/nfc/st95hf/spi.h
@@ -9,6 +9,7 @@
#ifndef __LINUX_ST95HF_SPI_H
#define __LINUX_ST95HF_SPI_H

+#include <linux/dma-mapping.h>
#include <linux/spi/spi.h>

/* Basic ST95HF SPI CMDs */
@@ -17,6 +18,7 @@
#define ST95HF_COMMAND_RECEIVE 0x2

#define ST95HF_RESET_CMD_LEN 0x1
+#define ST95HF_MAX_RESPONSE_BUFFER_SIZE 280

/*
* structure to contain st95hf spi communication specific information.
@@ -25,12 +27,20 @@
* @done: completion structure to wait for st95hf response
* for synchronous calls.
* @spi_lock: mutex to allow only one spi transfer at a time.
+ * @cmd_buf: DMA-safe buffer for command byte.
+ * @rx_buf: DMA-safe buffer for response transfers.
*/
struct st95hf_spi_context {
bool req_issync;
struct spi_device *spidev;
struct completion done;
struct mutex spi_lock;
+ __dma_from_device_group_begin(cmd);
+ u8 cmd_buf;
+ __dma_from_device_group_end(cmd);
+ __dma_from_device_group_begin(rx);
+ u8 rx_buf[ST95HF_MAX_RESPONSE_BUFFER_SIZE];
+ __dma_from_device_group_end(rx);
};

/* flag to differentiate synchronous & asynchronous spi request */
--
2.56.1.839.g22a8290db9-goog