Re: Using sparse to catch invalid RCU dereferences?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Johannes Berg
Date: Wednesday, April 9, 2008 - 1:09 pm

> It might be.  There are a number of places where it is legal to access

Ok, that I understand, but sparse always treats NULL specially anyway.


Hm? I thought that's in the current tree.


Ah, ok.

()

That I don't understand. Well, I do understand that omitting
rcu_dereference() is ok, but it seems to me that the memory and compiler
barrier in rcu_assign_pointer() is actually needed.

I've been playing a bit, see below for my play rcupdate.h and test.c
test program.

Unfortunately, sparse doesn't have the ability to declare
"=EF=BB=BF__attribute__((force_bitwise)) typeof(p)" or even
"=EF=BB=BF__attribute__((force)) typeof(p)" which makes this force more tha=
n
necessary and causes it to not catch when incompatible pointers are
used. gcc notices that because I only do a cast at all for sparse, but
that doesn't help, since e.g. list_for_each_entry_rcu() requires that
the correct type is returned. So without sparse supporting the latter
notation, we don't stand a chance.

Also, I wouldn't know how to declare that an array or so needs
rcu-access to the members.

johannes


rcupdate.h:

#define USE_BITWISE

#ifdef __CHECKER__
#ifdef USE_BITWISE
#define __rcu __attribute__((bitwise))
#define __force_rcu_cast(p) (*((__attribute__((force)) void **)&(p)))
// would like instead:
//#define __force_rcu_cast(p) ((__attribute__((force_bitwise)) typeof(p)) (=
p))
#else /* not bitwise */
#define __rcu __attribute__((address_space(3)))
#define __force_rcu_cast(p) (*((__attribute__((force)) void **)&(p)))
// would like instead:
//#define __force_rcu_cast(p) ((__attribute__((force_address_space)) typeof=
(p)) (p))
#endif

#else /* not checker */
#define __rcu
#define __force_rcu_cast(p) (p)
#endif

#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))

#define rcu_dereference(p)     ({ \
				typeof(p) _________p1 =3D ACCESS_ONCE(p); \
				smp_read_barrier_depends(); \
				__force_rcu_cast(_________p1); \
				})

/**
 * rcu_fetch - fetch an RCU-protected pointer in the update-locked
 * critical section.
 *
 * This macro exists for documentation and code checking purposes.
 */
#define rcu_fetch(p)     __force_rcu_cast(p);

#define rcu_assign_pointer(p, v) \
	({ \
		if (!__builtin_constant_p(v) || \
		    ((v) !=3D NULL)) \
			smp_wmb(); \
		__force_rcu_cast(p) =3D (v); \
	})


test.c:

#include <stdlib.h>
#include "rcupdate.h"

/* my rcu protected variables */
static unsigned int __rcu *prot;
static unsigned int __rcu *prot_same;
static unsigned char __rcu *prot2;

// dummies
static smp_read_barrier_depends(void) {}
static smp_wmb(void) {}

int main(void)
{
	unsigned int *tmp;

	// no warnings from sparse due to forced cast
	rcu_assign_pointer(prot, tmp);
	// but gcc warns
	rcu_assign_pointer(prot2, tmp);

	// no warnings
	rcu_assign_pointer(prot, NULL);
	rcu_assign_pointer(prot2, NULL);

	// no warnings
	prot =3D NULL;
	prot2 =3D NULL;

	// no warnings from sparse due to forced cast
	tmp =3D rcu_dereference(prot);
	// but gcc warns
	tmp =3D rcu_dereference(prot2);

	/* now within locked section rcu_dereference isn't required */

	// no warnings from sparse due to forced cast
	tmp =3D rcu_fetch(prot);
	// but gcc warns
	tmp =3D rcu_fetch(prot2);

	/* not caught with address_space, but is caught with bitwise */
	prot =3D prot_same;
}
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Using sparse to catch invalid RCU dereferences?, Johannes Berg, (Mon Apr 7, 3:04 pm)
Re: Using sparse to catch invalid RCU dereferences?, Paul E. McKenney, (Tue Apr 8, 8:52 am)
Re: Using sparse to catch invalid RCU dereferences?, Johannes Berg, (Tue Apr 8, 9:09 am)
Re: Using sparse to catch invalid RCU dereferences?, Paul E. McKenney, (Tue Apr 8, 10:24 am)
Re: Using sparse to catch invalid RCU dereferences?, Johannes Berg, (Wed Apr 9, 1:09 pm)
Re: Using sparse to catch invalid RCU dereferences?, Paul E. McKenney, (Thu Apr 10, 3:32 pm)
Re: Using sparse to catch invalid RCU dereferences?, Peter Zijlstra, (Fri Apr 11, 11:18 am)
Re: Using sparse to catch invalid RCU dereferences?, Paul E. McKenney, (Fri Apr 11, 11:43 am)
Re: Using sparse to catch invalid RCU dereferences?, Johannes Berg, (Fri Apr 11, 1:54 pm)