From: Stefan Metzmacher Date: Fri, 17 Oct 2025 13:15:05 +0000 (+0200) Subject: smb: smbdirect: introduce smbdirect_connection_grant_recv_credits() X-Git-Tag: v7.1-rc1~128^2~107 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=3514195010828078173dd0608ba04340b718892f;p=thirdparty%2Fkernel%2Flinux.git smb: smbdirect: introduce smbdirect_connection_grant_recv_credits() This is basically a copy of manage_credits_prior_sending() in the client and the server. It will replace both versions in future. Cc: Steve French Cc: Tom Talpey Cc: Long Li Cc: Namjae Jeon Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Signed-off-by: Stefan Metzmacher Acked-by: Namjae Jeon Signed-off-by: Steve French --- diff --git a/fs/smb/common/smbdirect/smbdirect_connection.c b/fs/smb/common/smbdirect/smbdirect_connection.c index 0eebc7f336f5a..40d830c58f195 100644 --- a/fs/smb/common/smbdirect/smbdirect_connection.c +++ b/fs/smb/common/smbdirect/smbdirect_connection.c @@ -688,6 +688,44 @@ static void smbdirect_connection_idle_timer_work(struct work_struct *work) queue_work(sc->workqueue, &sc->idle.immediate_work); } +__maybe_unused /* this is temporary while this file is included in others */ +static u16 smbdirect_connection_grant_recv_credits(struct smbdirect_socket *sc) +{ + int missing; + int available; + int new_credits; + + if (atomic_read(&sc->recv_io.credits.count) >= sc->recv_io.credits.target) + return 0; + + missing = (int)sc->recv_io.credits.target - atomic_read(&sc->recv_io.credits.count); + available = atomic_xchg(&sc->recv_io.credits.available, 0); + new_credits = min3((int)U16_MAX, missing, available); + if (new_credits <= 0) { + /* + * If credits are available, but not granted + * we need to re-add them again. + */ + if (available) + atomic_add(available, &sc->recv_io.credits.available); + return 0; + } + + if (new_credits < available) { + /* + * Readd the remaining available again. + */ + available -= new_credits; + atomic_add(available, &sc->recv_io.credits.available); + } + + /* + * Remember we granted the credits + */ + atomic_add(new_credits, &sc->recv_io.credits.count); + return new_credits; +} + __maybe_unused /* this is temporary while this file is included in others */ static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc) {