1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mm
import (
"bytes"
"fmt"
"strings"
"gvisor.dev/gvisor/pkg/sentry/context"
"gvisor.dev/gvisor/pkg/sentry/fs/proc/seqfile"
"gvisor.dev/gvisor/pkg/sentry/memmap"
"gvisor.dev/gvisor/pkg/sentry/usermem"
)
const (
// devMinorBits is the number of minor bits in a device number. Linux:
// include/linux/kdev_t.h:MINORBITS
devMinorBits = 20
vsyscallEnd = usermem.Addr(0xffffffffff601000)
vsyscallMapsEntry = "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]\n"
vsyscallSmapsEntry = vsyscallMapsEntry +
"Size: 4 kB\n" +
"Rss: 0 kB\n" +
"Pss: 0 kB\n" +
"Shared_Clean: 0 kB\n" +
"Shared_Dirty: 0 kB\n" +
"Private_Clean: 0 kB\n" +
"Private_Dirty: 0 kB\n" +
"Referenced: 0 kB\n" +
"Anonymous: 0 kB\n" +
"AnonHugePages: 0 kB\n" +
"Shared_Hugetlb: 0 kB\n" +
"Private_Hugetlb: 0 kB\n" +
"Swap: 0 kB\n" +
"SwapPss: 0 kB\n" +
"KernelPageSize: 4 kB\n" +
"MMUPageSize: 4 kB\n" +
"Locked: 0 kB\n" +
"VmFlags: rd ex \n"
)
// NeedsUpdate implements seqfile.SeqSource.NeedsUpdate.
func (mm *MemoryManager) NeedsUpdate(generation int64) bool {
return true
}
// ReadMapsSeqFileData is called by fs/proc.mapsData.ReadSeqFileData to
// implement /proc/[pid]/maps.
func (mm *MemoryManager) ReadMapsSeqFileData(ctx context.Context, handle seqfile.SeqHandle) ([]seqfile.SeqData, int64) {
mm.mappingMu.RLock()
defer mm.mappingMu.RUnlock()
var data []seqfile.SeqData
var start usermem.Addr
if handle != nil {
start = *handle.(*usermem.Addr)
}
for vseg := mm.vmas.LowerBoundSegment(start); vseg.Ok(); vseg = vseg.NextSegment() {
// FIXME(b/30793614): If we use a usermem.Addr for the handle, we get
// "panic: autosave error: type usermem.Addr is not registered".
vmaAddr := vseg.End()
data = append(data, seqfile.SeqData{
Buf: mm.vmaMapsEntryLocked(ctx, vseg),
Handle: &vmaAddr,
})
}
// We always emulate vsyscall, so advertise it here. Everything about a
// vsyscall region is static, so just hard code the maps entry since we
// don't have a real vma backing it. The vsyscall region is at the end of
// the virtual address space so nothing should be mapped after it (if
// something is really mapped in the tiny ~10 MiB segment afterwards, we'll
// get the sorting on the maps file wrong at worst; but that's not possible
// on any current platform).
//
// Artifically adjust the seqfile handle so we only output vsyscall entry once.
if start != vsyscallEnd {
// FIXME(b/30793614): Can't get a pointer to constant vsyscallEnd.
vmaAddr := vsyscallEnd
data = append(data, seqfile.SeqData{
Buf: []byte(vsyscallMapsEntry),
Handle: &vmaAddr,
})
}
return data, 1
}
// vmaMapsEntryLocked returns a /proc/[pid]/maps entry for the vma iterated by
// vseg, including the trailing newline.
//
// Preconditions: mm.mappingMu must be locked.
func (mm *MemoryManager) vmaMapsEntryLocked(ctx context.Context, vseg vmaIterator) []byte {
var b bytes.Buffer
mm.appendVMAMapsEntryLocked(ctx, vseg, &b)
return b.Bytes()
}
// Preconditions: mm.mappingMu must be locked.
func (mm *MemoryManager) appendVMAMapsEntryLocked(ctx context.Context, vseg vmaIterator, b *bytes.Buffer) {
vma := vseg.ValuePtr()
private := "p"
if !vma.private {
private = "s"
}
var dev, ino uint64
if vma.id != nil {
dev = vma.id.DeviceID()
ino = vma.id.InodeID()
}
devMajor := uint32(dev >> devMinorBits)
devMinor := uint32(dev & ((1 << devMinorBits) - 1))
// Do not include the guard page: fs/proc/task_mmu.c:show_map_vma() =>
// stack_guard_page_start().
fmt.Fprintf(b, "%08x-%08x %s%s %08x %02x:%02x %d ",
vseg.Start(), vseg.End(), vma.realPerms, private, vma.off, devMajor, devMinor, ino)
// Figure out our filename or hint.
var s string
if vma.hint != "" {
s = vma.hint
} else if vma.id != nil {
// FIXME(jamieliu): We are holding mm.mappingMu here, which is
// consistent with Linux's holding mmap_sem in
// fs/proc/task_mmu.c:show_map_vma() => fs/seq_file.c:seq_file_path().
// However, it's not clear that fs.File.MappedName() is actually
// consistent with this lock order.
s = vma.id.MappedName(ctx)
}
if s != "" {
// Per linux, we pad until the 74th character.
if pad := 73 - b.Len(); pad > 0 {
b.WriteString(strings.Repeat(" ", pad))
}
b.WriteString(s)
}
b.WriteString("\n")
}
// ReadSmapsSeqFileData is called by fs/proc.smapsData.ReadSeqFileData to
// implement /proc/[pid]/smaps.
func (mm *MemoryManager) ReadSmapsSeqFileData(ctx context.Context, handle seqfile.SeqHandle) ([]seqfile.SeqData, int64) {
mm.mappingMu.RLock()
defer mm.mappingMu.RUnlock()
var data []seqfile.SeqData
var start usermem.Addr
if handle != nil {
start = *handle.(*usermem.Addr)
}
for vseg := mm.vmas.LowerBoundSegment(start); vseg.Ok(); vseg = vseg.NextSegment() {
// FIXME(b/30793614): If we use a usermem.Addr for the handle, we get
// "panic: autosave error: type usermem.Addr is not registered".
vmaAddr := vseg.End()
data = append(data, seqfile.SeqData{
Buf: mm.vmaSmapsEntryLocked(ctx, vseg),
Handle: &vmaAddr,
})
}
// We always emulate vsyscall, so advertise it here. See
// ReadMapsSeqFileData for additional commentary.
if start != vsyscallEnd {
// FIXME(b/30793614): Can't get a pointer to constant vsyscallEnd.
vmaAddr := vsyscallEnd
data = append(data, seqfile.SeqData{
Buf: []byte(vsyscallSmapsEntry),
Handle: &vmaAddr,
})
}
return data, 1
}
// vmaSmapsEntryLocked returns a /proc/[pid]/smaps entry for the vma iterated
// by vseg, including the trailing newline.
//
// Preconditions: mm.mappingMu must be locked.
func (mm *MemoryManager) vmaSmapsEntryLocked(ctx context.Context, vseg vmaIterator) []byte {
var b bytes.Buffer
mm.appendVMAMapsEntryLocked(ctx, vseg, &b)
vma := vseg.ValuePtr()
// We take mm.activeMu here in each call to vmaSmapsEntryLocked, instead of
// requiring it to be locked as a precondition, to reduce the latency
// impact of reading /proc/[pid]/smaps on concurrent performance-sensitive
// operations requiring activeMu for writing like faults.
mm.activeMu.RLock()
var rss uint64
var anon uint64
vsegAR := vseg.Range()
for pseg := mm.pmas.LowerBoundSegment(vsegAR.Start); pseg.Ok() && pseg.Start() < vsegAR.End; pseg = pseg.NextSegment() {
psegAR := pseg.Range().Intersect(vsegAR)
size := uint64(psegAR.Length())
rss += size
if pseg.ValuePtr().private {
anon += size
}
}
mm.activeMu.RUnlock()
fmt.Fprintf(&b, "Size: %8d kB\n", vseg.Range().Length()/1024)
fmt.Fprintf(&b, "Rss: %8d kB\n", rss/1024)
// Currently we report PSS = RSS, i.e. we pretend each page mapped by a pma
// is only mapped by that pma. This avoids having to query memmap.Mappables
// for reference count information on each page. As a corollary, all pages
// are accounted as "private" whether or not the vma is private; compare
// Linux's fs/proc/task_mmu.c:smaps_account().
fmt.Fprintf(&b, "Pss: %8d kB\n", rss/1024)
fmt.Fprintf(&b, "Shared_Clean: %8d kB\n", 0)
fmt.Fprintf(&b, "Shared_Dirty: %8d kB\n", 0)
// Pretend that all pages are dirty if the vma is writable, and clean otherwise.
clean := rss
if vma.effectivePerms.Write {
clean = 0
}
fmt.Fprintf(&b, "Private_Clean: %8d kB\n", clean/1024)
fmt.Fprintf(&b, "Private_Dirty: %8d kB\n", (rss-clean)/1024)
// Pretend that all pages are "referenced" (recently touched).
fmt.Fprintf(&b, "Referenced: %8d kB\n", rss/1024)
fmt.Fprintf(&b, "Anonymous: %8d kB\n", anon/1024)
// Hugepages (hugetlb and THP) are not implemented.
fmt.Fprintf(&b, "AnonHugePages: %8d kB\n", 0)
fmt.Fprintf(&b, "Shared_Hugetlb: %8d kB\n", 0)
fmt.Fprintf(&b, "Private_Hugetlb: %7d kB\n", 0)
// Swap is not implemented.
fmt.Fprintf(&b, "Swap: %8d kB\n", 0)
fmt.Fprintf(&b, "SwapPss: %8d kB\n", 0)
fmt.Fprintf(&b, "KernelPageSize: %8d kB\n", usermem.PageSize/1024)
fmt.Fprintf(&b, "MMUPageSize: %8d kB\n", usermem.PageSize/1024)
locked := rss
if vma.mlockMode == memmap.MLockNone {
locked = 0
}
fmt.Fprintf(&b, "Locked: %8d kB\n", locked/1024)
b.WriteString("VmFlags: ")
if vma.realPerms.Read {
b.WriteString("rd ")
}
if vma.realPerms.Write {
b.WriteString("wr ")
}
if vma.realPerms.Execute {
b.WriteString("ex ")
}
if vma.canWriteMappableLocked() { // VM_SHARED
b.WriteString("sh ")
}
if vma.maxPerms.Read {
b.WriteString("mr ")
}
if vma.maxPerms.Write {
b.WriteString("mw ")
}
if vma.maxPerms.Execute {
b.WriteString("me ")
}
if !vma.private { // VM_MAYSHARE
b.WriteString("ms ")
}
if vma.growsDown {
b.WriteString("gd ")
}
if vma.mlockMode != memmap.MLockNone { // VM_LOCKED
b.WriteString("lo ")
}
if vma.mlockMode == memmap.MLockLazy { // VM_LOCKONFAULT
b.WriteString("?? ") // no explicit encoding in fs/proc/task_mmu.c:show_smap_vma_flags()
}
if vma.private && vma.effectivePerms.Write { // VM_ACCOUNT
b.WriteString("ac ")
}
b.WriteString("\n")
return b.Bytes()
}
|