Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2653564pxb; Thu, 11 Feb 2021 19:39:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJwHQJHEh+YWD1gq028H7uJbYY4KJC7zmtTrmk2vB9l4tePA0sE08SzMP9TDEFliiQmmncHM X-Received: by 2002:a17:907:170d:: with SMTP id le13mr1003597ejc.220.1613101196062; Thu, 11 Feb 2021 19:39:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613101196; cv=none; d=google.com; s=arc-20160816; b=l+Z2Z2Ysn0oANs5+ZkN7Pl7eo17A/IFLPpDkJnAnnEiUYMFxx7q1UNvOpSNgsuhgU/ uN4V+3oPBsL31sI8MkF30csj2ltLGnpSsdKE5fD3LG1fzbLWk4mWtvvghfiF8YQDOZgD 1Zo26WyEOdPo1iDEl35ZTLBRxJRHqBtB0nfrKwnLwZOgi9EnfT92yJ2sufvR4YB0ZeFn Vlx/UwM+TxSQ0N5bJuv9n9BKQ7izhBYNup4qx6bT2bpfCCGpkz0hf7QIiExq12AMO7Jp finSjzJMPeoXa8PgOuDsVO8resDHErGcGV2RP67R3ZxcOhbc7TtMj/t3cR4acpq51H3B hDLA== 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=ax9j7JpnwX2aHngFLHm4pofQY/NwW/wb9UVqk+4OdiA=; b=ncRN7kpLN/bCZlgCL+FAUK0l41DmPdVpD9nanYAGfLFJMIl/DbiEfQoK7f7eWPmq5u qhA41cmoSngf6zjEgTtzMepGiTwJ/bPihYBQPVUDA4xSoXC1MsX0tZnSUrDVAuiq2G5V uHNOxKeKNO/kKhIi8xnQRfDKPY1VBI5t0xu2OP4o2h2OvyJH11WCljUAj1NegGX8i0/J b8y2ljNb4EeXnYUQUeCG2mcOEdlmo+Rd29WsRK1u/eNnpvfk/26sj5RcfAsA+cE/v8vn wjNl4oVaXNBKGhIb27dKfNyuWYHQxZ9ibugL6fIl3g2i1XnvD/6YrjoZKuNRWTc5K2iV WFXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@beagleboard-org.20150623.gappssmtp.com header.s=20150623 header.b=CRqpWozf; 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 f7si6457893edd.595.2021.02.11.19.39.32; Thu, 11 Feb 2021 19:39:56 -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=CRqpWozf; 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 S229636AbhBLDiq (ORCPT + 99 others); Thu, 11 Feb 2021 22:38:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229608AbhBLDip (ORCPT ); Thu, 11 Feb 2021 22:38:45 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E998EC0613D6 for ; Thu, 11 Feb 2021 19:37:59 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id x9so4431209plb.5 for ; Thu, 11 Feb 2021 19:37:59 -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=ax9j7JpnwX2aHngFLHm4pofQY/NwW/wb9UVqk+4OdiA=; b=CRqpWozflkom32qIUXsfAivIflqvx6/ETRU0gKrlPX0bcAFVHMleAFzBDC1RtGwdRX TlGaD8FOFcijWiUdntOrIPLX7CA7sMcftmyY7WK0YcJhkrWLNBjID+TU/IHHq2hWVQTc EkGeBd4nv1YzQs/2JalDYnDpBzl5JPDR9kidM9aShFmkxnuyeLth1EkGItotpX7vFlXj YF8UcOwhMrCmicItxGYlne70LhDE7kPUPx/k1utsMk+RZ0cxmnJ6GxIr5IGp9zv9mPeL qW2jF0YwX1lM0vKkoZxCGeOwMUmdbvw2AIId+VMcr6oGdlbEStr/4HI8c4WuhVVSbKS2 diqg== 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=ax9j7JpnwX2aHngFLHm4pofQY/NwW/wb9UVqk+4OdiA=; b=AIa9YFcmNYna38MCK6Z8eGWWe6seLLMS07Yf4ZtnQwWgxTIAgj14bdx0gAGo95GwWw o0YVt7jYvClspIfcFF9ODu2+36S1d/EV6yQFKU7f9kaGQcePb5XKV0nafp4xThaOVrTZ rqakRRX6EcdgqLfcFAaltaMukskqNo2rs/sbx/DkG3df4qLRm5JLJj0vQju8uEx4U0ji qAwmvKAFvnksOyKuKo4j+GwGQj2u+8LcEU/RIRNnYCykPn2Z89pmhOQEZ+jV+7aDhrRC Gp8A5FHeI8udY9nUEVCCj85WKkrZd0oCKY0YqeDJ+2MiHr3AFgEszZz91mUZOfaL8Frm znFw== X-Gm-Message-State: AOAM533rXj5MKVGXe8t7heCezFsy2OP43L6NUVCkDNod2ftmGSTcI7Xz B3evo49fiRpo4MyHhLd9Zskeaw== X-Received: by 2002:a17:90a:2b8c:: with SMTP id u12mr913586pjd.98.1613101079374; Thu, 11 Feb 2021 19:37:59 -0800 (PST) Received: from x1 ([2601:1c0:4701:ae70:3a7f:bbfb:1664:2a63]) by smtp.gmail.com with ESMTPSA id b62sm5989958pga.8.2021.02.11.19.37.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Feb 2021 19:37:58 -0800 (PST) Date: Thu, 11 Feb 2021 19:37:56 -0800 From: Drew Fustini To: Geert Uytterhoeven Cc: Linus Walleij , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List , Tony Lindgren , Andy Shevchenko , Alexandre Belloni , Pantelis Antoniou , Jason Kridner , Robert Nelson , Joe Perches , Dan Carpenter Subject: Re: [PATCH v4 2/2] pinctrl: pinmux: Add pinmux-select debugfs file Message-ID: <20210212033756.GB347396@x1> References: <20210210222851.232374-1-drew@beagleboard.org> <20210210222851.232374-3-drew@beagleboard.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 11, 2021 at 09:09:03AM +0100, Geert Uytterhoeven wrote: > Hi Drew, > > On Wed, Feb 10, 2021 at 11:33 PM 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 > > Thanks for your patch! > > > --- 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); > > While this guarantees buf is not overflowed... > > > + 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); > > ... one of the two strings can still be longer than PINMUX_MAX_NAME, > thus overflowing fname or gname. > > As buf is already a copy, it may be easier to just find the strings in > buf, write the NUL terminators into buf, and set up fname and gname > to point to the strings inside buf. Thank you for pointing this out. I should have considered that one name could be much larger than the other name. I see Andy suggested alternative to sscanf() that gets around having to allocate seperate buffers for each name. > > + if (ret != 2) { > > + dev_err(pctldev->dev, "expected format: "); > > + goto free_gname; > > + } > > > +static const struct file_operations pinmux_select_ops = { > > + .owner = THIS_MODULE, > > + .open = pinmux_select_open, > > + .read = seq_read, > > I don't think you need to fill in .read for a write-only file. Thanks, I'll remove it. > > > + .write = pinmux_select, > > + .llseek = no_llseek, > > + .release = single_release, > > +}; > > + > > void pinmux_init_device_debugfs(struct dentry *devroot, > > struct pinctrl_dev *pctldev) > > { > > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org > > In personal conversations with technical people, I call myself a hacker. But > when I'm talking to journalists I just say "programmer" or something like that. > -- Linus Torvalds