From patchwork Mon Jul 15 20:29:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vlastimil Babka X-Patchwork-Id: 13733876 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 922CCC3DA59 for ; Mon, 15 Jul 2024 20:30:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 034086B00AA; Mon, 15 Jul 2024 16:29:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B35C46B00AB; Mon, 15 Jul 2024 16:29:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86DA76B009B; Mon, 15 Jul 2024 16:29:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 481396B009B for ; Mon, 15 Jul 2024 16:29:44 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EEC34C0224 for ; Mon, 15 Jul 2024 20:29:43 +0000 (UTC) X-FDA: 82343127846.08.A51CD45 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf13.hostedemail.com (Postfix) with ESMTP id B47362002B for ; Mon, 15 Jul 2024 20:29:40 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=y942AQiL; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BNzA+PdL; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=y942AQiL; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BNzA+PdL; spf=pass (imf13.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721075362; h=from:from:sender: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: references:dkim-signature; bh=nFhQJkP0hVFtv7RiR1BnRvXpYdV4uR56kXUx1iBwLxw=; b=7WqvvHncw/EdCcEMhGFuXRbWX6lt8v5ldg8UIEhDY1rjlh1G/74YqYUr8rD8Zpd1Qgn2b+ 6bqZEGWgeX7UTo/rSVNXK/lGrB437VcB3p4kjhGcQtFmCYV3iwESnopXXjoRy6a/tVL3oC FjlTxvBJJdJEj9lHtoQCP5ZsN+Ul7CY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=y942AQiL; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BNzA+PdL; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=y942AQiL; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=BNzA+PdL; spf=pass (imf13.hostedemail.com: domain of vbabka@suse.cz designates 195.135.223.131 as permitted sender) smtp.mailfrom=vbabka@suse.cz; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721075362; a=rsa-sha256; cv=none; b=jHG44tkUmcsmv+fQladHpONUPWtCPFc2u2mJ54q+s/HuqTysx6iZbN2ObS5b26qiXY7rPD qvM5g0TbBZBA0S4IsUBewHfzeFI5idixHS0z8EXGLcCJV+3OP3HyDstFZ8rmV8YRMtzFTY tCfljUNW2o2aY0jh0YizOMVfQcFhoZA= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A8D4D1F833; Mon, 15 Jul 2024 20:29:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1721075378; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nFhQJkP0hVFtv7RiR1BnRvXpYdV4uR56kXUx1iBwLxw=; b=y942AQiLOBUNQndlSNAur7bBcg+/vbEy+4H+qO+0upE5Hx9nPwnT7ceufmKKqikDHL8v19 bZw23E+Cc8cJUxs6BElFQKfe2CzrlRsJNgdVGQJNRyAYmp4KVfqACxSM68hrUYy8FkJy2a +B3poHOCLzXk00jSlDnIxz9QxBWhy/Q= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1721075378; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nFhQJkP0hVFtv7RiR1BnRvXpYdV4uR56kXUx1iBwLxw=; b=BNzA+PdLwNNR7LhbjRyxnB2nzkgpFlP9hqsnfvC7wZkrlhEbHQjxyF+exu92uqcNLBvgyM GLjKKUDgfyS211Cg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1721075378; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nFhQJkP0hVFtv7RiR1BnRvXpYdV4uR56kXUx1iBwLxw=; b=y942AQiLOBUNQndlSNAur7bBcg+/vbEy+4H+qO+0upE5Hx9nPwnT7ceufmKKqikDHL8v19 bZw23E+Cc8cJUxs6BElFQKfe2CzrlRsJNgdVGQJNRyAYmp4KVfqACxSM68hrUYy8FkJy2a +B3poHOCLzXk00jSlDnIxz9QxBWhy/Q= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1721075378; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nFhQJkP0hVFtv7RiR1BnRvXpYdV4uR56kXUx1iBwLxw=; b=BNzA+PdLwNNR7LhbjRyxnB2nzkgpFlP9hqsnfvC7wZkrlhEbHQjxyF+exu92uqcNLBvgyM GLjKKUDgfyS211Cg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 78D6A137EB; Mon, 15 Jul 2024 20:29:38 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id AkvnHLKGlWbvUwAAD6G6ig (envelope-from ); Mon, 15 Jul 2024 20:29:38 +0000 From: Vlastimil Babka Subject: [PATCH RFC 0/6] mm, slub: handle pending kfree_rcu() in kmem_cache_destroy() Date: Mon, 15 Jul 2024 22:29:26 +0200 Message-Id: <20240715-b4-slab-kfree_rcu-destroy-v1-0-46b2984c2205@suse.cz> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAKaGlWYC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIxMDc0NT3SQT3eKcxCTd7LSi1NT4ouRS3ZTU4pKi/EpdC9OUFKMko5TUFEs jJaD+gqLUtMwKsNnRSkFuzkqxtbUAu5BS8XAAAAA= To: "Paul E. McKenney" , Joel Fernandes , Josh Triplett , Boqun Feng , Christoph Lameter , David Rientjes Cc: Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Julia Lawall , Jakub Kicinski , "Jason A. Donenfeld" , "Uladzislau Rezki (Sony)" , Andrew Morton , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, rcu@vger.kernel.org, Vlastimil Babka X-Mailer: b4 0.14.0 X-Rspamd-Action: no action X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: B47362002B X-Stat-Signature: de8stnotaxjh5xqe8mi5bwjxr6qzdrtd X-HE-Tag: 1721075380-190637 X-HE-Meta: U2FsdGVkX18ksymk/Bk2smjlJC3ILxvxxQXSGOF16wjl+KjKyxYhoT94T+Erx/Yjvs+JMX+OTMt2tqAEKvo2QDYcKKQU8DtpUJCMD6nrMz04FwFUinx1TSXMbLDaTzHgO/6o7anDOIuzQAZT+puEGm8zYTKQe1+VCX0S9RvwCDOi+XTd7dYUz88OdfZzWMjyGrXb3kvSOGAGPStT06NNTdlOE/fRxx2e/X7+PGpbAL6ggub2t6BaVfQHnRM2xK1c0+u8RUExOJ8xBoWA+Ui469xK0DN1KA9ZQFTSSV7KXLyZOcx34T94BEcocXJItNPMFMNpZ2N5BJg8Ra0hWbKUuppBJqW9s0y5+nutPWnqlYMtjEbJxJ1COCu+AWywuCaw+DIvp5WMEBqApefjtiJDmQiNlV/dUw1tZGBYoL1xt3HdcfjLM7bq8CxA5VaV8S3Bg28Awxi/zJqdzc61jNnBavVvFNuZEg50YF76iAjT90DAarN5RRNXt6FcSaEQi9Yaea+haGO+aIHIeCcvouSwPMIMHT4NrDHJcg1TzWsmMJ/cYnvp0jSGbjRgW4QsbtHdRgG5MXqbLU/6FpggEoMzF4iuXLu5+unoRnL/3Us+eFlQ+QL9TqIIasRdmOf5opmn+EsJJo5YbNoMn+UEbWebjp93ik2UTuHJpUYpJqFmG5Q7dC98dqS/rg/hXtNc/dRbnv7bQt60tq2HE1ehC3UT1LXbWFD+fam4D7tpjFDvtH0ZqIlNMW7W7nHY3Wfd8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nb/wN+V6JU+zYIR6fGTSfKHVh6FyPIYkDc6Oz7MyibCUkSA+VebhVdWF6lB+jOrREU8oua1kZBjNN797YLSyxQ67M8xwn+wfRJ5aiioVWDlrS6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: First RFC, feel free to ignore for now if too busy with merge window. Also in git: https://git.kernel.org/vbabka/l/slab-kfree_rcu-destroy-v1r0 Based on slab/for-next: https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git/log/?h=slab/for-next Since SLOB was removed, we have allowed kfree_rcu() for objects allocated from any kmem_cache in addition to kmalloc(). Recently we have attempted to replace existing call_rcu() usage with kfree_rcu() where the callback is a plain kmem_cache_free(), in a series by Julia Lawall [1]. Jakub Kicinski pointed out [2] this was tried already in batman-adv but had to be reverted due to kmem_cache_destroy() failing due to objects remaining in the cache, despite rcu_barrier() being used. Jason Donenfeld found the culprit [3] being a35d16905efc ("rcu: Add basic support for kfree_rcu() batching") causing rcu_barrier() to be insufficient. This was never a problem for kfree_rcu() usage on kmalloc() objects as the kmalloc caches are never destroyed, but arbitrary caches can be, e.g. due to module unload. Out of the possible solutions collected by Paul McKenney [4] the most appealing to me is "kmem_cache_destroy() lingers for kfree_rcu()" as it adds no additional concerns to kfree_rcu() users. We already have the precedence in some parts of the kmem_cache cleanup being done asynchronously for SLAB_TYPESAFE_BY_RCU caches. This series implements the necessary changes to the slab allocator, mainly that if there are objects remaining in the cache when kmem_cache_destroy() is called it is assumed that it's due to pending kfree_rcu(), and a asynchronous work is scheduled that performs the necessary barrier and then tries again. If objects remain after the barrier, the usual warning is reported and the cache remains undestroyed. Notably the proper barrier doesn't yet exist so hopefully Paul or the RCU team can help here :) it should be able to make it pessimistic as it won't hold up anything but the work item. Some downsides of this approach for debugging exist but should be small enough: - we can no longer report the stack trace leading to a premature kmem_cache_destroy(), but arguably that's not that interesting as the allocation traces and other details about the remaining objects, which are still reported, just a bit later. - objects that are freed after kmem_cache_destroy() but before the work item proceeds with the destroy after the barrier, are technically bugs, but we won't be able to catch them unless we add some checks into the freeing hotpaths. It's not worth it, IMHO. There is also a bunch of preliminary steps. The potentially visible one is that sysfs and debugfs directories are now removed immediately during kmem_cache_destroy() - previously this would be delayed for SLAB_TYPESAFE_BY_RCU caches or left around forever if remaining objects were detected. The extra delay by asynchronous destroy due to kfree_rcu() could mean that a module unload/load cycle could create a new instance of the cache which would fail to create these directories - a concern raised by Paul. The immediate removal is the simplest solution (compared to e.g. renaming the directories) and should not make debugging harder - while it won't be possible to check debugfs for allocation traces of leaked objects, they are listed with more detail in dmesg anyway. [1] https://lore.kernel.org/all/20240609082726.32742-1-Julia.Lawall@inria.fr/ [2] https://lore.kernel.org/all/20240612143305.451abf58@kernel.org/ [3] https://lore.kernel.org/all/Zmo9-YGraiCj5-MI@zx2c4.com/ [4] https://docs.google.com/document/d/1v0rcZLvvjVGejT3523W0rDy_sLFu2LWc_NR3fQItZaA/edit To: Paul E. McKenney To: Joel Fernandes To: Josh Triplett To: Boqun Feng Cc: Steven Rostedt CC: Mathieu Desnoyers Cc: Lai Jiangshan Cc: Zqiang Cc: Julia Lawall Cc: Jakub Kicinski Cc: Jason A. Donenfeld Cc: Uladzislau Rezki (Sony) To: Christoph Lameter To: David Rientjes Cc: Andrew Morton Cc: Roman Gushchin Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org Cc: rcu@vger.kernel.org Signed-off-by: Vlastimil Babka --- Vlastimil Babka (6): mm, slab: make caches with refcount of 0 unmergeable mm, slab: always maintain per-node slab and object count mm, slab: unlink sysfs and debugfs immediately mm, slab: simplify kmem_cache_release() mm, slab: asynchronously destroy caches with outstanding objects kunit, slub: add test_kfree_rcu() lib/slub_kunit.c | 22 ++++++++++ mm/slab.h | 4 +- mm/slab_common.c | 121 +++++++++++++++++++++++++++++++++++-------------------- mm/slub.c | 58 ++++++++++++-------------- 4 files changed, 129 insertions(+), 76 deletions(-) --- base-commit: 436381eaf2a423e60fc8340399f7d2458091b383 change-id: 20240715-b4-slab-kfree_rcu-destroy-85dd2b2ded92 Best regards,