mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-03 18:20:25 +02:00 
			
		
		
		
	drm: simplify drm_*_set_unique()
Lets use kasprintf() to avoid pre-allocating the buffer. This is really nothing to optimize for speed and the input is trusted, so kasprintf() is just fine. Signed-off-by: David Herrmann <dh.herrmann@gmail.com> Reviewed-by: Thierry Reding <treding@nvidia.com> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Dave Airlie <airlied@redhat.com>
This commit is contained in:
		
							parent
							
								
									d7d2c48e5c
								
							
						
					
					
						commit
						d0a39164b6
					
				
					 2 changed files with 16 additions and 45 deletions
				
			
		| 
						 | 
					@ -129,31 +129,17 @@ static int drm_get_pci_domain(struct drm_device *dev)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
 | 
					static int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int len, ret;
 | 
						master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
 | 
				
			||||||
	master->unique_len = 40;
 | 
					 | 
				
			||||||
	master->unique_size = master->unique_len;
 | 
					 | 
				
			||||||
	master->unique = kmalloc(master->unique_size, GFP_KERNEL);
 | 
					 | 
				
			||||||
	if (master->unique == NULL)
 | 
					 | 
				
			||||||
		return -ENOMEM;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	len = snprintf(master->unique, master->unique_len,
 | 
					 | 
				
			||||||
		       "pci:%04x:%02x:%02x.%d",
 | 
					 | 
				
			||||||
					drm_get_pci_domain(dev),
 | 
										drm_get_pci_domain(dev),
 | 
				
			||||||
					dev->pdev->bus->number,
 | 
										dev->pdev->bus->number,
 | 
				
			||||||
					PCI_SLOT(dev->pdev->devfn),
 | 
										PCI_SLOT(dev->pdev->devfn),
 | 
				
			||||||
					PCI_FUNC(dev->pdev->devfn));
 | 
										PCI_FUNC(dev->pdev->devfn));
 | 
				
			||||||
 | 
						if (!master->unique)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (len >= master->unique_len) {
 | 
						master->unique_len = strlen(master->unique);
 | 
				
			||||||
		DRM_ERROR("buffer overflow");
 | 
						master->unique_size = master->unique_len + 1;
 | 
				
			||||||
		ret = -EINVAL;
 | 
					 | 
				
			||||||
		goto err;
 | 
					 | 
				
			||||||
	} else
 | 
					 | 
				
			||||||
		master->unique_len = len;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
err:
 | 
					 | 
				
			||||||
	return ret;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int drm_pci_set_unique(struct drm_device *dev,
 | 
					int drm_pci_set_unique(struct drm_device *dev,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,35 +70,20 @@ static int drm_get_platform_dev(struct platform_device *platdev,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
 | 
					static int drm_platform_set_busid(struct drm_device *dev, struct drm_master *master)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int len, ret, id;
 | 
						int id;
 | 
				
			||||||
 | 
					 | 
				
			||||||
	master->unique_len = 13 + strlen(dev->platformdev->name);
 | 
					 | 
				
			||||||
	master->unique_size = master->unique_len;
 | 
					 | 
				
			||||||
	master->unique = kmalloc(master->unique_len + 1, GFP_KERNEL);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (master->unique == NULL)
 | 
					 | 
				
			||||||
		return -ENOMEM;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	id = dev->platformdev->id;
 | 
						id = dev->platformdev->id;
 | 
				
			||||||
 | 
						if (id < 0)
 | 
				
			||||||
	/* if only a single instance of the platform device, id will be
 | 
					 | 
				
			||||||
	 * set to -1.. use 0 instead to avoid a funny looking bus-id:
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	if (id == -1)
 | 
					 | 
				
			||||||
		id = 0;
 | 
							id = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	len = snprintf(master->unique, master->unique_len,
 | 
						master->unique = kasprintf(GFP_KERNEL, "platform:%s:%02d",
 | 
				
			||||||
			"platform:%s:%02d", dev->platformdev->name, id);
 | 
											dev->platformdev->name, id);
 | 
				
			||||||
 | 
						if (!master->unique)
 | 
				
			||||||
	if (len > master->unique_len) {
 | 
							return -ENOMEM;
 | 
				
			||||||
		DRM_ERROR("Unique buffer overflowed\n");
 | 
					 | 
				
			||||||
		ret = -EINVAL;
 | 
					 | 
				
			||||||
		goto err;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						master->unique_len = strlen(master->unique);
 | 
				
			||||||
 | 
						master->unique_size = master->unique_len;
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
err:
 | 
					 | 
				
			||||||
	return ret;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct drm_bus drm_platform_bus = {
 | 
					static struct drm_bus drm_platform_bus = {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue