Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2654733pxb; Thu, 11 Feb 2021 19:43:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwekHrpJwjSA8jxcgMAM0xxmeHMalz2vZkNsaPLm8xGMvfdirGi2gmxwuaawm4ekILzcxiX X-Received: by 2002:a17:906:2e4f:: with SMTP id r15mr971015eji.407.1613101409396; Thu, 11 Feb 2021 19:43:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613101409; cv=none; d=google.com; s=arc-20160816; b=XDwXI+/ur2ByR92YPyAbqjkDskkX3vO7IjmdZWYEZszdRqp86QSg1r/j3lPwEqbZOE sTDJY5uKijLS89pbraHpMYUywxyQSFrgWffflsPs9QGKiQq8N0SltxS15CRgoDXFei8q sSz6nm17nP0CjCYnCWsvka1kr1yoFvup2jITecL42VNpvSGuOzTF7IX7ije7dDRGMMXi DN1iBn8VfefDfDub8Cct7rIhGHXUDh0DMxLf3J1oCKE7xFwL+HU8YfMqtmXeQGbpU0wW kTeVNauU+06Dtbth7IFQOZnf6Wblc6+j92zERAsDue7EH0Buy2J2VCOfPFIF/glBNSvQ 3EKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tTDYVeOuBaVSBqqaT4l7JPSDFmYOyLMrk9qMZM2itYY=; b=Ddh8aX4YzE0vzPaAD261Ee+1GPDx4VVYaNXZI6xYBXTgXofKMIPfmorOG2nTiIHuWD XytHT0oiKYOF0hMUh/4Bmge3/He0RpKd9mhvMiCgOHDmbjqAi+7R/WQuTEDeqpleAudC jSfhCNliJBWneyQnQ89NC+Ow15lEeAYx++jZQ3fpNt/4DMcP/VyhFv8MRnFAzMb5D6DO M6xXB/ot36zKZRsJvfa2gMi6TOeASDsZnNy7eYKMtuDkyIJq0EO//UYZAMqHJj7k8m8g dnn30qzz5BIE7Kh4wTtfgNzyTsGoGjy/usuWxk6FupFwDhANdl0BQkUPYkiHKwdjUf90 xYRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@beagleboard-org.20150623.gappssmtp.com header.s=20150623 header.b=s2pCs64q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j15si5526078edw.310.2021.02.11.19.43.05; Thu, 11 Feb 2021 19:43:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@beagleboard-org.20150623.gappssmtp.com header.s=20150623 header.b=s2pCs64q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229611AbhBLDl6 (ORCPT + 99 others); Thu, 11 Feb 2021 22:41:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229493AbhBLDl4 (ORCPT ); Thu, 11 Feb 2021 22:41:56 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 314BEC061756 for ; Thu, 11 Feb 2021 19:41:15 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id d2so4617669pjs.4 for ; Thu, 11 Feb 2021 19:41:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beagleboard-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=tTDYVeOuBaVSBqqaT4l7JPSDFmYOyLMrk9qMZM2itYY=; b=s2pCs64quzfkQ0UzIlFQ3tXiXmlesXUnb2HxWDzyStRVgnJbsKJFAYX18+EqZWQw3y iQ2sAn2GL91aJPxkLGeOdG8TKjkLqmG1tAl45LXhD2PJ+by5TeOt/xex3yAhh09mvNFV kP/BazcmjwvDXdZuFUD+vbUoJTTjWHVysU1UY9FoO5tZl6zaKZbytjWIQcrZcmdd20+Y PhZ+77mivgtDj8XessP9xD3qmS612VTZShYmiUDxQRS0S2E6z6eldHoa9eLpoZMJLI46 Pwtp3rRNW2ERUYGFnUd4qADm2OfzgEH892VJb8Q07nauuXg9fFnU5vdJGO7rd7kidPe2 uLNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tTDYVeOuBaVSBqqaT4l7JPSDFmYOyLMrk9qMZM2itYY=; b=s9oLt4EbEPCp6LevDNQ3Bc/U7Vezk/DWg1PuweDz7ipJr9vjudlpBn9uuULW84ZJOr LF4B02c6FZGI3jp7LJ43kUh+yUxIrnedAUvW/WJdWBawr6IT7O6mzseUXseooM13Hjt0 uXvqgF6X62hSAzGMEO3HDqPdIw0V55EiQMnzelgPUOYE/wMZT0mrVF57eRMx6Be3w4WY sRa7vufSakzbBSlo8JuDs1i68/3P1HISidujYKS0dR8gIh+B/aRUdN2tby9uc1mLoZ/r P1qEPS3y//uhhJzC1cB9GQdKhgBNoKWsxJcVO4gfj0qblV54MADVZkOAbzajHcmEoJ8o o9zg== X-Gm-Message-State: AOAM533HtpG5CSjRPz9aH5QLUD+khhN17sDxRwSfsYAcIfrSgfBAY715 Cu1RTZ3zRad/MNMnob+jQXbQew== X-Received: by 2002:a17:903:3109:b029:e2:86d3:bec with SMTP id w9-20020a1709033109b02900e286d30becmr1030600plc.49.1613101274692; Thu, 11 Feb 2021 19:41:14 -0800 (PST) Received: from x1 ([2601:1c0:4701:ae70:3a7f:bbfb:1664:2a63]) by smtp.gmail.com with ESMTPSA id a5sm7136604pgh.15.2021.02.11.19.41.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Feb 2021 19:41:13 -0800 (PST) Date: Thu, 11 Feb 2021 19:41:11 -0800 From: Drew Fustini To: Dan Carpenter Cc: Linus Walleij , linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Tony Lindgren , Andy Shevchenko , Alexandre Belloni , Geert Uytterhoeven , Pantelis Antoniou , Jason Kridner , Robert Nelson , Joe Perches Subject: Re: [PATCH v4 2/2] pinctrl: pinmux: Add pinmux-select debugfs file Message-ID: <20210212034111.GD347396@x1> References: <20210210222851.232374-1-drew@beagleboard.org> <20210210222851.232374-3-drew@beagleboard.org> <20210211120051.GN20820@kadam> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210211120051.GN20820@kadam> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 11, 2021 at 03:00:51PM +0300, Dan Carpenter wrote: > On Wed, Feb 10, 2021 at 02:28:54PM -0800, Drew Fustini wrote: > > Add "pinmux-select" to debugfs which will activate a function and group > > when "" are written to the file. The write > > operation pinmux_select() handles this by checking that the names map to > > valid selectors and then calling ops->set_mux(). > > > > The existing "pinmux-functions" debugfs file lists the pin functions > > registered for the pin controller. For example: > > > > function: pinmux-uart0, groups = [ pinmux-uart0-pins ] > > function: pinmux-mmc0, groups = [ pinmux-mmc0-pins ] > > function: pinmux-mmc1, groups = [ pinmux-mmc1-pins ] > > function: pinmux-i2c0, groups = [ pinmux-i2c0-pins ] > > function: pinmux-i2c1, groups = [ pinmux-i2c1-pins ] > > function: pinmux-spi1, groups = [ pinmux-spi1-pins ] > > > > To activate function pinmux-i2c1 and group pinmux-i2c1-pins: > > > > echo "pinmux-i2c1 pinmux-i2c1-pins" > pinmux-select > > > > Signed-off-by: Drew Fustini > > --- > > drivers/pinctrl/pinmux.c | 107 +++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 107 insertions(+) > > > > diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c > > index c651b2db0925..23fa32f0a067 100644 > > --- a/drivers/pinctrl/pinmux.c > > +++ b/drivers/pinctrl/pinmux.c > > @@ -673,6 +673,111 @@ void pinmux_show_setting(struct seq_file *s, > > DEFINE_SHOW_ATTRIBUTE(pinmux_functions); > > DEFINE_SHOW_ATTRIBUTE(pinmux_pins); > > > > +#define PINMUX_MAX_NAME 64 > > +static ssize_t pinmux_select(struct file *file, const char __user *user_buf, > > + size_t len, loff_t *ppos) > > +{ > > + struct seq_file *sfile = file->private_data; > > + struct pinctrl_dev *pctldev = sfile->private; > > + const struct pinmux_ops *pmxops = pctldev->desc->pmxops; > > + const char *const *groups; > > + char *buf, *fname, *gname; > > + unsigned int num_groups; > > + int fsel, gsel, ret; > > + > > + if (len > (PINMUX_MAX_NAME * 2)) { > > + dev_err(pctldev->dev, "write too big for buffer"); > > + return -EINVAL; > > + } > > + > > + buf = kzalloc(PINMUX_MAX_NAME * 2, GFP_KERNEL); > > + if (!buf) > > + return -ENOMEM; > > + > > + fname = kzalloc(PINMUX_MAX_NAME, GFP_KERNEL); > > + if (!fname) { > > + ret = -ENOMEM; > > + goto free_buf; > > + } > > + > > + gname = kzalloc(PINMUX_MAX_NAME, GFP_KERNEL); > > + if (!buf) { > > + ret = -ENOMEM; > > + goto free_fname; > > + } > > + > > + ret = strncpy_from_user(buf, user_buf, PINMUX_MAX_NAME * 2); > > + if (ret < 0) { > > + dev_err(pctldev->dev, "failed to copy buffer from userspace"); > > + goto free_gname; > > + } > > + buf[len-1] = '\0'; > > + > > + ret = sscanf(buf, "%s %s", fname, gname); > > + if (ret != 2) { > > + dev_err(pctldev->dev, "expected format: "); > > + goto free_gname; > > + } > > + > > + fsel = pinmux_func_name_to_selector(pctldev, fname); > > + if (fsel < 0) { > > + dev_err(pctldev->dev, "invalid function %s in map table\n", fname); > > + ret = -EINVAL; > > + goto free_gname; > > + } > > + > > + ret = pmxops->get_function_groups(pctldev, fsel, &groups, &num_groups); > > + if (ret) { > > + dev_err(pctldev->dev, "no groups for function %d (%s)", fsel, fname); > > + goto free_gname; > > + > > + } > > + > > + ret = match_string(groups, num_groups, gname); > > + if (ret < 0) { > > + dev_err(pctldev->dev, "invalid group %s", gname); > > + goto free_gname; > > + } > > + > > + ret = pinctrl_get_group_selector(pctldev, gname); > > + if (ret < 0) { > > + dev_err(pctldev->dev, "failed to get group selectorL %s", gname); > > + goto free_gname; > > + } > > + gsel = ret; > > + > > + ret = pmxops->set_mux(pctldev, fsel, gsel); > > + if (ret) { > > + dev_err(pctldev->dev, "set_mux() failed: %d", ret); > > + goto free_gname; > > + } > > + > > + return len; > > + > > +free_gname: > > + devm_kfree(pctldev->dev, gname); > > +free_fname: > > + devm_kfree(pctldev->dev, fname); > > +free_buf: > > + devm_kfree(pctldev->dev, buf); > > Ugh... I honestly thought Smatch was supposed to print a warning when > you used devm_kfree() on kzalloc()ed memory, but I guess the warning is > only the other way around. > > Smatch does complain about it as a leak because it was expecting a > regular free. > > drivers/pinctrl/pinmux.c:330 pinmux_func_name_to_selector() warn: potential NULL parameter dereference 'fname' > drivers/pinctrl/pinmux.c:764 pinmux_select() warn: possible memory leak of 'gname' > drivers/pinctrl/pinmux.c:764 pinmux_select() warn: sscanf doesn't return error codes > drivers/pinctrl/pinmux.c:764 pinmux_select() warn: returning success when sscanf failed Thank you pointing this out. I should have switched back to normal free now that I no longer using a device managed buffer. > > And what about the success path? Shouldn't we free these on the success > path as well? Good point. That is my fault. I will update it so that it correctly free's buffer no matter what happens in the function. thanks, drew