Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shivasharan S" <shivasharan.srikanteshwara@broadcom.com>,
	linux-scsi@vger.kernel.org
Subject: Re: [PATCH v2 16/37] scsi: mpt3sas: Fix page fault from stale ioc->chip after PCI error recovery
Date: Wed, 18 Nov 2026 11:01:47 +0000	[thread overview]
Message-ID: <sashiko-outbox-220782@kernel.org> (raw)
In-Reply-To: <1794995806-5835-17-git-send-email-shivasharan.srikanteshwara@broadcom.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] scsi: mpt3sas: NULL ioc->chip dereference in
  mpt3sas_scsih_issue_tm()
- [Medium] scsi: mpt3sas: NULL ioc->chip dereference in
  _ctl_ioctl_main() paths
- [Medium] scsi: mpt3sas: race on ioc->chip unmap in hard reset and
  config paths
--

commit a0d3f2d15f67ea6a7bb531279de17b869e022f10
Author: Shivasharan S <shivasharan.srikanteshwara@broadcom.com>

scsi: mpt3sas: Fix page fault from stale ioc->chip after PCI error recovery

This commit clears ioc->chip when unmapping controller resources and adds
NULL checks on ioc->chip across interrupt masking, IOC state queries, reset
handling, config requests, and shutdown paths.

> diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.c b/drivers/scsi/mpt3sas/mpt3sas_base.c
> index 24b74760d6816..cf7861a4068fc 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_base.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_base.c
[ ... ]
> @@ -3579,6 +3583,7 @@ mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
>  
>  	if (ioc->chip_phys) {
>  		iounmap(ioc->chip);
> +		ioc->chip = NULL;
>  		ioc->chip_phys = 0;
>  	}
>  

[Severity: Medium]
Can _ctl_ioctl_main() still reach ioc->put_smid_default() and dereference a
NULL ioc->chip when controller registers are unmapped?

In scsih_pci_slot_reset(), ioc->pci_error_recovery is cleared to 0 before
calling mpt3sas_base_map_resources(ioc); if mapping fails (or if the
adapter is suspended via scsih_suspend()), ioc->chip remains NULL while
ioc->pci_error_recovery, ioc->shost_recovery, ioc->is_driver_loading, and
ioc->remove_host are all 0.  _ctl_ioctl_main() only checks those four flags,
so ioctls such as MPT3EVENTENABLE (_ctl_eventenable() ->
mpt3sas_base_validate_event_type() -> _base_event_notification()) and
MPT3DIAGREADBUFFER with MPT3_FLAGS_REREGISTER (_ctl_diag_read_buffer())
proceed without checking ioc->chip or IOC state and call
ioc->put_smid_default(), which writes to ioc->chip registers.

[ ... ]
> @@ -6739,6 +6746,15 @@ mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
>  {
>  	u32 s, sc;
>  
> +	/*
> +	 * Registers are unmapped (e.g. after PCI error recovery/suspend).
> +	 * Return a state value that matches no real IOC state, so callers
> +	 * neither treat the IOC as FAULT (and try a register-based diag
> +	 * reset) nor as READY/OPERATIONAL.
> +	 */
> +	if (!ioc->chip)
> +		return MPI2_IOC_STATE_MASK;
> +
>  	s = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
>  	sc = s & MPI2_IOC_STATE_MASK;
>  	return cooked ? sc : s;

[Severity: High]
Does returning MPI2_IOC_STATE_MASK here cause mpt3sas_scsih_issue_tm() to
fall through and dereference a NULL ioc->chip during SCSI error recovery?

When in-flight SCSI commands time out after scsih_pci_slot_reset() clears
ioc->pci_error_recovery = 0 and fails in mpt3sas_base_map_resources(ioc),
the SCSI error handler runs eh_abort_handler, eh_device_reset_handler, and
eh_target_reset_handler before escalating to eh_host_reset_handler.  Those
callbacks invoke mpt3sas_scsih_issue_tm(), which only checks for
MPI2_DOORBELL_USED, MPI2_IOC_STATE_FAULT, and MPI2_IOC_STATE_COREDUMP:

drivers/scsi/mpt3sas/mpt3sas_scsih.c:mpt3sas_scsih_issue_tm() {
    ...
	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
	if (ioc_state & MPI2_DOORBELL_USED) {
		...
	}

	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
		...
	} else if ((ioc_state & MPI2_IOC_STATE_MASK) ==
	    MPI2_IOC_STATE_COREDUMP) {
		...
	}

	smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
	...
	ioc->put_smid_hi_priority(ioc, smid, msix_task);
    ...
}

Because MPI2_IOC_STATE_MASK matches none of those conditions,
mpt3sas_scsih_issue_tm() falls through and calls
ioc->put_smid_hi_priority(), dereferencing NULL ioc->chip (and scsih_abort()
also calls mpt3sas_halt_firmware(ioc) before checking ioc->chip).

