Re: [PATCH v7 19/36] function_graph: Implement fgraph_reserve_data() and fgraph_retrieve_data()

From: Steven Rostedt
Date: Wed Feb 14 2024 - 13:58:39 EST


On Wed, 7 Feb 2024 00:11:01 +0900
"Masami Hiramatsu (Google)" <mhiramat@xxxxxxxxxx> wrote:

> From: Ste
> +/**
> + * fgraph_reserve_data - Reserve storage on the task's ret_stack
> + * @idx: The index of fgraph_array
> + * @size_bytes: The size in bytes to reserve
> + *
> + * Reserves space of up to FGRAPH_MAX_DATA_SIZE bytes on the
> + * task's ret_stack shadow stack, for a given fgraph_ops during
> + * the entryfunc() call. If entryfunc() returns zero, the storage
> + * is discarded. An entryfunc() can only call this once per iteration.
> + * The fgraph_ops retfunc() can retrieve this stored data with
> + * fgraph_retrieve_data().
> + *
> + * Returns: On success, a pointer to the data on the stack.
> + * Otherwise, NULL if there's not enough space left on the
> + * ret_stack for the data, or if fgraph_reserve_data() was called
> + * more than once for a single entryfunc() call.
> + */
> +void *fgraph_reserve_data(int idx, int size_bytes)
> +{
> + unsigned long val;
> + void *data;
> + int curr_ret_stack = current->curr_ret_stack;
> + int data_size;
> +
> + if (size_bytes > FGRAPH_MAX_DATA_SIZE)
> + return NULL;
> +
> + /* Convert to number of longs + data word */
> + data_size = DIV_ROUND_UP(size_bytes, sizeof(long));

Hmm, the above is a fast path. I wonder if we should add a patch to make that into:

if (unlikely(size_bytes & (sizeof(long) - 1)))
data_size = DIV_ROUND_UP(size_bytes, sizeof(long));
else
data_size = size_bytes >> (sizeof(long) == 4 ? 2 : 3);

to keep from doing the division.

-- Steve

> +
> + val = get_fgraph_entry(current, curr_ret_stack - 1);
> + data = &current->ret_stack[curr_ret_stack];
> +
> + curr_ret_stack += data_size + 1;
> + if (unlikely(curr_ret_stack >= SHADOW_STACK_MAX_INDEX))
> + return NULL;
> +
> + val = make_fgraph_data(idx, data_size, __get_index(val) + data_size + 1);
> +
> + /* Set the last word to be reserved */
> + current->ret_stack[curr_ret_stack - 1] = val;
> +
> + /* Make sure interrupts see this */
> + barrier();
> + current->curr_ret_stack = curr_ret_stack;
> + /* Again sync with interrupts, and reset reserve */
> + current->ret_stack[curr_ret_stack - 1] = val;
> +
> + return data;
> +}
> +