Skip to contentSkip to conversation

Fix pointer arithmetic in hpt3xx driver code

PATCH In review Masoud Sharbiani 2 participants 2 messages Sep 5, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix pointer arithmetic in hpt3xx driver code

Hi there,
git commit 74811f355f4f69a187fa74892dcf2a684b84ce99 causes crash at module load (or boot) time on my machine with a hpt374 controller. Sergei says this is due to the pointer arithmatic. This patch fixes that and makes my machine boot again.

drivers/ide/pci/hpt366.c +15 -7
index eb107ee..f41f3fd 100644
@@ -613,6 +613,14 @@ static int check_in_drive_list(ide_drive_t *drive, const char **list)
 	return 0;
 }
 
+static struct hpt_info *hpt3xx_get_info(void *host_priv, struct device *hwif_dev, struct device *host_dev)
+{
+	struct hpt_info *info = (struct hpt_info *)host_priv;
+	if (hwif_dev == host_dev)
+		info++;
+	return info;
+}
+
 /*
  * The Marvell bridge chips used on the HighPoint SATA cards do not seem
  * to support the UltraDMA modes 1, 2, and 3 as well as any MWDMA modes...
@@ -623,7 +631,7 @@ static u8 hpt3xx_udma_filter(ide_drive_t *drive)
 	ide_hwif_t *hwif	= HWIF(drive);
 	struct pci_dev *dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 	u8 mask 		= hwif->ultra_mask;
 
 	switch (info->chip_type) {
@@ -664,7 +672,7 @@ static u8 hpt3xx_mdma_filter(ide_drive_t *drive)
 	ide_hwif_t *hwif	= HWIF(drive);
 	struct pci_dev *dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 
 	switch (info->chip_type) {
 	case HPT372 :
@@ -701,7 +709,7 @@ static void hpt3xx_set_mode(ide_drive_t *drive, const u8 speed)
 	ide_hwif_t *hwif	= drive->hwif;
 	struct pci_dev *dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 	struct hpt_timings *t	= info->timings;
 	u8  itr_addr		= 0x40 + (drive->dn * 4);
 	u32 old_itr		= 0;
@@ -745,7 +753,7 @@ static void hpt3xx_maskproc(ide_drive_t *drive, int mask)
 	ide_hwif_t *hwif	= HWIF(drive);
 	struct pci_dev	*dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 
 	if (drive->quirk_list) {
 		if (info->chip_type >= HPT370) {
@@ -974,7 +982,7 @@ static unsigned int __devinit init_chipset_hpt366(struct pci_dev *dev)
 {
 	unsigned long io_base	= pci_resource_start(dev, 4);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (&dev->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, &dev->dev, host->dev[1]);
 	const char *name	= DRV_NAME;
 	u8 pci_clk,  dpll_clk	= 0;	/* PCI and DPLL clock in MHz */
 	u8 chip_type;
@@ -1218,7 +1226,7 @@ static u8 hpt3xx_cable_detect(ide_hwif_t *hwif)
 {
 	struct pci_dev	*dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 	u8 chip_type		= info->chip_type;
 	u8 scr1 = 0, ata66	= hwif->channel ? 0x01 : 0x02;
 
@@ -1263,7 +1271,7 @@ static void __devinit init_hwif_hpt366(ide_hwif_t *hwif)
 {
 	struct pci_dev *dev	= to_pci_dev(hwif->dev);
 	struct ide_host *host	= pci_get_drvdata(dev);
-	struct hpt_info *info	= host->host_priv + (hwif->dev == host->dev[1]);
+	struct hpt_info *info	= hpt3xx_get_info(host->host_priv, hwif->dev, host->dev[1]);
 	int serialize		= HPT_SERIALIZE_IO;
 	u8  chip_type		= info->chip_type;
 	u8  new_mcr, old_mcr	= 0;
#1 lore raw
Quoted 13 lines · Masoud Sharbiani

Hi there,
git commit 74811f355f4f69a187fa74892dcf2a684b84ce99 causes crash at module load (or boot) time on my machine with a hpt374 controller. Sergei says this is due to the pointer arithmatic. This patch fixes that and makes my machine boot again.

drivers/ide/pci/hpt366.c +0 -0
index eb107ee..f41f3fd 100644

drivers/ide/pci/hpt366.c static int check_in_drive_list(ide_drive_t *drive, const char **list) · line 616

Quoted 4 lines
drivers/ide/pci/hpt366.c +1 -0
@@ -613,6 +613,14 @@ static int check_in_drive_list(ide_drive_t *drive, const char **list)
 	return 0;
 }
 
+static struct hpt_info *hpt3xx_get_info(void *host_priv, struct device *hwif_dev, struct device *host_dev)

Ugh, that's oversimplified and doesn't save much... I rather meant the following prototype:

static struct hpt_info *hpt3xx_get_info(struct pci_dev *dev)

In case it'll be too complex for you, I'll do it myself later.

drivers/ide/pci/hpt366.c static int check_in_drive_list(ide_drive_t *drive, const char **list) · line 618

Quoted 2 lines
drivers/ide/pci/hpt366.c +2 -0
+{
+	struct hpt_info *info = (struct hpt_info *)host_priv;

Newline needed after the declaration block.

drivers/ide/pci/hpt366.c static int check_in_drive_list(ide_drive_t *drive, const char **list) · line 619

Quoted 1 line
drivers/ide/pci/hpt366.c +1 -0
+	if (hwif_dev == host_dev)
		return info + 1;
	else
		return info;

or

	return hwif_dev == host_dev ? info + 1 : info;