The Problem
Some time ago I upgraded my memory from the standard (4G I think) to 16G, via Crucial. Crucial is awesome and I highly recommend them. While I haven't had any memory issues whatsoever, and running 16G is just hot, I've had issues when my lappy has resumed from hibernation.Lion (and older OS X versions) supports what they call SafeSleep. While SafeSleep is probably not needed on Lion since it has the fancy Resume features in the applications, by default on portables, it's enabled. The pmset manual page makes this statement:
hibernatemode = 3 (binary 0011) by default on supported portables. The system will store a copy of memory to persistent storage (the disk), and will power memory during sleep. The system will wake from memory, unless a power loss forces it to restore from disk image.In more detail:
0000 0001 (bit 0) enables hibernation; causes OS X to write memory state to hibernation image at sleep time. On wake (without bit 1 set) OS X will resume from the hibernation image. Bit 0 set (without bit 1 set) causes OS X to write memory state and immediately hibernate at sleep time.Last night I forgot to plug in my lappy. Now, my lappy usually doesn't go into hibernation often. In fact, I think this was the second time it has since I got it. And just like the first time, it didn't restore itself properly. During what I assume was the EFI boot sequence, about 1/2 way during restore, the system started beeping. Like, in that "Oh fuck something's wrong" way. Assuming that the file system was probably not in a happy state either, this could definitely lead to bad mojo down the road. So, how to fix?
0000 0010 (bit 1), in conjunction with bit 0, causes OS X to maintain system state in memory and leave system power on until battery level drops below a near empty threshold (This enables quicker wakeup from memory while battery power is available). Upon nearly emptying the battery, OS X shuts off all system power and hibernates; on wake the system will resume from hibernation image, not from memory.
Well, gotta figure out the problem first, right?
The Test
My first idea was that the 16G memory, which isn't technically supported by Apple, was causing a hiccup somewhere. Since the problem seemed in restore, I figured if I could perform a restore with the original memory, and then the 16G, that would reasonably rule out just having an unsupported memory size. Here's the test I went with:- Original Memory Test:
- Fully shutdown, including a power down.
- Swap out 16G with original memory.
- Power back up.
- Force hibernation / SafeSleep.
- Restore from hibernation. Does this work? Maybe hibernation is just broken with FileVault and my setup...
- 16G Memory Test:
- If the above works, then power down again.
- Swap out original with 16G.
- Repeat everything else above.
Reading more of the pmset manual page, I came across the standby options:
standby causes kernel power management to automatically hibernate a machine after it has slept for a specified time period. This saves power while asleep. This setting defaults to ON for supported hardware. The setting standby will be visible in pmset -g if the feature is supported on this machine.My hack-around was to enable standby, which was disabled by default, set the standbydelay to a low value (30), and then put the laptop to sleep. My lappy's configured to sleep when only on battery power, so I just disconnected it and put it to sleep via the Sleep option in the drop down Apple menu. This was done via the following command:
standby only works if hibernation is turned on to hibernatemode 3 or 25.
standbydelay specifies the delay, in seconds, before writing the hibernation image to disk and powering off memory for Standby.
sudo pmset -b standby 1 standbydelay 30I performed the test above first with original memory and then with the 16G. In both cases, the laptop was able to resume without a problem and without beeping. Sweet! The 16G in and of itself wasn't an issue. Hmm, what was?
The Guess
Re-reading the pmset manual and the hibernation options, the following popped out (emphasis added):0000 0010 (bit 1), in conjunction with bit 0, causes OS X to maintain system state in memory and leave system power on until battery level drops below a near empty threshold (This enables quicker wakeup from memory while battery power is available). Upon nearly emptying the battery, OS X shuts off all system power and hibernates; on wake the system will resume from hibernation image, not from memory.OK, so here's the guess. I'm running of an HDD versus an SSD. Having 16G of data written to disk takes longer that what the original memory is at. My guess is this threshold value is set to a low time remaining value, say 10 minutes estimated remaining. Since the 16G is a larger value than expected, it might not actually be fully written to disk when the battery has drained. And since it's writing more to disk, it's using more battery power, depleting it sooner than expected.
The Love
While I was reading the pmset manual, I came across this tidbit:destroyfvkeyonstandby - Destroy File Vault Key when going to standbymode. By default File vault keys are retained even when system goes to standby. If the keys are destroyed, user will be prompted to enter the password while coming out of standby mode.(value: 1 - Destroy, 0 - Retain)@diretraversal pointed me to a link on someone using the Thunderbolt slot to access memory again (similar to the Firewire attack of old). Now, since I am using sleep → standby → hibernate and also using FileVault, I could take advantage of this option as a best effort defense against someone attacking my memory and recovering my FileVault key. This option was enabled via the following command:
sudo pmset -b destroyfvkeyonstandby 1While I already had "Require password immediately after sleep or screen saver begins", there wasn't a guarantee that the key was actually cleared; I just was asked for the password again. Now it seems like it is, although it's just a setting. Who knows what's going on under the hood :)
The Recap
- Lappy wasn't restoring correctly from hibernation.
- In all likelihood, this was because OS X didn't write to disk early enough.
- The lappy goes into sleep after 15 idle minutes pass whilst unplugged or the lid closed.
- It now goes into standby mode 30 seconds after that, which forces a hibernation mode and memory power down.
- And as a check, the FileVault key is (hopefully) wiped from existence whenever standby occurs.
Update 1: nice link to someone actually doing an attack against FileVault2 in Lion.