Skip to content

Commit fb6b3cd

Browse files
konstantin-s-bogomgvisor-bot
authored andcommitted
Don't panic on KVM init if no memory for new vCPU.
This should be an indication to the user to set greater memory limits, a clean exit with an error should do that. PiperOrigin-RevId: 759294902
1 parent 16bb0d3 commit fb6b3cd

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

pkg/sentry/platform/kvm/machine.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ type dieState struct {
227227
// createVCPU creates and returns a new vCPU.
228228
//
229229
// Precondition: mu must be held.
230-
func (m *machine) createVCPU(id int) *vCPU {
230+
func (m *machine) createVCPU(id int) (*vCPU, error) {
231231
// Create the vCPU.
232232
fd, errno := hostsyscall.RawSyscall(unix.SYS_IOCTL, uintptr(m.fd), KVM_CREATE_VCPU, uintptr(id))
233233
if errno != 0 {
234-
panic(fmt.Sprintf("error creating new vCPU(id=%d): %v", id, errno))
234+
return nil, fmt.Errorf("error creating new vCPU(id=%d): %w", id, errno)
235235
}
236236

237237
c := &vCPU{
@@ -259,7 +259,7 @@ func (m *machine) createVCPU(id int) *vCPU {
259259
panic(fmt.Sprintf("error initializing vCPU(id=%d) state: %v", id, err))
260260
}
261261

262-
return c // Done.
262+
return c, nil // Done.
263263
}
264264

265265
// forceMappingEntireAddressSpace forces mapping the entire process address

pkg/sentry/platform/kvm/machine_amd64.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ func (m *machine) initArchState() error {
5050

5151
// Initialize all vCPUs to minimize kvm ioctl-s allowed by seccomp filters.
5252
m.mu.Lock()
53+
defer m.mu.Unlock()
5354
for i := 0; i < m.maxVCPUs; i++ {
54-
m.createVCPU(i)
55+
if _, err := m.createVCPU(i); err != nil {
56+
return err
57+
}
5558
}
56-
m.mu.Unlock()
5759

5860
return nil
5961
}

pkg/sentry/platform/kvm/machine_arm64_unsafe.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@ func (m *machine) initArchState() error {
4949
panic(fmt.Sprintf("error setting KVM_ARM_PREFERRED_TARGET failed: %v", errno))
5050
}
5151

52-
// Initialize all vCPUs on ARM64, while this does not happen on x86_64.
53-
// The reason for the difference is that ARM64 and x86_64 have different KVM timer mechanisms.
54-
// If we create vCPU dynamically on ARM64, the timer for vCPU would mess up for a short time.
52+
// Initialize all vCPUs on ARM64 (we also do this on x86_64, but for
53+
// ARM64 it is especially important as it has a different KVM timer
54+
// mechanism).
55+
// If we create vCPU dynamically on ARM64, the timer for vCPU would mess
56+
// up for a short time.
5557
// For more detail, please refer to https://github.com/google/gvisor/issues/5739
5658
m.mu.Lock()
59+
defer m.mu.Unlock()
5760
for i := 0; i < m.maxVCPUs; i++ {
58-
m.createVCPU(i)
61+
if _, err := m.createVCPU(i); err != nil {
62+
return err
63+
}
5964
}
60-
m.mu.Unlock()
6165
return nil
6266
}
6367

0 commit comments

Comments
 (0)