x86/mm: Clean up p2m_finish_type_change return value
PATCH v5 1/1 x86/mm: Clean up p2m_finish_type_change return value
In the case of any errors, finish_type_change() passes values returned from p2m->recalc() up the stack (with some exceptions in the case where an error is expected); this eventually ends up being returned to the XEN_DOMOP_map_mem_type_to_ioreq_server hypercall.
However, on Intel processors (but not on AMD processor), p2m->recalc() can also return '1' as well as '0'. This case is handled very
inconsistently: finish_type_change() will return the value of the final entry it attempts, discarding results for other entries; p2m_finish_type_change() will attempt to accumulate '1's, so that it returns '1' if any of the calls to finish_type_change() returns '1'; and dm_op() will again return '1' only if the very last call to p2m_finish_type_change() returns '1'. The result is that the XEN_DMOP_map_mem_type_to_ioreq_server() hypercall will sometimes return 0 and sometimes return 1 on success, in an unpredictable manner.
The hypercall documentation doesn't mention return values; but it's not clear what the caller could do with the information about whether entries had been changed or not. At the moment it's always 0 on AMD boxes, and *usually* 1 on Intel boxes; so nothing can be relying on a '1' return value for correctness (or if it is, it's broken).
Make the return value on success consistently '0' by only returning 0/-ERROR from finish_type_change(). Also remove the accumulation code from p2m_finish_type_change().
---
Changes since V4:
- Undo changes in p2m->recalc() and have 1 turn into 0 in finish_type_change().
--- xen/arch/x86/mm/p2m.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
xen/arch/x86/mm/p2m.c +5 -11
@@ -1158,7 +1158,7 @@ void p2m_change_type_range(struct domain *d,* Finish p2m type change for gfns which are marked as need_recalc in a range. * Uses the current p2m's max_mapped_pfn to further clip the invalidation * range for alternate p2ms. -* Returns: 0/1 for success, negative for failure +* Returns: 0 for success, negative for failure */ static int finish_type_change (struct p2m_domain *p2m,gfn_t first_gfn,unsigned long max_nr)
@@ -1174,9 +1174,9 @@ static int finish_type_change(struct p2m_domain *p2m,/* * ept->recalc could return 0/1/-ENOMEM. pt->recalc could return * 0/-ENOMEM/-ENOENT, -ENOENT isn't an error as we are looping -* gfn here. +* gfn here. If rc is 1 we need to have it 0 for success. */ -if ( rc == -ENOENT ) +if ( rc == -ENOENT || rc ==1 ) rc =0 ;else if ( rc <0 ) {
@@ -1213,19 +1213,13 @@ int p2m_finish_type_change(struct domain *d,if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) ) {struct p2m_domain *altp2m = d->arch.altp2m_p2m[i]; -int rc1;p2m_lock (altp2m); - rc1 =finish_type_change (altp2m, first_gfn, max_nr); + rc =finish_type_change (altp2m, first_gfn, max_nr);p2m_unlock (altp2m); -if ( rc1 <0 ) - { - rc = rc1; +if ( rc <0 ) goto out; - } - - rc |= rc1; } }#endif
Signature · 7 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
xen/arch/x86/mm/p2m.c static int finish_type_change(struct p2m_domain *p2m, · line 1179
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 29.03.19 at 13:50, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +2
-2@@ -1174,9 +1174,9 @@ static int finish_type_change(struct p2m_domain *p2m,/* * ept->recalc could return 0/1/-ENOMEM. pt->recalc could return * 0/-ENOMEM/-ENOENT, -ENOENT isn't an error as we are looping -* gfn here. +* gfn here. If rc is 1 we need to have it 0 for success. */ -if ( rc == -ENOENT ) +if ( rc == -ENOENT || rc ==1 )
I guess you mean "rc > 0" here? This and the comment are of course easy enough to adjust while committing, and with the adjustments
Quoted 5 lines
xen/arch/x86/mm/p2m.c +0
-1@@ -1213,19 +1213,13 @@ int p2m_finish_type_change(struct domain *d,if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) ) {struct p2m_domain *altp2m = d->arch.altp2m_p2m[i]; -int rc1;
xen/arch/x86/mm/p2m.c int p2m_finish_type_change(struct domain *d, · line 1221
Quoted 10 lines
xen/arch/x86/mm/p2m.c +2
-4p2m_lock (altp2m); - rc1 =finish_type_change (altp2m, first_gfn, max_nr); + rc =finish_type_change (altp2m, first_gfn, max_nr);p2m_unlock (altp2m); -if ( rc1 <0 ) - { - rc = rc1; +if ( rc <0 )
Along the lines of an earlier comment - I think it is generally better to not special case negative return values when positive ones don't have special meaning. But I know the code base is very inconsistent about this, so I don't really mind leaving it as is.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
George Dunlap
Reviewed-byQuoted 13 lines · Jan Beulich
xen/arch/x86/mm/p2m.c static int finish_type_change(struct p2m_domain *p2m, · line 1179
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 29.03.19 at 13:50, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +2
-2@@ -1174,9 +1174,9 @@ static int finish_type_change(struct p2m_domain *p2m,/* * ept->recalc could return 0/1/-ENOMEM. pt->recalc could return * 0/-ENOMEM/-ENOENT, -ENOENT isn't an error as we are looping -* gfn here. +* gfn here. If rc is 1 we need to have it 0 for success. */ -if ( rc == -ENOENT ) +if ( rc == -ENOENT || rc ==1 )I guess you mean "rc > 0" here? This and the comment are of course easy enough to adjust while committing, and with the adjustments
+1
Quoted 1 line
Reviewed-by Jan Beulich <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Roger/Paul are you ok with the latest changes? Can this go in?
Regards,
Alex
Quoted 75 lines · Alexandru Stefan ISAILA
In the case of any errors,
finish_type_change()passes values returned fromp2m->recalc()up the stack (with some exceptions in the case where an error is expected); this eventually ends up being returned to theXEN_DOMOP_map_mem_type_to_ioreq_serverhypercall.However, on Intel processors (but not on AMD processor),
p2m->recalc()can also return '1' as well as '0'. This case is handled very
inconsistently:finish_type_change()will return the value of the final entry it attempts, discarding results for other entries;p2m_finish_type_change()will attempt to accumulate '1's, so that it returns '1' if any of the calls tofinish_type_change()returns '1'; anddm_op()will again return '1' only if the very last call top2m_finish_type_change()returns '1'. The result is that theXEN_DMOP_map_mem_type_to_ioreq_server()hypercall will sometimes return 0 and sometimes return 1 on success, in an unpredictable manner.The hypercall documentation doesn't mention return values; but it's not clear what the caller could do with the information about whether entries had been changed or not. At the moment it's always 0 on AMD boxes, and *usually* 1 on Intel boxes; so nothing can be relying on a '1' return value for correctness (or if it is, it's broken).
Make the return value on success consistently '0' by only returning 0/-ERROR from
finish_type_change(). Also remove the accumulation code fromp2m_finish_type_change().Suggested-by George Dunlap <[email protected]>Signed-off-by Alexandru Isaila <[email protected]>---
Changes since V4:
- Undo changes inp2m->recalc()and have 1 turn into 0 infinish_type_change().--- xen/arch/x86/mm/p2m.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)xen/arch/x86/mm/p2m.c +4
-6@@ -1158,7 +1158,7 @@ void p2m_change_type_range(struct domain *d,* Finish p2m type change for gfns which are marked as need_recalc in a range. * Uses the current p2m's max_mapped_pfn to further clip the invalidation * range for alternate p2ms. -* Returns: 0/1 for success, negative for failure +* Returns: 0 for success, negative for failure */ static int finish_type_change (struct p2m_domain *p2m,gfn_t first_gfn,unsigned long max_nr)@@ -1174,9 +1174,9 @@ static int finish_type_change(struct p2m_domain *p2m,/* * ept->recalc could return 0/1/-ENOMEM. pt->recalc could return * 0/-ENOMEM/-ENOENT, -ENOENT isn't an error as we are looping -* gfn here. +* gfn here. If rc is 1 we need to have it 0 for success. */ -if ( rc == -ENOENT ) +if ( rc == -ENOENT || rc ==1 ) rc =0 ;else if ( rc <0 ) {@@ -1213,19 +1213,13 @@ int p2m_finish_type_change(struct domain *d,if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) ) {struct p2m_domain *altp2m = d->arch.altp2m_p2m[i]; -int rc1;p2m_lock (altp2m); - rc1 =finish_type_change (altp2m, first_gfn, max_nr); + rc =finish_type_change (altp2m, first_gfn, max_nr);p2m_unlock (altp2m); -if ( rc1 <0 )
xen/arch/x86/mm/p2m.c int p2m_finish_type_change(struct domain *d, · line 1225
Quoted 10 lines
xen/arch/x86/mm/p2m.c +1
-5- { - rc = rc1; +if ( rc <0 ) goto out; - } - - rc |= rc1; } }#endif
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 11 lines
-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Alexandru Stefan ISAILA
Sent: 10 April 2019 09:19
To: [email protected]; George Dunlap <[email protected]>; Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; [email protected]; Roger Pau Monne <[email protected]>
Subject: Re: [Xen-devel] [PATCH v5] x86/mm: Clean upp2m_finish_type_changereturn valueRoger/Paul are you ok with the latest changes? Can this go in?
Yes, I am happy for my R-b to be added if so desired.
Paul
Quoted 77 lines
Regards,
AlexQuoted 73 lines · Alexandru Stefan ISAILA
In the case of any errors,
finish_type_change()passes values returned fromp2m->recalc()up the stack (with some exceptions in the case where an error is expected); this eventually ends up being returned to theXEN_DOMOP_map_mem_type_to_ioreq_serverhypercall.However, on Intel processors (but not on AMD processor),
p2m->recalc()can also return '1' as well as '0'. This case is handled very
inconsistently:finish_type_change()will return the value of the final entry it attempts, discarding results for other entries;p2m_finish_type_change()will attempt to accumulate '1's, so that it returns '1' if any of the calls tofinish_type_change()returns '1'; anddm_op()will again return '1' only if the very last call top2m_finish_type_change()returns '1'. The result is that theXEN_DMOP_map_mem_type_to_ioreq_server()hypercall will sometimes return 0 and sometimes return 1 on success, in an unpredictable manner.The hypercall documentation doesn't mention return values; but it's not clear what the caller could do with the information about whether entries had been changed or not. At the moment it's always 0 on AMD boxes, and *usually* 1 on Intel boxes; so nothing can be relying on a '1' return value for correctness (or if it is, it's broken).
Make the return value on success consistently '0' by only returning 0/-ERROR from
finish_type_change(). Also remove the accumulation code fromp2m_finish_type_change().Suggested-by George Dunlap <[email protected]>Signed-off-by Alexandru Isaila <[email protected]>---
Changes since V4:
- Undo changes inp2m->recalc()and have 1 turn into 0 infinish_type_change().--- xen/arch/x86/mm/p2m.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)xen/arch/x86/mm/p2m.c +4
-5@@ -1158,7 +1158,7 @@ void p2m_change_type_range(struct domain *d,* Finish p2m type change for gfns which are marked as need_recalc in a range. * Uses the current p2m's max_mapped_pfn to further clip the invalidation * range for alternate p2ms. -* Returns: 0/1 for success, negative for failure +* Returns: 0 for success, negative for failure */ static int finish_type_change (struct p2m_domain *p2m,gfn_t first_gfn,unsigned long max_nr)@@ -1174,9 +1174,9 @@ static int finish_type_change(struct p2m_domain *p2m,/* * ept->recalc could return 0/1/-ENOMEM. pt->recalc could return * 0/-ENOMEM/-ENOENT, -ENOENT isn't an error as we are looping -* gfn here. +* gfn here. If rc is 1 we need to have it 0 for success. */ -if ( rc == -ENOENT ) +if ( rc == -ENOENT || rc ==1 ) rc =0 ;else if ( rc <0 ) {@@ -1213,19 +1213,13 @@ int p2m_finish_type_change(struct domain *d,if ( d->arch.altp2m_eptp[i] !=mfn_x (INVALID_MFN ) ) {struct p2m_domain *altp2m = d->arch.altp2m_p2m[i]; -int rc1;p2m_lock (altp2m); - rc1 =finish_type_change (altp2m, first_gfn, max_nr); + rc =finish_type_change (altp2m, first_gfn, max_nr);p2m_unlock (altp2m);
Quoted 16 lines
xen/arch/x86/mm/p2m.c int p2m_finish_type_change(struct domain *d, · line 1225
Quoted 12 lines
xen/arch/x86/mm/p2m.c +1
-6-if ( rc1 <0 ) - { - rc = rc1; +if ( rc <0 ) goto out; - } - - rc |= rc1; } }#endif _______________________________________________
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
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.19 at 10:19, <[email protected]> wrote:
Roger/Paul are you ok with the latest changes? Can this go in?
The thing keeping it from going in is the lack of George's ack. Judging from the To: list of your mail you may even have meant him instead of Roger.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel