<div dir="ltr">Thanks, if we don't end up dropping this patchset I'll incorporate your suggestions into a v3.<div><br></div><div>Tom</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 26, 2022 at 12:36 AM Limonciello, Mario <<a href="mailto:mario.limonciello@amd.com">mario.limonciello@amd.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">A few suggestion ideas inline.<br>
<br>
On 1/25/2022 12:18, Tom St Denis wrote:<br>
> Newer hardware has a discovery table in hardware that the kernel will<br>
> rely on instead of header files for things like IP offsets.  This<br>
> sysfs entry adds a simple to parse table of IP instances and segment<br>
> offsets.<br>
> <br>
> Produces output that looks like:<br>
> <br>
> $ cat ip_discovery_text<br>
> ATHUB{0} v2.0.0: 00000c00 02408c00<br>
> CLKA{0} v11.0.0: 00016c00 02401800<br>
> CLKA{1} v11.0.0: 00016e00 02401c00<br>
> CLKA{2} v11.0.0: 00017000 02402000<br>
> CLKA{3} v11.0.0: 00017200 02402400<br>
> CLKA{4} v11.0.0: 0001b000 0242d800<br>
> CLKB{0} v11.0.0: 00017e00 0240bc00<br>
> DBGU_NBIO{0} v3.0.0: 000001c0 02409000<br>
> DBGU0{0} v3.0.0: 00000180 02409800<br>
> DBGU1{0} v3.0.0: 000001a0 02409c00<br>
> DF{0} v3.0.0: 00007000 0240b800<br>
> DFX{0} v4.1.0: 00000580 02409400<br>
> DFX_DAP{0} v2.0.0: 000005a0 00b80000 0240c400<br>
> DMU{0} v2.0.2: 00000012 000000c0 000034c0 00009000 02403c00<br>
> FUSE{0} v11.0.0: 00017400 02401400<br>
> GC{0} v10.1.10: 00001260 0000a000 02402c00<br>
> <br>
> (v2): Use a macro for buffer size and fix alignment in amdgpu.h<br>
> <br>
> Signed-off-by: Tom St Denis <<a href="mailto:tom.stdenis@amd.com" target="_blank">tom.stdenis@amd.com</a>><br>
> ---<br>
>   drivers/gpu/drm/amd/amdgpu/amdgpu.h           |  1 +<br>
>   drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c | 79 ++++++++++++++++++-<br>
>   2 files changed, 79 insertions(+), 1 deletion(-)<br>
> <br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
> index 3bc76759c143..43caeb4bdc07 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
> @@ -1019,6 +1019,7 @@ struct amdgpu_device {<br>
>       struct amdgpu_ip_block          ip_blocks[AMDGPU_MAX_IP_NUM];<br>
>       uint32_t                        harvest_ip_mask;<br>
>       int                             num_ip_blocks;<br>
> +     char            *ip_discovery_text;<br>
>       struct mutex    mn_lock;<br>
>       DECLARE_HASHTABLE(mn_hash, 7);<br>
>   <br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c<br>
> index 07623634fdc2..d036977dab8a 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_discovery.c<br>
> @@ -267,6 +267,19 @@ static void amdgpu_discovery_harvest_config_quirk(struct amdgpu_device *adev)<br>
>       }<br>
>   }<br>
>   <br>
> +static ssize_t ip_discovery_text_show(struct device *dev,<br>
> +             struct device_attribute *attr, char *buf)<br>
> +{<br>
> +     struct drm_device *ddev = dev_get_drvdata(dev);<br>
> +     struct amdgpu_device *adev = drm_to_adev(ddev);<br>
> +<br>
> +     return sysfs_emit(buf, "%s", adev->ip_discovery_text);<br>
> +}<br>
> +<br>
> +static DEVICE_ATTR(ip_discovery_text, S_IRUGO,<br>
> +                             ip_discovery_text_show, NULL);<br>
> +<br>
> +<br>
>   static int amdgpu_discovery_init(struct amdgpu_device *adev)<br>
>   {<br>
>       struct table_info *info;<br>
> @@ -351,6 +364,11 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)<br>
>               goto out;<br>
>       }<br>
>   <br>
> +     // init sysfs for ip_discovery<br>
> +     r = sysfs_create_file(&adev->dev->kobj, &dev_attr_ip_discovery_text.attr);<br>
> +     if (r)<br>
> +             dev_err(adev->dev, "Could not create amdgpu device attr\n");<br>
> +<br>
>       return 0;<br>
>   <br>
>   out:<br>
> @@ -363,7 +381,11 @@ static int amdgpu_discovery_init(struct amdgpu_device *adev)<br>
>   void amdgpu_discovery_fini(struct amdgpu_device *adev)<br>
>   {<br>
>       kfree(adev->mman.discovery_bin);<br>
> +     kfree(adev->ip_discovery_text);<br>
> +     sysfs_remove_file(&adev->dev->kobj, &dev_attr_ip_discovery_text.attr);<br>
> +<br>
>       adev->mman.discovery_bin = NULL;<br>
> +     adev->ip_discovery_text = NULL;<br>
>   }<br>
>   <br>
>   static int amdgpu_discovery_validate_ip(const struct ip *ip)<br>
> @@ -382,6 +404,22 @@ static int amdgpu_discovery_validate_ip(const struct ip *ip)<br>
>       return 0;<br>
>   }<br>
>   <br>
> +#define IP_DISCOVERY_BLOCK_SIZE 4096<br>
> +<br>
> +static int add_string(char **dst, unsigned *size, char *src)<br>
> +{<br>
> +     if (strlen(src) + strlen(*dst) >= *size) {<br>
> +             void *tmp = krealloc(*dst, *size + IP_DISCOVERY_BLOCK_SIZE, GFP_KERNEL);<br>
> +             if (!tmp) {<br>
> +                     return -1;<br>
<br>
If you take my other suggestion on cleanup, maybe you can also return <br>
-ENOMEM here.<br>
<br>
> +             }<br>
> +             *dst = tmp;<br>
> +             *size = *size + IP_DISCOVERY_BLOCK_SIZE;<br>
> +     }<br>
> +     strcat(*dst, src);<br>
> +     return 0;<br>
> +}<br>
> +<br>
>   int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>   {<br>
>       struct binary_header *bhdr;<br>
> @@ -396,6 +434,8 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>       int hw_ip;<br>
>       int i, j, k;<br>
>       int r;<br>
> +     unsigned txt_size = IP_DISCOVERY_BLOCK_SIZE;<br>
> +     char *linebuf;<br>
>   <br>
>       r = amdgpu_discovery_init(adev);<br>
>       if (r) {<br>
> @@ -410,6 +450,15 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>   <br>
>       DRM_DEBUG("number of dies: %d\n", num_dies);<br>
>   <br>
> +     adev->ip_discovery_text = kzalloc(txt_size, GFP_KERNEL);<br>
> +     linebuf = kzalloc(IP_DISCOVERY_BLOCK_SIZE, GFP_KERNEL);<br>
> +     if (!adev->ip_discovery_text || !linebuf) {<br>
> +             DRM_ERROR("Out of memory\n");<br>
> +             kfree(linebuf);<br>
> +             kfree(adev->ip_discovery_text);<br>
<br>
You've got a variety of new codepaths that do this freeing of the <br>
memory.  Have you considered to add a "goto cleanup" instead at the end<br>
of the function?<br>
<br>
Then each of these turns into<br>
        ret = -ENOMEM;<br>
        goto cleanup;<br>
<br>
cleanup:<br>
        DRM_ERROR("Out of memory");<br>
        kfree(..)<br>
        kfree(..)<br>
        return ret;<br>
<br>
> +             return -ENOMEM;<br>
> +     }<br>
> +<br>
>       for (i = 0; i < num_dies; i++) {<br>
>               die_offset = le16_to_cpu(ihdr->die_info[i].die_offset);<br>
>               dhdr = (struct die_header *)(adev->mman.discovery_bin + die_offset);<br>
> @@ -419,6 +468,8 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>               if (le16_to_cpu(dhdr->die_id) != i) {<br>
>                       DRM_ERROR("invalid die id %d, expected %d\n",<br>
>                                       le16_to_cpu(dhdr->die_id), i);<br>
> +                     kfree(linebuf);<br>
> +                     kfree(adev->ip_discovery_text);<br>
>                       return -EINVAL;<br>
>               }<br>
>   <br>
> @@ -458,6 +509,19 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>                           le16_to_cpu(ip->hw_id) == SDMA3_HWID)<br>
>                               adev->sdma.num_instances++;<br>
>   <br>
> +                     snprintf(linebuf, IP_DISCOVERY_BLOCK_SIZE-1, "%s{%d} v%d.%d.%d: ",<br>
> +                               hw_id_names[le16_to_cpu(ip->hw_id)],<br>
> +                               ip->number_instance,<br>
> +                               ip->major, ip->minor,<br>
> +                               ip->revision);<br>
> +                     if (add_string(&adev->ip_discovery_text, &txt_size, linebuf)) {<br>
<br>
If you take my other suggestion, you could instead do something like<br>
ret = add_string(..)<br>
if (ret)<br>
        goto cleanup;<br>
<br>
Here and the other places you use add_string.<br>
<br>
> +                             DRM_ERROR("Out of memory\n");<br>
> +                             kfree(linebuf);<br>
> +                             kfree(adev->ip_discovery_text);<br>
> +                             return -ENOMEM;<br>
> +                     }<br>
> +<br>
> +<br>
>                       for (k = 0; k < num_base_address; k++) {<br>
>                               /*<br>
>                                * convert the endianness of base addresses in place,<br>
> @@ -465,6 +529,19 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>                                */<br>
>                               ip->base_address[k] = le32_to_cpu(ip->base_address[k]);<br>
>                               DRM_DEBUG("\t0x%08x\n", ip->base_address[k]);<br>
> +                             snprintf(linebuf, IP_DISCOVERY_BLOCK_SIZE-1, "%08lx ", (unsigned long)ip->base_address[k]);<br>
> +                             if (add_string(&adev->ip_discovery_text, &txt_size, linebuf)) {<br>
> +                                     DRM_ERROR("Out of memory\n");<br>
> +                                     kfree(linebuf);<br>
> +                                     kfree(adev->ip_discovery_text);<br>
> +                                     return -ENOMEM;<br>
> +                             }<br>
> +                     }<br>
> +                     if (add_string(&adev->ip_discovery_text, &txt_size, "\n")) {<br>
> +                             DRM_ERROR("Out of memory\n");<br>
> +                             kfree(linebuf);<br>
> +                             kfree(adev->ip_discovery_text);<br>
> +                             return -ENOMEM;<br>
>                       }<br>
>   <br>
>                       for (hw_ip = 0; hw_ip < MAX_HWIP; hw_ip++) {<br>
> @@ -491,7 +568,7 @@ int amdgpu_discovery_reg_base_init(struct amdgpu_device *adev)<br>
>                       ip_offset += sizeof(*ip) + 4 * (ip->num_base_address - 1);<br>
>               }<br>
>       }<br>
> -<br>
> +     kfree(linebuf);<br>
>       return 0;<br>
>   }<br>
>   <br>
<br>
</blockquote></div>