From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796202336; b=ljI4LUqxnhj3ENnkjswmUIlZlmYmPgkDkJMWM5t7qqzujO+GghjOssiX6NmVwqtN4yDr 658oXSDFVh9Ifbuw0OyQDPCHPURynwEyZNxm3xDyCUqHVpmqmyXMGbpkODxyCFrqMNKxH awRImp9CLuyQ/WK7cUJrJQBCbiN/k4DxM0K14uRZjSKKKNYUQTV1loNTxO1/Atrz1YbV9 JKUMMkvqA+rusZYqj2A05PR9zFgk7FNX4uYSqdfKX73j3uiA4LLMt8izR3ffnzVodeFkC EtpbolOtvrJx916hathmWyR8W6IyU5TUX/l0P/GMltO2SwG2jP0B7owHBdLX6tTD+Nw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796202336; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:Received:Received:Received:Mime-Version: Content-Transfer-Encoding:Content-Type:Date:Message-Id:Cc:Subject: From:To:X-Mailer:References:In-Reply-To:X-TM-AS-GCONF: X-Proofpoint-ORIG-GUID:X-Proofpoint-Spam-Details-Enc: X-Proofpoint-GUID:X-Proofpoint-Spam-Info:X-Authority-Analysis: X-Proofpoint-Virus-Version:X-Proofpoint-Spam-Details; bh=qnKPOM7h+vlH3SfnFNBcNjMG8e8AUJ8N7lVjZIJABig=; b=3XqQZTUMil1tf7bpAbO8w1gKpRhDJCCV43OuiFebFOof3DiAqL++pL7+UYrL3d2IkQbL bb82rRb1vW2XvV0Je60VFhePFR/tHhEvuCV5xgCLIIdFpQi5wGtLTy9VBNqz0PVEk3loi QmFnMtakoITeU0u4DJEyd75penpT6aSWF/YK2bgOa+zcA/U1uL/8g675bDH4YCEuwnjez qV5FWvWBemBM8g/P2JAGh2uPThsVfQJeXiH0q2vARlc8uryJX4F7B11u4CszyuK91TC9y rJ1/8Ac+pDL0WhEqO0RREOHirm+nDvOWXmuCo4lthR+MQEKAiOmEGaNjcBkCCm0baWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=OQPHrEAb; arc=none smtp.remote-ip=148.163.156.1 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD410606A31A for ; Wed, 2 Dec 2026 09:05:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DD410606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=OQPHrEAb Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B274YQj2397885 for ; Wed, 2 Dec 2026 09:05:35 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pp1; bh=qnKPOM 7h+vlH3SfnFNBcNjMG8e8AUJ8N7lVjZIJABig=; b=OQPHrEAb6gmVr8XnrcI91c yfQYGd0ueEO8f9hRWM4JyEJnh2P8Ynw9q7y3dRvEYmNy3sVQnnP9q5SenALkUuO+ sprTIWARWyHDYV2QYoPigK+cDL56mo4Qpa1AwFeCHBWYCfG2dFdEc0HF6NmpkJNP vkqOnVa+l1XftVG5zf80jFYvkbFrknsS8nruQ2bwMJYzZuAnT6GDB+CZkLAQf7HV hsutH6sfxg5cvfAd60PDnMvakZYuxP44t4ModrAHEy3NLA5zRyrP/+413AYlfzqJ 7aDqvq16KBOE3kEwn+2ojGMoMoP6fSl2h+5Y0VM2EkDzqzVmpzg2YMqUHHeLhuFw == Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4j7pmt8w80-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT) for ; Wed, 02 Dec 2026 09:05:34 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B26nK2J1626995 for ; Wed, 2 Dec 2026 09:05:34 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 4ja0pq3a67-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 02 Dec 2026 09:05:33 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B295Snj47579584 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 2 Dec 2026 09:05:28 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 60C4920040; Wed, 2 Dec 2026 09:05:28 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 17CA52004E; Wed, 2 Dec 2026 09:05:28 +0000 (GMT) Received: from localhost (unknown [9.68.1.24]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 2 Dec 2026 09:05:28 +0000 (GMT) X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 02 Dec 2026 10:05:27 +0100 Message-Id: Cc: , , , , , , , Subject: Re: [PATCH 1/4] dibs: introduce ring-buffer send/receive API From: "Tobias Schumacher" To: "Alexandra Winter" , "Julian Ruess" , , , , X-Mailer: aerc 0.22.0 References: <20261008-console_server-v1-0-636b11ebf191@linux.ibm.com> <20261008-console_server-v1-1-636b11ebf191@linux.ibm.com> <1ded15e9-99bd-44ca-82c2-5bc63ede39c7@linux.ibm.com> In-Reply-To: <1ded15e9-99bd-44ca-82c2-5bc63ede39c7@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: cSc5atiyezoou56lIo1Q4HyqDjGyQno5 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAyMDAzNiBTYWx0ZWRfX63n6oAN1ZXtp t+VvVE2nGODje4cpIPf6WpFYatD5rxiguBs8FEo8guVFraUUsE7N+p/D0LhRNIM8h9mOzKrEh9r s9UvvAF1ueFNdYYfkAfIHIbPp4b9lG4pswexhASOiNJSKVEnL6qH6wzz33sqBsEdZfYimWlR5LI dEcv+qMn1HbxEtMjOvLZwi+gGfGTQED6peSq38NJUNw86H/Wdr0j4nugwWgSdVjlsI19iQ1mdyz f1H70cBGD5vxz2DL/HWOiQzHups6K/iXLMf9ZTulE3gv14AHPlyvqzgynQ6Eo7Q7NW94EnHrlbU JOmuWWZOR4cwtvF02D1nHEgGEuGdvTArVfq8CyeQ7UIkdL+i54NmFSX0gUEDiW4oj65klKiAHZ5 DHOCzzvNwtZk2UTUOPonMRoIG+I6Kz7WYDPeqkPK/LyUHl6hzoyEWokG78z7Eytv2ZJa+qoEA7Z rizViXdwGa3fWmTtMbg== X-Proofpoint-GUID: cSc5atiyezoou56lIo1Q4HyqDjGyQno5 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAyMDAzNiBTYWx0ZWRfX1QsRmnUH5p6e FNpL0rbhf4AoEjVeY1SxZheGPj7jhTwgrpty+G8L6WOA1VVTkjPT2t/FI/jL+gC83L25HRPIs3E MHU8u3iDqYYJyp0d4u6AoQjOz+bBWQU= X-Authority-Analysis: v=2.4 cv=U9cHnuru c=1 sm=1 tr=0 ts=6b0fdf5e cx=c_pps a=AfN7/Ok6k8XGzOShvHwTGQ==:117 a=AfN7/Ok6k8XGzOShvHwTGQ==:17 a=IkcTkHD0fZMA:10 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=uAbxVGIbfxUO_5tXvNgY:22 a=VwQbUJbxAAAA:8 a=x92ouWQmYzUl1PteA8kA:9 a=uH9wPYOPX4UCiJZ7:21 a=QEXdDO2ut3YA:10 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-02_02,2026-12-01_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 phishscore=0 malwarescore=0 impostorscore=0 lowpriorityscore=0 clxscore=1015 spamscore=0 priorityscore=1501 bulkscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612020036 On Tue Nov 10, 2026 at 6:44 PM CET, Alexandra Winter wrote: -- snip -- >> /* use an array rather a list for fast mapping: */ >> @@ -93,7 +96,8 @@ int dibs_unregister_client(struct dibs_client *client) >> max_dmbs =3D dibs->ops->max_dmbs(); >> for (int i =3D 0; i < max_dmbs; ++i) { >> if (dibs->dmb_clientid_arr[i] =3D=3D client->id) { >> - WARN(1, "%s: attempt to unregister '%s' with registered dmb(s)\n", >> + WARN(1, >> + "%s: attempt to unregister '%s' with registered dmb(s)\n", >> __func__, client->name); >> rc =3D -EBUSY; >> goto err_reg_dmb; >> @@ -245,6 +249,491 @@ void dibs_dev_del(struct dibs_dev *dibs) >> } >> EXPORT_SYMBOL_GPL(dibs_dev_del); >> =20 > > Don't do formatting changes in lines you don't touch otherwise. [1] Will undo that. >> +/** >> + * dibs_ring_peer_meta() - take a snapshot of the peer's ring metadata >> + * @rb: pointer to the ring buffer >> + * @peer: snapshot to fill in >> + * >> + * The peer writes its metadata dmb at any time, so read each field onc= e and >> + * copy to the snapshot. >> + */ >> +static void dibs_ring_peer_meta(struct dibs_ring_buffer *rb, >> + struct dibs_ring_meta *peer) >> +{ >> + peer->meta_dmb_tok =3D READ_ONCE(rb->rd_ring_meta->meta_dmb_tok); >> + peer->buff_dmb_tok =3D READ_ONCE(rb->rd_ring_meta->buff_dmb_tok); >> + peer->size =3D READ_ONCE(rb->rd_ring_meta->size); >> + peer->head =3D READ_ONCE(rb->rd_ring_meta->head); >> + peer->tail =3D READ_ONCE(rb->rd_ring_meta->tail); >> + peer->reserved =3D 0; >> +} >> + > > Is this snapshotting really helpful/sufficient/necessary? > I guess you say that content of rd_ring_meta can change any time, but it > could even change inbetween the READ_ONCE calls. > > Seems you call it before dibs_ring_peer_replaced(), is this any better > than to compare the current rd_ring_meta->buff_dmb_tok? > > I guess there is a general question of validity of the meta buffers, > what if the other side changes and does not update its information in > rd_ring_meta atomically? > Maybe you need to define some rules? Agreed that the indivindual READ_ONCE()s do not give a consistent snapshot. Will define rules for metadata in v2: tokens and size do not change while a ring is registered, only head and tail are updated, each with a move_data of that single field. >> +static int dibs_ring_send_hdr(struct dibs_ring_buffer *rb, u64 meta_dmb= _tok) >> +{ >> + int res; >> + >> + if (!meta_dmb_tok) >> + return 0; >> + >> + res =3D rb->dibs->ops->move_data(rb->dibs, meta_dmb_tok, 0, true, 0, >> + &rb->wr_ring_meta, >> + sizeof(struct dibs_ring_meta)); >> + >> + if (!res) >> + rb->hdr_sent =3D true; >> + >> + return res; >> +} >> + >> +/** >> + * dibs_ring_register() - register a DIBS ring buffer >> + * @name: name of the ring buffer. This will be used in debug output to >> + * support identifying the ring buffer. >> + * @rb: pointer to the ring buffer struct. The fields in the struct don= 't have >> + * to be initialized. >> + * @client: pointer to the DIBS client >> + * @dibs: pointer to the DIBS device >> + * @rgid: the remote GID that will be allowed to write into this ring b= uffer >> + * @size: size of the ring buffer in bytes >> + * >> + * Register and initialize a DIBS ring buffer. Allocates the metadata D= MB and >> + * the payload DMB, initializes the fields in dibs_ring_buffer. >> + * >> + * Return: 0 in case of success, an error code in case of failure >> + */ >> +int dibs_ring_register(char *name, struct dibs_ring_buffer *rb, >> + struct dibs_client *client, struct dibs_dev *dibs, >> + uuid_t rgid, u32 size) >> +{ >> + int ret; >> + >> + if (!size || !is_power_of_2(size)) >> + return -EINVAL; >> + >> + strscpy(rb->name, name, sizeof(rb->name)); >> + rb->dibs =3D dibs; >> + rb->dmb.rgid =3D rgid; >> + >> + rb->rd_meta_dmb.dmb_len =3D ALIGN(sizeof(struct dibs_ring_meta), 4096)= ; >> + rb->rd_meta_dmb.rgid =3D rgid; >> + ret =3D dibs->ops->register_dmb(dibs, &rb->rd_meta_dmb, client); >> + if (ret) >> + return ret; >> + >> + rb->dmb.dmb_len =3D ALIGN(size, 4096); >> + ret =3D dibs->ops->register_dmb(dibs, &rb->dmb, client); >> + if (ret) { >> + dibs->ops->unregister_dmb(rb->dibs, &rb->rd_meta_dmb); >> + return ret; >> + } >> + >> + rb->dmb_registered =3D 1; >> + > > Maybe add a comment that now the fields in wr_ring_meta (better name?) > are filled out for the remote side. > >> + rb->wr_ring_meta.meta_dmb_tok =3D rb->rd_meta_dmb.dmb_tok; >> + rb->wr_ring_meta.buff_dmb_tok =3D rb->dmb.dmb_tok; >> + rb->wr_ring_meta.size =3D size; >> + rb->wr_ring_meta.head =3D 0; >> + rb->wr_ring_meta.tail =3D 0; >> + rb->wr_ring_meta.reserved =3D 0; >> + > > How is this ever sent to the other side? > ok, I see that all kinds of functions call dibs_ring_send_hdr(), e.g. > dibs_ring_cnt(). > Is there an recommended intital handshake sequence? I'll add documentation regarding the handshake, meaning of the struct's field in v2. One side must first call dibs_ring_set_rdmb_tok(), which then writes its header to the other side. The other side receives an interrupt and then sends its own header in dibs_ring_recv(). >> + rb->rd_ring_meta =3D rb->rd_meta_dmb.cpu_addr; >> + rb->peer_buff_dmb_tok =3D 0; >> + rb->msg_size =3D 0; >> + rb->hdr_sent =3D false; >> + >> + return ret; >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_register); >> + >> +/** >> + * dibs_ring_unregister() - unregister a DIBS ring buffer >> + * @rb: pointer to the ring buffer to unregister >> + * >> + * Free the resources allocated by dibs_ring_register(). >> + * >> + * Return: 0 in case of success, an error code in case of failure >> + */ >> +int dibs_ring_unregister(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + int ret; >> + >> + rb->wr_ring_meta.meta_dmb_tok =3D 0; >> + rb->wr_ring_meta.buff_dmb_tok =3D 0; >> + rb->wr_ring_meta.size =3D 0; >> + rb->wr_ring_meta.head =3D 0; >> + rb->wr_ring_meta.tail =3D 0; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + if (peer.meta_dmb_tok) { >> + ret =3D dibs_ring_send_hdr(rb, peer.meta_dmb_tok); >> + if (ret) >> + pr_warn("%s: failed to send header update: %d\n", >> + __func__, ret); > > I'm not sure a warning is a good level here. What if the peer > unregisters at the same time (e.g. local and remote using same ism > device, or client using dibs_loopback ?) ? maybe pr_debug or pr_info? Agreed, this is expected when both sides tear down at the same time. Will change it to pr_debug() in v2. >> + } >> + >> + ret =3D rb->dibs->ops->unregister_dmb(rb->dibs, &rb->dmb); >> + if (ret) >> + pr_warn("%s: failed to unregister payload dmb: %d\n", __func__, >> + ret); >> + >> + ret =3D rb->dibs->ops->unregister_dmb(rb->dibs, &rb->rd_meta_dmb); >> + if (ret) >> + pr_warn("%s: failed to unregister metadata dmb: %d\n", __func__, >> + ret); >> + >> + rb->dmb_registered =3D 0; >> + return ret; >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_unregister); >> + >> +/** >> + * dibs_ring_peer_replaced() - check if the peer has replaced its ring = buffer >> + * @rb: pointer to the ring buffer >> + * @peer: snapshot of the peer's metadata >> + * >> + * The fabric hands out a new dmb token for every registration, so a bu= ffer >> + * token that differs from the one this side is synchronized to means t= he peer >> + * ring this side writes into is a different one. >> + * >> + * Return: true if the peer ring has been replaced, false otherwise >> + */ >> +static bool dibs_ring_peer_replaced(struct dibs_ring_buffer *rb, >> + const struct dibs_ring_meta *peer) >> +{ >> + return peer->buff_dmb_tok !=3D rb->peer_buff_dmb_tok; >> +} >> + > > As mentioned above: Is it helpful/necessaryto compare against a snapshot? > > You are checking this in dibs_ring_cnt, dibs_ring_recv and > dibs_ring_space() Why in these functions? What were your criteria? > Why are the other functions (like dibs_ring_cnt_to_end()) safe wihtout > this check? > > >> +/** >> + * dibs_ring_sync_peer() - synchronize with a replaced peer ring buffer >> + * @rb: pointer to the ring buffer >> + * @peer: snapshot of the peer's metadata >> + * >> + * Restart both pointers: head is this side's write position in the pee= r's >> + * buffer, which is a new and empty one, and tail is this side's read p= osition >> + * in its own buffer, which the peer has started to write at offset 0 a= gain. >> + * Sends the updated metadata header to the peer and only then remember= s the >> + * new token, so that a failed send is retried on the next call. >> + * >> + * A peer that has torn its ring down is not synchronized to, it is onl= y >> + * forgotten - this side cannot send anything until the peer registers = again. >> + */ > > I'm not sure 'sync' is the best term. You reset the local read and write > pointers, becasue you assume the remote side has been replaced. > Thus it is fair to assume that its read and write pointers are also 0. (?= ) > > 'sync' sounds to me as if you could do it in a running system, but that > would break the accounting on the other side. Agreed, will drop the detection of a replaced ring in v2. A ring buffer is used for exactly one connection; to connect again, both sides register new rings and repeat the handshake, so there is no sync in a running system anymore. >> +static void dibs_ring_sync_peer(struct dibs_ring_buffer *rb, >> + const struct dibs_ring_meta *peer) >> +{ >> + if (!peer->buff_dmb_tok) { >> + rb->wr_ring_meta.tail =3D 0; >> + rb->peer_buff_dmb_tok =3D 0; >> + rb->hdr_sent =3D false; >> + return; >> + } >> + >> + rb->wr_ring_meta.head =3D 0; >> + rb->wr_ring_meta.tail =3D 0; >> + >> + if (dibs_ring_send_hdr(rb, peer->meta_dmb_tok)) { >> + pr_warn("%s(%s): failed to send header after peer restart\n", >> + __func__, rb->name); >> + return; >> + } >> + >> + rb->peer_buff_dmb_tok =3D peer->buff_dmb_tok; >> +} -- snip -- >> +/** >> + * dibs_ring_space() - query the ring buffer's free space for sending >> + * @rb: pointer to the ring buffer >> + * >> + * Get the free space in the ring buffer that can be used for sending d= ata. >> + * Automatically resynchronizes if the peer has replaced its ring. >> + * >> + * Return: the free space in the ring buffer > > Would be nice, if the The 'Return:' statements of the following > fucntions were in the same style > > Maybe: Return: Number of bytes that can be written into this rb Agreed, will change that in v2. >> + */ >> +size_t dibs_ring_space(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (dibs_ring_peer_replaced(rb, &peer)) >> + dibs_ring_sync_peer(rb, &peer); >> + >> + return __dibs_ring_space(rb, &peer); >> +} >> + >> +/** >> + * dibs_ring_space_to_end() - query the rb's space until the end of the= DMB >> + * @rb: pointer to the ring buffer >> + * >> + * Get the free space in the ring buffer until the end of the underlyin= g DMB. >> + * This is the amount of data that can be written without wrapping to t= he >> + * beginning of the DMB. >> + * >> + * Return: the free space in the ring buffer until the end of the DMB. > > Maybe: Return: Number of consecutive writable bytes > >> + */ >> +size_t dibs_ring_space_to_end(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + return __dibs_ring_space_to_end(rb, &peer); >> +} >> + >> +/** >> + * dibs_ring_cnt() - query the ring buffer's fill state >> + * @rb: pointer to the ring buffer >> + * >> + * Get the number of bytes available in the ring buffer for reading. >> + * Automatically resynchronizes if the peer has replaced its ring. >> + * >> + * Return: fill state of the ring buffer > > Maybe: Number of bytes that can be read from this rb > >> + */ >> +u32 dibs_ring_cnt(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (dibs_ring_peer_replaced(rb, &peer)) >> + dibs_ring_sync_peer(rb, &peer); >> + >> + return __dibs_ring_cnt(rb, &peer); >> +} >> + >> +/** >> + * dibs_ring_cnt_to_end() - query the number of bytes in rb before wrap >> + * @rb: pointer to the ring buffer >> + * >> + * Get the number of bytes that can be read from the ring buffer withou= t >> + * wrapping the tail pointer. >> + * >> + * Return: number of consecutive readable bytes >> + */ >> +u32 dibs_ring_cnt_to_end(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + return __dibs_ring_cnt_to_end(rb, &peer); >> +} >> + >> +/** >> + * dibs_ring_set_rdmb_tok() - complete ring buffer token exchange >> + * @rb: pointer to the ring buffer >> + * @rdmb_tok: remote peer's metadata DMB token >> + * >> + * Completes the ring buffer setup by storing the remote peer's metadat= a DMB >> + * token and sending our local metadata to the peer. Normally, DMB toke= ns are > > s/Normally, DMB tokens are/If available, DMB tokens are/g > >> + * read from rd_meta_dmb, but this creates a circular dependency during= initial >> + * setup (need the token to read the metadata that contains the token).= To break > > (need the remote meta dmb token to send my meta dmb token, so the remote > peer can send its meta dmb token) > > You don't really need the token to read. Right, thanks, will change that in v2. >> + * this, one peer must receive the remote token through an alternative = channel >> + * and call this function to send its own header as the initial handsha= ke step. >> + * After this, both peers can read each other's metadata. >> + */ > > Please describe the initial handshake in dibs.h in "Ring Buffer > Communication". Yes, will do. >> +void dibs_ring_set_rdmb_tok(struct dibs_ring_buffer *rb, u64 rdmb_tok) >> +{ >> + WRITE_ONCE(rb->rd_ring_meta->meta_dmb_tok, rdmb_tok); >> + dibs_ring_send_hdr(rb, rdmb_tok); >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_set_rdmb_tok); >> + >> +/** >> + * dibs_ring_send() - Send DIBS message to peer >> + * @rb: Ring buffer to send message to >> + * @msg: Message to send >> + * @size: size of the message to be sent >> + * @notify: if true, update the remote's ring header and generate an IR= Q after >> + * sending. >> + * >> + * Send a DIBS message to the peer device using the specified ring buff= er. >> + * >> + * Return: zero in case of success, an error code in case of failure >> + */ >> +int dibs_ring_send(struct dibs_ring_buffer *rb, void *msg, u16 size, >> + bool notify) >> +{ >> + struct dibs_ring_meta peer; >> + u32 old_head; >> + int ret; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (!dibs_ring_peer_ready(&peer)) >> + return -EAGAIN; >> + >> + if (__dibs_ring_space_to_end(rb, &peer) < size) >> + return -ENOSPC; > > ??? This is a ring buffer, why can't you write and wrap?? > How are you supposed to write and advance across the size?? > This is not described in the description. > > ok, I see pbd uses some weird padding jumps, so it never wraps inside a > message. But that should not be part of the ring_buffer design. dibs_ring_recv() hands out a record in place, without copying, so a record has to be contiguous; that is why dibs_ring_send() does not wrap inside one. The sender skips the rest of the buffer in a way the receiver recognizes, and how it does that is up to the layer above: the padding message is defined by the peer bus, not by the ring. Will doument this in v2. >> + >> + ret =3D rb->dibs->ops->move_data(rb->dibs, peer.buff_dmb_tok, 0, false= , >> + rb->wr_ring_meta.head, msg, size); >> + if (ret) >> + return ret; >> + >> + old_head =3D rb->wr_ring_meta.head; >> + rb->wr_ring_meta.head =3D (old_head + size) & (peer.size - 1); >> + >> + if (notify) { >> + ret =3D dibs_ring_send_hdr(rb, peer.meta_dmb_tok); >> + if (ret) { >> + rb->wr_ring_meta.head =3D old_head; >> + return ret; >> + } >> + } >> + >> + return 0; >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_send); >> + >> +/** >> + * dibs_ring_send_padding() - send padding data to ring buffer >> + * @rb: pointer to the ring buffer >> + * @size: number of bytes to send >> + * >> + * Advance the ring buffer's head pointer by size byte without actually >> + * writing any data into the data buffer. This can be used by higher le= vel >> + * APIs to send padding data which shall be ignored by the receiving pe= er. >> + * >> + * Return: zero in case of success, an error code in case of failure >> + */ >> +int dibs_ring_send_padding(struct dibs_ring_buffer *rb, u16 size) >> +{ >> + struct dibs_ring_meta peer; >> + u32 old_head; >> + int ret; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (!dibs_ring_peer_ready(&peer)) >> + return -EAGAIN; >> + >> + if (__dibs_ring_space(rb, &peer) < size) >> + return -ENOSPC; >> + >> + old_head =3D rb->wr_ring_meta.head; >> + rb->wr_ring_meta.head =3D (old_head + size) & (peer.size - 1); >> + >> + ret =3D dibs_ring_send_hdr(rb, peer.meta_dmb_tok); >> + if (ret) { >> + rb->wr_ring_meta.head =3D old_head; >> + return ret; >> + } >> + >> + return 0; >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_send_padding); >> + >> +/** >> + * dibs_ring_recv() - Receive message from DIBS ring buffer >> + * @rb: Ring buffer to get message from >> + * >> + * Get the next message from the ring buffer. After processing, the cal= ler must >> + * call dibs_ring_ack() to update the ring buffer. >> + * >> + * Return: pointer to the DIBS message or NULL if the ring buffer is em= pty > > s/DIBS/dibs/g > > What is a dibs message? Will fix in v2: ring functions talk about records; the message format belongs to the peer bus layer. >> + */ >> +void *dibs_ring_recv(struct dibs_ring_buffer *rb) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (unlikely(!rb->hdr_sent)) >> + dibs_ring_send_hdr(rb, peer.meta_dmb_tok); >> + >> + if (dibs_ring_peer_replaced(rb, &peer)) >> + dibs_ring_sync_peer(rb, &peer); >> + >> + if (!__dibs_ring_cnt(rb, &peer)) >> + return NULL; >> + >> + /* pairs with the peer filling the payload before publishing head */ >> + dma_rmb(); >> + >> + return rb->dmb.cpu_addr + rb->wr_ring_meta.tail; >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_recv); >> + >> +/** >> + * dibs_ring_ack() - Acknowledge received message >> + * @rb: Ring buffer to operate on >> + * >> + * Acknowledges the message received by dibs_ring_recv(). This means, t= he read >> + * pointer in the ring buffer header is incremented and the updated hea= der is >> + * sent to the remote side.=20 > > Imperative: Acknowledge the message...increment the read pointer and > send the updated header.. > > After calling dibs_ring_ack(), the message structure Will change that. >> + * returned by dibs_ring_recv() may be reused, so the driver must not u= se it > > s/driver/caller/g > >> + * anymore. >> + */ > > So you can only receive-ack one message after the other? > What if you receive 2 meessages and then ack? Probably a bad idea.. > You may want to point that out somewhere. Yes, one record at a time: each dibs_ring_recv() is followed by exactly one dibs_ring_ack(), and calling dibs_ring_recv() again without acknowledging returns the same record. >> +int dibs_ring_ack(struct dibs_ring_buffer *rb, size_t size) >> +{ >> + struct dibs_ring_meta peer; >> + >> + dibs_ring_peer_meta(rb, &peer); >> + >> + if (size > __dibs_ring_cnt(rb, &peer)) { >> + pr_warn("%s(%s, %lx): error: tail would overtake head\n", >> + __func__, rb->name, size); >> + return -EINVAL; >> + } >> + >> + rb->wr_ring_meta.tail =3D (rb->wr_ring_meta.tail + size) & >> + (rb->wr_ring_meta.size - 1); >> + >> + return dibs_ring_send_hdr(rb, peer.meta_dmb_tok); >> +} >> +EXPORT_SYMBOL_GPL(dibs_ring_ack); >> + >> static int __init dibs_init(void) >> { >> int rc; >> diff --git a/include/linux/dibs.h b/include/linux/dibs.h >> index 0c10c224bcca..30ae03484b58 100644 >> --- a/include/linux/dibs.h >> +++ b/include/linux/dibs.h >> @@ -67,6 +67,83 @@ struct dibs_dmb { >> dma_addr_t dma_addr; >> }; >> =20 >> +/* Ring Buffer Communication >> + * ------------------------- >> + * Based on the DMBs, DIBS provides a ring buffer mechanism for bi-dire= ctional >> + * communication between two peers. For each ring-buffer connection, tw= o DMBs >> + * are allocated per peer: >> + * - one DMB holding metadata about the ring and >> + * - one DMB holding the actual data to be transferred. >> + * >> + * Metadata consists of parameters like the DMB tokens, size, head poin= ter and >> + * tail pointer. Note that the metadata stored in the locally allocated >> + * metadata DMB does not necessarily corelate to the locally allocated = payload >> + * DMB. As illustrated below, each peer has access to one wr_ring_meta = DMB >> + * (the one the other peer has allocated) and one rd_ring_meta DMB (the= one >> + * it has allocated itself). This is because the locally allocated DMB = can only >> + * be written by the other peer and vice versa. >> + * >> + * Peer A Peer B >> + * --------------- --------------- >> + * wr_ring_meta ----------> rd_ring_meta >> + * rd_ring_meta <---------- wr_ring_meta >> + * send_buffer ----------> recv_buffer >> + * recv_buffer <---------- send_buffer >> + * >> + * Consider for example that peer A wants to send data to peer B. It fi= rst has >> + * to check how much data it previously has written (head pointer) and = how far >> + * peer B has read (tail pointer) and compare this to the size. Since f= or send >> + * operations head is updated by peer A, it is located in the wr_ring_m= eta DMB. >> + * Tail is updated by the receiving side, peer B in this case, so peer = A will >> + * find the current value in rd_ring_meta. The payload DMB was allocate= d by >> + * peer B, so peer B also updated the size - peer A therefore finds the= correct >> + * value in rd_ring_meta. >> + * > > > I understand what you are doing, but I think the naming wr_ring_meta and > rd_ring_meta is very misleading. Would something like local_ring_meta > and remote_ring_meta be better to understand? I'd like to keep the names, but I agree the comments didn't explain them, so I'll update the comments in v2. wr and rd say who writes the metadata: wr_ring_meta is written only by this side, rd_ring_meta is written only by the peer and read here. I went with that because neither struct describes a single ring. Each holds the head of the direction that side sends in, and the tail and size of the direction it receives in. With local/remote, I think it's too easy to read local_ring_mega as "the metadata of the local ring". > I guess you are saying the tail pointer fo a recv_buffer is in > wr_ring_meta together with the head pointer of the corresponding > send_buffer, right? Yes, all metadata this side changes goes into wr_ring_meta. So: - this side receives data on the receive ring and advances tail to inform the other side that the data was consumed. The tail of the receive ring must therefore be stored in wr_ring_meta. - this side sends data on the send ring and advances head to inform the other side that data is available. The head of the send ring must therefore also be stored in wr_ring meta. Consequently, tail in recv_ring_meta belongs to the send ring, head in recv_ring_meta belongs to the recv ring. >> + * Now peer a can write data into the send_buffer and update the head p= ointer >> + * in wr_ring_meta. Peer B then reads this head pointer from its rd_rin= g_meta >> + * DMB, process the data and update tail in its wr_ring_meta. >> + */ >> +struct dibs_ring_meta { >> + /* meta_dmb_tok - Token for the metadata dmb. */ >> + u64 meta_dmb_tok; > > > of this *_ring_meta or of the other *_ring_meta? > > >> + /* buff_dmb_tok - Token for the buffer dmb */ >> + u64 buff_dmb_tok; > > Of the send_buffer or of the recv_buffer? Both tokens are the tokens of the side that writes the metadata: meta_dmb_tok is its metadata DMB, buff_dmb_tok is its recv_buffer. So, in wr_ring_meta a peer writes the tokens of the buffers it has created, from rd_ring_meta a peer can read the tokens of the other peer. >> + /* size - size of the buffer for receiving data in number of byte */ >> + u32 size; >> + /* head - points to the head of the ring buffer, i.e., the element >> + * that will be written next >> + */ >> + u32 head; >> + /* tail - points to the end of the ring-buffer, i.e., the element >> + * that will be read next >> + */ >> + u32 tail; > > Iiuc, tail is the tail pointer of the OTHER ringbuffer, right? Yes, relative to head. In wr_ring_meta, tail and size are for this side's recv_buffer, head is for the peer's. In rd_ring_meta, it's the other way around. >> + /* reserved - keeps the struct at its natural size, must be zero */ >> + u32 reserved; >> +}; >> + >> +struct dibs_ring_buffer { >> + char name[256]; >> + struct dibs_dev *dibs; >> + int dmb_registered; >> + bool hdr_sent; > > Add a comment that this is the dmb for recv_buffer Agreed. >> + struct dibs_dmb dmb; >> + /* dmb for the ring metadata this side can read, peer can write */ >> + struct dibs_dmb rd_meta_dmb; >> + /* ring metadata this side can write, peer can read */ >> + struct dibs_ring_meta wr_ring_meta; >> + /* ring metadata this side can read, peer can write */ >> + struct dibs_ring_meta *rd_ring_meta; >> + /* size validated by dibs_pbd_msg_recv(), acked by dibs_pbd_msg_ack() = */ > > Is this msg_size really helpful??? Seems it is only used by pbd > functions in later patches. What is the intended mechanism? > Seems pbd uses it to remember the amount that should be ack'ed. Does > this make sense? What if a 2nd receive has overwritten it? > Are there any rules that need to be followed? > > I think for the ringbuffer functions, I'd prefer not to store this here. > Caller should knwo how many bytes to ack. Agreed, will remove that in v2, the caller passes the size to dibs_ring_ack(). >> + u32 msg_size; >> + /* buff_dmb_tok of the peer ring this side is synchronized to. The >> + * fabric hands out a new token for every registration, so a different >> + * token means the peer has replaced its ring and the pointers into it >> + * are stale. >> + */ >> + u64 peer_buff_dmb_tok; > > Naming? send_buffer_dmb_tok? To make it consistent with the drawing above= ? Gone in v2, together with the detection of a replaced ring. >> +}; >> + >> /* DIBS events >> * ----------- >> * Dibs devices can optionally notify dibs clients about events that ha= ppened >> @@ -432,6 +509,22 @@ static inline void *dibs_get_priv(struct dibs_dev *= dev, >> return dev->priv[client->id]; >> } >> =20 >> +int dibs_ring_register(char *name, struct dibs_ring_buffer *rb, >> + struct dibs_client *client, >> + struct dibs_dev *dibs, >> + uuid_t rgid, >> + u32 size); > > Does a ring have a state? > Is dibs_ring_ack only possible after dibs_ring_set_rdmb_tok? Yes, unregistered, registered, connected and peer gone. All functions can be called in any state and do nothing before the connection is established; dibs_ring_ack() only has an effecgt once a record was received. >> +int dibs_ring_unregister(struct dibs_ring_buffer *rb); >> +size_t dibs_ring_space(struct dibs_ring_buffer *rb); >> +size_t dibs_ring_space_to_end(struct dibs_ring_buffer *rb); >> +u32 dibs_ring_cnt(struct dibs_ring_buffer *rb); >> +u32 dibs_ring_cnt_to_end(struct dibs_ring_buffer *rb); > > naming? Is cnt the best name? This is how the circular buffers API calls it, I think it makes sense to use the same terms here. >> +void dibs_ring_set_rdmb_tok(struct dibs_ring_buffer *rb, u64 rdmb_tok); >> +int dibs_ring_send(struct dibs_ring_buffer *rb, void *msg, u16 size, bo= ol notify); >> +int dibs_ring_send_padding(struct dibs_ring_buffer *rb, u16 size); >> +void *dibs_ring_recv(struct dibs_ring_buffer *rb); >> +int dibs_ring_ack(struct dibs_ring_buffer *rb, size_t size); >> + >> /* ------- End of client-only functions ----------- */ >> =20 >> /* Functions to be called by dibs device drivers: >> @@ -461,4 +554,10 @@ int dibs_dev_add(struct dibs_dev *dibs); >> */ >> void dibs_dev_del(struct dibs_dev *dibs); >> =20 >> +struct dibs_msg_hdr { >> + u8 version; >> + u8 type; >> + u16 datalen; >> +} __packed; >> + >> #endif /* _DIBS_H */ >>=20 > > > Link: https://www.kernel.org/doc/html/v6.6/process/4.Coding.html [1] Thanks, Tobias