relayfs for 2.6.10: locking/lockless implementation
Greg,
There are a couple variables used throughout relayfs code that could
be modified at any point "simultaneously" by different processes. These variables were not declared volatile, thus when we modify them we need to tell the compiler to refetch from memory as another process could have changed out from under the current stream of execution since the last time there were accessed in the function. An alternative would be to mark the variables that we care about as volatile. I am not sure how best to make that tradeoff (i.e., always forcing a refetch by marking a variable volatile or only at points were we know we need to by memory clobbering) or on what side the Linux community comes down on. We certainly would be happy to go either way with the relayfs code, i.e., mark them variable and used the standard atomic operations. That explains compare_and_store, atomic_add, and atomic_sub. It does not explain the memory clobbering around the atomic set operation, which I'm guessing was there just to be consistent with the other operations, and could, I believe, be removed. Hopefully that helps answer the question. If it doesn't please feel free to ask more. Thanks.
-bob
Robert Wisniewski
The K42 MP OS Project
Advanced Operating Systems
Scalable Parallel Systems
IBM T.J. Watson Research Center
914-945-3181
http://www.research.ibm.com/K42/
[email protected]
----
Quoted 9 lines · Karim Yaghmour
(quoted diff) +9
-0+/** + * compare_and_store_volatile - self-explicit + * @ptr: ptr to the word that will receive the new value + * @oval: the value we think is currently in *ptr + * @nval: the value *ptr will get if we were right + */ +inline int +compare_and_store_volatile(volatile u32 *ptr, + u32 oval,
Review
Quoted 10 lines
(quoted diff) +10
-0+ u32 nval) +{ + u32 prev; + + barrier(); + prev = cmpxchg(ptr, oval, nval); + barrier(); + + return (prev == oval); +}
Why is this function needed? What's wrong with the "normal" cmpxchg?
Review
Quoted 13 lines
(quoted diff) +13
-0+/** + * atomic_set_volatile - atomically set the value in ptr to nval. + * @ptr: ptr to the word that will receive the new value + * @nval: the new value + */ +inline void +atomic_set_volatile(atomic_t *ptr, + u32 nval) +{ + barrier(); + atomic_set(ptr, (int)nval); + barrier(); +}
Same here, what's wrong with the normal atomic_set()?
Same question also goes for the other functions like this in this file.
thanks,
greg k-h
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
--------------080309080704060007080707--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
A: No.
Q: Should I include quotations after my reply?
Quoted 8 lines · Robert Wisniewski
Greg,
There are a couple variables used throughout relayfs code that could
be modified at any point "simultaneously" by different processes. These variables were not declared volatile, thus when we modify them we need to tell the compiler to refetch from memory as another process could have changed out from under the current stream of execution since the last time there were accessed in the function. An alternative would be to mark the variables that we care about as volatile.
marking them volatile does not protect across cpus. Just using a normal atomic_t will work properly.
Quoted 6 lines
I am not sure how best to make
that tradeoff (i.e., always forcing a refetch by marking a variable volatile or only at points were we know we need to by memory clobbering) or on what side the Linux community comes down on. We certainly would be happy to go either way with the relayfs code, i.e., mark them variable and used the standard atomic operations.
Just use atomic_t and don't mess with volatile. See the archives for why that (volatile) doesn't work like that.
Quoted 1 line
That explains
compare_and_store,atomic_add, andatomic_sub.
No it doesn't, why do your own version of this function with the barrier() function?
Quoted 5 lines
It does not explain the memory clobbering around the atomic set operation, which I'm guessing was there just to be consistent with the other operations, and could, I believe, be removed. Hopefully that helps answer the question. If it doesn't please feel free to ask more. Thanks.
So these can just be removed, and the code changed to use the proper atomic calls? If so, please do so.
thanks,
greg k-h
Quoted 3 lines · Greg KH
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
Okay - sorry about that - will respond inline.
Quoted 2 lines
A: Top-posting.
Q: What is the most annoying thing in e-mail?
ACTUALLY I HATE ALL CAPS MORE :-)
Quoted 13 lines
A: No.
Q: Should I include quotations after my reply?Quoted 8 lines · Robert Wisniewski
Greg,
There are a couple variables used throughout relayfs code that could
be modified at any point "simultaneously" by different processes. These variables were not declared volatile, thus when we modify them we need to tell the compiler to refetch from memory as another process could have changed out from under the current stream of execution since the last time there were accessed in the function. An alternative would be to mark the variables that we care about as volatile.
Quoted 2 lines
marking them volatile does not protect across cpus. Just using a normal
atomic_twill work properly.
I believe the below illustrates the problem that will be seen without volatile.
int g;
funct1()
{
g = 0;
while (g == 0) ;
printf("here\n");
}funct2()
{
g = 1;
}If funct1 and funct2 are executed either by different processes on the same processor (which is the case for relayfs as we have per-processor buffers), or across processors, and if g is not marked volatile, then if a process gets into funct1, does the first line, gets interrupted, funct2 executes, then funct1 continues, funct1 will never get out of the while loop. If however g is marked volatile then it will get out of the loop. Is this not true? If we agree that in this case g needs to be marked volatile there are now two choices. Either mark it volatile in the declaration or while(g==0) barrier(); If I understand it correctly barrier() invalidates all current register values and forces a reload from memory.
Quoted 15 lines
Quoted 6 lines
I am not sure how best to make
that tradeoff (i.e., always forcing a refetch by marking a variable volatile or only at points were we know we need to by memory clobbering) or on what side the Linux community comes down on. We certainly would be happy to go either way with the relayfs code, i.e., mark them variable and used the standard atomic operations.Just use
atomic_tand don't mess with volatile. See the archives for why that (volatile) doesn't work like that.Quoted 1 line
That explains
compare_and_store,atomic_add, andatomic_sub.No it doesn't, why do your own version of this function with the
barrier()function?
Quoted 8 lines
Quoted 5 lines
It does not explain the memory clobbering around the atomic set operation, which I'm guessing was there just to be consistent with the other operations, and could, I believe, be removed. Hopefully that helps answer the question. If it doesn't please feel free to ask more. Thanks.
So these can just be removed, and the code changed to use the proper atomic calls? If so, please do so.
Yes we can remove the code and use the standard atomic calls, but based on the above example, I think we need to mark a couple variables volatile. Do you agree, if so, and unless there's dissenting opinion we can make the change.
Thanks.
-bob
Quoted 4 lines
thanks,
greg k-h
Quoted 2 lines · Robert Wisniewski
I believe the below illustrates the problem that will be seen without volatile.
Please read the lkml archives about all of the problems surrounding marking variables "volatile" and how you really never want to do that in the kernel. If you want to access a variable in two different places, at the same time, just use a lock to keep it sane.
Quoted 7 lines
Quoted 2 lines
So these can just be removed, and the code changed to use the proper atomic calls? If so, please do so.
Yes we can remove the code and use the standard atomic calls, but based on the above example, I think we need to mark a couple variables volatile. Do you agree, if so, and unless there's dissenting opinion we can make the change.
No, I disagree. Just use the standard atomic calls, they will work just fine.
thanks,
greg k-h