Skip to content

Commit 287980e

Browse files
arndbtorvalds
authored andcommitted
remove lots of IS_ERR_VALUE abuses
Most users of IS_ERR_VALUE() in the kernel are wrong, as they pass an 'int' into a function that takes an 'unsigned long' argument. This happens to work because the type is sign-extended on 64-bit architectures before it gets converted into an unsigned type. However, anything that passes an 'unsigned short' or 'unsigned int' argument into IS_ERR_VALUE() is guaranteed to be broken, as are 8-bit integers and types that are wider than 'unsigned long'. Andrzej Hajda has already fixed a lot of the worst abusers that were causing actual bugs, but it would be nice to prevent any users that are not passing 'unsigned long' arguments. This patch changes all users of IS_ERR_VALUE() that I could find on 32-bit ARM randconfig builds and x86 allmodconfig. For the moment, this doesn't change the definition of IS_ERR_VALUE() because there are probably still architecture specific users elsewhere. Almost all the warnings I got are for files that are better off using 'if (err)' or 'if (err < 0)'. The only legitimate user I could find that we get a warning for is the (32-bit only) freescale fman driver, so I did not remove the IS_ERR_VALUE() there but changed the type to 'unsigned long'. For 9pfs, I just worked around one user whose calling conventions are so obscure that I did not dare change the behavior. I was using this definition for testing: #define IS_ERR_VALUE(x) ((unsigned long*)NULL == (typeof (x)*)NULL && \ unlikely((unsigned long long)(x) >= (unsigned long long)(typeof(x))-MAX_ERRNO)) which ends up making all 16-bit or wider types work correctly with the most plausible interpretation of what IS_ERR_VALUE() was supposed to return according to its users, but also causes a compile-time warning for any users that do not pass an 'unsigned long' argument. I suggested this approach earlier this year, but back then we ended up deciding to just fix the users that are obviously broken. After the initial warning that caused me to get involved in the discussion (fs/gfs2/dir.c) showed up again in the mainline kernel, Linus asked me to send the whole thing again. [ Updated the 9p parts as per Al Viro - Linus ] Signed-off-by: Arnd Bergmann <[email protected]> Cc: Andrzej Hajda <[email protected]> Cc: Andrew Morton <[email protected]> Link: https://lkml.org/lkml/2016/1/7/363 Link: https://lkml.org/lkml/2016/5/27/486 Acked-by: Srinivas Kandagatla <[email protected]> # For nvmem part Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7ded384 commit 287980e

File tree

38 files changed

+102
-103
lines changed

38 files changed

+102
-103
lines changed

drivers/acpi/acpi_dbg.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static int acpi_aml_write_kern(const char *buf, int len)
265265
char *p;
266266

267267
ret = acpi_aml_lock_write(crc, ACPI_AML_OUT_KERN);
268-
if (IS_ERR_VALUE(ret))
268+
if (ret < 0)
269269
return ret;
270270
/* sync tail before inserting logs */
271271
smp_mb();
@@ -286,7 +286,7 @@ static int acpi_aml_readb_kern(void)
286286
char *p;
287287

