From: Mathieu Poirier <mathieu.poirier@linaro.org>
To: Mike Leach <mike.leach@linaro.org>
Cc: linux-arm-kernel@lists.infradead.org, coresight@lists.linaro.org,
suzuki.poulose@arm.com, leo.yan@linaro.org
Subject: Re: [PATCH v3 3/5] coresight: configfs: Modify config files to allow userspace use
Date: Wed, 25 May 2022 13:57:42 -0600 [thread overview]
Message-ID: <20220525195742.GB273925@p14s> (raw)
In-Reply-To: <20220414064457.12052-4-mike.leach@linaro.org>
On Thu, Apr 14, 2022 at 07:44:55AM +0100, Mike Leach wrote:
> Update coresight-config.h and the coresight-config-file.c & .h
> to allow use in userspace programs.
>
> Use __KERNEL__ defines to filter out driver only structures and
> elements so that user space programs can use the descriptor structures.
>
> Abstract memory allocation in coresight-config-file.c to allow read
> file functions to be run in userspace and kernel drivers.
>
> Signed-off-by: Mike Leach <mike.leach@linaro.org>
> ---
> .../coresight/coresight-config-file.c | 95 +++++++++++++------
> .../hwtracing/coresight/coresight-config.h | 12 +++
> 2 files changed, 80 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/hwtracing/coresight/coresight-config-file.c b/drivers/hwtracing/coresight/coresight-config-file.c
> index 3fd001938324..5b8f635ac50e 100644
> --- a/drivers/hwtracing/coresight/coresight-config-file.c
> +++ b/drivers/hwtracing/coresight/coresight-config-file.c
> @@ -6,8 +6,57 @@
>
> #include "coresight-config.h"
> #include "coresight-config-file.h"
> +
> +/*
> + * To allow reuse of this source in tools, define memory allocation fns according
> + * to build environment.
> + */
> +
> +#ifdef __KERNEL__
> #include "coresight-syscfg.h"
>
> +static void *cscfg_calloc(size_t num, size_t size)
> +{
> + return devm_kcalloc(cscfg_device(), num, size, GFP_KERNEL);
> +}
> +
> +static char *cscfg_strdup(const char *str)
> +{
> + return devm_kstrdup(cscfg_device(), str, GFP_KERNEL);
> +}
> +
> +static void *cscfg_zalloc(size_t size)
> +{
> + return devm_kzalloc(cscfg_device(), size, GFP_KERNEL);
> +}
> +
> +#else
> +
> +#include <stddef.h>
> +#include <string.h>
> +#include <stdlib.h>
> +
> +static void *cscfg_calloc(size_t num, size_t size)
> +{
> + return calloc(num, size);
> +}
> +
> +static char *cscfg_strdup(const char *str)
> +{
> + return strdup(str);
> +}
> +
> +static void *cscfg_zalloc(size_t size)
> +{
> + void *ptr = malloc(size);
> +
> + if (ptr)
> + memset(ptr, 0, size);
> + return ptr;
> +}
> +
> +#endif
> +
> #define cscfg_extract_u64(val64) { \
> val64 = *(u64 *)(buffer + used); \
> used += sizeof(u64); \
> @@ -80,6 +129,7 @@ static int cscfg_file_read_elem_str(const u8 *buffer, const int buflen, int *buf
> struct cscfg_file_elem_str *elem_str)
> {
> int used = *buf_used;
> + const u8 *str;
>
> if ((buflen - used) < sizeof(u16))
> return -EINVAL;
> @@ -89,11 +139,13 @@ static int cscfg_file_read_elem_str(const u8 *buffer, const int buflen, int *buf
> if ((buflen - used) < elem_str->str_len)
> return -EINVAL;
>
> + str = buffer + used;
> +
> /* check for 0 termination */
> - if (buffer[elem_str->str_len - 1] != 0)
> + if (str[elem_str->str_len - 1] != 0)
> return -EINVAL;
>
Now it makes sense.
> - elem_str->str = devm_kstrdup(cscfg_device(), (char *)buffer, GFP_KERNEL);
> + elem_str->str = cscfg_strdup((char *)str);
> used += elem_str->str_len;
>
> *buf_used = used;
> @@ -104,14 +156,12 @@ static int cscfg_file_alloc_desc_arrays(struct cscfg_fs_load_descs *desc_arrays,
> int nr_features)
> {
> /* arrays are 0 terminated - max of 1 config & nr_features features */
> - desc_arrays->config_descs = devm_kcalloc(cscfg_device(), 2,
> - sizeof(struct cscfg_config_desc *),
> - GFP_KERNEL);
> + desc_arrays->config_descs = cscfg_calloc(2, sizeof(struct cscfg_config_desc *));
> if (!desc_arrays->config_descs)
> return -ENOMEM;
> - desc_arrays->feat_descs = devm_kcalloc(cscfg_device(), nr_features + 1,
> - sizeof(struct cscfg_feature_desc *),
> - GFP_KERNEL);
> +
> + desc_arrays->feat_descs = cscfg_calloc(nr_features + 1,
> + sizeof(struct cscfg_feature_desc *));
> if (!desc_arrays->feat_descs)
> return -ENOMEM;
> return 0;
> @@ -138,8 +188,7 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int *
> return 0;
>
> /* we have a config - allocate the descriptor */
> - config_desc = devm_kzalloc(cscfg_device(), sizeof(struct cscfg_config_desc),
> - GFP_KERNEL);
> + config_desc = cscfg_zalloc(sizeof(struct cscfg_config_desc));
> if (!config_desc)
> return -ENOMEM;
>
> @@ -165,8 +214,7 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int *
> /* read the array of 64bit presets if present */
> nr_preset_vals = config_desc->nr_total_params * config_desc->nr_presets;
> if (nr_preset_vals) {
> - presets = devm_kcalloc(cscfg_device(), nr_preset_vals,
> - sizeof(u64), GFP_KERNEL);
> + presets = cscfg_calloc(nr_preset_vals, sizeof(u64));
> if (!presets)
> return -ENOMEM;
>
> @@ -181,10 +229,8 @@ static int cscfg_file_read_elem_config(const u8 *buffer, const int buflen, int *
>
> /* read the array of feature names referenced by the config */
> if (config_desc->nr_feat_refs) {
> - config_desc->feat_ref_names = devm_kcalloc(cscfg_device(),
> - config_desc->nr_feat_refs,
> - sizeof(char *),
> - GFP_KERNEL);
> + config_desc->feat_ref_names = cscfg_calloc(config_desc->nr_feat_refs,
> + sizeof(char *));
> if (!config_desc->feat_ref_names)
> return -ENOMEM;
>
> @@ -262,8 +308,7 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int
> u32 val32;
>
> /* allocate the feature descriptor object */
> - feat_desc = devm_kzalloc(cscfg_device(), sizeof(struct cscfg_feature_desc),
> - GFP_KERNEL);
> + feat_desc = cscfg_zalloc(sizeof(struct cscfg_feature_desc));
> if (!feat_desc)
> return -ENOMEM;
>
> @@ -302,10 +347,8 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int
> nr_regs_bytes = ((sizeof(u32) + sizeof(u64)) * feat_desc->nr_regs);
> if ((buflen - used) < nr_regs_bytes)
> return -EINVAL;
> - feat_desc->regs_desc = devm_kcalloc(cscfg_device(),
> - feat_desc->nr_regs,
> - sizeof(struct cscfg_regval_desc),
> - GFP_KERNEL);
> + feat_desc->regs_desc = cscfg_calloc(feat_desc->nr_regs,
> + sizeof(struct cscfg_regval_desc));
> if (!feat_desc->regs_desc)
> return -ENOMEM;
>
> @@ -319,10 +362,8 @@ static int cscfg_file_read_elem_feature(const u8 *buffer, const int buflen, int
>
> /* parameter descriptors - string + 64 bit value */
> if (feat_desc->nr_params) {
> - feat_desc->params_desc = devm_kcalloc(cscfg_device(),
> - feat_desc->nr_params,
> - sizeof(struct cscfg_parameter_desc),
> - GFP_KERNEL);
> + feat_desc->params_desc = cscfg_calloc(feat_desc->nr_params,
> + sizeof(struct cscfg_parameter_desc));
> if (!feat_desc->params_desc)
> return -ENOMEM;
> for (i = 0; i < feat_desc->nr_params; i++) {
> @@ -399,7 +440,7 @@ int cscfg_file_read_buffer(const u8 *buffer, const int buflen,
> if (err)
> return err;
> }
> - return used;
> + return 0;
This belongs in patch 1.
> }
>
> int cscfg_file_read_buffer_first_name(const u8 *buffer, const int buflen,
> diff --git a/drivers/hwtracing/coresight/coresight-config.h b/drivers/hwtracing/coresight/coresight-config.h
> index 9cd3c26ce023..fce74e2fbd0c 100644
> --- a/drivers/hwtracing/coresight/coresight-config.h
> +++ b/drivers/hwtracing/coresight/coresight-config.h
> @@ -7,7 +7,14 @@
> #ifndef _CORESIGHT_CORESIGHT_CONFIG_H
> #define _CORESIGHT_CORESIGHT_CONFIG_H
>
> +/*
> + * Filter out kernel only portions of the file to allow user space programs
> + * to use the descriptor definitions.
> + */
> +#ifdef __KERNEL__
> #include <linux/coresight.h>
> +#endif
> +
> #include <linux/types.h>
>
> /* CoreSight Configuration Management - component and system wide configuration */
> @@ -100,6 +107,10 @@ struct cscfg_fs_load_descs {
> struct cscfg_feature_desc **feat_descs;
> };
>
> +
> +/* remainder of header is used by the kernel drivers only */
> +#ifdef __KERNEL__
> +
> /**
> * Device feature descriptor - combination of registers and parameters to
> * program a device to implement a specific complex function.
> @@ -272,4 +283,5 @@ void cscfg_csdev_disable_config(struct cscfg_config_csdev *config_csdev);
> /* reset a feature to default values */
> void cscfg_reset_feat(struct cscfg_feature_csdev *feat_csdev);
>
> +#endif /* __KERNEL__ */
> #endif /* _CORESIGHT_CORESIGHT_CONFIG_H */
> --
> 2.17.1
>
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-05-25 19:59 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-14 6:44 [PATCH v3 0/5] coresight: syscfg: Extend configfs for config load Mike Leach
2022-04-14 6:44 ` [PATCH v3 1/5] coresight: configfs: Add in functionality for load via configfs Mike Leach
2022-05-11 17:58 ` Mathieu Poirier
2022-05-16 12:43 ` Mike Leach
2022-05-12 17:54 ` Mathieu Poirier
2022-05-25 17:38 ` Mathieu Poirier
2022-06-01 8:32 ` Mike Leach
2022-04-14 6:44 ` [PATCH v3 2/5] coresight: configfs: Add in binary attributes to load files Mike Leach
2022-05-25 18:00 ` Mathieu Poirier
2022-05-25 19:30 ` Mathieu Poirier
2022-06-01 8:33 ` Mike Leach
2022-04-14 6:44 ` [PATCH v3 3/5] coresight: configfs: Modify config files to allow userspace use Mike Leach
2022-05-25 19:57 ` Mathieu Poirier [this message]
2022-04-14 6:44 ` [PATCH v3 4/5] coresight: tools: Add config file write and reader tools Mike Leach
2022-05-26 17:46 ` Mathieu Poirier
2022-05-27 16:25 ` Mathieu Poirier
2022-06-01 10:56 ` Mike Leach
2022-06-01 16:10 ` Mathieu Poirier
2022-04-14 6:44 ` [PATCH v3 5/5] Documentation: coresight: docs for config load via configfs Mike Leach
2022-05-26 17:48 ` Mathieu Poirier
2022-05-10 15:39 ` [PATCH v3 0/5] coresight: syscfg: Extend configfs for config load Mathieu Poirier
2022-05-27 16:32 ` Mathieu Poirier
2022-06-01 8:30 ` Mike Leach
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220525195742.GB273925@p14s \
--to=mathieu.poirier@linaro.org \
--cc=coresight@lists.linaro.org \
--cc=leo.yan@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=mike.leach@linaro.org \
--cc=suzuki.poulose@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.