From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25AD7171A7; Thu, 27 Feb 2025 01:48:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740620901; cv=none; b=T0l0A8p0M0/7DtNX2vGXbMiVqAdnLPL6sU08Qahu1EchHOS9hVLEEN+an/T0RkRsRlQoRUwWW4FnQHokn0xXySLKx0y8dFtdEcEPEH/Xr80eRecDne9CVO4scsVyzFBDpdAeobRaCcknz0yHD6aSkvCLLMbK/H6l8OeZKpkb1rY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740620901; c=relaxed/simple; bh=hXCpgkhn+skhmHAhl4wNgROyDP88FR/s5XtSXYcjl7M=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=fAXpFfr62FMkX5nFGIHrennZFnE23IjNgZDg5Wgg8rMEbKGueBbu/XzarrJK+CiI0SU2EqnsZdRVJIF+Vbk0cdvyhzTOHJDrjCYQJmxkJgXdbCqUrzY+A9Cdb2fsXIR2c9En45//mrLSnXeVNTndUzOHyAMurG6W93Aw9TiRM30= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Z3DkF6TwJz4f3kvt; Thu, 27 Feb 2025 09:47:49 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 365681A183A; Thu, 27 Feb 2025 09:48:13 +0800 (CST) Received: from [10.174.176.117] (unknown [10.174.176.117]) by APP3 (Coremail) with SMTP id _Ch0CgBH5sBWxL9nbZjDEw--.49894S2; Thu, 27 Feb 2025 09:48:10 +0800 (CST) Subject: Re: [RESEND] [PATCH bpf-next 2/3] bpf: Overwrite the element in hash map atomically To: Zvi Effron , Alexei Starovoitov Cc: =?UTF-8?Q?Toke_H=c3=b8iland-J=c3=b8rgensen?= , bpf , rcu@vger.kernel.org, LKML , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "Paul E . McKenney" , Cody Haas , Hou Tao References: <20250204082848.13471-1-hotforest@gmail.com> <20250204082848.13471-3-hotforest@gmail.com> <8734gr3yht.fsf@toke.dk> <6a84a878-0728-0a19-73d2-b5871e10e120@huaweicloud.com> From: Hou Tao Message-ID: <7e614d80-b45b-e2f9-5a39-39086c2392dc@huaweicloud.com> Date: Thu, 27 Feb 2025 09:48:06 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:_Ch0CgBH5sBWxL9nbZjDEw--.49894S2 X-Coremail-Antispam: 1UD129KBjvJXoW3Jr1kKr1xArWDGw4fZFWxCrg_yoW7XFyUpF WrKF1UKrWDJ340qwn2yr1xZFWYyrn3Jw1jqr1DJFy5Arn8Kr1Sqr4xZa1F9F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wrote: >> On Tue, Feb 25, 2025 at 8:05 PM Hou Tao wrote: >>> Hi, >>> >>> On 2/26/2025 11:24 AM, Alexei Starovoitov wrote: >>>> On Sat, Feb 8, 2025 at 2:17 AM Hou Tao wrote: >>>>> Hi Toke, >>>>> >>>>> On 2/6/2025 11:05 PM, Toke Høiland-Jørgensen wrote: >>>>>> Hou Tao writes: >>>>>> >>>>>>> +cc Cody Haas >>>>>>> >>>>>>> Sorry for the resend. I sent the reply in the HTML format. >>>>>>> >>>>>>> On 2/4/2025 4:28 PM, Hou Tao wrote: >>>>>>>> Currently, the update of existing element in hash map involves two >>>>>>>> steps: >>>>>>>> 1) insert the new element at the head of the hash list >>>>>>>> 2) remove the old element >>>>>>>> >>>>>>>> It is possible that the concurrent lookup operation may fail to find >>>>>>>> either the old element or the new element if the lookup operation starts >>>>>>>> before the addition and continues after the removal. >>>>>>>> >>>>>>>> Therefore, replacing the two-step update with an atomic update. After >>>>>>>> the change, the update will be atomic in the perspective of the lookup >>>>>>>> operation: it will either find the old element or the new element. >>>> I'm missing the point. >>>> This "atomic" replacement doesn't really solve anything. >>>> lookup will see one element. >>>> That element could be deleted by another thread. >>>> bucket lock and either two step update or single step >>>> don't change anything from the pov of bpf prog doing lookup. >>> The point is that overwriting an existed element may lead to concurrent >>> lookups return ENOENT as demonstrated by the added selftest and the >>> patch tried to "fix" that. However, it seems using >>> hlist_nulls_replace_rcu() for the overwriting update is still not >>> enough. Because when the lookup procedure found the old element, the old >>> element may be reusing, the comparison of the map key may fail, and the >>> lookup procedure may still return ENOENT. >> you mean l_old == l_new ? I don't think it's possible >> within htab_map_update_elem(), >> but htab_map_delete_elem() doing hlist_nulls_del_rcu() >> then free_htab_elem, htab_map_update_elem, alloc, hlist_nulls_add_head_rcu >> and just deleted elem is reused to be inserted >> into another bucket. No. I mean the following procedure in which the lookup procedure finds the old element and tries to match its key, one update procedure has already deleted the old element, and another update procedure is reusing the old element: lookup procedure A A: find the old element (instead of the new old)                                                        update procedure B               B: delete the old element               update procedure C on the same CPU:               C: reuse the old element (overwrite its key and insert in the same bucket) A: the key is mismatched and return -ENOENT. It can be reproduced by increasing ctx.loop in the selftest. >> >> I'm not sure whether this new hlist_nulls_replace_rcu() >> primitive works with nulls logic. >> >> So back to the problem statement.. >> Are you saying that adding new to a head while lookup is in the middle >> is causing it to miss an element that >> is supposed to be updated assuming atomicity of the update? >> While now update_elem() is more like a sequence of delete + insert? >> >> Hmm. > Yes, exactly that. Because update_elem is actually a delete + insert (actually > an insert + delete, I think?), it is possible for a concurrent lookup to see no > element instead of either the old or new value. Yep. > >>> I see. In v2 I will fallback to the original idea: adding a standalone >>> update procedure for htab of maps in which it will atomically overwrite >>> the map_ptr just like array of maps does. >> hold on. is this only for hash-of-maps ? > I believe this was also replicated for hash as well as hash-of-maps. Cody can > confirm, or use the reproducer he has to test that case. The fix for hash-of-maps will be much simpler and the returned map pointer will be correct. For other types of hash map, beside switching to hlist_nulls_replace_rcu(),  I think we also need to detect the reuse of element during the loop: if the element has been reused, the lookup should restart from the head again. However, even withing the above two fixes, the value returned by lookup procedure for other types of hash map may still be incorrect (as shown long time ago [1]), so I think maybe for other types of map, the atomic update doesn't matter too much. [1]: https://lore.kernel.org/bpf/20221230041151.1231169-1-houtao@huaweicloud.com/ > >> How that non-atomic update manifested in real production? >> > See [1] (in the cover letter for this series, also replicated below). > > [1] : https://lore.kernel.org/xdp-newbies/07a365d8-2e66-2899-4298-b8b158a928fa@huaweicloud.com/T/#m06fcd687c6cfdbd0f9b643b227e69b479fc8c2f6