From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796375315; b=JHqDGbQliURYVscV9RGO8SSf/mPya/PDKnKJwaveDL5k4jxK6zz70m2t5RrXapcot3ls E9gf4fvB6c1gToT8pBRUZvpCyAv/ZltCoeyooBloE3IhEFxF4pZlYYkQsvlJeMfBLmKDT 0ST2Wc2YPwowgKkYvt8gT70Of9jHxMWDE9nvSgahy1mz3laNlnf/ZI/WgSgJcPEzCU4jg fGtpqoegtDO/KsXNcYAwdkvOomAD1xVeeeGOpFqmR6ZHPg7DHvvfH25FYsDSPRr/b/Obn GJzW1EBxm/j7lbRrTav1g2xh4lOv6FTqI3IPrZcDoyLWsRzGUnnWWxalQ88CGxLHaKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796375315; h=DMARC-Filter:Received:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=rlBzcLOp7FHeMbTymY91qEjEE4TcEz443FB7G8FGsq0=; b=mE2p0kUxLmH6+bG21V31sIBq7q8eitN59NOY90i0h90mQKgVw1tlNi3t7AN1E/QUWStx q8ZfTXrGBVyrjv1G/0l7FY7HQApfK3bKJcOE/GD3v8qKUnb2Dtn1nAMo9EoEanvsgG9Rx Z/v0BLFDaL+2107dxEmhjOhDd2Py5VKw+86rXilJHbK8bO9mrsloX8VTy1NywSoxD8jyI 9VNUe/6Sbyg9Edv4wKldxA0c3kISk5ewn2U4n5Fy0wxRZW83qA4oJsrRO7Az8AI3G67xq 6YgOolFYWBpkMO8SnLvc/vo8Y7/Qs5E4FZVzXf5uw3UgIIzQXeKY8DYVXLRxgj5QYEg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.remote-ip=209.85.128.51 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDB1B606A322 for ; Fri, 4 Dec 2026 09:08:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BDB1B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4a35ac4475dso4337115e9.2 for ; Fri, 04 Dec 2026 01:08:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796375313; x=1796980113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=rlBzcLOp7FHeMbTymY91qEjEE4TcEz443FB7G8FGsq0=; b=zfSE9BjRbOh23gZD+OkOErVZY2YLR+sz1sqOa28z0bhmDIIINXRILG9L99Cd+gzJVM rNrUsuTm4Jp267mexG9VmTsHkmuf+YoVlov4d3szXai2ldJYx2IcxzMjNm6niAvH7IDp yQ4pTAFgUaBsuna5O8AKQgV+f/kPLKjNbMAPbsOsOhtlLffb9Y7HwXz2jBs5TBda9XRY /HUFFlqgT5sFIE6/Y7Ip4K6pvkfDQdLEaxZsRJ2fp0mNBzZny+/mq9vuE/EcVlbv6H16 t+0SF0mnQQ3gPDqBypHppAgIzQBbrDZcrbixaWvPEDbrqLdV7bNP/SteQ+PP3rxJg5R9 bDqw== X-Forwarded-Encrypted: i=1; AL+hErHRcOHbXJhRuUbQ0MNYXe9BOJbKqVvKMGyfIw/PAJVwDFmP3MaU6uKpuFtfCWKO06lNfV4xX6c5GoRsxBs=@vger.kernel.org X-Gm-Message-State: APuU4Z/TBW/ej/Goa31N6D0b9MWkAfDBTgeHMSveM/8Arz0p2AvJmFby peSJvfGZI/xq3tTEWQ+bAhMuNHZQIomGOSeAZLLjB9J76pnk7MjizSzt X-Gm-Gg: AZFdTZct7+6YE5WeTwvp6U28oy5xpw7vBKznRjt+7vcXVvuPN8XRMEEEEpHFiN7y4cs 5dAgAQ/T3kKbjmjd+FVKOxp1/QBMdeb2r0YuVM7GOGgWWATZcy0kG64tgdk5LYkrmfQD2lyuHyj 5CJ+mDZ5IIjv+5sRpaIRU5WjL2GW1lfdpY7ZLvqb05NB/UC3C5GPVjhGbQNCsfV6EUM+3GZTMTC U9Z4Ar0H+WbyzVEbKWonGyFVYDd7rsFfiowTP9ryfnmh/Dy/Ez4HLF4PuvrSJCZvVBewG63KZI1 7zrkCglQIj5GgSmx92Eav6pkb4IQvkFf5cnzsCe7uvwddxdac2rl7s6daw0YoiJfPYiaQCQwilR dAlLn9Jk8Zl8BpsYYXGaOaJCh5xbM3T0D5KQi/4FfdSjsFpgSJIF1kqwupgbsb58uAwwnAE6Scl vCyqQ0wa4jslW4FPIlxiolGslxSBrp3j/ouMCauFETS7EROqXdLjm1muby+HSX3JGx6l20nKjI1 5NcdTRS1C0JSTiUWYwL0zFHtrI8M9ie/XYEY5lUWHQzaNhjn8E8emMFLr96FsDFX7A6PuCIj0QB /Jee/LuXlDGsvtEJCe4fWhSBR9rZMvmu/kZS7K0dKsU5ng== X-Received: by 2002:a05:600c:19d0:b0:4bb:a9f7:681 with SMTP id 5b1f17b1804b1-4bbeb6fce55mr22139025e9.3.1796375312872; Fri, 04 Dec 2026 01:08:32 -0800 (PST) Received: from tiehlicka (109-81-27-207.rct.o2.cz. [109.81.27.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d173ab8d22sm4260573f8f.39.2026.12.04.01.08.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:08:32 -0800 (PST) From: Michal Hocko To: David Hildenbrand , Andrew Morton , "Lorenzo Stoakes (ARM)" Cc: David Rientjes , Ben Gardon , Johannes Weiner , , LKML , Michal Hocko , Chris Mason Subject: [PATCH v3] mm, oom: move MMF_OOM_SKIP after page table releasing in exit_mmap Date: Fri, 4 Dec 2026 10:08:27 +0100 Message-ID: <20261204090827.568530-1-mhocko@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202212147.379270-1-mhocko@kernel.org> References: <20261202212147.379270-1-mhocko@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Michal Hocko OOM victim could have consumed quite a lot of memory through page tables and those are currently released after we set MMF_OOM_SKIP in the exit_mmap path. If the memory consumed through the address space is not sufficient to recover from the OOM (e.g. most of it is shared memory etc.) then we could go after another oom victim even though page tables could have released enough memory to prevent from that. Move MMF_OOM_SKIP in exit_mmap path after page tables freeing to capture as much memory as possible to be released. We need to wait for RCU callbacks to see that memory freed. In order to prevent oom reaper or process_mrelease to stumble over freed page tables they are checking MMF_OOM_SKIP under mmap_lock for read which is synchronized with MMF_OOM_SKIP set under exclusive lock after page tables are freed. The move is safe from the forward progress POV too because oom_reaper will run concurrently in case the exit path gets stuck and also if RCU callbacks are not making forward progress for whatever reason and it will bail out by setting MMF_OOM_SKIP. Up until exit_mmap takes the exclusive mmap_lock they will cooperate in releasing the address space. We are going to see less memory freed by the oom reaper because it cannot tear down page tables directly but that should be acceptable. Please note that the reaper execution is delayed so the freeing should be typically done the exit path. Reported-by: Chris Mason Signed-off-by: Michal Hocko --- mm/mmap.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) Changes since v1 - restore tlb_flush_mmu after unmap_vmas since v2 - dropped rcu_barrier from the oom_reaper path as there might be an obscure case when RCU grace period might allocate https://lore.kernel.org/all/ed6c5f64-7afe-45a2-8d18-e4e1c0052840@paulmck-laptop/T/#u - set MMF_OOM_SKIP unconditionally in exit_mmap path - reworded the changelog to better describe the actual problem and explain the coordination and synchronization between exit, process_mrelease and oom_reaper diff --git a/mm/mmap.c b/mm/mmap.c index 4cb18b217048..eeb39eaeb23a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1314,19 +1314,11 @@ void exit_mmap(struct mm_struct *mm) /* Use ULONG_MAX here to ensure all VMAs in the mm are unmapped */ unmap_vmas(&tlb, &unmap); - /* - * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM - * killer will only skip this mm if it's already freed all it can. - */ + /* Free batched pages during unmap_vmas ASAP for oom victims */ if (mm_is_oom_victim(mm)) tlb_flush_mmu(&tlb); mmap_read_unlock(mm); - /* - * Set MMF_OOM_SKIP to hide this task from the oom killer/reaper - * because the memory has been already unmapped. - */ - mm_flags_set(MMF_OOM_SKIP, mm); mmap_write_lock(mm); unmap.mm_wr_locked = true; mt_clear_in_rcu(&mm->mm_mt); @@ -1334,6 +1326,14 @@ void exit_mmap(struct mm_struct *mm) free_pgtables(&tlb, &unmap); tlb_finish_mmu(&tlb); + /* + * Free the memory before marking MMF_OOM_SKIP so a re-triggered OOM + * killer will only skip this mm if it's already freed all it can. + */ + if (mm_is_oom_victim(mm)) + rcu_barrier(); + mm_flags_set(MMF_OOM_SKIP, mm); + /* * Walk the list again, actually closing and freeing it, with preemption * enabled, without holding any MM locks besides the unreachable -- 2.56.1