mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Alex Deucher <alexander.deucher@amd.com>
Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	"SHANMUGAM, SRINIVASAN" <SRINIVASAN.SHANMUGAM@amd.com>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: [bug report] drm/amdgpu: fix possible fence leaks from job structure
Date: Fri, 6 Feb 2026 16:41:26 +0300	[thread overview]
Message-ID: <aYXvhj1fJ8ZFGyV4@stanley.mountain> (raw)
In-Reply-To: <caa37f28-a2e8-4e0a-a9ce-a365ce805e4b@stanley.mountain>

[ Smatch checking is paused while we raise funding.  #SadFace
  https://lore.kernel.org/all/aTaiGSbWZ9DJaGo7@stanley.mountain/ -dan ]

Hello Alex Deucher,

Commit f903b85ed0f1 ("drm/amdgpu: fix possible fence leaks from job
structure") from Oct 22, 2025 (linux-next), leads to the following
Smatch static checker warning:

	drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c:232 amdgpu_ib_schedule()
	warn: missing unwind goto?

drivers/gpu/drm/amd/amdgpu/amdgpu_ib.c
    124 int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned int num_ibs,
    125                        struct amdgpu_ib *ibs, struct amdgpu_job *job,
    126                        struct dma_fence **f)
    127 {
    128         struct amdgpu_device *adev = ring->adev;
    129         struct amdgpu_ib *ib = &ibs[0];
    130         struct dma_fence *tmp = NULL;
    131         struct amdgpu_fence *af;
    132         bool need_ctx_switch;
    133         struct amdgpu_vm *vm;
    134         uint64_t fence_ctx;
    135         uint32_t status = 0, alloc_size;
    136         unsigned int fence_flags = 0;
    137         bool secure, init_shadow;
    138         u64 shadow_va, csa_va, gds_va;
    139         int vmid = AMDGPU_JOB_GET_VMID(job);
    140         bool need_pipe_sync = false;
    141         unsigned int cond_exec;
    142         unsigned int i;
    143         int r = 0;
    144 
    145         if (num_ibs == 0)
    146                 return -EINVAL;
    147 
    148         /* ring tests don't use a job */
    149         if (job) {
    150                 vm = job->vm;
    151                 fence_ctx = job->base.s_fence ?
    152                         job->base.s_fence->finished.context : 0;
    153                 shadow_va = job->shadow_va;
    154                 csa_va = job->csa_va;
    155                 gds_va = job->gds_va;
    156                 init_shadow = job->init_shadow;
    157                 af = job->hw_fence;
    158                 /* Save the context of the job for reset handling.
    159                  * The driver needs this so it can skip the ring
    160                  * contents for guilty contexts.
    161                  */
    162                 af->context = fence_ctx;
    163                 /* the vm fence is also part of the job's context */
    164                 job->hw_vm_fence->context = fence_ctx;
    165         } else {
    166                 vm = NULL;
    167                 fence_ctx = 0;
    168                 shadow_va = 0;
    169                 csa_va = 0;
    170                 gds_va = 0;
    171                 init_shadow = false;
    172                 af = kzalloc(sizeof(*af), GFP_ATOMIC);
    173                 if (!af)
    174                         return -ENOMEM;
    175         }
    176 
    177         if (!ring->sched.ready) {
    178                 dev_err(adev->dev, "couldn't schedule ib on ring <%s>\n", ring->name);
    179                 r = -EINVAL;
    180                 goto free_fence;
    181         }
    182 
    183         if (vm && !job->vmid) {
    184                 dev_err(adev->dev, "VM IB without ID\n");
    185                 r = -EINVAL;
    186                 goto free_fence;
    187         }
    188 
    189         if ((ib->flags & AMDGPU_IB_FLAGS_SECURE) &&
    190             (!ring->funcs->secure_submission_supported)) {
    191                 dev_err(adev->dev, "secure submissions not supported on ring <%s>\n", ring->name);
    192                 r = -EINVAL;
    193                 goto free_fence;
    194         }
    195 
    196         alloc_size = ring->funcs->emit_frame_size + num_ibs *
    197                 ring->funcs->emit_ib_size;
    198 
    199         r = amdgpu_ring_alloc(ring, alloc_size);
    200         if (r) {
    201                 dev_err(adev->dev, "scheduling IB failed (%d).\n", r);
    202                 goto free_fence;
    203         }
    204 
    205         need_ctx_switch = ring->current_ctx != fence_ctx;
    206         if (ring->funcs->emit_pipeline_sync && job &&
    207             ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) ||
    208              need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) {
    209 
    210                 need_pipe_sync = true;
    211 
    212                 if (tmp)
    213                         trace_amdgpu_ib_pipe_sync(job, tmp);
    214 
    215                 dma_fence_put(tmp);
    216         }
    217 
    218         if ((ib->flags & AMDGPU_IB_FLAG_EMIT_MEM_SYNC) && ring->funcs->emit_mem_sync)
    219                 ring->funcs->emit_mem_sync(ring);
    220 
    221         if (ring->funcs->emit_wave_limit &&
    222             ring->hw_prio == AMDGPU_GFX_PIPE_PRIO_HIGH)
    223                 ring->funcs->emit_wave_limit(ring, true);
    224 
    225         if (ring->funcs->insert_start)
    226                 ring->funcs->insert_start(ring);
    227 
    228         if (job) {
    229                 r = amdgpu_vm_flush(ring, job, need_pipe_sync);
    230                 if (r) {
    231                         amdgpu_ring_undo(ring);
--> 232                         return r;

The patch changed the other error paths to goto free_fence but this
one was accidentally skipped.

    233                 }
    234         }
    235 
    236         amdgpu_ring_ib_begin(ring);
    237 
    238         if (ring->funcs->emit_gfx_shadow && adev->gfx.cp_gfx_shadow)
    239                 amdgpu_ring_emit_gfx_shadow(ring, shadow_va, csa_va, gds_va,
    240                                             init_shadow, vmid);
    241 
    242         if (ring->funcs->init_cond_exec)
    243                 cond_exec = amdgpu_ring_init_cond_exec(ring,
    244                                                        ring->cond_exe_gpu_addr);
    245 
    246         amdgpu_device_flush_hdp(adev, ring);
    247 
    248         if (need_ctx_switch)
    249                 status |= AMDGPU_HAVE_CTX_SWITCH;
    250 
    251         if (job && ring->funcs->emit_cntxcntl) {
    252                 status |= job->preamble_status;
    253                 status |= job->preemption_status;
    254                 amdgpu_ring_emit_cntxcntl(ring, status);
    255         }
    256 
    257         /* Setup initial TMZiness and send it off.
    258          */
    259         secure = false;
    260         if (job && ring->funcs->emit_frame_cntl) {
    261                 secure = ib->flags & AMDGPU_IB_FLAGS_SECURE;
    262                 amdgpu_ring_emit_frame_cntl(ring, true, secure);
    263         }
    264 
    265         for (i = 0; i < num_ibs; ++i) {
    266                 ib = &ibs[i];
    267 
    268                 if (job && ring->funcs->emit_frame_cntl) {
    269                         if (secure != !!(ib->flags & AMDGPU_IB_FLAGS_SECURE)) {
    270                                 amdgpu_ring_emit_frame_cntl(ring, false, secure);
    271                                 secure = !secure;
    272                                 amdgpu_ring_emit_frame_cntl(ring, true, secure);
    273                         }
    274                 }
    275 
    276                 amdgpu_ring_emit_ib(ring, job, ib, status);
    277                 status &= ~AMDGPU_HAVE_CTX_SWITCH;
    278         }
    279 
    280         if (job && ring->funcs->emit_frame_cntl)
    281                 amdgpu_ring_emit_frame_cntl(ring, false, secure);
    282 
    283         amdgpu_device_invalidate_hdp(adev, ring);
    284 
    285         if (ib->flags & AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE)
    286                 fence_flags |= AMDGPU_FENCE_FLAG_TC_WB_ONLY;
    287 
    288         /* wrap the last IB with fence */
    289         if (job && job->uf_addr) {
    290                 amdgpu_ring_emit_fence(ring, job->uf_addr, job->uf_sequence,
    291                                        fence_flags | AMDGPU_FENCE_FLAG_64BIT);
    292         }
    293 
    294         if (ring->funcs->emit_gfx_shadow && ring->funcs->init_cond_exec &&
    295             adev->gfx.cp_gfx_shadow) {
    296                 amdgpu_ring_emit_gfx_shadow(ring, 0, 0, 0, false, 0);
    297                 amdgpu_ring_init_cond_exec(ring, ring->cond_exe_gpu_addr);
    298         }
    299 
    300         r = amdgpu_fence_emit(ring, af, fence_flags);
    301         if (r) {
    302                 dev_err(adev->dev, "failed to emit fence (%d)\n", r);
    303                 if (job && job->vmid)
    304                         amdgpu_vmid_reset(adev, ring->vm_hub, job->vmid);
    305                 amdgpu_ring_undo(ring);
    306                 goto free_fence;
    307         }
    308         *f = &af->base;
    309         /* get a ref for the job */
    310         if (job)
    311                 dma_fence_get(*f);
    312 
    313         if (ring->funcs->insert_end)
    314                 ring->funcs->insert_end(ring);
    315 
    316         amdgpu_ring_patch_cond_exec(ring, cond_exec);
    317 
    318         ring->current_ctx = fence_ctx;
    319         if (job && ring->funcs->emit_switch_buffer)
    320                 amdgpu_ring_emit_switch_buffer(ring);
    321 
    322         if (ring->funcs->emit_wave_limit &&
    323             ring->hw_prio == AMDGPU_GFX_PIPE_PRIO_HIGH)
    324                 ring->funcs->emit_wave_limit(ring, false);
    325 
    326         /* Save the wptr associated with this fence.
    327          * This must be last for resets to work properly
    328          * as we need to save the wptr associated with this
    329          * fence so we know what rings contents to backup
    330          * after we reset the queue.
    331          */
    332         amdgpu_fence_save_wptr(af);
    333 
    334         amdgpu_ring_ib_end(ring);
    335         amdgpu_ring_commit(ring);
    336 
    337         return 0;
    338 
    339 free_fence:
    340         if (!job)
    341                 kfree(af);
    342         return r;
    343 }

