From patchwork Tue Aug 6 14:38:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13755000 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 9F1BFC3DA64 for ; Tue, 6 Aug 2024 14:39:09 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E0CF110E396; Tue, 6 Aug 2024 14:39:08 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="JLNW/lID"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6A77310E39C for ; Tue, 6 Aug 2024 14:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722955146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=CC3DtKRmGhH5AipOFzN3At/MyjZL4PoYSf7Q164ZoF4=; b=JLNW/lIDSxxu36TxSzsfLjGu8eqEj78qoUBIgoCJkVkH84cGuIb2RLPjM9W16E7dx0cLaf SzhvqXAFgHlCv2HG1avZHpu7epGksDvosSn7E9JlX7VFJYQtgdNveY1sZABXKPXXX/9hd6 8nxprd2PUPt5yvIvi+TbxCcXULMGZSs= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-196-VKTb-4gGPp-Q4c1Vx0bE7Q-1; Tue, 06 Aug 2024 10:39:05 -0400 X-MC-Unique: VKTb-4gGPp-Q4c1Vx0bE7Q-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2ef2c1db4abso675981fa.3 for ; Tue, 06 Aug 2024 07:39:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722955143; x=1723559943; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CC3DtKRmGhH5AipOFzN3At/MyjZL4PoYSf7Q164ZoF4=; b=NMDJmrDIJZzCWhpukPGXQi+BUSD5yW8+jrZ3rhnGVk4P7w6wHlZCUWjuMKtfgTQ+gc Vlh3XFuSA75kLss/zIZ2bnIfQHjuOqiohtedG1ahZnEXuCmpNFkajX5fymn58ZpqeZWP K306oWjJfEw5BNyeg/QtSUT1cdtsB/Grb3OumWPKaTlGqvg1WsQraC+RCgZ+izCHwSth 8UEuLgSVf2LbLd9sFsroxzLEJsvbw3OmdEPUgikrsSBIeNOWCe6XePjG545rCRm3bpf9 RIF2femvnUjJRkvvhSYinReo2dWETqKKjDsuBmfPoeGbwRJWXFwNOIBPP36v/PKh5kJw dh2Q== X-Gm-Message-State: AOJu0YxRnWHRhEOnNaCZ78G/3XTMMkLq+hOjIYtfHQfPIyYelK7zyo4b rY1sJBnrBMiyLerEGa77g54EzPKR7fC7ta3vsxKgSRfFBpaj5D9fo2RRLGb7u/Xkmc/Rlah2ldf cC+1a5R6f5at7L84FfwNvnFl4030nyY+8ub7H25Ok4FFoOcrIx+PGX2yXaf3okVM1nw== X-Received: by 2002:a05:651c:210f:b0:2f1:5c3d:d12d with SMTP id 38308e7fff4ca-2f15c3dd22amr70169491fa.0.1722955142758; Tue, 06 Aug 2024 07:39:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH5jPI0DyawGRqcu7Ni4RByPLJdoSZNjigyyhoympR0yL332lXRzkYzFAnZqne8pTm24btWHA== X-Received: by 2002:a05:651c:210f:b0:2f1:5c3d:d12d with SMTP id 38308e7fff4ca-2f15c3dd22amr70169271fa.0.1722955142133; Tue, 06 Aug 2024 07:39:02 -0700 (PDT) Received: from eisenberg.fritz.box (200116b82d165b009a97301f486032f8.dip.versatel-1u1.de. [2001:16b8:2d16:5b00:9a97:301f:4860:32f8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e08012d7sm214285905e9.22.2024.08.06.07.39.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Aug 2024 07:39:01 -0700 (PDT) From: Philipp Stanner To: Luben Tuikov , Matthew Brost , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Danilo Krummrich Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Philipp Stanner Subject: [PATCH 1/2] drm/sched: memset() 'job' in drm_sched_job_init() Date: Tue, 6 Aug 2024 16:38:55 +0200 Message-ID: <20240806143855.29789-2-pstanner@redhat.com> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" drm_sched_job_init() has no control over how users allocate struct drm_sched_job. Unfortunately, the function can also not set some struct members such as job->sched. This could theoretically lead to UB by users dereferencing the struct's pointer members too early. It is easier to debug such issues if these pointers are initialized to NULL, so dereferencing them causes a NULL pointer exception. Accordingly, drm_sched_entity_init() does precisely that and initializes its struct with memset(). Initialize parameter "job" to 0 in drm_sched_job_init(). Signed-off-by: Philipp Stanner --- Hi all, I did some experiments with the scheduler recently and am trying to make the documentation and bits of the code more bullet proof. I tested the performance of v6.11-rc2 with and without this memset() by creating 1e6 jobs and found no performance regression. Cheers, P. --- drivers/gpu/drm/scheduler/sched_main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 76969f9c59c2..1498ee3cbf39 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -936,6 +936,14 @@ int drm_sched_job_init(struct drm_sched_job *job, return -EINVAL; } + /* + * We don't know for sure how the user has allocated. Thus, zero the + * struct so that unallowed (i.e., too early) usage of pointers that + * this function does not set is guaranteed to lead to a NULL pointer + * exception instead of UB. + */ + memset(job, 0, sizeof(*job)); + job->entity = entity; job->credits = credits; job->s_fence = drm_sched_fence_alloc(entity, owner); From patchwork Tue Aug 6 14:38:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13755001 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 80C8CC49EA1 for ; Tue, 6 Aug 2024 14:39:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 347ED10E397; Tue, 6 Aug 2024 14:39:09 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="VDmT+qQH"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 65A7A10E399 for ; Tue, 6 Aug 2024 14:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722955146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lulMY4n2fPx2dahXNVvaLbYmQNJeQbtHnB6R7KxHAis=; b=VDmT+qQHq45FyvNBIVcpvCADox685osB3BsHIH3TKtdB3XJxi8B8ozs9Xi1a/RLAmIbgA+ 6dmJbycrwxQ73NFn5uJGXN1Si4s2g8MYgvNtf7XAbQobVOnU2J6ml10ezHoAgwRIhqu9WF Eqqkz41H0RrkaVp2NoKsHtf8suuPlXc= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-503-XjcKeYIiN9WggsvxUeGXEA-1; Tue, 06 Aug 2024 10:39:05 -0400 X-MC-Unique: XjcKeYIiN9WggsvxUeGXEA-1 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-2ef1c0f5966so794051fa.1 for ; Tue, 06 Aug 2024 07:39:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722955143; x=1723559943; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lulMY4n2fPx2dahXNVvaLbYmQNJeQbtHnB6R7KxHAis=; b=EgxpX767gX9zW+ua0/EdHzU3Tq6a+C1AfMX0tuEb7kqk/OsbL8wS1YLrFYXLShoeg2 gGgBAK/k5T4PmlKGT4bZ3XQ8/D2gWxEJSod4ZTKT3argsaRSyj24qslvcq6o3pWcF7f3 TIAuEbRMVxqahdz6fX7EUxrszvmyxCI/MQkSNVA31WG4Dd5Hnt82UC01z7Au7QZsDSUR zkgehkpY8431ptYfRJS3AXzebbpAGRQF2Hqi2uVX0Z0d1F6oA5pSTUJIwMD21eHdTP4q UFVynUuLiT0azgxxV1eFBiKkBTDro7KpuMr8fUCXIWOdXB6AyCcSsUKwbeDJnUZ80Mua eivw== X-Gm-Message-State: AOJu0Ywf4WcynHPfzsgYeH/2V+ft1dxUbKfITv3DtV896vdX93cZ+S9P 7KQMbunW0Zt/hFcWnqw00J93AJYEOW/FyUZhLgcchLTjnGT/5FSp+b5Q29e2Qrl32gXX6/CHPRR Lp+UYwpHV8562xiznF23GD//qMVboY0HGpnWQ9PO30NTHrw4f3iEUoMs7n7G/h3CnsQ== X-Received: by 2002:a2e:87d3:0:b0:2f0:1a95:7102 with SMTP id 38308e7fff4ca-2f15ab1c215mr55822341fa.6.1722955143370; Tue, 06 Aug 2024 07:39:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG6nTV8TMdweugbmRIe2xn+r+NkGNern8stYSFF2V2rA6O68KoNSruXVS5LUUDKeNLhZZguPg== X-Received: by 2002:a2e:87d3:0:b0:2f0:1a95:7102 with SMTP id 38308e7fff4ca-2f15ab1c215mr55822181fa.6.1722955142966; Tue, 06 Aug 2024 07:39:02 -0700 (PDT) Received: from eisenberg.fritz.box (200116b82d165b009a97301f486032f8.dip.versatel-1u1.de. [2001:16b8:2d16:5b00:9a97:301f:4860:32f8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e08012d7sm214285905e9.22.2024.08.06.07.39.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Aug 2024 07:39:02 -0700 (PDT) From: Philipp Stanner To: Luben Tuikov , Matthew Brost , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Danilo Krummrich Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Philipp Stanner Subject: [PATCH 2/2] drm/sched: warn about drm_sched_job_init()'s partial init Date: Tue, 6 Aug 2024 16:38:56 +0200 Message-ID: <20240806143855.29789-3-pstanner@redhat.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240806143855.29789-2-pstanner@redhat.com> References: <20240806143855.29789-2-pstanner@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" drm_sched_job_init()'s name suggests that after the function succeeded, parameter "job" will be fully initialized. This is not the case; some members are only later set, notably "job->sched" by drm_sched_job_arm(). Document that drm_sched_job_init() does not set all struct members. Document that job->sched in particular is uninitialized before drm_sched_job_arm(). Signed-off-by: Philipp Stanner --- drivers/gpu/drm/scheduler/sched_main.c | 4 ++++ include/drm/gpu_scheduler.h | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 1498ee3cbf39..2adb13745500 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -911,6 +911,10 @@ EXPORT_SYMBOL(drm_sched_resubmit_jobs); * Drivers must make sure drm_sched_job_cleanup() if this function returns * successfully, even when @job is aborted before drm_sched_job_arm() is called. * + * Note that this function does not assign a valid value to each struct member + * of struct drm_sched_job. Take a look at that struct's documentation to see + * who sets which struct member with what lifetime. + * * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware * has died, which can mean that there's no valid runqueue for a @entity. * This function returns -ENOENT in this case (which probably should be -EIO as diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h index ce15c50d8a10..7df81a07f1f9 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -337,6 +337,13 @@ struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f); struct drm_sched_job { struct spsc_node queue_node; struct list_head list; + + /* + * The scheduler this job is or will be scheduled on. + * + * Gets set by drm_sched_arm(). Valid until the scheduler's backend_ops + * callback "free_job()" is called. + */ struct drm_gpu_scheduler *sched; struct drm_sched_fence *s_fence;