lib/dynamic_queue_limits.c

Source file repositories/reference/linux-study-clean/lib/dynamic_queue_limits.c

File Facts

System
Linux kernel
Corpus path
lib/dynamic_queue_limits.c
Extension
.c
Size
6782 bytes
Lines
218
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * Dynamic byte queue limits.  See include/linux/dynamic_queue_limits.h
 *
 * Copyright (c) 2011, Tom Herbert <therbert@google.com>
 */
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/dynamic_queue_limits.h>
#include <linux/compiler.h>
#include <linux/export.h>
#include <trace/events/napi.h>

#define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
#define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)

static void dql_check_stall(struct dql *dql, unsigned short stall_thrs)
{
	unsigned long now;

	if (!stall_thrs)
		return;

	now = jiffies;
	/* Check for a potential stall */
	if (time_after_eq(now, dql->last_reap + stall_thrs)) {
		unsigned long hist_head, t, start, end;

		/* We are trying to detect a period of at least @stall_thrs
		 * jiffies without any Tx completions, but during first half
		 * of which some Tx was posted.
		 */
dqs_again:
		hist_head = READ_ONCE(dql->history_head);
		/* pairs with smp_wmb() in dql_queued() */
		smp_rmb();

		/* Get the previous entry in the ring buffer, which is the
		 * oldest sample.
		 */
		start = (hist_head - DQL_HIST_LEN + 1) * BITS_PER_LONG;

		/* Advance start to continue from the last reap time */
		if (time_before(start, dql->last_reap + 1))
			start = dql->last_reap + 1;

		/* Newest sample we should have already seen a completion for */
		end = hist_head * BITS_PER_LONG + (BITS_PER_LONG - 1);

		/* Shrink the search space to [start, (now - start_thrs/2)] if
		 * `end` is beyond the stall zone
		 */
		if (time_before(now, end + stall_thrs / 2))
			end = now - stall_thrs / 2;

		/* Search for the queued time in [t, end] */
		for (t = start; time_before_eq(t, end); t++)
			if (test_bit(t % (DQL_HIST_LEN * BITS_PER_LONG),
				     dql->history))
				break;

		/* Variable t contains the time of the queue */
		if (!time_before_eq(t, end))
			goto no_stall;

		/* The ring buffer was modified in the meantime, retry */
		if (hist_head != READ_ONCE(dql->history_head))
			goto dqs_again;

		dql->stall_cnt++;
		dql->stall_max = max_t(unsigned short, dql->stall_max, now - t);

		trace_dql_stall_detected(dql->stall_thrs, now - t,
					 dql->last_reap, dql->history_head,
					 now, dql->history);
	}
no_stall:
	dql->last_reap = now;
}

/* Records completed count and recalculates the queue limit */
void dql_completed(struct dql *dql, unsigned int count)
{
	unsigned int inprogress, prev_inprogress, limit;
	unsigned int ovlimit, completed, num_queued;
	unsigned short stall_thrs;
	bool all_prev_completed;

	num_queued = READ_ONCE(dql->num_queued);

Annotation

Implementation Notes