Skip to contentSkip to conversation

vfree crash

Xen-devel Petre Ovidiu PIRCALABU 4 participants 10 messages Jun 28, 2019 – Jul 3, 2019

lore
Share & tools
lore
#0 lore raw

Hello,

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource
....
- xenforeignmemory_unmap_resource
- vfree.

(XEN) ----[ Xen-4.13-unstable x86_64 debug=y Tainted: C ]----
(XEN) CPU: 6
(XEN) RIP: e008:[<ffff82d08022618d>] free_domheap_pages+0x2d0/0x40d
(XEN) RFLAGS: 0000000000010246 CONTEXT: hypervisor (d0v3)
(XEN) rax: ffff82e00bf6ee00 rbx: ffff830806584000 rcx:
ffff82ffffffffe0
(XEN) rdx: ffff82ffffffffe0 rsi: 0000000000000000 rdi:
ffff830806584028
(XEN) rbp: ffff8308065a7c78 rsp:
ffff8308065a7c38 r8: 00000000ffffffff
(XEN) r9: 0000000000000001 r10: ffff82e000000000 r11:
4000000000000000
(XEN) r12: ffff82e00bf6ee00 r13: ffff830806584028 r14:
ffff830806584038
(XEN) r15: 00ffffffffffffff cr0: 0000000080050033 cr4:
0000000000362660
(XEN) cr3: 00000005fb565000 cr2: ffff82ffffffffe0
(XEN) fsb: 00007f1265e36700 gsb: ffff8882168c0000 gss:
0000000000000000
(XEN) ds: 0000 es: 0000 fs: 0000 gs: 0000 ss: e010 cs: e008
(XEN) Xen code around <ffff82d08022618d> (free_domheap_pages+0x2d0/0x40d):
(XEN) 47 08 0f 85 0f 01 00 00 <c7> 02 ff ff ff ff 48 89 57 08 eb 4b 48 39 83 30
(XEN) Xen stack trace from rsp=ffff8308065a7c38:
(XEN) ffff830806584020 0000000000000001 ffff8308065a7c78
ffff82e00bf6ee00
(XEN) 0000000000000000 ffff82e000000000 0000000000000001
000ffffffffff000
(XEN) ffff8308065a7cd8 ffff82d08024323f 0000000000000000
ffff82c000267000
(XEN) 0000000000000000 00000001065a7ca8 8086000000008086
ffff8305f822bfe0
(XEN) ffff8305fd17a000 00007f1265e53010 ffff8305fd17a000
0000000000000000
(XEN) ffff8308065a7d28 ffff82d0802484fc ffff8305fd17a000
0000000000000000
(XEN) 0000000000000292 0000000000000000 ffff8305fd17a000
00007f1265e53010
(XEN) ffff82d08020550f 0000000000000000 ffff8308065a7e48
ffff82d080206c2c
(XEN) 0000000000000003 00007f1265e52000 80000005fb770327
00007f1265e52000
(XEN) ffff830806559000 ffff830806584000 ffff830806584000
0000000000000001
(XEN) ffff8308065a7d88 ffff82d0802861cd 0000001100000053
0000000000000001
(XEN) 0000000000000001 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
09fab73a51d28500
(XEN) ffff82d0803803d4 ffff8308065a7ef8 ffff830806559000
0000000000000024
(XEN) ffff82d08020550f 0000000000000001 ffff8308065a7ee8
ffff82d080379fb7
(XEN) 00007f1265e53010 deadbeefdeadf00d deadbeefdeadf00d
deadbeefdeadf00d
(XEN) deadbeefdeadf00d deadbeefdeadf00d ffff82d0803803d4
ffff82d0803803c8
(XEN) ffff82d0803803d4 ffff82d0803803c8 ffff82d0803803d4
ffff82d0803803c8
(XEN) Xen call trace:

(XEN)    [<ffff82d08022618d>] free_domheap_pages+0x2d0/0x40d
(XEN)    [<ffff82d08024323f>] vfree+0x126/0x159
(XEN)    [<ffff82d0802484fc>] mock_domctl+0x177/0x19e
(XEN)    [<ffff82d080206c2c>] do_domctl+0x171d/0x1beb
(XEN)    [<ffff82d080379fb7>] pv_hypercall+0x2aa/0x521
(XEN)    [<ffff82d080380432>] lstar_enter+0x112/0x120

