Re: [RFC PATCH v2 08/15] misc/arm-cla: Manage domain contexts
From: Linus Walleij
Date: Sun Nov 22 2026 - 16:29:13 EST
Hi Ryan,
thanks for your patch!
On Thu, Oct 15, 2026 at 4:27 PM Ryan Roberts <ryan.roberts@xxxxxxx> wrote:
> A CLA domain cannot be used by multiple address spaces concurrently
Which address space? The physical one? The virtual address
space of the CPU for a certain CLA? The virtual address space
of the CLA on a certain CPU, as found from it's TTBR0/1?
I suspect the last one, but it needs clarifying.
And so the domain is definied in hardware but there is
a footgun waiting to be fired by not defining the virtual
address space exactly the same on all CLA:s in the domain?
Please elaborate.
> because its devices share a memory translation context and may
> communicate without isolation.
Again I stumble here. "without isolation". So this kind of resoning
implies I know of some isolation that I cannot have. But I don't
know that, I have no idea what kind of isolation I would want here.
This needs to be explained differently.
> Represent each user of a domain with a
> context keyed by the mm_struct and device file description, and track
> contexts in a per-domain hash table.
This I understand.
> Add a domain scheduler that queues contexts for assignment
Are these contexts the software abstraction of hardware
constraints (such as the virtual memory space)
around a certain domain, and since they are scheduled,
these constraints are then applied over a certain time window?
> and gives
> each one a fixed time slice. Before switching, revoke the outgoing
> context's mappings from all VMAs for the device file. Then save its
> accelerator state and remove its MTC, before installing the incoming
> context's MTC and restoring the incoming accelerator state.
I think I start to understand a little...
> Run hardware operations through CPU-bound kthread workers, since each
> CLA can only be accessed from its local CPU. Coordinate the per-device
> workers with a domain worker so that every CLA in the domain is switched
> as a single assignment unit.
>
> Treat a failure on any device as a failure of the whole domain. Stop
> further assignments and wake all waiters when a domain becomes unusable.
>
> Co-developed-by: Jean-Philippe Brucker <jpb@xxxxxxxxxx>
> Signed-off-by: Jean-Philippe Brucker <jpb@xxxxxxxxxx>
> Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
OK this is some kind of scheduler let's look, but right before we
start you should probably define the principles of this scheduler?
FIFO? round-robin? Something else?
> extern struct xarray cla_domains;
Nice use of xarray
> +static inline void cla_ctx_get(struct cla_ctx *ctx)
> +{
> + kref_get(&ctx->refcnt);
> +}
> +
> +static inline void cla_ctx_put(struct cla_ctx *ctx)
> +{
> + kref_put(&ctx->refcnt, cla_ctx_free);
> +}
Rename these something like _addref/_dropref or just inline
the kref calls. These names upsets the kernel conventions
for _get()/_put() of resources, see below.
> +/**
> + * cla_ctx_create - Create a context or increase its user count
Something that creates or increase a context should be named
cla_ctx_get() to avoid ambiguousness.
> +/**
> + * cla_ctx_destroy - Drop a context user
No it drops the reference and drops the context (destroy it) if the
usecount == 0.
Name it cla_ctx_put() to follow kernel patterns.
> + /* Release initial reference from cla_ctx_create(). */
> + cla_ctx_put(ctx);
So this needs a different name.
> +++ b/drivers/misc/arm-cla/cla-sched.c
I wanna spend some time here since this is RFC and we are looking
more at design than code details.
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Arm CLA driver - domain scheduling
In this header explain the principles for the scheduling, as well as in the
commit message.
> +#include <linux/cpuhplock.h>
> +#include <linux/rhashtable.h>
> +
> +#include "arm-cla.h"
> +
> +static struct cla_ctx invalid_ctx;
> +
> +static const struct rhashtable_params ctxs_rht_params = {
> + .head_offset = offsetof(struct cla_ctx, node),
> + .key_offset = offsetof(struct cla_ctx, key),
> + .key_len = sizeof(((struct cla_ctx *)0)->key),
> + .automatic_shrinking = true,
> +};
Neat!
> +static int __cla_domain_switch_ctx(struct cla_domain *domain,
> + struct cla_ctx *prev_ctx,
> + struct cla_ctx *next_ctx)
Is this one of those "__ means __inner_function()"?
Name it for exactly what it does instead.
> +static int cla_domain_switch_ctx(struct cla_domain *domain,
> + struct cla_ctx *prev_ctx,
> + struct cla_ctx *next_ctx,
> + struct cla_ctx **assigned_ctx)
Looks OK.
> +/*
> + * Run on the cla's local CPU, either in its worker or a CPU hotplug callback.
> + * Work submitters hold cpus_read_lock() until completion, excluding hotplug
> + * callbacks and keeping the worker bound. Hotplug callbacks hold domain->lock
> + * to protect the assigned context. Other devices may switch concurrently.
> + * On failure, mark the device broken; the caller handles domain failure.
> + */
> +int cla_dev_switch_ctx(struct cla_dev *dev, struct cla_ctx *prev_ctx,
> + struct cla_ctx *next_ctx)
Good explanation and name, easy to understand.
> +void cla_dev_switch_ctx_work(struct kthread_work *work)
Here too
> +static inline struct cla_ctx *cla_domain_get_next_ctx(struct cla_domain *domain)
> +{
> + return list_first_entry_or_null(&domain->queued_ctxs, struct cla_ctx,
> + queue_node);
> +}
This looks like a FIFO thing?
> +static inline struct cla_ctx *cla_domain_get_prev_ctx(struct cla_domain *domain,
> + struct cla_ctx *next_ctx)
> +{
> + if (domain->assigned_ctx && (next_ctx || cla_ctx_is_dying(domain->assigned_ctx)))
> + return domain->assigned_ctx;
> + return NULL;
> +}
> +
> +static void cla_vma_zap_domain(struct vm_area_struct *vma, struct cla_domain *domain)
"zap" is a very ambiguous word. This function needs to have
a name that express exactly what it does.
It looks like it should rather be infixed _initialize_?
cla_domain_initialize_vma()?
cla_domain_reinitialize_vma()?
> +{
> + unsigned long vma_start = vma->vm_pgoff;
> + unsigned long vma_end = vma_start + vma_pages(vma);
> + unsigned long domain_start = domain->pg_offset;
> + unsigned long domain_end = domain_start + domain->nr_devs;
> + unsigned long start = max(vma_start, domain_start);
> + unsigned long end = min(vma_end, domain_end);
> + unsigned long addr;
> + unsigned long size;
> +
> + if (start >= end)
> + return;
> +
> + addr = vma->vm_start + ((start - vma_start) << PAGE_SHIFT);
> + size = (end - start) << PAGE_SHIFT;
> + zap_special_vma_range(vma, addr, size);
That goes for that function too by the way. Two wrongs does not
make one right.
It might be that everyone in memory management disagree with
me and think this is crystal clear, then I can have other opinions.
> +static void cla_domain_zap(struct cla_domain *domain, struct cla_ctx *ctx)
And this one.
cla_domain_init()?
cla_domain_reinit()?
> +static void cla_domain_reassign(struct kthread_work *work)
What about a comment above this function explaining
what is being reassigned to what?
> + /*
> + * Exclude cpuhp until assigned_ctx reflects the hardware again. cpuhp
> + * callbacks use it to save/restore the local device and must not see
> + * invalid_ctx. Take before domain->lock, also used by cpuhp callbacks.
> + */
> + cpus_read_lock();
> + mutex_lock(&domain->lock);
> + if (domain->broken)
> + goto out_unlock;
> +
> + /* Figure out prev_ctx and next_ctx. NULL indicates don't [un]assign. */
> + next_ctx = cla_domain_get_next_ctx(domain);
> + prev_ctx = cla_domain_get_prev_ctx(domain, next_ctx);
> +
> + if (!prev_ctx && !next_ctx)
> + goto out_unlock;
> +
> + /* prev_ctx already holds its assignment reference. */
> + if (next_ctx)
> + cla_ctx_get(next_ctx);
> +
> + /*
> + * We need to grab mmap_lock, but can't do so while holding the domain
> + * lock (since cla_vma_fault() grabs the domain lock while holding
> + * mmap_lock). The queue may change and contexts may be killed by
> + * cla_ctx_destroy() while the lock is dropped.
> + *
> + * No user is allowed to access CLA while we are reassigning. Disable
> + * assigned_ctx before zapping so cla_vma_fault() won't re-map the
> + * domain during reassignment. The sentinel also defers broken-domain
> + * cleanup until we know which context hardware may still access.
> + */
> + WRITE_ONCE(domain->assigned_ctx, &invalid_ctx);
> + mutex_unlock(&domain->lock);
> +
> + /* Prevent prev_ctx from accessing any device in the domain. */
> + cla_domain_zap(domain, prev_ctx);
> +
> + /* cpus_read_lock() ensures workers remain cpu-bound and alive. */
> + ret = cla_domain_switch_ctx(domain, prev_ctx, next_ctx, &assigned_ctx);
> +
> + mutex_lock(&domain->lock);
> + WRITE_ONCE(domain->assigned_ctx, assigned_ctx);
> +
> + /* Also handle failure reported by another thread during the switch. */
> + if (ret || domain->broken) {
> + cla_domain_set_broken(domain);
> + goto out_unlock;
> + }
> +
> + /*
> + * Remove next_ctx from the queue and wake up all waiters so that the
> + * fault handler can map the domain into the newly assigned process.
> + * assigned_ctx must have been updated prior to waking the waiters.
> + *
> + * If next_ctx was killed while we were assigning it, schedule another
> + * reassignment to deassign it.
> + */
> + if (next_ctx) {
> + if (cla_ctx_is_dying(next_ctx)) {
> + cla_domain_schedule_reassignment(domain);
> + goto out_unlock;
> + }
> + list_del_init(&next_ctx->queue_node);
> + wake_up_all(&next_ctx->waitq);
> + }
> +
> + /* If there are more queued ctxs, schedule the next reassignment. */
> + if (!list_empty(&domain->queued_ctxs))
> + cla_domain_schedule_reassignment(domain);
> +
> +out_unlock:
> + mutex_unlock(&domain->lock);
> + cpus_read_unlock();
> +
> + /* Deliberately put references outside of the locks. */
> + if (next_ctx && next_ctx != assigned_ctx)
> + cla_ctx_put(next_ctx);
> + if (prev_ctx && prev_ctx != assigned_ctx)
> + cla_ctx_put(prev_ctx);
> +}
This function contains a bunch of comments that are hard for me
to understand without context (ha!) but it's probably all right,
I bet they are helpful once you get the hang of it.
> +int cla_domain_sched_init(struct cla_domain *domain)
OK.
> +/**
> + * cla_domain_sched_exit - Tear down scheduling for a CLA domain
> + * @domain: CLA domain
> + *
> + * Drain final deassignment, destroy the domain worker, and destroy the context
> + * table. All context users must have exited before calling this.
> + */
> +void cla_domain_sched_exit(struct cla_domain *domain)
OK.
> +void cla_domain_set_broken(struct cla_domain *domain)
OK.
> +struct cla_ctx *cla_domain_lookup_ctx(struct cla_domain *domain,
> + struct cla_ctx_key *key)
(...)
> +int cla_domain_insert_ctx(struct cla_domain *domain, struct cla_ctx *ctx)
(...)
> +void cla_domain_remove_ctx(struct cla_domain *domain, struct cla_ctx *ctx)
Clever use of rhashtable, good names.
> +/**
> + * cla_domain_schedule_reassignment - Schedule domain reassignment
> + * @domain: CLA domain
> + *
> + * The caller must hold the domain lock. If a live context is currently
> + * assigned, schedule with a delay (CLA_SLICE_MS), otherwise reassign
> + * immediately. Do nothing if the domain is broken or a switch is in progress;
> + * the running switch handles new requests before returning. If reassignment is
> + * already scheduled, the delay is modified.
> + */
> +void cla_domain_schedule_reassignment(struct cla_domain *domain)
Schedule reassign of the domain to ... what?
This use of verbs is really confusing me, I understand for example
"assign timeslice to process" then I know a timeslice is assigned to
a process. There is something implicit here making this really
hard to understand.
> +void cla_domains_sched_exit(void)
OK.
But where is the cla_schedule() function in this scheduler
akin to Linux' schedule() call? It feels like this is implicit
in some call order semantic, and the function
cla_domain_get_next_ctx() implements an implicit
FIFO scheduling principle.
Alternatively the scheduling isn't actually driven here
but from somehwere else, like userspace. Then that
needs to be explained. Then this file doesn't actually
implement any scheduling, it's a scheduling context
library nothing more.
This kind of stuff needs to be explained so I can review the
patch properly.
Yours,
Linus Walleij