Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1317577rbb; Mon, 26 Feb 2024 05:55:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX+SmzzEnkNtkwkbtxwoQEYEXdhcUBajLfFPIypsAQ+wwkBxkVFYyzbl0KqySyXVVmbPxW0p6ojCSCiN2zCeW2tTshYMVzbE5pi3zn/ZA== X-Google-Smtp-Source: AGHT+IH9u+HRcaUBww3cLWbIsLZBvSE8BjGXFcE2j4CjmDt6tzEO+SatEfNw41jOILWQMyHl+iRz X-Received: by 2002:a05:6a21:1518:b0:1a0:dda5:259 with SMTP id nq24-20020a056a21151800b001a0dda50259mr9840050pzb.0.1708955758949; Mon, 26 Feb 2024 05:55:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708955758; cv=pass; d=google.com; s=arc-20160816; b=mPMo959pg+qI50fWsg5owzLiQwvmU7NUZNPjA4+Zxn+yC2zdSxruzDSYN687Tl16Pf mAyUT3h/KneHQyNtsdGjGLe/FXNvnY3hnaPFz43F8YVnI3ReI8Wz5U7IXKPLnDfkcGc0 vEOdBxOFebHsX699Fw6G9y2X+IEIqA3EEEkO0zD/Cl0YdLXL0pCLbZoyCyFEia43YCNj +e7IHGPSVwnslvA52n9Pz9A8AjJV0MbOiKIsV1y/m90/uTJEIu1e1EZSmgfP+yrctOzN yZAzv4OrhmurhqsR+RKq/HWkCs6fnj/1Ye8tXbQrKT1hYSSkKbEakqch1lWQQz0+Ry+5 Bz6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=z3VNu3jbt2E2dPwyMwWXLjmnd7lyq2z8eAe3tEfU6WA=; fh=G/TqXNCqOO/UPWsWZr+eQPhRWHKtPe4zwqMX38k3ztc=; b=yeI2ggTw2q2H3VDCKq8PRXcWBfSfw372PBf9ErFs5tvJpSV8Atz21e/UEWSt0Jenwo FTOISP/szEQMcnox0TOE8lKgZ8aW86v54OwGivUN16v2D5ZVk/+CLAWk4IMDHwPdKF7u nkEmeOYB54C3kq39QFkQ8gJNdfDpIfLFFA1JJzmmWgeWtWZyoenfQTY6LUeJ+Bh6Rw0U 3/Q1Ut2B6dA+w8JFa0ipR2Sz1Vfak5r4juXEEMCtgPL8dH051j/+KlpxkylQBfAYW4UC j6kyBghqZtEO2hBtKsVUmIWDDed4ZDAZEdc4scpr4K1w7ODMu/KhSQeDU3q4gODZDnpv GWvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=M00khUpl; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-81529-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81529-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id k31-20020a635a5f000000b005d8c1d4553bsi3847766pgm.894.2024.02.26.05.55.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 05:55:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-81529-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=M00khUpl; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-81529-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-81529-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 9FBFD291EE5 for ; Mon, 26 Feb 2024 13:47:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A9E0A12AAE1; Mon, 26 Feb 2024 13:47:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="M00khUpl" Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 384CA12A146 for ; Mon, 26 Feb 2024 13:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708955234; cv=none; b=kA7R1rWYSaeDj0gv6eY5FKhB4pf5x1ssAhUNbADC7Tfu2fgYRhXCMA8yexlYkOviZqHWO6o+35UBfIHVsW6pjNLvpccwKf9jGUkzqSIkFUmgqDX8PDcEA/fYGWNQBBjOgTNGBpgCXt95SGcgQmURD1oQV9ZmWwthllzs+JCldjM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708955234; c=relaxed/simple; bh=YfrokfUsybNY74MtugqEcnTyIb7t+vGl7tW9pWrAewY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=dIdLV66ov8oPaDip2RQye4GuvZ0AmzlNimxMTH4gBnDIcdptuPq6N//p4U7CQSVy2Zi3QIM9EV+01MtH1JPbMPE31wQ+hERmiJClHSq0wwlUM60pjb2yWpBlfS2KiuY4ImjpexXED6qpqWtTzC4Nz1FpLJd2vslkmgt93p+P3n0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=M00khUpl; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-607e54b6cf5so20602347b3.0 for ; Mon, 26 Feb 2024 05:47:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708955232; x=1709560032; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=z3VNu3jbt2E2dPwyMwWXLjmnd7lyq2z8eAe3tEfU6WA=; b=M00khUplfsDX+0HrTs0wwASLK9eckclBujIbeZqxIYPV/HVRT/KhAk8s5QbWmM7oyi NDqzbC7rH5uC6WxF+pZPHLWouLPYIY4ZsWrTbA0KM/8vmxlWSpWPkDKhxb+ttVZA/+zF Nq4/REQ/mRjnHIHHfE57Jhwhu7tSPgVtREJ8qn4mpB1C2S1tCdP2pMUrTkfUt3UTKhCE vqKTz1dCWKiY1HZUiUVku9EEx6B5qHLqWKvEIY6/SqxytiB3O3m9Cr+L1x+Aamq9FJMj 1l0Hc/t+l/t8B+G7kmQLdk7A+c3b1f2UxSpfHnRsiIt0U90uMsb2k2xPr1mNzfbXRceb 9cUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708955232; x=1709560032; h=content-transfer-encoding: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=z3VNu3jbt2E2dPwyMwWXLjmnd7lyq2z8eAe3tEfU6WA=; b=VqqknBW/23XaP/nme4bpYxOfDzB2ShGC/8jijEO34NNnq3RkvmFVeR/OpGnrimSw39 NJMn1KYK3hmkqe6mA7KPYHSzwMowP2JcMPzZNd1/465IgR4OJjI/OTrYCADuzY4Os4yW 18iuFnJtILrC5pOnzdC/Kb5wCWBROks4p8TdkgTvetDXrFXmgfz5cGfEVEpFbRz9XGIw qi3iuAw37fx5VOjJX3XplWlDRyP5ji44lY31kSLqfYlGDDE6zhCDl+7S7RBiQbTDgjr9 R1Q+aYWKIoSaQ6i+FYhPJ1jO5sNpiWsTRzp7WR2vd0x7qCc59gWxfggZ2uI4bw9hroWJ 3QeQ== X-Forwarded-Encrypted: i=1; AJvYcCUj9B/qMVcr27kkabljXrFZ/ln72XLO8uJAxbayR9tLCDqP5tuRV8I1nuEh7VXWHgw+Xob5QSA48JM0UHNNoguagBm+Nhc9xKVkAAFq X-Gm-Message-State: AOJu0YwQvoqviV1oeYi+LJ0DA6ITGMRmoVTcyUweZfaiYO7W1z3u/R49 vN8SWMypc73cC9T0iSOV0VOwbHDmGdtkCA49h6U+pLPefniqm1eorYiQ3qf0C7P39P4GDys6vsr uZPx4TcoqzwN3WTe45G1L9dLP/x8= X-Received: by 2002:a0d:e813:0:b0:608:e551:d9e9 with SMTP id r19-20020a0de813000000b00608e551d9e9mr2190603ywe.16.1708955232261; Mon, 26 Feb 2024 05:47:12 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240226083526.26002-1-ioworker0@gmail.com> <8b909691-ca53-43b9-aab1-dba3ef3577cd@arm.com> <517e4c23-11f8-4ded-a502-354c482c4e51@redhat.com> In-Reply-To: <517e4c23-11f8-4ded-a502-354c482c4e51@redhat.com> From: Lance Yang Date: Mon, 26 Feb 2024 21:47:00 +0800 Message-ID: Subject: Re: [PATCH 1/1] mm/madvise: enhance lazyfreeing with mTHP in madvise_free To: David Hildenbrand Cc: Ryan Roberts , fengwei.yin@intel.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, minchan@kernel.org, peterx@redhat.com, shy828301@gmail.com, songmuchun@bytedance.com, wangkefeng.wang@huawei.com, zokeefe@google.com, 21cnbao@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 26, 2024 at 9:04=E2=80=AFPM David Hildenbrand wrote: > > On 26.02.24 13:57, Ryan Roberts wrote: > > On 26/02/2024 08:35, Lance Yang wrote: > >> Hey Fengwei, > >> > >> Thanks for taking time to review! > >> > >>> On Mon, Feb 26, 2024 at 10:38=E2=80=AFAM Yin Fengwei wrote: > >>>> On Sun, Feb 25, 2024 at 8:32=E2=80=AFPM Lance Yang wrote: > >> [...] > >>>> --- a/mm/madvise.c > >>>> +++ b/mm/madvise.c > >>>> @@ -676,11 +676,43 @@ static int madvise_free_pte_range(pmd_t *pmd, = unsigned long addr, > >>>> */ > >>>> if (folio_test_large(folio)) { > >>>> int err; > >>>> + unsigned long next_addr, align; > >>>> > >>>> - if (folio_estimated_sharers(folio) !=3D 1) > >>>> - break; > >>>> - if (!folio_trylock(folio)) > >>>> - break; > >>>> + if (folio_estimated_sharers(folio) !=3D 1 || > >>>> + !folio_trylock(folio)) > >>>> + goto skip_large_folio; > >>>> + > >>>> + align =3D folio_nr_pages(folio) * PAGE_SIZE; > >>>> + next_addr =3D ALIGN_DOWN(addr + align, align); > >>> There is a possible corner case: > >>> If there is a cow folio associated with this folio and the cow folio > >>> has smaller size than this folio for whatever reason, this change can= 't > >>> handle it correctly. > >> > >> Thanks for pointing that out; it's very helpful to me! > >> I made some changes. Could you please check if this corner case is now= resolved? > >> > >> As a diff against this patch. > >> > >> diff --git a/mm/madvise.c b/mm/madvise.c > >> index bcbf56595a2e..c7aacc9f9536 100644 > >> --- a/mm/madvise.c > >> +++ b/mm/madvise.c > >> @@ -686,10 +686,12 @@ static int madvise_free_pte_range(pmd_t *pmd, un= signed long addr, > >> next_addr =3D ALIGN_DOWN(addr + align, align); > >> > >> /* > >> - * If we mark only the subpages as lazyfree, > >> - * split the large folio. > >> + * If we mark only the subpages as lazyfree, or > >> + * if there is a cow folio associated with this f= olio, > >> + * then split the large folio. > >> */ > >> - if (next_addr > end || next_addr - addr !=3D alig= n) > >> + if (next_addr > end || next_addr - addr !=3D alig= n || > >> + folio_total_mapcount(folio) !=3D folio_nr_pag= es(folio)) > > > > I still don't think this is correct. I think you were previously assumi= ng that > > if you see a page from a large folio then the whole large folio should = be > > contiguously mapped? This new check doesn't validate that assumption re= liably; > > you need to iterate through every pte to generate a batch, like David d= oes in > > folio_pte_batch() for this to be safe. > > > > An example of when this check is insufficient; let's say you have a 4 p= age anon > > folio mapped contiguously in a process (total_mapcount=3D4). The proces= s is forked > > (total_mapcount=3D8). Then each process munmaps the second 2 pages > > (total_mapcount=3D4). In place of the munmapped 2 pages, 2 new pages ar= e mapped. > > Then call madvise. It's probably even easier to trigger for file-backed= memory > > (I think this code path is used for both file and anon?) > > What would work here is using folio_pte_batch() to get how many PTEs are > mapped *here*, then comparing the the batch size to folio_nr_pages(). If > both match, we are mapping all subpages. Thanks! I'll use folio_pte_batch() here in v2. Best, Lance > > -- > Cheers, > > David / dhildenb >