Received: by 2002:a05:7412:8d08:b0:f9:2d0a:d759 with SMTP id bj8csp129221rdb; Sun, 17 Dec 2023 05:25:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IF2yh8IHvYUt8lm75PLZo79JXwHI8/NBba6wGtMVa54lXQFS6L4xi4D72AK7stcFfjM2Rfu X-Received: by 2002:a05:6e02:2146:b0:35f:aa70:6888 with SMTP id d6-20020a056e02214600b0035faa706888mr2272876ilv.40.1702819549008; Sun, 17 Dec 2023 05:25:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702819548; cv=none; d=google.com; s=arc-20160816; b=aE0lpSI/wFUq1SWBPjq6fJ1lU2enixadX33z+bTicDAmIHLErueRC71THOqyH/zW1Y IeAeQRp/L8/I1LK1ih1wlrz8y6eEr1k/FJLygfiS4ody0Xa7ZGiHAZM64QNMLiMTtPQ0 MIpPjY2lAwsY1/jGR16pLHH3Q7gSTTYKNDjHZP9XhpybochLb8vw8U+zll+G27kpLLJ0 wTro9Gy9genloeE2FCZL3YQx27DujXfJLZS+mHPcA8b7XyhURq4SwFpwUyOq0CpMOo3H pquyu21SCT8W+lfuNnhAzoanVpOpf+plYkSmq6cBGy5ARthbjlLSu5na1jrJwGTfavQ1 hVbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=mAKaNIY6ya8ZIcDSUKI/lYDMtqNZN6GxcRlAqeYRQYI=; fh=VLuRt+OGq3YsnZD5PFZdYLjcGVpDXzmUGyphxObPv/4=; b=fRpk6rTs7CUT4YVxpsxVUQSUAZlHrTJUmYx0T0WLwitLTZa6JefzQOiMK796d8rq0v +cU4Vu7lKY9rmLVO5bAExssTp3uahC45snIVPNdyE1FOdmqbNQRQy7a8U0XyTtU6Wuo6 ZqF8AGStuv0UOxIJTs5/yC1/9HFyy0WdGhtFNTRytkCfGPK5MuxWMnvSZ6Xc+Q706iDW i0dVxdJCMDewJ1CbiPbKP9VqzhjAWCjUeif/FrDosoPvJhwzvej1oFn6U3zh5VNF2lQC yv8/iJdnx6S8vZzeHcAIQBpFTzwlpBBOdq1XmbWnYK9GEcxCazMetzEJ4r8FnBSiz11+ PSuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WwGAWtSw; spf=pass (google.com: domain of linux-kernel+bounces-2608-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2608-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 23-20020a630017000000b005c668dc2426si16391916pga.176.2023.12.17.05.25.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 05:25:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2608-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WwGAWtSw; spf=pass (google.com: domain of linux-kernel+bounces-2608-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2608-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 22CCAB2204A for ; Sun, 17 Dec 2023 13:25:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 191514436D; Sun, 17 Dec 2023 13:25:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WwGAWtSw" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35CA844369; Sun, 17 Dec 2023 13:25:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 261C9C433C8; Sun, 17 Dec 2023 13:24:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702819500; bh=TU3V0XOIrapBSmbG7wJEKVU27RSZUFivlAPhn2ejiho=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=WwGAWtSwzp+kQ33anWPPbw4sp0k5GSc+B7foyxBmmzTDn+ejGRP95AwXeRJ8aMy5j 5H1QIggxF76XNmex6sBrZ/ovgdJ+GozaNePV0sRO90+6SgNDfQqMKb/HhpkP8h6wys +bK9zS9XK0w+lX5jNaNXtmPitTf5LpxPSDHz3hRa/W5MVaRiQJai40lRtAu7DTKlRo aA6596234QCdxRp+BnPWKa2ogx7d4EXgRBESsPOKTj39jEkJ6MWJu2qmXuE/3xCsqN Xtv1I0EALe3T4KjKfWJtzUct9weHZxkgYuUjfp9XYijEgL9sCqB0lGePV5DxMIsUki V3Swj7REp0L6g== Date: Sun, 17 Dec 2023 13:24:47 +0000 From: Jonathan Cameron To: Joe Perches Cc: Justin Stitt , Lars-Peter Clausen , Stephen Boyd , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v3] iio: sx9324: avoid copying property strings Message-ID: <20231217132447.269072df@jic23-huawei> In-Reply-To: References: <20231212-strncpy-drivers-iio-proximity-sx9324-c-v3-1-b8ae12fc8a5d@google.com> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 11 Dec 2023 22:30:12 -0800 Joe Perches wrote: > On Tue, 2023-12-12 at 00:42 +0000, Justin Stitt wrote: > > We're doing some needless string copies when trying to assign the proper > > `prop` string. We can make `prop` a const char* and simply assign to > > string literals. > > trivia: > > I would have updated it like this moving the > various declarations into the case blocks > where they are used and removing a few unused > #defines I'd definitely like to see those defines gone. Arguably an unrelated change as I guess they are left from a previous refactor of this code. Why prop to type renaming? It's getting passed into calls where the parameter is propname so I'd understand renaming to that, but type just seems a bit random to me. I do wonder if we are better off having some long lines and getting rid of the property naming local variables completely by just duplicating the device_property_read_u32() call and passing them in directly. Moving declarations more locally is a nice to have but I'll leave that up to Justin. Anyhow, both solutions look much better than the original so I'm fine either way (subject to responses to Stephen's review) > > --- > drivers/iio/proximity/sx9324.c | 69 +++++++++++++++++++++++++----------------- > 1 file changed, 41 insertions(+), 28 deletions(-) > > diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c > index ac2ed2da21ccc..c50c1108a69cc 100644 > --- a/drivers/iio/proximity/sx9324.c > +++ b/drivers/iio/proximity/sx9324.c > @@ -877,17 +877,8 @@ static const struct sx_common_reg_default * > sx9324_get_default_reg(struct device *dev, int idx, > struct sx_common_reg_default *reg_def) > { > - static const char * const sx9324_rints[] = { "lowest", "low", "high", > - "highest" }; > - static const char * const sx9324_csidle[] = { "hi-z", "hi-z", "gnd", > - "vdd" }; > -#define SX9324_PIN_DEF "semtech,ph0-pin" > -#define SX9324_RESOLUTION_DEF "semtech,ph01-resolution" > -#define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength" > - unsigned int pin_defs[SX9324_NUM_PINS]; > - char prop[] = SX9324_PROXRAW_DEF; > - u32 start = 0, raw = 0, pos = 0; > - int ret, count, ph, pin; > + u32 raw = 0; > + int ret; > > memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def)); > > @@ -896,7 +887,13 @@ sx9324_get_default_reg(struct device *dev, int idx, > case SX9324_REG_AFE_PH0: > case SX9324_REG_AFE_PH1: > case SX9324_REG_AFE_PH2: > - case SX9324_REG_AFE_PH3: > + case SX9324_REG_AFE_PH3: { > + unsigned int pin_defs[SX9324_NUM_PINS]; > + int count; > + int pin; > + int ph; > + char prop[32]; > + > ph = reg_def->reg - SX9324_REG_AFE_PH0; > snprintf(prop, ARRAY_SIZE(prop), "semtech,ph%d-pin", ph); > > @@ -913,7 +910,15 @@ sx9324_get_default_reg(struct device *dev, int idx, > SX9324_REG_AFE_PH0_PIN_MASK(pin); > reg_def->def = raw; > break; > - case SX9324_REG_AFE_CTRL0: > + } > + case SX9324_REG_AFE_CTRL0: { > + static const char * const sx9324_csidle[] = { > + "hi-z", "hi-z", "gnd", "vdd" > + }; > + static const char * const sx9324_rints[] = { > + "lowest", "low", "high", "highest" > + }; > + > ret = device_property_match_property_string(dev, "semtech,cs-idle-sleep", > sx9324_csidle, > ARRAY_SIZE(sx9324_csidle)); > @@ -930,16 +935,17 @@ sx9324_get_default_reg(struct device *dev, int idx, > reg_def->def |= ret << SX9324_REG_AFE_CTRL0_RINT_SHIFT; > } > break; > + } > case SX9324_REG_AFE_CTRL4: > - case SX9324_REG_AFE_CTRL7: > + case SX9324_REG_AFE_CTRL7: { > + const char *type; > + > if (reg_def->reg == SX9324_REG_AFE_CTRL4) > - strncpy(prop, "semtech,ph01-resolution", > - ARRAY_SIZE(prop)); > + type = "semtech,ph01-resolution"; > else > - strncpy(prop, "semtech,ph23-resolution", > - ARRAY_SIZE(prop)); > + type = "semtech,ph23-resolution"; > > - ret = device_property_read_u32(dev, prop, &raw); > + ret = device_property_read_u32(dev, type, &raw); > if (ret) > break; > > @@ -949,6 +955,7 @@ sx9324_get_default_reg(struct device *dev, int idx, > reg_def->def |= FIELD_PREP(SX9324_REG_AFE_CTRL4_RESOLUTION_MASK, > raw); > break; > + } > case SX9324_REG_AFE_CTRL8: > ret = device_property_read_u32(dev, > "semtech,input-precharge-resistor-ohms", > @@ -982,17 +989,21 @@ sx9324_get_default_reg(struct device *dev, int idx, > 6 + raw * (raw + 3) / 2); > break; > > - case SX9324_REG_ADV_CTRL5: > + case SX9324_REG_ADV_CTRL5: { > + u32 start = 0; > + > ret = device_property_read_u32(dev, "semtech,startup-sensor", > &start); > if (ret) > break; > - > reg_def->def &= ~SX9324_REG_ADV_CTRL5_STARTUPSENS_MASK; > reg_def->def |= FIELD_PREP(SX9324_REG_ADV_CTRL5_STARTUPSENS_MASK, > start); > break; > - case SX9324_REG_PROX_CTRL4: > + } > + case SX9324_REG_PROX_CTRL4: { > + u32 pos = 0; > + > ret = device_property_read_u32(dev, "semtech,avg-pos-strength", > &pos); > if (ret) > @@ -1005,15 +1016,16 @@ sx9324_get_default_reg(struct device *dev, int idx, > reg_def->def |= FIELD_PREP(SX9324_REG_PROX_CTRL4_AVGPOSFILT_MASK, > raw); > break; > + } > case SX9324_REG_PROX_CTRL0: > - case SX9324_REG_PROX_CTRL1: > + case SX9324_REG_PROX_CTRL1: { > + const char *type; > + > if (reg_def->reg == SX9324_REG_PROX_CTRL0) > - strncpy(prop, "semtech,ph01-proxraw-strength", > - ARRAY_SIZE(prop)); > + type = "semtech,ph01-proxraw-strength"; > else > - strncpy(prop, "semtech,ph23-proxraw-strength", > - ARRAY_SIZE(prop)); > - ret = device_property_read_u32(dev, prop, &raw); > + type = "semtech,ph23-proxraw-strength"; > + ret = device_property_read_u32(dev, type, &raw); > if (ret) > break; > > @@ -1022,6 +1034,7 @@ sx9324_get_default_reg(struct device *dev, int idx, > raw); > break; > } > + } > return reg_def; > } > > >