Skip to contentSkip to conversation

scsi: aacraid: free aborted management FIBs on late completion

RFC PATCH In review Benjamin Saiz 2 participants 3 messages Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 scsi: aacraid: free aborted management FIBs on late completion

When a signal interrupts a process waiting in aac_fib_send() for a management command (for example FSACTL_SEND_FIB from arcconf, maxView or smartctl), aac_fib_send() marks the FIB as abandoned (done = 2) and returns -ERESTARTSYS. The ioctl handlers then deliberately keep the FIB, because the controller still owns it: ioctl_send_fib() only calls aac_fib_free() when retval != -ERESTARTSYS, and aac_send_raw_srb() does the same. The interrupt handler is expected to release the FIB when the late response arrives.

aac_intr_normal() handles that late response but only calls aac_fib_complete(); it never calls aac_fib_free(). The FIB is therefore never returned to the free list. Since commit 6bf3b630d0a7 ("aacraid: SCSI blk tag support"), the free list only holds the AAC_NUM_MGT_FIB (8) management FIBs, so eight interrupted management commands exhaust it: aac_fib_alloc() returns NULL and every later management ioctl fails with -ENOMEM until the adapter is reset or the host is rebooted. management_fib_count is decremented normally, so the "No management Fibs Available" check never fires and nothing is logged.

The exhausted pool also prevents aac_send_shutdown() from allocating a FIB, so VM_CloseAll is silently not sent at shutdown.

Observed on an Adaptec 6805 (Linux 5.10) with kprobes on aac_fib_send(), aac_fib_complete() and aac_fib_free(). Before the test, 1000 management commands all used the same FIB. arcconf was then killed (SIGKILL) while waiting for a ContainerCommand:

54803.925692 arcconf-1091331, CPU 11: aac_fib_send() returns -512 54803.927570 <idle>-0, CPU 12, hardirq:

               aac_fib_complete(fib=0xffff96570a8e7b00)

The late response arrived 1.9 ms later in hard interrupt context and completed the FIB, but aac_fib_free() was never called for it. The next 812 management commands all used another FIB; 0xffff96570a8e7b00 was never allocated again.

Commit cacb6dc3d7fe ("[SCSI] aacraid: fix File System going into read-only mode") introduced the done = 2 handshake and freed the FIB in both aac_response_normal() and aac_intr_normal(). Commit 6bf3b630d0a7 removed the aac_fib_free() calls from aac_intr_normal(), as I/O FIBs became indexed by block layer tags and must not be put on the management free list. The abandoned-FIB path, however, only ever sees management FIBs: every synchronous sender (wait != 0, no callback) gets its FIB from aac_fib_alloc(). Commit 423400e64d37 ("scsi: aacraid: Include HBA direct interface") restored aac_fib_free() for the callback-less async case but not for this one. aac_response_normal() still frees the FIB.

Free the FIB after completing it in both the native HBA and the regular branch. done is set to 1 before the call, so the early return in aac_fib_free() for done == 2 does not apply.

The issue was found, and this patch and its changelog were drafted, with the help of an AI coding assistant (Claude). I reviewed the analysis and the change and take responsibility for them.

Fixes 6bf3b630d0a7 ("aacraid: SCSI blk tag support")

---
RFC because the fix itself is only build-tested (x86_64, v7.0 headers, W=1, no new warnings). The leak was reproduced on the affected host as described above, but that host is a production NAS (Adaptec 6805, Debian 11, Linux 5.10) and the patched module was not loaded there.

Every synchronous sender (wait != 0, no callback) in aachba.c, commctrl.c, commsup.c and comminit.c gets its FIB from aac_fib_alloc(), so the done == 2 path cannot return a block layer tagged FIB to the management free list.

For reference, Microchip's out-of-tree driver 1.2.1-60001 frees the FIB at the same place (both done == 2 branches of aac_intr_normal()); its aac_fib_free() only returns pool FIBs to the free list.

Not addressed here: on -ERESTARTSYS, aac_send_raw_srb() also strands its SG buffers and DMA mappings (bugzilla 220504).

 drivers/scsi/aacraid/dpcsup.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/scsi/aacraid/dpcsup.c +6 -2
