<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt">Here is where you definitively set "ret" so DO NOT preinitialize it to 0,<br>
</span></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt">just to avoid "pesky compiler unininitialized variable warnings"--those<br>
are helpful to make the code more secure: a variable should be intentionally<br>
initialized in all paths.</span></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span style="color: rgb(12, 100, 192);">Rico: Still in confusion, pre-initialization can avoid "uninitialized variable", why should we can't pre-initialize?
</span><br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Tuikov, Luben <Luben.Tuikov@amd.com><br>
<b>Sent:</b> Wednesday, October 9, 2019 11:44<br>
<b>To:</b> Alex Deucher <alexdeucher@gmail.com>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Deucher, Alexander <Alexander.Deucher@amd.com>; Yin, Tianci (Rico) <Tianci.Yin@amd.com><br>
<b>Subject:</b> Re: [PATCH 7/8] drm/amdgpu: reserve vram for memory training</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">On 2019-10-08 3:29 p.m., Alex Deucher wrote:<br>
> From: "Tianci.Yin" <tianci.yin@amd.com><br>
> <br>
> memory training using specific fixed vram segment, reserve these<br>
> segments before anyone may allocate it.<br>
> <br>
> Change-Id: I1436755813a565608a2857a683f535377620a637<br>
> Reviewed-by: Alex Deucher <alexander.deucher@amd.com><br>
> Signed-off-by: Tianci.Yin <tianci.yin@amd.com><br>
> ---<br>
>  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 96 +++++++++++++++++++++++++<br>
>  1 file changed, 96 insertions(+)<br>
> <br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> index 74a9bd94f10c..0819721af16e 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> @@ -1667,6 +1667,93 @@ static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)<br>
>                                          &adev->fw_vram_usage.va);<br>
>  }<br>
>  <br>
> +/*<br>
> + * Memoy training reservation functions<br>
> + */<br>
<br>
Include an empty line between the two comments, just as you would<br>
a paragraph in written text.<br>
<br>
> +/**<br>
> + * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram<br>
> + *<br>
> + * @adev: amdgpu_device pointer<br>
> + *<br>
> + * free memory training reserved vram if it has been reserved.<br>
> + */<br>
> +static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)<br>
> +{<br>
> +     int ret = 0;<br>
> +     struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;<br>
> +<br>
> +     ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;<br>
> +     if(ctx->c2p_bo) {<br>
<br>
Space after keywords, according to LKCS:<br>
if (...)<br>
<br>
> +             amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);<br>
> +             ctx->c2p_bo = NULL;<br>
> +     }<br>
> +     if(ctx->p2c_bo) {<br>
<br>
Space after keywords, according to LKCS:<br>
if (...)<br>
<br>
> +             amdgpu_bo_free_kernel(&ctx->p2c_bo, NULL, NULL);<br>
> +             ctx->p2c_bo = NULL;<br>
> +     }<br>
> +<br>
> +     return ret;<br>
> +}<br>
<br>
Get rid of "ret" variable altogether as it is not used in this function.<br>
<br>
> +<br>
> +/**<br>
> + * amdgpu_ttm_training_reserve_vram_init - create bo vram reservation from memory training<br>
> + *<br>
> + * @adev: amdgpu_device pointer<br>
> + *<br>
> + * create bo vram reservation from memory training.<br>
> + */<br>
> +static int amdgpu_ttm_training_reserve_vram_init(struct amdgpu_device *adev)<br>
> +{<br>
> +     int ret = 0;<br>
<br>
DO NOT preinitialize ret.<br>
<br>
> +     struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;<br>
> +<br>
> +     memset(ctx, 0, sizeof(*ctx));<br>
> +     if(!adev->fw_vram_usage.mem_train_support) {<br>
<br>
Space after keywords: "if (".<br>
<br>
> +             DRM_DEBUG("memory training does not support!\n");<br>
> +             return 0;<br>
> +     }<br>
> +<br>
> +     ctx->c2p_train_data_offset = adev->fw_vram_usage.mem_train_fb_loc;<br>
> +     ctx->p2c_train_data_offset = (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);<br>
> +     ctx->train_data_size = GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;<br>
> +<br>
> +     DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",<br>
> +               ctx->train_data_size,<br>
> +               ctx->p2c_train_data_offset,<br>
> +               ctx->c2p_train_data_offset);<br>
> +<br>
> +     ret = amdgpu_bo_create_kernel_at(adev,<br>
<br>
Here is where you definitively set "ret" so DO NOT preinitialize it to 0,<br>
just to avoid "pesky compiler unininitialized variable warnings"--those<br>
are helpful to make the code more secure: a variable should be intentionally<br>
initialized in all paths.<br>
<br>
> +                                      ctx->p2c_train_data_offset,<br>
> +                                      ctx->train_data_size,<br>
> +                                      AMDGPU_GEM_DOMAIN_VRAM,<br>
> +                                      &ctx->p2c_bo,<br>
> +                                      NULL);<br>
> +     if(ret) {<br>
<br>
Space after keywords "if (".<br>
<br>
> +             DRM_ERROR("alloc p2c_bo failed(%d)!\n", ret);<br>
> +             ret = -ENOMEM;<br>
> +             goto err_out;<br>
> +     }<br>
> +<br>
> +     ret = amdgpu_bo_create_kernel_at(adev,<br>
> +                                      ctx->c2p_train_data_offset,<br>
> +                                      ctx->train_data_size,<br>
> +                                      AMDGPU_GEM_DOMAIN_VRAM,<br>
> +                                      &ctx->c2p_bo,<br>
> +                                      NULL);<br>
> +     if(ret) {<br>
<br>
Space after keywords: "if (", according to LKCS.<br>
<br>
Regards,<br>
Luben<br>
<br>
> +             DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);<br>
> +             ret = -ENOMEM;<br>
> +             goto err_out;<br>
> +     }<br>
> +<br>
> +     ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;<br>
> +     return 0;<br>
> +<br>
> +err_out:<br>
> +     amdgpu_ttm_training_reserve_vram_fini(adev);<br>
> +     return ret;<br>
> +}<br>
> +<br>
>  /**<br>
>   * amdgpu_ttm_init - Init the memory management (ttm) as well as various<br>
>   * gtt/vram related fields.<br>
> @@ -1740,6 +1827,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)<br>
>                return r;<br>
>        }<br>
>  <br>
> +     /*<br>
> +      *The reserved vram for memory training must be pinned to the specified<br>
> +      *place on the VRAM, so reserve it early.<br>
> +      */<br>
> +     r = amdgpu_ttm_training_reserve_vram_init(adev);<br>
> +     if (r)<br>
> +             return r;<br>
> +<br>
>        /* allocate memory as required for VGA<br>
>         * This is used for VGA emulation and pre-OS scanout buffers to<br>
>         * avoid display artifacts while transitioning between pre-OS<br>
> @@ -1825,6 +1920,7 @@ void amdgpu_ttm_fini(struct amdgpu_device *adev)<br>
>                return;<br>
>  <br>
>        amdgpu_ttm_debugfs_fini(adev);<br>
> +     amdgpu_ttm_training_reserve_vram_fini(adev);<br>
>        amdgpu_ttm_fw_reserve_vram_fini(adev);<br>
>        if (adev->mman.aper_base_kaddr)<br>
>                iounmap(adev->mman.aper_base_kaddr);<br>
> <br>
<br>
</div>
</span></font></div>
</body>
</html>