From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512224; b=U7bl3bGDnqzbpV8f4u+zkRh975+kPkmSI/hCtlvFL1yI4cFEODwHQcw2xFe70y9Z3Bs6 ibCkbOStK7nkW3CsUIbmiE+ebMTXuKkVqXxMutEbdL4KR/gym7apb21FQKpz+Fj0mhzeZ XVN+ZL21z7qKcpDeuNTm3Le8ayUpXKKLIYnQa1d6UE9oMYpAhrc30MqwP/mukD0Glc9In tqcysiA/xKtC4Z15y+356/vOCdfa5IMRfb7zOsqmsKU1sZw7clSc6Q92Z5aK3AgYT9N4N OrIqcSACKVSm2ai52IGdBgWm1ctCoIOybu0V/YE+TjpqePHovBXRvKqiNvxtbZc/0lQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512224; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=uwhVit64OzG+Xvb13ZnxgWU5H8O6stwKRliUKBOVTXI3SucW1tW8dmyKE1BQ88lbMg7O /gXyhV0u95gW8reWht0Yla59ZWl0knDHmRLrByNGOLdsmlfyft6BH9r6FiE0DeN5ZMs8W 6l0AegduI53qxhZz6chTf8BxAkFd2ly7l2au2rUtMH9bGDyZilzkYxFVfFuBlFs0Vbeu6 fht0MTaeVvHC/z/N47TdC/SA79Pj6XHthZvSy8IALquBosq+NapCVggDlGEHVt/Mxw23+ 1CGH7g70li1vBfdzbaEswgWz3pnTM01JXptEiWsfM8uYvh3gMobK6GLE/1xrBftcb7Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=Bnhiy1ig; arc=none smtp.remote-ip=209.85.210.52 Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED957606A32F for ; Sat, 5 Dec 2026 23:10:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ED957606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=Bnhiy1ig Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-86288992de5so1769784a34.1 for ; Sat, 05 Dec 2026 15:10:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512223; x=1797117023; 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=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=Bnhiy1igrzjgKqIYGTSkitV/Ksy3bZrnBIiHUUYNqOu2iEk59TQij0mynzT5L46UFc vu9OBpmL/Qyvwg7Hzv1+AGJxkHcsF4khmgh80Ozl+wk/cKjaTlb1kM2nvLDouFt+IFqI /AmIggvsq54gH6t6BjD3m4fZnSmUQlzNW97PDb4KnaMDzNIEl4xmlYNtOrRZUIMCzyJx 9I4EHN5m6st+zuK25fBM2VWH3HoAuSGeSoeSqHWBJjeQu5BZRMqetmqQh20ubKDo7Q17 kOi+WhpBh/iKEdN3nNXI0ZWnqmrP+qzaEO4orZTm4TseUNILST54LjVddHtqHkP+pk/p mgQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512223; x=1797117023; 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=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=FEiO8/z0txS3zkfSXGS75HvdHz2AqQ51HEjpWCctiTqUDWWo+rOJyVYBKjd2m/nrxS 55AqGui3SRb3oI701TKDHco97WljFcc1BufmHfx4Z5aDb6nEuYMwIaP06gemv1WtqY2c nEDB5rlbksO6SeptRm81XoTnNVXxS6EMU9mCeB0x8tyJzre+MgoOuFKUOoSe95sBLfMi MSTr6xEtO6RoqEvbx2NjwdqOjk1loV7qrs2X+GQAyrw09pSZzCdPV5QE/HkdjC78YJe7 T2uqWXgKWwUjszatz+0RcxhOiRsOtpxiqkHqR/c10AgbDBBZFMbrQYcYhmuV+i82Ehpm w++w== X-Gm-Message-State: APuU4Z8ZM0z9I3+/4LE9grjzrevbRP6euD3NV6S2akGZxZfFMFE+pxtR 6olCFn3Lf5KQIwy+zw5SvBbpICAYZDetEYjaawy0mg6h48b/I4f7bGvZyVWY2x8fT5MzIgcxE63 P+lChN4Y= X-Gm-Gg: AZFdTZcRICrysqlOkeNwdFa2h7zmAQYExgdyBxLcs3kk9WTGxQfaOKNPyRHnIrZWGtH oClLguvf992+EobvJA/UHWDZ4+FJTm9y1JBJ3IoxrAzoR6a/1zu6plA59yGYmEVys6nPa1ygU4T x0ZHu91Ci9ACy8NY/RYlkGtB8moBimK97OwcyNMLNr5qMYnqnMqA6hr+TWvsLLHADsbMSUk75nb SfMxQB/tPjjgd0ZLU5XrN5NwNgwq4QPplatOOvaQZ8RRrglHzardcj1OWuMtDhHxIrgVduOyygm Z+1nGof+Psyl1SCRaSQIe/cFAFMUx91cXJzIUbgJXeR/smTJbM+4/gomOnOdbFGmjdcAxRNG15X 01u9BBrZ+Jp8r+FZZ8HxNennaC8z3JeDhFOMxH2BgTCommeiVxrEmRXXJE3gzdOMusokFxBIeb+ IIxaa443J0JuB9xxOECVnWy3UPXnFZcCPkxDh+wDSbVZiiL629xPILVFPvEPe/Or4+M3v1O9lB/ uNuzgR1kEC0rCIlZ3UMJaXlkgHPGRDt/QlNUAPFXRkn5LS5OUwMNPPPBQi0zxBVnBUfMkh3LYnP pahdgLCNDBoO1OopqPJwSaoioPs2hhGkYy2Jy+DOwe1Gi+0Y7OMt8jxpLo407s3vL/wE3t+iVLh O8so9+fbU6w9xfxn4mYEj44BTgs3ryGjH8+TJ01ti3Vt0yDkYjMVVtrYTCQ== X-Received: by 2002:a05:6830:2b24:b0:85f:6f:6c39 with SMTP id 46e09a7af769-863f89c5df7mr4780558a34.13.1796512222850; Sat, 05 Dec 2026 15:10:22 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8313082sm6309624a34.16.2026.12.05.15.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:10:22 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.1.y 4/9] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:10:07 +0000 Message-ID: <20261205231012.3977993-5-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231012.3977993-1-axboe@kernel.dk> References: <20261205231012.3977993-1-axboe@kernel.dk> 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-Transfer-Encoding: 8bit From: Chris Mason Commit 0e433c705aff3846356f402e8b054d6e4ebbdbe0 upstream. On !PREEMPT_RT, a task that removes its tctx node on exit or exec can race with the exit work of a closed ring and cause a use-after-free of the io_ring_ctx. A mutex has to stay allocated until mutex_unlock() has returned. But io_uring_del_tctx_node() holds no reference on the ring, only its node on ->tctx_list keeps it, and io_ring_exit_work() frees the ring once it has tctx_lock and finds the list empty. If the exit work has queued as a waiter, it can get there while the unlock still needs the mutex: io_uring_del_tctx_node() io_ring_exit_work() ======================== =================== mutex_lock(tctx_lock) list_del() mutex_lock(tctx_lock) lock wait_lock set MUTEX_FLAG_WAITERS mutex_unlock(tctx_lock) clear the owner __mutex_trylock() succeeds unlock wait_lock list_empty() is true mutex_unlock(tctx_lock) io_ring_ctx_free() kfree(ctx) lock wait_lock waiter = lock->first_waiter Each node used to hold a file reference that was dropped after the unlock. Commit eebd2e37e662 ("io_uring: don't take task ring-file notes") removed it, and relies on the exit work to wait for the nodes. The fix used here is to do the unlock inside an RCU read section, and to free the io_ring_ctx only after a grace period. Fixes: eebd2e37e662 ("io_uring: don't take task ring-file notes") Signed-off-by: Chris Mason Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 2 ++ io_uring/io_uring.c | 2 +- io_uring/tctx.c | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 93d9e8130b5a..8bee77acb384 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -351,6 +351,8 @@ struct io_ring_ctx { struct llist_head fallback_llist; struct delayed_work fallback_work; struct work_struct exit_work; + /* the ctx is freed after a grace period, see io_uring_del_tctx_node() */ + struct rcu_head rcu; struct completion ref_comp; /* io-wq management, e.g. thread count */ diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 75e92f581f61..dc2cac09cbe6 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2805,7 +2805,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) kfree(ctx->io_bl); xa_destroy(&ctx->io_bl_xa); xa_destroy(&ctx->personalities); - kfree(ctx); + kfree_rcu(ctx, rcu); } static __poll_t io_uring_poll(struct file *file, poll_table *wait) diff --git a/io_uring/tctx.c b/io_uring/tctx.c index daba0a2c4ec2..85d670a52c51 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -180,7 +180,10 @@ __cold void io_uring_del_tctx_node(unsigned long index) mutex_lock(&node->ctx->tctx_lock); list_del(&node->ctx_node); + /* keeps the ctx until the unlock is done, see io_ring_ctx_free() */ + rcu_read_lock(); mutex_unlock(&node->ctx->tctx_lock); + rcu_read_unlock(); if (tctx->last == node->ctx) tctx->last = NULL; -- 2.55.0