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 E5C86C32772 for ; Tue, 23 Aug 2022 08:52:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE9526B0073; Tue, 23 Aug 2022 04:52:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E72266B0078; Tue, 23 Aug 2022 04:52:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CEC4A8D0001; Tue, 23 Aug 2022 04:52:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BADA16B0073 for ; Tue, 23 Aug 2022 04:52:39 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 85A801C6D04 for ; Tue, 23 Aug 2022 08:52:39 +0000 (UTC) X-FDA: 79830241638.04.8566039 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 2166740043 for ; Tue, 23 Aug 2022 08:52:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661244758; h=from:from: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; bh=6rFNVYKMEmLCxCNJlzeuMdSCAZX8hcdmfkVuOImr0KA=; b=CiVdtMO9vaQx/NbNt5GRgr7BgCWeijZiXGW7DkBpeWRw2huaH8/xwU+l1PfYjIrgHx+eqO oQuJ2IATyGFRjFlTNZWl3btmKzfpdU7IS7t5JsxLuk43HedbhJDecbWXxEGrJW0rBsje1J ul/YH8R4ikPqXRMIuntO7ahuM+6DUuc= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-94-Fo2Oz0r4P0WNB1RsJl7Tiw-1; Tue, 23 Aug 2022 04:52:37 -0400 X-MC-Unique: Fo2Oz0r4P0WNB1RsJl7Tiw-1 Received: by mail-wr1-f70.google.com with SMTP id e14-20020adf9bce000000b002254afda62aso1051381wrc.18 for ; Tue, 23 Aug 2022 01:52:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc; bh=6rFNVYKMEmLCxCNJlzeuMdSCAZX8hcdmfkVuOImr0KA=; b=F0G1DNFV/rziGqahjtsgaRQFnWGRb3NDGWV9P079bT7H6FBZznQ6CaIxoBNQ40maTD baVat0Wo6pAlZA/xoKzM7wDaRSdAuemg4KHrzUAx3XqyPQaAEUsFRbKkbCFHHXsDuRdr fyL8aasK420trwr1oxnIVycNr2Ve0oIeEgX2aG5nk0IqGad4zah5np64mxVpCwYE4qaa YwnE3s/5tQGpHkeWi6FwZ9YfV2G4Ykqc51KjML9eQReubqCmlvx5nRYw6W98BOwyGVYD 5hEplXsEtZfvbVNSzZ92W7arjrz2uGaham9DjjXuzCiCJCcwQ5Io6jIZbgMsjnKdGC5h Ti4A== X-Gm-Message-State: ACgBeo1FhdUS++SOd261Qet9Ec4fUTiBJqyro7tF3VpxmKVETE3I/1lN V9Y/x0+LdbN67lKDD7NcI63LeJeoQGno1Y5SKpZSRiQPO3FoxJzvACuLyvNe4pNsZ518kcEtJKy AinFx9bvxbig= X-Received: by 2002:a05:600c:4047:b0:3a5:452e:9117 with SMTP id j7-20020a05600c404700b003a5452e9117mr1499712wmm.117.1661244756113; Tue, 23 Aug 2022 01:52:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR5Xs+2wMBjg6GNe/rvU1p8cL/HLk8C296CFWS7tf8EMX5X+isZ49/loFEsYY/Tty+nDEKQ7Ow== X-Received: by 2002:a05:600c:4047:b0:3a5:452e:9117 with SMTP id j7-20020a05600c404700b003a5452e9117mr1499694wmm.117.1661244755829; Tue, 23 Aug 2022 01:52:35 -0700 (PDT) Received: from ?IPV6:2003:cb:c70b:1600:c48b:1fab:a330:5182? (p200300cbc70b1600c48b1faba3305182.dip0.t-ipconnect.de. [2003:cb:c70b:1600:c48b:1fab:a330:5182]) by smtp.gmail.com with ESMTPSA id v15-20020adfedcf000000b0022546f469e1sm8596099wro.28.2022.08.23.01.52.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Aug 2022 01:52:35 -0700 (PDT) Message-ID: Date: Tue, 23 Aug 2022 10:52:34 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 To: Mel Gorman Cc: Michal Hocko , Patrick Daly , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Juergen Gross References: <20220817034250.GB2473@hu-pdaly-lv.qualcomm.com> <20220817104028.uin7cmkb4qlpgfbi@suse.de> <11f91089-1958-c7eb-126f-af32130d9f8a@redhat.com> <20220823083349.5c2aolc6xgfhp3k7@suse.de> From: David Hildenbrand Organization: Red Hat Subject: Re: Race condition in build_all_zonelists() when offlining movable zone In-Reply-To: <20220823083349.5c2aolc6xgfhp3k7@suse.de> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661244759; a=rsa-sha256; cv=none; b=SDQnRae5J9zhtSp/e85bhJs+NljkSMQBaIMewR+HcxCpXPf/KPjM4uDuSUx2ZBKOlHFH2x 5gtWT5e6CKFE1K5REYRNN/HlwfCVvGa0NzbBwcusC0F/ZkYjbs0FLLB5wsFfi4J4Ywyr8+ /P1oLTESJ9PEEBa5ptcfGSujvRdg6sM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CiVdtMO9; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661244759; 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=6rFNVYKMEmLCxCNJlzeuMdSCAZX8hcdmfkVuOImr0KA=; b=DtBxvrXloizg3t9pe5ajVCj0NWcxhr5mlXaXud4eCHGE9CI+yTHFkwfgyjnD3aXoX0d3hF zsPMnzZeHkLkPwSeeSITbmY3IaXS6Z5DBbmYgCoTSM6D+ICYQAcvN2O4bUwrhaRLGahPje QScDXzTiJBsJpUDMZB9TbFA4rci+pyo= X-Rspamd-Queue-Id: 2166740043 X-Rspam-User: Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CiVdtMO9; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam01 X-Stat-Signature: kcpx4dawtdpzfsch9xm6toyayyeidkcm X-HE-Tag: 1661244758-74824 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 23.08.22 10:33, Mel Gorman wrote: > On Tue, Aug 23, 2022 at 08:36:34AM +0200, David Hildenbrand wrote: >>> @@ -6517,6 +6538,7 @@ static void __build_all_zonelists(void *data) >>> static DEFINE_SPINLOCK(lock); >>> >>> spin_lock(&lock); >>> + write_seqcount_begin(&zonelist_update_seq); >>> >>> #ifdef CONFIG_NUMA >>> memset(node_load, 0, sizeof(node_load)); >>> @@ -6553,6 +6575,7 @@ static void __build_all_zonelists(void *data) >>> #endif >>> } >>> >>> + write_seqcount_end(&zonelist_update_seq); >>> spin_unlock(&lock); >> >> Do we want to get rid of the static lock by using a seqlock_t instead of >> a seqcount_t? >> > > I do not think so because it's a relatively heavy lock compared to the > counter and the read side. I was primarily asking because seqlock.h states: "Sequential locks (seqlock_t): Sequence counters with an embedded spinlock for writer serialization and non-preemptibility." seems to be precisely what we are doing here. > > As the read-side can be called from hardirq or softirq context, the > write-side needs to disable irqs or bottom halves as well according to the > documentation. That is relatively minor as the write side is rare but it's > tricker because the calling context can be both IRQ or softirq so the IRQ > protection would have to be used. Naive me would just have used write_seqlock()/write_sequnlock() and read_seqbegin()/read_seqretry() to result in almost the same code as with your change -- but having both mechanisms encapsulated. Yeah, there are special write_seqlock_bh()/write_sequnlock_bh(), write_sequnlock_irq() ... but IIRC we don't have to care about that at all when just using the primitives as above. But most probably I am missing something important. > > The read-side also gets more complicated. The read side becomes > either read_seqlock_excl() (or bh or IRQ as appropriate) or > read_seqbegin_or_lock. The read_seqlock_excl acts like a spinlock blocking > out other readers and writers, we definitely do not want to block out other > readers in the allocator path because .... that is crazy, it's basically a > global memory allocator lock. There is not an obvious option of limiting Yes. > the scope of that lock such as a single zone because it's the zonelists > we care about, not an individual zone. I guess it could be done on a > pgdat basis selected by the preferred zone but it's also unnecessarily > complicated and a relatively heavy lock. > > The other obvious choice is read_seqbegin_or_lock to locklessly try and > then retry if necessary. This has better semantics as a lockless version > exists with the caller tracking more state but again, the retry step is > heavy and acts as a global lock. Documentation/locking/seqlock.rst points out read path #1, that's just for "Normal Sequence readers which never block a writer but they must retry if a writer is in progress by detecting change in the sequence number." It's a simple use of read_seqbegin/read_seqretry. read_seqbegin()/read_seqretry() translate essentially to read_seqcount_begin()/read_seqcount_begin() -- except some kcsan() checks. > > In this case, the seqcounter or seqlock is protecting relatively simple > data -- the zonelist pointing to struct zones that never disappear (the > zone might be unpopulated but the struct zone still exists). The critical > data being protected in this context is either the PCP lists or the buddy > lists, each which has separate locking. The zonelist needs less protection > although RCU protection would be a potential, if somewhat complicated > option, as even if the zonelist itself is valid after an RCU update, > the zones listed are not necessarily useful any more. > > There is no real advantage to using seqcount_spinlock_t either as the > associated lock would be a global lock and there isn't any lockdep advantage > to doing that either. > > As the alternatives have side effects, I would prefer to see any proposed > conversion on top of the fix with review determining if there is any > unnecessary additional serialisation. > Agreed with more complicated conversions. Using a seqlock_t to replace the spinlock and avoid introducing the sequcount here would have been easy and straight forward, though. -- Thanks, David / dhildenb