From mboxrd@z Thu Jan 1 00:00:00 1970 From: Arun Iyengar Date: Sun, 06 Dec 2026 07:17:15 +0000 Subject: [PATCH v4 4/7] accel/rocket: Serialise core probe and removal MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-rocket-fixes-v4-4-75a6279bce98@gmail.com> References: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> In-Reply-To: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> To: Tomeu Vizoso , Oded Gabbay Cc: Heiko Stuebner , Jeff Hugo , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , Danilo Krummrich , Matthew Brost , Igor Paunovic , Midgy BALON , Jiaxing Hu , dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, stable@vger.kernel.org, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796541431; l=5088; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=vUHeBL2FjxTf2Sr4BKGY7wyPetgaSdlPQeR0CRhnadM=; b=8kpnBCugrg1GXNNjy2TtC2RqcWPgoibv6xLE/x3wpUs0SDSkFAucEfcGIXQuX1mFYu9df0/cv +rNOCGrxk6UAmyn+BXNgXbhUh0LDD8EQFcyXHXcNivfqUu9bt7Mld/X X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 List-Id: B4 Relay Submissions Igor Paunovic posted the slot part in "accel/rocket: keep core slots stable across unbind and rebind" [1]. rocket_probe() takes slot num_cores, and rocket_remove() leaves the slot's .dev set. Rebinding any core but the last probed one overwrites a bound core's slot, and find_core_for_dev() then matches the stale slot. rocket_open() uses cores[0].dev, which may be unbound, and can put a core whose probe is still running in the file's entity: drm_sched_init() marks it ready before runtime PM is enabled. Two unbinds through sysfs can run at once and race on num_cores. Take the first free slot, clear it at removal, open on a bound core, and serialise probe, removal and open under a mutex. Link: https://lore.kernel.org/all/20260922080114.44662-5-royalnet026@gmail.com/ [1] Fixes: ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU") Cc: stable@vger.kernel.org Cc: Igor Paunovic Signed-off-by: Arun Iyengar --- drivers/accel/rocket/rocket_drv.c | 55 ++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index e45b3c22b31e..ccaedb004d19 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ static struct platform_device *drm_dev; static struct rocket_device *rdev; static void *rdev_group; +/* Serialises core probe, removal and open */ +static DEFINE_MUTEX(rocket_cores_lock); static void rocket_vm_destroy(struct kref *kref) @@ -83,6 +86,7 @@ rocket_open(struct drm_device *dev, struct drm_file *file) { struct rocket_device *rdev = to_rocket_device(dev); struct rocket_file_priv *rocket_priv; + unsigned int core; int ret; if (!try_module_get(THIS_MODULE)) @@ -95,17 +99,28 @@ rocket_open(struct drm_device *dev, struct drm_file *file) } rocket_priv->rdev = rdev; - rocket_priv->vm = rocket_vm_create(rdev->cores[0].dev); - if (IS_ERR(rocket_priv->vm)) { - ret = PTR_ERR(rocket_priv->vm); - goto err_free; - } + rocket_priv->vm = ERR_PTR(-ENODEV); + + scoped_guard(mutex, &rocket_cores_lock) { + for (core = 0; core < rdev->max_cores; core++) { + if (rdev->cores[core].dev) { + rocket_priv->vm = rocket_vm_create(rdev->cores[core].dev); + break; + } + } - file->driver_priv = rocket_priv; + if (IS_ERR(rocket_priv->vm)) { + ret = PTR_ERR(rocket_priv->vm); + goto err_free; + } - ret = rocket_job_open(rocket_priv); - if (ret) - goto err_vm_put; + file->driver_priv = rocket_priv; + + /* Not on a core whose probe is still running */ + ret = rocket_job_open(rocket_priv); + if (ret) + goto err_vm_put; + } return 0; @@ -195,11 +210,14 @@ static int rocket_core_hw_index(struct device *dev) static int rocket_probe(struct platform_device *pdev) { int index = rocket_core_hw_index(&pdev->dev); + unsigned int core; int ret; if (index < 0) return index; + guard(mutex)(&rocket_cores_lock); + if (rdev == NULL) { /* First core probing, initialize DRM device. */ rdev_group = devres_open_group(&drm_dev->dev, NULL, GFP_KERNEL); @@ -217,18 +235,25 @@ static int rocket_probe(struct platform_device *pdev) devres_close_group(&drm_dev->dev, rdev_group); } - unsigned int core = rdev->num_cores; + /* Cores can unbind and rebind in any order */ + for (core = 0; core < rdev->max_cores; core++) + if (!rdev->cores[core].dev) + break; + if (WARN_ON(core == rdev->max_cores)) + return -EBUSY; dev_set_drvdata(&pdev->dev, rdev); rdev->cores[core].rdev = rdev; - rdev->cores[core].dev = &pdev->dev; + /* Runtime PM looks the core up without the lock */ + WRITE_ONCE(rdev->cores[core].dev, &pdev->dev); rdev->cores[core].index = index; rdev->num_cores++; ret = rocket_core_init(&rdev->cores[core]); if (ret) { + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -246,12 +271,16 @@ static int find_core_for_dev(struct device *dev); static void rocket_remove(struct platform_device *pdev) { struct device *dev = &pdev->dev; - int core = find_core_for_dev(dev); + int core; + + guard(mutex)(&rocket_cores_lock); + core = find_core_for_dev(dev); if (core < 0) return; rocket_core_fini(&rdev->cores[core]); + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -273,7 +302,7 @@ static int find_core_for_dev(struct device *dev) struct rocket_device *rdev = dev_get_drvdata(dev); for (unsigned int core = 0; core < rdev->max_cores; core++) { - if (dev == rdev->cores[core].dev) + if (dev == READ_ONCE(rdev->cores[core].dev)) return core; } -- 2.55.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ECAA606A684 for ; Sun, 6 Dec 2026 07:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=nnerLCXq; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cCTlYRBP DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Reply-To:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References: Message-Id:MIME-Version:Subject:Date:From:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=dYsxRc0unr9GUS2DZoyWe1A2KY/ic2ykLbegNzVDCKk=; b=nnerLCXqOEAKi9V2g7zR+DHM3H vNmEIoE6F9DIv+yW4oWrvV/1C247WFrv1yUANV+L5rrK8zKs3mW2DpggWKrwK3ltNWdE5jjmZqmVV ba+deyZuokclEtEm/gE7n4pKxJ0YOceBwpO8UBSQScIkHrfiFU95WT8W+fRG6F3fAy0tYFt8pyuRa jDykVk0+jB3vnav6wv4UWyLfbIjzqcVt2uqAs0x0/2QKlYYX3DK7sHWhODyEMaM5kbsIbjIEOAOEO fUzR8uWXsRJCOXq9ZJnrxcUUrNfgUdGg/B5bIX+E7LSZlaffscupuhuQi4YMDGBwEbmIzLUtMZJNy dczJlh4g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xa6Un-00000003MTu-3skp; Sun, 06 Dec 2026 07:17:17 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xa6Ul-00000003MSa-3ZJE for linux-rockchip@lists.infradead.org; Sun, 06 Dec 2026 07:17:15 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id E290360D53; Sun, 6 Dec 2026 07:17:14 +0000 (UTC) Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796541434; b=cykbZfm3DPcvsgoFHDbXHtX3ghwJaFnnJ5vcEvbojxi+n+SdCIRB8znPk8gA9Q6Bnn4u 5VKx/DdoAKW5LF/mqsFr2b1x52pWQA9H32SPwq/zAXys4nwzJzjtDOLkA/98vE84toSZx enOHJlurnl7ljeeW84TEhCKbMEzxRIwLI24y3FPSvM7u/qrMVln/SKmLBejXjtr2eyNy1 kNj++SJTs5aYH2Ybnk2BzUPRPcUo/pEyCNkY0NlMHJFnJHXsZ3hnCkect5xcl9/byEPOq hiBkjBbYklWELPkz1uGVryC0mMw7tWuiCoHHLCLxSW2/EUNEipNX0lOYOjlWMZ0NzAA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796541434; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Ac1fJY9RbR+BYUyvgbnoAXcDkwt2rjcwNkpWwezjj2M=; b=Y/yGpBlIVoHprlp0qB01WyLKYR6TxRLpGhh+vjAWoygoAGC8nxClvcQFBgMz2e3vU+f9 90IMHL4gQj9vZPX6WkDPG8NS4Cr+NV5SIaIm97ms67e2Qq5mpgIIl101UXRSc/Whb8Qnb MaDEyDyt0Prv8FljEB7+UWZFAK6quWB/ACufShWtrifYcxE/T3F/d5DJ999tkLjiwSCMx wA9ho0BJmjn5isvBfNht47l4LUJUesAufx7lQ7BUF7Xpo86dwKtbfO8oc0RcfeLndC0RE Rxe2mxZHMZQmkvy5P2+MnxzeURXmiCRIWuOi78bEzdNzXAi8VxM9frH/eDZI0ezp+UQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id B4F611F00A12; Sun, 6 Dec 2026 07:17:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796541434; bh=Ac1fJY9RbR+BYUyvgbnoAXcDkwt2rjcwNkpWwezjj2M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=cCTlYRBPizR5n2uZcsU4OT0F4vgPdVDUIkOZQHPccj7Cw1sDS7x+0/zoXIBoGNTOv JXi7DDyHyluz2AxnRKQkWAxF7SAj7796GJ77ch6t/nGgmoQRU3yk+SY92nErTiojsb 2Fqm5LlEyDFb8uzc2004uL3M0ERXTZaP3uBvP+AZ6sCWh3N98jOkMBHKdNOo2O41tw F2IsO+HaJnz19taAi7FobvZpAJI7L5ZGQYPkWipNTXLO72Vn7oUl/WCInC3mRO61oE z6GulPCJ3uLnA3b+vZNRv0LdI9YSw3np8EQkqaMhF9SKrNTCZfRrGjwWbNbiigGFgm ihN2bOsPEi9iA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3A9C250681; Sun, 6 Dec 2026 07:17:13 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Sun, 06 Dec 2026 07:17:15 +0000 Subject: [PATCH v4 4/7] accel/rocket: Serialise core probe and removal X-Archived-By: MIME-Version: 1.0 Message-Id: <20261206-rocket-fixes-v4-4-75a6279bce98@gmail.com> References: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> In-Reply-To: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> To: Tomeu Vizoso , Oded Gabbay Cc: Heiko Stuebner , Jeff Hugo , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , Danilo Krummrich , Matthew Brost , Igor Paunovic , Midgy BALON , Jiaxing Hu , dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, stable@vger.kernel.org, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796541431; l=5088; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=vUHeBL2FjxTf2Sr4BKGY7wyPetgaSdlPQeR0CRhnadM=; b=8kpnBCugrg1GXNNjy2TtC2RqcWPgoibv6xLE/x3wpUs0SDSkFAucEfcGIXQuX1mFYu9df0/cv +rNOCGrxk6UAmyn+BXNgXbhUh0LDD8EQFcyXHXcNivfqUu9bt7Mld/X X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 X-Original-From: Arun Iyengar X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: aiyengar1990@gmail.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-rockchip" X-Spam-Status: No, score=-15.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,FREEMAIL_REPLYTO_END_DIGIT, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Arun Iyengar Igor Paunovic posted the slot part in "accel/rocket: keep core slots stable across unbind and rebind" [1]. rocket_probe() takes slot num_cores, and rocket_remove() leaves the slot's .dev set. Rebinding any core but the last probed one overwrites a bound core's slot, and find_core_for_dev() then matches the stale slot. rocket_open() uses cores[0].dev, which may be unbound, and can put a core whose probe is still running in the file's entity: drm_sched_init() marks it ready before runtime PM is enabled. Two unbinds through sysfs can run at once and race on num_cores. Take the first free slot, clear it at removal, open on a bound core, and serialise probe, removal and open under a mutex. Link: https://lore.kernel.org/all/20260922080114.44662-5-royalnet026@gmail.com/ [1] Fixes: ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU") Cc: stable@vger.kernel.org Cc: Igor Paunovic Signed-off-by: Arun Iyengar --- drivers/accel/rocket/rocket_drv.c | 55 ++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index e45b3c22b31e..ccaedb004d19 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ static struct platform_device *drm_dev; static struct rocket_device *rdev; static void *rdev_group; +/* Serialises core probe, removal and open */ +static DEFINE_MUTEX(rocket_cores_lock); static void rocket_vm_destroy(struct kref *kref) @@ -83,6 +86,7 @@ rocket_open(struct drm_device *dev, struct drm_file *file) { struct rocket_device *rdev = to_rocket_device(dev); struct rocket_file_priv *rocket_priv; + unsigned int core; int ret; if (!try_module_get(THIS_MODULE)) @@ -95,17 +99,28 @@ rocket_open(struct drm_device *dev, struct drm_file *file) } rocket_priv->rdev = rdev; - rocket_priv->vm = rocket_vm_create(rdev->cores[0].dev); - if (IS_ERR(rocket_priv->vm)) { - ret = PTR_ERR(rocket_priv->vm); - goto err_free; - } + rocket_priv->vm = ERR_PTR(-ENODEV); + + scoped_guard(mutex, &rocket_cores_lock) { + for (core = 0; core < rdev->max_cores; core++) { + if (rdev->cores[core].dev) { + rocket_priv->vm = rocket_vm_create(rdev->cores[core].dev); + break; + } + } - file->driver_priv = rocket_priv; + if (IS_ERR(rocket_priv->vm)) { + ret = PTR_ERR(rocket_priv->vm); + goto err_free; + } - ret = rocket_job_open(rocket_priv); - if (ret) - goto err_vm_put; + file->driver_priv = rocket_priv; + + /* Not on a core whose probe is still running */ + ret = rocket_job_open(rocket_priv); + if (ret) + goto err_vm_put; + } return 0; @@ -195,11 +210,14 @@ static int rocket_core_hw_index(struct device *dev) static int rocket_probe(struct platform_device *pdev) { int index = rocket_core_hw_index(&pdev->dev); + unsigned int core; int ret; if (index < 0) return index; + guard(mutex)(&rocket_cores_lock); + if (rdev == NULL) { /* First core probing, initialize DRM device. */ rdev_group = devres_open_group(&drm_dev->dev, NULL, GFP_KERNEL); @@ -217,18 +235,25 @@ static int rocket_probe(struct platform_device *pdev) devres_close_group(&drm_dev->dev, rdev_group); } - unsigned int core = rdev->num_cores; + /* Cores can unbind and rebind in any order */ + for (core = 0; core < rdev->max_cores; core++) + if (!rdev->cores[core].dev) + break; + if (WARN_ON(core == rdev->max_cores)) + return -EBUSY; dev_set_drvdata(&pdev->dev, rdev); rdev->cores[core].rdev = rdev; - rdev->cores[core].dev = &pdev->dev; + /* Runtime PM looks the core up without the lock */ + WRITE_ONCE(rdev->cores[core].dev, &pdev->dev); rdev->cores[core].index = index; rdev->num_cores++; ret = rocket_core_init(&rdev->cores[core]); if (ret) { + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -246,12 +271,16 @@ static int find_core_for_dev(struct device *dev); static void rocket_remove(struct platform_device *pdev) { struct device *dev = &pdev->dev; - int core = find_core_for_dev(dev); + int core; + + guard(mutex)(&rocket_cores_lock); + core = find_core_for_dev(dev); if (core < 0) return; rocket_core_fini(&rdev->cores[core]); + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -273,7 +302,7 @@ static int find_core_for_dev(struct device *dev) struct rocket_device *rdev = dev_get_drvdata(dev); for (unsigned int core = 0; core < rdev->max_cores; core++) { - if (dev == rdev->cores[core].dev) + if (dev == READ_ONCE(rdev->cores[core].dev)) return core; } -- 2.55.0 _______________________________________________ Linux-rockchip mailing list Linux-rockchip@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-rockchip From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796541434; b=XU0XaoeBalxJj26LECT38VMTaiUpm8UkPoxRl/YFagqTHCMOw5gFBmiRerwicqnwCyE/ GQ3t/RovmCogRc4A89Lwp8LTRkHCsAs7EPjPsqvAd4jJ8ZJCYJqiHl9kkD28bmsnOaggy oTp5jltS6BhtH4eVJxx+ohf3+tW39tL97ELMT7PnM9X7QFrs3G5xLmF6tDlFkr6cdZXQR SNtrYUDivlGEXvnGJntQf8m0Rit04ib1kX3GDfTjmzuisBcaGVfpuYmpxQ0mvwQAUZ769 IEaHZvrddtiZerIJh03hpv4HFqWm5nKMTPXk0rOqAQFLfMNGalrmQZUrF50fgy5tN8w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796541434; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Ac1fJY9RbR+BYUyvgbnoAXcDkwt2rjcwNkpWwezjj2M=; b=CjHbY5veqzmQUaMSUQZMwOdi8i5WFEP+uYNTmY0DdGvjLmf4dCmnvFd+w+wppHhxepBb yJ3NWAVszbPRRYtrzM7E62hLDaa7XwEhysLVRtYvNJyXSCOyjODcIasL4MvDAw46lYrp4 1N4Fcqov9FBk2sXKOOH3/8SnfwBIx0eYaoo6JCe+jLEt6AZ7JW6Zwl6xv8jC3tgOVQkvB OzkiPb5ewfg6ouD+Ed1uXSKOnkXQLujv1Tx+eV/qujLOixkyb58I3LowCEPQAtcoebmjT yt1NmkgilqcLviqxRqOGJ0G8m/du/trJF3enuyKCiIctr5SezaMTFqB1C185Pk/x4Pg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cCTlYRBP; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF819606A685; Sun, 6 Dec 2026 07:17:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cCTlYRBP Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796541434; b=cykbZfm3DPcvsgoFHDbXHtX3ghwJaFnnJ5vcEvbojxi+n+SdCIRB8znPk8gA9Q6Bnn4u 5VKx/DdoAKW5LF/mqsFr2b1x52pWQA9H32SPwq/zAXys4nwzJzjtDOLkA/98vE84toSZx enOHJlurnl7ljeeW84TEhCKbMEzxRIwLI24y3FPSvM7u/qrMVln/SKmLBejXjtr2eyNy1 kNj++SJTs5aYH2Ybnk2BzUPRPcUo/pEyCNkY0NlMHJFnJHXsZ3hnCkect5xcl9/byEPOq hiBkjBbYklWELPkz1uGVryC0mMw7tWuiCoHHLCLxSW2/EUNEipNX0lOYOjlWMZ0NzAA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796541434; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=Ac1fJY9RbR+BYUyvgbnoAXcDkwt2rjcwNkpWwezjj2M=; b=Y/yGpBlIVoHprlp0qB01WyLKYR6TxRLpGhh+vjAWoygoAGC8nxClvcQFBgMz2e3vU+f9 90IMHL4gQj9vZPX6WkDPG8NS4Cr+NV5SIaIm97ms67e2Qq5mpgIIl101UXRSc/Whb8Qnb MaDEyDyt0Prv8FljEB7+UWZFAK6quWB/ACufShWtrifYcxE/T3F/d5DJ999tkLjiwSCMx wA9ho0BJmjn5isvBfNht47l4LUJUesAufx7lQ7BUF7Xpo86dwKtbfO8oc0RcfeLndC0RE Rxe2mxZHMZQmkvy5P2+MnxzeURXmiCRIWuOi78bEzdNzXAi8VxM9frH/eDZI0ezp+UQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id B4F611F00A12; Sun, 6 Dec 2026 07:17:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796541434; bh=Ac1fJY9RbR+BYUyvgbnoAXcDkwt2rjcwNkpWwezjj2M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=cCTlYRBPizR5n2uZcsU4OT0F4vgPdVDUIkOZQHPccj7Cw1sDS7x+0/zoXIBoGNTOv JXi7DDyHyluz2AxnRKQkWAxF7SAj7796GJ77ch6t/nGgmoQRU3yk+SY92nErTiojsb 2Fqm5LlEyDFb8uzc2004uL3M0ERXTZaP3uBvP+AZ6sCWh3N98jOkMBHKdNOo2O41tw F2IsO+HaJnz19taAi7FobvZpAJI7L5ZGQYPkWipNTXLO72Vn7oUl/WCInC3mRO61oE z6GulPCJ3uLnA3b+vZNRv0LdI9YSw3np8EQkqaMhF9SKrNTCZfRrGjwWbNbiigGFgm ihN2bOsPEi9iA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 3A9C250681; Sun, 6 Dec 2026 07:17:13 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Sun, 06 Dec 2026 07:17:15 +0000 Subject: [PATCH v4 4/7] accel/rocket: Serialise core probe and removal X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261206-rocket-fixes-v4-4-75a6279bce98@gmail.com> References: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> In-Reply-To: <20261206-rocket-fixes-v4-0-75a6279bce98@gmail.com> To: Tomeu Vizoso , Oded Gabbay Cc: Heiko Stuebner , Jeff Hugo , Sumit Semwal , =?utf-8?q?Christian_K=C3=B6nig?= , Philipp Stanner , Danilo Krummrich , Matthew Brost , Igor Paunovic , Midgy BALON , Jiaxing Hu , dri-devel@lists.freedesktop.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org, stable@vger.kernel.org, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796541431; l=5088; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=vUHeBL2FjxTf2Sr4BKGY7wyPetgaSdlPQeR0CRhnadM=; b=8kpnBCugrg1GXNNjy2TtC2RqcWPgoibv6xLE/x3wpUs0SDSkFAucEfcGIXQuX1mFYu9df0/cv +rNOCGrxk6UAmyn+BXNgXbhUh0LDD8EQFcyXHXcNivfqUu9bt7Mld/X X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 X-Original-From: Arun Iyengar Reply-To: aiyengar1990@gmail.com Content-Transfer-Encoding: 7bit From: Arun Iyengar Igor Paunovic posted the slot part in "accel/rocket: keep core slots stable across unbind and rebind" [1]. rocket_probe() takes slot num_cores, and rocket_remove() leaves the slot's .dev set. Rebinding any core but the last probed one overwrites a bound core's slot, and find_core_for_dev() then matches the stale slot. rocket_open() uses cores[0].dev, which may be unbound, and can put a core whose probe is still running in the file's entity: drm_sched_init() marks it ready before runtime PM is enabled. Two unbinds through sysfs can run at once and race on num_cores. Take the first free slot, clear it at removal, open on a bound core, and serialise probe, removal and open under a mutex. Link: https://lore.kernel.org/all/20260922080114.44662-5-royalnet026@gmail.com/ [1] Fixes: ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU") Cc: stable@vger.kernel.org Cc: Igor Paunovic Signed-off-by: Arun Iyengar --- drivers/accel/rocket/rocket_drv.c | 55 ++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index e45b3c22b31e..ccaedb004d19 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,8 @@ static struct platform_device *drm_dev; static struct rocket_device *rdev; static void *rdev_group; +/* Serialises core probe, removal and open */ +static DEFINE_MUTEX(rocket_cores_lock); static void rocket_vm_destroy(struct kref *kref) @@ -83,6 +86,7 @@ rocket_open(struct drm_device *dev, struct drm_file *file) { struct rocket_device *rdev = to_rocket_device(dev); struct rocket_file_priv *rocket_priv; + unsigned int core; int ret; if (!try_module_get(THIS_MODULE)) @@ -95,17 +99,28 @@ rocket_open(struct drm_device *dev, struct drm_file *file) } rocket_priv->rdev = rdev; - rocket_priv->vm = rocket_vm_create(rdev->cores[0].dev); - if (IS_ERR(rocket_priv->vm)) { - ret = PTR_ERR(rocket_priv->vm); - goto err_free; - } + rocket_priv->vm = ERR_PTR(-ENODEV); + + scoped_guard(mutex, &rocket_cores_lock) { + for (core = 0; core < rdev->max_cores; core++) { + if (rdev->cores[core].dev) { + rocket_priv->vm = rocket_vm_create(rdev->cores[core].dev); + break; + } + } - file->driver_priv = rocket_priv; + if (IS_ERR(rocket_priv->vm)) { + ret = PTR_ERR(rocket_priv->vm); + goto err_free; + } - ret = rocket_job_open(rocket_priv); - if (ret) - goto err_vm_put; + file->driver_priv = rocket_priv; + + /* Not on a core whose probe is still running */ + ret = rocket_job_open(rocket_priv); + if (ret) + goto err_vm_put; + } return 0; @@ -195,11 +210,14 @@ static int rocket_core_hw_index(struct device *dev) static int rocket_probe(struct platform_device *pdev) { int index = rocket_core_hw_index(&pdev->dev); + unsigned int core; int ret; if (index < 0) return index; + guard(mutex)(&rocket_cores_lock); + if (rdev == NULL) { /* First core probing, initialize DRM device. */ rdev_group = devres_open_group(&drm_dev->dev, NULL, GFP_KERNEL); @@ -217,18 +235,25 @@ static int rocket_probe(struct platform_device *pdev) devres_close_group(&drm_dev->dev, rdev_group); } - unsigned int core = rdev->num_cores; + /* Cores can unbind and rebind in any order */ + for (core = 0; core < rdev->max_cores; core++) + if (!rdev->cores[core].dev) + break; + if (WARN_ON(core == rdev->max_cores)) + return -EBUSY; dev_set_drvdata(&pdev->dev, rdev); rdev->cores[core].rdev = rdev; - rdev->cores[core].dev = &pdev->dev; + /* Runtime PM looks the core up without the lock */ + WRITE_ONCE(rdev->cores[core].dev, &pdev->dev); rdev->cores[core].index = index; rdev->num_cores++; ret = rocket_core_init(&rdev->cores[core]); if (ret) { + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -246,12 +271,16 @@ static int find_core_for_dev(struct device *dev); static void rocket_remove(struct platform_device *pdev) { struct device *dev = &pdev->dev; - int core = find_core_for_dev(dev); + int core; + + guard(mutex)(&rocket_cores_lock); + core = find_core_for_dev(dev); if (core < 0) return; rocket_core_fini(&rdev->cores[core]); + WRITE_ONCE(rdev->cores[core].dev, NULL); rdev->num_cores--; if (rdev->num_cores == 0) { @@ -273,7 +302,7 @@ static int find_core_for_dev(struct device *dev) struct rocket_device *rdev = dev_get_drvdata(dev); for (unsigned int core = 0; core < rdev->max_cores; core++) { - if (dev == rdev->cores[core].dev) + if (dev == READ_ONCE(rdev->cores[core].dev)) return core; } -- 2.55.0