288288
ret = acpi_aml_lock_read(crc, ACPI_AML_IN_KERN);
289-
if (IS_ERR_VALUE(ret))
289+
if (ret < 0)
290290
return ret;
291291
/* sync head before removing cmds */
292292
smp_rmb();
@@ -330,7 +330,7 @@ static ssize_t acpi_aml_write_log(const char *msg)
330330
goto again;
331331
break;
332332
}
333-
if (IS_ERR_VALUE(ret))
333+
if (ret < 0)
334334
break;
335335
size += ret;
336336
count -= ret;
@@ -373,7 +373,7 @@ static ssize_t acpi_aml_read_cmd(char *msg, size_t count)
373373
if (ret == 0)
374374
goto again;
375375
}
376-
if (IS_ERR_VALUE(ret))
376+
if (ret < 0)
377377
break;
378378
*(msg + size) = (char)ret;
379379
size++;
@@ -526,7 +526,7 @@ static int acpi_aml_open(struct inode *inode, struct file *file)
526526
}
527527
acpi_aml_io.users++;
528528
err_lock:
529-
if (IS_ERR_VALUE(ret)) {
529+
if (ret < 0) {
530530
if (acpi_aml_active_reader == file)
531531
acpi_aml_active_reader = NULL;
532532
}
@@ -587,7 +587,7 @@ static int acpi_aml_read_user(char __user *buf, int len)
587587
char *p;
588588

589589
ret = acpi_aml_lock_read(crc, ACPI_AML_OUT_USER);
590-
if (IS_ERR_VALUE(ret))
590+
if (ret < 0)
591591
return ret;
592592
/* sync head before removing logs */
593593
smp_rmb();
@@ -602,7 +602,7 @@ static int acpi_aml_read_user(char __user *buf, int len)
602602
crc->tail = (crc->tail + n) & (ACPI_AML_BUF_SIZE - 1);
603603
ret = n;
604604
out:
605-
acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, !IS_ERR_VALUE(ret));
605+
acpi_aml_unlock_fifo(ACPI_AML_OUT_USER, !ret);
606606
return ret;
607607
}
608608

