Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1324796rwb; Thu, 19 Jan 2023 09:12:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXuHRQJZARXdajoCu61AgQx0zbV+4SMqnRxjX4DyuYQrdN5yAZHnz41TSluWsiJodtq2uGrL X-Received: by 2002:a17:907:88cd:b0:86e:4067:b6a1 with SMTP id rq13-20020a17090788cd00b0086e4067b6a1mr13927399ejc.10.1674148324433; Thu, 19 Jan 2023 09:12:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674148324; cv=none; d=google.com; s=arc-20160816; b=a6njj2GFjsrCS4pSGuyMfFvCUYtT152RcE13PERb+sTxUyUZSdwmfbXl535O5MslcU RknWqZpIooJ9mehtaOoM6QJ1Sy87DdnXHR11nGZYaa0RBCtOhSbMooVJZrBwaQrFzNPe DcLWe0qf6k2rfGWz967ItPVlRmWkkJDehITrLnfK4t0OtKERj8u3Nrxl1LgU/SgXYGzX dzZg2FHft8gZVNih0Bkd3kVgf20CzFtvxpe6q04AYPeShC1jiNxk5Hp36g7qZwfjyljN Bfu2ojiDxiR3zUYNBsbzk4vtm7GsMkHqgZx0xLS1qTl71nw7o3eMPDX5VK6VK5y0DOUL QA4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=+GDzdecZ9ZHDc+Kqg/2GlkFErLVEdDgNL8hJqFOSpVo=; b=u19iG+IGNxNcckfirjGbzl4p+ghx7PV7igWfD1OjzfaX6hKL8/I0st8TdvaX5EikaF OwDGCtSCdaQw+0s+/5FnYK2kpfheU7C99c6ErjnLYbK5wHqgTesum00r38nkHG3U1Sud YRDl8oUoLzTyJD/AjtMgA3eD6S8Y+JdpkENIJBiG+yWOJacIBo7eFxYCxBC+Y77msJlJ dS/1M9VIHVXzyZoHhBMHV0bb1C3cedYJYhN1OMT9B6wl8y1h+uoVcF+XeM98O2hHuwLm Z+hw/UL04wYI18IJeTDU7Lqc9tkwA+a5CtUNilasRZzdYv5nND/c2IaLLIyzar8JA46B JtMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Om2kEGhG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wy10-20020a170906fe0a00b0084d34979425si17599053ejb.324.2023.01.19.09.11.53; Thu, 19 Jan 2023 09:12:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Om2kEGhG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbjASQ6j (ORCPT + 45 others); Thu, 19 Jan 2023 11:58:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230434AbjASQ6a (ORCPT ); Thu, 19 Jan 2023 11:58:30 -0500 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCD152685 for ; Thu, 19 Jan 2023 08:58:25 -0800 (PST) Received: by mail-wm1-x331.google.com with SMTP id fl11-20020a05600c0b8b00b003daf72fc844so4031282wmb.0 for ; Thu, 19 Jan 2023 08:58:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=+GDzdecZ9ZHDc+Kqg/2GlkFErLVEdDgNL8hJqFOSpVo=; b=Om2kEGhGW2ZCtcCSpMJCYtO5UQeiNUUPVdu5gkEhK+PDChIW0NBjnWrJ+6hlpJfFVn sBvlqYKhckqsfgZqPoX86JrUySCGMawHad5N0nzcHqJXcdSTPGXMo1sDbsM7TeotF3Vw I/R4ocVXoOQ5LpiF794sUYXdcsWjZRJzEI05FWSt48/E3botvql4UaLy4F2YIMI0KGWH +s2mmVbdTNtSSUaIyrZFbkdm3/4a0VRM6X1A5XQY2foNkb3YP9juV6fvbmdv0DzwT1iC GkrOSDHJVBy2AzyoXa5ZbkAVKRirCtcOONlmYeFSAFvy4ifz224cZVy21Z+zWhZyCNV7 jYYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+GDzdecZ9ZHDc+Kqg/2GlkFErLVEdDgNL8hJqFOSpVo=; b=ZixE2Qa2m9XHI6xp0hCrgUJvaUXpd7e2gKaICQmRYZEm/fB31EzRgdGV2UW7Przc2s diYCz5evbnZ4Zo5GlFtsDlyqsPp/Z7wJYk13XTAbeoMhP+EEt4WyMxHkNrF9N4RrRwp0 b0EE1IsDn+ugbyUdf7Rk9HRFXmUvaxr5SKhO0f8mhoycXPswU3WIK1A5H2W5X4NCYhGy UaFpEwkG+HbvTadUaHJPf5lLkT63CCy6mHwDaCZlCWbK6qEco10UKURu+cAtjFNP8b7z QVBWTWj92I7GsC3D/VI8y99udGlJpQKZ/0QrhmfPySo5bt8EmPg3ef6jXsccMmUFKcwp hydg== X-Gm-Message-State: AFqh2kqhw585UJdK2ken+T87mf8oLTOeOB4xP0WJfewYu+yrMzHQw2WM T1xnOH+WK3lftoiohK1SMiB9YLoBLOurjzN8CJwD+w== X-Received: by 2002:a05:600c:ad5:b0:3da:1b37:8ff5 with SMTP id c21-20020a05600c0ad500b003da1b378ff5mr658748wmr.166.1674147504173; Thu, 19 Jan 2023 08:58:24 -0800 (PST) MIME-Version: 1.0 References: <06423461-c543-56fe-cc63-cabda6871104@redhat.com> <6548b3b3-30c9-8f64-7d28-8a434e0a0b80@redhat.com> In-Reply-To: From: James Houghton Date: Thu, 19 Jan 2023 08:57:47 -0800 Message-ID: Subject: Re: [PATCH 21/46] hugetlb: use struct hugetlb_pte for walk_hugetlb_range To: Mike Kravetz Cc: Peter Xu , David Hildenbrand , Muchun Song , David Rientjes , Axel Rasmussen , Mina Almasry , "Zach O'Keefe" , Manish Mishra , Naoya Horiguchi , "Dr . David Alan Gilbert" , "Matthew Wilcox (Oracle)" , Vlastimil Babka , Baolin Wang , Miaohe Lin , Yang Shi , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > > > > I wonder if the following crazy idea has already been discussed: treat the > > > > whole mapping as a single large logical mapping. One reference and one > > > > mapping, no matter how the individual parts are mapped into the assigned > > > > page table sub-tree. > > > > > > > > Because for hugetlb with MAP_SHARED, we know that the complete assigned > > > > sub-tree of page tables can only map the given hugetlb page, no fragments of > > > > something else. That's very different to THP in private mappings ... > > > > > > > > So as soon as the first piece gets mapped, we increment refcount+mapcount. > > > > Other pieces in the same subtree don't do that. > > > > > > > > Once the last piece is unmapped (or simpler: once the complete subtree of > > > > page tables is gone), we decrement refcount+mapcount. Might require some > > > > brain power to do this tracking, but I wouldn't call it impossible right > > > > from the start. > > > > > > > > Would such a design violate other design aspects that are important? > > > > This is actually how mapcount was treated in HGM RFC v1 (though not > > refcount); it is doable for both [2]. > > My apologies for being late to the party :) > > When Peter first brought up the issue with ref/map_count overflows I was > thinking that we should use a scheme like David describes above. As > James points out, this was the approach taken in the first RFC. > > > One caveat here: if a page is unmapped in small pieces, it is > > difficult to know if the page is legitimately completely unmapped (we > > would have to check all the PTEs in the page table). > > Are we allowing unmapping of small (non-huge page sized) areas with HGM? > We must be if you are concerned with it. What API would cause this? > I just do not remember this discussion. There was some discussion about allowing MADV_DONTNEED on less-than-hugepage pieces [3] (it actually motivated the switch from UFFD_FEATURE_MINOR_HUGETLBFS_HGM to MADV_SPLIT). It isn't implemented in this series, but it could be implemented in the future. In the more immediate future, we want hwpoison to unmap at 4K, so MADV_HWPOISON would be a mechanism that userspace is granted to do this. > > > would have to check all the PTEs in the page table). In RFC v1, I > > sidestepped this caveat by saying that "page_mapcount() is incremented > > if the hstate-level PTE is present". A single unmap on the whole > > hugepage will clear the hstate-level PTE, thus decrementing the > > mapcount. > > > > On a related note, there still exists an (albeit minor) API difference > > vs. THPs: a piece of a page that is legitimately unmapped can still > > have a positive page_mapcount(). > > > > Given that this approach allows us to retain the hugetlb vmemmap > > optimization (and it wouldn't require a horrible amount of > > complexity), I prefer this approach over the THP-like approach. > > Me too. > > > > > > > The question is how to maintaining above information. > > > > > > It needs to be per-map (so one page mapped multiple times can be accounted > > > differently), and per-page (so one mapping/vma can contain multiple pages). > > > So far I think that's exactly the pgtable. If we can squeeze information > > > into the pgtable it'll work out, but definitely not trivial. Or we can > > > maintain seperate allocates for such information, but that can be extra > > > overheads too. > > > > I don't think we necessarily need to check the page table if we allow > > for the limitations stated above. > > > > When I was thinking about this I was a bit concerned about having enough > information to know exactly when to inc or dec counts. I was actually > worried about knowing to do the increment. I don't recall how it was > done in the first RFC, but from a high level it would need to be done > when the first hstate level PTE is allocated/added to the page table. > Right? My concern was with all the places where we could 'error out' > after allocating the PTE, but before initializing it. I was just thinking > that we might need to scan the page table or keep metadata for better > or easier accounting. The only two places where we can *create* a high-granularity page table are: __mcopy_atomic_hugetlb (UFFDIO_CONTINUE) and copy_hugetlb_page_range. RFC v1 did not properly deal with the cases where we error out. To correctly handle these cases, we basically have to do the pagecache lookup before touching the page table. 1. For __mcopy_atomic_hugetlb, we can lookup the page before doing the PT walk/alloc. If PT walk tells us to inc the page ref/mapcount, we do so immediately. We can easily pass the page into hugetlb_mcopy_atomic_pte() (via 'pagep') . 2. For copy_hugetlb_page_range() for VM_MAYSHARE, we can also do the lookup before we do the page table walk. I'm not sure how to support non-shared HGM mappings with this scheme (in this series, we also don't support non-shared; we return -EINVAL). NB: The only case where high-granularity mappings for !VM_MAYSHARE VMAs would come up is as a result of hwpoison. So we can avoid keeping additional metadata for what this series is trying to accomplish, but if the above isn't acceptable, then I/we can try to come up with a scheme that would be acceptable. There is also the possibility that the scheme implemented in this version of the series is acceptable (i.e., the page_mapcount() API difference, which results in slightly modified page migration behavior and smaps output, is ok... assuming we have the refcount overflow check). > > I think Peter mentioned it elsewhere, we should come up with a workable > scheme for HGM ref/map counting. This can be done somewhat independently. FWIW, what makes the most sense to me right now is to implement the THP-like scheme and mark HGM as mutually exclusive with the vmemmap optimization. We can later come up with a scheme that lets us retain compatibility. (Is that what you mean by "this can be done somewhat independently", Mike?) [3]: https://lore.kernel.org/linux-mm/20221021163703.3218176-1-jthoughton@google.com/T/#m9a1090108b61d32c04b68a1f3f2577644823a999 - James