From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (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 3DC532517A5 for ; Tue, 20 May 2025 15:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747753840; cv=none; b=Uxqzk0om+jXZEQG7TvQekM9/3Ehbx6KRL2NiTTcfe7QPPf5AGsTqcnHCqhPCyRoOBHOFJE8oC9T+tvON30mV4zYB7zMUeDdaRdr46JWdIiu23N1c0sHYYICmgnwBk+/wRA1/viwISj1JuHTQWHgK9+PaDgOGINnhTiERPCN/lM8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747753840; c=relaxed/simple; bh=2un06VrPK/WUBLF3bKsicFJw3OV1P3KcckXmpF5KCZ4=; h=MIME-Version:Date:Content-Type:From:Message-ID:Subject:To:Cc: In-Reply-To:References; b=jXtquAPq3W3rpPj4aDWSsb8n5r4MtqkmmR9EDARODKTsTQIBN2EWuOS9jFFGd8h99zrpBnFRabaYjtgpl1BOTGQVVQMmaR9+F5OAtfuFMR3XJEUDYHwQM3HsS2P31jxtYCa1frrn3jwW6ijotRtSPu+emTafDeOuPizsQIH8RUA= 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=WyV9fTbs; arc=none smtp.client-ip=91.218.175.184 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="WyV9fTbs" Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1747753836; 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=bEX1jHUrub31rPVQwtog69oMgdloGivRIBG2sjKnaXg=; b=WyV9fTbsu4lxkfYNnEJh/F1W91jONFvvTP8k7RKIjUdMFfryWEiGiONMWLuQdW/JJL9blK XdrLd2ziLkffSiX2jy87Y7613p3BApHjZvemx5OIxCmOoafvfTNfc8rkoEHxwq/gyGY4Xc 00cCcauZG5jc9VCy9cvf0JbuLvR1k6A= Date: Tue, 20 May 2025 15:10:34 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: "Jiayuan Chen" Message-ID: TLS-Required: No Subject: Re: [PATCH bpf-next v1] bpf, sockmap: Fix concurrency issues between memory charge and uncharge To: "John Fastabend" , "Cong Wang" Cc: bpf@vger.kernel.org, "Jakub Sitnicki" , "David S. Miller" , "Eric Dumazet" , "Jakub Kicinski" , "Paolo Abeni" , "Simon Horman" , "Cong Wang" , "Alexei Starovoitov" , netdev@vger.kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250519200003.46elezpkkfx5grl4@gmail.com> References: <20250508062423.51978-1-jiayuan.chen@linux.dev> <20250519200003.46elezpkkfx5grl4@gmail.com> X-Migadu-Flow: FLOW_OUT May 20, 2025 at 04:00, "John Fastabend" wrote: >=20 >=20On 2025-05-18 11:48:31, Cong Wang wrote: >=20 [...] >=20>=20 >=20> Solution: > >=20 >=20> 1. Add locking to the kfree_sk_msg() process, which is only called= in the > >=20 >=20> user process context. > >=20 >=20> 2. Integrate the charge process into sk_psock_create_ingress_msg()= in the > >=20 >=20> backlog process and add locking. > >=20 >=20> 3. Reuse the existing psock->ingress_lock. > >=20 >=20>=20=20 >=20>=20 >=20> Reusing the psock->ingress_lock looks weird to me, as it is intend= ed for > >=20 >=20> locking ingress queue, at least at the time it was introduced. > >=20 >=20>=20=20 >=20>=20 >=20> And technically speaking, it is the sock lock which is supposed to= serialize > >=20 >=20> socket charging. > >=20 >=20>=20=20 >=20>=20 >=20> So is there any better solution here? > >=20 >=20 > Agree I would be more apt to add the sock_lock back to the backlog then >=20 >=20to punish fast path this way. >=20 >=20Holding the ref cnt on the psock stops blocks the sk_psock_destroy() = in >=20 >=20backlog now so is this still an issue? >=20 >=20Thanks, >=20 >=20John > Thanks to Cong and John for their feedback. For TCP, lock_sock(sk) works as expected. However, since we now support multiple socket types (UNIX, UDP), the locking mechanism must be adapted accordingly. For UNIX sockets, we must use u->iolock instead of lock_sock(sk) in the backlog path. This is because we already acquire lock(u->iolock) in both: ''' unix_bpf_recvmsg() (BPF handler) unix_stream_read_generic() (native handler) ''' For UDP, the native handler __skb_recv_udp() locks udp_sk(sk)->reader_que= ue->lock, but no locking is implemented in udp_bpf_recvmsg(). This implies that ing= ress_lock effectively serves the same purpose as udp_sk(sk)->reader_queue->lock to = prevent concurrent user-space access. Conclusion: To avoid using ingress_lock, we need to implement a per-socket locking st= rategy into psock: Default implementation: lock_sock(sk) UNIX sockets: Use lock(u->iolock) in backlog path. UDP sockets: Explicitly use reader_queue->lock both in udp_bpf_recvmsg() = and backlog path. As of now, I don=E2=80=99t have any better ideas.