Subject: Re: [PATCH v5 3/4] bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter

From: Alexei Starovoitov <[email-masked]>
Date: Fri, 31 Jul 2015 09:43:45 -0700

On 7/31/15 4:59 AM, Kaixu Xia wrote:
> +static int check_func_limit(struct bpf_map **mapp, int func_id)
> +{
> +	struct bpf_map *map = *mapp;
> +
> +	if (map && map->map_type == BPF_MAP_TYPE_PROG_ARRAY &&
> +	    func_id != BPF_FUNC_tail_call)
> +		/* prog_array map type needs extra care:
> +		 * only allow to pass it into bpf_tail_call() for now.
> +		 * bpf_map_delete_elem() can be allowed in the future,
> +		 * while bpf_map_update_elem() must only be done via syscall
> +		 */
> +		return -EINVAL;
> +
> +	if (func_id == BPF_FUNC_tail_call &&
> +	    map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
> +		/* don't allow any other map type to be passed into
> +		 * bpf_tail_call()
> +		 */
> +		return -EINVAL;
> +
> +	if (map && map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
> +	    func_id != BPF_FUNC_perf_event_read)
> +		/* perf_event_array map type needs extra care:
> +		 * only allow to pass it into bpf_perf_event_read() for now.
> +		 * bpf_map_update/delete_elem() must only be done via syscall
> +		 */
> +		return -EINVAL;
> +
> +	if (func_id == BPF_FUNC_perf_event_read &&
> +	    map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
> +		/* don't allow any other map type to be passed into
> +		 * bpf_perf_event_read()
> +		 */
> +		return -EINVAL;

copy paste of comments is too much.
The whole thing probably can be better generalized with an array
{{BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call},
  {BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read},
}
and a loop?