> @@ -8136,7 +8152,7 @@ mpt3sas_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
>  
>  	dinitprintk(ioc, ioc_info(ioc, "%s\n", __func__));
>  
> -	if (ioc->pci_error_recovery)
> +	if (ioc->pci_error_recovery || !ioc->chip)
>  		return 0;
>  
>  	ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
> @@ -8969,6 +8985,13 @@ mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
>  		goto out_unlocked;
>  	}
>  
> +	if (!ioc->chip) {
> +		ioc_err(ioc, "%s: controller registers are not mapped\n",
> +			__func__);
> +		r = -ENODEV;
> +		goto out_unlocked;
> +	}
> +
>  	if (mpt3sas_fwfault_debug)
>  		mpt3sas_halt_firmware(ioc);
>  
> diff --git a/drivers/scsi/mpt3sas/mpt3sas_config.c b/drivers/scsi/mpt3sas/mpt3sas_config.c
> index eb51ce3610c8a..2609b298af0b2 100644
> --- a/drivers/scsi/mpt3sas/mpt3sas_config.c
> +++ b/drivers/scsi/mpt3sas/mpt3sas_config.c
> @@ -304,6 +304,10 @@ _config_request(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigRequest_t
>  	struct config_request mem;
>  	u32 ioc_status = UINT_MAX;
>  
> +	/* registers are unmapped (e.g. after PCI error recovery/suspend) */
> +	if (!ioc->chip)
> +		return -ENODEV;
> +
>  	mutex_lock(&ioc->config_cmds.mutex);
>  	if (ioc->config_cmds.status != MPT3_CMD_NOT_USED) {
>  		ioc_err(ioc, "%s: config_cmd in use\n", __func__);

[Severity: Medium]
Can mpt3sas_base_free_resources() unmap and clear ioc->chip after the
!ioc->chip checks in mpt3sas_base_hard_reset_handler() and
_config_request()?

CPU0                                  CPU1
mpt3sas_base_hard_reset_handler()
  if (!ioc->chip)
  mutex_lock(&ioc->reset_in_progress_mutex)
  ...
  mpt3sas_wait_for_commands_to_complete()
                                      scsih_pci_error_detected()
                                        mpt3sas_base_free_resources()
                                          mutex_lock(&ioc->pci_access_mutex)
                                          mpt3sas_base_unmap_resources()
                                            iounmap(ioc->chip)
                                            ioc->chip = NULL
                                          mutex_unlock(&ioc->pci_access_mutex)
  mpt3sas_base_make_ioc_ready()
    if (... || !ioc->chip)
      return 0;
  _base_get_ioc_facts()
    _base_handshake_req_reply_wait()
      ioc->base_readl(&ioc->chip->Doorbell)
        <-- NULL pointer dereference

mpt3sas_base_free_resources() serializes only with ioc->pci_access_mutex,
which neither mpt3sas_base_hard_reset_handler() nor _config_request() holds.
When mpt3sas_base_make_ioc_ready() returns 0 for !ioc->chip,
mpt3sas_base_hard_reset_handler() treats the controller as ready and
proceeds into _base_get_ioc_facts(), dereferencing NULL ioc->chip.
Similarly, _config_request() checks !ioc->chip before acquiring
ioc->config_cmds.mutex and can race with mpt3sas_base_free_resources()
before calling ioc->put_smid_default().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/1794995806-5835-1-git-send-email-shivasharan.srikanteshwara@broadcom.com?part=16

  reply	other threads:[~2026-11-18 11:01 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  9:56 [PATCH v2 00/37] scsi: mpt3sas: Validate firmware/user input and fix error-path bugs Shivasharan S
2026-11-18  9:56 ` [PATCH v2 01/37] scsi: mpt3sas: Validate firmware-reported IOCRequestFrameSize Shivasharan S
2026-11-18 10:28   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 02/37] scsi: mpt3sas: Validate RAID configuration page length Shivasharan S
2026-11-18 10:22   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 03/37] scsi: mpt3sas: Pin Scsi_Host while an ioctl is using its ioc Shivasharan S
2026-11-18 10:41   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 04/37] scsi: mpt3sas: Fix locking and bounds checks on host_trace_buffer sysfs files Shivasharan S
2026-11-18 10:37   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 05/37] scsi: mpt3sas: Fix unlocked use of raid_device in enable_sdev_max_qd_store Shivasharan S
2026-11-18 10:36   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 06/37] scsi: mpt3sas: Fix invalid kfree() of fw_event_work event_data Shivasharan S
2026-11-18 10:30   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 07/37] scsi: mpt3sas: Validate SMP request/response length in bsg handler Shivasharan S
2026-11-18 10:29   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 08/37] scsi: mpt3sas: Validate firmware/user supplied device handles against MaxDevHandle Shivasharan S
2026-11-18 10:33   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 09/37] scsi: mpt3sas: Clamp topology/IR event entry counts to EventDataLength Shivasharan S
2026-11-18  9:56 ` [PATCH v2 10/37] scsi: mpt3sas: NULL ioc->reply_post after freeing it Shivasharan S
2026-11-18 10:27   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 11/37] scsi: mpt3sas: Fix port_table memory leak in _scsih_sas_port_refresh Shivasharan S
2026-11-18 10:22   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 12/37] scsi: mpt3sas: Fix leak and dangling pointer for io_uring_poll_queues Shivasharan S
2026-11-18 10:32   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 13/37] scsi: mpt3sas: Validate max_data_size in MPT3EVENTREPORT ioctl Shivasharan S
2026-11-18 10:20   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 14/37] scsi: mpt3sas: Fix buffer overflow in _scsih_scsi_ioc_info Shivasharan S
2026-11-18 10:21   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 15/37] scsi: mpt3sas: Fix Scsi_Host leak on invalid/tampered device probe Shivasharan S
2026-11-18 10:26   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 16/37] scsi: mpt3sas: Fix page fault from stale ioc->chip after PCI error recovery Shivasharan S
2026-11-18 11:01   ` sashiko-bot [this message]
2026-11-18  9:56 ` [PATCH v2 17/37] scsi: mpt3sas: Clamp ValidEntries in driver trigger page update helpers Shivasharan S
2026-11-18 10:29   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 18/37] scsi: mpt3sas: Clamp firmware reply copies to the allocated reply buffer Shivasharan S
2026-11-18 10:29   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 19/37] scsi: mpt3sas: Reject implausibly small MaxReplyDescriptorPostQueueDepth Shivasharan S
2026-11-18 10:33   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 20/37] scsi: mpt3sas: Validate CurrentHostPageSize before using it as a shift amount Shivasharan S
2026-11-18 10:33   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 21/37] scsi: mpt3sas: Clamp sense_length before copying into transfer_packet->sense Shivasharan S
2026-11-18 10:35   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 22/37] scsi: mpt3sas: Clamp num_luns in _scsih_wait_for_target_to_become_ready Shivasharan S
2026-11-18 10:32   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 23/37] scsi: mpt3sas: Validate phy_number in mpt3sas_transport_update_links Shivasharan S
2026-11-18 10:42   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 24/37] scsi: mpt3sas: Require CAP_SYS_RAWIO for mpt2ctl/mpt3ctl ioctls Shivasharan S
2026-11-18  9:56 ` [PATCH v2 25/37] scsi: mpt3sas: Fix SGE offset boundary check in _ctl_do_mpt_command Shivasharan S
2026-11-18 10:39   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 26/37] scsi: mpt3sas: Enforce minimum reply frame size and validate chain segment size Shivasharan S
2026-11-18 10:46   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 27/37] scsi: mpt3sas: Replace panic() with error return in hard_reset_handler Shivasharan S
2026-11-18  9:56 ` [PATCH v2 28/37] scsi: mpt3sas: Replace panic() calls with error returns in _config_request Shivasharan S
2026-11-18  9:56 ` [PATCH v2 29/37] scsi: mpt3sas: Bound event-log copy size against reply frame size Shivasharan S
2026-11-18 10:39   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 30/37] scsi: mpt3sas: Add _ctl_diag_buffer_reset() helper to clear diag buffer state Shivasharan S
2026-11-18  9:56 ` [PATCH v2 31/37] scsi: mpt3sas: Restrict ioc_dump debugfs and replace raw ioc dump with text Shivasharan S
2026-11-18 10:38   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 32/37] scsi: mpt3sas: Fix out-of-bounds bit operation in _scsih_check_topo_delete_events Shivasharan S
2026-11-18 10:38   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 33/37] scsi: mpt3sas: Bound SMP passthrough buffer reads to actual buffer size Shivasharan S
2026-11-18 10:40   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 34/37] scsi: mpt3sas: Release sas_device reference on error in mpt3sas_transport_port_add Shivasharan S
2026-11-18 10:51   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 35/37] scsi: mpt3sas: NULL the event_log pointer after freeing it Shivasharan S
2026-11-18  9:56 ` [PATCH v2 36/37] scsi: mpt3sas: Bound NVMe passthrough transfer size against PRP buffer capacity Shivasharan S
2026-11-18 10:47   ` sashiko-bot
2026-11-18  9:56 ` [PATCH v2 37/37] scsi: mpt3sas: Update driver version to 59.100.00.00 Shivasharan S

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=sashiko-outbox-220782@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shivasharan.srikanteshwara@broadcom.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