From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A14133A7D81 for ; Thu, 30 Jul 2026 08:04:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785398658; cv=none; b=IX3sPvdtpSeYL4FzTKzsg3WjXGUxuE1lXy6XicprakwVYcZ3igPCMwrGKVMk3FZ8y4GsXwsk9qeGJoTze8epb2Lu/pAsuA1brYlG0hWTzcldshAJPIRjvknvMUGaP/zWAkaxFD5TcFzvdqxh4tYfsUbDwQJsG2QBRiHJBZ1FmD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785398658; c=relaxed/simple; bh=03lEh5YT/7g53Zpteye4IQuy1UtQ9giqWvL89zrR0a8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jwJUdOgBWdXg4e1JZZk8EL8RFALAQTEZrXZSY3+dS3LCIibqyObisyv59xIFC8J5bk6iZxdcjhSY6XUwtmEOyCbfWTwIL+zX9hyCHRvWCKKGumF6+BaLCBc3+yOa1Xfd0dKQfsqJQZ3HawrV0kdGme36kzVfit2KHT88ujA0wjE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IhZ/MIiQ; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IhZ/MIiQ" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-47f502ff678so255078f8f.0 for ; Thu, 30 Jul 2026 01:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785398654; x=1786003454; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CIA2ESPy6hD2IsIBmPf7dAgmgpqiiLji99VHkKcY/l0=; b=IhZ/MIiQM+OsbB8I91lfvn3OdiHb15JjIQq+4jVUScdz4HvsfcYMQoXo/zePZlOMog H9/VlP7ZjByTxBMOAmwl8hCTGWIO/ouR+jpfOprM0OX+BQA2rYlC0Ie2iGB5pqiVxv6T ybS0ECP574y+8BmzRbPrQGhtzvhKwPfzI6eBRAyI19j5Inyizs9aVAlWix1Ad6NGshG5 jkaQox1tfeujrlhFkHHG1wkmd8fTEV5lhUz0kozdYpAqe8TQXEQMLcGThXWWrox6VzYN 7gG0MC6GSs0xLMznwyQY42SMoKxTxULGGmgTe9m4QEW9XC0CAUlnBXYFe+VUyGBRXzPA /mMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785398654; x=1786003454; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=CIA2ESPy6hD2IsIBmPf7dAgmgpqiiLji99VHkKcY/l0=; b=dKt/47xbsEPKD++XRVGMzL3T151TZ9/bPMcHKR4asOgC9VRt2x7d55IxtGLLRBoIjU Qe5GLKR2H4z2wrPWUqlv9J4MG7hcEUi2DJa+xqc98hhTe38k/9v0sG8goeV/gY8OkL1d 64qFB0f/bH7+2lCXqGR00YFAdg86pSZDIBBu22clXoZV1u8fr24CYeFHY0Wb6O3lKxvs gu+PDk+/vKZIZL1Mg8bhjLz5h+7dnQawGvGoqVpRHKiy5nEMHzSHCuYuplWXJR8nLRSq AO9YIBcO397M1NryHRUA/S/gWizCwBvxcHVqxwQ78Qq48ajC1WrC5nX9IijXX6NpyVjD s3Eg== X-Forwarded-Encrypted: i=1; AHgh+Rq7VquMNQzAXCAEryRMSq83BbtWqh5Fg7Wara10hQ4WabdBVvX1CNlo1fZs45yxAryzMd3AIQjsrpvVYlk=@vger.kernel.org X-Gm-Message-State: AOJu0YwE9kSuwrWcO5XEG+YSbqTdi/QaB1QQC2+NidUl+ry7ozs1mLXI 4nfYqP4bZsYcdO107qH1ySTHwHj0qwkiXGVd+SCO1LOKd1VPvk02EEM2 X-Gm-Gg: AR+sD135XgWbpUL1eSv68gc1Vu+t+n8AE1xtYzcnDzdK5//Ju+0mrZu83lXP9ytgmdC imA+QHdXWLWxzfHvDssB1JwyoJO1ga2rlKd3bb0AtSU3Lbt42ztubBSdYv0NI0W2s4nclXVFofc NM2rmPR2Y+Zqf2zk0GwNidqSmRVk9kXiaur5UenOkMPK2ehPsbygKUt+WnRSWhVKfN77GvF8hMK QO+A6QZxEXfuY0c4NvafAObQZVpzTJ3YWI/rIdyVe5l0Tgru1C2oEvm9taisml0pPDRs+40A5kH f+Xe3nDkp1uheL6K3QRn6SsSzTNxehvMlWJIFQQMUwQUxIL/RQ+zzTrIW8h5SqgWf/tMT4mDScZ PDTAd5S1k6R2H0kYjmo/DEIk/+i7W5nJ4ossY3drj/UN+SvWiB3BjBbFXXwUd4GyajP/45nCrT/ VbC/Cvvya/pV1SRhEDBbu4OekF3RWmpcDyys0SDSUW9ISSWdjsec+eq0/miwE5kHZp1cKf1Ck+d 6m/zK/00mav3sA/iDtlL3mZ+K0AaqpXgJDWZntrq8cGp3rPANEJCn/TCdzff/7NsN4Ybu/mZGDk 0VJtaw== X-Received: by 2002:a05:6000:2401:b0:47f:96bc:e370 with SMTP id ffacd0b85a97d-47fc832f362mr1869383f8f.4.1785398653062; Thu, 30 Jul 2026 01:04:13 -0700 (PDT) Received: from OrangePi5-Plus.BB-HOME (20014C4E1B8A5A00BF2D55A7591B4173.dsl.pool.telekom.hu. [2001:4c4e:1b8a:5a00:bf2d:55a7:591b:4173]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47fc88e4055sm3347685f8f.13.2026.07.30.01.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2026 01:04:12 -0700 (PDT) From: Igor Paunovic To: Tomeu Vizoso Cc: Oded Gabbay , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-rockchip@lists.infradead.org, Guangshuo Li , Jiaxing Hu , Igor Paunovic Subject: [PATCH 2/2] accel/rocket: keep core slots stable across unbind and rebind Date: Thu, 30 Jul 2026 10:03:54 +0200 Message-ID: <20260730080355.177422-3-royalnet026@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260730080355.177422-1-royalnet026@gmail.com> References: <20260730080355.177422-1-royalnet026@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The shared rocket_device tracks bound cores with a single counter and uses it for three different jobs at once: find_core_for_dev() searches [0, num_cores), rocket_probe() inserts the new core at index num_cores, and rocket_remove() only decrements the counter without clearing the slot. This bookkeeping falls apart as soon as cores are unbound in any order other than strict reverse bind order: - unbinding core 0 shrinks the search range, so the still-bound core at the highest index can no longer be found: its runtime PM callbacks start failing with -ENODEV and a later unbind of it is silently ignored, skipping rocket_core_fini() entirely; - a subsequent bind then reuses the index of that still-live core and overwrites its slot while its IRQ handler (dev_id points into cores[]) and its DRM scheduler are still active; - rocket_open() unconditionally uses cores[0].dev, which after an unbind of core 0 is a stale pointer to an unbound device. Give the array a fixed capacity (max_cores, the DT core count already used to size the allocation) and make .dev the slot-liveness marker: probe takes the first free slot, remove clears .dev after rocket_core_fini() and warns if the core cannot be found, lookups iterate the full capacity, and rocket_open() and rocket_job_open() use only live slots. num_cores keeps counting bound cores for the last-core teardown check. Fixes: ed98261b4168 ("accel/rocket: Add a new driver for Rockchip's NPU") Signed-off-by: Igor Paunovic --- Unbinding a core that still has jobs in flight has further pre-existing issues (scheduler and open-file lifetime) that are out of scope for this bookkeeping fix. Verified on RK3588 (Orange Pi 5 Plus): out-of-order unbind/rebind sequences (including the previously corrupting unbind of core 0 with cores 1 and 2 still bound, followed by rebind) now keep all three cores findable and functional, with a MobileNetV1 inference run via the Teflon TFLite delegate bit-identical to the stock driver afterwards. drivers/accel/rocket/rocket_device.c | 2 ++ drivers/accel/rocket/rocket_device.h | 3 +++ drivers/accel/rocket/rocket_drv.c | 32 ++++++++++++++++++++++++++++---- drivers/accel/rocket/rocket_job.c | 11 ++++++----- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/drivers/accel/rocket/rocket_device.c b/drivers/accel/rocket/rocket_device.c index 46e6ee1..8303f05 100644 --- a/drivers/accel/rocket/rocket_device.c +++ b/drivers/accel/rocket/rocket_device.c @@ -35,6 +35,8 @@ struct rocket_device *rocket_device_init(struct platform_device *pdev, if (!rdev->cores) return ERR_PTR(-ENOMEM); + rdev->max_cores = num_cores; + dma_set_max_seg_size(dev, UINT_MAX); err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); diff --git a/drivers/accel/rocket/rocket_device.h b/drivers/accel/rocket/rocket_device.h index ce662ab..7fb6a9d 100644 --- a/drivers/accel/rocket/rocket_device.h +++ b/drivers/accel/rocket/rocket_device.h @@ -18,6 +18,9 @@ struct rocket_device { struct mutex sched_lock; struct rocket_core *cores; + /* Slot capacity (DT core count); slots with a NULL .dev are free. */ + unsigned int max_cores; + /* Number of currently bound cores. */ unsigned int num_cores; }; diff --git a/drivers/accel/rocket/rocket_drv.c b/drivers/accel/rocket/rocket_drv.c index d29c5ee..04c1e47 100644 --- a/drivers/accel/rocket/rocket_drv.c +++ b/drivers/accel/rocket/rocket_drv.c @@ -69,11 +69,21 @@ rocket_iommu_domain_put(struct rocket_iommu_domain *domain) kref_put(&domain->kref, rocket_iommu_domain_destroy); } +static struct rocket_core *rocket_first_live_core(struct rocket_device *rdev) +{ + for (unsigned int core = 0; core < rdev->max_cores; core++) + if (rdev->cores[core].dev) + return &rdev->cores[core]; + + return NULL; +} + static int rocket_open(struct drm_device *dev, struct drm_file *file) { struct rocket_device *rdev = to_rocket_device(dev); struct rocket_file_priv *rocket_priv; + struct rocket_core *core; u64 start, end; int ret; @@ -86,8 +96,14 @@ rocket_open(struct drm_device *dev, struct drm_file *file) goto err_put_mod; } + core = rocket_first_live_core(rdev); + if (!core) { + ret = -ENODEV; + goto err_free; + } + rocket_priv->rdev = rdev; - rocket_priv->domain = rocket_iommu_domain_create(rdev->cores[0].dev); + rocket_priv->domain = rocket_iommu_domain_create(core->dev); if (IS_ERR(rocket_priv->domain)) { ret = PTR_ERR(rocket_priv->domain); goto err_free; @@ -179,10 +195,17 @@ static int rocket_probe(struct platform_device *pdev) devres_close_group(&drm_dev->dev, rdev_group); } - unsigned int core = rdev->num_cores; + unsigned int core; dev_set_drvdata(&pdev->dev, rdev); + /* Take the first free slot: 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 -ENXIO; + rdev->cores[core].rdev = rdev; rdev->cores[core].dev = &pdev->dev; rdev->cores[core].index = core; @@ -210,10 +233,11 @@ static void rocket_remove(struct platform_device *pdev) struct device *dev = &pdev->dev; int core = find_core_for_dev(dev); - if (core < 0) + if (WARN_ON(core < 0)) return; rocket_core_fini(&rdev->cores[core]); + rdev->cores[core].dev = NULL; rdev->num_cores--; if (rdev->num_cores == 0) { @@ -234,7 +258,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->num_cores; core++) { + for (unsigned int core = 0; core < rdev->max_cores; core++) { if (dev == rdev->cores[core].dev) return core; } diff --git a/drivers/accel/rocket/rocket_job.c b/drivers/accel/rocket/rocket_job.c index ac51bff..680f943 100644 --- a/drivers/accel/rocket/rocket_job.c +++ b/drivers/accel/rocket/rocket_job.c @@ -276,7 +276,7 @@ static struct rocket_core *sched_to_core(struct rocket_device *rdev, { unsigned int core; - for (core = 0; core < rdev->num_cores; core++) { + for (core = 0; core < rdev->max_cores; core++) { if (&rdev->cores[core].sched == sched) return &rdev->cores[core]; } @@ -498,16 +498,17 @@ int rocket_job_open(struct rocket_file_priv *rocket_priv) struct rocket_device *rdev = rocket_priv->rdev; struct drm_gpu_scheduler **scheds = kmalloc_objs(*scheds, rdev->num_cores); - unsigned int core; + unsigned int core, n = 0; int ret; - for (core = 0; core < rdev->num_cores; core++) - scheds[core] = &rdev->cores[core].sched; + for (core = 0; core < rdev->max_cores; core++) + if (rdev->cores[core].dev) + scheds[n++] = &rdev->cores[core].sched; ret = drm_sched_entity_init(&rocket_priv->sched_entity, DRM_SCHED_PRIORITY_NORMAL, scheds, - rdev->num_cores, NULL); + n, NULL); if (WARN_ON(ret)) return ret;