Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9524109ybi; Wed, 10 Jul 2019 11:48:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqzhs+6v++1WN1jtlpISIV/ncdkcU2O+tdGZvgpFDiV9jyLuhCWIaQVMZB3rmo09SUPD8Vu/ X-Received: by 2002:a63:480a:: with SMTP id v10mr38799671pga.60.1562784539479; Wed, 10 Jul 2019 11:48:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562784539; cv=none; d=google.com; s=arc-20160816; b=c71rFPvZVRhjz+ofBSK/PpRDIINXDSjGOzduX5rG0ZvzvSYCPYJkdSVgA+AlyEoqWl MtO2cWBMHDgLN23fMdoetesbou2LKGXSJtXzmbna8sChElBaZ4SBeaj+l1/5TLl9lXb4 SgR8QfNKPZraXaTHnJjs4Wwn+88ficD9XjrnhJ7nw7WMUGFOKSJO649tUSM4+howdUiC X3qN42PTX9US3fyNvVPkzy8vhEhEmwCUBpeZ5/OHfTZXoniRoeRn2Siruhh8GkMjSAcp b9Fxa2EKmOr9qMU6mtPML9vDu2sAR16AedIkZ7SBDI7MAEhBU2IBmpcgQCNSwWCo1K6O w3oA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=y9kooNntoKsSjfBoyAT/Wl3OUMWao+tgpJsSDEV/3mY=; b=ASgwG+adRd1XtIcsq+fU5jtNV66BuPYyJMplmcFTV2Lzr1Ea4pquVAegAPVi+f73qS sLgXIFehVWzsjbmYknXoCoQe1bKURE6mtthx7E54BsJfXHtRajt90rJ5K8v9a0Bp8Ui5 acEBk4Jolx6jskBZfXnnmLQMvz5xMZM/2kwwvxgjpA4PWn1Y5si5tL2xc5STkbYatACC UofSYs1dtMzO90CkAmWfIBKKN/wfJwkukZQyit883VwbEl59gZAT1tM5gB8p4nXRFQUf FDl2iskTfLq7OrEh6pRwjRDVKovWDNvvGbdfMKXbuUpTLBSPsJOxL4JaP+JxEMxqJQa/ STdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b="OFjD/iLu"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t14si2742595pgm.395.2019.07.10.11.48.42; Wed, 10 Jul 2019 11:48:59 -0700 (PDT) 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=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b="OFjD/iLu"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727525AbfGJSsP (ORCPT + 99 others); Wed, 10 Jul 2019 14:48:15 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:37264 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726957AbfGJSsO (ORCPT ); Wed, 10 Jul 2019 14:48:14 -0400 Received: by mail-pg1-f195.google.com with SMTP id g15so1661434pgi.4 for ; Wed, 10 Jul 2019 11:48:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=y9kooNntoKsSjfBoyAT/Wl3OUMWao+tgpJsSDEV/3mY=; b=OFjD/iLuYrkg1RhYeN3VKM0/ELOCopxGEBIDDqvo5swD04JGSTNq3LyO5brgEAqGsz UALrJXenvXS3YoCtUXy1CBNOI/pwmTHCuw1BEj4n86Ll2AEYX2iZ+SfVRUjx5S15W7RH 33kEls+0jpJZbSW7GcqyjXCHCAByMD4WHssllukI4Py2WXt6L9Ad+H0i8h7E/WSQphKg RLLmcPbF1sWNvg8/UeJfoQwz3J9c8A9TsX8KPp/38Rp7oVTCobf/hHCqPtJqFMy7sNkW AYN0vfhtiynvE5ZxG7n4/uXDHiUgnIzwMO5st4VDEViLo5enzimobH+MdiHGRzTrhzD2 +bGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=y9kooNntoKsSjfBoyAT/Wl3OUMWao+tgpJsSDEV/3mY=; b=kJXWkrIvbcAfki2TaSKlpCW/NxOVG7DrP09P7sdbS33uKClPLZ5Gh0RhzBvseB5uZj FDpUT6tk1ruhY9QEQBzZrdE7QinqOMrUAg7AT8M8OYhL21JhSj3Ao+JrU0yUs9DuzEep Fe/SViyl2yFQhwwOC+0SnuuOtwRPZvW4Nfuky9iAOxSFnY/151PWHC9hCo+jQpRz6vmk dFA77DTpI9BVdRuTOYwutPjaUvAzR8mA1v/pepsCoQ9gknoiiCNEKODgDOsx5pFxnNXJ WwmEmvHA6DBrZ96ocab5HKvfLDIZ1/558py6YP9VTiWKUkhY6ODUbG0Gh/rjl3sfMV6l QwQQ== X-Gm-Message-State: APjAAAVsjMM48tXmHxUVmm9EQX3HyFc+CUVD4vns6qY+HkffoP9qmP3H zV2qgy+IbWJhnda+zQ3bCPzNVw== X-Received: by 2002:a63:4404:: with SMTP id r4mr38331577pga.245.1562784493365; Wed, 10 Jul 2019 11:48:13 -0700 (PDT) Received: from localhost ([2620:10d:c091:500::2:5b9d]) by smtp.gmail.com with ESMTPSA id k184sm2700588pgk.7.2019.07.10.11.48.12 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 11:48:12 -0700 (PDT) Date: Wed, 10 Jul 2019 14:48:11 -0400 From: Johannes Weiner To: Song Liu Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, matthew.wilcox@oracle.com, kirill.shutemov@linux.intel.com, kernel-team@fb.com, william.kucharski@oracle.com, akpm@linux-foundation.org, hdanton@sina.com Subject: Re: [PATCH v9 5/6] mm,thp: add read-only THP support for (non-shmem) FS Message-ID: <20190710184811.GF11197@cmpxchg.org> References: <20190625001246.685563-1-songliubraving@fb.com> <20190625001246.685563-6-songliubraving@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190625001246.685563-6-songliubraving@fb.com> User-Agent: Mutt/1.12.0 (2019-05-25) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 24, 2019 at 05:12:45PM -0700, Song Liu wrote: > This patch is (hopefully) the first step to enable THP for non-shmem > filesystems. > > This patch enables an application to put part of its text sections to THP > via madvise, for example: > > madvise((void *)0x600000, 0x200000, MADV_HUGEPAGE); > > We tried to reuse the logic for THP on tmpfs. > > Currently, write is not supported for non-shmem THP. khugepaged will only > process vma with VM_DENYWRITE. sys_mmap() ignores VM_DENYWRITE requests > (see ksys_mmap_pgoff). The only way to create vma with VM_DENYWRITE is > execve(). This requirement limits non-shmem THP to text sections. > > The next patch will handle writes, which would only happen when the all > the vmas with VM_DENYWRITE are unmapped. > > An EXPERIMENTAL config, READ_ONLY_THP_FOR_FS, is added to gate this > feature. > > Acked-by: Rik van Riel > Signed-off-by: Song Liu This is really cool, and less invasive than I anticipated. Nice work. I only have one concern and one question: > @@ -1392,6 +1401,29 @@ static void collapse_file(struct mm_struct *mm, > result = SCAN_FAIL; > goto xa_unlocked; > } > + } else if (!page || xa_is_value(page)) { > + xas_unlock_irq(&xas); > + page_cache_sync_readahead(mapping, &file->f_ra, file, > + index, PAGE_SIZE); > + /* drain pagevecs to help isolate_lru_page() */ > + lru_add_drain(); > + page = find_lock_page(mapping, index); > + if (unlikely(page == NULL)) { > + result = SCAN_FAIL; > + goto xa_unlocked; > + } > + } else if (!PageUptodate(page)) { > + VM_BUG_ON(is_shmem); > + xas_unlock_irq(&xas); > + wait_on_page_locked(page); > + if (!trylock_page(page)) { > + result = SCAN_PAGE_LOCK; > + goto xa_unlocked; > + } > + get_page(page); > + } else if (!is_shmem && PageDirty(page)) { > + result = SCAN_FAIL; > + goto xa_locked; > } else if (trylock_page(page)) { > get_page(page); > xas_unlock_irq(&xas); The many else ifs here check fairly complex page state and are hard to follow and verify mentally. In fact, it's a bit easier now in the patch when you see how it *used* to work with just shmem, but the end result is fragile from a maintenance POV. The shmem and file cases have little in common - basically only the trylock_page(). Can you please make one big 'if (is_shmem) {} {}' structure instead that keeps those two scenarios separate? > @@ -1426,6 +1458,12 @@ static void collapse_file(struct mm_struct *mm, > goto out_unlock; > } > > + if (page_has_private(page) && > + !try_to_release_page(page, GFP_KERNEL)) { > + result = SCAN_PAGE_HAS_PRIVATE; > + break; > + } > + > if (page_mapped(page)) > unmap_mapping_pages(mapping, index, 1, false); > @@ -1607,6 +1658,17 @@ static void khugepaged_scan_file(struct mm_struct *mm, > break; > } > > + if (page_has_private(page) && trylock_page(page)) { > + int ret; > + > + ret = try_to_release_page(page, GFP_KERNEL); > + unlock_page(page); > + if (!ret) { > + result = SCAN_PAGE_HAS_PRIVATE; > + break; > + } > + } > + > if (page_count(page) != 1 + page_mapcount(page)) { > result = SCAN_PAGE_COUNT; > break; There is already a try_to_release() inside the page lock section in collapse_file(). I'm assuming you added this one because private data affects the refcount. But it seems a bit overkill just for that; we could also still fail the check, in which case we'd have dropped the buffers in vain. Can you fix the check instead? There is an is_page_cache_freeable() function in vmscan.c that handles private fs references: static inline int is_page_cache_freeable(struct page *page) { /* * A freeable page cache page is referenced only by the caller * that isolated the page, the page cache and optional buffer * heads at page->private. */ int page_cache_pins = PageTransHuge(page) && PageSwapCache(page) ? HPAGE_PMD_NR : 1; return page_count(page) - page_has_private(page) == 1 + page_cache_pins; } Wouldn't this work here as well? The rest looks great to me.