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 C692A42B732 for ; Mon, 20 Jul 2026 14:29:27 +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=1784557769; cv=none; b=QJBsK06QlCu2z4fRYUwvgGiy2ytIL10HwsaR/VvSETO3607a4qqwzI9INkf900g0nRdmXZpQDaeMFaWWfanLlkEWC5Sow66C3qOFbyMpUmX5xnGhQqqG3zEQuCRFr6fQq+IlJMWWAyaJs3IaN9l8OZRQ+/ilHVAKf5n6AAexc0Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784557769; c=relaxed/simple; bh=lvDcDspryRvBxxLCYm2W4oriv+PudjLNC0uD4dOVw6A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tyGn1C0zPRKjLk8WwU72l3SmfBmQeP7juwFxiqyR8FUqXGrv/JtdWQJPy1ND4V6ZHwY0Mu2pmsDpNhTJl7MxWbY3zDGG7Vfg1K/qD5IkAhjwUSyKDzU+q25jYcxqVhzQiIDrNglENQiQjIsC7cJWxhrH+rSKjlx9XfLo9HBSOVk= 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=r40uyrRo; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=y4Jm4JzQ; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=r40uyrRo; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=y4Jm4JzQ; 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="r40uyrRo"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="y4Jm4JzQ"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="r40uyrRo"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="y4Jm4JzQ" 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 671B679FCD; Mon, 20 Jul 2026 14:29:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557760; 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=tOHlMuSJ0FI7SQpr2VHqCuu+f1bO4v5hqudfd2KAok4=; b=r40uyrRoiqYJ5w6S9nx2T3R+WF0RHP5QmYJqEeckMXPikZ6+AZKWmnj68J3Y0QIgvXOwEC El8mJyYZ8SY6PLgH1k+vUVZK6ay/sG5j1i5QRQ00ZCWo4EnkKnIKKy8tp0snW6byxdiOZN Z6uH16Rb/t596hIIkccQu4M9XmD2X+A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557760; 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=tOHlMuSJ0FI7SQpr2VHqCuu+f1bO4v5hqudfd2KAok4=; b=y4Jm4JzQQMIayljn52lr6HptDGAg2ypunSHsK2VmiluQ0ISRluZh4giM0O4P5f8ldfzdbx vGFugCDgeOSecFDg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557760; 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=tOHlMuSJ0FI7SQpr2VHqCuu+f1bO4v5hqudfd2KAok4=; b=r40uyrRoiqYJ5w6S9nx2T3R+WF0RHP5QmYJqEeckMXPikZ6+AZKWmnj68J3Y0QIgvXOwEC El8mJyYZ8SY6PLgH1k+vUVZK6ay/sG5j1i5QRQ00ZCWo4EnkKnIKKy8tp0snW6byxdiOZN Z6uH16Rb/t596hIIkccQu4M9XmD2X+A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557760; 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=tOHlMuSJ0FI7SQpr2VHqCuu+f1bO4v5hqudfd2KAok4=; b=y4Jm4JzQQMIayljn52lr6HptDGAg2ypunSHsK2VmiluQ0ISRluZh4giM0O4P5f8ldfzdbx vGFugCDgeOSecFDg== 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 69BCE779AA; Mon, 20 Jul 2026 14:29:19 +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 WFyAFr8wXmrKTQAAD6G6ig (envelope-from ); Mon, 20 Jul 2026 14:29:19 +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 3/8] mm/khugepaged: factor out and simplify dirty/writeback handling Date: Mon, 20 Jul 2026 15:29:08 +0100 Message-ID: <20260720142913.846902-4-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 Dirty/writeback folios can easily share most of the branch. These checks are still gated under !is_shmem due to shmem not really having issues with dirty folios. While here, reflow the comment. Signed-off-by: Pedro Falcato --- mm/khugepaged.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 4cc6917a55c7..ed5d23317177 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2262,6 +2262,7 @@ static enum scan_result prepare_collapse_file_folio(pgoff_t index, struct collap enum scan_result result = SCAN_SUCCEED; const int is_shmem = state->is_shmem; struct folio *folio; + bool dirty; folio = collapse_read_folio(index, state); if (!folio) @@ -2278,39 +2279,40 @@ static enum scan_result prepare_collapse_file_folio(pgoff_t index, struct collap goto xa_unlocked; } - if (is_shmem) { - if (folio_trylock(folio)) { - folio_get(folio); - } else { - result = SCAN_PAGE_LOCK; - goto xa_locked; - } - } else { /* !is_shmem */ - if (folio_test_dirty(folio)) { + if (!is_shmem) { + dirty = folio_test_dirty(folio); + if (dirty || folio_test_writeback(folio)) { /* - * This page is dirty because it hasn't - * been flushed since first write. + * This folio is either dirty or under writeback. + * khugepaged cannot operate on such folios. * - * Trigger async flush for read-only files and - * hope the writeback is done when khugepaged - * revisits this page. Writable files can have - * their folios dirty at any time; blindly - * flushing them would cause undesirable + * For dirty folios, trigger async flush for + * read-only files and hope the writeback is done + * when khugepaged revisits this page. Writable + * files can have their folios dirty at any time; + * blindly flushing them would cause undesirable * system-wide writeback. * * This is a one-off situation. We are not * forcing writeback in loop. */ xas_unlock_irq(state->xas); - if (!inode_is_open_for_write(mapping->host)) + if (dirty && !inode_is_open_for_write(mapping->host)) filemap_flush(mapping); result = SCAN_PAGE_DIRTY_OR_WRITEBACK; goto xa_unlocked; - } else if (folio_test_writeback(folio)) { - xas_unlock_irq(state->xas); - result = SCAN_PAGE_DIRTY_OR_WRITEBACK; - goto xa_unlocked; - } else if (folio_trylock(folio)) { + } + } + + 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; -- 2.55.0