Received: by 2002:ab2:784b:0:b0:1fd:adc2:8405 with SMTP id m11csp485629lqp; Mon, 10 Jun 2024 09:48:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXrvqURGdlEXf8tulUYk/AFQ2GoQ6bQe+M6GIacCxLNJ6dPCz92L7Le5tv/n2PbkIO2bSMbwyEUcAnXuM5bAv+PVlgmlOVEAerUIit4pA== X-Google-Smtp-Source: AGHT+IGbJtf2SA/IlcVkOuYdE0yW+nTeOK9FPXvJ0P3O/7MW7lxqguNsAN/FqwxvauQ4sR1Menqq X-Received: by 2002:a17:906:3bc2:b0:a66:c338:65cc with SMTP id a640c23a62f3a-a6cd665c861mr604736566b.19.1718038109640; Mon, 10 Jun 2024 09:48:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718038109; cv=pass; d=google.com; s=arc-20160816; b=f3n82ADGTM6Zq6f3/llP4ktS/zygcG9LjsqOx8/bRaVWCwhhaoDWc4O/5O9su17gE8 wx/0XN1m/T/CtLq6moFLwFt9V6PtKRZreJzZloH7q+HKSW/aUwqUg8fpmvy+pi6akUxT /+XduD6nb+mvga4+dAcKnOWn//bxHYWOR7xdAnHGyi9HPgRHzVJ/9lBmbsCzhg+XfUHr 3dO//MhmQt2oFE1lSOLLtXxve2mTvKJ5wxmGqIpLAdOo1oC4Q/RFyG9/LHit85kpiT+l t/kLtGJ4jmrLVq0z7U3tp0GRyxl/wZM5cUiI6NKVeXPicP2bsBxla7PSjZ7xmC4khWBz dLFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:subject:cc:to:from:dkim-signature :dkim-signature:message-id; bh=/sm/3Zjyw3ZZ6H1Z9wjufOHjksvdXGdFCfNiQCsGsAs=; fh=joBs/8L+orz77ylyCy6wqNwwi35f7sYhLR+/TQjQjhI=; b=cLd/uH9BWK4V1cNIKA7SZWQ6gk44dciEVjQXmwiQ8orgdj3TJN7epQlSIPT2XRB8um MrFO2PaQEk189Je15NafKXlzhbAav560r8tmFVXCDjSu2nkNJRp6Om6aUhOVJH6pgyX3 JmY8rYwskeyz+is47zwP14hp/eGWhRR2o+N4CI1JUtvzaV/PC88WsZhtuTzRzjjXq42s 2/dCLu8OSShmbqThiWnMZ5P38waJSoo9g+6HACg+xqNavU2JcK/mGt0T3pMta86nXMyq Hm3/STWVKraW9fLt2Yv2030zdfeUsBpR87o04HvrL/AQvsuLxZC0a8FKz6xpSwOKpc9z sYPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TOwMPaZo; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208593-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208593-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6ef71db514si295936066b.869.2024.06.10.09.48.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 09:48:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-208593-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TOwMPaZo; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-208593-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-208593-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DA2AE1F228F5 for ; Mon, 10 Jun 2024 16:48:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4E976156885; Mon, 10 Jun 2024 16:42:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TOwMPaZo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="S92bN/nM" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 D31DD156642 for ; Mon, 10 Jun 2024 16:42:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037764; cv=none; b=atq/AztG9XWGWrRdc4n7cEebxevlxb2KIo8E9JDrhuNmNMSHk2trSf6Hl5t2ILrqTUA8NPACJT/Dy3KANaRMxwbI0MFC/Dxqj8fuH7m1nWozNnplRJznh0Yw4CAM9Bhk3jO1xpkquAXF2M/3715WtXUi2PP0IeG0CeBAXc3Yvoc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718037764; c=relaxed/simple; bh=KhNVeFgd6lRRQ/L8jZVrXHF5SioQPrOiKVccu67AM2o=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=ELfOqpgESq5nqY7+zHSM8FbleKp+r/D19NgBxGRVJ0PuuTspa0EsO5ejKbrKEnWftWZDLE8CywgEZC48JCp2AjT9a793Zl9OtbzJ3cR/BIKq21xvEre8HvfZ4MB/WeC7AS7b5+mvswvn37AYWr1yM22hWnjpArJglt9r1BRnLDA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TOwMPaZo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=S92bN/nM; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Message-ID: <20240610164027.240256030@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1718037760; 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: references:references; bh=/sm/3Zjyw3ZZ6H1Z9wjufOHjksvdXGdFCfNiQCsGsAs=; b=TOwMPaZo5gJpvAndfavHXFCa12+gHtuBVfzCRy9zIPtOHiC+lTv0fCSXXrF+v/62xP/9r9 amqtTjmZR+KGNg3/hPABHcLAjbwFDO6As5s+HibLUS4hUYYfCvoMMuPHCkxc9LYJBNS8/C qzTmZXLdgC9mdkHqyPJE2TWPjiBS2Is2csp7a1+ycAZf2LweTwHWqWPAMukIhttk6mYCQY SE3JkUZyxrp7xBcOOp28xMyvomx0gZT0BdG0nG1SOf+hhFPZuXjWihhDnGf/3XhAQ++SiD BegjwgDzWbX2LQ3RACsKrJlb8L3tZFCmGo01YMtv/+qTS25pfHrR+2aWKT/C3w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1718037760; 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: references:references; bh=/sm/3Zjyw3ZZ6H1Z9wjufOHjksvdXGdFCfNiQCsGsAs=; b=S92bN/nMNA7k/zwi9wfRRqWkzYm4oMv7uVY8SJFAEhCj6VxlB6gy3NWTdpKOOedaubzopc Rll/VOLo7MX3V6Aw== From: Thomas Gleixner To: LKML Cc: Anna-Maria Behnsen , Frederic Weisbecker , John Stultz , Peter Zijlstra , Ingo Molnar , Stephen Boyd , Eric Biederman , Oleg Nesterov Subject: [patch V3 28/51] posix-timers: Cure si_sys_private race References: <20240610163452.591699700@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Date: Mon, 10 Jun 2024 18:42:39 +0200 (CEST) The si_sys_private member of the siginfo which is embedded in the preallocated sigqueue is used by the posix timer code to decide whether a timer must be reprogrammed on signal delivery. The handling of this is racy as a long standing comment in that code documents. It is modified with the timer lock held, but without sighand lock being held. The actual signal delivery code checks for it under sighand lock without holding the timer lock. Hand the new value to send_sigqueue() as argument and store it with sighand lock held. Signed-off-by: Thomas Gleixner --- include/linux/sched/signal.h | 2 +- kernel/signal.c | 10 +++++++++- kernel/time/posix-timers.c | 15 +-------------- 3 files changed, 11 insertions(+), 16 deletions(-) --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -340,7 +340,7 @@ extern int send_sig(int, struct task_str extern int zap_other_threads(struct task_struct *p); extern struct sigqueue *sigqueue_alloc(void); extern void sigqueue_free(struct sigqueue *); -extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type); +extern int send_sigqueue(struct sigqueue *, struct pid *, enum pid_type, int si_private); extern int do_sigaction(int, struct k_sigaction *, struct k_sigaction *); static inline void clear_notify_signal(void) --- a/kernel/signal.c +++ b/kernel/signal.c @@ -1919,7 +1919,7 @@ void sigqueue_free(struct sigqueue *q) __sigqueue_free(q); } -int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) +int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type, int si_private) { int sig = q->info.si_signo; struct sigpending *pending; @@ -1954,6 +1954,14 @@ int send_sigqueue(struct sigqueue *q, st if (!likely(lock_task_sighand(t, &flags))) goto ret; + /* + * Update @q::info::si_sys_private for posix timer signals with + * sighand locked to prevent a race against dequeue_signal() which + * decides based on si_sys_private whether to invoke + * posixtimer_rearm() or not. + */ + q->info.si_sys_private = si_private; + ret = 1; /* the signal is ignored */ result = TRACE_SIGNAL_IGNORED; if (!prepare_signal(sig, t, false)) --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -299,21 +299,8 @@ int posix_timer_queue_signal(struct k_it if (timr->it_interval) si_private = ++timr->it_requeue_pending; - /* - * FIXME: if ->sigq is queued we can race with - * dequeue_signal()->posixtimer_rearm(). - * - * If dequeue_signal() sees the "right" value of - * si_sys_private it calls posixtimer_rearm(). - * We re-queue ->sigq and drop ->it_lock(). - * posixtimer_rearm() locks the timer - * and re-schedules it while ->sigq is pending. - * Not really bad, but not that we want. - */ - timr->sigq->info.si_sys_private = si_private; - type = !(timr->it_sigev_notify & SIGEV_THREAD_ID) ? PIDTYPE_TGID : PIDTYPE_PID; - ret = send_sigqueue(timr->sigq, timr->it_pid, type); + ret = send_sigqueue(timr->sigq, timr->it_pid, type, si_private); /* If we failed to send the signal the timer stops. */ return ret > 0; }