index 169c41d..a965f18 100644
@@ -379,8 +379,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
 					mflags);
 
 				FIB_COUNTER_INCREMENT(aac_config.NativeRecved);
-				if (completed)
+				if (completed) {
 					aac_fib_complete(fib);
+					aac_fib_free(fib);
+				}
 			}
 		} else {
 			struct hw_fib *hwfib = fib->hw_fib_va;
@@ -432,8 +434,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
 					mflags);
 
 				FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
-				if (completed)
+				if (completed) {
 					aac_fib_complete(fib);
+					aac_fib_free(fib);
+				}
 			}
 		}
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] scsi: aacraid: race on fib->done in aac_hba_send() and aac_intr_normal() - [Medium] scsi: aacraid…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] scsi: aacraid: race on fib->done in aac_hba_send() and aac_intr_normal()
- [Medium] scsi: aacraid: double free on -ERESTARTSYS in aac_send_safw_bmic_cmd()
--

commit bdfff41a3eec2c252b35e5b8c632cb03cf063779
Author: Benjamin Saiz <[email protected]>

scsi: aacraid: free aborted management FIBs on late completion

This commit calls aac_fib_free() in aac_intr_normal() when completing a management FIB whose caller was interrupted and marked fib->done == 2.

Quoted 6 lines
drivers/scsi/aacraid/dpcsup.c +0 -0
index 169c41d080a15..a965f18fe4849 100644
@@ -379,8 +379,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
 					mflags);

drivers/scsi/aacraid/dpcsup.c unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif, · line 388

Quoted 10 lines
drivers/scsi/aacraid/dpcsup.c +3 -1
 
 				FIB_COUNTER_INCREMENT(aac_config.NativeRecved);
-				if (completed)
+				if (completed) {
 					aac_fib_complete(fib);
+					aac_fib_free(fib);
+				}
 			}
 		} else {
 			struct hw_fib *hwfib = fib->hw_fib_va;

[Severity: Medium]
Can freeing fib here cause a use-after-free and double free when a synchronous native HBA command in aac_hba_send() is interrupted by a signal?

In aac_hba_send(), fibptr->done = 2 is written outside fibptr->event_lock:

drivers/scsi/aacraid/commsup.c:aac_hba_send() {
    ...
		if (wait_for_completion_interruptible(&fibptr->event_wait))
			fibptr->done = 2;
		fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT);

		spin_lock_irqsave(&fibptr->event_lock, flags);
		if ((fibptr->done == 0) || (fibptr->done == 2)) {
			fibptr->done = 2; /* Tell interrupt we aborted */
			spin_unlock_irqrestore(&fibptr->event_lock, flags);
			return -ERESTARTSYS;
		}
		spin_unlock_irqrestore(&fibptr->event_lock, flags);
    ...
}

If the completion interrupt arrives between the unlocked assignment and spin_lock_irqsave(&fibptr->event_lock, flags), both sides free the FIB:

CPU0 CPU1 (<hardirq>)
aac_send_raw_srb()
aac_hba_send()

    wait_for_completion_interruptible()
      /* interrupted by signal */
    fibptr->done = 2;
                                        aac_intr_normal()
                                          lock(&fib->event_lock)
                                          if (fib->done == 2) {
                                            fib->done = 1;
                                            completed = 1;
                                          }
                                          unlock(&fib->event_lock)
                                          aac_fib_complete(fib)
                                          aac_fib_free(fib)
    lock(&fibptr->event_lock)
    /* fibptr->done is 1, returns 0 */
    unlock(&fibptr->event_lock)

/* reads freed srbfib */ <-- use-after-free
if (rcode != -ERESTARTSYS)
aac_fib_free(srbfib) <-- double free

Because fibptr->done is now 1, the early return for done == 2 in aac_fib_free() does not trigger, pushing srbfib onto dev->free_fib twice and corrupting the management FIB free list into a cycle.

