Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (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 79D381E50E for ; Fri, 1 Aug 2025 01:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754010356; cv=none; b=Z8bgm/+7mVb3xAzckFNqcxtLRCZGugKwVQjCw1oQRTz31tTvCRBwimZqO0UQL21T5TDFZDUuon6Xc280OVti6/jadhKbgKmj7Hch9ilwQz7XdaDPSCYCxHFu2aKUeZcBMKQYKlLh/ZOQdXW5BIzncaFFRJmjirJjuC/AbDEL0wA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754010356; c=relaxed/simple; bh=CrqopOqnETrha7sxDqw5g7Qbwci0R9pf8ZjfuY374Gk=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=U1XjQDg6qLXJOi7FUDVvwdsJRku6q0/rLYen2xS3jUvH+JrQYNddopy2DvmiF0VaZDhMBD9zJ51GPyiDfm4nSMtckXiLvWFlZasUxy3oya6CzEXhbmIMw23vie/F/wNdrc496ljikmO/BKSyJh4+5e6k1h4GOhhGR6WMLz3Y5bs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=j22yoF36; arc=none smtp.client-ip=91.218.175.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="j22yoF36" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1754010351; 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=mWnfPBYlK3FgFXB+auPIsaTZOIy8G/auRktChN5Wm1g=; b=j22yoF36O7Wl/CWBmjIegSyfMPObwfQUWyL5Alo/iYKLFO7Kj8IDQYfA61MGWRgqI0VcMD /BhGqCqX0miqVasDbasMRXhQNSWG6+tu8s4fznPVgYELQYkV/yMuuJWX2OXdv0CrSANp+w kt18UwdyFUgHgNNnSmH2frTb37SiMfc= Date: Thu, 31 Jul 2025 18:05:45 -0700 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [RFC PATCH bpf-next v1 01/11] bpf: Convert bpf_selem_unlink_map to failable To: Amery Hung Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, alexei.starovoitov@gmail.com, andrii@kernel.org, daniel@iogearbox.net, memxor@gmail.com, kpsingh@kernel.org, martin.lau@kernel.org, yonghong.song@linux.dev, song@kernel.org, haoluo@google.com, kernel-team@meta.com References: <20250729182550.185356-1-ameryhung@gmail.com> <20250729182550.185356-2-ameryhung@gmail.com> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Martin KaFai Lau In-Reply-To: <20250729182550.185356-2-ameryhung@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 7/29/25 11:25 AM, Amery Hung wrote: > - bpf_local_storage_update() > > The three step update process: link_map(new_selem), > link_storage(new_selem), and unlink_map(old_selem) should not fail in > the middle. Hence, lock both b->lock before the update process starts. > > While locking two different buckets decided by the hash function > introduces different locking order, this will not cause ABBA deadlock > since this is performed under local_storage->lock. I am not sure it is always true. e.g. two threads running in different cores can do bpf_local_storage_update() for two different sk, then it will be two different local_storage->lock. My current thought is to change the select_bucket() to depend on the owner pointer (e.g. *sk, *task...) or the local_storage pointer instead. The intuitive thinking is the owner pointer is easier to understand than the local_storage pointer. Then the same owner always hash to the same bucket of a map. I am not sure the owner pointer is always available in the current setup during delete. This needs to check. iirc, the current setup is that local_storage->lock and bucket lock are not always acquired together. It seems the patch set now needs to acquire both of them together if possible. With this, I suspect something else can be simplified here and also make the owner pointer available during delete (if it is indeed missing in some cases now). Not very sure yet. I need a bit more time to take a closer look. Thanks for working on this! I think it can simplify the local storage. [ ... ] > @@ -560,8 +595,9 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, > struct bpf_local_storage_data *old_sdata = NULL; > struct bpf_local_storage_elem *alloc_selem, *selem = NULL; > struct bpf_local_storage *local_storage; > + struct bpf_local_storage_map_bucket *b, *old_b; > HLIST_HEAD(old_selem_free_list); > - unsigned long flags; > + unsigned long flags, b_flags, old_b_flags; > int err; > > /* BPF_EXIST and BPF_NOEXIST cannot be both set */ > @@ -645,20 +681,31 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, > goto unlock; > } > > + b = select_bucket(smap, selem); > + old_b = old_sdata ? select_bucket(smap, SELEM(old_sdata)) : b; > + > + raw_spin_lock_irqsave(&b->lock, b_flags); > + if (b != old_b) > + raw_spin_lock_irqsave(&old_b->lock, old_b_flags);