[PATCH 2/2] ntfs: fix kmap_local leak in ntfs_check_logfile()
From: Karl Mehltretter
Date: Tue Oct 06 2026 - 00:43:17 EST
ntfs_check_logfile() calls kmap_local_folio() on every iteration of its
search loop but kunmap_local() only once per folio. The loop visits
positions 0, 512, 1024, 2048, 4096 and so on. With 4 KiB pages the first
four are in one page, so a mount whose first LogFile folio is in highmem
leaves three entries on the kmap_local stack of the mounting task.
Kernels without HIGHMEM are not affected.
The leaked entries keep migration disabled. nr_pinned of the runqueue
the mount ran on stays raised after the task exits, so taking that CPU
offline blocks forever in balance_hotplug_wait().
The fifth mount in one task reaches KM_MAX_IDX (12 leaked entries plus
4 in the loop) and hits the BUG_ON() in kmap_local_idx_push():
kernel BUG at mm/highmem.c:480!
On x86-32 the generic entry code also warns on return to user space:
DEBUG_LOCKS_WARN_ON(current->kmap_ctrl.idx)
WARNING: include/linux/highmem-internal.h:17 at __do_fast_syscall_32+0x21e/0x550
Map each page once, when it is read, and unmap that address.
Fixes: 5218cd102aec ("ntfs: update misc operations")
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: LLM
Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
---
Tested in QEMU on top of patch 1 with a fresh mkntfs volume, on the ARM
and x86-32 kernels described there. ARM and x86-32 behave the same in
all three tests.
- One mount on CPU 1 with taskset, then writing 0 to
/sys/devices/system/cpu/cpu1/online: without this patch the write
was still blocked after 60 seconds and new ssh logins hung. With it
CPU 1 goes offline. The same write before the mount works on both
kernels.
- A program that calls mount(2) and umount(2) eight times in one
process: without this patch mounts one to four succeed and the fifth
hits the BUG_ON() in kmap_local_idx_push(). With it all eight
complete.
- Three mounts in a row with mount(8) are clean. x86-32 no longer warns
when mount(2) returns.
fs/ntfs/logfile.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/fs/ntfs/logfile.c b/fs/ntfs/logfile.c
index 1404664dacc0..ce38ce87f8fc 100644
--- a/fs/ntfs/logfile.c
+++ b/fs/ntfs/logfile.c
@@ -457,7 +457,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
struct ntfs_volume *vol = NTFS_SB(log_vi->i_sb);
struct address_space *mapping = log_vi->i_mapping;
struct folio *folio = NULL;
- u8 *kaddr = NULL;
+ u8 *kaddr = NULL, *kbase = NULL;
struct restart_page_header *rstr1_ph = NULL;
struct restart_page_header *rstr2_ph = NULL;
int log_page_size, err;
@@ -511,7 +511,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
if (!folio || folio->index != idx) {
if (folio) {
- kunmap_local(kaddr);
+ kunmap_local(kbase);
folio_put(folio);
}
folio = read_mapping_folio(mapping, idx, NULL);
@@ -520,8 +520,9 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
idx);
goto err_out;
}
+ kbase = kmap_local_folio(folio, 0);
}
- kaddr = (u8 *)kmap_local_folio(folio, 0) + (pos & ~PAGE_MASK);
+ kaddr = kbase + (pos & ~PAGE_MASK);
/*
* A non-empty block means the logfile is not empty while an
* empty block after a non-empty block has been encountered
@@ -574,7 +575,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
* find a valid one further in the file.
*/
if (err != -EINVAL) {
- kunmap_local(kaddr);
+ kunmap_local(kbase);
folio_put(folio);
goto err_out;
}
@@ -583,7 +584,7 @@ bool ntfs_check_logfile(struct inode *log_vi, struct restart_page_header **rp)
pos = NTFS_BLOCK_SIZE >> 1;
}
if (folio) {
- kunmap_local(kaddr);
+ kunmap_local(kbase);
folio_put(folio);
}
if (logfile_is_empty) {
--
2.53.0