| 1 |
Solaris Kernel Address Filtering in lsof 4.50 and Above |
| 2 |
|
| 3 |
Current Filter |
| 4 |
============== |
| 5 |
|
| 6 |
Lsof revisions 4.49 and below, have exactly one filter: the kernel |
| 7 |
virtual address is checked against the kernel's virtual address |
| 8 |
base -- e.g., what's found in the kernel variable kernelbase. For |
| 9 |
sun4m that's 0xf0000000, for sun4u, 0x10000000. |
| 10 |
|
| 11 |
This filter keeps lsof from handing some bad addresses to the |
| 12 |
kernel, but not all bad addresses. For example, the virtual address |
| 13 |
0x657a682e passes this test on a sun4u machine, but on at least |
| 14 |
one sun4u that virtual address translates to the physical address |
| 15 |
0x1cf08c30000, which is the address of a register of a qfe interface |
| 16 |
on the machine. There is some evidence that a kvm_kread() call for |
| 17 |
the 0x657a682e address may crash that sun4u. |
| 18 |
|
| 19 |
Lsof 4.71 and above use no filter if they detect that /dev/allkmem |
| 20 |
exists. That is done because, when /dev/allkmem exists, /dev/kmem has |
| 21 |
address filtering in its device driver. |
| 22 |
|
| 23 |
|
| 24 |
====================== |
| 25 |
!!!IMPORTANT UPDATE!!! |
| 26 |
====================== |
| 27 |
|
| 28 |
In late May 2002 I learned that Sun had reports of other kernel |
| 29 |
crashes, caused by adb, lsof, and mdb, related to incorrect addresses |
| 30 |
being supplied to /dev/kmem. (This report was written originally |
| 31 |
on July 18, 2000.) |
| 32 |
|
| 33 |
The problem is described in and fixed or patched: |
| 34 |
|
| 35 |
Solaris 7: SPARC kernel patch 106541-20 |
| 36 |
Intel kernel patch 106542-20 |
| 37 |
|
| 38 |
Solaris 8: SPARC kernel patch 108528-14 |
| 39 |
Intel kernel patch 108529-14 |
| 40 |
|
| 41 |
Solaris 9: bug 4344513 |
| 42 |
|
| 43 |
So, if you want to be comfortable using lsof (or adb or mdb) with |
| 44 |
Solaris, install the appropriate Solaris 7 or 8 patches, or upgrade |
| 45 |
to Solaris 9. |
| 46 |
|
| 47 |
Note that these patches provide the /dev/allkmem device, whose presence |
| 48 |
causes lsof to rely on the address filtering of the /dev/kmem device. |
| 49 |
|
| 50 |
|
| 51 |
New Filters |
| 52 |
=========== |
| 53 |
|
| 54 |
Lsof 4.50 adds additional filters to the kernelbase check. The |
| 55 |
filters differ, based on the Solaris version: |
| 56 |
|
| 57 |
Solaris |
| 58 |
Version New Filters |
| 59 |
======= =========== |
| 60 |
|
| 61 |
2.5 and below none |
| 62 |
2.5.1 kvm_physaddr() (-lkvm), caching, llseek(), |
| 63 |
and /dev/mem |
| 64 |
2.6 kvm_physaddr() (-lkvm), caching, llseek(), |
| 65 |
and /dev/mem |
| 66 |
7, 8, and 9 kvm_physaddr() (ioctl()), caching, and |
| 67 |
kvm_pread() |
| 68 |
|
| 69 |
See !!!IMPORTANT NOTICE!! above for |
| 70 |
information on a Solaris 9 bug report about, |
| 71 |
or Solaris 7 and 8 kernel patches to the |
| 72 |
kernel /dev/kmem driver. Those fixes |
| 73 |
obviate the need for the kernel address |
| 74 |
filtering described in this report. |
| 75 |
|
| 76 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 77 |
!!! I STRONGLY RECOMMEND YOU INSTALL !!! |
| 78 |
!!! THE PATCHES OR UPGRADE TO SOLARIS !!! |
| 79 |
!!! 9. !!! |
| 80 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 81 |
|
| 82 |
kvm_physaddr() (-lkvm) |
| 83 |
====================== |
| 84 |
|
| 85 |
Solaris has an undocumented function called kvm_physaddr() that |
| 86 |
will convert a kernel virtual address to a kernel physical address. |
| 87 |
(Until Solaris 7 this function doesn't even have a prototype |
| 88 |
definition in <kvm.h>.) |
| 89 |
|
| 90 |
I have been assured repeatedly by Casper Dik of Sun that this |
| 91 |
function, when given a kernel virtual address, will produce addresses |
| 92 |
of physical memory only; it will not produce physical addresses of |
| 93 |
interface registers, such as the one for the qfe interface. |
| 94 |
|
| 95 |
In Solaris 2.5.1 this function runs in application space from within |
| 96 |
the KVM library. Since it needs to know the components of the |
| 97 |
kernel's address space map, it must read those from kernel memory |
| 98 |
each time it is called. That can be time consuming. |
| 99 |
|
| 100 |
I'm not sure about kvm_physaddr() for Solaris 2.6. It may still |
| 101 |
run in application space from within the KVM library, but if so, |
| 102 |
it is much faster than its 2.5.1 ancestor. |
| 103 |
|
| 104 |
kvm_physaddr() (ioctl()) |
| 105 |
======================== |
| 106 |
|
| 107 |
I'm sure that at Solaris 7 and above kvm_physaddr() has moved inside |
| 108 |
the kernel and is called with an ioctl(). That makes it much faster |
| 109 |
than its ancestors. |
| 110 |
|
| 111 |
kvm_physaddr() Use |
| 112 |
================== |
| 113 |
|
| 114 |
Lsof 4.50 for Solaris will use one or the other version of |
| 115 |
kvm_physaddr() for Solaris 2.5.1, 2.6, 7, and 8. |
| 116 |
|
| 117 |
Using it for Solaris 2.5.1 causes lsof to take four times as much |
| 118 |
real time as it formerly did with only the kernelbase filtering. |
| 119 |
|
| 120 |
Caching |
| 121 |
======= |
| 122 |
|
| 123 |
To recover the performance lost by kvm_physaddr() on Solaris 2.5.1, |
| 124 |
I added virtual-to-physical address caching to lsof's kernel read |
| 125 |
function, kread(). This improves Solaris 2.6, 7, and 8 performance, |
| 126 |
too, but by a smaller amount. |
| 127 |
|
| 128 |
It turns out that a typical lsof run may require reading from 16,000 |
| 129 |
or more different kernel virtual addresses. However, it also turns |
| 130 |
out that those addresses are contained within about 600 distinct |
| 131 |
kernel memory pages. |
| 132 |
|
| 133 |
To exploit this condition lsof caches each virtual page address |
| 134 |
that has a corresponding legitimate physical page address for use |
| 135 |
in checking later addresses. This caching regains all but a bit |
| 136 |
of the performance loss on Solaris 2.5.1. |
| 137 |
|
| 138 |
Caching can provide some performance gain on Solaris 2.6, 7, and |
| 139 |
8, but it's not nearly as large as the gain for 2.5.1, and may |
| 140 |
depend on the machine architecture type. |
| 141 |
|
| 142 |
/dev/mem |
| 143 |
======== |
| 144 |
|
| 145 |
Once lsof has kernel physical addresses, on Solaris 2.5.1 and 2.6 |
| 146 |
it seeks to those addresses with llseek() and reads from them via |
| 147 |
the /dev/mem device. This contrasts with lsof's pre-4.50 behavior |
| 148 |
where it fed kernel virtual addresses to kvm_kread(), letting it |
| 149 |
and the kernel do the virtual to physical translations -- and |
| 150 |
letting that combined process crash that one unlucky sun4u via its |
| 151 |
qfe interface. |
| 152 |
|
| 153 |
Using /dev/mem requires no more permission for lsof, but it does |
| 154 |
require an additional open file descriptor and use of the 64 bit |
| 155 |
llseek() function. |
| 156 |
|
| 157 |
The additional file descriptor is an unfortunate consequence of |
| 158 |
the KVM library's opacity. The library usually has /dev/kmem open |
| 159 |
to a file descriptor, but lsof can't easily get at that descriptor, |
| 160 |
so it opens one of its own. |
| 161 |
|
| 162 |
On Solaris 2.6 for one test system, a 4 CPU E4000 sun4u, doing |
| 163 |
physical kernel address reads from /dev/mem turned out to be faster |
| 164 |
than using kvm_kread(). It was marginally faster on a sun4d, and |
| 165 |
marginally slower on two sun4m's. |
| 166 |
|
| 167 |
kvm_pread() |
| 168 |
=========== |
| 169 |
|
| 170 |
Even though it is still undocumented, the kvm_physaddr() function |
| 171 |
is represented by a prototype in the Solaris 7 and 8 <kvm.h>. |
| 172 |
Additionally useful is another undocumented function, kvm_pread() |
| 173 |
(for physical read), that also is represented by a <kvm.h> prototype |
| 174 |
in Solaris 7 and 8. |
| 175 |
|
| 176 |
Lsof 4.50 for Solaris 7 and 8 uses kvm_pread() instead of opening |
| 177 |
a descriptor to /dev/mem, llseek()-ing to physical addresses in |
| 178 |
it, and using read(2) to obtain physical address contents. The |
| 179 |
bonus of kvm_pread() is two-fold: 1) it does positioning as well |
| 180 |
as reading, so there's one less function call; and 2) its combined |
| 181 |
operation appears to be faster than llseek() plus read() -- or even |
| 182 |
kvm_kread(). |
| 183 |
|
| 184 |
Combined with the virtual-to-physical address caching, the performance |
| 185 |
boost of kvm_pread() makes lsof faster on Solaris 7 and 8 than |
| 186 |
previous revisions, using only kernelbase filtering and kvm_kread(). |
| 187 |
|
| 188 |
Remaining Risks |
| 189 |
=============== |
| 190 |
|
| 191 |
There may remain some extremely small likelihood that lsof will |
| 192 |
transmit a bad physical address to the kernel. Here are some |
| 193 |
possible failure scenarios: |
| 194 |
|
| 195 |
* The physical address filters haven't been tested on |
| 196 |
the machine whose qfe interface was affected. That's |
| 197 |
because the machine's memory configuration was changed |
| 198 |
before the test could be run. |
| 199 |
|
| 200 |
* The kvm_physaddr() function, especially in Solaris |
| 201 |
2.5.1, might fail to map an address correctly. Only |
| 202 |
Sun can correct this problem. |
| 203 |
|
| 204 |
* Because lsof must read the kernel address map from |
| 205 |
kernel virtual memory to pass it to the Solaris 2.5.1 |
| 206 |
and 2.6 kvm_physaddr() functions, lsof must use kvm_kread() |
| 207 |
to read the map. |
| 208 |
|
| 209 |
There's also the chance that lsof could pass a stale |
| 210 |
kernel address map to kvm_physaddr(), because re-reading |
| 211 |
it for each call to kvm_physaddr() would lead to |
| 212 |
unacceptable performance. When in repeat mode lsof |
| 213 |
re-reads the map between each cycle. |
| 214 |
|
| 215 |
On Solaris 7 and 8, since kvm_physaddr() is inside the |
| 216 |
kernel, there's no chance of its having a stale address |
| 217 |
map. |
| 218 |
|
| 219 |
* There's an extremely small chance that a cached |
| 220 |
virtual+physical page address could become invalid. |
| 221 |
This is so small I think it can be ignored, since the |
| 222 |
kernel memory map rarely changes. |
| 223 |
|
| 224 |
When in repeat mode, lsof clears its virtual+physical |
| 225 |
address map between cycles. |
| 226 |
|
| 227 |
* Lsof still uses Sun's kvm_getproc() (from -lkvm), and |
| 228 |
I have no idea what kernel address filtering it does, |
| 229 |
if any. |
| 230 |
|
| 231 |
I wish to acknowledge: Casper Dik of Sun, who provided information |
| 232 |
about kvm_physaddr() and helped test the lsof changes; Jim Mewes |
| 233 |
of Phone.com, who reported the initial problem and helped test the |
| 234 |
lsof changes; and several readers of the lsof-l listserv, who |
| 235 |
volunteered to run test programs. |
| 236 |
|
| 237 |
|
| 238 |
Vic Abell |
| 239 |
March 16, 2004 |