From patchwork Thu Jul 8 21:54:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 12366281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67BE9C07E96 for ; Thu, 8 Jul 2021 21:54:48 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D5A0261450 for ; Thu, 8 Jul 2021 21:54:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5A0261450 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ffwll.ch Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6BF2C6E90C; Thu, 8 Jul 2021 21:54:47 +0000 (UTC) Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by gabe.freedesktop.org (Postfix) with ESMTPS id 46E1B6E8FF for ; Thu, 8 Jul 2021 21:54:46 +0000 (UTC) Received: by mail-ej1-x636.google.com with SMTP id o5so12309588ejy.7 for ; Thu, 08 Jul 2021 14:54:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=d1w93YeUX+1iq+sUD/eLjhVo09cAXuAFNyzb5+/PtX8=; b=AOaW9lRzd+A1i5+zCfXF2et/fkCG3+e1jzD9l6EGMpAMRCbDI2g8RtEEeELlakqjrQ FfjquAzlGIGCyPWOsaRi8yGmEtOWzfK9bXPwnT7LhcoiZ8ELHJPTBTdO5IfQv/Af1YN9 UwQw9QVw5gOxh+dhd9y8DgPwAXAGq6nP32IYM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d1w93YeUX+1iq+sUD/eLjhVo09cAXuAFNyzb5+/PtX8=; b=XPx7JZjFBzWYoSTVJSKy32RHCi9c7/JUZeggUAuqiNjDLeYMg7PQx/TMq9NFXcVnZi HoRSqZrueOaemrYEihvJfpgD2wP3FTivRfvcbCuZ38sRudMjYSOPTzsZTIo26s83/Wrv m1Lydl1iZ+JoPyEGbXAstqeKF1RBQm/FkQdtqM7WK9U4v2RUowGwcwNcqwidl5CCAGaC bHVioq0WLtTJuXVpmueK4t5ZuQZODYtj90rv7IsZOohyrdbTrYrPhCfxJnHwuNFgcpIY D2wAlYjyZIOLRb9svZWKi9kKZTIcue9K2ElqWxM5A9QgNuxCMRcfNr6XtJhtSUYyO33p y7rQ== X-Gm-Message-State: AOAM531Dh6QqWLSGZave+ELY5Fq02q5baznrQgLBcpo4SwW79b98bbQM 8KOwz1tmvDEZRf+baiJHuWID7g== X-Google-Smtp-Source: ABdhPJytUe48vXWCnbKLcG9uXFEIys7ziaSlLcRL//xsMtv0kdYbzJ/NtS4SJACwnSrbDNJz2/+qSg== X-Received: by 2002:a17:906:3919:: with SMTP id f25mr12129311eje.190.1625781284650; Thu, 08 Jul 2021 14:54:44 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id s26sm421243ejv.87.2021.07.08.14.54.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jul 2021 14:54:43 -0700 (PDT) From: Daniel Vetter To: DRI Development Date: Thu, 8 Jul 2021 23:54:39 +0200 Message-Id: <20210708215439.4093557-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210708173754.3877540-4-daniel.vetter@ffwll.ch> References: <20210708173754.3877540-4-daniel.vetter@ffwll.ch> MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH] drm/sched: Barriers are needed for entity->last_scheduled X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Andrey Grodzovsky , Daniel Vetter , Intel Graphics Development , Steven Price , Daniel Vetter , Lee Jones , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" It might be good enough on x86 with just READ_ONCE, but the write side should then at least be WRITE_ONCE because x86 has total store order. It's definitely not enough on arm. Fix this proplery, which means - explain the need for the barrier in both places - point at the other side in each comment Also pull out the !sched_list case as the first check, so that the code flow is clearer. While at it sprinkle some comments around because it was very non-obvious to me what's actually going on here and why. Note that we really need full barriers here, at first I thought store-release and load-acquire on ->last_scheduled would be enough, but we actually requiring ordering between that and the queue state. v2: Put smp_rmp() in the right place and fix up comment (Andrey) Signed-off-by: Daniel Vetter Cc: "Christian König" Cc: Steven Price Cc: Daniel Vetter Cc: Andrey Grodzovsky Cc: Lee Jones Cc: Boris Brezillon --- drivers/gpu/drm/scheduler/sched_entity.c | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c index 64d398166644..6366006c0fcf 100644 --- a/drivers/gpu/drm/scheduler/sched_entity.c +++ b/drivers/gpu/drm/scheduler/sched_entity.c @@ -439,8 +439,16 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED); dma_fence_put(entity->last_scheduled); + entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished); + /* + * If the queue is empty we allow drm_sched_entity_select_rq() to + * locklessly access ->last_scheduled. This only works if we set the + * pointer before we dequeue and if we a write barrier here. + */ + smp_wmb(); + spsc_queue_pop(&entity->job_queue); return sched_job; } @@ -459,10 +467,25 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity) struct drm_gpu_scheduler *sched; struct drm_sched_rq *rq; - if (spsc_queue_count(&entity->job_queue) || !entity->sched_list) + /* single possible engine and already selected */ + if (!entity->sched_list) + return; + + /* queue non-empty, stay on the same engine */ + if (spsc_queue_count(&entity->job_queue)) return; - fence = READ_ONCE(entity->last_scheduled); + /* + * Only when the queue is empty are we guaranteed that the scheduler + * thread cannot change ->last_scheduled. To enforce ordering we need + * a read barrier here. See drm_sched_entity_pop_job() for the other + * side. + */ + smp_rmb(); + + fence = entity->last_scheduled; + + /* stay on the same engine if the previous job hasn't finished */ if (fence && !dma_fence_is_signaled(fence)) return;