From patchwork Mon Oct 21 10:50:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13843901 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 28E74D0E6EE for ; Mon, 21 Oct 2024 10:51:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8B7AE10E48D; Mon, 21 Oct 2024 10:51:10 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="a0NVYHjE"; 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 2488A10E496 for ; Mon, 21 Oct 2024 10:51:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729507868; 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=h2ciBpbcKEJEKHld76VWMnvn0NitnUmbu3TROV6N+1g=; b=a0NVYHjEcz24fNzDqFrOcihRuHZwNRxoS6F2Qod2ubQPLD7nF14dDnhCLemlQoHFYSr6kO arqPiXYN9teGXThEp7HeRnCnW1RVaCevtskllr5M9B2BSayHxvBMAI+lm6QxbGBDdXH9VM O3wiERDA0fRavspnfiNNKMQukWZPs90= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-104-7y2op6cBM5a2uJvsMTFnnw-1; Mon, 21 Oct 2024 06:51:07 -0400 X-MC-Unique: 7y2op6cBM5a2uJvsMTFnnw-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43159603c92so30245605e9.2 for ; Mon, 21 Oct 2024 03:51:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729507865; x=1730112665; 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=h2ciBpbcKEJEKHld76VWMnvn0NitnUmbu3TROV6N+1g=; b=Cs7H/0gD+ZyRKVE0aiFz5ywbQQbuaBiJndZZ/X0dZ3T28kEP/3xaZaVNivD0sT3Svo CdzT8GDY3qjgZtwfHNjEcHKOSRN0CPlisEykVdX/cMFusKKM2Iekln6oJcPIuRwvQqS9 hAxDTjg5pUy68QjbZf+bgJQjfb/iboxk1HPL8TdnGtGIoriW/97IMok+Bf/iaEv53F4t Js77JQdAZ4uhhE+WhjXrLzaizm3qVBdvlDN9+9smxWVwVebzqhjNsiuucDrb8mLkVmdy wJB61yWr7i23o7UVjxmAClPDZsNniUUM1hV/j6we4zOyQlTv+vc8j4ud8CqXtACG5wxc ifWA== X-Gm-Message-State: AOJu0YzsKw1aw+gJHqhCveRmHkxGP0tSPCnIwXgZ9JzIGOcV+pNe+vwP RjqnYkTH/cNHlALOKB+KD39N/1ACP1HN2plgAt1xmLnSkjTv2oeqZEdnYa5bd+PID/sBlvGz1Fm nTpdJpr1QwIUVufFEPxDEK7JhdV2FG0DM3cogHzZbgRK2BwlsHSaw7pUsSSrhmw82wQ== X-Received: by 2002:a05:600c:354e:b0:42b:a7c7:5667 with SMTP id 5b1f17b1804b1-431616a3a15mr83928845e9.25.1729507865494; Mon, 21 Oct 2024 03:51:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoDq0zpTx4/a8pJZ3ce7VUx/vHA7i5tc7ntMyJG6ujE23KLHEQO3J1ivFPoUl4rZrPbQ5gBQ== X-Received: by 2002:a05:600c:354e:b0:42b:a7c7:5667 with SMTP id 5b1f17b1804b1-431616a3a15mr83928585e9.25.1729507865021; Mon, 21 Oct 2024 03:51:05 -0700 (PDT) Received: from eisenberg.fritz.box (200116b82d449800aee93296d73e68da.dip.versatel-1u1.de. [2001:16b8:2d44:9800:aee9:3296:d73e:68da]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f57fdb2sm53065615e9.20.2024.10.21.03.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 03:51:04 -0700 (PDT) From: Philipp Stanner To: Luben Tuikov , Matthew Brost , Danilo Krummrich , Philipp Stanner , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , =?utf-8?q?Christian_K=C3=B6nig?= , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] drm/sched: memset() 'job' in drm_sched_job_init() Date: Mon, 21 Oct 2024 12:50:28 +0200 Message-ID: <20241021105028.19794-2-pstanner@redhat.com> X-Mailer: git-send-email 2.47.0 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 --- No changes in v2. +CC Christian and Tvrtko in this thread. Would be cool if someone can do a review. --- 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 dab8cca79eb7..2e0e5a9577d1 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -796,6 +796,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 Mon Oct 21 10:50:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 13843902 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 847E5D0E6EE for ; Mon, 21 Oct 2024 10:51:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EEE8A10E48F; Mon, 21 Oct 2024 10:51:13 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="QmWGEjhV"; 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 018F310E490 for ; Mon, 21 Oct 2024 10:51:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729507870; 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=uERVLrzhOKhLyLR6uH7ER1kb2hRnG0ld4wlyovWxQbk=; b=QmWGEjhVOkJY9AUXQFkE4THkhKxy86At++WieYv4fHq5puGU4VGwDIWYgj7eBIxk405Ph6 8vK3HoTygiYGe+kvSa8IgQDJ4ST3mm4snpncSx4Je+xvxamv+FcwyOE3ZbpNBD3QWLA4o8 7/+41TGWVQJugi0sXqL3OSHozsr9Si4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-88-qH2kJRRSObqtP9n4IspgXg-1; Mon, 21 Oct 2024 06:51:08 -0400 X-MC-Unique: qH2kJRRSObqtP9n4IspgXg-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-431673032e6so17242975e9.0 for ; Mon, 21 Oct 2024 03:51:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729507867; x=1730112667; 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=uERVLrzhOKhLyLR6uH7ER1kb2hRnG0ld4wlyovWxQbk=; b=cEdxIEu66MK6nkPll8nK4b5Q3EjP7I2ggpsn+yTS7qfyoiJAuYHOcZWO7KOR3djt/m k1cYP9RUNN5yrGmJFHz+wAHCOMpE9wJEptnSAFnDa/9m1NrJQa8fnJBX6EAXe05odFEM zMHsRwZqSCDwilNKI7Xt/ojMhhAbNk56V5to0ghXOUDBzhZc8NRfjhse1mehMeZdnaz3 qbwo8R7775lH4nuW90F8KMJnQ0i0uAIhJA9yA3VvDItjdyG3WxV+Ng3OcXuzZkulwUfB VpTPn3uyIo2B3fhS5myB8OxMyEDndoohpMTNOxPePUNqph1rIAgkBy8t0Vyk3xHFeb8E OgcQ== X-Gm-Message-State: AOJu0Yx9EgVA8fT/2m5wdUPGu0lms/gND+bSijMMDRC/dpJn4FefDQ3k t/lm8sBZEBrkEki9n6+jpTTtCncneXmqq+KJirN+rOVikj8tXDlMwIcQXEExm6FbGpeGyCG+qGt 4H4EnuoIYpB0f5xIwTS3PxZVouWCndacRf8fqy/7eRYh+jq+cqJal/vs2P6EEiJUU+A== X-Received: by 2002:a05:600c:4f92:b0:430:57f2:bae2 with SMTP id 5b1f17b1804b1-4316168f5c5mr89908195e9.23.1729507867296; Mon, 21 Oct 2024 03:51:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFPmPGAC/TJn/N01rVgAYAftWJLh4CDyx+85d5RAJFtI4za1CbAHsmGjBAZD5WWq6terZNiEg== X-Received: by 2002:a05:600c:4f92:b0:430:57f2:bae2 with SMTP id 5b1f17b1804b1-4316168f5c5mr89907975e9.23.1729507866946; Mon, 21 Oct 2024 03:51:06 -0700 (PDT) Received: from eisenberg.fritz.box (200116b82d449800aee93296d73e68da.dip.versatel-1u1.de. [2001:16b8:2d44:9800:aee9:3296:d73e:68da]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f57fdb2sm53065615e9.20.2024.10.21.03.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 03:51:06 -0700 (PDT) From: Philipp Stanner To: Luben Tuikov , Matthew Brost , Danilo Krummrich , Philipp Stanner , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , =?utf-8?q?Christian_K=C3=B6nig?= , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] drm/sched: warn about drm_sched_job_init()'s partial init Date: Mon, 21 Oct 2024 12:50:29 +0200 Message-ID: <20241021105028.19794-3-pstanner@redhat.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241021105028.19794-2-pstanner@redhat.com> References: <20241021105028.19794-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 --- Changes in v2: - Change grammar in the new comments a bit. --- 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 2e0e5a9577d1..2f1b514ff4cf 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -771,6 +771,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 ab161289d1bf..f7d9bdd0fb6b 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -340,6 +340,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()" has been called. + */ struct drm_gpu_scheduler *sched; struct drm_sched_fence *s_fence;