Message ID | 20250114200716.969457-1-krzysztof.kozlowski@linaro.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | ufs: Use str_enable_disable-like helpers | expand |
Hello Krzysztof, > -----Original Message----- > From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> > Sent: Wednesday, January 15, 2025 1:37 AM > To: Alim Akhtar <alim.akhtar@samsung.com>; Avri Altman > <avri.altman@wdc.com>; Bart Van Assche <bvanassche@acm.org>; James > E.J. Bottomley <James.Bottomley@HansenPartnership.com>; Martin K. > Petersen <martin.petersen@oracle.com>; Peter Wang > <peter.wang@mediatek.com>; Stanley Jhu <chu.stanley@gmail.com>; > Matthias Brugger <matthias.bgg@gmail.com>; AngeloGioacchino Del Regno > <angelogioacchino.delregno@collabora.com>; linux-scsi@vger.kernel.org; > linux-kernel@vger.kernel.org; linux-mediatek@lists.infradead.org; linux- > arm-kernel@lists.infradead.org > Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> > Subject: [PATCH] ufs: Use str_enable_disable-like helpers > > Replace ternary (condition ? "enable" : "disable") syntax with helpers from > string_choices.h because: > 1. Simple function call with one argument is easier to read. Ternary > operator has three arguments and with wrapping might lead to quite > long code. > 2. Is slightly shorter thus also easier to read. > 3. It brings uniformity in the text - same string. > 4. Allows deduping by the linker, which results in a smaller binary > file. > > Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> > --- > drivers/ufs/core/ufshcd.c | 11 ++++++----- > drivers/ufs/host/ufs-mediatek.c | 7 +++---- > 2 files changed, 9 insertions(+), 9 deletions(-) > Reviewed-by: Alim Akhtar <alim.akhtar@samsung.com> On a side note, there are other host controller driver (e.g. exynos and Qcomm) which also uses few conditional operators, But I didn't find a matching helper in string_choices.h, so may be that can be taken separately in future. > diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index > 27154a5dcb7b..5225d48a47f8 100644 > --- a/drivers/ufs/core/ufshcd.c > +++ b/drivers/ufs/core/ufshcd.c [Snip] > -- > 2.43.0
On Tue, 2025-01-14 at 21:07 +0100, Krzysztof Kozlowski wrote: > > External email : Please do not click links or open attachments until > you have verified the sender or the content. > > > Replace ternary (condition ? "enable" : "disable") syntax with > helpers > from string_choices.h because: > 1. Simple function call with one argument is easier to read. Ternary > operator has three arguments and with wrapping might lead to quite > long code. > 2. Is slightly shorter thus also easier to read. > 3. It brings uniformity in the text - same string. > 4. Allows deduping by the linker, which results in a smaller binary > file. > > Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> > --- > drivers/ufs/core/ufshcd.c | 11 ++++++----- > drivers/ufs/host/ufs-mediatek.c | 7 +++---- > 2 files changed, 9 insertions(+), 9 deletions(-) > Reviewed-by: Peter Wang <peter.wang@mediatek.com>
On 1/14/25 12:07 PM, Krzysztof Kozlowski wrote:
> 2. Is slightly shorter thus also easier to read.
Does this change really make code easier to read? It forces readers
of the code to look up a function definition. Isn't there a general
preference in the Linux kernel to inline function definitions if the
function body is shorter than or close to the length of the function
name? I'm referring to functions like this one:
static inline const char *str_up_down(bool v)
{
return v ? "up" : "down";
}
Bart.
On 15/01/2025 18:53, Bart Van Assche wrote: > On 1/14/25 12:07 PM, Krzysztof Kozlowski wrote: >> 2. Is slightly shorter thus also easier to read. > > Does this change really make code easier to read? It forces readers > of the code to look up a function definition. Isn't there a general There is no general preference, up to you. > preference in the Linux kernel to inline function definitions if the > function body is shorter than or close to the length of the function > name? I'm referring to functions like this one: > > static inline const char *str_up_down(bool v) > { > return v ? "up" : "down"; > } > > Bart. It's subjective - in some places ternary is really complicating, from my other patch: data & XCSI_DLXINFR_SOTERR ? "true" : "false", video->hq_mode ? "on" : "off", video->jpeg_hq_quality); note that's ternary is split here: dstat & BT848_DSTATUS_HLOC ? "yes" : "no"); I understand if you don't find the code here better. Best regards, Krzysztof
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c index 27154a5dcb7b..5225d48a47f8 100644 --- a/drivers/ufs/core/ufshcd.c +++ b/drivers/ufs/core/ufshcd.c @@ -23,6 +23,7 @@ #include <linux/pm_opp.h> #include <linux/regulator/consumer.h> #include <linux/sched/clock.h> +#include <linux/string_choices.h> #include <linux/iopoll.h> #include <scsi/scsi_cmnd.h> #include <scsi/scsi_dbg.h> @@ -1187,7 +1188,7 @@ static int ufshcd_scale_clks(struct ufs_hba *hba, unsigned long freq, out: trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), - (scale_up ? "up" : "down"), + str_up_down(scale_up), ktime_to_us(ktime_sub(ktime_get(), start)), ret); return ret; } @@ -1549,7 +1550,7 @@ static int ufshcd_devfreq_target(struct device *dev, hba->clk_scaling.target_freq = *freq; trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), - (scale_up ? "up" : "down"), + str_up_down(scale_up), ktime_to_us(ktime_sub(ktime_get(), start)), ret); out: @@ -6026,7 +6027,7 @@ int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable) hba->dev_info.wb_enabled = enable; dev_dbg(hba->dev, "%s: Write Booster %s\n", - __func__, enable ? "enabled" : "disabled"); + __func__, str_enabled_disabled(enable)); return ret; } @@ -6044,7 +6045,7 @@ static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba, return; } dev_dbg(hba->dev, "%s: WB-Buf Flush during H8 %s\n", - __func__, enable ? "enabled" : "disabled"); + __func__, str_enabled_disabled(enable)); } int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable) @@ -6064,7 +6065,7 @@ int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable) hba->dev_info.wb_buf_flush_enabled = enable; dev_dbg(hba->dev, "%s: WB-Buf Flush %s\n", - __func__, enable ? "enabled" : "disabled"); + __func__, str_enabled_disabled(enable)); return ret; } diff --git a/drivers/ufs/host/ufs-mediatek.c b/drivers/ufs/host/ufs-mediatek.c index 135cd78109e2..a7804fe387e9 100644 --- a/drivers/ufs/host/ufs-mediatek.c +++ b/drivers/ufs/host/ufs-mediatek.c @@ -19,6 +19,7 @@ #include <linux/platform_device.h> #include <linux/regulator/consumer.h> #include <linux/reset.h> +#include <linux/string_choices.h> #include <ufs/ufshcd.h> #include "ufshcd-pltfrm.h" @@ -480,10 +481,8 @@ static int ufs_mtk_mphy_power_on(struct ufs_hba *hba, bool on) } out: if (ret) { - dev_info(hba->dev, - "failed to %s va09: %d\n", - on ? "enable" : "disable", - ret); + dev_info(hba->dev, "failed to %s va09: %d\n", + str_enable_disable(on), ret); } else { host->mphy_powered_on = on; }
Replace ternary (condition ? "enable" : "disable") syntax with helpers from string_choices.h because: 1. Simple function call with one argument is easier to read. Ternary operator has three arguments and with wrapping might lead to quite long code. 2. Is slightly shorter thus also easier to read. 3. It brings uniformity in the text - same string. 4. Allows deduping by the linker, which results in a smaller binary file. Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> --- drivers/ufs/core/ufshcd.c | 11 ++++++----- drivers/ufs/host/ufs-mediatek.c | 7 +++---- 2 files changed, 9 insertions(+), 9 deletions(-)