From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.0 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8CA5C432C3 for ; Mon, 25 Nov 2019 18:33:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 77ACE20679 for ; Mon, 25 Nov 2019 18:33:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=shutemov-name.20150623.gappssmtp.com header.i=@shutemov-name.20150623.gappssmtp.com header.b="15Bpu0X6" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 77ACE20679 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 108CD6B0274; Mon, 25 Nov 2019 13:33:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BAC36B0275; Mon, 25 Nov 2019 13:33:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEB8B6B0276; Mon, 25 Nov 2019 13:33:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id D57846B0274 for ; Mon, 25 Nov 2019 13:33:44 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 82A32824999B for ; Mon, 25 Nov 2019 18:33:44 +0000 (UTC) X-FDA: 76195648368.13.plant17_77e1121785131 X-HE-Tag: plant17_77e1121785131 X-Filterd-Recvd-Size: 6797 Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Mon, 25 Nov 2019 18:33:43 +0000 (UTC) Received: by mail-lj1-f194.google.com with SMTP id k8so6553977ljh.5 for ; Mon, 25 Nov 2019 10:33:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=kPjtxI/TrF4UnMikjlggsxMbTRpW7xiQMfu1Q1pQSto=; b=15Bpu0X6m1bgIwc9pGxLJZIxvfZTaxrG3jlKJDzx9i0BU/czx4OvuotVSORu/UJMHA 5xAzIKc+sBj6fQJq7T5O+9lHX1m2VhKwfkd0yD8vRmCSzrz2q3HitQYPUdT/0O5TqWNW lWITqzgChNWXPkzX5ejQHEnESq/ytb4CupHwGgWuqWuzEz2CIZfIbGGXxW49d02VEAaJ ajqTbNbaTb3Q7BjdqUbyjB12Qx9qfQUbA0u7ndYqDInYEz75ynlU29iGTUEonXISUMRf y1zDm0i3Wt4IK0Ndyr337xWNITXajNPS9NHKNamlJbrqEiUGGwadbe4Yf5ktuPmKK6iA XFpQ== 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:content-transfer-encoding :in-reply-to:user-agent; bh=kPjtxI/TrF4UnMikjlggsxMbTRpW7xiQMfu1Q1pQSto=; b=LgLiu8yndefLGzK4QkO2p84/tLUM7PS0gsKSV/Whitf833ebVwZkaDoDHvpMKG4MnE egaGY51Dsv/aUreLCunfmu41ZHoqD0tRpfqDJwv2ysaSuhsOreUdzDO4n351IwAwMkqr dVhM8Q8vrpNWSzbKWpGk+3HozHgvq1eOBGTRavsazl9qUkPQurv0xvPm08o+fZmJtSI+ X2FCd4bJ8oPxsNSz64U/uNaXsD8en3y4LJMcPTD7kyDP3pnwd6vvnuFxl1Qbr/NkR/kz dKgNyL16hdlZbKqzHNndA90Ei/Blh/CLWyFdI2a23gptbZKCHndh+d6GoKh5DBuBVYf6 pkow== X-Gm-Message-State: APjAAAXdr5+sISuyUH2uuMaET/v0bZzKWrHTodkjmlVP8Czmi0Whv8kD NVTAG+vZOzQuI2V4V1JM9ezABA== X-Google-Smtp-Source: APXvYqytq8uQHbgkfOudQHi3MYcOIVXdIxAHGRYx6WisuZYKhE/mnmwsQkgLfFth9cwc6u+RUlHkjw== X-Received: by 2002:a2e:9f4d:: with SMTP id v13mr23663505ljk.78.1574706822066; Mon, 25 Nov 2019 10:33:42 -0800 (PST) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id n19sm4019290lfl.85.2019.11.25.10.33.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Nov 2019 10:33:41 -0800 (PST) Received: by box.localdomain (Postfix, from userid 1000) id B23951032C4; Mon, 25 Nov 2019 21:33:50 +0300 (+03) Date: Mon, 25 Nov 2019 21:33:50 +0300 From: "Kirill A. Shutemov" To: Yang Shi Cc: hughd@google.com, kirill.shutemov@linux.intel.com, aarcange@redhat.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH] mm: shmem: allow split THP when truncating THP partially Message-ID: <20191125183350.5gmcln6t3ofszbsy@box> References: <1574471132-55639-1-git-send-email-yang.shi@linux.alibaba.com> <20191125093611.hlamtyo4hvefwibi@box> <3a35da3a-dff0-a8ca-8269-3018fff8f21b@linux.alibaba.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <3a35da3a-dff0-a8ca-8269-3018fff8f21b@linux.alibaba.com> User-Agent: NeoMutt/20180716 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Nov 25, 2019 at 10:24:38AM -0800, Yang Shi wrote: >=20 >=20 > On 11/25/19 1:36 AM, Kirill A. Shutemov wrote: > > On Sat, Nov 23, 2019 at 09:05:32AM +0800, Yang Shi wrote: > > > Currently when truncating shmem file, if the range is partial of TH= P > > > (start or end is in the middle of THP), the pages actually will jus= t get > > > cleared rather than being freed unless the range cover the whole TH= P. > > > Even though all the subpages are truncated (randomly or sequentiall= y), > > > the THP may still be kept in page cache. This might be fine for so= me > > > usecases which prefer preserving THP. > > >=20 > > > 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 subpa= ges are > > > unmapped, but shmem THP would still stay in page cache. > > >=20 > > > To protect the usecases which may prefer preserving THP, introduce = a > > > new fallocate mode: FALLOC_FL_SPLIT_HPAGE, which means spltting THP= is > > > preferred behavior if truncating partial THP. This mode just makes > > > sense to tmpfs for the time being. > > We need to clarify interaction with khugepaged. This implementation > > doesn't do anything to prevent khugepaged from collapsing the range b= ack > > to THP just after the split. >=20 > Yes, it doesn't. Will clarify this in the commit log. Okay, but I'm not sure that documention alone will be enough. We need proper design. > > > @@ -976,8 +1022,31 @@ static void shmem_undo_range(struct inode *in= ode, loff_t lstart, loff_t lend, > > > } > > > unlock_page(page); > > > } > > > +rescan_split: > > > pagevec_remove_exceptionals(&pvec); > > > pagevec_release(&pvec); > > > + > > > + if (split && PageTransCompound(page)) { > > > + /* The THP may get freed under us */ > > > + if (!get_page_unless_zero(compound_head(page))) > > > + goto rescan_out; > > > + > > > + lock_page(page); > > > + > > > + /* > > > + * The extra pins from page cache lookup have been > > > + * released by pagevec_release(). > > > + */ > > > + if (!split_huge_page(page)) { > > > + unlock_page(page); > > > + put_page(page); > > > + /* Re-look up page cache from current index */ > > > + goto again; > > > + } > > > + unlock_page(page); > > > + put_page(page); > > > + } > > > +rescan_out: > > > index++; > > > } > > Doing get_page_unless_zero() just after you've dropped the pin for th= e > > page looks very suboptimal. >=20 > If I don't drop the pins the THP can't be split. And, there might be mo= re > than one pins from find_get_entries() if I read the code correctly. For > example, truncate 8K length in the middle of THP, the THP's refcount wo= uld > get bumpped twice since=A0 two sub pages would be returned. Pin the page before pagevec_release() and avoid get_page_unless_zero(). Current code is buggy. You need to check that the page is still belong to the file after speculative lookup. --=20 Kirill A. Shutemov