From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 99BF54418CC for ; Tue, 4 Aug 2026 23:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785887601; cv=none; b=uEz4bfR3whzTY1Ar5gbzKxYAJlrswEbM0nK6Ebw3N7uEcymhPBKMDWtut6jA7ofze3EYfKld8hbmL47KR5G+enEzSOC63oLJbWRqd9cUuEAmc8tOUWj1Ma+ECFGfLBMCO3+i4QUkXtRFJD6ozkMQkml3wpsS0ET01YSSHU421Zw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785887601; c=relaxed/simple; bh=1GzqBxHQaNlOzo9lhvcvRdSTkM4atr/ZBarsUmMvf1c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rvXRXkSiUbfoP0KkgfScK8EHXyiKQftrwdA8OUO5Xw8o7uZ1WSpxTIduJU5mFpqXpZU76z+/DGTP8SfWzhVmwXtI9KOHKfZkZfcFRbTNk81/8vJZoufIgAi8hVJlxEmqaQI9JAK26qBMCt1dIPbOL92iVOHjjUZ1PztdoQ4ahxk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iG8hn585; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iG8hn585" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98DFB1F00A3A; Tue, 4 Aug 2026 23:53:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785887600; bh=tX26UcNQcpwpUpKUI2NES9lwWKY+4sm0L85R3xkQmIs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=iG8hn585KZjy4uzOYteMLKfwvP9TDZPmFZcVX5eJFVwLKnl9TPstHf9Wm3UAVkn3c hj3up0ugG7+ZX8Lrmmzt33db/0s9/G91PdHu8Uz7F12GCF+k7Eb7dGoQwJ/xAKXXEi RBniR7/peOQTyLuoClZQPIZ5poNh5pivybeg6RroYjrbQaUN9x8WOh1kVqt+jwyjus tMRqvIjclZoK7Yn/n1JMXfXm2AQntM7g1q1wHV1LnaP/dxdsRKmKdPTwLP9VmhEaFh ymP+U+6defhJTs9+cCp6ZRZrHrN+J7zsM7Pa4aIxuwin16w/BFGX/jpxtyCK780CuH djm33OTIc3NMg== Date: Tue, 4 Aug 2026 23:53:18 +0000 From: Yosry Ahmed To: Brendan Jackman Cc: Borislav Petkov , Dave Hansen , Peter Zijlstra , Andrew Morton , David Hildenbrand , Vlastimil Babka , Mike Rapoport , Wei Xu , Johannes Weiner , Zi Yan , Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, Sumit Garg , Will Deacon , rientjes@google.com, patrick.roy@linux.dev, "Itazuri, Takahiro" , Andy Lutomirski , David Kaplan , Thomas Gleixner , Patrick Bellasi , Reiji Watanabe , Sean Christopherson Subject: Re: [PATCH v3 21/26] mm/page_alloc: implement FREETYPE_UNMAPPED allocations Message-ID: References: <20260726-page_alloc-unmapped-v3-0-6f5729aa9832@google.com> <20260726-page_alloc-unmapped-v3-21-6f5729aa9832@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260726-page_alloc-unmapped-v3-21-6f5729aa9832@google.com> > @@ -3400,6 +3426,127 @@ static inline void zone_statistics(struct zone *preferred_zone, struct zone *z, > #endif > } > > +#ifdef CONFIG_PAGE_ALLOC_UNMAPPED > +/* Try to allocate a page by mapping/unmapping a block from the direct map. */ > +static inline struct page * > +__rmqueue_direct_map(struct zone *zone, unsigned int request_order, > + unsigned int alloc_flags, freetype_t freetype) > +{ > + unsigned int ft_flags_other = freetype_flags(freetype) ^ FREETYPE_UNMAPPED; > + freetype_t ft_other = migrate_to_freetype(free_to_migratetype(freetype), > + ft_flags_other); > + bool want_mapped = !(freetype_flags(freetype) & FREETYPE_UNMAPPED); > + enum rmqueue_mode rmqm = RMQUEUE_NORMAL; > + unsigned long irq_flags; > + int nr_pageblocks, nr_freed; > + struct page *page; > + int alloc_order; > + int err; > + > + if (freetype_idx(ft_other) < 0) > + return NULL; > + > + /* > + * Might need a TLB shootdown. Even if IRQs are on this isn't > + * safe if the caller holds a lock (in case the other CPUs need that > + * lock to handle the shootdown IPI). > + */ > + if (alloc_flags & ALLOC_NOBLOCK) > + return NULL; > + > + if (!can_set_direct_map() || alloc_flags & ALLOC_NOLOCK) > + return NULL; > + > + lockdep_assert(!irqs_disabled() || unlikely(early_boot_irqs_disabled)); > + > + /* > + * Need to [un]map a whole pageblock (otherwise it might require > + * allocating pagetables). First allocate it. > + */ > + alloc_order = max(request_order, pageblock_order); > + nr_pageblocks = 1 << (alloc_order - pageblock_order); > + spin_lock_irqsave(&zone->lock, irq_flags); > + /* First try a block that already has the right migratetype. */ > + page = __rmqueue(zone, alloc_order, ft_other, alloc_flags, &rmqm); > + if (!page) { > + /* Fallback to changing a block's migratetype. */ > + rmqm = RMQUEUE_CLAIM; > + page = __rmqueue(zone, alloc_order, ft_other, alloc_flags, &rmqm); > + } > + spin_unlock_irqrestore(&zone->lock, irq_flags); > + if (!page) > + return NULL; IIUC we only try to change an entire pageblock here, but what if we can't? If memory is fragmented enough that many pageblocks have few unmapped pages in them, how do we serve a mapped allocation (e.g. a slab allocation)? We'll go into reclaim/compaction, but there's a chance we'll end up with unexpected allocation failures or OOM kills even though we have free memory, because unmapped memory is not movable or reclaimable (as of now, at least). The same could happen if many pageblocks have few mapped but unmovable pages in them, and we make an unmapped allocation. I wonder if we still need a fallback case where a pageblock contains a mix of mapped and unmapped pages. We need to carefully handle such pageblocks: - For unmapped allocations, we need to unmap the relevant PTEs and potentially do a TLB shootdown (if they were previously mapped). Maybe we should always flush the TLB for simplicity for now. - For mapped allocations, we need to map the relevant PTEs. No TLB shootdown should be needed. Assuming unmapped allocations are always zeroed by the users on alloc and free, we don't need to worry about zeroing pages either way. We may want to track the number of unmapped pages in such page blocks to now when it's fully mapped or fully unmapped and change its type, but maybe this can be a followup if needed.