eBPF-based security platform
coccinelle: let bpf_map_rmw report every update it names
Daniel Borkmann9238e7bd9a21
diff --git a/Documentation/upatches/write/memory-consistency/hazards.rst b/Documentation/upatches/write/memory-consistency/hazards.rstindex 7bde4f91..a74b09b1 100644--- a/Documentation/upatches/write/memory-consistency/hazards.rst+++ b/Documentation/upatches/write/memory-consistency/hazards.rst@@ -153,10 +153,11 @@ of a map that is not per-CPU, and for inode, socket and cgroup storage,which hooks on different CPUs reach for the same inode, socket orcgroup. Update such a value with ``__sync_fetch_and_add()``,``__sync_fetch_and_or()`` and their kin, which do it in one instruction.-``bpf_map_rmw`` refuses the plain forms. A global variable is a value-too, of the one array map that holds the object's ``.bss`` or ``.data``,-which every CPU shares; ``bpf_map_rmw`` does not see it, so update it-the same way.+``bpf_map_rmw`` refuses the plain forms: ``++``, ``--``, ``+=``, ``-=``,+``|=``, ``&=``, ``x = x + n`` and ``x = x - n``. A global variable is a+value too, of the one array map that holds the object's ``.bss`` or+``.data``, which every CPU shares; ``bpf_map_rmw`` does not see it, so+update it the same way.A per-CPU counter needs an atomic only where a run can come in betweenon the same CPU: in a program the kernel does not guard, as listeddiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 8dd5a419..bf111c87 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -312,6 +312,41 @@ struct meta *flags_reassigned(struct sock *sk)m->flags |= 1;return m;}++void count_add(void)+{+ __u32 zero = 0;+ __u64 *count = bpf_map_lookup_elem(&hits, &zero);++ if (count)+ *count += 1;+}++void count_sum(void)+{+ __u32 zero = 0;+ __u64 *count = bpf_map_lookup_elem(&hits, &zero);++ if (count)+ *count = *count + 2;+}++void count_percpu_add(void)+{+ __u32 zero = 0;+ __u64 *count = bpf_map_lookup_elem(&cpu_hits, &zero);++ if (count)+ *count += 1;+}++void flags_inode(struct inode *inode)+{+ struct meta *m = bpf_inode_storage_get(&inodes, inode, 0, 0);++ if (m)+ m->flags = m->flags + 1;+}EOF}@@ -330,7 +365,7 @@ test_expect_success 'each BPF rule reports its sample and spares the safe form'grep -q "^$r: samples.bpf.c:" "$W/err" || exit 1done &&grep -q "6 of 6 rules matched" "$W/err" &&- [ "$(grep -c "^bpf_[a-z_]*: samples.bpf.c:" "$W/err")" = 7 ]+ [ "$(grep -c "^bpf_[a-z_]*: samples.bpf.c:" "$W/err")" = 10 ]'test_expect_success 'the tree passes' 'diff --git a/testing/coccinelle/bpf_map_rmw.cocci b/testing/coccinelle/bpf_map_rmw.cocciindex d462aa1a..d5322b0c 100644--- a/testing/coccinelle/bpf_map_rmw.cocci+++ b/testing/coccinelle/bpf_map_rmw.cocci@@ -35,7 +35,7 @@ def shared_maps(path):shared[path] = namesreturn shared[path]-@r depends on report@+@r depends on report exists@attribute name SEC;identifier m, p, f;expression k, e, e2;@@ -65,14 +65,24 @@ p->f@pos &= e|(*p@pos)--|-*p@pos += e+ *p@pos += e|-*p@pos -= e+ *p@pos -= e|-*p@pos |= e+ *p@pos |= e+|+ *p@pos &= e+|+ *p@pos = *p + e+|+ *p@pos = *p - e+|+p->f@pos = p->f + e+|+p->f@pos = p->f - e)-@s depends on report@+@s depends on report exists@identifier get = {bpf_inode_storage_get, bpf_sk_storage_get,bpf_cgrp_storage_get};expression list args;@@ -99,6 +109,10 @@ p->f@pos -= ep->f@pos |= e|p->f@pos &= e+|+p->f@pos = p->f + e+|+p->f@pos = p->f - e)@script:python depends on report@