Improvements on pstore user experience, take 2
Guilherme G. Piccoli
reviewerv2 cover Improvements on pstore user experience, take 2
Hi Kees, Tony, all
here is the second iteration of the patchset.
V1 here: https://lore.kernel.org/r/[email protected]/
The main changes of this version are related to potential races Sashiko caught, and are hereby addressed (hopefully!). I put the diff V2/V1 in each patch. Lemme know if there's anything else to improve and thanks for reviews/suggestions!
Cheers,
Guilherme G. Piccoli (2):
pstore: Consolidate compression handling in both init paths
pstore: Allow kmsg_bytes parameter to be written at runtime
fs/pstore/inode.c | 8 +++++--- fs/pstore/platform.c | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 11 deletions(-)
Signature · 1 line
2.50.1
Guilherme G. Piccoli
reviewerPATCH v2 1/2 pstore: Consolidate compression handling in both init paths
The pstore infrastructure can be initialized in two ways: either through the filesystem registration at pstore_init(), or via backend registration that calls pstore_register(). At the same time, we have a peculiarity regarding pstore compression: multiple algorithms were once supported, but they were phased out, and only "deflate" is now supported, along with the backward-compatible "none" value.
These two apparently unrelated topics have one (lacking) point of
convergence: the compression parameter handling is done only in the pstore registration phase, at allocate_buf_for_compression(). Now, if the pstore fs gets registered prior to any backend registration, which is common in practice, some funny scenarios are possible, for example:
users can set any string for compression and it is held in the compression parameter up to the backend registration (if any). Another one (maybe even more odd): it's possible to set any value to this parameter even if CONFIG_PSTORE_COMPRESS is disabled, and such a value is kept until backend registration, which is counter-intuitive.
Bring both initialization points of pstore in sync regarding the compression set-up, by funneling the parameter handling through a single function called in both paths. Notice that the mutex is used to serialize access to the compression parameter setup routine.
The __always_inline attribute usage is deliberate and necessary:
the pstore code relies on the compiler's Dead Code Elimination (DCE) to drop references to zlib symbols that may not be built. The code checks for CONFIG_PSTORE_COMPRESS and bails early in some functions, which leads to zlib calls becoming dead code that is gone with DCE. So, inlining is required here to avoid undefined references to zlib symbols not built at link time.
On top of that, also improve the compression parameter module info.
---
V2:
- Guard the compression parameter routine access on pstore_init() with the psinfo_lock mutex, in order to avoid potential NULL pointer dereferences on strcmp() (thanks Sashiko!).
fs/pstore/platform.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
fs/pstore/platform.c +17 -7
@@ -89,7 +89,7 @@ MODULE_PARM_DESC(backend, "specific backend to use");*/ static char *compress ="deflate" ;module_param (compress, charp,0444 ); -MODULE_PARM_DESC (compress,"compression to use" ); +MODULE_PARM_DESC (compress,"compression to use (deflate/none)" );/* How much of the kernel log to snapshot */ unsigned int kmsg_bytes =CONFIG_PSTORE_DEFAULT_KMSG_BYTES ;
@@ -201,16 +201,12 @@ static int pstore_compress(const void *in, void *out,return zstream.total_out; } -static void allocate_buf_for_compression (void ) +static __always_inline bool compress_param_adjust(void) { -size_t compressed_size; -char *buf; - -/* Skip if not built-in or compression disabled. */ if (!IS_ENABLED (CONFIG_PSTORE_COMPRESS ) || !compress || !strcmp (compress,"none" )) { compress =NULL ; -return ; +return false; }if (strcmp (compress,"deflate" )) {
@@ -219,6 +215,16 @@ static void allocate_buf_for_compression(void) compress ="deflate" ; } +return true; +} + +static void allocate_buf_for_compression (void ) +{ +size_t compressed_size; +char *buf; + +if (!compress_param_adjust ()) +return ;/* * The compression buffer only needs to be as large as the maximum * uncompressed record size, since any record that would be expanded by
@@ -747,10 +753,14 @@ static int __init pstore_init(void) {int ret; +mutex_lock (&psinfo_lock); +compress_param_adjust (); + ret =pstore_init_fs ();if (ret)free_buf_for_compression (); +mutex_unlock (&psinfo_lock);return ret; }late_initcall (pstore_init);
Signature · 1 line
2.50.1
Guilherme G. Piccoli
reviewerPATCH v2 2/2 pstore: Allow kmsg_bytes parameter to be written at runtime
In reply to Guilherme G. PiccoliCurrently the kmsg_bytes parameter is read-only in sysfs, though it can be changed both via command-line at boot time, and through the pstore filesystem mount option, at runtime.
Add a module parameter callback in order for users to change kmsg_bytes via the runtime sysfs interface, the same way they can do it using the mount option.
Notice this exposes more potential races on reading / writing the variable, since the parameter can now be written at any time through sysfs, so annotate the concurrent accesses to kmsg_bytes with {READ,WRITE}_ONCE to pair the readers with the writers.
---
V2:
- Add READ_ONCE() to missing places on inode.c, given that now, with the sysfs writing possibility, we increase the potential race on reading/writing kmsg_bytes from different CPUs.
fs/pstore/inode.c | 8 +++++--- fs/pstore/platform.c | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-)
fs/pstore/inode.c +5 -3
@@ -267,8 +267,10 @@ static int pstore_parse_param(struct fs_context *fc, struct fs_parameter *param)*/ static int pstore_show_options (struct seq_file *m,struct dentry *root) { -if (kmsg_bytes !=CONFIG_PSTORE_DEFAULT_KMSG_BYTES ) -seq_printf (m,",kmsg_bytes=%u" , kmsg_bytes); +unsigned int bytes =READ_ONCE (kmsg_bytes); + +if (bytes !=CONFIG_PSTORE_DEFAULT_KMSG_BYTES ) +seq_printf (m,",kmsg_bytes=%u" , bytes);return 0 ; }
@@ -488,7 +490,7 @@ static int pstore_init_fs_context(struct fs_context *fc)* every time we (re)mount the single-sb filesystem with the * option specified. */ - ctx->kmsg_bytes = kmsg_bytes; + ctx->kmsg_bytes =READ_ONCE (kmsg_bytes); fc->fs_private = ctx; fc->ops = &pstore_context_ops;
fs/pstore/platform.c +19 -1
@@ -93,7 +93,25 @@ MODULE_PARM_DESC(compress, "compression to use (deflate/none)");/* How much of the kernel log to snapshot */ unsigned int kmsg_bytes =CONFIG_PSTORE_DEFAULT_KMSG_BYTES ; -module_param (kmsg_bytes, uint,0444 ); +static int param_set_kmsg_bytes (const char *val,const struct kernel_param *kp) +{ +unsigned int num; + +if (kstrtouint (val,0 , &num)) +return -EINVAL ; + +WRITE_ONCE (kmsg_bytes, num); + +return 0 ; +} + +static const struct kernel_param_ops kmsg_bytes_ops = { + .set = param_set_kmsg_bytes, + .get = param_get_uint, +}; + +module_param_cb (kmsg_bytes, &kmsg_bytes_ops, &kmsg_bytes,0644 ); +__MODULE_PARM_TYPE (kmsg_bytes,"uint" );MODULE_PARM_DESC (kmsg_bytes,"amount of kernel log to snapshot (in bytes)" );static void *compress_workspace;
Signature · 1 line
2.50.1
Guilherme G. Piccoli
reviewerQuoted 12 lines · Guilherme G. Piccoli
Hi Kees, Tony, all
here is the second iteration of the patchset.
V1 here: https://lore.kernel.org/r/[email protected]/
The main changes of this version are related to potential races Sashiko caught, and are hereby addressed (hopefully!). I put the diff V2/V1 in each patch. Lemme know if there's anything else to improve and thanks for reviews/suggestions!
Cheers,
Quoted 8 lines
Guilherme G. Piccoli (2):
pstore: Consolidate compression handling in both init paths
pstore: Allowkmsg_bytesparameter to be written at runtimefs/pstore/inode.c | 8 +++++--- fs/pstore/platform.c | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 11 deletions(-)
Hi folks, gently monthly ping - is there anything else I should change? Any feedback is appreciated - thanks in advance!
Guilherme