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=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 1A4D1ECE58B for ; Sat, 5 Oct 2019 20:04:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CDC2F20867 for ; Sat, 5 Oct 2019 20:04:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HkYm1YbP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CDC2F20867 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 708716B0003; Sat, 5 Oct 2019 16:04:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B83F8E0003; Sat, 5 Oct 2019 16:04:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F44C6B0006; Sat, 5 Oct 2019 16:04:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0253.hostedemail.com [216.40.44.253]) by kanga.kvack.org (Postfix) with ESMTP id 38B9A6B0003 for ; Sat, 5 Oct 2019 16:04:05 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id C320C6D77 for ; Sat, 5 Oct 2019 20:04:04 +0000 (UTC) X-FDA: 76010807208.20.rate78_7ad530ce3c518 X-HE-Tag: rate78_7ad530ce3c518 X-Filterd-Recvd-Size: 5642 Received: from mail-io1-f68.google.com (mail-io1-f68.google.com [209.85.166.68]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Sat, 5 Oct 2019 20:04:03 +0000 (UTC) Received: by mail-io1-f68.google.com with SMTP id c25so20625557iot.12 for ; Sat, 05 Oct 2019 13:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mRHgwxZdGw0blwU74PDIectE4sOFeAoxwqMR6ZTtSjU=; b=HkYm1YbP6dhP5HDrvhFJiVGj0WMBirms1vp0+/Dp4li8Rvy0DQ9X4+delTdtfmi3Py fySH0qUwpqO0SpoBuhDqygMEfYV8TKIybhe5uVnusupZQvLT+2HrATBMXO+3Hgi4tb0n WJIUcc3n1Gbqv9oIFBnSWHh98uADLYgPPxmv3SMKEM8S7VsTwwANAq1F0aHecJcYmGJE wC5pK6o77+Ls5d0gdraEJsF7mMdFV9EYaVtVukrwL3fQzLAaPwDPL8tt6bckpw1JJkh8 MPWS+sqOm06P+IjTjvlrANW12l1OQFIVbhe6l+Ub1nhei9TdlEOYl7ZOMLB73rjhnNuo fDzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mRHgwxZdGw0blwU74PDIectE4sOFeAoxwqMR6ZTtSjU=; b=iSMl78H08UnjrqXM6PB8B0N4qP+vxEIjViXRtzHPs6OefOitiZ0m8vb+kJ3D0pDGC0 RdwbP9EebMJYzHO5cPVOCNpZ0lmj7BiUJhCFxNIG92nN6UBPlJ9w1Ml1qXa6t1rYFeT2 S6LrMaXLw7zkIX78pskcDLQRsNhCjIn3YPEFmGb8qqouycXjhR+VZq2IF530owo1RYBI gcQ3CoW//gWpG2MdvlxvndnnLFieR7p/HUkKDc7cy4+Cmae/phmL2HrA/dwgHEGqoV2q bR3VHFfrgUr2Up76xaZm1RJNehwjxmSimoFmo3muGMZgdXM3aLAKQUTBAz0l60+80Kob N6MQ== X-Gm-Message-State: APjAAAUMD4VUuOA/nEioexDC4wnoLckjugU2HMHuEP78Cg63MxfudU+m ZDHJ9ro9KmE35cSRxIaMFC0SQ5O2YRAluKLa2rU= X-Google-Smtp-Source: APXvYqwOrc0R/c3VOz/QM5b3vD7qEJVUgTKPFzQytiP7GeQI2Rf77AeDFQs4Rii1UaUX2u4CEmwV/h5mSqlM6ZRWB34= X-Received: by 2002:a02:4487:: with SMTP id o129mr21197311jaa.136.1570305843122; Sat, 05 Oct 2019 13:04:03 -0700 (PDT) MIME-Version: 1.0 References: <157019456205.3142.3369423180908482020.stgit@buzz> <20191005123523.0db4ad1b9f268c419f8a59eb@linux-foundation.org> In-Reply-To: <20191005123523.0db4ad1b9f268c419f8a59eb@linux-foundation.org> From: Konstantin Khlebnikov Date: Sat, 5 Oct 2019 23:03:50 +0300 Message-ID: Subject: Re: [PATCH v2] mm/swap: piggyback lru_add_drain_all() calls To: Andrew Morton Cc: Konstantin Khlebnikov , linux-mm@kvack.org, Michal Hocko , Linux Kernel Mailing List , Matthew Wilcox Content-Type: text/plain; charset="UTF-8" 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 Sat, Oct 5, 2019 at 10:35 PM Andrew Morton wrote: > > 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? > > No, raw_write_seqcount_latch() should be exactly before draining. Here seqcount works as generation of pages that could be in vectors. And all steps are serialized by mutex: only after taking lock we could be sure that all previous generations are gone. Here CPU #2 will see same generation at entry and after taking lock. So it will drain own pages.