Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2476915ybv; Fri, 21 Feb 2020 16:41:22 -0800 (PST) X-Google-Smtp-Source: APXvYqynxCobC4lJlSSLTzxl9tbIZH1/YDz2UoGJBdhQwALZGVDORmoNjrV+NB3nQYcTbMPDzK0+ X-Received: by 2002:aca:1704:: with SMTP id j4mr4124127oii.12.1582332082247; Fri, 21 Feb 2020 16:41:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582332082; cv=none; d=google.com; s=arc-20160816; b=irE6cDplHybrjXfvxfwYQg+1ZrPhma50BKQDdtepvlnPLrpb7aNIHpTHLV7l37kbd/ a6ffwAXbxONwEX3JVW6Ti8eIcHYVf6OALd0i+vwk0XNU2IoYBvbVcFIUuJycToenvVT5 /zb9Q+QQNcWm2tx+wuCwp8OnHca34ut06MjukCbFv3z/8bjdSiZJu2B9VJmiPGdHHyXS gEyFQ75PaYUU7e7G6blkJaDWiepaVGy/zOcYqKvMyLlasDvy72Vg/UxbV2SSmAxp0351 0J5bOqhvCJS1N2zxj26Ta4iqsiJSbED8dY5fy3Jv6kdlw0ZCeH4sWfn74l5KMAv/yWjt P48Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=HkzVUvb4J3yMIlLcArUIbWQ/BtybgRg77idRRt9Ey0I=; b=H0NpTE6uNnz2NkZJx3Ttkex7Byylsy8/KpcT60acSivG0j9ko0WyXiUJHQZIqusKpV V2zPSDfOqY64xScEZ2ksZZ0LdusenSE2secyn+riBXRWAGSTCWhE+umwWNgsniHTeF0v 7wWdrgVSDQMHlQBUFl9kNJIr+uHmqvfq7ISgkGEkAIhls52bWeS+JnO8NbUaPqiwgNYd OehPiJzlmXl1bl0g+G8ecWCuEgCzg/LBaKnnZw3NLNWUlT9hy3OCkb6SE1ciqM+Gto6d 73YGIqp3NJJo7j5e+V1VcU4fQS2QElYuhugHF2ubSxrZdR0VAoPM/jpastcGgBMpZa+j +7xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=F1fGCSkC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 24si1423543oiz.237.2020.02.21.16.41.10; Fri, 21 Feb 2020 16:41:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=F1fGCSkC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726842AbgBVAjm (ORCPT + 99 others); Fri, 21 Feb 2020 19:39:42 -0500 Received: from mail-io1-f65.google.com ([209.85.166.65]:46029 "EHLO mail-io1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726613AbgBVAjm (ORCPT ); Fri, 21 Feb 2020 19:39:42 -0500 Received: by mail-io1-f65.google.com with SMTP id i11so4275797ioi.12 for ; Fri, 21 Feb 2020 16:39:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=HkzVUvb4J3yMIlLcArUIbWQ/BtybgRg77idRRt9Ey0I=; b=F1fGCSkCdIyKeEo+oAV2qph81izXfYQYZFG3vZ0svm5XRYMOLXCxbc6SrtsKKX35gh AOcueM6f5QdKPqy7j/RzWkFHs0MshH/Xj6PeLcu3zSlzDmB/cxhwq+5vJPYJGmbTFnnk h/ug6mxJELKkMcSe3RaeOBz6KyPdv0487/ROuE/lSMI/Bcw+2JuZT4yR0bmx/OQ7iC7+ sZKDz36Oow+QQ1zUeMqya0iSbmgQ8I6QMtOrygYHuCQIvQnM2LWxg8QZu6wN5hy+xz62 mR4hLfENClVE8iTAgayXJHBSaT6tXaukg98zA/06xI/bEnBRpZRb7uDSvf+JB6oGU0Z4 Gz5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=HkzVUvb4J3yMIlLcArUIbWQ/BtybgRg77idRRt9Ey0I=; b=MFqqr1jSUC6wRiCKDYyR2hH0hk050ELvpNui3sMcszn07sLaIRv/lKQ2Np3X3kDWdy 27kgAncCeT8BH9z/CQFI38c7sI4CssNBBT6n1UqeOS4NWfuL4qaTlzGjzb3LDsCOUQYI MxR6kEEWiX4+dn6YSNJymTQXNKPW1Fba7d51xlaqUaoGuvKq7coxIEwsA4q6JvF84X7b +RY2c9yQPFi+Su6qHIFjodyBLB+uDOKqpjwcj49jLSfxvLRKdCTGy3/yn+vSdOq4gOzW oG76nVDyDunFSPrLSA6RMRNGYX+81Pa8Z8dcfWaByQ8PCz67Ecyvxysahvi9xezcaN0Q RGPw== X-Gm-Message-State: APjAAAVpZwNLnA4Seax1tBlaqvZsU2LZ6vbEEHES4FAkArS66hdLZ1nb jvKmVWMqCQeUnOy2Ph/4flKvuJc4Hyu3HG3xa4g= X-Received: by 2002:a05:6638:3f9:: with SMTP id s25mr34883973jaq.83.1582331980873; Fri, 21 Feb 2020 16:39:40 -0800 (PST) MIME-Version: 1.0 References: <1575420174-19171-1-git-send-email-yang.shi@linux.alibaba.com> <20200221040237-mutt-send-email-mst@kernel.org> In-Reply-To: From: Alexander Duyck Date: Fri, 21 Feb 2020 16:39:29 -0800 Message-ID: Subject: Re: [v2 PATCH] mm: shmem: allow split THP when truncating THP partially To: David Hildenbrand Cc: "Michael S. Tsirkin" , Yang Shi , Hugh Dickins , "Kirill A. Shutemov" , Andrea Arcangeli , Andrew Morton , linux-mm , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 21, 2020 at 1:36 AM David Hildenbrand wrote: > > On 21.02.20 10:07, Michael S. Tsirkin wrote: > > On Thu, Feb 20, 2020 at 10:16:54AM -0800, Alexander Duyck wrote: > >> On Tue, Dec 3, 2019 at 4:43 PM Yang Shi wrote: > >>> > >>> Currently when truncating shmem file, if the range is partial of THP > >>> (start or end is in the middle of THP), the pages actually will just get > >>> cleared rather than being freed unless the range cover the whole THP. > >>> Even though all the subpages are truncated (randomly or sequentially), > >>> the THP may still be kept in page cache. This might be fine for some > >>> usecases which prefer preserving THP. > >>> > >>> But, when doing balloon inflation in QEMU, QEMU actually does hole punch > >>> or MADV_DONTNEED in base page size granulairty if hugetlbfs is not used. > >>> So, when using shmem THP as memory backend QEMU inflation actually doesn't > >>> work as expected since it doesn't free memory. But, the inflation > >>> usecase really needs get the memory freed. Anonymous THP will not get > >>> freed right away too but it will be freed eventually when all subpages are > >>> unmapped, but shmem THP would still stay in page cache. > >>> > >>> Split THP right away when doing partial hole punch, and if split fails > >>> just clear the page so that read to the hole punched area would return > >>> zero. > >>> > >>> Cc: Hugh Dickins > >>> Cc: Kirill A. Shutemov > >>> Cc: Andrea Arcangeli > >>> Signed-off-by: Yang Shi > >> > >> One question I would have is if this is really the desired behavior we > >> are looking for? > >> > >> By proactively splitting the THP you are likely going to see a > >> performance regression with the virtio-balloon driver enabled in QEMU. > >> I would suspect the response to that would be to update the QEMU code > >> to identify the page size of the shared memory ramblock. At that > >> point I suspect it would start behaving the same as how it currently > >> handles anonymous memory, and the work done here would essentially > >> have been wasted other than triggering the desire to resolve this in > >> QEMU to avoid a performance regression. > >> > >> The code for inflating a the balloon in virtio-balloon in QEMU can be > >> found here: > >> https://github.com/qemu/qemu/blob/master/hw/virtio/virtio-balloon.c#L66 > >> > >> If there is a way for us to just populate the value obtained via > >> qemu_ram_pagesize with the THP page size instead of leaving it at 4K, > >> which is the size I am assuming it is at since you indicated that it > >> is just freeing the base page size, then we could address the same > >> issue and likely get the desired outcome of freeing the entire THP > >> page when it is no longer used. > >> > >> - Alex > > > > Well that would be racy right? It could be THP when you call > > the function, by the time you try to free it, it's already > > split up ... > > > > > > Two more points: > > > > 1. we can probably teach QEMU to always use the pbp > > machinery - will be helpful to reduce number of madvise calls too. > > The pbp machinery only works in the special case where the target page > size > 4k and the guest is nice enough to send the 4k chunks of a target > page sequentially. If the guest sends random pages, it is not of any use. Honestly I hadn't looked that close at the code. I had looked it over briefly when I was working on the page reporting logic and had decided against even bothering with it when I decided to use the scatterlist approach since I can simply ignore the pages that fall below the lowest order supported for the reporting. > > > > 2. Something we should do is teach balloon to > > inflate using address/length pairs instead of PFNs. > > This way we can pass a full THP in one go. > > The balloon works on 4k pages only. It is expected to break up THP and > harm performance. Or if that's not possible *do nothing*. Similar to > when balloon inflation is inhibited (e.g., vfio). Yes, but I think the point is that this is counter productive. If we can allocate something up to MAX_ORDER - 1 and hand that to the balloon driver instead then it would make the driver much more efficient. We could basically just work from the highest available order to the lowest and if that pushes us to the point of breaking up THP pages then at that point it would make sense. Us allocating the lower order pages first just makes it more difficult to go through and compact pages back up to higher order. The goal should really always be highest order to lowest order for inflation, and lowest to highest for deflation. That way we put pressure on the guest to compact its memory making it possible for us to squeeze it down even smaller and provide more THP pages for the rest of the system. > There was some work on huge page ballooning in a paper I read. But once > the guest is out of huge pages to report, it would want to fallback to > smaller granularity (down to 4k, to create real memory pressure), where > you would end up in the very same situation you are right now. So it's - > IMHO - only of limited used. I wouldn't think it would be that limited of a use case. By having the balloon inflate with higher order pages you should be able to put more pressure on the guest to compact the memory and reduce fragmentation instead of increasing it. If you have the balloon flushing out the lower order pages it is sitting on when there is pressure it seems like it would be more likely to reduce fragmentation further. > With what you suggest, you'll harm performance to reuse more memory. > IMHO, ballooning can be expected to harm performance. (after all, if you > inflate a 4k page in your guest, the guest won't be able to use a huge > page around that page anymore as well - until it compacts balloon > memory, resulting in new deflate/inflate steps). But I guess, it depends > on the use case ... I think it depends on how you are using the balloon. If you have the hypervisor only doing the MADV_DONTNEED on 2M pages, while letting it fill the balloon in the guest with everything down to 4K it might lead to enough memory churn to actually reduce the fragmentation as the lower order pages are inflated/deflated as we maintain memory pressure. It would probably be an interesting experiment if nothing else, and probably wouldn't take much more than a few tweaks to make use of inflation/deflation queues similar to what I did with the page reporting/hinting interface and a bit of logic to try allocating from highest order to lowest.