Also, unlike aac_fib_send(), neither aac_fib_alloc() nor aac_hba_send() initializes fibptr->done = 0, so recycled FIBs enter aac_hba_send() with fibptr->done == 1. If aac_intr_normal() runs after wait_for_completion_interruptible() is interrupted but before fibptr->done = 2 is written, aac_intr_normal() sees fib->done != 2 and does not free the FIB, while aac_hba_send() overwrites fibptr->done = 2 and returns -ERESTARTSYS, leaking the FIB.

drivers/scsi/aacraid/dpcsup.c unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif, · line 443

Quoted 12 lines
drivers/scsi/aacraid/dpcsup.c +3 -1
@@ -432,8 +434,10 @@ unsigned int aac_intr_normal(struct aac_dev *dev, u32 index, int isAif,
 					mflags);
 
 				FIB_COUNTER_INCREMENT(aac_config.NormalRecved);
-				if (completed)
+				if (completed) {
 					aac_fib_complete(fib);
+					aac_fib_free(fib);
+				}
 			}
 		}
 

[Severity: Medium]
Does this double free fibptr when aac_fib_send() is interrupted by a signal in aac_send_safw_bmic_cmd()?

Unlike ioctl_send_fib() and aac_send_raw_srb(), aac_send_safw_bmic_cmd() translates -ERESTARTSYS to -ERESTART and unconditionally cleans up:

drivers/scsi/aacraid/aachba.c:aac_send_safw_bmic_cmd() {
    ...
	if (rcode == -ERESTARTSYS)
		rcode = -ERESTART;

	if (unlikely(rcode < 0))
		goto bmic_error;
    ...
bmic_error:
	dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
fib_error:
	aac_fib_complete(fibptr);
	aac_fib_free(fibptr);
    ...
}

Previously, the early return for fibptr->done == 2 in aac_fib_complete() and aac_fib_free() made those calls no-ops after an interrupted aac_fib_send(). With this change, if aac_intr_normal() completes the abandoned FIB before aac_send_safw_bmic_cmd() reaches aac_fib_free():

CPU0 CPU1 (<hardirq>)
aac_send_safw_bmic_cmd()
aac_fib_send()

    /* interrupted, sets done = 2 */
    return -ERESTARTSYS;
                                        aac_intr_normal()
                                          if (fib->done == 2) {
                                            fib->done = 1;
                                            completed = 1;
                                          }
                                          aac_fib_complete(fib)
                                          aac_fib_free(fib)

dma_unmap_single(...)
aac_fib_complete(fibptr)
aac_fib_free(fibptr) <-- double free

fibptr->done is already reset to 1, so aac_fib_free() does not return early and pushes fibptr onto dev->free_fib a second time.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Thanks for the review. Both findings are correct, so please do not apply this RFC as is.

Before a v2, I would like to agree on the direction, because the fix touches the ownership of abandoned FIBs in several callers and I can only test one of them. The question is at the end; the findings behind it come first.

I had the RFC and the surrounding code reviewed again, and checked each item below against the v7.4-rc4 sources (file:line refer to drivers/scsi/aacraid/ in v7.4-rc4).

Made worse by the RFC on controllers handled by aac_intr_normal() (a second free, or a write to a FIB the interrupt handler may already have freed and handed out again):

1. aac_hba_send(): done = 2 is written outside event_lock (commsup.c:761-762), and done is never reset before delivery (only aac_fib_send() does it, commsup.c:561). This is the pattern commit cacb6dc3d7fe ("[SCSI] aacraid: fix File System going into read-only mode") removed from aac_fib_send(); commit 423400e64d37 ("scsi: aacraid: Include HBA direct interface") reintroduced it. Dan Carpenter flagged this block in 2017:
https://lore.kernel.org/all/20170213184101.GA4762@mwanda/
Without the RFC, a stale done == 1 already leaks the FIB when the reply arrives before the unlocked write, as the review notes.

2. aac_send_safw_bmic_cmd(): completes and frees the FIB unconditionally after -ERESTARTSYS (aachba.c:1736-1753). This was harmless while done == 2 turned both calls into no-ops. Commit 8fb391827f57 ("scsi: aacraid: Create bmic submission function from bmic identify") added the -ERESTARTSYS to -ERESTART conversion, and commit 5480aa18375e ("scsi: aacraid: Change phy luns function to use common bmic function") dropped the -ERESTARTSYS check its old caller had.

