linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hannes Reinecke <hare@suse.de>
To: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Christoph Hellwig <hch@lst.de>,
	James Bottomley <james.bottomley@hansenpartnership.com>,
	linux-scsi@vger.kernel.org, Hannes Reinecke <hare@suse.de>
Subject: [PATCH] scsi_scan: handle REPORT LUN overflow
Date: Fri, 29 May 2020 15:38:55 +0200	[thread overview]
Message-ID: <20200529133855.146357-1-hare@suse.de> (raw)

Devices might report a really large REPORT LUN buffer, which will
cause kmalloc to bail if it can't allocate enough continuguous pages.
However, at that time we already have received a perfectly good
response, so we should continue using that buffer to allow us to
register at least the LUNs from the 'good' response.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 drivers/scsi/scsi_scan.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 82a00d7751b3..0a344653487d 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -1290,7 +1290,8 @@ static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflag
 	struct scsi_sense_hdr sshdr;
 	struct scsi_device *sdev;
 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
-	int ret = 0;
+	int ret = 0, alloc_flags =
+		shost->unchecked_isa_dma ? __GFP_DMA : 0;
 
 	/*
 	 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
@@ -1327,14 +1328,13 @@ static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflag
 	 * value of the now removed max_report_luns parameter.
 	 */
 	length = (511 + 1) * sizeof(struct scsi_lun);
-retry:
-	lun_data = kmalloc(length, GFP_KERNEL |
-			   (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
+	lun_data = kmalloc(length, GFP_KERNEL | alloc_flags);
 	if (!lun_data) {
 		printk(ALLOC_FAILURE_MSG, __func__);
 		goto out;
 	}
 
+retry:
 	scsi_cmd[0] = REPORT_LUNS;
 
 	/*
@@ -1395,12 +1395,21 @@ static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflag
 	 */
 	if (get_unaligned_be32(lun_data->scsi_lun) +
 	    sizeof(struct scsi_lun) > length) {
-		length = get_unaligned_be32(lun_data->scsi_lun) +
+		unsigned int resp_length;
+		void *resp_data;
+
+		resp_length = get_unaligned_be32(lun_data->scsi_lun) +
 			 sizeof(struct scsi_lun);
-		kfree(lun_data);
-		goto retry;
-	}
-	length = get_unaligned_be32(lun_data->scsi_lun);
+		resp_data = kmalloc(resp_length, GFP_KERNEL | alloc_flags);
+		if (resp_data) {
+			kfree(lun_data);
+			lun_data = resp_data;
+			length = resp_length;
+			goto retry;
+		}
+		printk(ALLOC_FAILURE_MSG, __func__);
+	} else
+		length = get_unaligned_be32(lun_data->scsi_lun);
 
 	num_luns = (length / sizeof(struct scsi_lun));
 
-- 
2.16.4


             reply	other threads:[~2020-05-29 13:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29 13:38 Hannes Reinecke [this message]
2020-05-31  4:50 ` [PATCH] scsi_scan: handle REPORT LUN overflow kbuild test robot

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=20200529133855.146357-1-hare@suse.de \
    --to=hare@suse.de \
    --cc=hch@lst.de \
    --cc=james.bottomley@hansenpartnership.com \
    --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;
as well as URLs for NNTP newsgroup(s).