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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D70E8C4345F for ; Fri, 12 Apr 2024 01:48:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B9D36B0092; Thu, 11 Apr 2024 21:48:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56A616B0093; Thu, 11 Apr 2024 21:48:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 431126B0095; Thu, 11 Apr 2024 21:48:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 259256B0092 for ; Thu, 11 Apr 2024 21:48:35 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DEF10C0D4F for ; Fri, 12 Apr 2024 01:48:34 +0000 (UTC) X-FDA: 81999195348.13.6E2245C Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) by imf05.hostedemail.com (Postfix) with ESMTP id 15C97100005 for ; Fri, 12 Apr 2024 01:48:32 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YgkIPQDy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712886513; a=rsa-sha256; cv=none; b=Hl3G6rtKUxR/Ts64tec1zOb55Jy/EhZdopOrKcsOOsN75dfpyfC1bkDGFIZfOTutpnaLqH fxfNX6h9NFkhdHsr7HA+aDBN690/a5TeRFnaTce8KFMuVseAHvp+kkkj+uNrGX8/zT9O/X PbPgOk+Skf3rBDaOYbaD2UF3f6pj7Zw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YgkIPQDy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf05.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.208.54 as permitted sender) smtp.mailfrom=ioworker0@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712886513; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lGCc2xE7kNa3p/vj3nCRy1hQloRnEiDZ2zpk6AwtsY4=; b=b21/1pZl17+lWyXjnPjFnDzm2RKVWrLWk1JompMxI90Gedvx2pUmzGDZFzKp8+L3uunBZa KbdU5eX42ASCmQkDu0YC6b8K9FenyN5ZlZ+Ravt/VPuwuksRmSo/uniLgf9HjWy9C3ctCh 0m9c59tWWbd+qVetVHdALmpHs+IQ+lo= Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-56e69888a36so485019a12.3 for ; Thu, 11 Apr 2024 18:48:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712886511; x=1713491311; darn=kvack.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=lGCc2xE7kNa3p/vj3nCRy1hQloRnEiDZ2zpk6AwtsY4=; b=YgkIPQDyIOKkASgyCr5rlTzSgx931EbBUNS00BPyKMJ/OaBK6vVtTNS6hZ9OqZelvw CmvglzYxfEehw1+a2hcYHtxZWjOPWO08AaklkdzZXtfOPK9zRvEetXKhWp4jm2RnyzOM hCYob374PVS37M3U7O1bV6YCdYFLMjVr4ekKuAcvoRrlSttrj/PMrGi5c2iWR2GA73Cb 7fCnNIl2jsF/6uclbJPMItSjvzjdpbjrCMqd/Mt4i1tAd8oy0bDfss2+iue0BaWuIYg1 /riuMXN7HIj1sYl7Dqb3CobEjoRc26k1dLPwPhgxdS1sklFXQoFs4j75ucyJcDKoWt9m dsqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712886511; x=1713491311; 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=lGCc2xE7kNa3p/vj3nCRy1hQloRnEiDZ2zpk6AwtsY4=; b=R9dEG6M+D1ryUj8TJxsNctl9812Snvu+kFr2C4VoUxLqaZzFeEkYmrC3G2w8c3U1hn twwBp9hj+SrR4ATUHHABih7PzdplQj2poTd8kt24DH8MwZEcbICpIe/pxxtWVeRml1ki YDwaB5WgwnzE/Pgjj78DR7m/0EHWlDe1k/DZolQ7swwqByaRIGc9Fc6vf4oNhUasxQJ8 IgXANnV+bHszeHCS8sWYGZqVqWAiRfXtK3DhRNxbt/CX8kSlassIFhUY7vogiCgff6D2 VBKLmQeNGW+tRrkeCOVg27Cw7UvxBsE+o4XDA8AE1hza92fFHzRjBexqMfoXaYxclPyn /6yg== X-Forwarded-Encrypted: i=1; AJvYcCVss9CcscF6ly/65tD/Ef3jw2o3LYlE6vu4FXsONq4GWZ1q/JVyk3pIDxdGhKhyW46ncgCOfwhgWAVqsHJgAfUdvAk= X-Gm-Message-State: AOJu0YxweHxs5XjBrIKtcveZPvz6ng4yu/jf20LYS7w6M44WI/fCMzjs cPppxAqUqvTJXu2R/gusdHSRegUh5vcecdCm+1ff/megTB7g1bVzujvUIagEmCMaaVyOpeVHX/D Nrj/7Uc42uSWoxLF9sMsamYvnZlg= X-Google-Smtp-Source: AGHT+IEkT0SlHP+XvdDvomqIEYCJ0NkqUuIeE4LOBoI0dZ2bGFUm17BCitiNyd+8IXaiol4X/fjni5n49yo6n4y0Ubs= X-Received: by 2002:a50:a411:0:b0:56f:e75b:83aa with SMTP id u17-20020a50a411000000b0056fe75b83aamr918869edb.6.1712886511248; Thu, 11 Apr 2024 18:48:31 -0700 (PDT) MIME-Version: 1.0 References: <20240408042437.10951-1-ioworker0@gmail.com> <20240408042437.10951-2-ioworker0@gmail.com> <38c4add8-53a2-49ca-9f1b-f62c2ee3e764@arm.com> <3cda8e87-7095-4aad-beb1-6a420912df34@arm.com> <8d674b15-ef74-4d96-bc27-8794f744517c@arm.com> In-Reply-To: <8d674b15-ef74-4d96-bc27-8794f744517c@arm.com> From: Lance Yang Date: Fri, 12 Apr 2024 09:48:19 +0800 Message-ID: Subject: Re: [PATCH v5 1/2] mm/madvise: optimize lazyfreeing with mTHP in madvise_free To: Ryan Roberts Cc: akpm@linux-foundation.org, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.com, xiehuan09@gmail.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 15C97100005 X-Stat-Signature: s9wxouxcgth5omf4em5pi5oieuszxgwi X-HE-Tag: 1712886512-865584 X-HE-Meta: U2FsdGVkX1+hPRxD43WabSTiz/H1GUNDF2wbA7z47tL7sCYRHbKFp7pyPT+4MM2e1VlTeEdb148RtK8UZFaLLaOmJyRwhpND4lBVccnzB/QxeHb2xbeBZ7B0Y7AVpqHxjcMJBsBw/58sqDbeMhWA1isYTsXTtm7r1SX7IExrSwFeEBKM5nrp+GRanYGV4IislbUkd7emVgj/uESytLm5w8vQxVx37zZl0vtpm32ZrmctqkQ5FHdHpXojprC46YM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Hg8l7bIibLSByqTag/J/h3VfgF0iLT3Rf9ZkjI+nQ0SAzdT+Dxi5DWtra7D3epBgz7Pl2wetFnFt0IQbjYaiW2NNxST18k7heFqp5UYgyx8VTXpnc/MUfwz9/TwBKV6NKdisIGTcPyFLSDWk5fG7Ukd0sICWkYSnO2fMaarFWGOZF+mqsSEGTiNMwti3Asw1Y/c X-Bogosity: Ham, tests=bogofilter, spamicity=0.005578, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Apr 11, 2024 at 10:39=E2=80=AFPM Ryan Roberts wrote: > > On 11/04/2024 15:07, Lance Yang wrote: > > On Thu, Apr 11, 2024 at 9:48=E2=80=AFPM Ryan Roberts wrote: > >> > >> [...] > >> > >>>>> + > >>>>> + if (!folio_trylock(folio)) > >>>>> + continue; > >>>> > >>>> This is still wrong. This should all be protected by the "if > >>>> (folio_test_swapcache(folio) || folio_test_dirty(folio))" as it was = previously > >>>> so that you only call folio_trylock() if that condition is true. You= are > >>>> unconditionally locking here, then unlocking, then relocking below i= f the > >>>> condition is met. Just put everything inside the condition and lock = once. > >>> > >>> I'm not sure if it's safe to call folio_mapcount() without holding th= e > >>> folio lock. > >>> > >>> As mentioned earlier by David in the v2[1] > >>>> What could work for large folios is making sure that #ptes that map = the > >>>> folio here correspond to the folio_mapcount(). And folio_mapcount() > >>>> should be called under folio lock, to avoid racing with swapout/migr= ation. > >>> > >>> [1] https://lore.kernel.org/all/5cc05529-eb80-410e-bc26-233b0ba0b21f@= redhat.com/ > >> > >> But I'm not suggesting that you should call folio_mapcount() without t= he lock. > >> I'm proposing this: > >> > >> if (folio_test_swapcache(folio) || folio_test_dirty(fo= lio)) { > >> if (!folio_trylock(folio)) > >> continue; > >> /* > >> - * If folio is shared with others, we mustn't = clear > >> - * the folio's dirty flag. > >> + * If we have a large folio at this point, we = know it is > >> + * fully mapped so if its mapcount is the same= as its > >> + * number of pages, it must be exclusive. > >> */ > >> - if (folio_mapcount(folio) !=3D 1) { > >> + if (folio_mapcount(folio) !=3D folio_nr_pages(= folio)) { > >> folio_unlock(folio); > >> continue; > >> } > > > > IIUC, if the folio is clean and not in the swapcache, we still need to > > compare the number of batched PTEs against folio_mapcount(). > > Why? That's not how the old code worked. In fact the comment says that th= e > reason for the exclusive check is to avoid marking a dirty *folio* as cle= an if > shared; that would be bad because we could throw away data that others re= lied > upon. It's perfectly safe to clear the dirty flag from the *pte* even if = it is > shared; the ptes are private to the process so that won't affect sharers. > > You should just follow the pattern already estabilished by the original c= ode. > The only difference is that because the folio is now (potentially) large,= you > have to change the way to detect exclusivity. Thanks a lot for your patience and help! My bad for the oversight and mistake :( I'll take another look at the original code and make adjustments following = the established pattern. Thanks, Lance > > > > > Thanks, > > Lance > > > >> > >> What am I missing? > >> >