Skip to contentSkip to conversation

kernel/bpf/cgroup.c:710:10-17: WARNING: ERR_CAST can be used with anchor_link

PATCH kernel test robot 2 participants 2 messages Jul 20, 2025

lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
head:   d086c886ceb9f59dea6c3a9dae7eb89e780a20c9
commit: 1209339844601ec1766f4ff430673fbcfe42bb51 [1023/11307] bpf: Implement mprog API on top of existing cgroup progs
config: alpha-randconfig-r054-20250720 (https://download.01.org/0day-ci/archive/20250720/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 13.4.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

cocci warnings: (new ones prefixed by >>)

>> kernel/bpf/cgroup.c:710:10-17: WARNING: ERR_CAST can be used with anchor_link
>> kernel/bpf/cgroup.c:714:10-17: WARNING: ERR_CAST can be used with anchor_prog

vim +710 kernel/bpf/cgroup.c

682
683 static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_prog *prog,
684 struct bpf_cgroup_link *link, u32 flags, u32 id_or_fd)
685 {
686 bool is_link = flags & BPF_F_LINK, is_id = flags & BPF_F_ID;
687 struct bpf_prog_list *pltmp, *pl = ERR_PTR(-EINVAL);
688 bool preorder = flags & BPF_F_PREORDER;
689 struct bpf_link *anchor_link = NULL;
690 struct bpf_prog *anchor_prog = NULL;
691 bool is_before, is_after;
692
693 is_before = flags & BPF_F_BEFORE;
694 is_after = flags & BPF_F_AFTER;
695 if (is_link || is_id || id_or_fd) {
696 /* flags must have either BPF_F_BEFORE or BPF_F_AFTER */
697 if (is_before == is_after)
698 return ERR_PTR(-EINVAL);
699 if ((is_link && !link) || (!is_link && !prog))
700 return ERR_PTR(-EINVAL);
701 } else if (!hlist_empty(progs)) {
702 /* flags cannot have both BPF_F_BEFORE and BPF_F_AFTER */
703 if (is_before && is_after)
704 return ERR_PTR(-EINVAL);
705 }
706
707 if (is_link) {
708 anchor_link = bpf_get_anchor_link(flags, id_or_fd);
709 if (IS_ERR(anchor_link))

 > 710				return ERR_PTR(PTR_ERR(anchor_link));
   711		} else if (is_id || id_or_fd) {
   712			anchor_prog = bpf_get_anchor_prog(flags, id_or_fd);
   713			if (IS_ERR(anchor_prog))
 > 714				return ERR_PTR(PTR_ERR(anchor_prog));

715 }
716
717 if (!anchor_prog && !anchor_link) {
718 /* if there is no anchor_prog/anchor_link, then BPF_F_PREORDER
719 * doesn't matter since either prepend or append to a combined
720 * list of progs will end up with correct result.
721 */
722 hlist_for_each_entry(pltmp, progs, node) {
723 if (is_before)
724 return pltmp;
725 if (pltmp->node.next)
726 continue;
727 return pltmp;
728 }
729 return NULL;
730 }
731
732 hlist_for_each_entry(pltmp, progs, node) {
733 if ((anchor_prog && anchor_prog == pltmp->prog) ||
734 (anchor_link && anchor_link == &pltmp->link->link)) {
735 if (!!(pltmp->flags & BPF_F_PREORDER) != preorder)
736 goto out;
737 pl = pltmp;
738 goto out;
739 }
740 }
741
742 pl = ERR_PTR(-ENOENT);
743 out:
744 if (anchor_link)
745 bpf_link_put(anchor_link);
746 else
747 bpf_prog_put(anchor_prog);
748 return pl;
749 }
750

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw
Quoted 6 lines · kernel test robot
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
head:   d086c886ceb9f59dea6c3a9dae7eb89e780a20c9
commit: 1209339844601ec1766f4ff430673fbcfe42bb51 [1023/11307] bpf: Implement mprog API on top of existing cgroup progs
config: alpha-randconfig-r054-20250720 (https://download.01.org/0day-ci/archive/20250720/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 13.4.0
Quoted 8 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

cocci warnings: (new ones prefixed by >>)

Quoted 2 lines
Quoted 2 lines

kernel/bpf/cgroup.c:710:10-17: WARNING: ERR_CAST can be used with anchor_link kernel/bpf/cgroup.c:714:10-17: WARNING: ERR_CAST can be used with anchor_prog

I don't think we have correctness here. But, indeed ERR_CAST is better than ERR_PTR(PTR_ERR(anchor_link). I will send a patch to upstream for this.

Quoted 72 lines

vim +710 kernel/bpf/cgroup.c

    682	
    683	static struct bpf_prog_list *get_prog_list(struct hlist_head *progs, struct bpf_prog *prog,
    684						   struct bpf_cgroup_link *link, u32 flags, u32 id_or_fd)
    685	{
    686		bool is_link = flags & BPF_F_LINK, is_id = flags & BPF_F_ID;
    687		struct bpf_prog_list *pltmp, *pl = ERR_PTR(-EINVAL);
    688		bool preorder = flags & BPF_F_PREORDER;
    689		struct bpf_link *anchor_link = NULL;
    690		struct bpf_prog *anchor_prog = NULL;
    691		bool is_before, is_after;
    692	
    693		is_before = flags & BPF_F_BEFORE;
    694		is_after = flags & BPF_F_AFTER;
    695		if (is_link || is_id || id_or_fd) {
    696			/* flags must have either BPF_F_BEFORE or BPF_F_AFTER */
    697			if (is_before == is_after)
    698				return ERR_PTR(-EINVAL);
    699			if ((is_link && !link) || (!is_link && !prog))
    700				return ERR_PTR(-EINVAL);
    701		} else if (!hlist_empty(progs)) {
    702			/* flags cannot have both BPF_F_BEFORE and BPF_F_AFTER */
    703			if (is_before && is_after)
    704				return ERR_PTR(-EINVAL);
    705		}
    706	
    707		if (is_link) {
    708			anchor_link = bpf_get_anchor_link(flags, id_or_fd);
    709			if (IS_ERR(anchor_link))
Quoted 1 line

710 return ERR_PTR(PTR_ERR(anchor_link));

    711		} else if (is_id || id_or_fd) {
    712			anchor_prog = bpf_get_anchor_prog(flags, id_or_fd);
    713			if (IS_ERR(anchor_prog))
Quoted 1 line

714 return ERR_PTR(PTR_ERR(anchor_prog));

    715		}
    716	
    717		if (!anchor_prog && !anchor_link) {
    718			/* if there is no anchor_prog/anchor_link, then BPF_F_PREORDER
    719			 * doesn't matter since either prepend or append to a combined
    720			 * list of progs will end up with correct result.
    721			 */
    722			hlist_for_each_entry(pltmp, progs, node) {
    723				if (is_before)
    724					return pltmp;
    725				if (pltmp->node.next)
    726					continue;
    727				return pltmp;
    728			}
    729			return NULL;
    730		}
    731	
    732		hlist_for_each_entry(pltmp, progs, node) {
    733			if ((anchor_prog && anchor_prog == pltmp->prog) ||
    734			    (anchor_link && anchor_link == &pltmp->link->link)) {
    735				if (!!(pltmp->flags & BPF_F_PREORDER) != preorder)
    736					goto out;
    737				pl = pltmp;
    738				goto out;
    739			}
    740		}
    741	
    742		pl = ERR_PTR(-ENOENT);
    743	out:
    744		if (anchor_link)
    745			bpf_link_put(anchor_link);
    746		else
    747			bpf_prog_put(anchor_prog);
    748		return pl;
    749	}
    750