diff mbox series

[v2,1/1] perf: RISC-V: Limit the number of counters returned from SBI

Message ID 20230504145453.981767-1-v.v.mitrofanov@yadro.com (mailing list archive)
State Superseded
Headers show
Series [v2,1/1] perf: RISC-V: Limit the number of counters returned from SBI | expand

Checks

Context Check Description
conchuod/cover_letter success Single patches do not need cover letters
conchuod/tree_selection success Guessed tree name to be for-next at HEAD c2d3c8441e3d
conchuod/fixes_present success Fixes tag not required for -next series
conchuod/maintainers_pattern success MAINTAINERS pattern errors before the patch: 5 and now 5
conchuod/verify_signedoff success Signed-off-by tag matches author and committer
conchuod/kdoc success Errors and warnings before: 0 this patch: 0
conchuod/build_rv64_clang_allmodconfig success Errors and warnings before: 8 this patch: 8
conchuod/module_param success Was 0 now: 0
conchuod/build_rv64_gcc_allmodconfig success Errors and warnings before: 8 this patch: 8
conchuod/build_rv32_defconfig success Build OK
conchuod/dtb_warn_rv64 success Errors and warnings before: 5 this patch: 5
conchuod/header_inline success No static functions without inline keyword in header files
conchuod/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
conchuod/source_inline success Was 0 now: 0
conchuod/build_rv64_nommu_k210_defconfig success Build OK
conchuod/verify_fixes success No Fixes tag
conchuod/build_rv64_nommu_virt_defconfig success Build OK

Commit Message

Viacheslav Mitrofanov May 4, 2023, 2:54 p.m. UTC
Perf gets the number of supported counters from SBI. If it happens that
the number of returned counters more than RISCV_MAX_COUNTERS the code
trusts it. It does not lead to an immediate problem but can potentially
lead to it. Prevent getting more than RISCV_MAX_COUNTERS from SBI.

Signed-off-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
Changes in v2:
- Add new line
- Substitute pr_warn for pr_info

 drivers/perf/riscv_pmu_sbi.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Atish Patra May 4, 2023, 8:49 p.m. UTC | #1
On Thu, May 4, 2023 at 8:25 PM Viacheslav Mitrofanov
<v.v.mitrofanov@yadro.com> wrote:
>
> Perf gets the number of supported counters from SBI. If it happens that
> the number of returned counters more than RISCV_MAX_COUNTERS the code
> trusts it. It does not lead to an immediate problem but can potentially
> lead to it. Prevent getting more than RISCV_MAX_COUNTERS from SBI.
>
> Signed-off-by: Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> ---
> Changes in v2:
> - Add new line
> - Substitute pr_warn for pr_info
>
>  drivers/perf/riscv_pmu_sbi.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
> index 70cb50fd41c2..9d07af2b6be0 100644
> --- a/drivers/perf/riscv_pmu_sbi.c
> +++ b/drivers/perf/riscv_pmu_sbi.c
> @@ -868,6 +868,12 @@ static int pmu_sbi_device_probe(struct platform_device *pdev)
>                 goto out_free;
>         }
>
> +       /* It is possible to get from SBI more than max number of counters */
> +       if (num_counters > RISCV_MAX_COUNTERS) {
> +               pr_info("SBI returned more than maximum number of counters\n");

Being a little verbose to provide a clear message ?
"Limiting the number of counters to %d


> +               num_counters = RISCV_MAX_COUNTERS;
> +       }
> +
>         /* cache all the information about counters now */
>         if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
>                 goto out_free;
> --
> 2.37.2
>
>

Otherwise, LGTM.
Reviewed-by: Atish Patra <atishp@rivosinc.com>


--
Regards,
Atish
diff mbox series

Patch

diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.c
index 70cb50fd41c2..9d07af2b6be0 100644
--- a/drivers/perf/riscv_pmu_sbi.c
+++ b/drivers/perf/riscv_pmu_sbi.c
@@ -868,6 +868,12 @@  static int pmu_sbi_device_probe(struct platform_device *pdev)
 		goto out_free;
 	}
 
+	/* It is possible to get from SBI more than max number of counters */
+	if (num_counters > RISCV_MAX_COUNTERS) {
+		pr_info("SBI returned more than maximum number of counters\n");
+		num_counters = RISCV_MAX_COUNTERS;
+	}
+
 	/* cache all the information about counters now */
 	if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
 		goto out_free;