From: Ruoyu Wang <ruoyuw560@gmail.com>
To: "James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>,
"Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
Ruoyu Wang <ruoyuw560@gmail.com>
Subject: [PATCH] scsi: arcmsr: check XOR DMA allocations
Date: Mon, 8 Jun 2026 14:36:50 +0800 [thread overview]
Message-ID: <20260608063650.54-1-ruoyuw560@gmail.com> (raw)
arcmsr_alloc_xor_buffer() allocates the XOR metadata area with
dma_alloc_coherent() and then derives the XOR scatter-gather metadata from
the returned pointer. If the coherent allocation fails, the metadata setup
would dereference NULL.
Check the metadata allocation before using it. If a later XOR segment
allocation fails, release the partially allocated XOR buffers immediately
and clear the XOR state so the common CCB pool cleanup can safely release
only the CCB pool.
If XOR buffer setup fails after the CCB pool has been allocated, release
the CCB pool before returning the allocation error so the caller does not
leak the partially initialized resources.
Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>
---
drivers/scsi/arcmsr/arcmsr_hba.c | 70 +++++++++++++++++++-------------
1 file changed, 42 insertions(+), 28 deletions(-)
diff --git a/drivers/scsi/arcmsr/arcmsr_hba.c b/drivers/scsi/arcmsr/arcmsr_hba.c
index 8aa948f06cacf..c2ef752fd34c0 100644
--- a/drivers/scsi/arcmsr/arcmsr_hba.c
+++ b/drivers/scsi/arcmsr/arcmsr_hba.c
@@ -753,9 +753,39 @@ static bool arcmsr_alloc_io_queue(struct AdapterControlBlock *acb)
return rtn;
}
+static void arcmsr_free_xor_buffer(struct AdapterControlBlock *acb, int entries)
+{
+ struct Xor_sg *xor_phys;
+ void **xor_virt;
+ int i;
+
+ if (!acb->xorVirt)
+ return;
+ xor_phys = (struct Xor_sg *)(acb->xorVirt +
+ sizeof(struct HostRamBuf));
+ xor_virt = (void **)((unsigned long)acb->xorVirt +
+ (unsigned long)acb->xorVirtOffset);
+ for (i = 0; i < entries; i++) {
+ if (xor_phys->xorPhys) {
+ dma_free_coherent(&acb->pdev->dev,
+ ARCMSR_XOR_SEG_SIZE,
+ *xor_virt, xor_phys->xorPhys);
+ xor_phys->xorPhys = 0;
+ *xor_virt = NULL;
+ }
+ xor_phys++;
+ xor_virt++;
+ }
+ dma_free_coherent(&acb->pdev->dev, acb->init2cfg_size,
+ acb->xorVirt, acb->xorPhys);
+ acb->xorVirt = NULL;
+ acb->xorPhys = 0;
+ acb->xorVirtOffset = 0;
+ acb->xor_mega = 0;
+}
+
static int arcmsr_alloc_xor_buffer(struct AdapterControlBlock *acb)
{
- int rc = 0;
struct pci_dev *pdev = acb->pdev;
void *dma_coherent;
dma_addr_t dma_coherent_handle;
@@ -771,6 +801,10 @@ static int arcmsr_alloc_xor_buffer(struct AdapterControlBlock *acb)
(sizeof(struct XorHandle) * acb->xor_mega);
dma_coherent = dma_alloc_coherent(&pdev->dev, acb->init2cfg_size,
&dma_coherent_handle, GFP_KERNEL);
+ if (!dma_coherent) {
+ acb->xor_mega = 0;
+ return -ENOMEM;
+ }
acb->xorVirt = dma_coherent;
acb->xorPhys = dma_coherent_handle;
pXorPhys = (struct Xor_sg *)((unsigned long)dma_coherent +
@@ -792,8 +826,8 @@ static int arcmsr_alloc_xor_buffer(struct AdapterControlBlock *acb)
} else {
pr_info("arcmsr%d: alloc max XOR buffer = 0x%x MB\n",
acb->host->host_no, i);
- rc = -ENOMEM;
- break;
+ arcmsr_free_xor_buffer(acb, i);
+ return -ENOMEM;
}
}
pRamBuf = (struct HostRamBuf *)acb->xorVirt;
@@ -801,7 +835,7 @@ static int arcmsr_alloc_xor_buffer(struct AdapterControlBlock *acb)
pRamBuf->hrbSize = i * ARCMSR_XOR_SEG_SIZE;
pRamBuf->hrbRes[0] = 0;
pRamBuf->hrbRes[1] = 0;
- return rc;
+ return 0;
}
static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
@@ -895,8 +929,10 @@ static int arcmsr_alloc_ccb_pool(struct AdapterControlBlock *acb)
break;
}
if ((acb->firm_PicStatus >> 24) & 0x0f) {
- if (arcmsr_alloc_xor_buffer(acb))
+ if (arcmsr_alloc_xor_buffer(acb)) {
+ arcmsr_free_ccb_pool(acb);
return -ENOMEM;
+ }
}
return 0;
}
@@ -2083,29 +2119,7 @@ static void arcmsr_stop_adapter_bgrb(struct AdapterControlBlock *acb)
static void arcmsr_free_ccb_pool(struct AdapterControlBlock *acb)
{
- if (acb->xor_mega) {
- struct Xor_sg *pXorPhys;
- void **pXorVirt;
- int i;
-
- pXorPhys = (struct Xor_sg *)(acb->xorVirt +
- sizeof(struct HostRamBuf));
- pXorVirt = (void **)((unsigned long)acb->xorVirt +
- (unsigned long)acb->xorVirtOffset);
- for (i = 0; i < acb->xor_mega; i++) {
- if (pXorPhys->xorPhys) {
- dma_free_coherent(&acb->pdev->dev,
- ARCMSR_XOR_SEG_SIZE,
- *pXorVirt, pXorPhys->xorPhys);
- pXorPhys->xorPhys = 0;
- *pXorVirt = NULL;
- }
- pXorPhys++;
- pXorVirt++;
- }
- dma_free_coherent(&acb->pdev->dev, acb->init2cfg_size,
- acb->xorVirt, acb->xorPhys);
- }
+ arcmsr_free_xor_buffer(acb, acb->xor_mega);
dma_free_coherent(&acb->pdev->dev, acb->uncache_size, acb->dma_coherent, acb->dma_coherent_handle);
}
--
2.51.0
reply other threads:[~2026-06-08 6:36 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260608063650.54-1-ruoyuw560@gmail.com \
--to=ruoyuw560@gmail.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.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
all inboxes | Powered by JetHome®