From: Akira Yokosawa <akiyks@gmail.com>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: perfbook@vger.kernel.org, Akira Yokosawa <akiyks@gmail.com>
Subject: [PATCH -perfbook 3/6] defer/rcuintro: Tweak Listings 9.13 and 9.14 by employing fancyvrb
Date: Tue, 23 Jun 2026 19:26:29 +0900 [thread overview]
Message-ID: <6cb62056-ff2f-413a-9ff5-de9eb688044f@gmail.com> (raw)
In-Reply-To: <5365c170-af04-466e-876b-7c220b327591@gmail.com>
For better consistency in presenting code snippets, employ
fancyvrb's Verbatim env. Automate line counting as well.
While here, do "s/-/--/" for "lines~C1-C3" as well.
(Line number labeling/referencing might be automated in the
future ...)
Signed-off-by: Akira Yokosawa <akiyks@gmail.com>
---
defer/rcuintro.tex | 96 +++++++++++++++++++++++++++-------------------
1 file changed, 57 insertions(+), 39 deletions(-)
diff --git a/defer/rcuintro.tex b/defer/rcuintro.tex
index 21a99243..596f138f 100644
--- a/defer/rcuintro.tex
+++ b/defer/rcuintro.tex
@@ -69,64 +69,82 @@ can be implemented with a single load instruction, exactly the instruction
that would normally be used in single-threaded code.
\begin{listing}[tb]
-{ \small
-\begin{verbatim}
- A1 p = gp;
- A2 do_something_with(p->a);
- A3 do_something_with(p->b);
-\end{verbatim}
+\small
+\fvset{numbers=left,numbersep=5pt,fontsize=\scriptsize,frame=single,xleftmargin=15pt,xrightmargin=5pt}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = gp;
+ do_something_with(p->a);
+ do_something_with(p->b);
+\end{Verbatim}
+}
Might be transformed to:
-\begin{verbatim}
- B1 p = gp;
- B2 do_something_with(p->a);
- B3 p = gp;
- B4 do_something_with(p->b);
-\end{verbatim}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = gp;
+ do_something_with(p->a);
+ p = gp;
+ do_something_with(p->b);
+\end{Verbatim}
+}
The compiler assumes normal variables do not spontaneously change,
\co{do_something_with()} might use many machine registers, and this
transformation reduces register pressure.
-But if some other thread changes \co{gp} between lines~1 and~3 of the
+But if some other thread changes \co{gp} between lines~B1 and~B3 of the
transformed code, the values of \co{p->a} and \co{p->b} will be mismatched.
Prevent this by using \co{rcu_dereference()} as follows:
-\begin{verbatim}
- C1 p = rcu_dereference(gp);
- C2 do_something_with(p->a);
- C3 do_something_with(p->b);
-\end{verbatim}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = rcu_dereference(gp);
+ do_something_with(p->a);
+ do_something_with(p->b);
+\end{Verbatim}
}
\caption{Compilers Can Reload Values}
\label{lst:defer:Compilers Can Reload Values}
\end{listing}
\begin{listing}[tb]
-{ \small
-\begin{verbatim}
- A1 p = malloc(sizeof(*p));
- A2 p->a = compute_value();
- A3 p->b = 42;
- A4 gp = p;
-\end{verbatim}
+\small
+\fvset{numbers=left,numbersep=5pt,fontsize=\scriptsize,frame=single,xleftmargin=15pt,xrightmargin=5pt}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny A\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = malloc(sizeof(*p));
+ p->a = compute_value();
+ p->b = 42;
+ gp = p;
+\end{Verbatim}
+}
Might be transformed to:
-\begin{verbatim}
- B1 p = malloc(sizeof(*p));
- B2 gp = p;
- B3 p->a = compute_value();
- B4 p->b = 42;
-\end{verbatim}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny B\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = malloc(sizeof(*p));
+ gp = p;
+ p->a = compute_value();
+ p->b = 42;
+\end{Verbatim}
+}
The compiler assumes normal variables are not concurrently accessed,
and thus that the order of stores does not matter.
If \co{compute_value()} was inlined, this transformation might produce
better code.
In this example, if some other thread loads \co{gp} immediately after
-line~2 of the transformed code, that thread might see pre-initialization
+line~B2 of the transformed code, that thread might see pre-initialization
garbage in \co{p->a} and \co{p->b}.
Prevent this by using \co{rcu_assign_pointer()} as follows:
-\begin{verbatim}
- C1 p = malloc(sizeof(*p));
- C2 p->a = compute_value();
- C3 p->b = 42;
- C4 rcu_assign_pointer(gp, p);
-\end{verbatim}
+{\renewcommand{\theFancyVerbLine}{%
+ {\rmfamily\tiny C\arabic{FancyVerbLine}}}
+\begin{Verbatim}
+ p = malloc(sizeof(*p));
+ p->a = compute_value();
+ p->b = 42;
+ rcu_assign_pointer(gp, p);
+\end{Verbatim}
}
\caption{Compilers Can Reorder Accesses}
\label{lst:defer:Compilers Can Reorder Accesses}
@@ -146,7 +164,7 @@ This transformation would fatally confuse any implementation of
\co{do_something_with()} that assumed that it was being passed values
from the same structure.
To prevent this transformation, use \co{rcu_dereference()} as
-shown on lines~C1-C3 of this listing, thus informing the compiler of the
+shown on lines~C1--C3 of this listing, thus informing the compiler of the
possibility of concurrent updates to \co{gp}.
To see the need for \co{rcu_assign_pointer()}, please see lines~A1--A4 of
--
2.43.0
next prev parent reply other threads:[~2026-06-23 10:26 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-23 10:20 [PATCH -perfbook 0/6] 2nd batch for June 2026 Akira Yokosawa
2026-06-23 10:21 ` [PATCH -perfbook 1/6] runlatex.sh: Cope with missing cleveref.sty Akira Yokosawa
2026-06-23 10:22 ` [PATCH -perfbook 2/6] Stop loading braket.sty Akira Yokosawa
2026-06-23 10:26 ` Akira Yokosawa [this message]
2026-06-23 10:28 ` [PATCH -perfbook 4/6] defer/rcuintro: More tweaks for Listings 9.13 and 9.14 Akira Yokosawa
2026-06-23 10:30 ` [PATCH -perfbook 5/6] defer/rcufundamental: Fix typo ("cannot not return") Akira Yokosawa
2026-06-23 10:31 ` [PATCH -perfbook 6/6] Update rcu-test-ratio for Linux v7.1 Akira Yokosawa
2026-06-23 16:33 ` [PATCH -perfbook 0/6] 2nd batch for June 2026 Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6cb62056-ff2f-413a-9ff5-de9eb688044f@gmail.com \
--to=akiyks@gmail.com \
--cc=paulmck@kernel.org \
--cc=perfbook@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.