Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 729a6a30 authored by Tejun Heo's avatar Tejun Heo Committed by Jeff Garzik
Browse files

libata: set queue DMA alignment to sector size for ATAPI too



ata_pio_sectors() expects buffer for each sector to be contained in a
single page; otherwise, it ends up overrunning the first page.  This
is achieved by setting queue DMA alignment.  If sector_size is smaller
than PAGE_SIZE and all buffers are sector_size aligned, buffer for
each sector is always contained in a single page.

This wasn't applied to ATAPI devices but IDENTIFY_PACKET is executed
as ATA_PROT_PIO and thus uses ata_pio_sectors().  Newer versions of
udev issue IDENTIFY_PACKET with unaligned buffer triggering the
problem and causing oops.

This patch fixes the problem by setting sdev->sector_size to
ATA_SECT_SIZE on ATATPI devices and always setting DMA alignment to
sector_size.  While at it, add a warning for the unlikely but still
possible scenario where sector_size is larger than PAGE_SIZE, in which
case the alignment wouldn't be enough.

Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Reported-by: default avatarJohn Stanley <jpsinthemix@verizon.net>
Tested-by: default avatarJohn Stanley <jpsinthemix@verizon.net>
Cc: stable@kernel.org
Signed-off-by: default avatarJeff Garzik <jgarzik@redhat.com>
parent 4a5610a0
Loading
Loading
Loading
Loading
+18 −6
Original line number Original line Diff line number Diff line
@@ -1099,9 +1099,9 @@ static int ata_scsi_dev_config(struct scsi_device *sdev,
		struct request_queue *q = sdev->request_queue;
		struct request_queue *q = sdev->request_queue;
		void *buf;
		void *buf;


		/* set the min alignment and padding */
		sdev->sector_size = ATA_SECT_SIZE;
		blk_queue_update_dma_alignment(sdev->request_queue,

					       ATA_DMA_PAD_SZ - 1);
		/* set DMA padding */
		blk_queue_update_dma_pad(sdev->request_queue,
		blk_queue_update_dma_pad(sdev->request_queue,
					 ATA_DMA_PAD_SZ - 1);
					 ATA_DMA_PAD_SZ - 1);


@@ -1115,13 +1115,25 @@ static int ata_scsi_dev_config(struct scsi_device *sdev,


		blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
		blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
	} else {
	} else {
		/* ATA devices must be sector aligned */
		sdev->sector_size = ata_id_logical_sector_size(dev->id);
		sdev->sector_size = ata_id_logical_sector_size(dev->id);
		blk_queue_update_dma_alignment(sdev->request_queue,
					       sdev->sector_size - 1);
		sdev->manage_start_stop = 1;
		sdev->manage_start_stop = 1;
	}
	}


	/*
	 * ata_pio_sectors() expects buffer for each sector to not cross
	 * page boundary.  Enforce it by requiring buffers to be sector
	 * aligned, which works iff sector_size is not larger than
	 * PAGE_SIZE.  ATAPI devices also need the alignment as
	 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
	 */
	if (sdev->sector_size > PAGE_SIZE)
		ata_dev_printk(dev, KERN_WARNING,
			"sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
			sdev->sector_size);

	blk_queue_update_dma_alignment(sdev->request_queue,
				       sdev->sector_size - 1);

	if (dev->flags & ATA_DFLAG_AN)
	if (dev->flags & ATA_DFLAG_AN)
		set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
		set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);