From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D57AC77B7F for ; Fri, 27 Jun 2025 07:15:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B2038D0013; Fri, 27 Jun 2025 03:15:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 963228D0006; Fri, 27 Jun 2025 03:15:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 878DF8D0013; Fri, 27 Jun 2025 03:15:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 77EBE8D0006 for ; Fri, 27 Jun 2025 03:15:55 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 10E011A05AC for ; Fri, 27 Jun 2025 07:15:55 +0000 (UTC) X-FDA: 83600321070.30.35D227F Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 220091A000E for ; Fri, 27 Jun 2025 07:15:52 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="b/S9FkV1"; spf=pass (imf19.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.172 as permitted sender) smtp.mailfrom=lance.yang@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751008553; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2YlWsvrV7BaACfyKv597+qNsvY3pISsv0aYbyAd91Ns=; b=dXkr+WQS2Fxo2gJQMWJEfC7J0YdLW06wbQCNHSQgp6A/u/yN2npOjEmeghRKZxExgMVEco YEr31dOegN2j55OknzmtIBqQCuTOmCfiIBfTVx0xmnvc/f8f9xKCNWo6omkljxKH9fFAgk NJWRIcL4O8cB1/+WSRglsL8bwIDRjEY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="b/S9FkV1"; spf=pass (imf19.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.172 as permitted sender) smtp.mailfrom=lance.yang@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751008553; a=rsa-sha256; cv=none; b=xLCXsihxWNLrQjuNE17aMgQXehWZFBa8TplC4sSfwmW+hjA2z1GOrWTQ4FEeVBvHVNmYCI 0NsHbHXEVLuUS1eD1aduTYpq27FuctMzeuhejF3HHWPt9Mfs1f24HvkUA34T1sif8ftTYN l9dAieS1PxCaBbKUzFwHo4UZjmYUXpk= Message-ID: <1d39b66e-4009-4143-a8fa-5d876bc1f7e7@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1751008550; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2YlWsvrV7BaACfyKv597+qNsvY3pISsv0aYbyAd91Ns=; b=b/S9FkV17dZyBU9gyIoqvCZ9lEWKMXK/izhpXtTf2yF3lzMVVnGX5YDWIEAMOyw+JsnWYy ZS/rlXdv+yGYpIGe7LtEkl5IvH7HC+w5BVIfUfUlYeVeQFfEQNv2+ilmf3FIPf6WpIp1B5 GZr8YjBl/kjDa13f8vVty5wzMBiXjgI= Date: Fri, 27 Jun 2025 15:15:41 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v2 1/1] mm/rmap: fix potential out-of-bounds page table access during batched unmap Content-Language: en-US To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, david@redhat.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, lorenzo.stoakes@oracle.com, ryan.roberts@arm.com, v-songbaohua@oppo.com, x86@kernel.org, huang.ying.caritas@gmail.com, zhengtangquan@oppo.com, riel@surriel.com, Liam.Howlett@oracle.com, vbabka@suse.cz, harry.yoo@oracle.com, mingzhe.yang@ly.com, stable@vger.kernel.org, Lance Yang References: <20250627062319.84936-1-lance.yang@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 220091A000E X-Stat-Signature: di6r49ppyrbk4fgw9gan6zh5t5iw9n38 X-Rspam-User: X-HE-Tag: 1751008552-261512 X-HE-Meta: U2FsdGVkX18PDs3cBvfzShjpttZ/p3Tma6WbpyUemJ/cdQ4+1LliIP2jE0J4s0s232oJoF88eTh3ZgL3cBVvvz5eCIhffOAdNx30e0k3CA3iHtnpukIt7q8W5NK8aHBVSYgodR4+ZbXwSqzjBhgKxylvdtM1ojbHSEHe+glD9mZcBfzo08LOOTEdS4Wm+Tj2FtufM4qVtC514yOyXO9PVqWWbcFvAdm0T6D12P6kZB9Ab6eQ+XGuy03ZMy3Qa/ZViefcEtqt/qqL8mHasNh5EPfzIIZ/oZ7Q/PeZHy3Aht3c3u9Mie4MWYQBqm/Vf2MWB7EZgX02gIB+lTThlCR2Dc38oTQ2zNBk2ydDiAxx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m@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2025/6/27 14:55, Barry Song wrote: > On Fri, Jun 27, 2025 at 6:52 PM Barry Song <21cnbao@gmail.com> wrote: >> >> On Fri, Jun 27, 2025 at 6:23 PM Lance Yang wrote: >>> >>> From: Lance Yang >>> >>> As pointed out by David[1], the batched unmap logic in try_to_unmap_one() >>> can read past the end of a PTE table if a large folio is mapped starting at >>> the last entry of that table. It would be quite rare in practice, as >>> MADV_FREE typically splits the large folio ;) >>> >>> So let's fix the potential out-of-bounds read by refactoring the logic into >>> a new helper, folio_unmap_pte_batch(). >>> >>> The new helper now correctly calculates the safe number of pages to scan by >>> limiting the operation to the boundaries of the current VMA and the PTE >>> table. >>> >>> In addition, the "all-or-nothing" batching restriction is removed to >>> support partial batches. The reference counting is also cleaned up to use >>> folio_put_refs(). >>> >>> [1] https://lore.kernel.org/linux-mm/a694398c-9f03-4737-81b9-7e49c857fcbe@redhat.com >>> >> >> What about ? >> >> As pointed out by David[1], the batched unmap logic in try_to_unmap_one() >> may read past the end of a PTE table when a large folio spans across two PMDs, >> particularly after being remapped with mremap(). This patch fixes the >> potential out-of-bounds access by capping the batch at vm_end and the PMD >> boundary. >> >> It also refactors the logic into a new helper, folio_unmap_pte_batch(), >> which supports batching between 1 and folio_nr_pages. This improves code >> clarity. Note that such cases are rare in practice, as MADV_FREE typically >> splits large folios. > > Sorry, I meant that MADV_FREE typically splits large folios if the specified > range doesn't cover the entire folio. Hmm... I got it wrong as well :( It's the partial coverage that triggers the split. how about this revised version: As pointed out by David[1], the batched unmap logic in try_to_unmap_one() may read past the end of a PTE table when a large folio spans across two PMDs, particularly after being remapped with mremap(). This patch fixes the potential out-of-bounds access by capping the batch at vm_end and the PMD boundary. It also refactors the logic into a new helper, folio_unmap_pte_batch(), which supports batching between 1 and folio_nr_pages. This improves code clarity. Note that such boundary-straddling cases are rare in practice, as MADV_FREE will typically split a large folio if the advice range does not cover the entire folio.