refcount: introduce refcount_is_one() helper function
PATCH 1/1 refcount: introduce refcount_is_one() helper function
There are many cases where it is necessary to determine if refcount is one, introduce refcount_is_one() helper function for these cases.
--- include/linux/refcount.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/refcount.h +5 -0
@@ -147,6 +147,11 @@ static inline unsigned int refcount_read(const refcount_t *r)return atomic_read (&r->refs); } +static inline bool refcount_is_one (const refcount_t *r) +{ +return refcount_read (r) ==1 ; +} +static inline __must_check bool __refcount_add_not_zero (int i,refcount_t *r,int *oldp) {int old =refcount_read (r);
Signature · 1 line
2.32.0
Peter Zijlstra
maintainerQuoted 2 lines · Yajun Deng
There are many cases where it is necessary to determine if refcount is one, introduce
refcount_is_one()helper function for these cases.
Give me one that is not racy?
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Yajun Deng
There are many cases where it is necessary to determine if refcount is one, introduce
refcount_is_one()helper function for these cases.Give me one that is not racy?
The following determine refcount is one, 35 count. linux (next-20211117) $ grep "refcount_read.*== 1" ./ -RnI./tools/lib/perf/mmap.c: 101: if (refcount_read(&map->refcnt) == 1 && perf_mmap__empty(map))
./tools/perf/tests/cpumap.c: 72: TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
./tools/perf/tests/thread-map.c: 42: refcount_read(&map->refcnt) == 1);
./tools/perf/tests/thread-map.c: 57: refcount_read(&map->refcnt) == 1);
./tools/perf/tests/thread-map.c: 84: refcount_read(&threads->refcnt) == 1);
./tools/perf/tests/maps.c: 28: TEST_ASSERT_VAL("wrong map refcnt", refcount_read(&map->refcnt) == 1);
./include/linux/skbuff.h: 1078: if (likely(refcount_read(&skb->users) == 1))
./include/net/sock.h: 726: WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
./include/net/sock.h: 748: WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
./net/core/neighbour.c: 214: if (refcount_read(&n->refcnt) == 1) {
./net/core/neighbour.c: 264: if (refcount_read(&n->refcnt) == 1) {
./net/core/neighbour.c: 965: if (refcount_read(&n->refcnt) == 1 &&
./net/core/dev.c: 3017: if (likely(refcount_read(&skb->users) == 1)) {
./net/core/skbuff.c: 710: if (refcount_read(&fclones->fclone_ref) == 1)
./net/core/skbuff.c: 1514: refcount_read(&fclones->fclone_ref) == 1) {
./net/core/skbuff.c: 6278: if (refcount_read(&old->refcnt) == 1)
./net/core/skbuff.c: 6402: refcount_read(&ext->refcnt) == 1) {
./net/core/skbuff.c: 6417: if (refcount_read(&ext->refcnt) == 1)./net/netlink/af_netlink.c: 614: WARN_ON(refcount_read(&sk->sk_refcnt) == 1);./net/tipc/socket.c: 3032: WARN_ON(refcount_read(&sk->sk_refcnt) == 1);./net/netfilter/ipvs/ip_vs_conn.c: 480: (refcount_read(&cp->refcnt) == 1) &&./net/sctp/auth.c:998: refcount_read(&key->refcnt) == 1) {./drivers/tty/serial/sb1250-duart.c: 698: if (refcount_read(&duart->map_guard) == 1) {./drivers/infiniband/core/cm.c: 1048: WARN_ON(refcount_read(&cm_id_priv->refcount) == 1);./drivers/infiniband/hw/irdma/utils.c:513: refcount_read(&cqp_request->refcnt) == 1, 1000);./drivers/crypto/caam/qi.c: 306: if (refcount_read(&drv_ctx->refcnt) == 1)./drivers/net/ethernet/mellanox/mlx5/core/fs_core.c:1633: if (refcount_read(&handle->rule[i]->node.refcount) == 1) {
./drivers/net/vxlan.c: 1537: if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1) ./drivers/net/vxlan.c: 1541: if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
./kernel/events/core.c: 12861: wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);./fs/nfs/nfs4proc.c: 9127: if (refcount_read(&clp->cl_count) == 1)./fs/lockd/mon.c: 195: if (refcount_read(&nsm->sm_count) == 1./fs/btrfs/transaction.c: 2061: ASSERT(refcount_read(&trans->use_count) == 1);./fs/btrfs/block-group.c: 3960: ASSERT(refcount_read(&block_group->refs) == 1);./fs/afs/write.c: 921: if (refcount_read(&wbk->usage) == 1)
The following determine refcount isn't one, 19 count. linux (next-20211117) $ grep "refcount_read.*!= 1" ./ -RnI./include/linux/skbuff.h: 1762: return refcount_read(&skb->users) != 1;./include/net/sock.h: 1297: if (refcount_read(&sk->sk_refcnt) != 1)
./crypto/algapi.c: 461: BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
./crypto/algapi.c: 560: BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
./net/sunrpc/auth.c: 690: if (refcount_read(&cred->cr_count) != 1 ||
./net/llc/llc_conn.c: 977: if (refcount_read(&sk->sk_refcnt) != 1) {
./net/ceph/osd_client.c: 5297: WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
./net/ipv6/ip6_fib.c: 1037: if (refcount_read(&rt->fib6_ref) != 1) {
./net/core/neighbour.c: 349: if (refcount_read(&n->refcnt) != 1) {
./net/core/pktgen.c: 3430: while (refcount_read(&(pkt_dev->skb->users)) != 1) {
./drivers/crypto/marvell/octeontx/otx_cptvf_algs.c: 1584: if (refcount_read(&otx_cpt_skciphers[i].base.cra_refcnt) != 1)
./drivers/crypto/marvell/octeontx/otx_cptvf_algs.c: 1587: if (refcount_read(&otx_cpt_aeads[i].base.cra_refcnt) != 1)./drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c:406: refcount_read(&lkey_id->refcnt) != 1, lkey_id->id,./drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c:435: refcount_read(&lkey_id->refcnt) != 1,./drivers/net/ethernet/mellanox/mlxsw/spectrum_acl_atcam.c:464: refcount_read(&lkey_id->refcnt) != 1, lkey_id->id,./drivers/block/nbd.c: 2541: if (refcount_read(&nbd->refs) != 1)./fs/exec.c: 1182: if (refcount_read(&oldsighand->count) != 1) {./fs/btrfs/tests/extent-map-tests.c: 24: if (refcount_read(&em->refs) != 1) {./arch/s390/mm/extmem.c: 472: if (refcount_read(&seg->ref_count) != 1) {
Peter Zijlstra
maintainerQuoted 10 lines · On Thu, Nov 18, 2021 at 08:12:56AM +0000, [email protected] wrote:
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Yajun Deng
There are many cases where it is necessary to determine if refcount is one, introduce
refcount_is_one()helper function for these cases.Give me one that is not racy?
The following determine refcount is one, 35 count.
Very good, now get me one that isn't broken :-)
Quoted 6 lines · Peter Zijlstra
Quoted 4 lines · On Thu, Nov 18, 2021 at 08:12:56AM +0000, [email protected] wrote:
November 18, 2021 3:42 PM, "Peter Zijlstra" <[email protected]> wrote:
On Thu, Nov 18, 2021 at 11:53:28AM +0800, Yajun Deng wrote:
Quoted 8 lines
Quoted 6 lines
There are many cases where it is necessary to determine if refcount is one, introduce
refcount_is_one()helper function for these cases.Give me one that is not racy?
The following determine refcount is one, 35 count.
Very good, now get me one that isn't broken :-)
Sorry, I didn't understand what is the 'isn't broken'。
Peter Zijlstra
maintainerQuoted 11 lines · On Thu, Nov 18, 2021 at 10:34:44AM +0000, [email protected] wrote:
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · On Thu, Nov 18, 2021 at 08:12:56AM +0000, [email protected] wrote:
November 18, 2021 3:42 PM, "Peter Zijlstra" <[email protected]> wrote:
On Thu, Nov 18, 2021 at 11:53:28AM +0800, Yajun Deng wrote:
There are many cases where it is necessary to determine if refcount is one, introduce
refcount_is_one()helper function for these cases.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Give me one that is not racy?
The following determine refcount is one, 35 count.
Very good, now get me one that isn't broken :-)
Sorry, I didn't understand what is the 'isn't broken'。
What's the value of refcount_read() given that at any moment a concurrent refcount_{inc,dec}() can happen?
If you can't know the current value (per the above) then what's the value of knowing it was one some time ago?
Fundamentally using refcount_read() in control flow is broken, it's a very bad anti-pattern.