(XEN)
(XEN) Pagetable walk from ffff82ffffffffe0:
(XEN) L4[0x105] = 00000000dd28e063 ffffffffffffffff (XEN) L3[0x1ff] = 0000000000000000 ffffffffffffffff (XEN)
(XEN) **************************************** (XEN) Panic on CPU 6:
(XEN) FATAL PAGE FAULT
(XEN) [error_code=0002]
(XEN) Faulting linear address: ffff82ffffffffe0 (XEN) **************************************** (XEN)
(XEN) Reboot in five seconds...
(XEN) APIC error on CPU0: 40(00)

The crash happens when page_list_del2 is called (arch_free_heap_page(d, &pg[i])). This in turn calls __page_list_del_head and is caused by the "prev->list.next = PAGE_LIST_NULL;" statement (head->tail == page)

The problem is strictly related to vfree because if I call vunmap and free_domheap_page manually, the crash doesn't occur anymore.

Unfortunately I have no ideea what might cause this.

I have pushed a small test which triggers this crash at
https://github.com/petrepircalabu/xen/tree/vfree_crash and I would greatly appreciate your input.

Many thanks for your support,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw
Quoted 9 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper <[email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

Quoted 7 lines

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Do you really mean xenforeignmemory_map_resource()? If the memory is assigned to the calling domain then this is quite likely not to work. There were reference counting problems with that code, which is why caller assigned resources were dropped.

Paul

Quoted 104 lines

....
- xenforeignmemory_unmap_resource
- vfree.

(XEN) ----[ Xen-4.13-unstable x86_64 debug=y Tainted: C ]----
(XEN) CPU: 6
(XEN) RIP: e008:[<ffff82d08022618d>] free_domheap_pages+0x2d0/0x40d
(XEN) RFLAGS: 0000000000010246 CONTEXT: hypervisor (d0v3)
(XEN) rax: ffff82e00bf6ee00 rbx: ffff830806584000 rcx:
ffff82ffffffffe0
(XEN) rdx: ffff82ffffffffe0 rsi: 0000000000000000 rdi:
ffff830806584028
(XEN) rbp: ffff8308065a7c78 rsp:
ffff8308065a7c38 r8: 00000000ffffffff
(XEN) r9: 0000000000000001 r10: ffff82e000000000 r11:
4000000000000000
(XEN) r12: ffff82e00bf6ee00 r13: ffff830806584028 r14:
ffff830806584038
(XEN) r15: 00ffffffffffffff cr0: 0000000080050033 cr4:
0000000000362660
(XEN) cr3: 00000005fb565000 cr2: ffff82ffffffffe0
(XEN) fsb: 00007f1265e36700 gsb: ffff8882168c0000 gss:
0000000000000000
(XEN) ds: 0000 es: 0000 fs: 0000 gs: 0000 ss: e010 cs: e008
(XEN) Xen code around <ffff82d08022618d> (free_domheap_pages+0x2d0/0x40d):
(XEN) 47 08 0f 85 0f 01 00 00 <c7> 02 ff ff ff ff 48 89 57 08 eb 4b 48 39 83 30
(XEN) Xen stack trace from rsp=ffff8308065a7c38:
(XEN) ffff830806584020 0000000000000001 ffff8308065a7c78
ffff82e00bf6ee00
(XEN) 0000000000000000 ffff82e000000000 0000000000000001
000ffffffffff000
(XEN) ffff8308065a7cd8 ffff82d08024323f 0000000000000000
ffff82c000267000
(XEN) 0000000000000000 00000001065a7ca8 8086000000008086
ffff8305f822bfe0
(XEN) ffff8305fd17a000 00007f1265e53010 ffff8305fd17a000
0000000000000000
(XEN) ffff8308065a7d28 ffff82d0802484fc ffff8305fd17a000
0000000000000000
(XEN) 0000000000000292 0000000000000000 ffff8305fd17a000
00007f1265e53010
(XEN) ffff82d08020550f 0000000000000000 ffff8308065a7e48
ffff82d080206c2c
(XEN) 0000000000000003 00007f1265e52000 80000005fb770327
00007f1265e52000
(XEN) ffff830806559000 ffff830806584000 ffff830806584000
0000000000000001
(XEN) ffff8308065a7d88 ffff82d0802861cd 0000001100000053
0000000000000001
(XEN) 0000000000000001 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
0000000000000000
(XEN) 0000000000000000 0000000000000000 0000000000000000
09fab73a51d28500
(XEN) ffff82d0803803d4 ffff8308065a7ef8 ffff830806559000
0000000000000024
(XEN) ffff82d08020550f 0000000000000001 ffff8308065a7ee8
ffff82d080379fb7
(XEN) 00007f1265e53010 deadbeefdeadf00d deadbeefdeadf00d
deadbeefdeadf00d
(XEN) deadbeefdeadf00d deadbeefdeadf00d ffff82d0803803d4
ffff82d0803803c8
(XEN) ffff82d0803803d4 ffff82d0803803c8 ffff82d0803803d4
ffff82d0803803c8
(XEN) Xen call trace:

(XEN)    [<ffff82d08022618d>] free_domheap_pages+0x2d0/0x40d
(XEN)    [<ffff82d08024323f>] vfree+0x126/0x159
(XEN)    [<ffff82d0802484fc>] mock_domctl+0x177/0x19e
(XEN)    [<ffff82d080206c2c>] do_domctl+0x171d/0x1beb
(XEN)    [<ffff82d080379fb7>] pv_hypercall+0x2aa/0x521
(XEN)    [<ffff82d080380432>] lstar_enter+0x112/0x120

(XEN)
(XEN) Pagetable walk from ffff82ffffffffe0:
(XEN) L4[0x105] = 00000000dd28e063 ffffffffffffffff (XEN) L3[0x1ff] = 0000000000000000 ffffffffffffffff (XEN)
(XEN) **************************************** (XEN) Panic on CPU 6:
(XEN) FATAL PAGE FAULT
(XEN) [error_code=0002]
(XEN) Faulting linear address: ffff82ffffffffe0 (XEN) **************************************** (XEN)
(XEN) Reboot in five seconds...
(XEN) APIC error on CPU0: 40(00)

The crash happens when page_list_del2 is called (arch_free_heap_page(d, &pg[i])). This in turn calls __page_list_del_head and is caused by the "prev->list.next = PAGE_LIST_NULL;" statement (head->tail == page)

The problem is strictly related to vfree because if I call vunmap and free_domheap_page manually, the crash doesn't occur anymore.

Unfortunately I have no ideea what might cause this.

I have pushed a small test which triggers this crash at
https://github.com/petrepircalabu/xen/tree/vfree_crash and I would greatly appreciate your input.

Quoted 8 lines

Many thanks for your support,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 21 lines · Paul Durrant
Quoted 12 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper < [email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

Quoted 8 lines

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Quoted 7 lines

Do you really mean xenforeignmemory_map_resource()? If the memory is assigned to the calling domain then this is quite likely not to work. There were reference counting problems with that code, which is why caller assigned resources were dropped.

Paul

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

Many thanks,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 35 lines

-----Original Message-----
From: Petre Ovidiu PIRCALABU <[email protected]>
Sent: 01 July 2019 09:46
To: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: Re: vfree crash

Quoted 26 lines · Paul Durrant
Quoted 12 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper < [email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

Quoted 8 lines

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Do you really mean xenforeignmemory_map_resource()? If the memory is assigned to the calling domain then this is quite likely not to work. There were reference counting problems with that code, which is why caller assigned resources were dropped.

Quoted 6 lines
Quoted 2 lines

Paul

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

So, looking at the code, I don't think it is intended that v[m|z]alloced pages get assigned to a domain. Could you point at where the assignment is happening?

Paul

Quoted 4 lines

Many thanks,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw
In reply to Petre Ovidiu PIRCALABU
Quoted 26 lines · Petre Ovidiu PIRCALABU
Quoted 25 lines · Paul Durrant
Quoted 12 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper < [email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

Quoted 8 lines

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Do you really mean xenforeignmemory_map_resource()? If the memory is assigned to the calling domain then this is quite likely not to work. There were reference counting problems with that code, which is why caller assigned resources were dropped.

Quoted 5 lines
Quoted 2 lines

Paul

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

The problem here is that struct page_info only has a single linked list pointer, and vfree() blindly assumes it is available for use, which isn't true once you've called assign_pages() on the vmap area.

At the moment, it doesn't look like it is possible to set v*alloc()'d pages up suitably to be mapped by a guest.  (Similar corruption will occur via share_xen_page_with_guest() and the xenheap list).

This is obviously something which wants fixing, but its not immediately obvious how.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Paul Durrant
Quoted 51 lines · Paul Durrant
Quoted 43 lines

-----Original Message-----
From: Petre Ovidiu PIRCALABU <[email protected]>
Sent: 01 July 2019 09:46
To: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: Re: vfree crash

Quoted 32 lines · Paul Durrant
Quoted 17 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf
Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper < [email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

Quoted 6 lines

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a
domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Do you really mean xenforeignmemory_map_resource()? If the memory is
assigned to the calling domain then this is quite likely not to work.
There were reference counting problems with that code, which is why
caller assigned resources were dropped.

Quoted 7 lines
Quoted 2 lines

Paul

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these
steps manually (without adding them to a new list) it works.

Quoted 10 lines

So, looking at the code, I don't think it is intended that v[m|z]alloced pages get assigned to a domain. Could you point at where the assignment is happening?

Paul

Quoted 4 lines

Many thanks,
Petre

I'm doing it locally in my test (mock_enable domctl) by calling assign_pages for each one allocated by vzalloc.

https://gitlab.com/xen-project/people/bitdefender/xen/blob/vfree_crash/xen/common/mock.c
(line 55)

Many thanks,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 71 lines

-----Original Message-----
From: Petre Ovidiu PIRCALABU <[email protected]>
Sent: 01 July 2019 10:27
To: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: Re: vfree crash

Quoted 51 lines · Paul Durrant
Quoted 43 lines

-----Original Message-----
From: Petre Ovidiu PIRCALABU <[email protected]>
Sent: 01 July 2019 09:46
To: Paul Durrant <[email protected]>; [email protected]; Andrew Cooper <[email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: Re: vfree crash

Quoted 32 lines · Paul Durrant
Quoted 17 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf
Of Petre Ovidiu PIRCALABU
Sent: 28 June 2019 19:00
To: [email protected]; Andrew Cooper < [email protected]>
Cc: Alexandru Stefan ISAILA <[email protected]>; [email protected]
Subject: [Xen-devel] vfree crash

Hello,

I need your help to pinpoint the root cause of a problem. To my understanding vfree should be used when allocating memory with vmalloc.

Quoted 6 lines

But, I have the following scenario which results in a XEN crash:
- allocate a number of frames using vmalloc (vzalloc) (e.g. using a
domctl) and assign them to the calling domain
- map the frames using xenforeignmemory_map_resource

Do you really mean xenforeignmemory_map_resource()? If the memory is
assigned to the calling domain then this is quite likely not to work.
There were reference counting problems with that code, which is why
caller assigned resources were dropped.

Quoted 7 lines
Quoted 2 lines

Paul

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these
steps manually (without adding them to a new list) it works.

Quoted 10 lines

So, looking at the code, I don't think it is intended that v[m|z]alloced pages get assigned to a domain. Could you point at where the assignment is happening?

Paul

Quoted 4 lines

Many thanks,
Petre

Quoted 5 lines

I'm doing it locally in my test (mock_enable domctl) by calling assign_pages for each one allocated by vzalloc.

https://gitlab.com/xen-project/people/bitdefender/xen/blob/vfree_crash/xen/common/mock.c
(line 55)

I'm getting a 404 for that URL. But, as Andy says, the problem is that assigning to a domain will place the page on a list and that will then mean you're gonna have problems calling vfree().

Paul

Quoted 3 lines

Many thanks,
Petre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper
Quoted 3 lines · Petre Ovidiu PIRCALABU

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

Quoted 7 lines

The problem here is that struct page_info only has a single linked list pointer, and vfree() blindly assumes it is available for use, which isn't true once you've called assign_pages() on the vmap area.

At the moment, it doesn't look like it is possible to set v*alloc()'d pages up suitably to be mapped by a guest.  (Similar corruption will occur via share_xen_page_with_guest() and the xenheap list).

Well, whoever assigns pages to a domain behind vmalloc()'s back has got to make sure to de-assign those pages before freeing them. An alternative _might_ be to leave freeing to the normal cleanup processes (when the last page ref gets put), and just vunmap()-ing the range when the mapping isn't needed anymore.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 3 lines · Petre Ovidiu PIRCALABU

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

The problem here is that struct page_info only has a single linked list pointer, and vfree() blindly assumes it is available for use, which isn't true once you've called assign_pages() on the vmap area.

Quoted 5 lines
Quoted 3 lines

At the moment, it doesn't look like it is possible to set v*alloc()'d pages up suitably to be mapped by a guest.  (Similar corruption will occur via share_xen_page_with_guest() and the xenheap list).

Well, whoever assigns pages to a domain behind vmalloc()'s back has got to make sure to de-assign those pages before freeing them.

Why?  Or perhaps more accurately, where is any of this written down.

Allocation of memory seems logically unrelated to making it mappable by guests, so when vmalloc() *is* the correct allocation function to use, the fact that assign_pages() results in vfree() silently corrupting the domains memory list is unexpected behaviour.

Quoted 4 lines

An alternative
_might_ be to leave freeing to the normal cleanup processes (when the last page ref gets put), and just vunmap()-ing the range when the mapping isn't needed anymore.

So this is what I suggested as an interim solution, but I'm not sure if it is a sensible option longterm.

The scenario here is for the "vm-event-ng" interface which was posted as an RFC earlier.  Several key purposes for the new interface is to be a slot-per-vcpu, and to be usable via the acquire_resource infrastructure.

struct vm_event_st is currently 384 bytes, which is only 10 full structs per page.  The size of the structure is liable to change over time, and most likely won't evenly divide a page, so vmalloc() is the correct allocation interface within Xen.

The alloc and free in this case is being done as a side effect of the vmi enable/disable calls.  The lifetime of the VMI interface isn't the same as the lifetime of the VM.

With HVI specifically, the SVA VM can reboot, and it needs to re-attach to the protected VMs.  There are other load balancing scenarios where the protection of a VM might logically move between two different SVAs.

In either case, retaining the first vmalloc() will result in a failure to remap the ring, as the domain assignment will be to the old domid.

Therefore, I think it is important to be able to fully disable and clean up the VMI interface at some point before the protected VM is destroyed.

As a result, I think the proper fix here is to modify vfree() not to clobber the pagelist.

Thoughts?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper
Quoted 3 lines · Petre Ovidiu PIRCALABU

The problem lies with vfree because it creates a new list with the pages, unmaps the va pointer and then frees the pages. If I do these steps manually (without adding them to a new list) it works.

The problem here is that struct page_info only has a single linked list pointer, and vfree() blindly assumes it is available for use, which isn't true once you've called assign_pages() on the vmap area.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

At the moment, it doesn't look like it is possible to set v*alloc()'d pages up suitably to be mapped by a guest.  (Similar corruption will occur via share_xen_page_with_guest() and the xenheap list).

Well, whoever assigns pages to a domain behind vmalloc()'s back has got to make sure to de-assign those pages before freeing them.

Why?  Or perhaps more accurately, where is any of this written down.

I take this to be a rhetorical question.

Quoted 4 lines

Allocation of memory seems logically unrelated to making it mappable by guests, so when vmalloc() *is* the correct allocation function to use, the fact that assign_pages() results in vfree() silently corrupting the domains memory list is unexpected behaviour.

At best this depends on the position you take. Assignment of pages _always_ transfers their freeing to the refcounting machinery. You won't find many free_domheap_pages() matching alloc_domheap_pages() with a non-NULL d (and no special flags). They'll be paired with put_page() instead. The ones you will find (like in memory_exchange() have special precautions taken up front).

Quoted 28 lines
Quoted 4 lines

An alternative
_might_ be to leave freeing to the normal cleanup processes (when the last page ref gets put), and just vunmap()-ing the range when the mapping isn't needed anymore.

So this is what I suggested as an interim solution, but I'm not sure if it is a sensible option longterm.

The scenario here is for the "vm-event-ng" interface which was posted as an RFC earlier.  Several key purposes for the new interface is to be a slot-per-vcpu, and to be usable via the acquire_resource infrastructure.

struct vm_event_st is currently 384 bytes, which is only 10 full structs per page.  The size of the structure is liable to change over time, and most likely won't evenly divide a page, so vmalloc() is the correct allocation interface within Xen.

The alloc and free in this case is being done as a side effect of the vmi enable/disable calls.  The lifetime of the VMI interface isn't the same as the lifetime of the VM.

With HVI specifically, the SVA VM can reboot, and it needs to re-attach to the protected VMs.  There are other load balancing scenarios where the protection of a VM might logically move between two different SVAs.

In either case, retaining the first vmalloc() will result in a failure to remap the ring, as the domain assignment will be to the old domid.

Quoted 7 lines

Therefore, I think it is important to be able to fully disable and clean up the VMI interface at some point before the protected VM is destroyed.

As a result, I think the proper fix here is to modify vfree() not to clobber the pagelist.

Thoughts?

Beyond what I've said above already, from your description I imply that it's the monitoring domain which is to own the page. Yet that's in conflict with you also saying that this domain may want rebooting. In such a case, the pages need to either not be owned by that domain, or they need to be re-allocated during every attach operation.

Furthermore - what use would not clobbering the list be? There would still be the call to free_domheap_page() there, which is legitimate only if the page (still having an owner) has just been transitioned to a zero general ref count. What might be possible here is

     for ( i = 0; i < pages; i++ )
     {
         struct page_info *page = vmap_to_page(va + i * PAGE_SIZE);

         ASSERT(page);
         if ( <whatever> )
             put_page(pg)
         else
             page_list_add(page, &pg_list);
     }

Or maybe this is actually what you have been thinking of.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel