From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3E5842A7B1 for ; Mon, 20 Jul 2026 14:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784557775; cv=none; b=sJE+mbfbHo/pjzwRWZuovyjLfcT3giXOI7vWm6MEu76g7VPssDxMvwQjTnwYAes8AtyZrzw5WWvsfF85c4Dp1ibLZqUtbiyx+FhNS0aXqr3B1TweQni6i/AUWXiELQiLqqixc9HwBtQzXdX8zigP0h6JVkDacGbQ+lwiVONvFf4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784557775; c=relaxed/simple; bh=RW1rEmwRZWZWBRI317Ab8uK7CcPVT5OUBLsJ1QvtzPo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GS0vpJF9w9bCoeOLbtbcudB6PUtpalP3bnp6utv9jXZjWSp6BRvr1F56QD0qw7OVOB4uInFIUlZxwRF7g04bMogbpaEpbZngo/f3dooWFq8VbzeDzlbxyY+lT8Gz6Uj/zEcMkI9sy0JAlwqKnUO7ointSWs/+UF5VJ4E0tJXU8o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=ISgXyunc; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=P4vFvQAE; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=ISgXyunc; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=P4vFvQAE; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ISgXyunc"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="P4vFvQAE"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ISgXyunc"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="P4vFvQAE" Received: from imap1.dmz-prg2.suse.org (unknown [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-out1.suse.de (Postfix) with ESMTPS id 8451079FCF; Mon, 20 Jul 2026 14:29:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mtAUlclE6KOqACLOJXzP3lbBnDD0ozmS9uuandpFwC4=; b=ISgXyuncxsOmNKEHpWbgZmlvEcLQ7a+uufeHsKQudQufx67G8VWSVaD2ybnvNXc6BLZdGM nvA/dmS03+4HW23nDhogiUsTMuN5AhKbB00uz39J/GLh8ICMv4ozeevk+raa9DCBqoQg2x rHHgYWqXCwo0bYskTomMc21Pht3cNK0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mtAUlclE6KOqACLOJXzP3lbBnDD0ozmS9uuandpFwC4=; b=P4vFvQAEX0x+BYfyrYZe4HJzHZZgHIfdqyAgELWzHgq7047zE2Ff67MCsXxhfv3NKKdZv0 EYQqZk9cYQb4AFAg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mtAUlclE6KOqACLOJXzP3lbBnDD0ozmS9uuandpFwC4=; b=ISgXyuncxsOmNKEHpWbgZmlvEcLQ7a+uufeHsKQudQufx67G8VWSVaD2ybnvNXc6BLZdGM nvA/dmS03+4HW23nDhogiUsTMuN5AhKbB00uz39J/GLh8ICMv4ozeevk+raa9DCBqoQg2x rHHgYWqXCwo0bYskTomMc21Pht3cNK0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557761; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mtAUlclE6KOqACLOJXzP3lbBnDD0ozmS9uuandpFwC4=; b=P4vFvQAEX0x+BYfyrYZe4HJzHZZgHIfdqyAgELWzHgq7047zE2Ff67MCsXxhfv3NKKdZv0 EYQqZk9cYQb4AFAg== 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 80362779AA; Mon, 20 Jul 2026 14:29:20 +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 COzlG8AwXmrKTQAAD6G6ig (envelope-from ); Mon, 20 Jul 2026 14:29:20 +0000 From: Pedro Falcato To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes Cc: Pedro Falcato , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/8] mm/khugepaged: simplify prepare folio locking and exit paths Date: Mon, 20 Jul 2026 15:29:09 +0100 Message-ID: <20260720142913.846902-5-pfalcato@suse.de> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260720142913.846902-1-pfalcato@suse.de> References: <20260720142913.846902-1-pfalcato@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Flag: NO X-Spamd-Result: default: False [-6.80 / 50.00]; REPLY(-4.00)[]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.992]; MIME_GOOD(-0.10)[text/plain]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:email,suse.de:mid]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_DN_SOME(0.00)[]; RCPT_COUNT_TWELVE(0.00)[15]; RCVD_VIA_SMTP_AUTH(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; R_RATELIMIT(0.00)[to_ip_from(RL8d8xedm6iu8o66torxsk6bwd)]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Score: -6.80 folio_trylock() + folio_get() do the exact same thing on both is_shmem and !is_shmem paths. Unify them into one path. At the end of the day, there are two large invariants for the function: - xarray lock is not held at exit. Either it was dropped on error, or dropped on success/IO. - success always means a locked-and-ref'd-up folio. After this transformation, it's trivial to notice how simple the error paths are, so do away with the gotos and hoist some code. Signed-off-by: Pedro Falcato --- mm/khugepaged.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index ed5d23317177..d170b64378e6 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2259,8 +2259,8 @@ static struct folio *collapse_read_folio(pgoff_t index, struct collapse_file_sta static enum scan_result prepare_collapse_file_folio(pgoff_t index, struct collapse_file_state *state) { struct address_space *mapping = state->mapping; - enum scan_result result = SCAN_SUCCEED; const int is_shmem = state->is_shmem; + enum scan_result ret = SCAN_SUCCEED; struct folio *folio; bool dirty; @@ -2276,7 +2276,8 @@ static enum scan_result prepare_collapse_file_folio(pgoff_t index, struct collap * will not succeed. */ lru_add_drain(); - goto xa_unlocked; + state->folio = folio; + return SCAN_SUCCEED; } if (!is_shmem) { @@ -2299,32 +2300,21 @@ static enum scan_result prepare_collapse_file_folio(pgoff_t index, struct collap xas_unlock_irq(state->xas); if (dirty && !inode_is_open_for_write(mapping->host)) filemap_flush(mapping); - result = SCAN_PAGE_DIRTY_OR_WRITEBACK; - goto xa_unlocked; + return SCAN_PAGE_DIRTY_OR_WRITEBACK; } } - if (is_shmem) { - if (folio_trylock(folio)) { - folio_get(folio); - } else { - result = SCAN_PAGE_LOCK; - goto xa_locked; - } - } else { /* !is_shmem */ - if (folio_trylock(folio)) { - folio_get(folio); - } else { - result = SCAN_PAGE_LOCK; - goto xa_locked; - } - } -xa_locked: + /* + * Note: trylock + simple folio_get() is safe here due to the i_pages + * lock being held; this excludes truncation happening in parallel. + */ + if (!folio_trylock(folio)) + ret = SCAN_PAGE_LOCK; + else + folio_get(folio); xas_unlock_irq(state->xas); -xa_unlocked: - state->folio = folio; - return result; + return ret; } /** -- 2.55.0