From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796057275; b=F2rxbIAeJMMBKvyxDlW7fjPlYZ01C+5e14xgoloJxOiwWDXxX/rFsjhnqM7kIePl1BkZ SrD88/gXAFecgV6zpnbOThFjqQ7eHBRHh1NOm/2Cii11ovUjDQOyjLNdW8VGovBYchTWF elmnRwF4xFaCn4HHnRjMQwVtAkZKMGPZGH36HLXspUgW78mbG5HabSgmkeOnBtveo90Tn 9j+9JioQk1dsLVk/fVLv0zBAXYMBouLWz/4v2LmFqnYXwMu2vS2XIAEyXgxr9VfPxUUDm fUmn404A9XNB5cJVN+iUTP2UybOLIFqAC7snNKWElTN8T1EwaNte3TFFvQtlIbtFoNQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796057275; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Content-Language:From:Subject: To:Cc:Content-Type:Content-Transfer-Encoding; bh=4OqtmamPktkQ6npWbkeGCBBvOiauJ1w71WJmjLckShc=; b=p9AX5M1Rr/WaadQdrcDXBe3LI9kpHK2C/fbxN712/93neSP+jTDZvUMYXy6xcaHrmLAW n3z5UA9WF5/Ko6vPmAoM1qCRceluGaFHCSfBTz3P1U8h8e+ChQoC6mniijTeCB0807c6C NNHw3fRIMU1+g+hojelSr+4sc8Z+AOjrjkkswdrtnQjneEEA+t9M9rAFKH7ukYh7sUaLh oWjbgwGBY9+WyftnngXe7Ce1zHSWgtEjJUsweT6wZNPfnM+LwFRIaCx6WQ3OCyWpR3TUe u+48RjUN7doGLLNHB9vvFCI1gBff7NrsI0jT621zORTazfZF86+Geyvy6QqY9+zbGQg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ai+vcSGy; arc=none smtp.remote-ip=209.85.208.174 Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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 53F8D606A31C for ; Mon, 30 Nov 2026 16:47:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 53F8D606A31C 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.a=rsa-sha256 header.s=20260707 header.b=ai+vcSGy Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-3ae85add512so92210261fa.0 for ; Mon, 30 Nov 2026 08:47:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796057273; x=1796662073; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:subject:from :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=4OqtmamPktkQ6npWbkeGCBBvOiauJ1w71WJmjLckShc=; b=ai+vcSGy395/i4OfX5rvC6cvvOuSfQA1i4FmNuSIwC2Z7bbBnFay8TI+Lrtbofd5Xb f6xO6ygwdfULuczX6C2b+yH2Iv/lb6P35VxEjauflCC1kU1kXjSG15s0bgf6/RkYiKc+ Z9usyIOu6EBizUxUvzgM6g0u9ZU8i2NCOPwndtEr19eKZF2vvMF2EBzE64DS8PAtaza8 /aD2jQBWQtVKHLXaY31/QA0ZiwmSIymEZgAwfvZ9Z6/znm0mwEZqgtqjlw8+z3Qn1tXO AhCOB52iyaBxcHmQlRa9YDANXK4yF6Y3KZSGnAJe3MMi1E2Y+8ksYX8No7bhepulrJds fdEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796057273; x=1796662073; h=content-transfer-encoding:content-type:cc:to:subject:from :content-language:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=4OqtmamPktkQ6npWbkeGCBBvOiauJ1w71WJmjLckShc=; b=HzjGIZ4Rbhf0DZZr9DVteJilXhC5ip0kNY7A7Z3CbWt4zbHe78w+WSvE1psiQQ7aym heCUiLyGvKgKEpXf43xAg+Mn69tkSaNN6tJRGodOx/3eKJfjJwldKupDh2YxeIZqNfNr fXu1lEjUjpkkSNtlO2IWT3fsYe7sdPh+icu8KYmavuZC6+KYbFE+XM0Bzqebc5roBPP0 mH5PrYnMtTTCvX1iNX2+SNHlk1MmseZma4KPM2lvB9J0ntJwIcF8nibE54ERBnH/VbKg xpV7PyRr14cmBlBXgvOsWAx8R6hcIxbwapNJD0c+tGd8Exb7erTyBGYeNqDXJhRrx9et 5sZw== X-Forwarded-Encrypted: i=1; AL+hErHxLFiw8+/LL8rsf82fnCm/XsL8BiKojBa+twLl6bdT5HhvEvYHjLvhPGFzIesexXEX6qEo5KamlSUqMA4=@vger.kernel.org X-Gm-Message-State: APuU4Z9SXxGKw+bSTkpkuf7JXZyNNUgpSYHXB9qpBdlG9lbBXEWLruay O5ilp74wtojHkmQoIcRFm7YphBBKurUha2cUoCIjbwsLlc4yCZ4rG6OS X-Gm-Gg: AZFdTZfdvI8W0ae+5LgZGjutXf7hoFZA/FvKi/e9IpvecLEkrCZlsPct+x0h3H5mAPz jCsE2sNNC6SlO7ICln/H6BioT8WEVhDZzc2eTKecP7E01zMS6cWzvKvIcvMRWRU8er+UANCp7ho 0stAqGuEavJfLy6NcmrpuzpxgMJvIxkl3peVkzMEW5KuNyA4nAcz4H55x0azOk4J1jzvlPIDk4S iQTh4dGcKZEPjuveCOsyHLbLi+h7+BKMcMVbMZHNJ3JOdq+n87070nwG5yWmbZv/T6f+nE2k/JC bJ+4hi+TOV8gMXJ+b/6iIrLqKvl0+TdqX3ypgNjNGW3zGDk+YLWMC/k/IQ+e10TrTRWpRxm7wS7 Aegjim1HOfJmivrxJWEql1iujlLJyr3nFQD+ySXFX1OpmBfz2JSl83jMxxtiO9eny44bhryOYV8 f93arQvv2/A5YBmW6UXtDNh/ZfX7Y9/sJtGYeoGDgoc6lUVDflQLT7olbtAo9Bu+0JpHFUD2bA8 x4geh0dlH1UVRlIOkIU6Nq1IGWIMfNPf+dkWIZ75Q2WH++pD5amqWbIc8hw3JpVlABn3U62IBGw qP4Ds+nuCAWaTYg+uJfqpU7oucrca+HE6evSW1iNqxcPne+r1KY9lOwjNOPhWA== X-Received: by 2002:a05:6512:3f23:b0:5c1:528e:1a9c with SMTP id 2adb3069b0e04-5c1bc8194f1mr10403863e87.52.1796057273349; Mon, 30 Nov 2026 08:47:53 -0800 (PST) Received: from [192.168.0.103] ([46.8.6.187]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5c3475b6675sm2060338e87.62.2026.11.30.08.47.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 08:47:52 -0800 (PST) Message-ID: Date: Mon, 30 Nov 2026 19:47:51 +0300 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Egor Shalashnov Subject: [RFC] percpu_ref: deadlock when release calls exit on an atomic ref To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, After implementing hashed switch locks, I continued experimenting with percpu_ref and came across a deadlock when killing a ref that is already in atomic mode and whose release callback calls percpu_ref_exit(). This also reproduces with the original global switch lock. A minimal example is: static void release(struct percpu_ref *ref) { percpu_ref_exit(ref); } static int reproduce(void) { struct percpu_ref ref; int ret; ret = percpu_ref_init(&ref, release, PERCPU_REF_INIT_ATOMIC, GFP_KERNEL); if (ret) return ret; percpu_ref_kill(&ref); return 0; } percpu_ref_kill_and_confirm() holds percpu_ref_switch_lock while dropping the initial reference. With no additional references, percpu_ref_put() decrements the count from 1 to 0 and invokes release() synchronously. percpu_ref_exit() then tries to acquire the same lock: percpu_ref_kill() percpu_ref_kill_and_confirm() spin_lock_irqsave() percpu_ref_put() release() percpu_ref_exit() spin_lock_irqsave() /* recursive acquisition */ The same situation can arise after explicitly switching a ref to atomic mode. I have not identified an in-tree caller that triggers this exact sequence, but the documentation for percpu_ref_exit() explicitly says: "The usual places to invoke this function from are the @ref->release() callback or in init failure path [...]" When kill starts from percpu mode, the transition to atomic mode takes an extra reference, which is dropped from the RCU callback. This prevents the put in percpu_ref_kill_and_confirm() from invoking release() while holding the switch lock. No such extra reference is taken when the ref is already in atomic mode. My codex agent suggested the following fix: diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -393,9 +393,10 @@ ref->percpu_count_ptr |= __PERCPU_REF_DEAD; __percpu_ref_switch_mode(ref, confirm_kill); + + spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + percpu_ref_put(ref); - - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm); However, I'm not sure whether moving the put outside the lock could introduce races with concurrent mode switching or reinitialization. Is there a reason the initial reference needs to be dropped while holding the switch lock? If this usage is intentionally unsupported, should the documentation explicitly warn that calling percpu_ref_exit() from the release callback can deadlock when kill drops the last reference on a ref already in atomic mode? Thanks, Egor