I first encountered the C language about three decades ago, and I must confess that my first impressions were not particularly positive. Part of my early dissatisfaction was due to the fact that I was using 8-bit microprocessors and that early C compilers had a bad habit of promoting char to int in the generated object code, regardless of whether this made any particular sense. For example, if x and y were of type char, and were to be added with the sum placed in z, then the assembly output would execute something resembling the following steps:
Load x into an eight-bit register, which on the 8080 or z80 would normally be the a register.
Move the a register to the lower half of a sixteen-bit register, for example, the l portion of the hl register.
Test the sign bit of the a register.
Using one of a number of obscure instruction sequences, fill the upper half of the sixteen-bit register (in this example, h) with eight copies of this sign bit.
Repeat the above steps to get a sign-extended copy of y into (say) the de register.
Move l to a.
Add e to a (which sets the carry bit in the condition-code register appropriately).
Move a to (say) c.
Repeat the preceding three steps to add (with carry) h to d, placing the result in b.
Move b to a, never mind that this is where we just got b from.
Store a into z.
This sort of assembly code sequence was simply not what you wanted to see when attempting to jam 50,000 lines of source code into a 64KB address space, even when given a full 256K of physical memory and an overlay loader hacked up to do bank switching. Other long-dead and unlamented languages were able to generate a pair of loads, an add, and a store, which did much to limit C's uptake on 8-bit platforms. Of course, these other languages had their own charming limitations, such as lacking any variable-initialization syntax (use assembly instead!), insanely inefficient for loops, strange restrictions on their equivalent of union, and much else besides.
After all, the compiler had to fit in 48K, since the operating system (such as it was) consumed the remaining 16K. (My application dispensed with the operating system, and hence could use the entire 64K.)
The past three decades have seen C grow up to be almost unrecognizable, which, believe me, is a very good thing. This growth allows us to enjoy a huge number of language features, including:
identifiers longer than eight characters (and longer than seven for extern identifiers).
type-checked function arguments and return values.
the deprecation of the gets() function.
32-bit and 64-bit integers.
inline functions.
standard printf() formats (yes, these really did vary from compiler to compiler).
But one feature that I hadn't noticed until recently is the empty structure declaration, which Peter Zijlstra recently introduced me to in the form of the non-lockdep definition of struct lock_class_key. A recently detected hang in RCU under extreme conditions caused me to want an array of struct lock_class_key, which of course turns into an array of empty structures in non-lockdep builds.
To my surprise, this actually works, at least in gcc:
My children seem to have decided that I am not putting enough bugs in my software, given that they gave me one of these:
This does raise the question as to whether I prefer bugs in my software or in my candy. In this case, the answer is easy: the candy is transparent, so I know what I would be eating, so that cricket in the candy is far less troublesome than a bug in my software. Of course, if the candy were opaque, this decision might be somewhat more difficult.
In happy contrast to previous lives, these days my software is also transparent: freely available for all to download, use, study, redistribute, and improve. This means that other people can find my bugs (and vice versa), which can be surprisingly helpful. It is often the case that the “simple” matter of identifying the bug is the most difficult step in the process of fixing it.
Bugs can be inserted into code in the process of writing it and in the process of fixing other bugs, and I certainly have inserted plenty of bugs using both of these time-honored mechanisms. However, bugs can also appear spontaneously, as a result of changing workloads and changing requirements. I offer two examples from the current mainline implementation of RCU:
The call_rcu() function has a tunable safety limit (defaulting to 10,000 callbacks per CPU) beyond which RCU takes extreme measures to force the current grace period to complete. This has worked flawlessly for more than five years, but recent patches (which are otherwise eminently reasonable) cause this limit to be exceeded on a routine basis. Poof!!! The call_rcu() function now has a bug! And yes, I sent out a temporary hack, and now have a real fix in mainline.
Both Mathieu Desnoyers and Udo Steinberg have recently argued that the call_rcu() function should execute deterministically when invoked from a process running at real-time priority. And older versions of call_rcu() used to do just that. However, recent concerns with grace-period latency have resulted in my adding non-deterministic grace-period-acceleration code to call_rcu(). So, does call_rcu() have a real-time-response bug or doesn't it? I guess that the final answer is up to me, but the more I think about it, the more reasonable Mathieu's and Udo's position seems. So it seems that a second call_rcu() bug is in the offing.
All that aside, exactly when did these bugs appear? When the code was last changed? When the new use cases were first articulated? Or when I decided to accept them as bugs?
In the end, I guess it really doesn't matter so long as I fix them.
As noted earlier, Mathieu Desnoyers recently posted a user-level RCU implementation, which I have been contributing to. Mathieu, Michel R. Dagenais, Alan Stern, Jonathan Walpole, and I are putting together a paper covering this implementation (along with formal validation), two earlier user-level RCU implementations (1 and 2), and a semi-formal description of RCU semantics that Alan and I started work on back in late 2006 and that Jonathan and I have chewed over since (how time flies!). Finally, experimental results compare these implementations on real hardware, both against each other and against locking.
If you are interested in looking over an early draft and giving us feedback, please drop me an email. We are interested in pretty high-level feedback initially, as in "should this be 1, 2, or 3 papers". Of course, we are also quite interested in suggested venues, and we certainly won't turn away any detailed feedback that happens to come our way.
Interestingly enough, this is the first paper that I have collaborated on using git, which after a few initialization and learning-curve problems has performed admirably. It is really nice to be able to run gitk on the paper and look at how it has evolved. Of course, digital packrat that I am, I can get almost the same information on my earlier drafts and papers by "ls *.pdf", but this simply does not provide the same level of detail.
My first-ever source-code control system was a large metal cabinet filled with these:
This worked quite well, especially once you mastered the art of creating drum cards for the keypunch and of programming the card sorter. You will notice that this punched card (from an assembly-language program for a now-ancient machine) does not have a sequence number punched in the right-most columns, meaning that I was still in the process of writing it when this deck was punched. Another trick essential to preserving my sanity in case the rubber band broke was to use a felt-tip pen to draw a slanted line across the edge of the deck. This allowed me to reassemble the deck by restoring the line. I printed listings very frequently, marking them up before keypunching the changes. In the worst case, I could then use a recent listing to get the card deck back into some semblance of order. Or keypunch it from the listing in case the cardreader was especially hungry that day.
Of course, the BASIC programmers would ostentatiously drop their card decks any time someone new showed up. You see, the old versions of BASIC required a line number on each and every line, in ascending order. This meant that such a BASIC program had the same meaning no matter what order the cards were in: the first thing that the BASIC interpreter would do was to sort the program into line-number order.
But later on, as technology advanced, I found myself consulting for a company that had more advanced hardware, including hard disks with many megabytes of capacity. Because we had multiple developers working on this project, we needed a modern source-code control system. We therefore pressed a large whiteboard into service. Each developer would write the names of the files being modified in his portion of the whiteboard. All in all, this system worked quite well, especially given that we all worked in the same room.
Networking? That was for fishermen.
Remote access? Yes, we did download crash dumps over a 1200-baud modem, but not if we could possibly avoid it.
Even later, I consulted for a company that ran BSD 2.9 UNIX. We handled the source-code control problem by assigning different developers to the separate programs making up the application. We also made use of floppy disks for backup, though you had to be very careful, since accessing an empty floppy drive would panic the system. This was of course an excellent way of terminally annoying the other developers sharing the machine, especially given that this was long before “vi” gained the “-r” command-line argument. Listings were still vitally important, and it was not unusual to find oneself re-typing a program from a listing (hopefully not too far out of date) while vigorously cursing whoever tried to use the floppy drive without having first put a floppy disk in the drive.
Later still, I worked as a system administrator for a system that not only boasted 32-bit addressing, but also possessed almost a full gigabyte of disk. Suddenly, a tool called “RCS” appeared on the scene. This had an amazing impact — you could actually undo mistakes without having to find an old listing of the program!!! I used RCS for about 20 years, often wrapped up in elaborate scripts, but not infrequently in its raw form.
Of course, after I started working with the Linux community, I was introduced to a number of new tools, including cvs and bitkeeper. These supported remote access, but were otherwise similar in spirit to RCS. But mostly I just downloaded Linux-kernel tarballs, submitted patches, and quite happily left the source-code control to Linus and his maintainers.
Then, with much noise and fanfare, “git” arrived. My first encounters with this tool were not particularly happy ones. When I did need to access someone's git tree, I would delete my old version and reclone the new one. It was quite some time before I learned about git checkout linus/master, without which git remote update appears to be an extremely slow no-op. I did make a few attempts to actually modify a git tree, but these ended in frustration. It would be quite some time before I learned that git requires you to add files even though they are already there!!! After I learned the subtleties of git add and git commit -a, I had to wonder if this was some sort of divine punishment for having loosed RCU on the Linux kernel community.
Over time, numerous git how-tos appeared on the web, and they did help a little. The problem was that most of them were written for people who were new to source code control, so they didn't help dinosaurs like myself unlearn all the now-counterproductive habits from our decades with RCS. But over time, I read enough of them to be able to issue up to ten git commands without destroying my local git archive.
Over the past year, I started working with Ingo's and Thomas's -tip tree, which included README files that led me step by step through a few common tasks. Once I learned about the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME, and GIT_COMMITTER_EMAIL environment variables, the --signoff arguments suddenly seemed significantly less sadistic. When I foolishly made multiple changes to RCU at the same time, git add allowed me to split it up into several commits. Suddenly it made sense to tell git about a change to an already-existing file. The fact that git kept all my commits in time order grew on me (you can do this in RCS, but it requires much more care than I am usually able to muster). The git diff command frequently (but pleasantly) surprised me by doing what I meant it to, despite my having accidentally hit the return key before I was telling it all I thought it needed to know. The gitk command is wonderful, especially its ability to focus on only those commits that affect a specified list of files or subtrees. The git format-patch and git send-email commands automate the otherwise painstaking task of sending a multi-part patch to LKML.
So after some years of fear and loathing, I have finally gotten to the point where git is extremely useful to me. Earlier today, I even found myself explaining it to someone else.
Who knows, some day I might even start really liking git!
Each community, proprietary or otherwise, has its own coding style, and the Linux kernel community is famously no different. The Linux kernel community's style differs in a number of ways from that of my earlier proprietary community, with perhaps the most jarring difference being the prohibition against #ifdef outside of header files. But when in Rome, it is wise to do as the Romans do, so I played along.
Fast forward a year or two, and I was writing a smallish experimental project (my first abortive attempt at a user-level RCU implementation, if you must know). So I coded the project twice, once using #ifdef freely and again adhering to the Linux kernel community's prohibitions. The second version was so much simpler that I stuck with it.
Of course, the reason that confining #ifdef to header files works so well is that it forces you to create meaningful APIs that provide higher-level abstractions. Without these abstractions, you might well have #ifdef in the middle of functions, in the middle of parameter declarations, or even in the middle of statements. Code that makes use of good higher-level abstractions will have much better structure and will therefore be much easier to debug, maintain, and extend.
Other elements of the Linux kernel community's coding style are taking longer to fully appreciate. I still have a very hard time omitting the braces around a single-statement body of an if or while statement, though I suppose that omitting the braces does shorten the code a bit, which might well make it easier to read. It is certainly the case that Linux kernel code is read much more frequently than it is written, so that would be the right way to optimize.
It is quite possible that some elements of the Linux kernel community's coding style are arbitrary, or perhaps even slightly counterproductive. If so, it is nonetheless standing the test of time quite well. And besides, when in Rome, it is wise to do as the Romans do!
Mathieu Desnoyer posted an interesting user-level RCU implementation. It uses signal handlers to avoid memory barriers in the read-side primitives, while also avoiding the need for all threads to pass through a designated quiescent state. I have a few quibbles with it, but a very interesting approach!
Yes, I do have a few user-level RCU implementations of my own, which may be found in the CodeSamples/defer directory of the git tree at git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/perfbook.git. Some of these require memory barriers in the read-side primitives, while others require that all threads in the application periodically pass through designated quiescent states. Mathieu's implementation therefore provides an interesting third approach that might turn out to be quite useful.
I often introduce myself as a recovering proprietary programmer, given that I have been working on the Linux kernel for less than ten years of a 30+ year career. As you might guess, I will always be a recovering proprietary programmer — even if there is a seven-step process out there somewhere, I doubt that anyone actually makes it to the seventh step.
This begs the question “why bother recovering?” The answer to this question has many aspects, but the first part is that I have come to realize that the open-source development process simply produces better results.
And here is a full list of confessions for your amusement:
I realize this post is from 2022. Earlier this year I added a second argument to bpftrace hist(x, b) where b >=0 means each power of 2 is split into 2^b buckets. Using b=3 already gives good…
I recently added some references to the last sentence of that section, so please feel free to take a look. And a great deal more information is available here.
Comments