Skip to content

Commit b2256d8

Browse files
committed
task_switch: explicitly drop current task's TLS reference (#989)
* This change ensures that the `CURRENT_TASK` TLS variable that is used in task switching is dropped *before* switching to the next task, in order to ensure that `RefCell`'s runtime borrowing mechanic always works across task switches. bb66e6a
1 parent d12f819 commit b2256d8

25 files changed

+72
-58
lines changed

doc/src/task/lib.rs.html

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,13 @@
12501250
<a href="#1250" id="1250">1250</a>
12511251
<a href="#1251" id="1251">1251</a>
12521252
<a href="#1252" id="1252">1252</a>
1253+
<a href="#1253" id="1253">1253</a>
1254+
<a href="#1254" id="1254">1254</a>
1255+
<a href="#1255" id="1255">1255</a>
1256+
<a href="#1256" id="1256">1256</a>
1257+
<a href="#1257" id="1257">1257</a>
1258+
<a href="#1258" id="1258">1258</a>
1259+
<a href="#1259" id="1259">1259</a>
12531260
</pre><pre class="rust"><code><span class="doccomment">//! Key types and functions for multitasking that build on the basic [`Task`].
12541261
//!
12551262
//! The main types of interest are:
@@ -1289,6 +1296,7 @@
12891296
};
12901297
<span class="kw">use </span>core::{
12911298
any::Any,
1299+
cell::RefMut,
12921300
fmt,
12931301
hash::{Hash, Hasher},
12941302
ops::Deref,
@@ -2062,12 +2070,12 @@
20622070
/// Hence, the the main [`task_switch()`] routine proceeds with the context switch
20632071
/// after we return to it from this function.
20642072
</span><span class="kw">fn </span>task_switch_inner(
2065-
curr_task_tls_slot: <span class="kw-2">&amp;mut </span><span class="prelude-ty">Option</span>&lt;TaskRef&gt;,
2073+
<span class="kw-2">mut </span>curr_task_tls_slot: RefMut&lt;<span class="lifetime">&#39;_</span>, <span class="prelude-ty">Option</span>&lt;TaskRef&gt;&gt;,
20662074
next: TaskRef,
20672075
cpu_id: CpuId,
20682076
preemption_guard: PreemptionGuard,
20692077
) -&gt; <span class="prelude-ty">Result</span>&lt;TaskSwitchInnerRet, (bool, PreemptionGuard)&gt; {
2070-
<span class="kw">let </span><span class="prelude-val">Some</span>(<span class="kw-2">ref </span>curr) = curr_task_tls_slot <span class="kw">else </span>{
2078+
<span class="kw">let </span><span class="prelude-val">Some</span>(curr) = curr_task_tls_slot.as_ref() <span class="kw">else </span>{
20712079
<span class="macro">error!</span>(<span class="string">&quot;BUG: task_switch_inner(): couldn&#39;t get current task&quot;</span>);
20722080
<span class="kw">return </span><span class="prelude-val">Err</span>((<span class="bool-val">false</span>, preemption_guard));
20732081
};
@@ -2077,7 +2085,7 @@
20772085
<span class="kw">return </span><span class="prelude-val">Err</span>((<span class="bool-val">false</span>, preemption_guard));
20782086
}
20792087

2080-
<span class="comment">// trace!(&quot;task_switch [0]: (CPU {}) prev {:?}, next {:?}, interrupts?: {}&quot;, cpu_id, curr, next, irq_safety::interrupts_enabled());
2088+
<span class="comment">// log::trace!(&quot;task_switch [0]: (CPU {}) prev {:?}, next {:?}, interrupts?: {}&quot;, cpu_id, curr, next, irq_safety::interrupts_enabled());
20812089

20822090
// These conditions are checked elsewhere, but can be re-enabled if we want to be extra strict.
20832091
// if !next.is_runnable() {
@@ -2160,11 +2168,15 @@
21602168
// We store the removed `TaskRef` in CPU-local storage so that it remains accessible
21612169
// until *after* the context switch.
21622170
</span><span class="kw">if </span>curr_task_has_exited {
2163-
<span class="comment">// trace!(&quot;task_switch(): deiniting current task TLS for: {:?}, next: {}&quot;, curr_task_tls_slot.as_deref(), next.deref());
2171+
<span class="comment">// log::trace!(&quot;[CPU {}] task_switch(): deiniting current task TLS for: {:?}, next: {}&quot;, cpu_id, curr_task_tls_slot.as_deref(), next.deref());
21642172
</span><span class="kw">let </span>prev_taskref = curr_task_tls_slot.take();
21652173
DROP_AFTER_TASK_SWITCH.with_mut(|d| d.<span class="number">0 </span>= prev_taskref);
21662174
}
21672175

2176+
<span class="comment">// Now we are done touching the current task&#39;s TLS slot, so proactively drop it now
2177+
// to ensure that it isn&#39;t accidentally dropped later after we&#39;ve switched the active TLS area.
2178+
</span>drop(curr_task_tls_slot);
2179+
21682180
<span class="comment">// Now, set the next task as the current task running on this CPU.
21692181
//
21702182
// Note that we cannot do this until we&#39;ve done the above part that cleans up
@@ -2381,7 +2393,9 @@
23812393
<span class="prelude-val">Ok</span>(ExitableTaskRef { task: taskref })
23822394
}
23832395
<span class="prelude-val">Err</span>(_e) =&gt; {
2384-
<span class="macro">log::error!</span>(<span class="string">&quot;BUG: init_current_task() failed to mutably borrow CURRENT_TASK&quot;</span>);
2396+
<span class="macro">log::error!</span>(<span class="string">&quot;[CPU {}] BUG: init_current_task(): failed to mutably borrow CURRENT_TASK. \
2397+
Task ID: {}, {:?}&quot;</span>, cpu::current_cpu(), current_task_id, taskref,
2398+
);
23852399
<span class="prelude-val">Err</span>(InitCurrentTaskError::AlreadyBorrowed(current_task_id))
23862400
}
23872401
}
@@ -2397,9 +2411,9 @@
23972411
/// Returns an `Err` containing the `value` if the current task cannot be obtained.
23982412
</span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">fn </span>with_current_task_tls_slot_mut&lt;F, R, T&gt;(function: F, value: T) -&gt; <span class="prelude-ty">Result</span>&lt;R, T&gt;
23992413
<span class="kw">where
2400-
</span>F: FnOnce(<span class="kw-2">&amp;mut </span><span class="prelude-ty">Option</span>&lt;TaskRef&gt;, T) -&gt; R
2414+
</span>F: FnOnce(core::cell::RefMut&lt;<span class="lifetime">&#39;_</span>, <span class="prelude-ty">Option</span>&lt;TaskRef&gt;&gt;, T) -&gt; R
24012415
{
2402-
<span class="kw">if let </span><span class="prelude-val">Ok</span>(tls_slot) = CURRENT_TASK.try_borrow_mut().as_deref_mut() {
2416+
<span class="kw">if let </span><span class="prelude-val">Ok</span>(tls_slot) = CURRENT_TASK.try_borrow_mut() {
24032417
<span class="prelude-val">Ok</span>(function(tls_slot, value))
24042418
} <span class="kw">else </span>{
24052419
<span class="prelude-val">Err</span>(value)

doc/task/enum.InitCurrentTaskError.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

doc/task/fn.all_tasks.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Returns a list containing a snapshot of all tasks that currently exist."><meta name="keywords" content="rust, rustlang, rust-lang, all_tasks"><title>all_tasks in task - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-1f7d512b176f0f72.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-124a1ca42af929b6.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-fbcde169c4acde5f.css" id="mainThemeStyle"><link rel="stylesheet" id="themeStyle" href="../static.files/light-4743e13df3dfe8c4.css"><link rel="stylesheet" disabled href="../static.files/dark-0e1b889528bd466b.css"><link rel="stylesheet" disabled href="../static.files/ayu-65289d5d067c7c66.css"><script id="default-settings" ></script><script src="../static.files/storage-d43fa987303ecbbb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-bc1b32400f872ddb.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-13285aec31fa243e.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="logo-container" href="../task/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2></h2></nav><nav class="sidebar"><a class="logo-container" href="../task/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><div class="sidebar-elems"><h2><a href="index.html">In task</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-5ec35bf9ca753509.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="index.html">task</a>::<wbr><a class="fn" href="#">all_tasks</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/task/lib.rs.html#82-87">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn all_tasks() -&gt; <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;(<a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a>, <a class="struct" href="struct.WeakTaskRef.html" title="struct task::WeakTaskRef">WeakTaskRef</a>)&gt;</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Returns a list containing a snapshot of all tasks that currently exist.</p>
1+
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Returns a list containing a snapshot of all tasks that currently exist."><meta name="keywords" content="rust, rustlang, rust-lang, all_tasks"><title>all_tasks in task - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-1f7d512b176f0f72.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Bold-124a1ca42af929b6.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-fbcde169c4acde5f.css" id="mainThemeStyle"><link rel="stylesheet" id="themeStyle" href="../static.files/light-4743e13df3dfe8c4.css"><link rel="stylesheet" disabled href="../static.files/dark-0e1b889528bd466b.css"><link rel="stylesheet" disabled href="../static.files/ayu-65289d5d067c7c66.css"><script id="default-settings" ></script><script src="../static.files/storage-d43fa987303ecbbb.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-bc1b32400f872ddb.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-13285aec31fa243e.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc fn"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="logo-container" href="../task/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><h2></h2></nav><nav class="sidebar"><a class="logo-container" href="../task/index.html"><img class="rust-logo" src="../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><div class="sidebar-elems"><h2><a href="index.html">In task</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-5ec35bf9ca753509.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Function <a href="index.html">task</a>::<wbr><a class="fn" href="#">all_tasks</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/task/lib.rs.html#83-88">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>&#x2212;</span>]</button></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn all_tasks() -&gt; <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;(<a class="primitive" href="https://doc.rust-lang.org/nightly/core/primitive.usize.html">usize</a>, <a class="struct" href="struct.WeakTaskRef.html" title="struct task::WeakTaskRef">WeakTaskRef</a>)&gt;</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Returns a list containing a snapshot of all tasks that currently exist.</p>
22
<h2 id="usage-notes"><a href="#usage-notes">Usage Notes</a></h2>
33
<ul>
44
<li>This is an expensive and slow function, so it should be used rarely.</li>

0 commit comments

Comments
 (0)