Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp4584068rwi; Mon, 17 Oct 2022 08:07:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5BrT18m7iTwPXBU+6g2BVh138TRG5/B7Nv9AUiNDRFiNYkXJsByQIVkZr01R9N6ivf4K4M X-Received: by 2002:a17:907:7da5:b0:78e:2c3b:55a2 with SMTP id oz37-20020a1709077da500b0078e2c3b55a2mr8993399ejc.96.1666019228030; Mon, 17 Oct 2022 08:07:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666019227; cv=none; d=google.com; s=arc-20160816; b=GOnsoH77YzVb+QrfDLx+T5Ws8csZnfMSOuqqmFUFeiyTrUwm6Q3p4vR6KSe2iC/WqW 9eO6SGL426rCpGYyOkRPKGqeREq0p6wXZcLve39YLVj+zfOteIViKDtm6XmVvov25Y49 N7qcso+zOmHum8Lbhjg2ugneqqoC2LbZyhyCt1i6RmrmQSf2xW/pg7I1Bvor1D5DDw6+ xWUMfWXS1yAtM+LxdEx+spyVh3/ebXPRxINTI2drHCtQ4U4StkkRqQjT0oOO6TjNJ9mn 1PCod1im1FdQFihNTrK4EX243NI2cC3/+hc7FXird1iFgOpyD8c6eAlWdb06arXssoRT ELyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=K3SycNJ7w4AdqzkckBu+ABwBP4C0TDu4y0Ri/+wpNo4=; b=nse2Rcw7MpGifrt6K8Mr10p8J3kcj0THYC2qd1cijBcd3aCv4iXkVygoDexxr7LCPs aqh7sNvANWBIH+RBRwCVEHQWeNIOgmqUxdDc2FG6U4uD++fl4uJp4eMoq3X0cV0Wef9M oFyuvmWafSY/OF7zJbGiJHURenyEyxZ02qhmvtzYx7u8xZZ0lA/NqhJ9lFFk/FgBLpK6 yryHKNBJT8+Qfq+mk/Z3pejJcppDJ9V1VmdKjUKnER5nzrU7Q0KTkzGOfVNlx/bqLz+3 o8ndq4AJWUj9dxik/vKHia9F50bik0HMXRlyKCswEfJEZ9ogeI6CyzNDzfMpCZSqsjfK IExw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="bkZec/mp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w5-20020a170906d20500b0078d2784552fsi8718084ejz.684.2022.10.17.08.06.36; Mon, 17 Oct 2022 08:07:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="bkZec/mp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230442AbiJQOjp (ORCPT + 99 others); Mon, 17 Oct 2022 10:39:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230303AbiJQOjn (ORCPT ); Mon, 17 Oct 2022 10:39:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7644DF43; Mon, 17 Oct 2022 07:39:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3F8316117D; Mon, 17 Oct 2022 14:39:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 97457C43144; Mon, 17 Oct 2022 14:39:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666017576; bh=S3adBKWWRCbdw9aTgDVjLyyamcpV9tzhi9nN0qio5y8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bkZec/mpzcK73DGRsO+9WMq/Vf5m3ykC5aBPApPU4IlCMDH/aVBUT6I0Xcr+eR1vi latXBJJn9/vjqk+Cmg3zR8EZ18LjveNkJxgSQHO0b6oqQ6sJKJIbwom6zZh0xOBWSj oHmxLPQjjacMwWcJYTQwvXDUYaKLd7RgFWyrS/gaymZeeCaCBcw04K6kOKaMdH52hC 8kuWJnaipTMtjkAqtwXBGuaU5bF3t1m6zpN3iz03ulc7N9sigKx9tZEeE2aeow9XLA V8xWJnLvFkTABpmBnID3kfXpocnADmMUA6jchPTPVMC3emP5huEQdY4kYMT0HLUvXE bV3P5Rn20U/ow== Received: by mail-lf1-f49.google.com with SMTP id f37so17819625lfv.8; Mon, 17 Oct 2022 07:39:36 -0700 (PDT) X-Gm-Message-State: ACrzQf2pjYg+1o41l68BmIxh8yBnAO5i7KDzKrgd/e+wnqkIykLzGOvU sXRLt4OS1YyVbb2ewwussiAIqtl+5bzRftrNXJ0= X-Received: by 2002:ac2:4c47:0:b0:4a2:c07b:4b62 with SMTP id o7-20020ac24c47000000b004a2c07b4b62mr3839451lfk.426.1666017574462; Mon, 17 Oct 2022 07:39:34 -0700 (PDT) MIME-Version: 1.0 References: <20221017130140.420986-1-justin.he@arm.com> <20221017130140.420986-7-justin.he@arm.com> In-Reply-To: <20221017130140.420986-7-justin.he@arm.com> From: Ard Biesheuvel Date: Mon, 17 Oct 2022 16:39:22 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v9 6/7] apei/ghes: Use xchg_release() for updating new cache slot instead of cmpxchg() To: Jia He Cc: Len Brown , Tony Luck , Borislav Petkov , Mauro Carvalho Chehab , Robert Richter , Robert Moore , Qiuxu Zhuo , Yazen Ghannam , Jan Luebbe , Khuong Dinh , Kani Toshi , James Morse , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org, devel@acpica.org, "Rafael J . Wysocki" , Shuai Xue , Jarkko Sakkinen , linux-efi@vger.kernel.org, nd@arm.com, Peter Zijlstra Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 17 Oct 2022 at 15:02, Jia He wrote: > > From: Ard Biesheuvel > > From: Ard Biesheuvel > > ghes_estatus_cache_add() selects a slot, and either succeeds in > replacing its contents with a pointer to a new cached item, or it just > gives up and frees the new item again, without attempting to select > another slot even if one might be available. > > Since only inserting new items is needed, the race can only cause a failure > if the selected slot was updated with another new item concurrently, > which means that it is arbitrary which of those two items gets > dropped. This means the cmpxchg() and the special case are not necessary, > and hence just drop the existing item unconditionally. Note that this > does not result in loss of error events, it simply means we might > cause a false cache miss, and report the same event one additional > time in quick succession even if the cache should have prevented that. > > Move the xchg_release() and call_rcu out of rcu_read_lock/unlock section > since there is no actually dereferencing the pointer at all. > > Co-developed-by: Jia He > Signed-off-by: Jia He > Signed-off-by: Ard Biesheuvel > --- > drivers/acpi/apei/ghes.c | 47 +++++++++++++++++++++------------------- > 1 file changed, 25 insertions(+), 22 deletions(-) > > diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c > index 27c72b175e4b..5d7754053ca0 100644 > --- a/drivers/acpi/apei/ghes.c > +++ b/drivers/acpi/apei/ghes.c > @@ -150,7 +150,7 @@ struct ghes_vendor_record_entry { > static struct gen_pool *ghes_estatus_pool; > static unsigned long ghes_estatus_pool_size_request; > > -static struct ghes_estatus_cache *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE]; > +static struct ghes_estatus_cache __rcu *ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE]; > static atomic_t ghes_estatus_cache_alloced; > > static int ghes_panic_timeout __read_mostly = 30; > @@ -785,31 +785,26 @@ static struct ghes_estatus_cache *ghes_estatus_cache_alloc( > return cache; > } > > -static void ghes_estatus_cache_free(struct ghes_estatus_cache *cache) > +static void ghes_estatus_cache_rcu_free(struct rcu_head *head) > { > + struct ghes_estatus_cache *cache; > u32 len; > > + cache = container_of(head, struct ghes_estatus_cache, rcu); > len = cper_estatus_len(GHES_ESTATUS_FROM_CACHE(cache)); > len = GHES_ESTATUS_CACHE_LEN(len); > gen_pool_free(ghes_estatus_pool, (unsigned long)cache, len); > atomic_dec(&ghes_estatus_cache_alloced); > } > > -static void ghes_estatus_cache_rcu_free(struct rcu_head *head) > -{ > - struct ghes_estatus_cache *cache; > - > - cache = container_of(head, struct ghes_estatus_cache, rcu); > - ghes_estatus_cache_free(cache); > -} > - > static void ghes_estatus_cache_add( > struct acpi_hest_generic *generic, > struct acpi_hest_generic_status *estatus) > { > int i, slot = -1, count; > unsigned long long now, duration, period, max_period = 0; > - struct ghes_estatus_cache *cache, *slot_cache = NULL, *new_cache; > + struct ghes_estatus_cache *cache, *new_cache; > + struct ghes_estatus_cache __rcu *victim; > > new_cache = ghes_estatus_cache_alloc(generic, estatus); > if (new_cache == NULL) > @@ -820,13 +815,11 @@ static void ghes_estatus_cache_add( > cache = rcu_dereference(ghes_estatus_caches[i]); > if (cache == NULL) { > slot = i; > - slot_cache = NULL; > break; > } > duration = now - cache->time_in; > if (duration >= GHES_ESTATUS_IN_CACHE_MAX_NSEC) { > slot = i; > - slot_cache = cache; > break; > } > count = atomic_read(&cache->count); > @@ -835,18 +828,28 @@ static void ghes_estatus_cache_add( > if (period > max_period) { > max_period = period; > slot = i; > - slot_cache = cache; > } > } > - /* new_cache must be put into array after its contents are written */ > - smp_wmb(); > - if (slot != -1 && cmpxchg(ghes_estatus_caches + slot, > - slot_cache, new_cache) == slot_cache) { > - if (slot_cache) > - call_rcu(&slot_cache->rcu, ghes_estatus_cache_rcu_free); > - } else > - ghes_estatus_cache_free(new_cache); > rcu_read_unlock(); > + > + if (slot != -1) { > + /* > + * Use release semantics to ensure that ghes_estatus_cached() > + * running on another CPU will see the updated cache fields if > + * it can see the new value of the pointer. > + */ > + victim = xchg_release(&ghes_estatus_caches[slot], new_cache); > + This still lacks the RCU_INITIALIZER() > + /* > + * At this point, victim may point to a cached item different > + * from the one based on which we selected the slot. Instead of > + * going to the loop again to pick another slot, let's just > + * drop the other item anyway: this may cause a false cache > + * miss later on, but that won't cause any problems. > + */ > + if (victim) > + call_rcu(unrcu_pointer(&victim->rcu), ghes_estatus_cache_rcu_free); Please use &unrcu_pointer(victim)->rcu here. > + } > } > > static void __ghes_panic(struct ghes *ghes, > -- > 2.25.1 >