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=-0.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS 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 BFBDFC4740A for ; Sat, 5 Oct 2019 19:35:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6A713222C0 for ; Sat, 5 Oct 2019 19:35:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="NXlIvr4a" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6A713222C0 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C4E366B0005; Sat, 5 Oct 2019 15:35:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BFDBA6B0006; Sat, 5 Oct 2019 15:35:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B13686B0007; Sat, 5 Oct 2019 15:35:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id 8A1346B0005 for ; Sat, 5 Oct 2019 15:35:26 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with SMTP id 0CFD2181AC9AE for ; Sat, 5 Oct 2019 19:35:26 +0000 (UTC) X-FDA: 76010735052.15.clock94_124b2c5d42c4c X-HE-Tag: clock94_124b2c5d42c4c X-Filterd-Recvd-Size: 3737 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Sat, 5 Oct 2019 19:35:25 +0000 (UTC) Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 150B4222BE; Sat, 5 Oct 2019 19:35:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570304124; bh=2AV4N+2t6XV2T7V3z+wVg4ZZIC+gZEAl81XxyuQYcl0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=NXlIvr4atLhu9hnsyz6hFcgQqApIB9vToxavFStoJDSwSKUDqhvpbalqg+14TTwo8 iX41QNQvzAD2vM+RhCgHsxCeIpcgoPDY7siAjctJWzLWlkQggvNx6h6kFwgQACOYGx cfH3LaTQaq+3wQ4WFBHseuNosoaIoVSE/29IrX70= Date: Sat, 5 Oct 2019 12:35:23 -0700 From: Andrew Morton To: Konstantin Khlebnikov Cc: linux-mm@kvack.org, Michal Hocko , linux-kernel@vger.kernel.org, Matthew Wilcox Subject: Re: [PATCH v2] mm/swap: piggyback lru_add_drain_all() calls Message-Id: <20191005123523.0db4ad1b9f268c419f8a59eb@linux-foundation.org> In-Reply-To: <157019456205.3142.3369423180908482020.stgit@buzz> References: <157019456205.3142.3369423180908482020.stgit@buzz> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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 Fri, 04 Oct 2019 16:09:22 +0300 Konstantin Khlebnikov wrote: > This is very slow operation. There is no reason to do it again if somebody > else already drained all per-cpu vectors while we waited for lock. > > Piggyback on drain started and finished while we waited for lock: > all pages pended at the time of our enter were drained from vectors. > > Callers like POSIX_FADV_DONTNEED retry their operations once after > draining per-cpu vectors when pages have unexpected references. > > ... > > --- a/mm/swap.c > +++ b/mm/swap.c > @@ -708,9 +708,10 @@ static void lru_add_drain_per_cpu(struct work_struct *dummy) > */ > void lru_add_drain_all(void) > { > + static seqcount_t seqcount = SEQCNT_ZERO(seqcount); > static DEFINE_MUTEX(lock); > static struct cpumask has_work; > - int cpu; > + int cpu, seq; > > /* > * Make sure nobody triggers this path before mm_percpu_wq is fully > @@ -719,7 +720,19 @@ void lru_add_drain_all(void) > if (WARN_ON(!mm_percpu_wq)) > return; > > + seq = raw_read_seqcount_latch(&seqcount); > + > mutex_lock(&lock); > + > + /* > + * Piggyback on drain started and finished while we waited for lock: > + * all pages pended at the time of our enter were drained from vectors. > + */ > + if (__read_seqcount_retry(&seqcount, seq)) > + goto done; > + > + raw_write_seqcount_latch(&seqcount); > + > cpumask_clear(&has_work); > > for_each_online_cpu(cpu) { > @@ -740,6 +753,7 @@ void lru_add_drain_all(void) > for_each_cpu(cpu, &has_work) > flush_work(&per_cpu(lru_add_drain_work, cpu)); > > +done: > mutex_unlock(&lock); > } I'm not sure this works as intended. Suppose CPU #30 is presently executing the for_each_online_cpu() loop and has reached CPU #15's per-cpu data. Now CPU #2 comes along, adds some pages to its per-cpu vectors then calls lru_add_drain_all(). AFAICT the code will assume that CPU #30 has flushed out all of the pages which CPU #2 just added, but that isn't the case. Moving the raw_write_seqcount_latch() to the point where all processing has completed might fix?