VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/alloc-r0drv-linux.c@ 96716

Last change on this file since 96716 was 96716, checked in by vboxsync, 2 years ago

Backed out r153600: Incorrect fix, bugref:4567:65.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.2 KB
Line 
1/* $Id: alloc-r0drv-linux.c 96716 2022-09-13 10:12:48Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-linux-kernel.h"
42#include "internal/iprt.h"
43#include <iprt/mem.h>
44
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include "r0drv/alloc-r0drv.h"
48
49
50#if (defined(RT_ARCH_AMD64) || defined(DOXYGEN_RUNNING)) && !defined(RTMEMALLOC_EXEC_HEAP)
51# if RTLNX_VER_MIN(2,6,23) && RTLNX_VER_MAX(5,8,0) && !RTLNX_RHEL_MAJ_PREREQ(8,5)
52/**
53 * Starting with 2.6.23 we can use __get_vm_area and map_vm_area to allocate
54 * memory in the moduel range. This is preferrable to the exec heap below.
55 */
56# define RTMEMALLOC_EXEC_VM_AREA
57# else
58/**
59 * We need memory in the module range (~2GB to ~0) this can only be obtained
60 * thru APIs that are not exported (see module_alloc()).
61 *
62 * So, we'll have to create a quick and dirty heap here using BSS memory.
63 * Very annoying and it's going to restrict us!
64 */
65# define RTMEMALLOC_EXEC_HEAP
66# endif
67#endif
68
69#ifdef RTMEMALLOC_EXEC_HEAP
70# include <iprt/heap.h>
71# include <iprt/spinlock.h>
72# include <iprt/errcore.h>
73#endif
74
75#include "internal/initterm.h"
76
77
78/*********************************************************************************************************************************
79* Structures and Typedefs *
80*********************************************************************************************************************************/
81#ifdef RTMEMALLOC_EXEC_VM_AREA
82/**
83 * Extended header used for headers marked with RTMEMHDR_FLAG_EXEC_VM_AREA.
84 *
85 * This is used with allocating executable memory, for things like generated
86 * code and loaded modules.
87 */
88typedef struct RTMEMLNXHDREX
89{
90 /** The VM area for this allocation. */
91 struct vm_struct *pVmArea;
92 void *pvDummy;
93 /** The header we present to the generic API. */
94 RTMEMHDR Hdr;
95} RTMEMLNXHDREX;
96AssertCompileSize(RTMEMLNXHDREX, 32);
97/** Pointer to an extended memory header. */
98typedef RTMEMLNXHDREX *PRTMEMLNXHDREX;
99#endif
100
101
102/*********************************************************************************************************************************
103* Global Variables *
104*********************************************************************************************************************************/
105#ifdef RTMEMALLOC_EXEC_HEAP
106/** The heap. */
107static RTHEAPSIMPLE g_HeapExec = NIL_RTHEAPSIMPLE;
108/** Spinlock protecting the heap. */
109static RTSPINLOCK g_HeapExecSpinlock = NIL_RTSPINLOCK;
110#endif
111
112
113/**
114 * API for cleaning up the heap spinlock on IPRT termination.
115 * This is as RTMemExecDonate specific to AMD64 Linux/GNU.
116 */
117DECLHIDDEN(void) rtR0MemExecCleanup(void)
118{
119#ifdef RTMEMALLOC_EXEC_HEAP
120 RTSpinlockDestroy(g_HeapExecSpinlock);
121 g_HeapExecSpinlock = NIL_RTSPINLOCK;
122#endif
123}
124
125
126/**
127 * Donate read+write+execute memory to the exec heap.
128 *
129 * This API is specific to AMD64 and Linux/GNU. A kernel module that desires to
130 * use RTMemExecAlloc on AMD64 Linux/GNU will have to donate some statically
131 * allocated memory in the module if it wishes for GCC generated code to work.
132 * GCC can only generate modules that work in the address range ~2GB to ~0
133 * currently.
134 *
135 * The API only accept one single donation.
136 *
137 * @returns IPRT status code.
138 * @retval VERR_NOT_SUPPORTED if the code isn't enabled.
139 * @param pvMemory Pointer to the memory block.
140 * @param cb The size of the memory block.
141 */
142RTR0DECL(int) RTR0MemExecDonate(void *pvMemory, size_t cb)
143{
144#ifdef RTMEMALLOC_EXEC_HEAP
145 int rc;
146 AssertReturn(g_HeapExec == NIL_RTHEAPSIMPLE, VERR_WRONG_ORDER);
147
148 rc = RTSpinlockCreate(&g_HeapExecSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTR0MemExecDonate");
149 if (RT_SUCCESS(rc))
150 {
151 rc = RTHeapSimpleInit(&g_HeapExec, pvMemory, cb);
152 if (RT_FAILURE(rc))
153 rtR0MemExecCleanup();
154 }
155 return rc;
156#else
157 RT_NOREF_PV(pvMemory); RT_NOREF_PV(cb);
158 return VERR_NOT_SUPPORTED;
159#endif
160}
161RT_EXPORT_SYMBOL(RTR0MemExecDonate);
162
163
164
165#ifdef RTMEMALLOC_EXEC_VM_AREA
166/**
167 * Allocate executable kernel memory in the module range.
168 *
169 * @returns Pointer to a allocation header success. NULL on failure.
170 *
171 * @param cb The size the user requested.
172 */
173static PRTMEMHDR rtR0MemAllocExecVmArea(size_t cb)
174{
175 size_t const cbAlloc = RT_ALIGN_Z(sizeof(RTMEMLNXHDREX) + cb, PAGE_SIZE);
176 size_t const cPages = cbAlloc >> PAGE_SHIFT;
177 struct page **papPages;
178 struct vm_struct *pVmArea;
179 size_t iPage;
180
181 pVmArea = __get_vm_area(cbAlloc, VM_ALLOC, MODULES_VADDR, MODULES_END);
182 if (!pVmArea)
183 return NULL;
184 pVmArea->nr_pages = 0; /* paranoia? */
185 pVmArea->pages = NULL; /* paranoia? */
186
187 papPages = (struct page **)kmalloc(cPages * sizeof(papPages[0]), GFP_KERNEL | __GFP_NOWARN);
188 if (!papPages)
189 {
190 vunmap(pVmArea->addr);
191 return NULL;
192 }
193
194 for (iPage = 0; iPage < cPages; iPage++)
195 {
196 papPages[iPage] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN);
197 if (!papPages[iPage])
198 break;
199 }
200 if (iPage == cPages)
201 {
202 /*
203 * Map the pages.
204 *
205 * Not entirely sure we really need to set nr_pages and pages here, but
206 * they provide a very convenient place for storing something we need
207 * in the free function, if nothing else...
208 */
209# if RTLNX_VER_MAX(3,17,0)
210 struct page **papPagesIterator = papPages;
211# endif
212 pVmArea->nr_pages = cPages;
213 pVmArea->pages = papPages;
214 if (!map_vm_area(pVmArea, PAGE_KERNEL_EXEC,
215# if RTLNX_VER_MAX(3,17,0)
216 &papPagesIterator
217# else
218 papPages
219# endif
220 ))
221 {
222 PRTMEMLNXHDREX pHdrEx = (PRTMEMLNXHDREX)pVmArea->addr;
223 pHdrEx->pVmArea = pVmArea;
224 pHdrEx->pvDummy = NULL;
225 return &pHdrEx->Hdr;
226 }
227 /* bail out */
228# if RTLNX_VER_MAX(3,17,0)
229 pVmArea->nr_pages = papPagesIterator - papPages;
230# endif
231 }
232
233 vunmap(pVmArea->addr);
234
235 while (iPage-- > 0)
236 __free_page(papPages[iPage]);
237 kfree(papPages);
238
239 return NULL;
240}
241#endif /* RTMEMALLOC_EXEC_VM_AREA */
242
243
244/**
245 * OS specific allocation function.
246 */
247DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
248{
249 PRTMEMHDR pHdr;
250 IPRT_LINUX_SAVE_EFL_AC();
251
252 /*
253 * Allocate.
254 */
255 if (fFlags & RTMEMHDR_FLAG_EXEC)
256 {
257 if (fFlags & RTMEMHDR_FLAG_ANY_CTX)
258 return VERR_NOT_SUPPORTED;
259
260#if defined(RT_ARCH_AMD64)
261# ifdef RTMEMALLOC_EXEC_HEAP
262 if (g_HeapExec != NIL_RTHEAPSIMPLE)
263 {
264 RTSpinlockAcquire(g_HeapExecSpinlock);
265 pHdr = (PRTMEMHDR)RTHeapSimpleAlloc(g_HeapExec, cb + sizeof(*pHdr), 0);
266 RTSpinlockRelease(g_HeapExecSpinlock);
267 fFlags |= RTMEMHDR_FLAG_EXEC_HEAP;
268 }
269 else
270 pHdr = NULL;
271
272# elif defined(RTMEMALLOC_EXEC_VM_AREA)
273 pHdr = rtR0MemAllocExecVmArea(cb);
274 fFlags |= RTMEMHDR_FLAG_EXEC_VM_AREA;
275
276# else /* !RTMEMALLOC_EXEC_HEAP */
277# error "you don not want to go here..."
278 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, MY_PAGE_KERNEL_EXEC);
279# endif /* !RTMEMALLOC_EXEC_HEAP */
280
281#elif defined(PAGE_KERNEL_EXEC) && defined(CONFIG_X86_PAE)
282 pHdr = (PRTMEMHDR)__vmalloc(cb + sizeof(*pHdr), GFP_KERNEL | __GFP_HIGHMEM | __GFP_NOWARN, MY_PAGE_KERNEL_EXEC);
283#else
284 pHdr = (PRTMEMHDR)vmalloc(cb + sizeof(*pHdr));
285#endif
286 }
287 else
288 {
289 if (
290#if 1 /* vmalloc has serious performance issues, avoid it. */
291 cb <= PAGE_SIZE*16 - sizeof(*pHdr)
292#else
293 cb <= PAGE_SIZE
294#endif
295 || (fFlags & RTMEMHDR_FLAG_ANY_CTX)
296 )
297 {
298 fFlags |= RTMEMHDR_FLAG_KMALLOC;
299 pHdr = kmalloc(cb + sizeof(*pHdr),
300 (fFlags & RTMEMHDR_FLAG_ANY_CTX_ALLOC) ? (GFP_ATOMIC | __GFP_NOWARN)
301 : (GFP_KERNEL | __GFP_NOWARN));
302 if (RT_UNLIKELY( !pHdr
303 && cb > PAGE_SIZE
304 && !(fFlags & RTMEMHDR_FLAG_ANY_CTX) ))
305 {
306 fFlags &= ~RTMEMHDR_FLAG_KMALLOC;
307 pHdr = vmalloc(cb + sizeof(*pHdr));
308 }
309 }
310 else
311 pHdr = vmalloc(cb + sizeof(*pHdr));
312 }
313 if (RT_UNLIKELY(!pHdr))
314 {
315 IPRT_LINUX_RESTORE_EFL_AC();
316 return VERR_NO_MEMORY;
317 }
318
319 /*
320 * Initialize.
321 */
322 pHdr->u32Magic = RTMEMHDR_MAGIC;
323 pHdr->fFlags = fFlags;
324 pHdr->cb = cb;
325 pHdr->cbReq = cb;
326
327 *ppHdr = pHdr;
328 IPRT_LINUX_RESTORE_EFL_AC();
329 return VINF_SUCCESS;
330}
331
332
333/**
334 * OS specific free function.
335 */
336DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
337{
338 IPRT_LINUX_SAVE_EFL_AC();
339
340 pHdr->u32Magic += 1;
341 if (pHdr->fFlags & RTMEMHDR_FLAG_KMALLOC)
342 kfree(pHdr);
343#ifdef RTMEMALLOC_EXEC_HEAP
344 else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_HEAP)
345 {
346 RTSpinlockAcquire(g_HeapExecSpinlock);
347 RTHeapSimpleFree(g_HeapExec, pHdr);
348 RTSpinlockRelease(g_HeapExecSpinlock);
349 }
350#endif
351#ifdef RTMEMALLOC_EXEC_VM_AREA
352 else if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC_VM_AREA)
353 {
354 PRTMEMLNXHDREX pHdrEx = RT_FROM_MEMBER(pHdr, RTMEMLNXHDREX, Hdr);
355 size_t iPage = pHdrEx->pVmArea->nr_pages;
356 struct page **papPages = pHdrEx->pVmArea->pages;
357 void *pvMapping = pHdrEx->pVmArea->addr;
358
359 vunmap(pvMapping);
360
361 while (iPage-- > 0)
362 __free_page(papPages[iPage]);
363 kfree(papPages);
364 }
365#endif
366 else
367 vfree(pHdr);
368
369 IPRT_LINUX_RESTORE_EFL_AC();
370}
371
372
373
374/**
375 * Compute order. Some functions allocate 2^order pages.
376 *
377 * @returns order.
378 * @param cPages Number of pages.
379 */
380static int CalcPowerOf2Order(unsigned long cPages)
381{
382 int iOrder;
383 unsigned long cTmp;
384
385 for (iOrder = 0, cTmp = cPages; cTmp >>= 1; ++iOrder)
386 ;
387 if (cPages & ~(1 << iOrder))
388 ++iOrder;
389
390 return iOrder;
391}
392
393
394/**
395 * Allocates physical contiguous memory (below 4GB).
396 * The allocation is page aligned and the content is undefined.
397 *
398 * @returns Pointer to the memory block. This is page aligned.
399 * @param pPhys Where to store the physical address.
400 * @param cb The allocation size in bytes. This is always
401 * rounded up to PAGE_SIZE.
402 */
403RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
404{
405 int cOrder;
406 unsigned cPages;
407 struct page *paPages;
408 void *pvRet;
409 IPRT_LINUX_SAVE_EFL_AC();
410
411 /*
412 * validate input.
413 */
414 AssertPtr(pPhys);
415 Assert(cb > 0);
416
417 /*
418 * Allocate page pointer array.
419 */
420 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
421 cPages = cb >> PAGE_SHIFT;
422 cOrder = CalcPowerOf2Order(cPages);
423#if (defined(RT_ARCH_AMD64) || defined(CONFIG_X86_PAE)) && defined(GFP_DMA32)
424 /* ZONE_DMA32: 0-4GB */
425 paPages = alloc_pages(GFP_DMA32 | __GFP_NOWARN, cOrder);
426 if (!paPages)
427#endif
428#ifdef RT_ARCH_AMD64
429 /* ZONE_DMA; 0-16MB */
430 paPages = alloc_pages(GFP_DMA | __GFP_NOWARN, cOrder);
431#else
432 /* ZONE_NORMAL: 0-896MB */
433 paPages = alloc_pages(GFP_USER | __GFP_NOWARN, cOrder);
434#endif
435 if (paPages)
436 {
437 /*
438 * Reserve the pages and mark them executable.
439 */
440 unsigned iPage;
441 for (iPage = 0; iPage < cPages; iPage++)
442 {
443 Assert(!PageHighMem(&paPages[iPage]));
444 if (iPage + 1 < cPages)
445 {
446 AssertMsg( (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage])) + PAGE_SIZE
447 == (uintptr_t)phys_to_virt(page_to_phys(&paPages[iPage + 1]))
448 && page_to_phys(&paPages[iPage]) + PAGE_SIZE
449 == page_to_phys(&paPages[iPage + 1]),
450 ("iPage=%i cPages=%u [0]=%#llx,%p [1]=%#llx,%p\n", iPage, cPages,
451 (long long)page_to_phys(&paPages[iPage]), phys_to_virt(page_to_phys(&paPages[iPage])),
452 (long long)page_to_phys(&paPages[iPage + 1]), phys_to_virt(page_to_phys(&paPages[iPage + 1])) ));
453 }
454
455 SetPageReserved(&paPages[iPage]);
456 }
457 *pPhys = page_to_phys(paPages);
458 pvRet = phys_to_virt(page_to_phys(paPages));
459 }
460 else
461 pvRet = NULL;
462
463 IPRT_LINUX_RESTORE_EFL_AC();
464 return pvRet;
465}
466RT_EXPORT_SYMBOL(RTMemContAlloc);
467
468
469/**
470 * Frees memory allocated using RTMemContAlloc().
471 *
472 * @param pv Pointer to return from RTMemContAlloc().
473 * @param cb The cb parameter passed to RTMemContAlloc().
474 */
475RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
476{
477 if (pv)
478 {
479 int cOrder;
480 unsigned cPages;
481 unsigned iPage;
482 struct page *paPages;
483 IPRT_LINUX_SAVE_EFL_AC();
484
485 /* validate */
486 AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
487 Assert(cb > 0);
488
489 /* calc order and get pages */
490 cb = RT_ALIGN_Z(cb, PAGE_SIZE);
491 cPages = cb >> PAGE_SHIFT;
492 cOrder = CalcPowerOf2Order(cPages);
493 paPages = virt_to_page(pv);
494
495 /*
496 * Restore page attributes freeing the pages.
497 */
498 for (iPage = 0; iPage < cPages; iPage++)
499 {
500 ClearPageReserved(&paPages[iPage]);
501 }
502 __free_pages(paPages, cOrder);
503 IPRT_LINUX_RESTORE_EFL_AC();
504 }
505}
506RT_EXPORT_SYMBOL(RTMemContFree);
507
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette