License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
// SPDX-License-Identifier: GPL-2.0
2009-06-18 03:28:08 +04:00
/*
* This code exports profiling data as debugfs files to userspace .
*
* Copyright IBM Corp . 2009
* Author ( s ) : Peter Oberparleiter < oberpar @ linux . vnet . ibm . com >
*
* Uses gcc - internal data definitions .
* Based on the gcov - kernel patch by :
* Hubertus Franke < frankeh @ us . ibm . com >
* Nigel Hinds < nhinds @ us . ibm . com >
* Rajan Ravindran < rajancr @ us . ibm . com >
* Peter Oberparleiter < oberpar @ linux . vnet . ibm . com >
* Paul Larson
* Yi CDL Yang
*/
# define pr_fmt(fmt) "gcov: " fmt
# include <linux/init.h>
# include <linux/module.h>
# include <linux/debugfs.h>
# include <linux/fs.h>
# include <linux/list.h>
# include <linux/string.h>
# include <linux/slab.h>
# include <linux/mutex.h>
# include <linux/seq_file.h>
# include "gcov.h"
/**
* struct gcov_node - represents a debugfs entry
* @ list : list head for child node list
* @ children : child nodes
* @ all : list head for list of all nodes
* @ parent : parent node
2010-09-10 03:37:35 +04:00
* @ loaded_info : array of pointers to profiling data sets for loaded object
* files .
* @ num_loaded : number of profiling data sets for loaded object files .
* @ unloaded_info : accumulated copy of profiling data sets for unloaded
* object files . Used only when gcov_persist = 1.
2009-06-18 03:28:08 +04:00
* @ dentry : main debugfs entry , either a directory or data file
* @ links : associated symbolic links
* @ name : data file basename
*
* struct gcov_node represents an entity within the gcov / subdirectory
* of debugfs . There are directory and data file nodes . The latter represent
* the actual synthesized data file plus any associated symbolic links which
* are needed by the gcov tool to work correctly .
*/
struct gcov_node {
struct list_head list ;
struct list_head children ;
struct list_head all ;
struct gcov_node * parent ;
2010-09-10 03:37:35 +04:00
struct gcov_info * * loaded_info ;
struct gcov_info * unloaded_info ;
2009-06-18 03:28:08 +04:00
struct dentry * dentry ;
struct dentry * * links ;
2010-09-10 03:37:35 +04:00
int num_loaded ;
2009-06-18 03:28:08 +04:00
char name [ 0 ] ;
} ;
static const char objtree [ ] = OBJTREE ;
static const char srctree [ ] = SRCTREE ;
static struct gcov_node root_node ;
static struct dentry * reset_dentry ;
static LIST_HEAD ( all_head ) ;
static DEFINE_MUTEX ( node_lock ) ;
/* If non-zero, keep copies of profiling data for unloaded modules. */
static int gcov_persist = 1 ;
static int __init gcov_persist_setup ( char * str )
{
unsigned long val ;
2013-09-13 02:14:07 +04:00
if ( kstrtoul ( str , 0 , & val ) ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " invalid gcov_persist parameter '%s' \n " , str ) ;
2009-06-18 03:28:08 +04:00
return 0 ;
}
gcov_persist = val ;
pr_info ( " setting gcov_persist to %d \n " , gcov_persist ) ;
return 1 ;
}
__setup ( " gcov_persist= " , gcov_persist_setup ) ;
/*
* seq_file . start ( ) implementation for gcov data files . Note that the
* gcov_iterator interface is designed to be more restrictive than seq_file
* ( no start from arbitrary position , etc . ) , to simplify the iterator
* implementation .
*/
static void * gcov_seq_start ( struct seq_file * seq , loff_t * pos )
{
loff_t i ;
gcov_iter_start ( seq - > private ) ;
for ( i = 0 ; i < * pos ; i + + ) {
if ( gcov_iter_next ( seq - > private ) )
return NULL ;
}
return seq - > private ;
}
/* seq_file.next() implementation for gcov data files. */
static void * gcov_seq_next ( struct seq_file * seq , void * data , loff_t * pos )
{
struct gcov_iterator * iter = data ;
if ( gcov_iter_next ( iter ) )
return NULL ;
( * pos ) + + ;
return iter ;
}
/* seq_file.show() implementation for gcov data files. */
static int gcov_seq_show ( struct seq_file * seq , void * data )
{
struct gcov_iterator * iter = data ;
if ( gcov_iter_write ( iter , seq ) )
return - EINVAL ;
return 0 ;
}
static void gcov_seq_stop ( struct seq_file * seq , void * data )
{
/* Unused. */
}
static const struct seq_operations gcov_seq_ops = {
. start = gcov_seq_start ,
. next = gcov_seq_next ,
. show = gcov_seq_show ,
. stop = gcov_seq_stop ,
} ;
/*
2010-09-10 03:37:35 +04:00
* Return a profiling data set associated with the given node . This is
* either a data set for a loaded object file or a data set copy in case
* all associated object files have been unloaded .
2009-06-18 03:28:08 +04:00
*/
static struct gcov_info * get_node_info ( struct gcov_node * node )
{
2010-09-10 03:37:35 +04:00
if ( node - > num_loaded > 0 )
return node - > loaded_info [ 0 ] ;
2009-06-18 03:28:08 +04:00
2010-09-10 03:37:35 +04:00
return node - > unloaded_info ;
}
/*
* Return a newly allocated profiling data set which contains the sum of
* all profiling data associated with the given node .
*/
static struct gcov_info * get_accumulated_info ( struct gcov_node * node )
{
struct gcov_info * info ;
int i = 0 ;
if ( node - > unloaded_info )
info = gcov_info_dup ( node - > unloaded_info ) ;
else
info = gcov_info_dup ( node - > loaded_info [ i + + ] ) ;
if ( ! info )
return NULL ;
for ( ; i < node - > num_loaded ; i + + )
gcov_info_add ( info , node - > loaded_info [ i ] ) ;
return info ;
2009-06-18 03:28:08 +04:00
}
/*
* open ( ) implementation for gcov data files . Create a copy of the profiling
* data set and initialize the iterator and seq_file interface .
*/
static int gcov_seq_open ( struct inode * inode , struct file * file )
{
struct gcov_node * node = inode - > i_private ;
struct gcov_iterator * iter ;
struct seq_file * seq ;
struct gcov_info * info ;
int rc = - ENOMEM ;
mutex_lock ( & node_lock ) ;
/*
* Read from a profiling data copy to minimize reference tracking
2010-09-10 03:37:35 +04:00
* complexity and concurrent access and to keep accumulating multiple
* profiling data sets associated with one node simple .
2009-06-18 03:28:08 +04:00
*/
2010-09-10 03:37:35 +04:00
info = get_accumulated_info ( node ) ;
2009-06-18 03:28:08 +04:00
if ( ! info )
goto out_unlock ;
iter = gcov_iter_new ( info ) ;
if ( ! iter )
goto err_free_info ;
rc = seq_open ( file , & gcov_seq_ops ) ;
if ( rc )
goto err_free_iter_info ;
seq = file - > private_data ;
seq - > private = iter ;
out_unlock :
mutex_unlock ( & node_lock ) ;
return rc ;
err_free_iter_info :
gcov_iter_free ( iter ) ;
err_free_info :
gcov_info_free ( info ) ;
goto out_unlock ;
}
/*
* release ( ) implementation for gcov data files . Release resources allocated
* by open ( ) .
*/
static int gcov_seq_release ( struct inode * inode , struct file * file )
{
struct gcov_iterator * iter ;
struct gcov_info * info ;
struct seq_file * seq ;
seq = file - > private_data ;
iter = seq - > private ;
info = gcov_iter_get_info ( iter ) ;
gcov_iter_free ( iter ) ;
gcov_info_free ( info ) ;
seq_release ( inode , file ) ;
return 0 ;
}
/*
* Find a node by the associated data file name . Needs to be called with
* node_lock held .
*/
static struct gcov_node * get_node_by_name ( const char * name )
{
struct gcov_node * node ;
struct gcov_info * info ;
list_for_each_entry ( node , & all_head , all ) {
info = get_node_info ( node ) ;
2013-11-13 03:11:24 +04:00
if ( info & & ( strcmp ( gcov_info_filename ( info ) , name ) = = 0 ) )
2009-06-18 03:28:08 +04:00
return node ;
}
return NULL ;
}
2010-09-10 03:37:35 +04:00
/*
* Reset all profiling data associated with the specified node .
*/
static void reset_node ( struct gcov_node * node )
{
int i ;
if ( node - > unloaded_info )
gcov_info_reset ( node - > unloaded_info ) ;
for ( i = 0 ; i < node - > num_loaded ; i + + )
gcov_info_reset ( node - > loaded_info [ i ] ) ;
}
2009-06-18 03:28:08 +04:00
static void remove_node ( struct gcov_node * node ) ;
/*
* write ( ) implementation for gcov data files . Reset profiling data for the
2010-09-10 03:37:35 +04:00
* corresponding file . If all associated object files have been unloaded ,
* remove the debug fs node as well .
2009-06-18 03:28:08 +04:00
*/
static ssize_t gcov_seq_write ( struct file * file , const char __user * addr ,
size_t len , loff_t * pos )
{
struct seq_file * seq ;
struct gcov_info * info ;
struct gcov_node * node ;
seq = file - > private_data ;
info = gcov_iter_get_info ( seq - > private ) ;
mutex_lock ( & node_lock ) ;
2013-11-13 03:11:24 +04:00
node = get_node_by_name ( gcov_info_filename ( info ) ) ;
2009-06-18 03:28:08 +04:00
if ( node ) {
/* Reset counts or remove node for unloaded modules. */
2010-09-10 03:37:35 +04:00
if ( node - > num_loaded = = 0 )
2009-06-18 03:28:08 +04:00
remove_node ( node ) ;
else
2010-09-10 03:37:35 +04:00
reset_node ( node ) ;
2009-06-18 03:28:08 +04:00
}
/* Reset counts for open file. */
gcov_info_reset ( info ) ;
mutex_unlock ( & node_lock ) ;
return len ;
}
/*
* Given a string < path > representing a file path of format :
* path / to / file . gcda
* construct and return a new string :
* < dir / > path / to / file . < ext >
*/
static char * link_target ( const char * dir , const char * path , const char * ext )
{
char * target ;
char * old_ext ;
char * copy ;
copy = kstrdup ( path , GFP_KERNEL ) ;
if ( ! copy )
return NULL ;
old_ext = strrchr ( copy , ' . ' ) ;
if ( old_ext )
* old_ext = ' \0 ' ;
if ( dir )
target = kasprintf ( GFP_KERNEL , " %s/%s.%s " , dir , copy , ext ) ;
else
target = kasprintf ( GFP_KERNEL , " %s.%s " , copy , ext ) ;
kfree ( copy ) ;
return target ;
}
/*
* Construct a string representing the symbolic link target for the given
* gcov data file name and link type . Depending on the link type and the
* location of the data file , the link target can either point to a
* subdirectory of srctree , objtree or in an external location .
*/
static char * get_link_target ( const char * filename , const struct gcov_link * ext )
{
const char * rel ;
char * result ;
if ( strncmp ( filename , objtree , strlen ( objtree ) ) = = 0 ) {
rel = filename + strlen ( objtree ) + 1 ;
if ( ext - > dir = = SRC_TREE )
result = link_target ( srctree , rel , ext - > ext ) ;
else
result = link_target ( objtree , rel , ext - > ext ) ;
} else {
/* External compilation. */
result = link_target ( NULL , filename , ext - > ext ) ;
}
return result ;
}
# define SKEW_PREFIX ".tmp_"
/*
* For a filename . tmp_filename . ext return filename . ext . Needed to compensate
* for filename skewing caused by the mod - versioning mechanism .
*/
static const char * deskew ( const char * basename )
{
if ( strncmp ( basename , SKEW_PREFIX , sizeof ( SKEW_PREFIX ) - 1 ) = = 0 )
return basename + sizeof ( SKEW_PREFIX ) - 1 ;
return basename ;
}
/*
* Create links to additional files ( usually . c and . gcno files ) which the
* gcov tool expects to find in the same directory as the gcov data file .
*/
static void add_links ( struct gcov_node * node , struct dentry * parent )
{
2013-11-13 03:11:31 +04:00
const char * basename ;
2009-06-18 03:28:08 +04:00
char * target ;
int num ;
int i ;
for ( num = 0 ; gcov_link [ num ] . ext ; num + + )
/* Nothing. */ ;
node - > links = kcalloc ( num , sizeof ( struct dentry * ) , GFP_KERNEL ) ;
if ( ! node - > links )
return ;
for ( i = 0 ; i < num ; i + + ) {
2013-11-13 03:11:24 +04:00
target = get_link_target (
gcov_info_filename ( get_node_info ( node ) ) ,
& gcov_link [ i ] ) ;
2009-06-18 03:28:08 +04:00
if ( ! target )
goto out_err ;
2013-11-13 03:11:31 +04:00
basename = kbasename ( target ) ;
if ( basename = = target )
2009-06-18 03:28:08 +04:00
goto out_err ;
node - > links [ i ] = debugfs_create_symlink ( deskew ( basename ) ,
parent , target ) ;
if ( ! node - > links [ i ] )
goto out_err ;
kfree ( target ) ;
}
return ;
out_err :
kfree ( target ) ;
while ( i - - > 0 )
debugfs_remove ( node - > links [ i ] ) ;
kfree ( node - > links ) ;
node - > links = NULL ;
}
static const struct file_operations gcov_data_fops = {
. open = gcov_seq_open ,
. release = gcov_seq_release ,
. read = seq_read ,
. llseek = seq_lseek ,
. write = gcov_seq_write ,
} ;
/* Basic initialization of a new node. */
static void init_node ( struct gcov_node * node , struct gcov_info * info ,
const char * name , struct gcov_node * parent )
{
INIT_LIST_HEAD ( & node - > list ) ;
INIT_LIST_HEAD ( & node - > children ) ;
INIT_LIST_HEAD ( & node - > all ) ;
2010-09-10 03:37:35 +04:00
if ( node - > loaded_info ) {
node - > loaded_info [ 0 ] = info ;
node - > num_loaded = 1 ;
}
2009-06-18 03:28:08 +04:00
node - > parent = parent ;
if ( name )
strcpy ( node - > name , name ) ;
}
/*
* Create a new node and associated debugfs entry . Needs to be called with
* node_lock held .
*/
static struct gcov_node * new_node ( struct gcov_node * parent ,
struct gcov_info * info , const char * name )
{
struct gcov_node * node ;
node = kzalloc ( sizeof ( struct gcov_node ) + strlen ( name ) + 1 , GFP_KERNEL ) ;
2010-09-10 03:37:35 +04:00
if ( ! node )
goto err_nomem ;
if ( info ) {
node - > loaded_info = kcalloc ( 1 , sizeof ( struct gcov_info * ) ,
GFP_KERNEL ) ;
if ( ! node - > loaded_info )
goto err_nomem ;
2009-06-18 03:28:08 +04:00
}
init_node ( node , info , name , parent ) ;
/* Differentiate between gcov data file nodes and directory nodes. */
if ( info ) {
node - > dentry = debugfs_create_file ( deskew ( node - > name ) , 0600 ,
parent - > dentry , node , & gcov_data_fops ) ;
} else
node - > dentry = debugfs_create_dir ( node - > name , parent - > dentry ) ;
if ( ! node - > dentry ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not create file \n " ) ;
2009-06-18 03:28:08 +04:00
kfree ( node ) ;
return NULL ;
}
if ( info )
add_links ( node , parent - > dentry ) ;
list_add ( & node - > list , & parent - > children ) ;
list_add ( & node - > all , & all_head ) ;
return node ;
2010-09-10 03:37:35 +04:00
err_nomem :
kfree ( node ) ;
2013-11-13 03:11:29 +04:00
pr_warn ( " out of memory \n " ) ;
2010-09-10 03:37:35 +04:00
return NULL ;
2009-06-18 03:28:08 +04:00
}
/* Remove symbolic links associated with node. */
static void remove_links ( struct gcov_node * node )
{
int i ;
if ( ! node - > links )
return ;
for ( i = 0 ; gcov_link [ i ] . ext ; i + + )
debugfs_remove ( node - > links [ i ] ) ;
kfree ( node - > links ) ;
node - > links = NULL ;
}
/*
* Remove node from all lists and debugfs and release associated resources .
* Needs to be called with node_lock held .
*/
static void release_node ( struct gcov_node * node )
{
list_del ( & node - > list ) ;
list_del ( & node - > all ) ;
debugfs_remove ( node - > dentry ) ;
remove_links ( node ) ;
2010-09-10 03:37:35 +04:00
kfree ( node - > loaded_info ) ;
if ( node - > unloaded_info )
gcov_info_free ( node - > unloaded_info ) ;
2009-06-18 03:28:08 +04:00
kfree ( node ) ;
}
/* Release node and empty parents. Needs to be called with node_lock held. */
static void remove_node ( struct gcov_node * node )
{
struct gcov_node * parent ;
while ( ( node ! = & root_node ) & & list_empty ( & node - > children ) ) {
parent = node - > parent ;
release_node ( node ) ;
node = parent ;
}
}
/*
* Find child node with given basename . Needs to be called with node_lock
* held .
*/
static struct gcov_node * get_child_by_name ( struct gcov_node * parent ,
const char * name )
{
struct gcov_node * node ;
list_for_each_entry ( node , & parent - > children , list ) {
if ( strcmp ( node - > name , name ) = = 0 )
return node ;
}
return NULL ;
}
/*
* write ( ) implementation for reset file . Reset all profiling data to zero
2010-09-10 03:37:35 +04:00
* and remove nodes for which all associated object files are unloaded .
2009-06-18 03:28:08 +04:00
*/
static ssize_t reset_write ( struct file * file , const char __user * addr ,
size_t len , loff_t * pos )
{
struct gcov_node * node ;
mutex_lock ( & node_lock ) ;
restart :
list_for_each_entry ( node , & all_head , all ) {
2010-09-10 03:37:35 +04:00
if ( node - > num_loaded > 0 )
reset_node ( node ) ;
2009-06-18 03:28:08 +04:00
else if ( list_empty ( & node - > children ) ) {
remove_node ( node ) ;
/* Several nodes may have gone - restart loop. */
goto restart ;
}
}
mutex_unlock ( & node_lock ) ;
return len ;
}
/* read() implementation for reset file. Unused. */
static ssize_t reset_read ( struct file * file , char __user * addr , size_t len ,
loff_t * pos )
{
/* Allow read operation so that a recursive copy won't fail. */
return 0 ;
}
static const struct file_operations gcov_reset_fops = {
. write = reset_write ,
. read = reset_read ,
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
. llseek = noop_llseek ,
2009-06-18 03:28:08 +04:00
} ;
/*
* Create a node for a given profiling data set and add it to all lists and
* debugfs . Needs to be called with node_lock held .
*/
static void add_node ( struct gcov_info * info )
{
char * filename ;
char * curr ;
char * next ;
struct gcov_node * parent ;
struct gcov_node * node ;
2013-11-13 03:11:24 +04:00
filename = kstrdup ( gcov_info_filename ( info ) , GFP_KERNEL ) ;
2009-06-18 03:28:08 +04:00
if ( ! filename )
return ;
parent = & root_node ;
/* Create directory nodes along the path. */
for ( curr = filename ; ( next = strchr ( curr , ' / ' ) ) ; curr = next + 1 ) {
if ( curr = = next )
continue ;
* next = 0 ;
if ( strcmp ( curr , " . " ) = = 0 )
continue ;
if ( strcmp ( curr , " .. " ) = = 0 ) {
if ( ! parent - > parent )
goto err_remove ;
parent = parent - > parent ;
continue ;
}
node = get_child_by_name ( parent , curr ) ;
if ( ! node ) {
node = new_node ( parent , NULL , curr ) ;
if ( ! node )
goto err_remove ;
}
parent = node ;
}
/* Create file node. */
node = new_node ( parent , info , curr ) ;
if ( ! node )
goto err_remove ;
out :
kfree ( filename ) ;
return ;
err_remove :
remove_node ( parent ) ;
goto out ;
}
/*
2010-09-10 03:37:35 +04:00
* Associate a profiling data set with an existing node . Needs to be called
* with node_lock held .
2009-06-18 03:28:08 +04:00
*/
2010-09-10 03:37:35 +04:00
static void add_info ( struct gcov_node * node , struct gcov_info * info )
2009-06-18 03:28:08 +04:00
{
2010-09-10 03:37:35 +04:00
struct gcov_info * * loaded_info ;
int num = node - > num_loaded ;
/*
* Prepare new array . This is done first to simplify cleanup in
* case the new data set is incompatible , the node only contains
* unloaded data sets and there ' s not enough memory for the array .
*/
loaded_info = kcalloc ( num + 1 , sizeof ( struct gcov_info * ) , GFP_KERNEL ) ;
if ( ! loaded_info ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not add '%s' (out of memory) \n " ,
gcov_info_filename ( info ) ) ;
2010-09-10 03:37:35 +04:00
return ;
}
memcpy ( loaded_info , node - > loaded_info ,
num * sizeof ( struct gcov_info * ) ) ;
loaded_info [ num ] = info ;
/* Check if the new data set is compatible. */
if ( num = = 0 ) {
/*
* A module was unloaded , modified and reloaded . The new
* data set replaces the copy of the last one .
*/
if ( ! gcov_info_is_compatible ( node - > unloaded_info , info ) ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " discarding saved data for %s "
" (incompatible version) \n " ,
gcov_info_filename ( info ) ) ;
2010-09-10 03:37:35 +04:00
gcov_info_free ( node - > unloaded_info ) ;
node - > unloaded_info = NULL ;
}
} else {
/*
* Two different versions of the same object file are loaded .
* The initial one takes precedence .
*/
if ( ! gcov_info_is_compatible ( node - > loaded_info [ 0 ] , info ) ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not add '%s' (incompatible "
" version) \n " , gcov_info_filename ( info ) ) ;
2010-09-10 03:37:35 +04:00
kfree ( loaded_info ) ;
return ;
}
2009-06-18 03:28:08 +04:00
}
2010-09-10 03:37:35 +04:00
/* Overwrite previous array. */
kfree ( node - > loaded_info ) ;
node - > loaded_info = loaded_info ;
node - > num_loaded = num + 1 ;
}
2009-06-18 03:28:08 +04:00
2010-09-10 03:37:35 +04:00
/*
* Return the index of a profiling data set associated with a node .
*/
static int get_info_index ( struct gcov_node * node , struct gcov_info * info )
{
int i ;
for ( i = 0 ; i < node - > num_loaded ; i + + ) {
if ( node - > loaded_info [ i ] = = info )
return i ;
}
return - ENOENT ;
2009-06-18 03:28:08 +04:00
}
/*
2010-09-10 03:37:35 +04:00
* Save the data of a profiling data set which is being unloaded .
2009-06-18 03:28:08 +04:00
*/
2010-09-10 03:37:35 +04:00
static void save_info ( struct gcov_node * node , struct gcov_info * info )
2009-06-18 03:28:08 +04:00
{
2010-09-10 03:37:35 +04:00
if ( node - > unloaded_info )
gcov_info_add ( node - > unloaded_info , info ) ;
2009-06-18 03:28:08 +04:00
else {
2010-09-10 03:37:35 +04:00
node - > unloaded_info = gcov_info_dup ( info ) ;
if ( ! node - > unloaded_info ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not save data for '%s' "
" (out of memory) \n " ,
gcov_info_filename ( info ) ) ;
2010-09-10 03:37:35 +04:00
}
}
}
/*
* Disassociate a profiling data set from a node . Needs to be called with
* node_lock held .
*/
static void remove_info ( struct gcov_node * node , struct gcov_info * info )
{
int i ;
i = get_info_index ( node , info ) ;
if ( i < 0 ) {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not remove '%s' (not found) \n " ,
gcov_info_filename ( info ) ) ;
2010-09-10 03:37:35 +04:00
return ;
2009-06-18 03:28:08 +04:00
}
2010-09-10 03:37:35 +04:00
if ( gcov_persist )
save_info ( node , info ) ;
/* Shrink array. */
node - > loaded_info [ i ] = node - > loaded_info [ node - > num_loaded - 1 ] ;
node - > num_loaded - - ;
if ( node - > num_loaded > 0 )
return ;
/* Last loaded data set was removed. */
kfree ( node - > loaded_info ) ;
node - > loaded_info = NULL ;
node - > num_loaded = 0 ;
if ( ! node - > unloaded_info )
remove_node ( node ) ;
2009-06-18 03:28:08 +04:00
}
/*
* Callback to create / remove profiling files when code compiled with
* - fprofile - arcs is loaded / unloaded .
*/
void gcov_event ( enum gcov_action action , struct gcov_info * info )
{
struct gcov_node * node ;
mutex_lock ( & node_lock ) ;
2013-11-13 03:11:24 +04:00
node = get_node_by_name ( gcov_info_filename ( info ) ) ;
2009-06-18 03:28:08 +04:00
switch ( action ) {
case GCOV_ADD :
2010-09-10 03:37:35 +04:00
if ( node )
add_info ( node , info ) ;
else
2009-06-18 03:28:08 +04:00
add_node ( info ) ;
break ;
case GCOV_REMOVE :
2010-09-10 03:37:35 +04:00
if ( node )
remove_info ( node , info ) ;
else {
2013-11-13 03:11:29 +04:00
pr_warn ( " could not remove '%s' (not found) \n " ,
gcov_info_filename ( info ) ) ;
2009-06-18 03:28:08 +04:00
}
break ;
}
mutex_unlock ( & node_lock ) ;
}
/* Create debugfs entries. */
static __init int gcov_fs_init ( void )
{
int rc = - EIO ;
init_node ( & root_node , NULL , NULL , NULL ) ;
/*
* / sys / kernel / debug / gcov will be parent for the reset control file
* and all profiling files .
*/
root_node . dentry = debugfs_create_dir ( " gcov " , NULL ) ;
if ( ! root_node . dentry )
goto err_remove ;
/*
* Create reset file which resets all profiling counts when written
* to .
*/
reset_dentry = debugfs_create_file ( " reset " , 0600 , root_node . dentry ,
NULL , & gcov_reset_fops ) ;
if ( ! reset_dentry )
goto err_remove ;
/* Replay previous events to get our fs hierarchy up-to-date. */
gcov_enable_events ( ) ;
return 0 ;
err_remove :
pr_err ( " init failed \n " ) ;
2014-08-09 01:22:20 +04:00
debugfs_remove ( root_node . dentry ) ;
2009-06-18 03:28:08 +04:00
return rc ;
}
device_initcall ( gcov_fs_init ) ;