Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp1783008rdb; Sat, 20 Jan 2024 13:20:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHewDf/DkkdhLnQr/hR0xMIUUPiNrEQLEDZs05JViMQWK0bE91mWfP2BTuQQCQ+iVGEyYAd X-Received: by 2002:a5d:81cb:0:b0:7bf:5767:57e1 with SMTP id t11-20020a5d81cb000000b007bf576757e1mr2672024iol.30.1705785647380; Sat, 20 Jan 2024 13:20:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705785647; cv=pass; d=google.com; s=arc-20160816; b=G2Bg1p/Tab4S+uUJHX8idTlnwtxZHIz71VUq6z4Wz9CzPWW41a0f7cnFQ69qexyMNA A1aTYuZ8NC9EzT6mRMd09r0/9no9JYk0973TACbDPFg4eqrIbfvWaqvaRV3a9kP5jnRu AkycPBRaWXZX4yA+Gg3pbjmyHJR9Fu/nrzGf6yPlNBEhwp504vqmWlmMn3OpVaF8/D87 BMBTtmXuqqI4ZbScvsemrTkvVUaxMbWa17lYv4AKn8tnj5IYPW1dDUCMGEX/XlOi9Fgk 5JCbXQGvAmeqZjt2FkrB7bTcmf4tXtvMkGQGbq1I4lH1YbGuSVl7JW3OUKCqq48VSYQN vsHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:message-id:in-reply-to:subject:cc:to:from :date:dkim-signature; bh=aLyOhQr3iN/L4ULpUgDrbX9u1EDxEeONAWsqwCNe/IY=; fh=91Vlw+UT9Ajh6776bI9zBmGAqzKidV6BDCGAscXv97c=; b=PXZfhQfoMmK1ZfnOfHFcZazN5BzMq8RaifwB70fMgJpL2JzRwgLG8HkmOWoHZIH6qc yGeyrKUXTUXMkYCIPDEg4pxrfmhQ69DpGvGywEq0mQy/VRmJwfXzxVIs8wI3m3yryTA9 YUiZwJGm+ZbqzEkscSLj37nnykfnE3GlkrqYMuItj4TMr0mVwOPsPXQWjOKebYSSxjxB a05TTteBGFsSLFgLP53TvQhXnExu14OV7InB/nIofGiwlZEQxehRhaGRkaLfY51IjFTL jn8Xthv3GDL8GeW22ONYFxZ48SPyyHNjRQkh1jOoEojh9MEq7KPs4C8u9wI7pG4zfWkG TYMw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=glBUkyZG; arc=pass (i=1 spf=pass spfdomain=inria.fr dkim=pass dkdomain=inria.fr dmarc=pass fromdomain=inria.fr); spf=pass (google.com: domain of linux-kernel+bounces-31922-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r68-20020a632b47000000b005cd8938b10csi5545821pgr.631.2024.01.20.13.20.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jan 2024 13:20:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-31922-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@inria.fr header.s=dc header.b=glBUkyZG; arc=pass (i=1 spf=pass spfdomain=inria.fr dkim=pass dkdomain=inria.fr dmarc=pass fromdomain=inria.fr); spf=pass (google.com: domain of linux-kernel+bounces-31922-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-31922-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=inria.fr 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id DC55B282A0A for ; Sat, 20 Jan 2024 21:20:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 275971401C; Sat, 20 Jan 2024 21:20:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=inria.fr header.i=@inria.fr header.b="glBUkyZG" Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) (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 8AB5212E69 for ; Sat, 20 Jan 2024 21:20:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.134.164.104 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705785639; cv=none; b=i04TuJzU4EP3QxvUQUzop1n78d0nbSFuGwKO3pzej90viGZDUUo6jolnqxv4Ux/yRmav3jp2KTStrmu2azosmRlMtZJDOTtqb3fESK3N57Nfk0HavliM2UtgAgPIrcKoYlKqlYnD7f5RnLuMvlzHEdxJHI2yebeMeJGAKvPM8F0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705785639; c=relaxed/simple; bh=ZGkailrca/i8oHMh0IRF8KnrjV+IcsO0DQlFP38qjSM=; h=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References: MIME-Version:Content-Type; b=MY3jytDePh5Gt68vkgyrRbnG5/Ne2e6qAzZ9GmitHvsqjqSKc/7+4YrQ59KJFDnF5+qaxGgjEHu0zbzF81gSjGB+xOqrAUu3CH+UzfIEBtzJWwbv/UIlFmJvkgwCFjPX7ZuR5Dzk22R+k2oA9tm9NMQbQL8BuQu/SzzFx/l04DY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=inria.fr; spf=pass smtp.mailfrom=inria.fr; dkim=pass (1024-bit key) header.d=inria.fr header.i=@inria.fr header.b=glBUkyZG; arc=none smtp.client-ip=192.134.164.104 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=inria.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=inria.fr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inria.fr; s=dc; h=date:from:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=aLyOhQr3iN/L4ULpUgDrbX9u1EDxEeONAWsqwCNe/IY=; b=glBUkyZGQwPfjO/e2MF/Bb9T7i7TeWW10fRFH98M70+pyvgRzxTRlojP wnwOetgYbFRlrH+MAtSIeEB4ShXqBfQQrW1zzifIMF4+uIZv5D+LXoZft yM6E94v35xubzr8nF/PHhnCsEalUtMNa4wfdxSQ/xd9tRrrdSCUJTThj5 o=; Authentication-Results: mail3-relais-sop.national.inria.fr; dkim=none (message not signed) header.i=none; spf=SoftFail smtp.mailfrom=julia.lawall@inria.fr; dmarc=fail (p=none dis=none) d=inria.fr X-IronPort-AV: E=Sophos;i="6.05,208,1701126000"; d="scan'208";a="77364056" Received: from 231.85.89.92.rev.sfr.net (HELO hadrien) ([92.89.85.231]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jan 2024 22:20:27 +0100 Date: Sat, 20 Jan 2024 22:20:26 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Li Zhijian cc: linux-kernel@vger.kernel.org, Nicolas Palix , cocci@inria.fr Subject: Re: [PATCH v2] coccinelle: device_attr_show: Adapt to the latest Documentation/filesystems/sysfs.rst In-Reply-To: <20240119062057.4026888-1-lizhijian@fujitsu.com> Message-ID: References: <20240119062057.4026888-1-lizhijian@fujitsu.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) 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 On Fri, 19 Jan 2024, Li Zhijian wrote: > Adapt description, warning message and MODE=patch according to the latest > Documentation/filesystems/sysfs.rst: > > show() should only use sysfs_emit() or sysfs_emit_at() when formatting > > the value to be returned to user space. > > After this patch: > When MODE=report, > $ make coccicheck COCCI=scripts/coccinelle/api/device_attr_show.cocci M=drivers/hid/hid-picolcd_core.c MODE=report > <...snip...> > drivers/hid/hid-picolcd_core.c:304:8-16: WARNING: please use sysfs_emit or sysfs_emit_at > drivers/hid/hid-picolcd_core.c:259:9-17: WARNING: please use sysfs_emit or sysfs_emit_at > > When MODE=patch, > $ make coccicheck COCCI=scripts/coccinelle/api/device_attr_show.cocci M=drivers/hid/hid-picolcd_core.c MODE=patch > <...snip...> > diff -u -p a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c > --- a/drivers/hid/hid-picolcd_core.c > +++ b/drivers/hid/hid-picolcd_core.c > @@ -255,10 +255,12 @@ static ssize_t picolcd_operation_mode_sh > { > struct picolcd_data *data = dev_get_drvdata(dev); > > - if (data->status & PICOLCD_BOOTLOADER) > - return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n"); > - else > - return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n"); > + if (data->status & PICOLCD_BOOTLOADER) { > + return sysfs_emit(buf, "[bootloader] lcd\n"); > + } > + else { > + return sysfs_emit(buf, "bootloader [lcd]\n"); > + } > } > > static ssize_t picolcd_operation_mode_store(struct device *dev, > @@ -301,7 +303,7 @@ static ssize_t picolcd_operation_mode_de > { > struct picolcd_data *data = dev_get_drvdata(dev); > > - return snprintf(buf, PAGE_SIZE, "hello world\n"); > + return sysfs_emit(buf, "hello world\n"); > } > > static ssize_t picolcd_operation_mode_delay_store(struct device *dev, > > CC: Julia Lawall > CC: Nicolas Palix > CC: cocci@inria.fr > Signed-off-by: Li Zhijian > --- > V2: > - changed title from coccinelle: device_attr_show.cocci: update description and warning message > - Fix MODE=patch > - Extract patch from the patch set[1] so that maintainer can accept it separately. Applied. I subsequently simplified the patch case to contain the following: - snprintf(BUF, SZ, FORMAT + sysfs_emit(BUF, FORMAT ,...); This also works for the case where there are only three arguments. It has the benefit that the change is recognized as a line replacement, so no extra {} are added when the call is in an if branch. julia > [1] https://lore.kernel.org/lkml/20240116041129.3937800-1-lizhijian@fujitsu.com/ > --- > scripts/coccinelle/api/device_attr_show.cocci | 22 +++++++++++-------- > 1 file changed, 13 insertions(+), 9 deletions(-) > > diff --git a/scripts/coccinelle/api/device_attr_show.cocci b/scripts/coccinelle/api/device_attr_show.cocci > index a28dc061653a..634514937e63 100644 > --- a/scripts/coccinelle/api/device_attr_show.cocci > +++ b/scripts/coccinelle/api/device_attr_show.cocci > @@ -1,10 +1,8 @@ > // SPDX-License-Identifier: GPL-2.0-only > /// > /// From Documentation/filesystems/sysfs.rst: > -/// show() must not use snprintf() when formatting the value to be > -/// returned to user space. If you can guarantee that an overflow > -/// will never happen you can use sprintf() otherwise you must use > -/// scnprintf(). > +/// show() should only use sysfs_emit() or sysfs_emit_at() when formatting > +/// the value to be returned to user space. > /// > // Confidence: High > // Copyright: (C) 2020 Denis Efremov ISPRAS > @@ -30,15 +28,21 @@ ssize_t show(struct device *dev, struct device_attribute *attr, char *buf) > > @rp depends on patch@ > identifier show, dev, attr, buf; > +expression BUF, SZ, FORMAT, STR; > @@ > > ssize_t show(struct device *dev, struct device_attribute *attr, char *buf) > { > <... > +( > return > -- snprintf > -+ scnprintf > - (...); > +- snprintf(BUF, SZ, FORMAT, STR); > ++ sysfs_emit(BUF, FORMAT, STR); > +| > + return > +- snprintf(BUF, SZ, STR); > ++ sysfs_emit(BUF, STR); > +) > ...> > } > > @@ -46,10 +50,10 @@ ssize_t show(struct device *dev, struct device_attribute *attr, char *buf) > p << r.p; > @@ > > -coccilib.report.print_report(p[0], "WARNING: use scnprintf or sprintf") > +coccilib.report.print_report(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at") > > @script: python depends on org@ > p << r.p; > @@ > > -coccilib.org.print_todo(p[0], "WARNING: use scnprintf or sprintf") > +coccilib.org.print_todo(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at") > -- > 2.29.2 > >