3. ioctl_send_fib() with a replaced hw_fib (size > max_fib_size):
on -ERESTARTSYS the cleanup frees the coherent buffer and restores hw_fib_va/hw_fib_pa (commctrl.c:80-102 and 146-155). With the RFC that FIB may already be back on the free list.

4. aac_get_adapter_info(): if both DMA mask settings fail, rcode = -ENOMEM overwrites the -ERESTARTSYS of GetBusInfo before the final cleanup, which then frees the FIB (aachba.c:2238-2248, 2306-2312). This needs both dma_set_mask() calls to fail, so it is unlikely.

Controllers whose replies go through aac_response_normal() (rx.c, sa.c) already free abandoned FIBs unconditionally today (dpcsup.c:126-132), so items 3 and 4 already apply there.

Pre-existing, independent of the RFC:

5. fibptr->event_wait is initialised once (commsup.c:169) and never re-initialised. If the reply posts complete() while the waiter is leaving on a signal (done already 1), that completion is never consumed and may wake the next user of the FIB early. The same applies to the polled and sync_mode paths.

6. Block layer tagged FIBs reach aac_fib_free() on error paths (for example aachba.c:2490-2507, 2598-2599, 2686-2687, and dpcsup.c:346-349), so the management free list does not only hold the 8 pool FIBs. My changelog was wrong on that point, and also in saying that cacb6dc3d7fe introduced done = 2: it moved the abort marking under event_lock.

7. Buffers released while the controller may still use them: the BMIC buffer (dma_unmap_single(), aachba.c:1749, then freed by the callers), the wellness buffer (dma_free_coherent() right after the send, commsup.c:2207), and the replaced ioctl hw_fib (item 3). Raw SRB instead strands its SG buffers and leaves its DMA mappings unmapped (bugzilla 220504).

8. Polled sends (wait < 0) that time out or see the adapter offline (commsup.c:640-662), and sync_mode on a signal (commsup.c:604-610), return after delivery without marking the FIB as abandoned, and their callers free it.

I see two directions:

(a) An explicit, scoped ownership handover, for controllers handled by aac_intr_normal(). ioctl_send_fib(), on its plain path only (no replaced hw_fib), marks the FIB before delivery as handed over on abort, in a dedicated field rather than in fib->flags, which is updated outside the lock. aac_fib_alloc() clears the mark, and so does aac_fib_alloc_tag(), since tagged FIBs reach the free list (item 6). On a late reply, aac_intr_normal() decides under event_lock whether done == 2 and the FIB is marked, then completes and frees it once. The completion is re-initialised before each synchronous send. This fixes the leak I observed (plain FSACTL_SEND_FIB on a Series 6). Every other path keeps its current behaviour on these controllers: it leaks an abandoned FIB but never frees it twice. A FIB whose reply never arrives stays lost until an adapter reset, as today.

(b) A series that first fixes each path above (aac_hba_send() locking and done reset; ownership in BMIC, GetBusInfo and the replaced ioctl hw_fib; buffer lifetime; items 5 and 8), then frees abandoned FIBs for every caller, in aac_intr_normal() as aac_response_normal() already does.

For reference, the Microchip out-of-tree driver 1.2.1-60001 frees the FIB in both done == 2 branches of aac_intr_normal() and only returns pool FIBs to the free list. It marks done = 2 under the lock when a polled send times out. But it keeps the same aac_hba_send() and BMIC patterns as mainline, and an unlocked done = 2 in aac_fib_send() too.

Testing: my only affected controller is an Adaptec 6805 in production. I can observe the driver there with kprobes, but I cannot load a modified driver on it. I have no hardware for the native HBA or SA firmware paths.

Which direction would you accept? Sagar, could Microchip test the native HBA and SA paths on Series 7/8 hardware if we go for (b)?

Unless someone prefers (b), I plan to send (a) as v2 in a week or two, with the remaining items listed in the cover letter.

This analysis was done with the help of AI coding assistants (Claude, and Codex for an independent review). I checked each item against the v7.4-rc4 sources.

Benjamin