tools: fix error path of xendevicemodel_open()
PATCH 1/1 tools: fix error path of xendevicemodel_open()
c/s 6902cb00e03 "tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to both xencall_open() and osdep_xendevicemodel_open() but failed to fix up the error path.
c/s f68c7c618a3 "libs/devicemodel: free xencall handle in error path in _open()" fixed up the xencall_open() aspect of the error path (missing the osdep_xendevicemodel_open() aspect), but positioned the xencall_close() incorrectly, creating the same pattern proved to be problematic by c/s 30a72f02870 "tools: fix error path of xenhypfs_open()".
Reposition xtl_logger_destroy(), and introduce the missing osdep_xendevicemodel_close().
Backport: 4.9+
---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Paul Durrant <[email protected]>
RFC - this is still broken.
Failure to create the logger will still hit the NULL deference, in all of the stable libs, not just devicemodel.
Also, unless I'd triple checked the history, I was about to reintroduce the deadlock from c/s 9976f3874d4, because it totally counterintuitive wrong to expect setup and teardown in opposite orders.
--- tools/libs/devicemodel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/libs/devicemodel/core.c +2 -1
@@ -67,9 +67,10 @@ xendevicemodel_handle *xendevicemodel_open(xentoollog_logger *logger,return dmod; err: -xtl_logger_destroy (dmod->logger_tofree); +osdep_xendevicemodel_close (dmod);xentoolcore__deregister_active_handle (&dmod->tc_ah);xencall_close (dmod->xcall); +xtl_logger_destroy (dmod->logger_tofree);free (dmod);return NULL ; }
Signature · 1 line
2.11.0
Quoted 10 lines · Andrew Cooper
c/s
6902cb00e03"tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to bothxencall_open()andosdep_xendevicemodel_open()but failed to fix up the error path.c/s
f68c7c618a3"libs/devicemodel: free xencall handle in error path in_open()" fixed up thexencall_open()aspect of the error path (missing theosdep_xendevicemodel_open()aspect), but positioned thexencall_close()incorrectly, creating the same pattern proved to be problematic by c/s30a72f02870"tools: fix error path ofxenhypfs_open()".
Quoted 7 lines
Reposition
xtl_logger_destroy(), and introduce the missingosdep_xendevicemodel_close().Fixes 6902cb00e03 ("tools/libxendevicemodel: extract functions and add a compat layer")Fixes f68c7c618a3 ("libs/devicemodel: free xencall handle in error path in _open()")Backport: 4.9+
Signed-off-by Andrew Cooper <[email protected]>
Juergen
Quoted 24 lines
-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 10 June 2020 12:40
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Juergen Gross <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH for-4.14] tools: fix error path ofxendevicemodel_open()c/s
6902cb00e03"tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to bothxencall_open()andosdep_xendevicemodel_open()but failed to fix up the error path.c/s
f68c7c618a3"libs/devicemodel: free xencall handle in error path in_open()" fixed up thexencall_open()aspect of the error path (missing theosdep_xendevicemodel_open()aspect), but positioned thexencall_close()incorrectly, creating the same pattern proved to be problematic by c/s30a72f02870"tools: fix error path ofxenhypfs_open()".Reposition
xtl_logger_destroy(), and introduce the missingosdep_xendevicemodel_close().Fixes 6902cb00e03 ("tools/libxendevicemodel: extract functions and add a compat layer")Fixes f68c7c618a3 ("libs/devicemodel: free xencall handle in error path in _open()")Backport: 4.9+
Quoted 9 lines
Signed-off-by Andrew Cooper <[email protected]>---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Paul Durrant <[email protected]>RFC - this is still broken.
I'm slightly confused. Do you want this in 4.14 in this form or are you expecting to update it?
Paul
Quoted 28 lines
Failure to create the logger will still hit the NULL deference, in all of the stable libs, not just devicemodel.
Also, unless I'd triple checked the history, I was about to reintroduce the deadlock from c/s
9976f3874d4, because it totally counterintuitive wrong to expect setup and teardown in opposite orders.--- tools/libs/devicemodel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/libs/devicemodel/core.c +2
-2@@ -67,9 +67,10 @@ xendevicemodel_handle *xendevicemodel_open(xentoollog_logger *logger,return dmod; err: -xtl_logger_destroy (dmod->logger_tofree); +osdep_xendevicemodel_close (dmod);xentoolcore__deregister_active_handle (&dmod->tc_ah);xencall_close (dmod->xcall); +xtl_logger_destroy (dmod->logger_tofree);free (dmod);return NULL ; } --2.11.0
Quoted 31 lines · Paul Durrant
Quoted 31 lines
-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 10 June 2020 12:40
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Juergen Gross <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH for-4.14] tools: fix error path ofxendevicemodel_open()c/s
6902cb00e03"tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to bothxencall_open()andosdep_xendevicemodel_open()but failed to fix up the error path.c/s
f68c7c618a3"libs/devicemodel: free xencall handle in error path in_open()" fixed up thexencall_open()aspect of the error path (missing theosdep_xendevicemodel_open()aspect), but positioned thexencall_close()incorrectly, creating the same pattern proved to be problematic by c/s30a72f02870"tools: fix error path ofxenhypfs_open()".Reposition
xtl_logger_destroy(), and introduce the missingosdep_xendevicemodel_close().Fixes 6902cb00e03 ("tools/libxendevicemodel: extract functions and add a compat layer")Fixes f68c7c618a3 ("libs/devicemodel: free xencall handle in error path in _open()")Backport: 4.9+
Signed-off-by Andrew Cooper <[email protected]>---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Paul Durrant <[email protected]>
Quoted 3 lines
Quoted 2 lines
RFC - this is still broken.
I'm slightly confused. Do you want this in 4.14 in this form or are you expecting to update it?
In this form, it is an improvement over before.
There is still the crash described below which needs some form of figuring out and fixing.
~Andrew
Quoted 32 lines
Paul
Quoted 28 lines
Failure to create the logger will still hit the NULL deference, in all of the stable libs, not just devicemodel.
Also, unless I'd triple checked the history, I was about to reintroduce the deadlock from c/s
9976f3874d4, because it totally counterintuitive wrong to expect setup and teardown in opposite orders.--- tools/libs/devicemodel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/libs/devicemodel/core.c +2
-2@@ -67,9 +67,10 @@ xendevicemodel_handle *xendevicemodel_open(xentoollog_logger *logger,return dmod; err: -xtl_logger_destroy (dmod->logger_tofree); +osdep_xendevicemodel_close (dmod);xentoolcore__deregister_active_handle (&dmod->tc_ah);xencall_close (dmod->xcall); +xtl_logger_destroy (dmod->logger_tofree);free (dmod);return NULL ; } --2.11.0
Quoted 39 lines
-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 12 June 2020 10:54
To: [email protected]; 'Xen-devel' <[email protected]>
Cc: 'Ian Jackson' <[email protected]>; 'Wei Liu' <[email protected]>; 'Juergen Gross' <[email protected]>
Subject: Re: [PATCH for-4.14] tools: fix error path ofxendevicemodel_open()Quoted 31 lines · Paul Durrant
Quoted 31 lines
-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 10 June 2020 12:40
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Juergen Gross <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH for-4.14] tools: fix error path ofxendevicemodel_open()c/s
6902cb00e03"tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to bothxencall_open()andosdep_xendevicemodel_open()but failed to fix up the error path.c/s
f68c7c618a3"libs/devicemodel: free xencall handle in error path in_open()" fixed up thexencall_open()aspect of the error path (missing theosdep_xendevicemodel_open()aspect), but positioned thexencall_close()incorrectly, creating the same pattern proved to be problematic by c/s30a72f02870"tools: fix error path ofxenhypfs_open()".Reposition
xtl_logger_destroy(), and introduce the missingosdep_xendevicemodel_close().Fixes 6902cb00e03 ("tools/libxendevicemodel: extract functions and add a compat layer")Fixes f68c7c618a3 ("libs/devicemodel: free xencall handle in error path in _open()")Backport: 4.9+
Signed-off-by Andrew Cooper <[email protected]>---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Paul Durrant <[email protected]>
Quoted 9 lines
Quoted 3 lines
Quoted 2 lines
RFC - this is still broken.
I'm slightly confused. Do you want this in 4.14 in this form or are you expecting to update it?
In this form, it is an improvement over before.
There is still the crash described below which needs some form of figuring out and fixing.
Ok, in which case consider it...
Quoted 34 lines
~Andrew
Quoted 32 lines
Paul
Quoted 28 lines
Failure to create the logger will still hit the NULL deference, in all of the stable libs, not just devicemodel.
Also, unless I'd triple checked the history, I was about to reintroduce the deadlock from c/s
9976f3874d4, because it totally counterintuitive wrong to expect setup and teardown in opposite orders.--- tools/libs/devicemodel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/libs/devicemodel/core.c +2
-2@@ -67,9 +67,10 @@ xendevicemodel_handle *xendevicemodel_open(xentoollog_logger *logger,return dmod; err: -xtl_logger_destroy (dmod->logger_tofree); +osdep_xendevicemodel_close (dmod);xentoolcore__deregister_active_handle (&dmod->tc_ah);xencall_close (dmod->xcall); +xtl_logger_destroy (dmod->logger_tofree);free (dmod);return NULL ; } --2.11.0
Andrew Cooper writes ("Re: [PATCH for-4.14] tools: fix error path of xendevicemodel_open()"):
Quoted 2 lines
There is still the crash described below which needs some form of figuring out and fixing.
...
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Failure to create the logger will still hit the NULL deference, in all of the stable libs, not just devicemodel.
Are you sure ?
I think you mean this sequence of events:
xencall_open(logger=NULL, open_flags=0)
xcall->logger = NULL; /* from logger */
xcall->logger_tofree = NULL;
if (1) {
xtl_createlogger_stdiostream => NULL
/* so */ goto err;
} err:
xentoolcore__deregister_active_handle(&xcall->tc_ah); /* " */
osdep_xencall_close(xcall);
xencall_close(dmod->xcall);
xtl_logger_destroy(xcall->logger_tofree /* NULL */); // <- crash?
free(xcall);But xtl_logger_destroy(NULL) is a safe no-op.
However,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Also, unless I'd triple checked the history, I was about to reintroduce the deadlock from c/s
9976f3874d4
These comments made me look again at 9976f3874d4 "tools:xentoolcore_restrict_all: Do deregistration before close".
Just now I wrote:
I notice that the tail of xendevicemodel_open is now identical to xendevicemodel_close. I think this is expected, and that it would be better to combine the two sets of code. If they hadn't been separate then we might have avoided this bug...
But in fact this is not true. xendevicemodel_close has them in the right order, but xendevicemodel_open's err block has them in the wrong order.
Now that I look at xencall, I discover tht xencall_open's err block is not identical to xencall_close. xencall close calls buffer_release_cache and xencall_open's err block does not. Looking more closely I think this happens not to be a memory leak because xcall->buffer* don't contain any malloc'd stuff until they are used.
But I think this is poor practice and another example of teardown code duplication being a bad idea.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
because it totally counterintuitive wrong to
expect setup and teardown in opposite orders.
Are you sure you wrote what you meant ? To my mind it is usual for setup and teardown to proceed in precisely the opposite order.
The need to call xentoolcore__deregister_active_handle before closing the fd is to my mind unusual and counterintuitive. The reasons are explained in the commit message for 9976f3874d4cca82.
Does that all make sense ?
Perhaps we should at least delete the wrong err path of xendevicemodel_open with a call to xendevicemodel_close ?
Ian.
Andrew Cooper writes ("[PATCH for-4.14] tools: fix error path of xendevicemodel_open()"):
Quoted 4 lines
c/s
6902cb00e03"tools/libxendevicemodel: extract functions and add a compat layer" introduced calls to bothxencall_open()andosdep_xendevicemodel_open()but failed to fix up the error path.
Quoted 8 lines
c/s
f68c7c618a3"libs/devicemodel: free xencall handle in error path in_open()" fixed up thexencall_open()aspect of the error path (missing theosdep_xendevicemodel_open()aspect), but positioned thexencall_close()incorrectly, creating the same pattern proved to be problematic by c/s30a72f02870"tools: fix error path ofxenhypfs_open()".Reposition
xtl_logger_destroy(), and introduce the missingosdep_xendevicemodel_close().
I notice that the tail of xendevicemodel_open is now identical to xendevicemodel_close. I think this is expected, and that it would be better to combine the two sets of code. If they hadn't been separate then we might have avoided this bug...
Ian.