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 14:07:57 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifndef _LINUX_PERCPU_COUNTER_H
|
|
|
|
#define _LINUX_PERCPU_COUNTER_H
|
|
|
|
/*
|
|
|
|
* A simple "approximate counter" for use in ext2 and ext3 superblocks.
|
|
|
|
*
|
|
|
|
* WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/smp.h>
|
2007-07-16 06:39:51 +00:00
|
|
|
#include <linux/list.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/threads.h>
|
|
|
|
#include <linux/percpu.h>
|
2006-06-23 09:05:41 +00:00
|
|
|
#include <linux/types.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2022-09-13 19:25:37 +00:00
|
|
|
/* percpu_counter batch for local add or sub */
|
|
|
|
#define PERCPU_COUNTER_LOCAL_BATCH INT_MAX
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
|
|
|
|
struct percpu_counter {
|
2009-07-25 14:21:48 +00:00
|
|
|
raw_spinlock_t lock;
|
2006-06-23 09:05:41 +00:00
|
|
|
s64 count;
|
2007-07-16 06:39:51 +00:00
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
|
|
struct list_head list; /* All percpu_counters are on a list */
|
|
|
|
#endif
|
2010-02-02 05:38:57 +00:00
|
|
|
s32 __percpu *counters;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2009-01-06 22:41:04 +00:00
|
|
|
extern int percpu_counter_batch;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2023-08-23 05:06:08 +00:00
|
|
|
int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount,
|
|
|
|
gfp_t gfp, u32 nr_counters,
|
|
|
|
struct lock_class_key *key);
|
2008-12-26 14:08:55 +00:00
|
|
|
|
2023-08-23 05:06:08 +00:00
|
|
|
#define percpu_counter_init_many(fbc, value, gfp, nr_counters) \
|
2008-12-26 14:08:55 +00:00
|
|
|
({ \
|
|
|
|
static struct lock_class_key __key; \
|
|
|
|
\
|
2023-08-23 05:06:08 +00:00
|
|
|
__percpu_counter_init_many(fbc, value, gfp, nr_counters,\
|
|
|
|
&__key); \
|
2008-12-26 14:08:55 +00:00
|
|
|
})
|
|
|
|
|
2023-08-23 05:06:08 +00:00
|
|
|
|
|
|
|
#define percpu_counter_init(fbc, value, gfp) \
|
|
|
|
percpu_counter_init_many(fbc, value, gfp, 1)
|
|
|
|
|
|
|
|
void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counters);
|
|
|
|
static inline void percpu_counter_destroy(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
percpu_counter_destroy_many(fbc, 1);
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:44 +00:00
|
|
|
void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
|
2017-06-20 18:01:20 +00:00
|
|
|
void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,
|
|
|
|
s32 batch);
|
2008-12-09 21:14:14 +00:00
|
|
|
s64 __percpu_counter_sum(struct percpu_counter *fbc);
|
2015-05-28 21:39:34 +00:00
|
|
|
int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
bool __percpu_counter_limited_add(struct percpu_counter *fbc, s64 limit,
|
|
|
|
s64 amount, s32 batch);
|
2020-08-07 06:23:11 +00:00
|
|
|
void percpu_counter_sync(struct percpu_counter *fbc);
|
2015-05-28 21:39:34 +00:00
|
|
|
|
|
|
|
static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
|
|
|
|
{
|
|
|
|
return __percpu_counter_compare(fbc, rhs, percpu_counter_batch);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-17 06:25:43 +00:00
|
|
|
static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
|
2007-10-17 06:25:43 +00:00
|
|
|
{
|
2017-06-20 18:01:20 +00:00
|
|
|
percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
|
2007-10-17 06:25:43 +00:00
|
|
|
}
|
|
|
|
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
static inline bool
|
|
|
|
percpu_counter_limited_add(struct percpu_counter *fbc, s64 limit, s64 amount)
|
|
|
|
{
|
|
|
|
return __percpu_counter_limited_add(fbc, limit, amount,
|
|
|
|
percpu_counter_batch);
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:25:37 +00:00
|
|
|
/*
|
|
|
|
* With percpu_counter_add_local() and percpu_counter_sub_local(), counts
|
|
|
|
* are accumulated in local per cpu counter and not in fbc->count until
|
|
|
|
* local count overflows PERCPU_COUNTER_LOCAL_BATCH. This makes counter
|
|
|
|
* write efficient.
|
|
|
|
* But percpu_counter_sum(), instead of percpu_counter_read(), needs to be
|
|
|
|
* used to add up the counts from each CPU to account for all the local
|
|
|
|
* counts. So percpu_counter_add_local() and percpu_counter_sub_local()
|
|
|
|
* should be used when a counter is updated frequently and read rarely.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH);
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:45 +00:00
|
|
|
static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
|
|
|
|
{
|
2008-12-09 21:14:14 +00:00
|
|
|
s64 ret = __percpu_counter_sum(fbc);
|
2007-10-17 06:25:45 +00:00
|
|
|
return ret < 0 ? 0 : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
|
|
|
|
{
|
2008-12-09 21:14:14 +00:00
|
|
|
return __percpu_counter_sum(fbc);
|
2007-10-17 06:25:45 +00:00
|
|
|
}
|
|
|
|
|
2006-06-23 09:05:41 +00:00
|
|
|
static inline s64 percpu_counter_read(struct percpu_counter *fbc)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return fbc->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible for the percpu_counter_read() to return a small negative
|
|
|
|
* number for some counter which should never be negative.
|
2006-06-23 09:05:41 +00:00
|
|
|
*
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2006-06-23 09:05:41 +00:00
|
|
|
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2020-04-07 03:10:25 +00:00
|
|
|
/* Prevent reloads of fbc->count */
|
|
|
|
s64 ret = READ_ONCE(fbc->count);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-06-23 09:05:41 +00:00
|
|
|
if (ret >= 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
2011-05-25 00:13:35 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 06:48:32 +00:00
|
|
|
static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
|
2010-10-28 01:30:13 +00:00
|
|
|
{
|
|
|
|
return (fbc->counters != NULL);
|
|
|
|
}
|
|
|
|
|
2013-02-04 22:14:12 +00:00
|
|
|
#else /* !CONFIG_SMP */
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct percpu_counter {
|
2006-06-23 09:05:41 +00:00
|
|
|
s64 count;
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
2023-08-23 05:06:08 +00:00
|
|
|
static inline int percpu_counter_init_many(struct percpu_counter *fbc,
|
|
|
|
s64 amount, gfp_t gfp,
|
|
|
|
u32 nr_counters)
|
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_counters; i++)
|
|
|
|
fbc[i].count = amount;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:51:29 +00:00
|
|
|
static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount,
|
|
|
|
gfp_t gfp)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2023-08-23 05:06:08 +00:00
|
|
|
return percpu_counter_init_many(fbc, amount, gfp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void percpu_counter_destroy_many(struct percpu_counter *fbc,
|
|
|
|
u32 nr_counters)
|
|
|
|
{
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void percpu_counter_destroy(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:44 +00:00
|
|
|
static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
fbc->count = amount;
|
|
|
|
}
|
|
|
|
|
2010-08-10 00:19:04 +00:00
|
|
|
static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
|
|
|
|
{
|
|
|
|
if (fbc->count > rhs)
|
|
|
|
return 1;
|
|
|
|
else if (fbc->count < rhs)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:39:34 +00:00
|
|
|
static inline int
|
|
|
|
__percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
|
|
|
|
{
|
|
|
|
return percpu_counter_compare(fbc, rhs);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static inline void
|
2007-10-17 06:25:43 +00:00
|
|
|
percpu_counter_add(struct percpu_counter *fbc, s64 amount)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2022-12-16 15:04:40 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
fbc->count += amount;
|
2022-12-16 15:04:40 +00:00
|
|
|
local_irq_restore(flags);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
static inline bool
|
|
|
|
percpu_counter_limited_add(struct percpu_counter *fbc, s64 limit, s64 amount)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2023-10-12 04:40:09 +00:00
|
|
|
bool good = false;
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
s64 count;
|
|
|
|
|
2023-10-12 04:40:09 +00:00
|
|
|
if (amount == 0)
|
|
|
|
return true;
|
|
|
|
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
local_irq_save(flags);
|
|
|
|
count = fbc->count + amount;
|
2023-10-12 04:40:09 +00:00
|
|
|
if ((amount > 0 && count <= limit) ||
|
|
|
|
(amount < 0 && count >= limit)) {
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
fbc->count = count;
|
2023-10-12 04:40:09 +00:00
|
|
|
good = true;
|
|
|
|
}
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
local_irq_restore(flags);
|
2023-10-12 04:40:09 +00:00
|
|
|
return good;
|
shmem,percpu_counter: add _limited_add(fbc, limit, amount)
Percpu counter's compare and add are separate functions: without locking
around them (which would defeat their purpose), it has been possible to
overflow the intended limit. Imagine all the other CPUs fallocating tmpfs
huge pages to the limit, in between this CPU's compare and its add.
I have not seen reports of that happening; but tmpfs's recent addition of
dquot_alloc_block_nodirty() in between the compare and the add makes it
even more likely, and I'd be uncomfortable to leave it unfixed.
Introduce percpu_counter_limited_add(fbc, limit, amount) to prevent it.
I believe this implementation is correct, and slightly more efficient than
the combination of compare and add (taking the lock once rather than twice
when nearing full - the last 128MiB of a tmpfs volume on a machine with
128 CPUs and 4KiB pages); but it does beg for a better design - when
nearing full, there is no new batching, but the costly percpu counter sum
across CPUs still has to be done, while locked.
Follow __percpu_counter_sum()'s example, including cpu_dying_mask as well
as cpu_online_mask: but shouldn't __percpu_counter_compare() and
__percpu_counter_limited_add() then be adding a num_dying_cpus() to
num_online_cpus(), when they calculate the maximum which could be held
across CPUs? But the times when it matters would be vanishingly rare.
Link: https://lkml.kernel.org/r/bb817848-2d19-bcc8-39ca-ea179af0f0b4@google.com
Signed-off-by: Hugh Dickins <hughd@google.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Tim Chen <tim.c.chen@intel.com>
Cc: Dave Chinner <dchinner@redhat.com>
Cc: Darrick J. Wong <djwong@kernel.org>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Carlos Maiolino <cem@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2023-09-30 03:42:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 19:25:37 +00:00
|
|
|
/* non-SMP percpu_counter_add_local is the same with percpu_counter_add */
|
|
|
|
static inline void
|
|
|
|
percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
percpu_counter_add(fbc, amount);
|
|
|
|
}
|
|
|
|
|
2010-02-02 22:46:10 +00:00
|
|
|
static inline void
|
2017-06-20 18:01:20 +00:00
|
|
|
percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
|
2010-02-02 22:46:10 +00:00
|
|
|
{
|
|
|
|
percpu_counter_add(fbc, amount);
|
|
|
|
}
|
|
|
|
|
2006-06-23 09:05:41 +00:00
|
|
|
static inline s64 percpu_counter_read(struct percpu_counter *fbc)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return fbc->count;
|
|
|
|
}
|
|
|
|
|
2011-05-25 00:13:35 +00:00
|
|
|
/*
|
|
|
|
* percpu_counter is intended to track positive numbers. In the UP case the
|
|
|
|
* number should never be negative.
|
|
|
|
*/
|
2006-06-23 09:05:41 +00:00
|
|
|
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
return fbc->count;
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:44 +00:00
|
|
|
static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
|
2006-03-08 05:55:31 +00:00
|
|
|
{
|
|
|
|
return percpu_counter_read_positive(fbc);
|
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:45 +00:00
|
|
|
static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
return percpu_counter_read(fbc);
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:48:32 +00:00
|
|
|
static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
|
2010-10-28 01:30:13 +00:00
|
|
|
{
|
2017-12-15 06:48:32 +00:00
|
|
|
return true;
|
2010-10-28 01:30:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 06:23:11 +00:00
|
|
|
static inline void percpu_counter_sync(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
|
|
|
static inline void percpu_counter_inc(struct percpu_counter *fbc)
|
|
|
|
{
|
2007-10-17 06:25:42 +00:00
|
|
|
percpu_counter_add(fbc, 1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void percpu_counter_dec(struct percpu_counter *fbc)
|
|
|
|
{
|
2007-10-17 06:25:42 +00:00
|
|
|
percpu_counter_add(fbc, -1);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 06:25:42 +00:00
|
|
|
static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
percpu_counter_add(fbc, -amount);
|
|
|
|
}
|
|
|
|
|
2022-09-13 19:25:37 +00:00
|
|
|
static inline void
|
|
|
|
percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
percpu_counter_add_local(fbc, -amount);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#endif /* _LINUX_PERCPU_COUNTER_H */
|