Skip to contentSkip to conversation

boot time variable

DISCUSSION Tobin C. Harding 3 participants 4 messages Oct 9, 2017 – Oct 10, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

I would like to create a boot time variable i.e a variable that is set once at boot time. Variable does not need to be globally accessible. (actually I am using two variables).

Could any one point me to examples of this already intree please?

I have tried the following but it has a race condition on the zero check and assignment of randval/oddval.

/* Maps a pointer to a unique identifier. */
static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)

{
	long hashval;
	static long randval = 0;
	static long oddval = 0;

	if (oddval == 0 && randval == 0) {
		randval = get_random_long();
		oddval = get_random_odd_long();
	}

	hashval = ptr_obfuscate((unsigned long)ptr, randval, oddval);
	spec.base = 16;

	return number(buf, end, hashval, spec);
}

And the compiler doesn't like

        static long randval = get_random_long();
	static long oddval = get_random_odd_long();

I thought of wrapping oddval/randval in a struct and protecting it with a lock but I don't know how/where to initialize the lock in a race free manner?

Any tips or pointers please?

thanks,
Tobin.

#1 lore raw
Quoted 4 lines · Tobin C. Harding

Hi,

I would like to create a boot time variable i.e a variable that is set once at boot time. Variable does not need to be globally accessible. (actually I am using two variables).

static foo = 42;

should be all you need, right?

If not, what exactly do you mean by "boot time variable"?

Quoted 6 lines

Could any one point me to examples of this already intree please?

I have tried the following but it has a race condition on the zero check and assignment of randval/oddval.

/* Maps a pointer to a unique identifier. */
static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)

Quoted 15 lines
{
	long hashval;
	static long randval = 0;
	static long oddval = 0;

	if (oddval == 0 && randval == 0) {
		randval = get_random_long();
		oddval = get_random_odd_long();
	}

	hashval = ptr_obfuscate((unsigned long)ptr, randval, oddval);
	spec.base = 16;

	return number(buf, end, hashval, spec);
}

What's wrong with this code?

Quoted 4 lines

And the compiler doesn't like

        static long randval = get_random_long();
	static long oddval = get_random_odd_long();

Yeah, that will not work, static initializers are at link/load time, not runtime.

Quoted 2 lines

I thought of wrapping oddval/randval in a struct and protecting it with a lock but I don't know how/where to initialize the lock in a race free manner?

Put a local lock in the function when testing if the variables are == 0, if you are worried that two different callers will enter it at the same time.

hope this helps,

greg k-h

#3 lore raw
In reply to Tobin C. Harding
Quoted 2 lines · Tobin C. Harding

I would like to create a boot time variable i.e a variable that is set once at boot time. Variable does not need to be globally accessible. (actually I am using two variables).

The canonical way to have stuff happen at boot is usually declaring an initcall.

Quoted 1 line

how/where to initialize the lock in a race free manner?

Step 0: Figure out how once-at-boot code could *possibly* be called
a second time. What are you worried about racing here?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 486 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20171010/35645399/attachment.bin

#2 lore raw
In reply to Greg KH
Quoted 28 lines · Greg KH
Quoted 4 lines · Tobin C. Harding

Hi,

I would like to create a boot time variable i.e a variable that is set once at boot time. Variable does not need to be globally accessible. (actually I am using two variables).

static foo = 42;

should be all you need, right?

If not, what exactly do you mean by "boot time variable"?

Quoted 16 lines

Could any one point me to examples of this already intree please?

I have tried the following but it has a race condition on the zero check and assignment of randval/oddval.

/* Maps a pointer to a unique identifier. */
static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)

{
	long hashval;
	static long randval = 0;
	static long oddval = 0;

	if (oddval == 0 && randval == 0) {
		randval = get_random_long();
		oddval = get_random_odd_long();
	}
Quoted 7 lines
Quoted 5 lines
	hashval = ptr_obfuscate((unsigned long)ptr, randval, oddval);
	spec.base = 16;

	return number(buf, end, hashval, spec);
}

What's wrong with this code?

Maybe I just have race conditions on the brain.

Quoted 8 lines
Quoted 4 lines

And the compiler doesn't like

        static long randval = get_random_long();
	static long oddval = get_random_odd_long();

Yeah, that will not work, static initializers are at link/load time, not runtime.

Quoted 6 lines
Quoted 2 lines

I thought of wrapping oddval/randval in a struct and protecting it with a lock but I don't know how/where to initialize the lock in a race free manner?

Put a local lock in the function when testing if the variables are == 0, if you are worried that two different callers will enter it at the same time.

Okay.

Quoted 3 lines

hope this helps,

greg k-h

Cheers Greg