regards,
dan carpenter

  parent reply	other threads:[~2026-02-06 13:41 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-08 10:02 Support needed to continue Smatch work Dan Carpenter
2026-02-06 13:38 ` Dan Carpenter
2026-02-06 13:38   ` [bug report] net: ethtool: Introduce per-PHY DUMP operations Dan Carpenter
2026-02-06 17:04     ` Maxime Chevallier
2026-02-09  7:09       ` Dan Carpenter
2026-02-09  8:09         ` Maxime Chevallier
2026-02-09 13:10           ` Andrew Lunn
2026-02-10 10:37             ` Dan Carpenter
2026-02-06 13:38   ` [bug report] net: wwan: Add Qualcomm BAM-DMUX WWAN network driver Dan Carpenter
2026-02-06 15:12     ` Stephan Gerhold
2026-02-06 15:23       ` Dan Carpenter
2026-02-06 13:38   ` [bug report] iommu/amd: Introduce gDomID-to-hDomID Mapping and handle parent domain invalidation Dan Carpenter
2026-02-06 13:38   ` [bug report] drm/amdkfd: add debug set and clear address watch points operation Dan Carpenter
2026-02-06 13:38   ` [PATCH next] mtd: spi-nor: hisi-sfc: fix refcounting bug in hisi_spi_nor_register_all() Dan Carpenter
2026-02-06 14:14     ` Pratyush Yadav
2026-02-06 14:22       ` Miquel Raynal
2026-02-06 14:23     ` Miquel Raynal
2026-02-06 13:39   ` [bug report] media: synopsys: add driver for the designware mipi csi-2 receiver Dan Carpenter
2026-02-06 13:39   ` [bug report] crush: remove forcefeed functionality Dan Carpenter
2026-02-06 20:44     ` Viacheslav Dubeyko
2026-02-06 13:39   ` [bug report] net: ethernet: ti: am65-cpsw: enable bc/mc storm prevention support Dan Carpenter
2026-02-06 13:39   ` [bug report] phy: qcom: qmp-usbc: Add QCS615 USB/DP PHY config and DP mode support Dan Carpenter
2026-02-17 15:27     ` Konrad Dybcio
2026-02-27  5:11       ` Xiangxu Yin
2026-02-06 13:39   ` [bug report] drm/amd/display: add DC changes for DCN351 Dan Carpenter
2026-02-06 13:39   ` [bug report] media: rockchip: rkcif: add support for rk3568 vicap mipi capture Dan Carpenter
2026-02-16 13:33     ` Michael Riesch
2026-02-06 13:39   ` [bug report] drm/imagination: Add gpuid module parameter Dan Carpenter
2026-02-06 13:39   ` [bug report] ASoC: SOF: ipc4-control: Add support for generic bytes control Dan Carpenter
2026-02-06 13:39   ` [bug report] media: iris: gen1: Destroy internal buffers after FW releases Dan Carpenter
2026-02-06 13:39   ` [bug report] cifs: Fix locking usage for tcon fields Dan Carpenter
2026-02-06 13:40   ` [bug report] drm/xe: Avoid toggling schedule state to check LRC timestamp in TDR Dan Carpenter
2026-02-06 13:40   ` [bug report] iio: dac: adding support for Microchip MCP47FEB02 Dan Carpenter
2026-02-06 14:04     ` Andy Shevchenko
2026-02-06 14:33       ` Dan Carpenter
2026-02-06 15:14         ` Andy Shevchenko
2026-02-06 15:32           ` Dan Carpenter
2026-02-06 15:57             ` Andy Shevchenko
2026-02-10 10:26               ` Ariana.Lazar
2026-03-01 12:31                 ` Jonathan Cameron
2026-03-02 10:28                   ` Ariana.Lazar
2026-03-03 21:41                     ` Jonathan Cameron
2026-02-06 13:40   ` [bug report] power: sequencing: qcom-wcn: add support for WCN39xx Dan Carpenter
2026-02-06 13:40   ` [bug report] io_uring: add task fork hook Dan Carpenter
2026-02-06 14:28     ` Jens Axboe
2026-02-06 13:40   ` [bug report] ACPI: battery: Adjust event notification routine Dan Carpenter
2026-02-06 21:28     ` [PATCH v1] ACPI: battery: Drop redundant check from acpi_battery_notify() Rafael J. Wysocki
2026-02-06 13:40   ` [bug report] iio: adc: Add support for ad4062 Dan Carpenter
2026-02-06 14:07     ` Andy Shevchenko
2026-03-01 12:34       ` Jonathan Cameron
2026-03-05 17:10         ` Jorge Marques
2026-02-06 13:40   ` [bug report] ext4: refactor zeroout path and handle all cases Dan Carpenter
2026-02-06 15:44     ` Ojaswin Mujoo
2026-02-06 13:40   ` [bug report] media: chips-media: wave5: Fix Null reference while testing fluster Dan Carpenter
2026-02-11  7:59     ` Nas Chung
2026-02-06 13:40   ` [bug report] phy: apple: Add Apple Type-C PHY Dan Carpenter
2026-02-06 21:47     ` Janne Grunau
2026-02-06 21:48       ` Sven Peter
2026-02-06 13:40   ` [bug report] spi: stm32: properly fail on dma_request_chan error Dan Carpenter
2026-02-06 13:40   ` [bug report] tracing: Properly process error handling in event_hist_trigger_parse() Dan Carpenter
2026-02-06 13:40   ` [bug report] drm/amd/display: Only poll analog connectors Dan Carpenter
2026-02-06 13:41   ` [bug report] fs/ntfs3: Add initialization of super block Dan Carpenter
2026-02-09 10:20     ` Konstantin Komarov
2026-02-09 15:35     ` [PATCH] (resend: correct threading) fs/ntfs3: avoid calling run_get_entry() when run == NULL in ntfs_read_run_nb_ra() Konstantin Komarov
2026-02-06 13:41   ` [bug report] remoteproc: imx_rproc: Introduce prepare ops for imx_rproc_dcfg Dan Carpenter
2026-02-06 16:29     ` Mathieu Poirier
2026-02-08 11:45     ` Peng Fan
2026-02-06 13:41   ` [bug report] irqchip/ls-extirq: Convert to a platform driver to make it work again Dan Carpenter
2026-02-06 13:41   ` [bug report] soc: rockchip: grf: Support multiple grf to be handled Dan Carpenter
2026-02-06 13:41   ` Dan Carpenter [this message]
2026-02-06 13:41   ` [bug report] bio: add allocation cache abstraction Dan Carpenter
2026-02-06 13:41   ` [bug report] ASoC: codecs: ACF bin parsing and check library file for aw88395 Dan Carpenter
2026-02-06 13:41   ` [bug report] xfrm: always fail xfrm_dev_{state,policy}_flush_secctx_check() Dan Carpenter
2026-02-06 14:05     ` Tetsuo Handa
2026-02-09  9:43   ` [bug report] wifi: mwifiex: Allocate dev name earlier for interface workqueue name Dan Carpenter
2026-02-09  9:44   ` [bug report] apparmor: add support loading per permission tagging Dan Carpenter
2026-02-10 17:15     ` [apparmor][PATCH] apparmor: fix signedness bug in unpack_tags() Massimiliano Pellizzer
2026-02-09  9:45   ` [bug report] regulator: s2mps11: add S2MPG10 regulator Dan Carpenter
2026-02-09 14:07     ` André Draszik
2026-02-10  8:43   ` [bug report] btrfs: tests: zoned: add tests cases for zoned code Dan Carpenter
2026-02-10 19:05     ` David Sterba
2026-02-10  8:51   ` [bug report] ASoC: SOF: sof-audio: Add support for loopback capture Dan Carpenter
2026-02-13  5:56   ` [bug report] bpf: Fix a potential use-after-free of BTF object Dan Carpenter
2026-02-13 10:29     ` Anton Protopopov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aYXvhj1fJ8ZFGyV4@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=SRINIVASAN.SHANMUGAM@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome