From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 484C03CB2E5 for ; Thu, 16 Jul 2026 13:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784209825; cv=none; b=Ty+8q94zTlvBKtVHXfXoPIugyKh/nnAkaaI8RNh9VXjl+OjPOLgj6Q7ASFvBXa5joQvlHtw8sP5nbQNE4q2j05dzHIHeKcyDqScpHi3dX45KB2bkzpL/oqE3t2l/Y3LIpv7JFQ6XL+IXVzZ4LvUGinHPcm4DWE8M7oRcKIE8v3A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784209825; c=relaxed/simple; bh=4ItGtr4M/7pLQsCgRhJiP3VW20cNzsCj+k1+4Otk41E=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=Joy4eHiDQ/wm27sbJ1FSR/XRZ6bw7RTQheIudseKOEAC1W/eh3IjgFZ0Ocohab0PID8uYu4L975lwkLqnCyt0I2Jb+Bm16YunqX0z9dZTX/7e/y9KuPCb53hfMKVIKhV2WOLgdloyzEvHbxB8OQ7dbtVeJa+CWk3UFm1UHuViNk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LkPMvYwu; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=HohJgHjs; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LkPMvYwu"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="HohJgHjs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1784209823; 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:autocrypt:autocrypt; bh=67Gm5PocQdlRFd5TX7tAjWlVgcuxtlTA0Eh397YmwuI=; b=LkPMvYwu+6Z6z/tPXqDom390ypPyJa3MFfPMaeTTONbx04zkmd4rJd3qLbJI+Ddo8kmaSs 0H2rTF9sILpM1oyr9f9FUTncwi1Ez1KOBJnpg1TXAVO2XgB+OAACEfK8+EiTjy1EMmu+Gh YVGAYeqhxgDDIx6OTxJP5AdM33wFACg= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-156-4nxg1D0BNhm93AvHsg0jaQ-1; Thu, 16 Jul 2026 09:50:21 -0400 X-MC-Unique: 4nxg1D0BNhm93AvHsg0jaQ-1 X-Mimecast-MFC-AGG-ID: 4nxg1D0BNhm93AvHsg0jaQ_1784209820 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-69cd6606b19so3429931a12.1 for ; Thu, 16 Jul 2026 06:50:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1784209820; x=1784814620; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :from:to:cc:subject:date:message-id:reply-to:content-type; bh=67Gm5PocQdlRFd5TX7tAjWlVgcuxtlTA0Eh397YmwuI=; b=HohJgHjs2ET/aApb1FMcsrJi6tRQ74gt39KrrPxR69hghn2HhEOstW8pL7TB1it3ZB F9KMjuDS6pda1me75DyQS01f9+hZJCjX1jC15XNydJ0bDo/XrZBAWbCiFFKjVoBT5QUI qAWKXC2E7hbHxcGIySZwC/2yK0biTQS3v5Ezt5JRZK9wjYzLNNvx9y7GRhXvz0DCcp2n 2dKnTb4l2W792ZvVMzxMgeieFP7PvI+r28/i0/fClalFqJsFOzKNC70c3Vj3a+pxCQON XQBqTbVYYwIPjgBiIWIB2nDyDTemHvju149loOodHuZDmNbLZoNrjH0NyUetj2MEDkJ3 J0KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784209820; x=1784814620; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=67Gm5PocQdlRFd5TX7tAjWlVgcuxtlTA0Eh397YmwuI=; b=ObJshPLfeewtM0N8Uy5i+gVeNYsuILeK4xfkQRMJocOlgSqC8xFEZTc38LzrCghKlV K8vNmIaD+SbRxlo5r4jSsHr6fK3zcR07t2J4qU26nXH/kc56YtktFU1JCWAdYwh+apkJ BwP+Sj8tEpZc/IZMR+fQMKg2wEORskuyRKnjZSChwOrB3nl5AQt1NQ9qb1fR5Cm4uoB4 phvqeUD2umRJikkGfZBujUjngmfql10RNEFKHwtT4vb2YIvqV399XdRYQ8/LjDJ3Yjg7 tpJ+4MnZoISriI29wAyZ8yKUAUrn+aaEMq7unl6UGiSzd+ajfy/jUt4vhZ9CIVKCSON2 JK4Q== X-Gm-Message-State: AOJu0YwiYvxKYm8Ppjr92O13SA+LcTOIR2aQIs9hUiC7vNqgEqjJX99D mVhmX8DBw3EJra7J4vP1iOThOIKUgyRJSsEemvvsNv1VX37swqp0fnhtFN1NkG1EEtXetlaFKao dSMtkzqNUP4MiJUDCWrWt6oD2x6Fit8bWC+I/9fWGHe+mWBbiV92EGjUtdgMbt1jybw== X-Gm-Gg: AfdE7ckr3gCIQ4KGefSZJToan0OpIGf990PbfVrEW1TenctWUNzFzmCK0cCiOY2kgYA B61U7Hur2F5i45upI+YnqbehmSAlcY3Qa7EnBa1XAF8ZCCwr6x36DJoJso4PTFmRkfL8mVSXRi9 uEMVbWhkoACloGkeZdlPUaIVwr2CbTmig8VClZ78xOcTnH2VbqICEUc5USTPf4VPvzmRHHiWTxn KyD/epfbpY7dED0t4wMJou3N6w0oW/fYZwvN0RMfDdH2cmZTnJSX/pLKpkmo+bM3KnS/fBl7hd9 ZAFsfXZEAR/oFNjW9ARhmg8Hm/6xkRg4GOwiyv981iXMWCwmhHs2px6ZKMgLqCofeQ+C1CboHJ4 6Ht/mmIUemV5+Kb/MGsUyEx1Xk4yp0+V1hMjzQb4h7Jd3BErgRh+H3m6yz7+pQEUPq1trZw== X-Received: by 2002:a05:6402:42cb:b0:69c:7406:9492 with SMTP id 4fb4d7f45d1cf-69cd6ddb660mr6244194a12.39.1784209819994; Thu, 16 Jul 2026 06:50:19 -0700 (PDT) X-Received: by 2002:a05:6402:42cb:b0:69c:7406:9492 with SMTP id 4fb4d7f45d1cf-69cd6ddb660mr6244175a12.39.1784209819600; Thu, 16 Jul 2026 06:50:19 -0700 (PDT) Received: from gmonaco-thinkpadt14gen3.rmtit.csb (212-8-243-115.hosted-by-worldstream.net. [212.8.243.115]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-69cd293bb8bsm4731784a12.22.2026.07.16.06.50.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2026 06:50:19 -0700 (PDT) Message-ID: <2d5915906df646c206b425ef326fd54fd7250a8f.camel@redhat.com> Subject: Re: [PATCH 1/2] rv/nomiss: fix task storage lifetime From: Gabriele Monaco To: Li Qiang , linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, namcao@linutronix.de, juri.lelli@redhat.com, wen.yang@linux.dev Date: Thu, 16 Jul 2026 15:50:17 +0200 In-Reply-To: <20260715060023.1502983-1-liqiang01@kylinos.cn> References: <20260715060023.1502983-1-liqiang01@kylinos.cn> Autocrypt: addr=gmonaco@redhat.com; prefer-encrypt=mutual; keydata=mDMEZuK5YxYJKwYBBAHaRw8BAQdAmJ3dM9Sz6/Hodu33Qrf8QH2bNeNbOikqYtxWFLVm0 1a0JEdhYnJpZWxlIE1vbmFjbyA8Z21vbmFjb0BrZXJuZWwub3JnPoiZBBMWCgBBFiEEysoR+AuB3R Zwp6j270psSVh4TfIFAmjKX2MCGwMFCQWjmoAFCwkIBwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgk Q70psSVh4TfIQuAD+JulczTN6l7oJjyroySU55Fbjdvo52xiYYlMjPG7dCTsBAMFI7dSL5zg98I+8 cXY1J7kyNsY6/dcipqBM4RMaxXsOtCRHYWJyaWVsZSBNb25hY28gPGdtb25hY29AcmVkaGF0LmNvb T6InAQTFgoARAIbAwUJBaOagAULCQgHAgIiAgYVCgkICwIEFgIDAQIeBwIXgBYhBMrKEfgLgd0WcK eo9u9KbElYeE3yBQJoymCyAhkBAAoJEO9KbElYeE3yjX4BAJ/ETNnlHn8OjZPT77xGmal9kbT1bC1 7DfrYVISWV2Y1AP9HdAMhWNAvtCtN2S1beYjNybuK6IzWYcFfeOV+OBWRDQ== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.60.2 (3.60.2-1.fc44) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Wed, 2026-07-15 at 14:00 +0800, Li Qiang wrote: > The nomiss monitor stores a pointer to a task's embedded deadline entity > in per-PID object storage. The exit handler only removed that storage whe= n > the task was still SCHED_DEADLINE. A task that changed to another policy > before exiting left its entry behind. Once the PID was reused, the new ta= sk > could reuse the old entry and dereference the freed deadline entity. Yes, this is actually an issue, but wouldn't it be solved by just deleting = the storage as soon as a task changes its policy out of DEADLINE ? We are currently only resetting the monitor (so timers won't fire), but del= eting would probably be more appropriate. Then there's the issue we are tracking the syscall entry, so we don't know = if that is going to succeed, a more robust solution would be to add a tracepoi= nt inside the actual __sched_setscheduler() (but maintainers may not want that= ) or tracking the context (old policy) inside sys_enter and commit the result in sys_exit only if it succeeds. > Remove storage unconditionally at sched_process_exit. Serialize creation > and destruction with locks sharded by PID, and do not create storage once > PF_EXITING is set. Each shard tracks both stored entries and in-flight > creators, allowing exits for shards without storage to avoid locking and > hash lookups while preserving the create/exit ordering. I don't get why we need this complex machinery. What races are you trying t= o solve with this locks? Concurrent syscalls and newtask/exit can this even happen? Thanks, Gabriele >=20 > Fixes: b133207deb72 ("rv: Add nomiss deadline monitor") > Signed-off-by: Li Qiang > --- > =C2=A0kernel/trace/rv/monitors/deadline/deadline.h | 86 +++++++++++++++++= ++- > =C2=A0kernel/trace/rv/monitors/nomiss/nomiss.c=C2=A0=C2=A0=C2=A0=C2=A0 |= =C2=A0 2 +- > =C2=A02 files changed, 84 insertions(+), 4 deletions(-) >=20 > diff --git a/kernel/trace/rv/monitors/deadline/deadline.h > b/kernel/trace/rv/monitors/deadline/deadline.h > index 78fca873d61e..b92480e79807 100644 > --- a/kernel/trace/rv/monitors/deadline/deadline.h > +++ b/kernel/trace/rv/monitors/deadline/deadline.h > @@ -1,6 +1,8 @@ > =C2=A0/* SPDX-License-Identifier: GPL-2.0 */ > =C2=A0 > =C2=A0#include > +#include > +#include > =C2=A0#include > =C2=A0#include > =C2=A0#include > @@ -148,6 +150,37 @@ static inline struct sched_dl_entity *get_server(str= uct > task_struct *tsk, u8 typ > =C2=A0 return NULL; > =C2=A0} > =C2=A0 > +#define DEADLINE_STORAGE_LOCK_BITS 6 > +#define DEADLINE_STORAGE_LOCKS BIT(DEADLINE_STORAGE_LOCK_BITS) > + > +/* > + * A shard tracks both entries and creators which have not yet checked > + * PF_EXITING. This lets unrelated exits avoid taking a storage lock whi= le > + * preserving the create/exit ordering. > + */ > +struct deadline_storage_shard { > + raw_spinlock_t lock; > + atomic_t users; > +} ____cacheline_aligned_in_smp; > + > +static struct deadline_storage_shard > + deadline_storage_shards[DEADLINE_STORAGE_LOCKS]; > + > +static inline struct deadline_storage_shard *deadline_storage_shard(int = pid) > +{ > + return &deadline_storage_shards[hash_32(pid, > DEADLINE_STORAGE_LOCK_BITS)]; > +} > + > +static void deadline_storage_init(void) > +{ > + int i; > + > + for (i =3D 0; i < DEADLINE_STORAGE_LOCKS; i++) { > + raw_spin_lock_init(&deadline_storage_shards[i].lock); > + atomic_set(&deadline_storage_shards[i].users, 0); > + } > +} > + > =C2=A0/* > =C2=A0 * Initialise monitors for all tasks and pre-allocate the storage f= or > servers. > =C2=A0 * This is necessary since we don't have access to the servers here= and > @@ -159,6 +192,8 @@ static inline int init_storage(bool skip_tasks) > =C2=A0 struct task_struct *g, *p; > =C2=A0 int cpu; > =C2=A0 > + deadline_storage_init(); > + > =C2=A0 for_each_possible_cpu(cpu) { > =C2=A0 if (!da_create_empty_storage(fair_server_id(cpu))) > =C2=A0 goto fail; > @@ -177,6 +212,7 @@ static inline int init_storage(bool skip_tasks) > =C2=A0 read_unlock(&tasklist_lock); > =C2=A0 goto fail; > =C2=A0 } > + atomic_inc(&deadline_storage_shard(p->pid)->users); > =C2=A0 } > =C2=A0 } > =C2=A0 read_unlock(&tasklist_lock); > @@ -187,17 +223,61 @@ static inline int init_storage(bool skip_tasks) > =C2=A0 return -ENOMEM; > =C2=A0} > =C2=A0 > +static void deadline_create_task_storage(struct task_struct *task) > +{ > + struct deadline_storage_shard *shard =3D deadline_storage_shard(task- > >pid); > + bool created =3D false; > + > + /* > + * The temporary reference makes an in-flight creator visible to > exit. > + * If storage is created, it becomes the reference owned by that > entry. > + */ > + atomic_inc(&shard->users); > + /* Pair with exit before it tests users without taking the shard > lock. */ > + smp_mb__after_atomic(); > + > + raw_spin_lock(&shard->lock); > + if (!(READ_ONCE(task->flags) & PF_EXITING)) { > + guard(rcu)(); > + if (!da_get_monitor(EXPAND_ID_TASK(task)) && > + =C2=A0=C2=A0=C2=A0 da_create_storage(EXPAND_ID_TASK(task), NULL)) > + created =3D true; > + } > + raw_spin_unlock(&shard->lock); > + > + if (!created) > + atomic_dec(&shard->users); > +} > + > +static void deadline_destroy_task_storage(struct task_struct *task) > +{ > + struct deadline_storage_shard *shard =3D deadline_storage_shard(task- > >pid); > + > + /* Pair with the creator barrier before taking the no-storage fast > path. */ > + smp_mb(); > + if (!atomic_read(&shard->users)) > + return; > + > + raw_spin_lock(&shard->lock); > + guard(rcu)(); > + if (da_get_monitor(task->pid, NULL)) { > + /* A task may leave SCHED_DEADLINE before exiting. */ > + da_destroy_storage(task->pid); > + atomic_dec(&shard->users); > + } > + raw_spin_unlock(&shard->lock); > +} > + > =C2=A0static void __maybe_unused handle_newtask(void *data, struct task_s= truct > *task, u64 flags) > =C2=A0{ > =C2=A0 /* Might be superfluous as tasks are not started with this policy.= . > */ > =C2=A0 if (task->policy =3D=3D SCHED_DEADLINE) > - da_create_storage(EXPAND_ID_TASK(task), NULL); > + deadline_create_task_storage(task); > =C2=A0} > =C2=A0 > =C2=A0static void __maybe_unused handle_exit(void *data, struct task_stru= ct *p, > bool group_dead) > =C2=A0{ > - if (p->policy =3D=3D SCHED_DEADLINE) > - da_destroy_storage(get_entity_id(&p->dl, DL_TASK, DL_TASK)); > + deadline_destroy_task_storage(p); > =C2=A0} > =C2=A0 > =C2=A0#endif > diff --git a/kernel/trace/rv/monitors/nomiss/nomiss.c > b/kernel/trace/rv/monitors/nomiss/nomiss.c > index 8ead8783c29f..2e5ee681505b 100644 > --- a/kernel/trace/rv/monitors/nomiss/nomiss.c > +++ b/kernel/trace/rv/monitors/nomiss/nomiss.c > @@ -214,7 +214,7 @@ static void handle_sys_enter(void *data, struct pt_re= gs > *regs, long id) > =C2=A0 if (p->policy =3D=3D SCHED_DEADLINE) > =C2=A0 da_reset(EXPAND_ID_TASK(p)); > =C2=A0 else if (new_policy =3D=3D SCHED_DEADLINE) > - da_create_or_get(EXPAND_ID_TASK(p)); > + deadline_create_task_storage(p); > =C2=A0} > =C2=A0 > =C2=A0static void handle_sched_wakeup(void *data, struct task_struct *tsk= )