Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp1255190pxb; Fri, 20 Nov 2020 05:18:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJxwVcf/VIdFitJiGS+E0sHCM7HjyXlBnQD2WbZTLyOWKs2w/Mh1zop/VvqrxZqB044G0PX/ X-Received: by 2002:a50:ff0c:: with SMTP id a12mr16164134edu.79.1605878320531; Fri, 20 Nov 2020 05:18:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605878320; cv=none; d=google.com; s=arc-20160816; b=gQ0bm1DgBoj6GofYKiMbbkL8hQEmxu7UlaAmBPzYLxr9RZ8pBzR2ALFWYe/kGtRzrM OUbthrj3ytmBKtuAOOVVAaFx4DIM5wtBBIMVyvoVnmavEfbF8aa7UGKTHPg/wWFSqWUG hx7JNUlhYl3HKT1L6J3S+epzwNM9xESLUXQmSC/eURw3orROgpGnBlNmUnWuEcU7FfMb gAOMcBFvqrRDy+mLb67Fokc8o5zyehdFapKRyOQ+aAWslRhcKmFbMmQDzLtpHYWIjKCR b9HWF0oLQskrNIXmyNBZ+CP+Z8OSbtdwUFYFjmLk4unHs6sup0Fki5/JgIZ8A3v5RSMR d7IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=uNlmvTkJlfUfkP1mNVRlX6byRmlW03DDOuT/fy6Pb1s=; b=SxhUYOt1WKR/GaAUduRnGaHS/IkGbk1oaJFP5V9U9QO4LvYv+MHJrJXFoWIpHfK0KM KlZQJj0zXTDpbbtSWVYoqMwUzA95TfR2SRd3oWT68vljZIWSnbE6GRL7Wc+M4HTll4mO dlpqgJj4YEKWKl1VsxVBJD+KFJGKAQFDgAgc8p1zJgBvN5zm+wroDm1/JkQgqxRFV/+5 NIpkx+NPp4OEVERODascNU+EAxpLhLBOhh5N56ipfZk+IXc4NmRbYegd4JR0SWm1giRi Fl1oGDasM22fVkeNaYq6CAuAT5cVlTlLDaUV3O7sFC46/E04+UuGhEK8gsbJ5YYp/HDQ jqng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metafoo.de header.s=default2002 header.b=ca0XXgVY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=metafoo.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m1si1984753ejj.517.2020.11.20.05.18.16; Fri, 20 Nov 2020 05:18:40 -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=@metafoo.de header.s=default2002 header.b=ca0XXgVY; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=metafoo.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727307AbgKTNQJ (ORCPT + 99 others); Fri, 20 Nov 2020 08:16:09 -0500 Received: from www381.your-server.de ([78.46.137.84]:51368 "EHLO www381.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725890AbgKTNQJ (ORCPT ); Fri, 20 Nov 2020 08:16:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=metafoo.de; s=default2002; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID; bh=uNlmvTkJlfUfkP1mNVRlX6byRmlW03DDOuT/fy6Pb1s=; b=ca0XXgVYissccmdKgaqut10/FO TAtmChNCYpNV89EasjoGQIDrEctV7V6CPclmKyJOWw+zsRFnSr4Au5MONgXwyKSQnat4ipk2TizpY fRarKyOct1Zg7jjRizKZUxEm2VCKviawLgjQfzRd8pPyWCLlVB/Mj7hjPQrBZywYKuxpzMrAvJ/K4 q9acNRrRnuxyKoaj7WbyhejLQUuCN9Mikz3YfPCfb4JA6O4bndMpfjO/Qvbwa2lkI9IYgHJ5yKkE7 gEyBZ8l/xrX2O13LRtPXxr6kRiXcaRblQ/9DBNPbsUw5K1OVFGCZbGXnPRGw/LJDH4ZV3IKfE7fBr 5uVDzPhA==; Received: from sslproxy06.your-server.de ([78.46.172.3]) by www381.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1kg6Gf-0004jy-6g; Fri, 20 Nov 2020 14:16:02 +0100 Received: from [62.216.202.98] (helo=[192.168.178.20]) by sslproxy06.your-server.de with esmtpsa (TLSv1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kg6Ge-0008B9-Ue; Fri, 20 Nov 2020 14:16:01 +0100 Subject: Re: [Cocci] Proposal for a new checkpatch check; matching _set_drvdata() & _get_drvdata() To: Alexandru Ardelean , Julia Lawall Cc: Joe Perches , Andy Shevchenko , Robo Bot , Alexandru Ardelean , LKML , cocci References: From: Lars-Peter Clausen Message-ID: <4af50412-a22f-4ca1-adb0-d732438c6669@metafoo.de> Date: Fri, 20 Nov 2020 14:16:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Authenticated-Sender: lars@metafoo.de X-Virus-Scanned: Clear (ClamAV 0.102.4/25993/Thu Nov 19 14:11:24 2020) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/20/20 12:54 PM, Alexandru Ardelean wrote: > On Fri, Nov 20, 2020 at 12:47 PM Julia Lawall wrote: >> >> >> On Thu, 19 Nov 2020, Joe Perches wrote: >> >>> On Thu, 2020-11-19 at 17:16 +0200, Andy Shevchenko wrote: >>>> On Thu, Nov 19, 2020 at 4:09 PM Alexandru Ardelean >>>> wrote: >>>>> Hey, >>>>> >>>>> So, I stumbled on a new check that could be added to checkpatch. >>>>> Since it's in Perl, I'm reluctant to try it. >>>>> >>>>> Seems many drivers got to a point where they now call (let's say) >>>>> spi_set_drvdata(), but never access that information via >>>>> spi_get_drvdata(). >>>>> Reasons for this seem to be: >>>>> 1. They got converted to device-managed functions and there is no >>>>> longer a remove hook to require the _get_drvdata() access >>>>> 2. They look like they were copied from a driver that had a >>>>> _set_drvdata() and when the code got finalized, the _set_drvdata() was >>>>> omitted >>>>> >>>>> There are a few false positives that I can notice at a quick look, >>>>> like the data being set via some xxx_set_drvdata() and retrieved via a >>>>> dev_get_drvdata(). >>>> I can say quite a few. And this makes a difference. >>>> So, basically all drivers that are using PM callbacks would rather use >>>> dev_get_drvdata() rather than bus specific. >>>> >>>>> I think checkpatch reporting these as well would be acceptable simply >>>>> from a reviewability perspective. >>>>> >>>>> I did a shell script to quickly check these. See below. >>>>> It's pretty badly written but it is enough for me to gather a list. >>>>> And I wrote it in 5 minutes :P >>>>> I initially noticed this in some IIO drivers, and then I suspected >>>>> that this may be more widespread. >>>> It seems more suitable for coccinelle. >>> To me as well. >> To me as well, since it seems to involve nonlocal information. >> >> I'm not sure to understand the original shell script. Is there >> something interesting about pci_set_drvdata? > Ah, it's a stupid script I wrote in 5 minutes, so I did not bother to > make things smart. > In the text-matching I did in shell, there are some entries that come > from comments and docs. > It's only about 3-4 entries, so I just did a visual/manual ignore. > > In essence: > The script searches for all strings that contain _set_drvdata. > The separators are whitespace. > It creates a list of all xxxx_set_drvdata functions. > For each xxxx_set_drvdata function: > It checks all files that have a xxxx_set_drvdata entry, but no > xxxx_get_drvdata > > I piped this output into a file and started manually checking the drivers. > There is one [I forget which function] that is xxxx_set_drvdata() but > equivalent is xxxx_drvdata() > > As Andy said, some precautions must be taken in places where > xxxx_set_drvdata() is called but dev_get_drvdata() is used. > Cases like PM suspend/resume calls. > And there may be some cases outside this context. > Doing something like this with coccinelle is fairly easy. But I'd be very cautious about putting such a script into the kernel. It will result in too many false positive drive-by patches. Such a script will not detect cases such as:  * Driver is split over multiple files. One file does ..._set_drvdata(), another does ..._get_drvdata().  * Framework uses drvdata to exchange data with the driver. E.g driver is expected to call set_drvdata() and then the framework uses get_drvdata() to retrieve the data. This is not a very good pattern, but there are some palces int he kernel where this is used. I believe for example V4L2 uses this. - Lars