@@ -634,7 +634,7 @@ static ssize_t acpi_aml_read(struct file *file, char __user *buf,
634634
goto again;
635635
}
636636
}
637-
if (IS_ERR_VALUE(ret)) {
637+
if (ret < 0) {
638638
if (!acpi_aml_running())
639639
ret = 0;
640640
break;
@@ -657,7 +657,7 @@ static int acpi_aml_write_user(const char __user *buf, int len)
657657
char *p;
658658

659659
ret = acpi_aml_lock_write(crc, ACPI_AML_IN_USER);
660-
if (IS_ERR_VALUE(ret))
660+
if (ret < 0)
661661
return ret;
662662
/* sync tail before inserting cmds */
663663
smp_mb();
@@ -672,7 +672,7 @@ static int acpi_aml_write_user(const char __user *buf, int len)
672672
crc->head = (crc->head + n) & (ACPI_AML_BUF_SIZE - 1);
673673
ret = n;
674674
out:
675-
acpi_aml_unlock_fifo(ACPI_AML_IN_USER, !IS_ERR_VALUE(ret));
675+
acpi_aml_unlock_fifo(ACPI_AML_IN_USER, !ret);
676676
return n;
677677
}
678678

@@ -704,7 +704,7 @@ static ssize_t acpi_aml_write(struct file *file, const char __user *buf,
704704
goto again;
705705
}
706706
}
707-
if (IS_ERR_VALUE(ret)) {
707+
if (ret < 0) {
708708
if (!acpi_aml_running())
709709
ret = 0;
710710
break;

drivers/ata/sata_highbank.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static void highbank_set_em_messages(struct device *dev,
197197

198198
for (i = 0; i < SGPIO_PINS; i++) {
199199
err = of_get_named_gpio(np, "calxeda,sgpio-gpio", i);
200-
if (IS_ERR_VALUE(err))
200+
if (err < 0)
201201
return;
202202

203203
pdata->sgpio_gpio[i] = err;

drivers/clk/tegra/clk-tegra210.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ static int tegra210_pll_fixed_mdiv_cfg(struct clk_hw *hw,
12211221
p = rate >= params->vco_min ? 1 : -EINVAL;
12221222
}
12231223

1224-
if (IS_ERR_VALUE(p))
1224+
if (p < 0)
12251225
return -EINVAL;
12261226

12271227
cfg->m = tegra_pll_get_fixed_mdiv(hw, input_rate);

drivers/cpufreq/omap-cpufreq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int omap_target(struct cpufreq_policy *policy, unsigned int index)
5454

5555
freq = new_freq * 1000;
5656
ret = clk_round_rate(policy->clk, freq);
57-
if (IS_ERR_VALUE(ret)) {
57+
if (ret < 0) {
5858
dev_warn(mpu_dev,
5959
"CPUfreq: Cannot find matching frequency for %lu\n",
6060
freq);

drivers/crypto/caam/ctrl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ int caam_get_era(void)
402402
ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
403403
of_node_put(caam_node);
404404

405-
return IS_ERR_VALUE(ret) ? -ENOTSUPP : prop;
405+
return ret ? -ENOTSUPP : prop;
406406
}
407407
EXPORT_SYMBOL(caam_get_era);
408408

drivers/dma/sun4i-dma.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,25 +461,25 @@ generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
461461

462462
/* Source burst */
463463
ret = convert_burst(sconfig->src_maxburst);
464-
if (IS_ERR_VALUE(ret))
464+
if (ret < 0)
465465
goto fail;
466466
promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
467467

468468
/* Destination burst */
469469
ret = convert_burst(sconfig->dst_maxburst);
470-
if (IS_ERR_VALUE(ret))
470+
if (ret < 0)
471471
goto fail;
472472
promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
473473

474474
/* Source bus width */
475475
ret = convert_buswidth(sconfig->src_addr_width);
476-
if (IS_ERR_VALUE(ret))
476+
if (ret < 0)
477477
goto fail;
478478
promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
479479

480480
/* Destination bus width */
481481
ret = convert_buswidth(sconfig->dst_addr_width);
482-
if (IS_ERR_VALUE(ret))
482+
if (ret < 0)
483483
goto fail;
484484
promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
485485

@@ -518,25 +518,25 @@ generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
518518

519519
/* Source burst */
520520
ret = convert_burst(sconfig->src_maxburst);
521-
if (IS_ERR_VALUE(ret))
521+
if (ret < 0)
522522
goto fail;
523523
promise->cfg |= SUN4I_DMA_CFG_SRC_BURST_LENGTH(ret);
524524

525525
/* Destination burst */
526526
ret = convert_burst(sconfig->dst_maxburst);
527-
if (IS_ERR_VALUE(ret))
527+
if (ret < 0)
528528
goto fail;
529529
promise->cfg |= SUN4I_DMA_CFG_DST_BURST_LENGTH(ret);
530530

531531
/* Source bus width */
532532
ret = convert_buswidth(sconfig->src_addr_width);
533-
if (IS_ERR_VALUE(ret))
533+
if (ret < 0)
534534
goto fail;
535535
promise->cfg |= SUN4I_DMA_CFG_SRC_DATA_WIDTH(ret);
536536

537537
/* Destination bus width */
538538
ret = convert_buswidth(sconfig->dst_addr_width);
539-
if (IS_ERR_VALUE(ret))
539+
if (ret < 0)
540540
goto fail;
541541
promise->cfg |= SUN4I_DMA_CFG_DST_DATA_WIDTH(ret);
542542

drivers/gpio/gpio-xlp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static int xlp_gpio_probe(struct platform_device *pdev)
393393
irq_base = irq_alloc_descs(-1, 0, gc->ngpio, 0);
394394
else
395395
irq_base = irq_alloc_descs(-1, XLP_GPIO_IRQ_BASE, gc->ngpio, 0);
396-
if (IS_ERR_VALUE(irq_base)) {
396+
if (irq_base < 0) {
397397
dev_err(&pdev->dev, "Failed to allocate IRQ numbers\n");
398398
return irq_base;
399399
}

drivers/gpu/drm/sti/sti_vtg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ static int vtg_probe(struct platform_device *pdev)
437437
return -EPROBE_DEFER;
438438
} else {
439439
vtg->irq = platform_get_irq(pdev, 0);
440-
if (IS_ERR_VALUE(vtg->irq)) {
440+
if (vtg->irq < 0) {
441441
DRM_ERROR("Failed to get VTG interrupt\n");
442442
return vtg->irq;
443443
}
@@ -447,7 +447,7 @@ static int vtg_probe(struct platform_device *pdev)
447447
ret = devm_request_threaded_irq(dev, vtg->irq, vtg_irq,
448448
vtg_irq_thread, IRQF_ONESHOT,
449449
dev_name(dev), vtg);
450-
if (IS_ERR_VALUE(ret)) {
450+
if (ret < 0) {
451451
DRM_ERROR("Failed to register VTG interrupt\n");
452452
return ret;
453453
}

drivers/gpu/drm/tilcdc/tilcdc_tfp410.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ static int tfp410_probe(struct platform_device *pdev)
342342

343343
tfp410_mod->gpio = of_get_named_gpio_flags(node, "powerdn-gpio",
344344
0, NULL);
345-
if (IS_ERR_VALUE(tfp410_mod->gpio)) {
345+
if (tfp410_mod->gpio < 0) {
346346
dev_warn(&pdev->dev, "No power down GPIO\n");
347347
} else {
348348
ret = gpio_request(tfp410_mod->gpio, "DVI_PDn");

drivers/gpu/host1x/hw/intr_hw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static int _host1x_intr_init_host_sync(struct host1x *host, u32 cpm,
8585
err = devm_request_irq(host->dev, host->intr_syncpt_irq,
8686
syncpt_thresh_isr, IRQF_SHARED,
8787
"host1x_syncpt", host);
88-
if (IS_ERR_VALUE(err)) {
88+
if (err < 0) {
8989
WARN_ON(1);
9090
return err;
9191
}

drivers/iommu/arm-smmu-v3.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
14771477
struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
14781478

14791479
asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1480-
if (IS_ERR_VALUE(asid))
1480+
if (asid < 0)
14811481
return asid;
14821482

14831483
cfg->cdptr = dmam_alloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
@@ -1508,7 +1508,7 @@ static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
15081508
struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
15091509

15101510
vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1511-
if (IS_ERR_VALUE(vmid))
1511+
if (vmid < 0)
15121512
return vmid;
15131513

15141514
cfg->vmid = (u16)vmid;
@@ -1569,7 +1569,7 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain)
15691569
smmu_domain->pgtbl_ops = pgtbl_ops;
15701570

15711571
ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1572-
if (IS_ERR_VALUE(ret))
1572+
if (ret < 0)
15731573
free_io_pgtable_ops(pgtbl_ops);
15741574

15751575
return ret;
@@ -1642,7 +1642,7 @@ static void arm_smmu_detach_dev(struct device *dev)
16421642
struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
16431643

16441644
smmu_group->ste.bypass = true;
1645-
if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
1645+
if (arm_smmu_install_ste_for_group(smmu_group) < 0)
16461646
dev_warn(dev, "failed to install bypass STE\n");
16471647

16481648
smmu_group->domain = NULL;
@@ -1694,7 +1694,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
16941694
smmu_group->ste.bypass = domain->type == IOMMU_DOMAIN_DMA;
16951695

16961696
ret = arm_smmu_install_ste_for_group(smmu_group);
1697-
if (IS_ERR_VALUE(ret))
1697+
if (ret < 0)
16981698
smmu_group->domain = NULL;
16991699

17001700
out_unlock:
@@ -2235,7 +2235,7 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
22352235
arm_smmu_evtq_handler,
22362236
arm_smmu_evtq_thread,
22372237
0, "arm-smmu-v3-evtq", smmu);
2238-
if (IS_ERR_VALUE(ret))
2238+
if (ret < 0)
22392239
dev_warn(smmu->dev, "failed to enable evtq irq\n");
22402240
}
22412241

@@ -2244,15 +2244,15 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
22442244
ret = devm_request_irq(smmu->dev, irq,
22452245
arm_smmu_cmdq_sync_handler, 0,
22462246
"arm-smmu-v3-cmdq-sync", smmu);
2247-
if (IS_ERR_VALUE(ret))
2247+
if (ret < 0)
22482248
dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
22492249
}
22502250

22512251
irq = smmu->gerr_irq;
22522252
if (irq) {
22532253
ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
22542254
0, "arm-smmu-v3-gerror", smmu);
2255-
if (IS_ERR_VALUE(ret))
2255+
if (ret < 0)
22562256
dev_warn(smmu->dev, "failed to enable gerror irq\n");
22572257
}
22582258

@@ -2264,7 +2264,7 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
22642264
arm_smmu_priq_thread,
22652265
0, "arm-smmu-v3-priq",
22662266
smmu);
2267-
if (IS_ERR_VALUE(ret))
2267+
if (ret < 0)
22682268
dev_warn(smmu->dev,
22692269
"failed to enable priq irq\n");
22702270
else

drivers/iommu/arm-smmu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
950950

951951
ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
952952
smmu->num_context_banks);
953-
if (IS_ERR_VALUE(ret))
953+
if (ret < 0)
954954
goto out_unlock;
955955

956956
cfg->cbndx = ret;
@@ -989,7 +989,7 @@ static int arm_smmu_init_domain_context(struct iommu_domain *domain,
989989
irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
990990
ret = request_irq(irq, arm_smmu_context_fault, IRQF_SHARED,
991991
"arm-smmu-context-fault", domain);
992-
if (IS_ERR_VALUE(ret)) {
992+
if (ret < 0) {
993993
dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
994994
cfg->irptndx, irq);
995995
cfg->irptndx = INVALID_IRPTNDX;
@@ -1099,7 +1099,7 @@ static int arm_smmu_master_configure_smrs(struct arm_smmu_device *smmu,
10991099
for (i = 0; i < cfg->num_streamids; ++i) {
11001100
int idx = __arm_smmu_alloc_bitmap(smmu->smr_map, 0,
11011101
smmu->num_mapping_groups);
1102-
if (IS_ERR_VALUE(idx)) {
1102+
if (idx < 0) {
11031103
dev_err(smmu->dev, "failed to allocate free SMR\n");
11041104
goto err_free_smrs;
11051105
}
@@ -1233,7 +1233,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
12331233

12341234
/* Ensure that the domain is finalised */
12351235
ret = arm_smmu_init_domain_context(domain, smmu);
1236-
if (IS_ERR_VALUE(ret))
1236+
if (ret < 0)
12371237
return ret;
12381238

12391239
/*

drivers/irqchip/irq-clps711x.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static int __init _clps711x_intc_init(struct device_node *np,
182182
writel_relaxed(0, clps711x_intc->intmr[2]);
183183

184184
err = irq_alloc_descs(-1, 0, ARRAY_SIZE(clps711x_irqs), numa_node_id());
185-
if (IS_ERR_VALUE(err))
185+
if (err < 0)
186186
goto out_iounmap;
187187

188188
clps711x_intc->ops.map = clps711x_intc_irq_map;

drivers/irqchip/irq-gic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ static int __init __gic_init_bases(struct gic_chip_data *gic, int irq_start,
11231123

11241124
irq_base = irq_alloc_descs(irq_start, 16, gic_irqs,
11251125
numa_node_id());
1126-
if (IS_ERR_VALUE(irq_base)) {
1126+
if (irq_base < 0) {
11271127
WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
11281128
irq_start);
11291129
irq_base = irq_start;

drivers/irqchip/irq-hip04.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ hip04_of_init(struct device_node *node, struct device_node *parent)
402402
nr_irqs -= hwirq_base; /* calculate # of irqs to allocate */
403403

404404
irq_base = irq_alloc_descs(-1, hwirq_base, nr_irqs, numa_node_id());
405-
if (IS_ERR_VALUE(irq_base)) {
405+
if (irq_base < 0) {
406406
pr_err("failed to allocate IRQ numbers\n");
407407
return -EINVAL;
408408
}

drivers/irqchip/spear-shirq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static int __init shirq_init(struct spear_shirq **shirq_blocks, int block_nr,
232232
nr_irqs += shirq_blocks[i]->nr_irqs;
233233

234234
virq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
235-
if (IS_ERR_VALUE(virq_base)) {
235+
if (virq_base < 0) {
236236
pr_err("%s: irq desc alloc failed\n", __func__);
237237
goto err_unmap;
238238
}

0 commit comments

Comments
 (0)