On Sat, Jul 31, 2021 at 10:22 PM Hugh Dickins <hughd@xxxxxxxxxx> wrote: > > On Fri, 30 Jul 2021, Yang Shi wrote: > > On Fri, Jul 30, 2021 at 12:42 AM Hugh Dickins <hughd@xxxxxxxxxx> wrote: > > > > > > Extend shmem_huge_enabled(vma) to shmem_is_huge(vma, inode, index), so > > > that a consistent set of checks can be applied, even when the inode is > > > accessed through read/write syscalls (with NULL vma) instead of mmaps > > > (the index argument is seldom of interest, but required by mount option > > > "huge=within_size"). Clean up and rearrange the checks a little. > > > > > > This then replaces the checks which shmem_fault() and shmem_getpage_gfp() > > > were making, and eliminates the SGP_HUGE and SGP_NOHUGE modes: while it's > > > still true that khugepaged's collapse_file() at that point wants a small > > > page, the race that might allocate it a huge page is too unlikely to be > > > worth optimizing against (we are there *because* there was at least one > > > small page in the way), and handled by a later PageTransCompound check. > > > > Yes, it seems too unlikely. But if it happens the PageTransCompound > > check may be not good enough since the page allocated by > > shmem_getpage() may be charged to wrong memcg (root memcg). And it > > won't be replaced by a newly allocated huge page so the wrong charge > > can't be undone. > > Good point on the memcg charge: I hadn't thought of that. Of course > it's not specific to SGP_CACHE versus SGP_NOHUGE (this patch), but I > admit that a huge mischarge is hugely worse than a small mischarge. The small page could be collapsed to a huge page sooner or later, so the mischarge may be transient. But huge page can't be replaced. > > We could fix it by making shmem_getpage_gfp() non-static, and pointing > to the vma (hence its mm, hence its memcg) here, couldn't we? Easily > done, but I don't really want to make shmem_getpage_gfp() public just > for this, for two reasons. > > One is that the huge race it just so unlikely; and a mischarge to root > is not the end of the world, so long as it's not reproducible. It can > only happen on the very first page of the huge extent, and the prior OK, if so the mischarge is not as bad as what I thought in the first place. > "Stop if extent has been truncated" check makes sure there was one > entry in the extent at that point: so the race with hole-punch can only > occur after we xas_unlock_irq(&xas) immediately before shmem_getpage() > looks up the page in the tree (and I say hole-punch not truncate, > because shmem_getpage()'s i_size check will reject when truncated). > I don't doubt that it could happen, but stand by not optimizing against. I agree the race is so unlikely and it may be not worth optimizing against it right now, but a note or a comment may be worth. > > Other reason is that doing shmem_getpage() (or shmem_getpage_gfp()) > there is unhealthy for unrelated reasons, that I cannot afford to get > into sending patches for at this time: but some of our users found the > worst-case latencies in collapse_file() intolerable - shmem_getpage() > may be reading in from swap, while the locked head of the huge page > being built is in the page cache keeping other users waiting. So, > I'd say there's something worse than memcg in that shmem_getpage(), > but fixing that cannot be a part of this series. Yeah, that is a different problem. > > > > > And, another question is it seems the newly allocated huge page will > > just be uncharged instead of being freed until > > "khugepaged_pages_to_scan" pages are scanned. The > > khugepaged_prealloc_page() is called to free the allocated huge page > > before each call to khugepaged_scan_mm_slot(). But > > khugepaged_scan_file() -> collapse_fille() -> khugepaged_alloc_page() > > may be called multiple times in the loop in khugepaged_scan_mm_slot(), > > so khugepaged_alloc_page() may see that page to trigger VM_BUG IIUC. > > > > The code is quite convoluted, I'm not sure whether I miss something or > > not. And this problem seems very hard to trigger in real life > > workload. > > Just to clarify, those two paragraphs are not about this patch, but about > what happens to mm/khugepaged.c's newly allocated huge page, when collapse > fails for any reason. > > Yes, the code is convoluted: that's because it takes very different paths > when CONFIG_NUMA=y (when it cannot predict which node to allocate from) > and when not NUMA (when it can allocate the huge page at a good unlocked > moment, and carry it forward from one attempt to the next). > > I don't like it at all, the two paths are confusing: sometimes I wonder > whether we should just remove the !CONFIG_NUMA path entirely; and other > times I wonder in the other direction, whether the CONFIG_NUMA=y path > ought to go the other way when it finds nr_node_ids is 1. Undecided. I'm supposed it is just performance consideration to keep the allocated huge page, but I'm not sure how much the difference would be if we remove it (remove the !CONFIG_NUMA path) because the pcp could cache THP now since Mel's patch 44042b449872 ("mm/page_alloc: allow high-order pages to be stored on the per-cpu lists"). It seems to provide the similar optimization but in the buddy allocator layer so that khugepaged doesn't have to maintain its own implementation. > > I'm confident that if you work through the two cases (thinking about > only one of them at once!), you'll find that the failure paths (not > to mention the successful paths) do actually work correctly without > leaking (well, maybe the !NUMA path can hold on to one huge page > indefinitely, I forget, but I wouldn't count that as leaking). IIUC the NUMA page could hold on to one huge page indefinitely. But I've never seen the BUG personally, so maybe you are right. > > Collapse failure is not uncommon and leaking huge pages gets noticed. > > Hugh