<div> </div>
<div class="gmail_quote">On Thu, Aug 18, 2011 at 11:14 AM, Srivatsa Bhat <span dir="ltr">&lt;<a href="mailto:bhat.srivatsa@gmail.com">bhat.srivatsa@gmail.com</a>&gt;</span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote"><br><br>
<div class="gmail_quote">
<div>
<div></div>
<div class="h5">On Thu, Aug 18, 2011 at 11:40 PM, Srivatsa Bhat <span dir="ltr">&lt;<a href="mailto:bhat.srivatsa@gmail.com" target="_blank">bhat.srivatsa@gmail.com</a>&gt;</span> wrote:<br></div></div>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote"><br><br>
<div class="gmail_quote">
<div>
<div></div>
<div class="h5">
<div>
<div></div>
<div>On Thu, Aug 18, 2011 at 10:44 PM, Vaibhav Jain <span dir="ltr">&lt;<a href="mailto:vjoss197@gmail.com" target="_blank">vjoss197@gmail.com</a>&gt;</span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>
<div></div>
<div><br><br>
<div class="gmail_quote">On Thu, Aug 18, 2011 at 9:02 AM, srivatsa bhat <span dir="ltr">&lt;<a href="mailto:bhat.srivatsa@gmail.com" target="_blank">bhat.srivatsa@gmail.com</a>&gt;</span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Hi Vaibhav,<br><br>
<div class="gmail_quote">
<div>On Thu, Aug 18, 2011 at 8:24 PM, Vaibhav Jain <span dir="ltr">&lt;<a href="mailto:vjoss197@gmail.com" target="_blank">vjoss197@gmail.com</a>&gt;</span> wrote:<br></div>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">Hi,<br><br>
<div>I talked to a friend of mine and he suggested that <br>in a logical offline state the cpu is powered on and ready to execute instructions<br>just that the kernel is not aware of it. But in case of physical offline state the cpu <br>
is powered off and cannot run.<br>Are you saying something similar ? 
<div>
<div></div>
<div><br></div></div></div></blockquote>Yes, you are right, mostly. <br>When you try to logically offline a CPU, the kernel will do task migration (i.e., move out all the tasks running on that CPU to other CPUs in the system) and it ensures that it doesn&#39;t need that CPU anymore. This also means that, from now on, the context of that CPU need not be saved (because the kernel has moved that CPU&#39;s tasks elsewhere). At this point, it is as if the kernel is purposefully using only a subset of the available CPUs. This step is a necessary prerequisite to do physical CPU offline later on.<br>
<br>But I don&#39;t think CPU power ON or OFF is the differentiating factor between logical and physical offlining. In logical offline, you still have the CPUs in the system but you just tell the kernel not to use them. At this stage, you can power off your CPU, to save power for example.<br>
But in physical offline, from a software perspective, you do additional work at the firmware level (apart from logical offlining at the OS level), to ensure that physically plugging out the CPUs will not affect the running system in any way.<br>
<br>Please note that you can logically online and offline the same CPUs over and over again without rebooting the system. Here, while onlining a CPU which was offlined previously, the kernel follows almost the same sequence which it normally follows while booting the CPUs during full system booting.<br>
<br>Also one more thing to be noted is that, to be able to physically hot-plug CPUs, apart from OS and firmware support, you also need the hardware to support this feature. That is, the electrical wiring to the individual CPUs should be such that plugging them in and out does not interfere with the functioning of the rest of the system. As of today, there are only a few systems that support physical CPU-hotplug. But you can do logical CPU hotplug easily, by configuring the kernel appropriately during compilation, as you have noted in one of your previous mails.<br>
<br>Regards,<br>Srivatsa S. Bhat<br></div></blockquote></div><br><br></div></div>Hi Srivatsa,<br><br>That was great explanation! Thanks! <br>I have just one more query. You mentioned above that &quot;<span style="COLOR: rgb(0,0,102)"> the kernel follows almost the same <b>sequence </b>which it normally follows while booting the CPUs during full system booting</span>.&quot; <br>
<br>Can you please explain this sequence a little ?<br><font color="#888888"><br></font></blockquote></div></div></div></div>
<div>
<div>
<div></div>
<div class="h5"><br>Hi Vaibhav,<br><br>I&#39;ll try to outline a very high level view of what happens while booting an SMP (Symmetric Multi-Processor) system. Instead of going through the entire boot sequence, let me just highlight only the part that is of interest in this discussion: booting multiple CPUs.<br>
<br>The &quot;boot processor&quot; is the one which is booted first while booting a system. On x86 architecture, CPU 0 is always the boot processor. Hence, if you have observed, you cannot offline CPU0 using CPU hot-plugging on an x86 machine. (On an Intel box, the file /sys/devices/system/cpu/cpu0/online is purposefully absent, for this reason!). But in other architectures, this might not be the case. For example on POWER architecture, any processor in the system can act as the boot processor. <br>
<br>Once the boot processor does its initialization, the other processors, known as &quot;secondary processors or application processors (APs)&quot; are booted/initialized. Here, obviously some synchronization mechanism is necessary to ensure that this order is followed. So in Linux, we use 2 bitmasks called &quot;cpu_callout_mask&quot; and &quot;cpu_callin_mask&quot;. These bitmasks are used to indicate the processors available in the system.<br>
<br>Once the boot processor initializes itself, it updates cpu_callout_mask to indicate which secondary processor (or application processor AP) can initialize itself next (for example, the boot processor sets a particular bit as 1 in the cpu_callout_mask). On the other hand, the secondary processor would have done some very basic initialization till then and will be testing the value of &#39;cpu_callout_mask&#39; in a while loop to see if its number has been &quot;called out&quot; by the boot processor. Only after the boot processor &quot;calls out&quot; this AP, this AP will continue the rest of its initialization and completes it. <br>
<br>Once the AP completes its initialization, it reports back to the boot processor by setting its number in the cpu_callin_mask. As expected, the boot processor would have been waiting in a while loop on cpu_callin_mask to see if this AP booted OK or not. Once it finds that the cpu_callin_mask for this AP has been set, the boot processor follows the same procedure to boot other APs: i.e., it updates cpu_callout_mask and waits for the corresponding entry to be set in cpu_callin_mask by that AP and so on. This process continues until all the APs are booted up. <br>
<br>Of course, each of these &quot;waiting&quot; times (of both boot processor and APs) are capped by some preset value, say for example 5 seconds. If some AP takes more than that time to boot, the boot processor declares that the AP could not boot and takes appropriate action (like clearing its bit in cpu_callout_mask and logically removing that AP from its tables etc, effectively forgetting about that processor). Similarly while the APs wait for the boot processor to call them out, if the boot processor does not call them within a given time period, they declare kernel panic.<br>
<br>Here are some references, if you are interested in more details:<br><br>Linux kernel source code:<br>1. linux/arch/x86/kernel/smpboot.c : start_secondary() and smp_callin()<br>    These are the functions executed by the APs (secondary or application processors). Actually smp_callin() is called within start_secondary() which is the primary function executed by APs.<br>
<br></div></div>2. linux/arch/x86/kernel/smpboot.c :  do_boot_cpu()<br></div></div></blockquote>
<div>       This is executed by the boot processor.  You can look up other important functions such as native_cpu_up().<br> <br>    General SMP booting info:<br>    1. <a href="http://www.cheesecake.org/sac/smp.html" target="_blank">http://www.cheesecake.org/sac/smp.html</a><br>
</div><br>[ Sorry, I accidentally sent the earlier mail before composing the text fully. ]<br><br>Regards,<br>Srivatsa S. Bhat<br></div></blockquote></div>
<div><br> </div>
<div> </div>
<div>
<div>Awesome explanation Srivatsa!! Thanks a lot!!</div>
<div>Just had one more doubt. I am a little unclear about how the APs get initialized in the beginning. In the case of Boot Processor</div>
<div>its just like a uniprocessor system. But how do the APs start executing code ?</div>
<div>Could you please explain a little ?</div>
<div> </div>
<div>Thanx!!<br><br></div></div>