linux-3.4.10 _pci_assign_resource函数bug
<linux-3.4.10/drivers/pci/setup-res.c>
int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,resource_size_t min_align)
{
//omitted
/* already aligned with min_align */
new_size = resource_size(res) + addsize;
ret = _pci_assign_resource(dev, resno, new_size, min_align);
if (!ret) {
res->flags &= ~IORESOURCE_STARTALIGN;
dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
if (resno < PCI_BRIDGE_RESOURCES)
pci_update_resource(dev, resno);
}
return ret;
}
static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
{
//………
bus = dev->bus;
while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
if (!bus->parent || !bus->self->transparent)
break;
bus = bus->parent;
}
// ……...
}
static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,int resno, resource_size_t size, resource_size_t align)
从以上的函数调用关系可知
pci_reassign_resource()-->_pci_assign_resource()-->__pci_assign_resource()
函数的定义里size定义的类型不一致,这样在64位操作系统里,会引起符号扩展的错误,因为当resource_size_t为64位无符号数时,而int定义为32位的有符号数,如果int定义的数最高位为1,这样再把int值赋值回u64的值就会引起符号扩展导致size错误。
<linux-3.4.10/include/linux/types.h>
#ifdef CONFIG_PHYS_ADDR_T_64BIT
typedef u64 phys_addr_t;
#else
typedef u32 phys_addr_t;
#endif
typedef phys_addr_t resource_size_t;
例如:在X86_64机器内核版本Linux ubuntu 2.6.32-30-server #59-Ubuntu SMP
测试程序<test.c>
#include <stdio.h>
int main()
{
unsigned long a=0xe0000000;
int b=a;
unsigned long c=b;
printf("a=%#lx, b=%#x, c=%#lx\n", a, b, c);
return 0;
}
swq@ubuntu:~$ gcc test.c
swq@ubuntu:~$ ./a.out
a=0xe0000000, b=0xe0000000, c=0xffffffffe0000000
<linux-3.4.10/drivers/pci/setup-res.c>
int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsize,resource_size_t min_align)
{
//omitted
/* already aligned with min_align */
new_size = resource_size(res) + addsize;
ret = _pci_assign_resource(dev, resno, new_size, min_align);
if (!ret) {
res->flags &= ~IORESOURCE_STARTALIGN;
dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
if (resno < PCI_BRIDGE_RESOURCES)
pci_update_resource(dev, resno);
}
return ret;
}
static int _pci_assign_resource(struct pci_dev *dev, int resno, int size, resource_size_t min_align)
{
//………
bus = dev->bus;
while ((ret = __pci_assign_resource(bus, dev, resno, size, min_align))) {
if (!bus->parent || !bus->self->transparent)
break;
bus = bus->parent;
}
// ……...
}
static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,int resno, resource_size_t size, resource_size_t align)
从以上的函数调用关系可知
pci_reassign_resource()-->_pci_assign_resource()-->__pci_assign_resource()
函数的定义里size定义的类型不一致,这样在64位操作系统里,会引起符号扩展的错误,因为当resource_size_t为64位无符号数时,而int定义为32位的有符号数,如果int定义的数最高位为1,这样再把int值赋值回u64的值就会引起符号扩展导致size错误。
<linux-3.4.10/include/linux/types.h>
#ifdef CONFIG_PHYS_ADDR_T_64BIT
typedef u64 phys_addr_t;
#else
typedef u32 phys_addr_t;
#endif
typedef phys_addr_t resource_size_t;
例如:在X86_64机器内核版本Linux ubuntu 2.6.32-30-server #59-Ubuntu SMP
测试程序<test.c>
#include <stdio.h>
int main()
{
unsigned long a=0xe0000000;
int b=a;
unsigned long c=b;
printf("a=%#lx, b=%#x, c=%#lx\n", a, b, c);
return 0;
}
swq@ubuntu:~$ gcc test.c
swq@ubuntu:~$ ./a.out
a=0xe0000000, b=0xe0000000, c=0xffffffffe0000000