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 4FF7CC54E5D for ; Tue, 12 Mar 2024 22:12:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB6636B0074; Tue, 12 Mar 2024 18:12:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A3F3B6B007E; Tue, 12 Mar 2024 18:12:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8919A6B007B; Tue, 12 Mar 2024 18:12:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 72C486B0103 for ; Tue, 12 Mar 2024 18:12:04 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 534401604E9 for ; Tue, 12 Mar 2024 22:12:04 +0000 (UTC) X-FDA: 81889785768.26.F193260 Received: from mail-il1-f173.google.com (mail-il1-f173.google.com [209.85.166.173]) by imf06.hostedemail.com (Postfix) with ESMTP id A5334180010 for ; Tue, 12 Mar 2024 22:12:01 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hJcogAY0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of yuzhao@google.com designates 209.85.166.173 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710281521; 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=aHhknlqvNfTdhS7268rU8Y8hdQqP/mq9n0R8UEetUI8=; b=ka26ucALcKMumxTWJCRLPkTRJkc8kwZuOcGdqfkM1mnGQ4i2G/u1xhXO5y3PKhxMUycX4v +B+bcUEAG+9nsgMYUHFxPTRv9ANoFdvjaCBZBJ/7QzWBm1ZGcArZMUVEbuye70wJUS4wj7 aNg6Gi5s1j3dFOg1mw0r4SHp7daxHg4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=hJcogAY0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of yuzhao@google.com designates 209.85.166.173 as permitted sender) smtp.mailfrom=yuzhao@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710281521; a=rsa-sha256; cv=none; b=Q79I6XriZ68iNRV1rmu9uLgGtT6R86+7LtLzKFpM+QXeHyDlg7MMj72/qwX+Jk3mFL63Uo jxUfwtrRk/XGmW/y+rfX/NxPCBOad4txpcIIYD1B4cuvMkJMwNZp3bO1Wm+x5PJxnDyX9G b3qqSEWb3KsPkvINX+/18YQccNR5Jzk= Received: by mail-il1-f173.google.com with SMTP id e9e14a558f8ab-36645c1169cso43865ab.0 for ; Tue, 12 Mar 2024 15:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710281521; x=1710886321; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=aHhknlqvNfTdhS7268rU8Y8hdQqP/mq9n0R8UEetUI8=; b=hJcogAY0DgE1+3hORBGsHLX+EmRrbBwGU3XNZYNrE3HfgBGIOHjlqRYiIQj3H/FPCm n/3L1V1E5KbbE5HCQv7JpKc/r0hphBlmUv02sKQ9M2OybEmqiZ9e+PJcP3tymrTPRqwf mdQ/78H2AbxMDXzul53VQFDCGPT7Wct3mAV/doq9wygC1FvAL9Hio9Cr2dG3mEFLkgv0 a/K/nGBXcYcTyptYy0kLLstttB4csRFy3ov8XP1L13ookMRkOZK4dLI0Or+/N40arzcx Hz/SUbPXf9qIB/scdScLmQXpwVL1JiLnv12edG2MxOcpG87NXdBpS0rAMNrA+R3aYxp1 +kcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710281521; x=1710886321; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aHhknlqvNfTdhS7268rU8Y8hdQqP/mq9n0R8UEetUI8=; b=hc2VU5VykHrHcrx+Fb0NuEEGtDOn+mXlh7qEJnYINN4wY2EkDmEL+/TU408hR4qj+p k8PLHcWtf98YNUK0Bo5r/jPHsFRFWlEhFzUZe6z4MdxXMRcdec4FmvS+CenWi+svGEgX QCdHsINZRygErrhd4eUojDkuEseG3GkcOBTG7JYQtq8N3XQNYPuuEGSSC0eCJbuJ07oV mtUmDba5UxNzrOicDXRzGbXFMlQ+LbvhYGNnQ4ehQDXlfIImc4RteZfjBLZnrgZw9tcr Dy1GyKfU1pfylnW/n3HOMX3yBX4c43epofOy6mCoSyTQdUjwIBcfjxpLa2S2Z4Ot5rrP ggbQ== X-Forwarded-Encrypted: i=1; AJvYcCUolIcBmgsnQsTXkLUU4I0V/yxfaOeZqfBz0ozAzj54plaoEMjup3ktH2zoL+3ia2lpf5+du2V0dh8VhLTa6nUuyD0= X-Gm-Message-State: AOJu0Yx+SCEnh9iZFX1TqF9iPhph1wMVGiT9QQh4d1cCkbCTgIAHz7+w D8Zi+YXjBLUMHEX3FkHdmkGe33ceiDxwwEPfUsmGAjjf0w+ns40YnA02A0S9tA== X-Google-Smtp-Source: AGHT+IGQ/ZlKlS1lCQethqtslXn37Rb1npiCTAGmypdYNYmQ/gfgoKMfjvGdrTzQRF2gWpV2EuFTqg== X-Received: by 2002:a05:6e02:1cab:b0:363:e69d:896a with SMTP id x11-20020a056e021cab00b00363e69d896amr54805ill.18.1710281520620; Tue, 12 Mar 2024 15:12:00 -0700 (PDT) Received: from google.com ([100.64.188.49]) by smtp.gmail.com with ESMTPSA id n10-20020a02a90a000000b00474d2a8e83fsm2548322jam.83.2024.03.12.15.11.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 15:12:00 -0700 (PDT) Date: Tue, 12 Mar 2024 16:11:55 -0600 From: Yu Zhao To: Yafang Shao Cc: Andrew Morton , linux-mm@kvack.org, stable@vger.kernel.org Subject: Re: [PATCH] mm: mglru: Fix soft lockup attributed to scanning folios Message-ID: References: <20240307031952.2123-1-laoar.shao@gmail.com> <20240307090618.50da28040e1263f8af39046f@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A5334180010 X-Stat-Signature: jtyjft6583zoryhpfj9bxc1pcuo6ym9j X-Rspam-User: X-HE-Tag: 1710281521-108404 X-HE-Meta: U2FsdGVkX19iO0lABHZO3zd7Idr8So2uuquZ96rOv/lxZu0zDQLJSi7j6Hb4lPempU9dnwkGFWJw9S8mggK6gXu4AYZRE0oLKspsBYIwCVJFWW7z553LppOPbTes8w1ppCE9l0ap2t1g9xr69fdF4H5qreyIvezOdAWMXhekHC+DVV5bgKWXRHWaTuqjezF8shrGSvoNK72M91shY+ghbvlVS5Kh1oX+uy8A+YF4RHO/9XUT9ae+bJqXUqTYrAO5bgqjFob0RdUwLuc2MZR14lOvpLZdHbTE+uUmEfpbZATwkEV0DpeGJEEoCtycNZHtESmnvxK/wbUwR+2hNozn4VZYS9GDVR1ow21qb1zYfInCZQnXAjUZyLC9a9nHOUmG5+2u4+VMYTZQbcBni+Ds+7nhfWaFSwi3G7tPvC+70pLM+XrK147bsx5V8WMGbYm8WPycdKon2zHR1ul82eL7xlNx52rdHXfm6TnLC17Sx/UNAD8smuhg5esRoTxdZ3hJwiG+sddoLdIN1z1hrfkPhC1KirUXw6JsTceNdZHcR12as29bEyQDn6lvZzXfM2mLF0J0GRhaGEoJPk6/UQS4P84II2ddK1PV9mmEYAJfJVVsC7wCkszcJcz8ur2yk1MwQKujNg+cNbbq8SBrgGvnxksiJqfxihISQbkEJXw/YBr+8tvUm2kcankEp/0bYYMUIWDzcXxzFi6XishDW12dtD75WEVNbRgSW/65SnGQsmfVD6lI3wLphfKMrpbLO6vB0zUTSZUhFV0rAjSQ6OSMys7UjtAKx6FImsElB7Z56UoG8CUsCZdsBNl/u+13jh8zXszlFRzhjNv8pciV/5pe9AD7IJXZ3qTZLOjHiUGyZZ7Lq60fdxPw9yRYKkCcIjkExsLEVd53DD01NgQdhhjo1qlZhwfomDoqPsghK/VZTI17J++Y1d4be/lXroMlrNg1tq0xl0uSJf4ACgBvj3H XjcilLDs oJpuCXrXRR33AOoEnjnnYDrUY3GLXAQ5fM8FHf4LugdyDhCAza0uvPQjiJYY6N2/11mesmHJwPRiJolJKt2PWY/CUcvywhxECPJcD+SWlqmniWArzeIKwv7YBesaQofUrYmGRTgF1Djuu38GXGjZZiaSMVQ== 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: List-Subscribe: List-Unsubscribe: On Tue, Mar 12, 2024 at 02:29:48PM -0600, Yu Zhao wrote: > On Fri, Mar 08, 2024 at 04:57:08PM +0800, Yafang Shao wrote: > > On Fri, Mar 8, 2024 at 1:06 AM Andrew Morton wrote: > > > > > > On Thu, 7 Mar 2024 11:19:52 +0800 Yafang Shao wrote: > > > > > > > After we enabled mglru on our 384C1536GB production servers, we > > > > encountered frequent soft lockups attributed to scanning folios. > > > > > > > > The soft lockup as follows, > > > > > > > > ... > > > > > > > > There were a total of 22 tasks waiting for this spinlock > > > > (RDI: ffff99d2b6ff9050): > > > > > > > > crash> foreach RU bt | grep -B 8 queued_spin_lock_slowpath | grep "RDI: ffff99d2b6ff9050" | wc -l > > > > 22 > > > > > > If we're holding the lock for this long then there's a possibility of > > > getting hit by the NMI watchdog also. > > > > The NMI watchdog is disabled as these servers are KVM guest. > > > > kernel.nmi_watchdog = 0 > > kernel.soft_watchdog = 1 > > > > > > > > > Additionally, two other threads were also engaged in scanning folios, one > > > > with 19 waiters and the other with 15 waiters. > > > > > > > > To address this issue under heavy reclaim conditions, we introduced a > > > > hotfix version of the fix, incorporating cond_resched() in scan_folios(). > > > > Following the application of this hotfix to our servers, the soft lockup > > > > issue ceased. > > > > > > > > ... > > > > > > > > --- a/mm/vmscan.c > > > > +++ b/mm/vmscan.c > > > > @@ -4367,6 +4367,10 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, > > > > > > > > if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH) > > > > break; > > > > + > > > > + spin_unlock_irq(&lruvec->lru_lock); > > > > + cond_resched(); > > > > + spin_lock_irq(&lruvec->lru_lock); > > > > } > > > > > > Presumably wrapping this with `if (need_resched())' will save some work. > > > > good suggestion. > > > > > > > > This lock is held for a reason. I'd like to see an analysis of why > > > this change is safe. > > > > I believe the key point here is whether we can reduce the scope of > > this lock from: > > > > evict_folios > > spin_lock_irq(&lruvec->lru_lock); > > scanned = isolate_folios(lruvec, sc, swappiness, &type, &list); > > scanned += try_to_inc_min_seq(lruvec, swappiness); > > if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS) > > scanned = 0; > > spin_unlock_irq(&lruvec->lru_lock); > > > > to: > > > > evict_folios > > spin_lock_irq(&lruvec->lru_lock); > > scanned = isolate_folios(lruvec, sc, swappiness, &type, &list); > > spin_unlock_irq(&lruvec->lru_lock); > > > > spin_lock_irq(&lruvec->lru_lock); > > scanned += try_to_inc_min_seq(lruvec, swappiness); > > if (get_nr_gens(lruvec, !swappiness) == MIN_NR_GENS) > > scanned = 0; > > spin_unlock_irq(&lruvec->lru_lock); > > > > In isolate_folios(), it merely utilizes the min_seq to retrieve the > > generation without modifying it. If multiple tasks are running > > evict_folios() concurrently, it seems inconsequential whether min_seq > > is incremented by one task or another. I'd appreciate Yu's > > confirmation on this matter. > > Hi Yafang, > > Thanks for the patch! > > Yes, your second analysis is correct -- we can't just drop the lock > as the original patch does because min_seq can be updated in the mean > time. If this happens, the gen value becomes invalid, since it's based > on the expired min_seq: > > sort_folio() > { > .. > gen = lru_gen_from_seq(lrugen->min_seq[type]); > .. > } > > The following might be a better approach (untested): > > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 4255619a1a31..6fe53cfa8ef8 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -4365,7 +4365,8 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, > skipped_zone += delta; > } > > - if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH) > + if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH || > + spin_is_contended(&lruvec->lru_lock)) > break; > } > > @@ -4375,7 +4376,8 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, > skipped += skipped_zone; > } > > - if (!remaining || isolated >= MIN_LRU_BATCH) > + if (!remaining || isolated >= MIN_LRU_BATCH || > + (scanned && spin_is_contended(&lruvec->lru_lock))) > break; > } A better way might be: diff --git a/mm/vmscan.c b/mm/vmscan.c index 4255619a1a31..ac59f064c4e1 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4367,6 +4367,11 @@ static int scan_folios(struct lruvec *lruvec, struct scan_control *sc, if (!--remaining || max(isolated, skipped_zone) >= MIN_LRU_BATCH) break; + + if (need_resched() || spin_is_contended(&lruvec->lru_lock)) { + remaining = 0; + break; + } } if (skipped_zone) {