From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (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 5953C42A7B1 for ; Mon, 20 Jul 2026 14:29:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784557779; cv=none; b=V/Js3bYncNDcFQOEi9YoU2lUbjNG0jbuMyEK9rBebqtVYQLa36JZzlxxdGjbbnI9ENcQqggHGG+pi8VPrtdQ2fF02lCsL8jlNLmTVmc+1+iTmPUO781v08g4Cdcm6gEonAlmAM0Swi8DtbpF1rnVwVNtoJcIvFx+4DAoj27mDgI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784557779; c=relaxed/simple; bh=+BIRuKu8qqAY1LVaoR6nr009QzucxOO0f8gD0aYY4LM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FVlkMt28bXkTxGbFeQagHiIw8g2YBTEgzrA1U32fn3Vca98vlAD4L5YUWLawuhPqVQ3PpNjFVawwfN0fSqw5aUKrxHiASdVdBY8Nv4axtGeHsguBczdyor9olyk5ZEUmZ9zTyoNfwtS/yp4OeBLjmy2fIBn9xYM1YzdnTemYSaA= 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=I8f093Gt; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=zIPot4lI; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=I8f093Gt; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=zIPot4lI; arc=none smtp.client-ip=195.135.223.131 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="I8f093Gt"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="zIPot4lI"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="I8f093Gt"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="zIPot4lI" 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-out2.suse.de (Postfix) with ESMTPS id D8F663E22; Mon, 20 Jul 2026 14:29:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557764; 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=QwWtCS1crk1JPnVWYwUvcYFHRLHis6CsOgoX49n95Ww=; b=I8f093GtNZzJw23a862NbIXxjnZL0gLD4DhsL4eieV5dsdC9VLY0iNBweLhQtwx8BksDQZ tvyfKJmwjTK7+L3H3JEd+qIG6Qyx59u81vbdBKiE2An3hNI98zWpiJNYZT4H7Y7hLm4X/E qsZ3y4+nukAPhchkdttG7sRYkNXevbA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557764; 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=QwWtCS1crk1JPnVWYwUvcYFHRLHis6CsOgoX49n95Ww=; b=zIPot4lIM66SF1r38ioJl2NT6cNaDwQdluP8gN4lhBmyRhMnDwkgzXXG6ktKmg9iV/aNCM ntUAYPOOEj4Z25AA== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1784557764; 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=QwWtCS1crk1JPnVWYwUvcYFHRLHis6CsOgoX49n95Ww=; b=I8f093GtNZzJw23a862NbIXxjnZL0gLD4DhsL4eieV5dsdC9VLY0iNBweLhQtwx8BksDQZ tvyfKJmwjTK7+L3H3JEd+qIG6Qyx59u81vbdBKiE2An3hNI98zWpiJNYZT4H7Y7hLm4X/E qsZ3y4+nukAPhchkdttG7sRYkNXevbA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1784557764; 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=QwWtCS1crk1JPnVWYwUvcYFHRLHis6CsOgoX49n95Ww=; b=zIPot4lIM66SF1r38ioJl2NT6cNaDwQdluP8gN4lhBmyRhMnDwkgzXXG6ktKmg9iV/aNCM ntUAYPOOEj4Z25AA== 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 D4C21779AA; Mon, 20 Jul 2026 14:29:23 +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 +PvML8MwXmrKTQAAD6G6ig (envelope-from ); Mon, 20 Jul 2026 14:29:23 +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 7/8] mm/khugepaged: hoist more code into collapse_isolate_folio() Date: Mon, 20 Jul 2026 15:29:12 +0100 Message-ID: <20260720142913.846902-8-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.994]; MIME_GOOD(-0.10)[text/plain]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email,suse.de:mid,imap1.dmz-prg2.suse.org:helo]; 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 Checking expected refcounts or dirtyness has no need for the i_pages xarray lock. Signed-off-by: Pedro Falcato --- mm/khugepaged.c | 83 ++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 429e2c5833d0..a09e4e4e1943 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2382,13 +2382,48 @@ static enum scan_result collapse_isolate_folio(struct collapse_file_state *state if (!filemap_release_folio(folio, GFP_KERNEL)) { result = SCAN_PAGE_HAS_PRIVATE; - folio_putback_lru(folio); - goto out_unlock; + goto out_putback; } if (folio_mapped(folio)) try_to_unmap(folio, TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH); + /* + * We control 2 + nr_pages references to the folio: + * - we hold a pin on it; + * - nr_pages reference from page cache; + * - one from lru_isolate_folio; + * If those are the only references, then any new usage + * of the folio will have to fetch it from the page + * cache. That requires locking the folio to handle + * truncate, so any new usage will be blocked until we + * unlock folio after collapse/during rollback. + */ + if (folio_ref_count(folio) != 2 + folio_nr_pages(folio)) { + result = SCAN_PAGE_COUNT; + goto out_putback; + } + + /* + * At this point, the folio is locked and unmapped. If the PTE + * was dirty, try_to_unmap() has transferred the dirty bit to + * the folio and we must not collapse it into a clean + * file-backed folio. + * + * If the folio is clean here, no one can write it until we + * drop the folio lock. A write through a stale TLB entry came + * from a clean PTE and must fault because the PTE has been + * cleared; the fault path has to take the folio lock before + * installing a writable mapping. Buffered write paths also + * have to take the folio lock before modifying file contents + * without a mapping, typically via write_begin_get_folio(). + */ + if (!state->is_shmem && folio_test_dirty(folio)) { + result = SCAN_PAGE_DIRTY_OR_WRITEBACK; + goto out_putback; + } return SCAN_SUCCEED; +out_putback: + folio_putback_lru(folio); out_unlock: folio_unlock(folio); folio_put(folio); @@ -2513,55 +2548,11 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, xas_lock_irq(&xas); VM_BUG_ON_FOLIO(folio != xa_load(xas.xa, index), folio); - /* - * We control 2 + nr_pages references to the folio: - * - we hold a pin on it; - * - nr_pages reference from page cache; - * - one from lru_isolate_folio; - * If those are the only references, then any new usage - * of the folio will have to fetch it from the page - * cache. That requires locking the folio to handle - * truncate, so any new usage will be blocked until we - * unlock folio after collapse/during rollback. - */ - if (folio_ref_count(folio) != 2 + folio_nr_pages(folio)) { - result = SCAN_PAGE_COUNT; - xas_unlock_irq(&xas); - folio_putback_lru(folio); - goto out_unlock; - } - - /* - * At this point, the folio is locked and unmapped. If the PTE - * was dirty, try_to_unmap() has transferred the dirty bit to - * the folio and we must not collapse it into a clean - * file-backed folio. - * - * If the folio is clean here, no one can write it until we - * drop the folio lock. A write through a stale TLB entry came - * from a clean PTE and must fault because the PTE has been - * cleared; the fault path has to take the folio lock before - * installing a writable mapping. Buffered write paths also - * have to take the folio lock before modifying file contents - * without a mapping, typically via write_begin_get_folio(). - */ - if (!is_shmem && folio_test_dirty(folio)) { - result = SCAN_PAGE_DIRTY_OR_WRITEBACK; - xas_unlock_irq(&xas); - folio_putback_lru(folio); - goto out_unlock; - } - /* * Accumulate the folios that are being collapsed. */ list_add_tail(&folio->lru, &pagelist); index += folio_nr_pages(folio); - continue; -out_unlock: - folio_unlock(folio); - folio_put(folio); - goto xa_unlocked; } xa_locked: -- 2.55.0