Skip to contentSkip to conversation

incoming

PATCH In review Andrew Morton 6 participants 106 messages Dec 16, 2020 – Dec 17, 2020

Subsystems (55)
lore
Share & tools
lore

Andrew Morton

author maintainer
#0 lore raw

- lots of little subsystems

- a few post-linux-next MM material. Most of this awaits more merging of other trees.

95 patches, based on 489e9fea66f31086f85d9a18e61e4791d94a56a4.

Subsystems affected by this patch series:

mm/swap
mm/memory-hotplug
alpha
procfs
misc
core-kernel
bitmap
lib
lz4
bitops
checkpatch
nilfs
kdump
rapidio
gcov
bfs
relay
resource
ubsan
reboot
fault-injection
lzo
apparmor
mm/pagemap
mm/cleanups
mm/gup

Subsystem: mm/swap

Zhaoyang Huang <[email protected]>:
mm: fix a race on nr_swap_pages

Subsystem: mm/memory-hotplug

Laurent Dufour <[email protected]>:
mm/memory_hotplug: quieting offline operation

Subsystem: alpha

Thomas Gleixner <[email protected]>:
alpha: replace bogus in_interrupt()

Subsystem: procfs

Randy Dunlap <[email protected]>:
procfs: delete duplicated words + other fixes

Anand K Mistry <[email protected]>:
proc: provide details on indirect branch speculation

Alexey Dobriyan <[email protected]>:
proc: fix lookup in /proc/net subdirectories after setns(2)

Hui Su <[email protected]>:
fs/proc: make pde_get() return nothing

Subsystem: misc

Christophe Leroy <[email protected]>:
asm-generic: force inlining of get_order() to work around gcc10 poor decision

Andy Shevchenko <[email protected]>:
kernel.h: split out mathematical helpers

Subsystem: core-kernel

Hui Su <[email protected]>:
kernel/acct.c: use #elif instead of #end and #elif

Subsystem: bitmap

Andy Shevchenko <[email protected]>:
include/linux/bitmap.h: convert bitmap_empty() / bitmap_full() to return boolean

"Ma, Jianpeng" <[email protected]>:
bitmap: remove unused function declaration

Subsystem: lib

Geert Uytterhoeven <[email protected]>:
lib/test_free_pages.c: add basic progress indicators

"Gustavo A. R. Silva" <[email protected]>:
Patch series "] lib/stackdepot.c: Replace one-element array with flexible-array member":
lib/stackdepot.c: replace one-element array with flexible-array member lib/stackdepot.c: use flex_array_size() helper in memcpy() lib/stackdepot.c: use array_size() helper in jhash2()

Sebastian Andrzej Siewior <[email protected]>:
lib/test_lockup.c: minimum fix to get it compiled on PREEMPT_RT

Andy Shevchenko <[email protected]>:
lib/list_kunit: follow new file name convention for KUnit tests lib/linear_ranges_kunit: follow new file name convention for KUnit tests lib/bits_kunit: follow new file name convention for KUnit tests lib/cmdline: fix get_option() for strings starting with hyphen lib/cmdline: allow NULL to be an output for get_option() lib/cmdline_kunit: add a new test suite for cmdline API

Jakub Jelinek <[email protected]>:
ilog2: improve ilog2 for constant arguments

Nick Desaulniers <[email protected]>:
lib/string: remove unnecessary #undefs

Daniel Axtens <[email protected]>:
Patch series "Fortify strscpy()", v7:
lib: string.h: detect intra-object overflow in fortified string functions
lkdtm: tests for FORTIFY_SOURCE

Francis Laniel <[email protected]>:
string.h: add FORTIFY coverage for strscpy() drivers/misc/lkdtm: add new file in LKDTM to test fortified strscpy drivers/misc/lkdtm/lkdtm.h: correct wrong filenames in comment

Alexey Dobriyan <[email protected]>:
lib: cleanup kstrto*() usage

Subsystem: lz4

Gao Xiang <[email protected]>:
lib/lz4: explicitly support in-place decompression

Subsystem: bitops

Syed Nayyar Waris <[email protected]>:
Patch series "Introduce the for_each_set_clump macro", v12:
bitops: introduce the for_each_set_clump macro lib/test_bitmap.c: add for_each_set_clump test cases
gpio: thunderx: utilize for_each_set_clump macro
gpio: xilinx: utilize generic bitmap_get_value and _set_value

Subsystem: checkpatch

Dwaipayan Ray <[email protected]>:
checkpatch: add new exception to repeated word check

Aditya Srivastava <[email protected]>:
checkpatch: fix false positives in REPEATED_WORD warning

Łukasz Stelmach <[email protected]>:
checkpatch: ignore generated CamelCase defines and enum values

Joe Perches <[email protected]>:
checkpatch: prefer static const declarations
checkpatch: allow --fix removal of unnecessary break statements

Dwaipayan Ray <[email protected]>:
checkpatch: extend attributes check to handle more patterns

Tom Rix <[email protected]>:
checkpatch: add a fixer for missing newline at eof

Joe Perches <[email protected]>:
checkpatch: update __attribute__((section("name"))) quote removal

Aditya Srivastava <[email protected]>:
checkpatch: add fix option for GERRIT_CHANGE_ID

Joe Perches <[email protected]>:
checkpatch: add __alias and __weak to suggested __attribute__ conversions

Dwaipayan Ray <[email protected]>:
checkpatch: improve email parsing
checkpatch: fix spelling errors and remove repeated word

Aditya Srivastava <[email protected]>:
checkpatch: avoid COMMIT_LOG_LONG_LINE warning for signature tags

Dwaipayan Ray <[email protected]>:
checkpatch: fix unescaped left brace

Aditya Srivastava <[email protected]>:
checkpatch: add fix option for ASSIGNMENT_CONTINUATIONS
checkpatch: add fix option for LOGICAL_CONTINUATIONS
checkpatch: add fix and improve warning msg for non-standard signature

Dwaipayan Ray <[email protected]>:
checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]
checkpatch: add warning for lines starting with a '#' in commit log
checkpatch: fix TYPO_SPELLING check for words with apostrophe

Joe Perches <[email protected]>:
checkpatch: add printk_once and printk_ratelimit to prefer pr_<level> warning

Subsystem: nilfs

Alex Shi <[email protected]>:
fs/nilfs2: remove some unused macros to tame gcc

Subsystem: kdump

Alexander Egorenkov <[email protected]>:
kdump: append uts_namespace.name offset to VMCOREINFO

Subsystem: rapidio

Sebastian Andrzej Siewior <[email protected]>:
rapidio: remove unused rio_get_asm() and rio_get_device()

Subsystem: gcov

Nick Desaulniers <[email protected]>:
gcov: remove support for GCC < 4.9

Alex Shi <[email protected]>:
gcov: fix kernel-doc markup issue

Subsystem: bfs

Randy Dunlap <[email protected]>:
bfs: don't use WARNING: string when it's just info.

Subsystem: relay

Jani Nikula <[email protected]>:
Patch series "relay: cleanup and const callbacks", v2:
relay: remove unused buf_mapped and buf_unmapped callbacks
relay: require non-NULL callbacks in relay_open()
relay: make create_buf_file and remove_buf_file callbacks mandatory
relay: allow the use of const callback structs drm/i915: make relay callbacks const
ath10k: make relay callbacks const
ath11k: make relay callbacks const
ath9k: make relay callbacks const
blktrace: make relay callbacks const

Subsystem: resource

Mauro Carvalho Chehab <[email protected]>:
kernel/resource.c: fix kernel-doc markups

Subsystem: ubsan

Kees Cook <[email protected]>:
Patch series "Clean up UBSAN Makefile", v2:
ubsan: remove redundant -Wno-maybe-uninitialized
ubsan: move cc-option tests into Kconfig
ubsan: disable object-size sanitizer under GCC
ubsan: disable UBSAN_TRAP for all*config
ubsan: enable for all*config builds
ubsan: remove UBSAN_MISC in favor of individual options
ubsan: expand tests and reporting

Dmitry Vyukov <[email protected]>:
kcov: don't instrument with UBSAN

Zou Wei <[email protected]>:
lib/ubsan.c: mark type_check_kinds with static keyword

Subsystem: reboot

Matteo Croce <[email protected]>:
reboot: refactor and comment the cpu selection code
reboot: allow to specify reboot mode via sysfs
reboot: remove cf9_safe from allowed types and rename cf9_force
Patch series "reboot: sysfs improvements":
reboot: allow to override reboot type if quirks are found
reboot: hide from sysfs not applicable settings

Subsystem: fault-injection

Barnabás Pőcze <[email protected]>:
fault-injection: handle EI_ETYPE_TRUE

Subsystem: lzo

Jason Yan <[email protected]>:
lib/lzo/lzo1x_compress.c: make lzogeneric1x_1_compress() static

Subsystem: apparmor

Andy Shevchenko <[email protected]>:
apparmor: remove duplicate macro list_entry_is_head()

Subsystem: mm/pagemap

Christoph Hellwig <[email protected]>:
Patch series "simplify follow_pte a bit":
mm: unexport follow_pte_pmd
mm: simplify follow_pte{,pmd}

Subsystem: mm/cleanups

Haitao Shi <[email protected]>:
mm: fix some spelling mistakes in comments

Subsystem: mm/gup

Jann Horn <[email protected]>:
mmap locking API: don't check locking if the mm isn't live yet mm/gup: assert that the mmap lock is held in __get_user_pages()

 Documentation/ABI/testing/sysfs-kernel-reboot    |   32 
 Documentation/admin-guide/kdump/vmcoreinfo.rst   |    6 
 Documentation/dev-tools/ubsan.rst                |    1 
 Documentation/filesystems/proc.rst               |    2 
 MAINTAINERS                                      |    5 
 arch/alpha/kernel/process.c                      |    2 
 arch/powerpc/kernel/vmlinux.lds.S                |    4 
 arch/s390/pci/pci_mmio.c                         |    4 
 drivers/gpio/gpio-thunderx.c                     |   11 
 drivers/gpio/gpio-xilinx.c                       |   61 -
 drivers/gpu/drm/i915/gt/uc/intel_guc_log.c       |    2 
 drivers/misc/lkdtm/Makefile                      |    1 
 drivers/misc/lkdtm/bugs.c                        |   50 +
 drivers/misc/lkdtm/core.c                        |    3 
 drivers/misc/lkdtm/fortify.c                     |   82 ++
 drivers/misc/lkdtm/lkdtm.h                       |   19 
 drivers/net/wireless/ath/ath10k/spectral.c       |    2 
 drivers/net/wireless/ath/ath11k/spectral.c       |    2 
 drivers/net/wireless/ath/ath9k/common-spectral.c |    2 
 drivers/rapidio/rio.c                            |   81 --
 fs/bfs/inode.c                                   |    2 
 fs/dax.c                                         |    9 
 fs/exec.c                                        |    8 
 fs/nfs/callback_proc.c                           |    5 
 fs/nilfs2/segment.c                              |    5 
 fs/proc/array.c                                  |   28 
 fs/proc/base.c                                   |    2 
 fs/proc/generic.c                                |   24 
 fs/proc/internal.h                               |   10 
 fs/proc/proc_net.c                               |   20 
 include/asm-generic/bitops/find.h                |   19 
 include/asm-generic/getorder.h                   |    2 
 include/linux/bitmap.h                           |   67 +-
 include/linux/bitops.h                           |   24 
 include/linux/dcache.h                           |    1 
 include/linux/iommu-helper.h                     |    4 
 include/linux/kernel.h                           |  173 -----
 include/linux/log2.h                             |    3 
 include/linux/math.h                             |  177 +++++
 include/linux/mm.h                               |    6 
 include/linux/mm_types.h                         |   10 
 include/linux/mmap_lock.h                        |   16 
 include/linux/proc_fs.h                          |    8 
 include/linux/rcu_node_tree.h                    |    2 
 include/linux/relay.h                            |   29 
 include/linux/rio_drv.h                          |    3 
 include/linux/string.h                           |   75 +-
 include/linux/units.h                            |    2 
 kernel/Makefile                                  |    3 
 kernel/acct.c                                    |    7 
 kernel/crash_core.c                              |    1 
 kernel/fail_function.c                           |    6 
 kernel/gcov/gcc_4_7.c                            |   10 
 kernel/reboot.c                                  |  308 ++++++++-
 kernel/relay.c                                   |  111 ---
 kernel/resource.c                                |   24 
 kernel/trace/blktrace.c                          |    2 
 lib/Kconfig.debug                                |   11 
 lib/Kconfig.ubsan                                |  154 +++-
 lib/Makefile                                     |    7 
 lib/bits_kunit.c                                 |   75 ++
 lib/cmdline.c                                    |   20 
 lib/cmdline_kunit.c                              |  100 +++
 lib/errname.c                                    |    1 
 lib/error-inject.c                               |    2 
 lib/errseq.c                                     |    1 
 lib/find_bit.c                                   |   17 
 lib/linear_ranges_kunit.c                        |  228 +++++++
 lib/list-test.c                                  |  748 -----------------------
 lib/list_kunit.c                                 |  748 +++++++++++++++++++++++
 lib/lz4/lz4_decompress.c                         |    6 
 lib/lz4/lz4defs.h                                |    1 
 lib/lzo/lzo1x_compress.c                         |    2 
 lib/math/div64.c                                 |    4 
 lib/math/int_pow.c                               |    2 
 lib/math/int_sqrt.c                              |    3 
 lib/math/reciprocal_div.c                        |    9 
 lib/stackdepot.c                                 |   11 
 lib/string.c                                     |    4 
 lib/test_bitmap.c                                |  143 ++++
 lib/test_bits.c                                  |   75 --
 lib/test_firmware.c                              |    9 
 lib/test_free_pages.c                            |    5 
 lib/test_kmod.c                                  |   26 
 lib/test_linear_ranges.c                         |  228 -------
 lib/test_lockup.c                                |   16 
 lib/test_ubsan.c                                 |   74 ++
 lib/ubsan.c                                      |    2 
 mm/filemap.c                                     |    2 
 mm/gup.c                                         |    2 
 mm/huge_memory.c                                 |    2 
 mm/khugepaged.c                                  |    2 
 mm/memblock.c                                    |    2 
 mm/memory.c                                      |   36 -
 mm/memory_hotplug.c                              |    2 
 mm/migrate.c                                     |    2 
 mm/page_ext.c                                    |    2 
 mm/swapfile.c                                    |   11 
 scripts/Makefile.ubsan                           |   49 -
 scripts/checkpatch.pl                            |  495 +++++++++++----
 security/apparmor/apparmorfs.c                   |    3 
 tools/testing/selftests/lkdtm/tests.txt          |    1 
 102 files changed, 3022 insertions(+), 1899 deletions(-)
author maintainer
#1 lore raw

PATCH 1/95 mm: fix a race on nr_swap_pages

From: Zhaoyang Huang <[email protected]>
Subject: mm: fix a race on nr_swap_pages

The scenario on which "Free swap = -4kB" happens in my system, which is caused by several get_swap_pages racing with each other and show_swap_cache_info happens simutaniously. No need to add a lock on get_swap_page_of_type as we remove "Presub/PosAdd" here.

ProcessA ProcessB ProcessC
ngoals = 1 ngoals = 1
avail = nr_swap_pages(1) avail = nr_swap_pages(1)
nr_swap_pages(1) -= ngoals

				nr_swap_pages(0) -= ngoals
								nr_swap_pages = -1

---

 mm/swapfile.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
mm/swapfile.c renamed from mm/swapfile.c~mm-fix-a-race-on-nr_swap_pages +6 -5
@@ -1042,16 +1042,18 @@ int get_swap_pages(int n_goal, swp_entry
 	/* Only single cluster request supported */
 	WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
 
+	spin_lock(&swap_avail_lock);
+
 	avail_pgs = atomic_long_read(&nr_swap_pages) / size;
-	if (avail_pgs <= 0)
+	if (avail_pgs <= 0) {
+		spin_unlock(&swap_avail_lock);
 		goto noswap;
+	}
 
 	n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs);
 
 	atomic_long_sub(n_goal * size, &nr_swap_pages);
 
-	spin_lock(&swap_avail_lock);
-
 start_over:
 	node = numa_node_id();
 	plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
@@ -1125,14 +1127,13 @@ swp_entry_t get_swap_page_of_type(int ty
 
 	spin_lock(&si->lock);
 	if (si->flags & SWP_WRITEOK) {
-		atomic_long_dec(&nr_swap_pages);
 		/* This is called for allocating swap entry, not cache */
 		offset = scan_swap_map(si, 1);
 		if (offset) {
+			atomic_long_dec(&nr_swap_pages);
 			spin_unlock(&si->lock);
 			return swp_entry(type, offset);
 		}
-		atomic_long_inc(&nr_swap_pages);
 	}
 	spin_unlock(&si->lock);
 fail:

_

author maintainer
#2 lore raw

PATCH 2/95 mm/memory_hotplug: quieting offline operation

In reply to Andrew Morton

From: Laurent Dufour <[email protected]>
Subject: mm/memory_hotplug: quieting offline operation

On PowerPC, when dymically removing memory from a system we can see in the console a lot of messages like this:

[  186.575389] Offlined Pages 4096

This message is displayed on each LMB (256MB) removed, which means that we removing 1TB of memory, this message is displayed 4096 times.

Moving it to DEBUG to not flood the console.

---

 mm/memory_hotplug.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memory_hotplug.c renamed from mm/memory_hotplug.c~mm-memory_hotplug-quieting-offline-operation +1 -1
@@ -1561,7 +1561,7 @@ int __ref offline_pages(unsigned long st
 
 	/* Mark all sections offline and remove free pages from the buddy. */
 	__offline_isolated_pages(start_pfn, end_pfn);
-	pr_info("Offlined Pages %ld\n", nr_pages);
+	pr_debug("Offlined Pages %ld\n", nr_pages);
 
 	/*
 	 * The memory sections are marked offline, and the pageblock flags

_

author maintainer
#3 lore raw

PATCH 3/95 alpha: replace bogus in_interrupt()

In reply to Andrew Morton

From: Thomas Gleixner <[email protected]>
Subject: alpha: replace bogus in_interrupt()

in_interrupt() is true for a variety of things including bottom half disabled regions. Deducing hard interrupt context from it is dubious at best.

Use in_irq() which is true if called in hard interrupt context. Otherwise calling irq_exit() would do more harm than good.

---

 arch/alpha/kernel/process.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/alpha/kernel/process.c renamed from arch/alpha/kernel/process.c~alpha-replace-bogus-in_interrupt +1 -1
@@ -134,7 +134,7 @@ common_shutdown_1(void *generic_ptr)
 #ifdef CONFIG_DUMMY_CONSOLE
 		/* If we've gotten here after SysRq-b, leave interrupt
 		   context before taking over the console. */
-		if (in_interrupt())
+		if (in_irq())
 			irq_exit();
 		/* This has the effect of resetting the VGA video origin.  */
 		console_lock();

_

author maintainer
#4 lore raw

PATCH 4/95 procfs: delete duplicated words + other fixes

In reply to Andrew Morton

From: Randy Dunlap <[email protected]>
Subject: procfs: delete duplicated words + other fixes

Delete repeated words in fs/proc/.
{the, which}
where "which which" was changed to "with which".

---

 fs/proc/base.c     |    2 +-
 fs/proc/proc_net.c |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/proc/base.c renamed from fs/proc/base.c~procfs-delete-duplicated-words-other-fixes +1 -1
@@ -2021,7 +2021,7 @@ const struct dentry_operations pid_dentr
  * file type from dcache entry.
  *
  * Since all of the proc inode numbers are dynamically generated, the inode
- * numbers do not exist until the inode is cache.  This means creating the
+ * numbers do not exist until the inode is cache.  This means creating
  * the dcache entry in readdir is necessary to keep the inode numbers
  * reported by readdir in sync with the inode numbers reported
  * by stat.
fs/proc/proc_net.c renamed from fs/proc/proc_net.c~procfs-delete-duplicated-words-other-fixes +2 -2
@@ -140,7 +140,7 @@ EXPORT_SYMBOL_GPL(proc_create_net_data);
  * @mode: The file's access mode.
  * @parent: The parent directory in which to create.
  * @ops: The seq_file ops with which to read the file.
- * @write: The write method which which to 'modify' the file.
+ * @write: The write method with which to 'modify' the file.
  * @data: Data for retrieval by PDE_DATA().
  *
  * Create a network namespaced proc file in the @parent directory with the
@@ -232,7 +232,7 @@ EXPORT_SYMBOL_GPL(proc_create_net_single
  * @mode: The file's access mode.
  * @parent: The parent directory in which to create.
  * @show: The seqfile show method with which to read the file.
- * @write: The write method which which to 'modify' the file.
+ * @write: The write method with which to 'modify' the file.
  * @data: Data for retrieval by PDE_DATA().
  *
  * Create a network-namespaced proc file in the @parent directory with the

_

author maintainer
#5 lore raw

PATCH 5/95 proc: provide details on indirect branch speculation

In reply to Andrew Morton

From: Anand K Mistry <[email protected]>
Subject: proc: provide details on indirect branch speculation

Similar to speculation store bypass, show information about the indirect branch speculation mode of a task in /proc/$pid/status.

For testing/benchmarking, I needed to see whether IB (Indirect Branch) speculation (see Spectre-v2) is enabled on a task, to see whether an IBPB instruction should be executed on an address space switch. Unfortunately, this information isn't available anywhere else and currently the only way to get it is to hack the kernel to expose it (like this change). It also helped expose a bug with conditional IB speculation on certain CPUs.

Another place this could be useful is to audit the system when using sanboxing. With this change, I can confirm that seccomp-enabled process have IB speculation force disabled as expected when the kernel command line parameter spectre_v2_user=seccomp.

Since there's already a 'Speculation_Store_Bypass' field, I used that as precedent for adding this one.

[[email protected]: remove underscores from field name to workaround documentation issue]
Cc Anthony Steinhauser <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Anand K Mistry <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Alexey Gladkov <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Kees Cook <[email protected]>
Cc Mauro Carvalho Chehab <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc NeilBrown <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

 Documentation/filesystems/proc.rst |    2 +
 fs/proc/array.c                    |   28 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
Documentation/filesystems/proc.rst renamed from Documentation/filesystems/proc.rst~proc-provide-details-on-indirect-branch-speculation +2 -0
@@ -210,6 +210,7 @@ read the file /proc/PID/status::
   NoNewPrivs:     0
   Seccomp:        0
   Speculation_Store_Bypass:       thread vulnerable
+  SpeculationIndirectBranch:      conditional enabled
   voluntary_ctxt_switches:        0
   nonvoluntary_ctxt_switches:     1
 
@@ -292,6 +293,7 @@ It's slow but very precise.
  NoNewPrivs                  no_new_privs, like prctl(PR_GET_NO_NEW_PRIV, ...)
  Seccomp                     seccomp mode, like prctl(PR_GET_SECCOMP, ...)
  Speculation_Store_Bypass    speculative store bypass mitigation status
+ SpeculationIndirectBranch   indirect branch speculation mode
  Cpus_allowed                mask of CPUs on which this process may run
  Cpus_allowed_list           Same as previous, but in "list format"
  Mems_allowed                mask of memory nodes allowed to this process
fs/proc/array.c renamed from fs/proc/array.c~proc-provide-details-on-indirect-branch-speculation +28 -0
@@ -369,6 +369,34 @@ static inline void task_seccomp(struct s
 		seq_puts(m, "vulnerable");
 		break;
 	}
+
+	seq_puts(m, "\nSpeculationIndirectBranch:\t");
+	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
+	case -EINVAL:
+		seq_puts(m, "unsupported");
+		break;
+	case PR_SPEC_NOT_AFFECTED:
+		seq_puts(m, "not affected");
+		break;
+	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
+		seq_puts(m, "conditional force disabled");
+		break;
+	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
+		seq_puts(m, "conditional disabled");
+		break;
+	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
+		seq_puts(m, "conditional enabled");
+		break;
+	case PR_SPEC_ENABLE:
+		seq_puts(m, "always enabled");
+		break;
+	case PR_SPEC_DISABLE:
+		seq_puts(m, "always disabled");
+		break;
+	default:
+		seq_puts(m, "unknown");
+		break;
+	}
 	seq_putc(m, '\n');
 }
 

_

author maintainer
#6 lore raw

PATCH 6/95 proc: fix lookup in /proc/net subdirectories after setns(2)

In reply to Andrew Morton

From: Alexey Dobriyan <[email protected]>
Subject: proc: fix lookup in /proc/net subdirectories after setns(2)

	commit 1fde6f21d90f8ba5da3cb9c54ca991ed72696c43
	proc: fix /proc/net/* after setns(2)

only forced revalidation of regular files under /proc/net/

However, /proc/net/ is unusual in the sense of /proc/net/foo handlers take netns pointer from parent directory which is old netns.

Steps to reproduce:

	(void)open("/proc/net/sctp/snmp", O_RDONLY);
	unshare(CLONE_NEWNET);

	int fd = open("/proc/net/sctp/snmp", O_RDONLY);
	read(fd, &c, 1);

Read will read wrong data from original netns.

Patch forces lookup on every directory under /proc/net .

---

 fs/proc/generic.c       |   24 ++++++++++++++++++++++--
 fs/proc/internal.h      |    7 +++++++
 fs/proc/proc_net.c      |   16 ----------------
 include/linux/proc_fs.h |    8 +++++++-
 4 files changed, 36 insertions(+), 19 deletions(-)
fs/proc/generic.c renamed from fs/proc/generic.c~proc-fix-lookup-in-proc-net-subdirectories-after-setns2 +22 -2
@@ -349,6 +349,16 @@ static const struct file_operations proc
 	.iterate_shared		= proc_readdir,
 };
 
+static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
+{
+	return 0;
+}
+
+const struct dentry_operations proc_net_dentry_ops = {
+	.d_revalidate	= proc_net_d_revalidate,
+	.d_delete	= always_delete_dentry,
+};
+
 /*
  * proc directories can do almost nothing..
  */
@@ -471,8 +481,8 @@ struct proc_dir_entry *proc_symlink(cons
 }
 EXPORT_SYMBOL(proc_symlink);
 
-struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
-		struct proc_dir_entry *parent, void *data)
+struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode,
+		struct proc_dir_entry *parent, void *data, bool force_lookup)
 {
 	struct proc_dir_entry *ent;
 
@@ -484,10 +494,20 @@ struct proc_dir_entry *proc_mkdir_data(c
 		ent->data = data;
 		ent->proc_dir_ops = &proc_dir_operations;
 		ent->proc_iops = &proc_dir_inode_operations;
+		if (force_lookup) {
+			pde_force_lookup(ent);
+		}
 		ent = proc_register(parent, ent);
 	}
 	return ent;
 }
+EXPORT_SYMBOL_GPL(_proc_mkdir);
+
+struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
+		struct proc_dir_entry *parent, void *data)
+{
+	return _proc_mkdir(name, mode, parent, data, false);
+}
 EXPORT_SYMBOL_GPL(proc_mkdir_data);
 
 struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
fs/proc/internal.h renamed from fs/proc/internal.h~proc-fix-lookup-in-proc-net-subdirectories-after-setns2 +7 -0
@@ -310,3 +310,10 @@ extern unsigned long task_statm(struct m
 				unsigned long *, unsigned long *,
 				unsigned long *, unsigned long *);
 extern void task_mem(struct seq_file *, struct mm_struct *);
+
+extern const struct dentry_operations proc_net_dentry_ops;
+static inline void pde_force_lookup(struct proc_dir_entry *pde)
+{
+	/* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
+	pde->proc_dops = &proc_net_dentry_ops;
+}
fs/proc/proc_net.c renamed from fs/proc/proc_net.c~proc-fix-lookup-in-proc-net-subdirectories-after-setns2 +0 -16
@@ -39,22 +39,6 @@ static struct net *get_proc_net(const st
 	return maybe_get_net(PDE_NET(PDE(inode)));
 }
 
-static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
-{
-	return 0;
-}
-
-static const struct dentry_operations proc_net_dentry_ops = {
-	.d_revalidate	= proc_net_d_revalidate,
-	.d_delete	= always_delete_dentry,
-};
-
-static void pde_force_lookup(struct proc_dir_entry *pde)
-{
-	/* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
-	pde->proc_dops = &proc_net_dentry_ops;
-}
-
 static int seq_open_net(struct inode *inode, struct file *file)
 {
 	unsigned int state_size = PDE(inode)->state_size;
include/linux/proc_fs.h renamed from include/linux/proc_fs.h~proc-fix-lookup-in-proc-net-subdirectories-after-setns2 +7 -1
@@ -80,6 +80,7 @@ extern void proc_flush_pid(struct pid *)
 
 extern struct proc_dir_entry *proc_symlink(const char *,
 		struct proc_dir_entry *, const char *);
+struct proc_dir_entry *_proc_mkdir(const char *, umode_t, struct proc_dir_entry *, void *, bool);
 extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *);
 extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t,
 					      struct proc_dir_entry *, void *);
@@ -162,6 +163,11 @@ static inline struct proc_dir_entry *pro
 static inline struct proc_dir_entry *proc_mkdir(const char *name,
 	struct proc_dir_entry *parent) {return NULL;}
 static inline struct proc_dir_entry *proc_create_mount_point(const char *name) { return NULL; }
+static inline struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode,
+		struct proc_dir_entry *parent, void *data, bool force_lookup)
+{
+	return NULL;
+}
 static inline struct proc_dir_entry *proc_mkdir_data(const char *name,
 	umode_t mode, struct proc_dir_entry *parent, void *data) { return NULL; }
 static inline struct proc_dir_entry *proc_mkdir_mode(const char *name,
@@ -199,7 +205,7 @@ struct net;
 static inline struct proc_dir_entry *proc_net_mkdir(
 	struct net *net, const char *name, struct proc_dir_entry *parent)
 {
-	return proc_mkdir_data(name, 0, parent, net);
+	return _proc_mkdir(name, 0, parent, net, true);
 }
 
 struct ns_common;

_

author maintainer
#7 lore raw

PATCH 7/95 fs/proc: make pde_get() return nothing

In reply to Andrew Morton

From: Hui Su <[email protected]>
Subject: fs/proc: make pde_get() return nothing

We don't need pde_get()'s return value, so make pde_get() return nothing

---

 fs/proc/internal.h |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/proc/internal.h renamed from fs/proc/internal.h~fs-proc-make-pde_get-return-nothing +1 -2
@@ -190,10 +190,9 @@ struct dentry *proc_lookup_de(struct ino
 extern int proc_readdir(struct file *, struct dir_context *);
 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
 
-static inline struct proc_dir_entry *pde_get(struct proc_dir_entry *pde)
+static inline void pde_get(struct proc_dir_entry *pde)
 {
 	refcount_inc(&pde->refcnt);
-	return pde;
 }
 extern void pde_put(struct proc_dir_entry *);
 

_

author maintainer
#8 lore raw

PATCH 8/95 asm-generic: force inlining of get_order() to work around gcc10 poor decision

In reply to Andrew Morton

From: Christophe Leroy <[email protected]>
Subject: asm-generic: force inlining of get_order() to work around gcc10 poor decision

When building mpc885_ads_defconfig with gcc 10.1, the function get_order() appears 50 times in vmlinux:

[linux]# ppc-linux-objdump -x vmlinux | grep get_order | wc -l 50

[linux]# size vmlinux
text data bss dec hex filename
3842620 675624 135160 4653404 47015c vmlinux

In the old days, marking a function 'static inline' was forcing GCC to inline, but since commit ac7c3e4ff401 ("compiler: enable CONFIG_OPTIMIZE_INLINING forcibly") GCC may decide to not inline a function.

It looks like GCC 10 is taking poor decisions on this.

get_order() compiles into the following tiny function, occupying 20 bytes of text.

0000007c <get_order>:

  7c:   38 63 ff ff     addi    r3,r3,-1
  80:   54 63 a3 3e     rlwinm  r3,r3,20,12,31
  84:   7c 63 00 34     cntlzw  r3,r3
  88:   20 63 00 20     subfic  r3,r3,32
  8c:   4e 80 00 20     blr

By forcing get_order() to be __always_inline, the size of text is reduced by 1940 bytes, that is almost twice the space occupied by 50 times get_order()

[linux-powerpc]# size vmlinux
text data bss dec hex filename
3840680 675588 135176 4651444 46f9b4 vmlinux

---

 include/asm-generic/getorder.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/asm-generic/getorder.h renamed from include/asm-generic/getorder.h~asm-generic-force-inlining-of-get_order-to-work-around-gcc10-poor-decision +1 -1
@@ -26,7 +26,7 @@
  *
  * The result is undefined if the size is 0.
  */
-static inline __attribute_const__ int get_order(unsigned long size)
+static __always_inline __attribute_const__ int get_order(unsigned long size)
 {
 	if (__builtin_constant_p(size)) {
 		if (!size)

_

author maintainer
#9 lore raw

PATCH 9/95 kernel.h: split out mathematical helpers

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: kernel.h: split out mathematical helpers

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out mathematical helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

---

 fs/nfs/callback_proc.c        |    5 
 include/linux/bitops.h        |   11 +
 include/linux/dcache.h        |    1 
 include/linux/iommu-helper.h  |    4 
 include/linux/kernel.h        |  173 -------------------------------
 include/linux/math.h          |  177 ++++++++++++++++++++++++++++++++
 include/linux/rcu_node_tree.h |    2 
 include/linux/units.h         |    2 
 lib/errname.c                 |    1 
 lib/errseq.c                  |    1 
 lib/find_bit.c                |    3 
 lib/math/div64.c              |    4 
 lib/math/int_pow.c            |    2 
 lib/math/int_sqrt.c           |    3 
 lib/math/reciprocal_div.c     |    9 +
 15 files changed, 215 insertions(+), 183 deletions(-)
fs/nfs/callback_proc.c renamed from fs/nfs/callback_proc.c~kernelh-split-out-mathematical-helpers +5 -0
@@ -6,10 +6,15 @@
  *
  * NFSv4 callback procedures
  */
+
+#include <linux/errno.h>
+#include <linux/math.h>
 #include <linux/nfs4.h>
 #include <linux/nfs_fs.h>
 #include <linux/slab.h>
 #include <linux/rcupdate.h>
+#include <linux/types.h>
+
 #include "nfs4_fs.h"
 #include "callback.h"
 #include "delegation.h"
include/linux/bitops.h renamed from include/linux/bitops.h~kernelh-split-out-mathematical-helpers +7 -4
@@ -1,9 +1,12 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _LINUX_BITOPS_H
 #define _LINUX_BITOPS_H
+
 #include <asm/types.h>
 #include <linux/bits.h>
 
+#include <uapi/linux/kernel.h>
+
 /* Set bits in the first 'n' bytes when loaded from memory */
 #ifdef __LITTLE_ENDIAN
 #  define aligned_byte_mask(n) ((1UL << 8*(n))-1)
@@ -12,10 +15,10 @@
 #endif
 
 #define BITS_PER_TYPE(type)	(sizeof(type) * BITS_PER_BYTE)
-#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
-#define BITS_TO_U64(nr)		DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
-#define BITS_TO_U32(nr)		DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
-#define BITS_TO_BYTES(nr)	DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
+#define BITS_TO_LONGS(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
+#define BITS_TO_U64(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
+#define BITS_TO_U32(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
+#define BITS_TO_BYTES(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
 
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
include/linux/dcache.h renamed from include/linux/dcache.h~kernelh-split-out-mathematical-helpers +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/atomic.h>
 #include <linux/list.h>
+#include <linux/math.h>
 #include <linux/rculist.h>
 #include <linux/rculist_bl.h>
 #include <linux/spinlock.h>
include/linux/iommu-helper.h renamed from include/linux/iommu-helper.h~kernelh-split-out-mathematical-helpers +3 -1
@@ -3,7 +3,9 @@
 #define _LINUX_IOMMU_HELPER_H
 
 #include <linux/bug.h>
-#include <linux/kernel.h>
+#include <linux/log2.h>
+#include <linux/math.h>
+#include <linux/types.h>
 
 static inline unsigned long iommu_device_max_index(unsigned long size,
 						   unsigned long offset,
include/linux/kernel.h renamed from include/linux/kernel.h~kernelh-split-out-mathematical-helpers +3 -170

213 lines not on this page · raw message

include/linux/math.h new file +177 -0

177 lines not on this page · raw message

include/linux/rcu_node_tree.h renamed from include/linux/rcu_node_tree.h~kernelh-split-out-mathematical-helpers +2 -0
@@ -20,6 +20,8 @@
 #ifndef __LINUX_RCU_NODE_TREE_H
 #define __LINUX_RCU_NODE_TREE_H
 
+#include <linux/math.h>
+
 /*
  * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and
  * CONFIG_RCU_FANOUT_LEAF.
include/linux/units.h renamed from include/linux/units.h~kernelh-split-out-mathematical-helpers +1 -1
@@ -2,7 +2,7 @@
 #ifndef _LINUX_UNITS_H
 #define _LINUX_UNITS_H
 
-#include <linux/kernel.h>
+#include <linux/math.h>
 
 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
 
lib/errname.c renamed from lib/errname.c~kernelh-split-out-mathematical-helpers +1 -0
@@ -3,6 +3,7 @@
 #include <linux/errno.h>
 #include <linux/errname.h>
 #include <linux/kernel.h>
+#include <linux/math.h>
 
 /*
  * Ensure these tables do not accidentally become gigantic if some
lib/errseq.c renamed from lib/errseq.c~kernelh-split-out-mathematical-helpers +1 -0
@@ -3,6 +3,7 @@
 #include <linux/bug.h>
 #include <linux/atomic.h>
 #include <linux/errseq.h>
+#include <linux/log2.h>
 
 /*
  * An errseq_t is a way of recording errors in one place, and allowing any
lib/find_bit.c renamed from lib/find_bit.c~kernelh-split-out-mathematical-helpers +2 -1
@@ -15,8 +15,9 @@
 #include <linux/bitops.h>
 #include <linux/bitmap.h>
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/minmax.h>
+#include <linux/swab.h>
 
 #if !defined(find_next_bit) || !defined(find_next_zero_bit) ||			\
 	!defined(find_next_bit_le) || !defined(find_next_zero_bit_le) ||	\
lib/math/div64.c renamed from lib/math/div64.c~kernelh-split-out-mathematical-helpers +3 -1
@@ -18,9 +18,11 @@
  * or by defining a preprocessor macro in arch/include/asm/div64.h.
  */
 
+#include <linux/bitops.h>
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/math64.h>
+#include <linux/log2.h>
 
 /* Not needed on 64bit architectures */
 #if BITS_PER_LONG == 32
lib/math/int_pow.c renamed from lib/math/int_pow.c~kernelh-split-out-mathematical-helpers +1 -1
@@ -6,7 +6,7 @@
  */
 
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/types.h>
 
 /**
lib/math/int_sqrt.c renamed from lib/math/int_sqrt.c~kernelh-split-out-mathematical-helpers +2 -1
@@ -6,9 +6,10 @@
  *  square root from Guy L. Steele.
  */
 
-#include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/bitops.h>
+#include <linux/limits.h>
+#include <linux/math.h>
 
 /**
  * int_sqrt - computes the integer square root
lib/math/reciprocal_div.c renamed from lib/math/reciprocal_div.c~kernelh-split-out-mathematical-helpers +6 -3
@@ -1,10 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <linux/bitops.h>
 #include <linux/bug.h>
-#include <linux/kernel.h>
-#include <asm/div64.h>
-#include <linux/reciprocal_div.h>
 #include <linux/export.h>
+#include <linux/limits.h>
+#include <linux/math.h>
 #include <linux/minmax.h>
+#include <linux/types.h>
+
+#include <linux/reciprocal_div.h>
 
 /*
  * For a description of the algorithm please have a look at

_

author maintainer
#10 lore raw

PATCH 10/95 kernel/acct.c: use #elif instead of #end and #elif

In reply to Andrew Morton

From: Hui Su <[email protected]>
Subject: kernel/acct.c: use #elif instead of #end and #elif

Cleanup: use #elif instead of #end and #elif.

---

 kernel/acct.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/acct.c renamed from kernel/acct.c~acctc-use-elif-instead-of-end-and-elif +2 -5
@@ -381,9 +381,7 @@ static comp2_t encode_comp2_t(u64 value)
 		return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
 	}
 }
-#endif
-
-#if ACCT_VERSION == 3
+#elif ACCT_VERSION == 3
 /*
  * encode an u64 into a 32 bit IEEE float
  */
@@ -500,8 +498,7 @@ static void do_acct_process(struct bsd_a
 	/* backward-compatible 16 bit fields */
 	ac.ac_uid16 = ac.ac_uid;
 	ac.ac_gid16 = ac.ac_gid;
-#endif
-#if ACCT_VERSION == 3
+#elif ACCT_VERSION == 3
 	{
 		struct pid_namespace *ns = acct->ns;
 

_

author maintainer
#11 lore raw

PATCH 11/95 include/linux/bitmap.h: convert bitmap_empty() / bitmap_full() to return boolean

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: include/linux/bitmap.h: convert bitmap_empty() / bitmap_full() to return boolean

There is no need to return int type out of boolean expression.

---

 include/linux/bitmap.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/bitmap.h renamed from include/linux/bitmap.h~bitmap-convert-bitmap_empty-bitmap_full-to-return-boolean +2 -2
@@ -379,7 +379,7 @@ static inline int bitmap_subset(const un
 		return __bitmap_subset(src1, src2, nbits);
 }
 
-static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
+static inline bool bitmap_empty(const unsigned long *src, unsigned nbits)
 {
 	if (small_const_nbits(nbits))
 		return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
@@ -387,7 +387,7 @@ static inline int bitmap_empty(const uns
 	return find_first_bit(src, nbits) == nbits;
 }
 
-static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
+static inline bool bitmap_full(const unsigned long *src, unsigned int nbits)
 {
 	if (small_const_nbits(nbits))
 		return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));

_

author maintainer
#12 lore raw

PATCH 12/95 bitmap: remove unused function declaration

In reply to Andrew Morton

From: "Ma, Jianpeng" <[email protected]>
Subject: bitmap: remove unused function declaration

---

 include/linux/bitmap.h |    2 --
 1 file changed, 2 deletions(-)
include/linux/bitmap.h renamed from include/linux/bitmap.h~bitmap-remove-unused-function-declaration +0 -2
@@ -126,8 +126,6 @@ extern void bitmap_free(const unsigned l
  * lib/bitmap.c provides these functions:
  */
 
-extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
-extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
 extern int __bitmap_equal(const unsigned long *bitmap1,
 			  const unsigned long *bitmap2, unsigned int nbits);
 extern bool __pure __bitmap_or_equal(const unsigned long *src1,

_

author maintainer
#13 lore raw

PATCH 13/95 lib/test_free_pages.c: add basic progress indicators

In reply to Andrew Morton

From: Geert Uytterhoeven <[email protected]>
Subject: lib/test_free_pages.c: add basic progress indicators

The test module to check that free_pages() does not leak memory does not provide any feedback whatsoever its state or progress, but may take some time on slow machines. Add the printing of messages upon starting each phase of the test, and upon completion.

---

 lib/test_free_pages.c |    5 +++++
 1 file changed, 5 insertions(+)
lib/test_free_pages.c renamed from lib/test_free_pages.c~lib-test_free_pages-add-basic-progress-indicators +5 -0
@@ -5,6 +5,8 @@
  * Author: Matthew Wilcox <[email protected]>
  */
 
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
 #include <linux/gfp.h>
 #include <linux/mm.h>
 #include <linux/module.h>
@@ -26,8 +28,11 @@ static void test_free_pages(gfp_t gfp)
 
 static int m_in(void)
 {
+	pr_info("Testing with GFP_KERNEL\n");
 	test_free_pages(GFP_KERNEL);
+	pr_info("Testing with GFP_KERNEL | __GFP_COMP\n");
 	test_free_pages(GFP_KERNEL | __GFP_COMP);
+	pr_info("Test completed\n");
 
 	return 0;
 }

_

author maintainer
#14 lore raw

PATCH 14/95 lib/stackdepot.c: replace one-element array with flexible-array member

In reply to Andrew Morton

From: "Gustavo A. R. Silva" <[email protected]>
Subject: lib/stackdepot.c: replace one-element array with flexible-array member

Patch series "] lib/stackdepot.c: Replace one-element array with flexible-array member".

This series aims to replace a one-element array with a flexible-array member. Also, make use of the struct_size(), flexible_array_size() and array_size() helpers.

This patch (of 3):

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure. Kernel code should always use “flexible array members”[1] for these cases. The older style of one-element or zero-length arrays should no longer be used[2].

Refactor the code according to the use of a flexible-array member in struct stack_record, instead of a one-element array, and use the struct_size() helper to calculate the size for the allocation.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9-rc1/process/deprecated.html#zero-length-and-one-element-arrays

---

 lib/stackdepot.c |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
lib/stackdepot.c renamed from lib/stackdepot.c~lib-stackdepotc-replace-one-element-array-with-flexible-array-member +2 -3
@@ -62,7 +62,7 @@ struct stack_record {
 	u32 hash;			/* Hash in the hastable */
 	u32 size;			/* Number of frames in the stack */
 	union handle_parts handle;
-	unsigned long entries[1];	/* Variable-sized array of entries. */
+	unsigned long entries[];	/* Variable-sized array of entries. */
 };
 
 static void *stack_slabs[STACK_ALLOC_MAX_SLABS];
@@ -104,9 +104,8 @@ static bool init_stack_slab(void **preal
 static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
 		u32 hash, void **prealloc, gfp_t alloc_flags)
 {
-	int required_size = offsetof(struct stack_record, entries) +
-		sizeof(unsigned long) * size;
 	struct stack_record *stack;
+	size_t required_size = struct_size(stack, entries, size);
 
 	required_size = ALIGN(required_size, 1 << STACK_ALLOC_ALIGN);
 

_

author maintainer
#15 lore raw

PATCH 15/95 lib/stackdepot.c: use flex_array_size() helper in memcpy()

In reply to Andrew Morton

From: "Gustavo A. R. Silva" <[email protected]>
Subject: lib/stackdepot.c: use flex_array_size() helper in memcpy()

Make use of the flex_array_size() helper to calculate the size of a flexible array member within an enclosing structure.

This helper offers defense-in-depth against potential integer overflows, while at the same time makes it explicitly clear that we are dealing with a flexible array member.

---

 lib/stackdepot.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/stackdepot.c renamed from lib/stackdepot.c~lib-stackdepotc-use-flex_array_size-helper-in-memcpy +1 -1
@@ -135,7 +135,7 @@ static struct stack_record *depot_alloc_
 	stack->handle.slabindex = depot_index;
 	stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN;
 	stack->handle.valid = 1;
-	memcpy(stack->entries, entries, size * sizeof(unsigned long));
+	memcpy(stack->entries, entries, flex_array_size(stack, entries, size));
 	depot_offset += required_size;
 
 	return stack;

_

author maintainer
#16 lore raw

PATCH 16/95 lib/stackdepot.c: use array_size() helper in jhash2()

In reply to Andrew Morton

From: "Gustavo A. R. Silva" <[email protected]>
Subject: lib/stackdepot.c: use array_size() helper in jhash2()

Use array_size() helper instead of the open-coded version in jhash2(). These sorts of multiplication factors need to be wrapped in array_size().

Also, use the preferred form for passing the size of an object type.

---

 lib/stackdepot.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/stackdepot.c renamed from lib/stackdepot.c~lib-stackdepotc-use-array_size-helper-in-jhash2 +2 -2
@@ -154,8 +154,8 @@ static struct stack_record *stack_table[
 static inline u32 hash_stack(unsigned long *entries, unsigned int size)
 {
 	return jhash2((u32 *)entries,
-			       size * sizeof(unsigned long) / sizeof(u32),
-			       STACK_HASH_SEED);
+		      array_size(size,  sizeof(*entries)) / sizeof(u32),
+		      STACK_HASH_SEED);
 }
 
 /* Use our own, non-instrumented version of memcmp().

_

author maintainer
#17 lore raw

PATCH 17/95 lib/test_lockup.c: minimum fix to get it compiled on PREEMPT_RT

In reply to Andrew Morton

From: Sebastian Andrzej Siewior <[email protected]>
Subject: lib/test_lockup.c: minimum fix to get it compiled on PREEMPT_RT

On PREEMPT_RT the locks are quite different so they can't be tested as it is done below. The alternative is to test for the waitlock within rtmutex.

This is the bare minimun to get it compiled. Problems which exist on PREEMP_RT:

- none of the locks (spinlock_t, rwlock_t, mutex_t, rw_semaphore) may be acquired with disabled preemption or interrupts.
If I read the code correct the it is possible to acquire a mutex_t with disabled interrupts.
I don't know how to obtain a lock pointer. Technically they are not exported to userland.

- memory can not be allocated with disabled preemption or interrupts even with GFP_ATOMIC.

---

 lib/test_lockup.c |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
lib/test_lockup.c renamed from lib/test_lockup.c~lib-test_lockup-minimum-fix-to-get-it-compiled-on-preempt_rt +16 -0
@@ -480,6 +480,21 @@ static int __init test_lockup_init(void)
 		return -EINVAL;
 
 #ifdef CONFIG_DEBUG_SPINLOCK
+#ifdef CONFIG_PREEMPT_RT
+	if (test_magic(lock_spinlock_ptr,
+		       offsetof(spinlock_t, lock.wait_lock.magic),
+		       SPINLOCK_MAGIC) ||
+	    test_magic(lock_rwlock_ptr,
+		       offsetof(rwlock_t, rtmutex.wait_lock.magic),
+		       SPINLOCK_MAGIC) ||
+	    test_magic(lock_mutex_ptr,
+		       offsetof(struct mutex, lock.wait_lock.magic),
+		       SPINLOCK_MAGIC) ||
+	    test_magic(lock_rwsem_ptr,
+		       offsetof(struct rw_semaphore, rtmutex.wait_lock.magic),
+		       SPINLOCK_MAGIC))
+		return -EINVAL;
+#else
 	if (test_magic(lock_spinlock_ptr,
 		       offsetof(spinlock_t, rlock.magic),
 		       SPINLOCK_MAGIC) ||
@@ -494,6 +509,7 @@ static int __init test_lockup_init(void)
 		       SPINLOCK_MAGIC))
 		return -EINVAL;
 #endif
+#endif
 
 	if ((wait_state != TASK_RUNNING ||
 	     (call_cond_resched && !reacquire_locks) ||

_

author maintainer
#18 lore raw

PATCH 18/95 lib/list_kunit: follow new file name convention for KUnit tests

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/list_kunit: follow new file name convention for KUnit tests

Follow new file name convention for the KUnit tests. Since we have lib/*test*.c in a few variations, use 'kunit' suffix to distinguish usual test cases with KUnit-based ones.

---

 MAINTAINERS  |    2 +-
 lib/Makefile |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
lib/Makefile renamed from lib/Makefile~lib-list_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -350,6 +350,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
-obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
+obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o
MAINTAINERS renamed from MAINTAINERS~lib-list_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -10263,7 +10263,7 @@ M:	David Gow <[email protected]>
 L:	[email protected]
 L:	[email protected]
 S:	Maintained
-F:	lib/list-test.c
+F:	lib/list_kunit.c
 
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>

_

#19 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

This is complete garbage, Andrew.

In this patch, you change the name in the Makefile:

Quoted 7 lines · Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/list_kunit: follow new file name convention for KUnit tests

Follow new file name convention for the KUnit tests. Since we have lib/*test*.c in a few variations, use 'kunit' suffix to distinguish usual test cases with KUnit-based ones.

...

lib/Makefile obj-$(CONFIG_PLDMFW) += pldmfw/ · line 355

Quoted 10 lines
lib/Makefile renamed from lib/Makefile~lib-list_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -350,6 +350,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
-obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
+obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o

but the actual *file* isn't changed.

So there is no way in hell this will build.

The file is _actually_ renamed in patch [20/95], which claims to do the lib/bits_kunit stuff, but actually does both the bits _and_ the list_kunit stuff.

Making things worse, your use of substandard tools means that this garbage shows up as a patch that is over sixteen *hundred* lines long, when proper tooling would hav eactually shown it as a rename.

It _should_ have been about 10 lines total. Not 1600 lines that hide the problem and make it really nasty to see.

That 1600 lines of noise is ignoring patch [19/95], which does the "linear_ranges_kunit" renaming, adding another ~500 lines of illegible garbage to my mailbox.

At least that one got the Makefile right, although it was really hard to actually see that, considering how nasty and illegible the patch was due to renaming.

Basically, I'm going to throw all these rename patches away. Not only were they were completely buggy, but they were also illegible because of your inferior tools.

Don't send me any more rename patches until your tools can actually do renames.

Linus

#20 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

Hmm... sorry about this: it definitely shouldn't've happened.

I know the original patchset did have an issue (with one of the other patches) that Andrew fixed while merging, so maybe it snuck through while that was happening.

Quoted 27 lines · Linus Torvalds

This is complete garbage, Andrew.

In this patch, you change the name in the Makefile:

Quoted 7 lines · Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/list_kunit: follow new file name convention for KUnit tests

Follow new file name convention for the KUnit tests. Since we have lib/*test*.c in a few variations, use 'kunit' suffix to distinguish usual test cases with KUnit-based ones.

...

lib/Makefile obj-$(CONFIG_PLDMFW) += pldmfw/ · line 355

Quoted 10 lines
lib/Makefile renamed from lib/Makefile~lib-list_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -350,6 +350,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
-obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
+obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o

but the actual *file* isn't changed.

Quoted 1 line

So there is no way in hell this will build.

This is interesting, as the original patch did rename it here:
https://lore.kernel.org/linux-kselftest/[email protected]/

So something's definitely more muddled than just the renames being expanded out...

Quoted 4 lines

The file is _actually_ renamed in patch [20/95], which claims to do the lib/bits_kunit stuff, but actually does both the bits _and_ the list_kunit stuff.

And again, that rename is not in the bits_kunit patch in the original thread:
https://lore.kernel.org/linux-kselftest/[email protected]/

Quoted 12 lines

Making things worse, your use of substandard tools means that this garbage shows up as a patch that is over sixteen *hundred* lines long, when proper tooling would hav eactually shown it as a rename.

It _should_ have been about 10 lines total. Not 1600 lines that hide the problem and make it really nasty to see.

That 1600 lines of noise is ignoring patch [19/95], which does the "linear_ranges_kunit" renaming, adding another ~500 lines of illegible garbage to my mailbox.

Quoted 7 lines

At least that one got the Makefile right, although it was really hard to actually see that, considering how nasty and illegible the patch was due to renaming.

Basically, I'm going to throw all these rename patches away. Not only were they were completely buggy, but they were also illegible because of your inferior tools.

Note that the three rename patches (list_kunit, linear_ranges_kunit, and bits_kunit) were part of the same patchset as the following three lib/cmdline{,_kunit} changes. Those don't depend on the earlier patches, don't have renames, and seem to be fine from a cursory glance, but the last one ([23/95]) did have some issues earlier (which are now fixed).

Quoted 4 lines

Don't send me any more rename patches until your tools can actually do renames.

Linus

My other thought is that this sort of patchset really makes more sense to push through the kselftest/kunit branch anyway, as all of the changes were really more KUnit related than anything else. Does it make sense to re-submit this that way?

Cheers,
-- David

#21 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

Quoted 4 lines · David Gow

I know the original patchset did have an issue (with one of the other patches) that Andrew fixed while merging, so maybe it snuck through while that was happening.

Yeah, I suspect it happened when Andrew was shuffling things around.

And it's probably partly due to the fact that the patches themselves are _really_ hard to actually read, because all the real changes are hidden by the huge patches to remove and add files.

When you have 1600 lines of patch, it's really easy to get "patch blind" and miss the small patch fragments that change the Makefile or Kconfig file etc.

Back in the days when we worked almost exclusively with patches (ie before BK and git), we had very high barriers for renaming files partly for this exact reason. Renames as patches are just _so_ hard to read. It's almost completely impossible to see if it's a pure rename of if something else also changed, when you have one big hunk that completely removes one file, and another big hunk that completely adds a new one.

Of course, patch conflicts when there are changes to the files also then make renames a huge pain in that situation.

With git, we've been *much* more open to file renames, because git itself handles at least the usual simple cases of merge conflicts automatically for us, and follows renames etc. And the git diff extension to actually show renames as renames in a diff make it a *lot* easier to see what a rename patch actually does.

But as long as renames are treated as patches, I'm going to go back to the old rules that were "we never rename a file unless there is some absolutely massive critical reason to do so".

Linus

#22 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

In reply to David Gow
Quoted 2 lines · David Gow

On Wed, Dec 16, 2020 at 2:02 PM Linus Torvalds
<[email protected]> wrote:

...

Quoted 6 lines
Quoted 1 line

Don't send me any more rename patches until your tools can actually do renames.

My other thought is that this sort of patchset really makes more sense to push through the kselftest/kunit branch anyway, as all of the changes were really more KUnit related than anything else. Does it make sense to re-submit this that way?

I think it makes sense because this is driven by rules set up by kselftest/kunit. My main concern here is to have cmdline_kunit in the tree (it is a new file). Renaming is up to you, I just wanted to be consistent with names and KUnit documentation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#23 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

Quoted 14 lines · Andy Shevchenko
Quoted 2 lines · David Gow

On Wed, Dec 16, 2020 at 2:02 PM Linus Torvalds
<[email protected]> wrote:

...

Quoted 6 lines
Quoted 1 line

Don't send me any more rename patches until your tools can actually do renames.

My other thought is that this sort of patchset really makes more sense to push through the kselftest/kunit branch anyway, as all of the changes were really more KUnit related than anything else. Does it make sense to re-submit this that way?

Quoted 4 lines

I think it makes sense because this is driven by rules set up by kselftest/kunit. My main concern here is to have cmdline_kunit in the tree (it is a new file). Renaming is up to you, I just wanted to be consistent with names and KUnit documentation.

It looks like the cmdline_kunit changes have been merged now, so it's a relief that those weren't held up: I agree that they're the more important changes.

I do think renaming things to match the new convention is a good idea (so thanks again for doing that), but it's not exactly an urgent fix. My preference is that these get added to one of the kunit branches in the kselftest repo, so they can be picked up when convenient. This should just be the first three patches in this series:
https://lore.kernel.org/linux-kselftest/[email protected]/

I'd expect those to still apply pretty cleanly, but I haven't actually checked yet.

Cheers,
-- David

#24 lore raw

Re: [patch 18/95] lib/list_kunit: follow new file name convention for KUnit tests

Quoted 5 lines · David Gow
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · David Gow

On Wed, Dec 16, 2020 at 2:02 PM Linus Torvalds
<[email protected]> wrote:

...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Don't send me any more rename patches until your tools can actually do renames.

My other thought is that this sort of patchset really makes more sense to push through the kselftest/kunit branch anyway, as all of the changes were really more KUnit related than anything else. Does it make sense to re-submit this that way?

Quoted 8 lines
Quoted 4 lines

I think it makes sense because this is driven by rules set up by kselftest/kunit. My main concern here is to have cmdline_kunit in the tree (it is a new file). Renaming is up to you, I just wanted to be consistent with names and KUnit documentation.

It looks like the cmdline_kunit changes have been merged now, so it's a relief that those weren't held up: I agree that they're the more important changes.

Yes, they are in, thanks Linus!

Quoted 9 lines

I do think renaming things to match the new convention is a good idea (so thanks again for doing that), but it's not exactly an urgent fix. My preference is that these get added to one of the kunit branches in the kselftest repo, so they can be picked up when convenient. This should just be the first three patches in this series:
https://lore.kernel.org/linux-kselftest/[email protected]/

I'd expect those to still apply pretty cleanly, but I haven't actually checked yet.

They won't apply after v5.11-rc1 due to cmdline_kunit entry. I can rebase after rc1 and send it to KUnit/kselftest mailing lists.

Andrew, please drop these patches from your quilt and thanks for carrying them.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#25 lore raw

PATCH 19/95 lib/linear_ranges_kunit: follow new file name convention for KUnit tests

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/linear_ranges_kunit: follow new file name convention for KUnit tests

Follow new file name convention for the KUnit tests. Since we have lib/*test*.c in a few variations, use 'kunit' suffix to distinguish usual test cases with KUnit-based ones.

---

 MAINTAINERS               |    3 
 lib/Makefile              |    2 
 lib/linear_ranges_kunit.c |  228 ++++++++++++++++++++++++++++++++++++
 lib/test_linear_ranges.c  |  228 ------------------------------------
 4 files changed, 230 insertions(+), 231 deletions(-)
lib/linear_ranges_kunit.c new file +228 -0
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for the linear_ranges helper.
+ *
+ * Copyright (C) 2020, ROHM Semiconductors.
+ * Author: Matti Vaittinen <[email protected]>
+ */
+#include <kunit/test.h>
+
+#include <linux/linear_range.h>
+
+/* First things first. I deeply dislike unit-tests. I have seen all the hell
+ * breaking loose when people who think the unit tests are "the silver bullet"
+ * to kill bugs get to decide how a company should implement testing strategy...
+ *
+ * Believe me, it may get _really_ ridiculous. It is tempting to think that
+ * walking through all the possible execution branches will nail down 100% of
+ * bugs. This may lead to ideas about demands to get certain % of "test
+ * coverage" - measured as line coverage. And that is one of the worst things
+ * you can do.
+ *
+ * Ask people to provide line coverage and they do. I've seen clever tools
+ * which generate test cases to test the existing functions - and by default
+ * these tools expect code to be correct and just generate checks which are
+ * passing when ran against current code-base. Run this generator and you'll get
+ * tests that do not test code is correct but just verify nothing changes.
+ * Problem is that testing working code is pointless. And if it is not
+ * working, your test must not assume it is working. You won't catch any bugs
+ * by such tests. What you can do is to generate a huge amount of tests.
+ * Especially if you were are asked to proivde 100% line-coverage x_x. So what
+ * does these tests - which are not finding any bugs now - do?
+ *
+ * They add inertia to every future development. I think it was Terry Pratchet
+ * who wrote someone having same impact as thick syrup has to chronometre.
+ * Excessive amount of unit-tests have this effect to development. If you do
+ * actually find _any_ bug from code in such environment and try fixing it...
+ * ...chances are you also need to fix the test cases. In sunny day you fix one
+ * test. But I've done refactoring which resulted 500+ broken tests (which had
+ * really zero value other than proving to managers that we do do "quality")...
+ *
+ * After this being said - there are situations where UTs can be handy. If you
+ * have algorithms which take some input and should produce output - then you
+ * can implement few, carefully selected simple UT-cases which test this. I've
+ * previously used this for example for netlink and device-tree data parsing
+ * functions. Feed some data examples to functions and verify the output is as
+ * expected. I am not covering all the cases but I will see the logic should be
+ * working.
+ *
+ * Here we also do some minor testing. I don't want to go through all branches
+ * or test more or less obvious things - but I want to see the main logic is
+ * working. And I definitely don't want to add 500+ test cases that break when
+ * some simple fix is done x_x. So - let's only add few, well selected tests
+ * which ensure as much logic is good as possible.
+ */
+
+/*
+ * Test Range 1:
+ * selectors:	2	3	4	5	6
+ * values (5):	10	20	30	40	50
+ *
+ * Test Range 2:
+ * selectors:	7	8	9	10
+ * values (4):	100	150	200	250
+ */
+
+#define RANGE1_MIN 10
+#define RANGE1_MIN_SEL 2
+#define RANGE1_STEP 10
+
+/* 2, 3, 4, 5, 6 */
+static const unsigned int range1_sels[] = { RANGE1_MIN_SEL, RANGE1_MIN_SEL + 1,
+					    RANGE1_MIN_SEL + 2,
+					    RANGE1_MIN_SEL + 3,
+					    RANGE1_MIN_SEL + 4 };
+/* 10, 20, 30, 40, 50 */
+static const unsigned int range1_vals[] = { RANGE1_MIN, RANGE1_MIN +
+					    RANGE1_STEP,
+					    RANGE1_MIN + RANGE1_STEP * 2,
+					    RANGE1_MIN + RANGE1_STEP * 3,
+					    RANGE1_MIN + RANGE1_STEP * 4 };
+
+#define RANGE2_MIN 100
+#define RANGE2_MIN_SEL 7
+#define RANGE2_STEP 50
+
+/*  7, 8, 9, 10 */
+static const unsigned int range2_sels[] = { RANGE2_MIN_SEL, RANGE2_MIN_SEL + 1,
+					    RANGE2_MIN_SEL + 2,
+					    RANGE2_MIN_SEL + 3 };
+/* 100, 150, 200, 250 */
+static const unsigned int range2_vals[] = { RANGE2_MIN, RANGE2_MIN +
+					    RANGE2_STEP,
+					    RANGE2_MIN + RANGE2_STEP * 2,
+					    RANGE2_MIN + RANGE2_STEP * 3 };
+
+#define RANGE1_NUM_VALS (ARRAY_SIZE(range1_vals))
+#define RANGE2_NUM_VALS (ARRAY_SIZE(range2_vals))
+#define RANGE_NUM_VALS (RANGE1_NUM_VALS + RANGE2_NUM_VALS)
+
+#define RANGE1_MAX_SEL (RANGE1_MIN_SEL + RANGE1_NUM_VALS - 1)
+#define RANGE1_MAX_VAL (range1_vals[RANGE1_NUM_VALS - 1])
+
+#define RANGE2_MAX_SEL (RANGE2_MIN_SEL + RANGE2_NUM_VALS - 1)
+#define RANGE2_MAX_VAL (range2_vals[RANGE2_NUM_VALS - 1])
+
+#define SMALLEST_SEL RANGE1_MIN_SEL
+#define SMALLEST_VAL RANGE1_MIN
+
+static struct linear_range testr[] = {
+	{
+		.min = RANGE1_MIN,
+		.min_sel = RANGE1_MIN_SEL,
+		.max_sel = RANGE1_MAX_SEL,
+		.step = RANGE1_STEP,
+	}, {
+		.min = RANGE2_MIN,
+		.min_sel = RANGE2_MIN_SEL,
+		.max_sel = RANGE2_MAX_SEL,
+		.step = RANGE2_STEP
+	},
+};
+
+static void range_test_get_value(struct kunit *test)
+{
+	int ret, i;
+	unsigned int sel, val;
+
+	for (i = 0; i < RANGE1_NUM_VALS; i++) {
+		sel = range1_sels[i];
+		ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
+		KUNIT_EXPECT_EQ(test, 0, ret);
+		KUNIT_EXPECT_EQ(test, val, range1_vals[i]);
+	}
+	for (i = 0; i < RANGE2_NUM_VALS; i++) {
+		sel = range2_sels[i];
+		ret = linear_range_get_value_array(&testr[0], 2, sel, &val);
+		KUNIT_EXPECT_EQ(test, 0, ret);
+		KUNIT_EXPECT_EQ(test, val, range2_vals[i]);
+	}
+	ret = linear_range_get_value_array(&testr[0], 2, sel + 1, &val);
+	KUNIT_EXPECT_NE(test, 0, ret);
+}
+
+static void range_test_get_selector_high(struct kunit *test)
+{
+	int ret, i;
+	unsigned int sel;
+	bool found;
+
+	for (i = 0; i < RANGE1_NUM_VALS; i++) {
+		ret = linear_range_get_selector_high(&testr[0], range1_vals[i],
+						     &sel, &found);
+		KUNIT_EXPECT_EQ(test, 0, ret);
+		KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
+		KUNIT_EXPECT_TRUE(test, found);
+	}
+
+	ret = linear_range_get_selector_high(&testr[0], RANGE1_MAX_VAL + 1,
+					     &sel, &found);
+	KUNIT_EXPECT_LE(test, ret, 0);
+
+	ret = linear_range_get_selector_high(&testr[0], RANGE1_MIN - 1,
+					     &sel, &found);
+	KUNIT_EXPECT_EQ(test, 0, ret);
+	KUNIT_EXPECT_FALSE(test, found);
+	KUNIT_EXPECT_EQ(test, sel, range1_sels[0]);
+}
+
+static void range_test_get_value_amount(struct kunit *test)
+{
+	int ret;
+
+	ret = linear_range_values_in_range_array(&testr[0], 2);
+	KUNIT_EXPECT_EQ(test, (int)RANGE_NUM_VALS, ret);
+}
+
+static void range_test_get_selector_low(struct kunit *test)
+{
+	int i, ret;
+	unsigned int sel;
+	bool found;
+
+	for (i = 0; i < RANGE1_NUM_VALS; i++) {
+		ret = linear_range_get_selector_low_array(&testr[0], 2,
+							  range1_vals[i], &sel,
+							  &found);
+		KUNIT_EXPECT_EQ(test, 0, ret);
+		KUNIT_EXPECT_EQ(test, sel, range1_sels[i]);
+		KUNIT_EXPECT_TRUE(test, found);
+	}
+	for (i = 0; i < RANGE2_NUM_VALS; i++) {
+		ret = linear_range_get_selector_low_array(&testr[0], 2,
+							  range2_vals[i], &sel,
+							  &found);
+		KUNIT_EXPECT_EQ(test, 0, ret);
+		KUNIT_EXPECT_EQ(test, sel, range2_sels[i]);
+		KUNIT_EXPECT_TRUE(test, found);
+	}
+
+	/*
+	 * Seek value greater than range max => get_selector_*_low should
+	 * return Ok - but set found to false as value is not in range
+	 */
+	ret = linear_range_get_selector_low_array(&testr[0], 2,
+					range2_vals[RANGE2_NUM_VALS - 1] + 1,
+					&sel, &found);
+
+	KUNIT_EXPECT_EQ(test, 0, ret);
+	KUNIT_EXPECT_EQ(test, sel, range2_sels[RANGE2_NUM_VALS - 1]);
+	KUNIT_EXPECT_FALSE(test, found);
+}
+
+static struct kunit_case range_test_cases[] = {
+	KUNIT_CASE(range_test_get_value_amount),
+	KUNIT_CASE(range_test_get_selector_high),
+	KUNIT_CASE(range_test_get_selector_low),
+	KUNIT_CASE(range_test_get_value),
+	{},
+};
+
+static struct kunit_suite range_test_module = {
+	.name = "linear-ranges-test",
+	.test_cases = range_test_cases,
+};
+
+kunit_test_suites(&range_test_module);
+
+MODULE_LICENSE("GPL");
lib/Makefile renamed from lib/Makefile~lib-linear_ranges_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -350,6 +350,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
+obj-$(CONFIG_LINEAR_RANGES_TEST) += linear_ranges_kunit.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o
-obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
 obj-$(CONFIG_BITS_TEST) += test_bits.o
lib/test_linear_ranges.c deleted +0 -228

228 lines not on this page · raw message

MAINTAINERS renamed from MAINTAINERS~lib-linear_ranges_kunit-follow-new-file-name-convention-for-kunit-tests +1 -2
@@ -10144,8 +10144,7 @@ F:	include/uapi/linux/lightnvm.h
 LINEAR RANGES HELPERS
 M:	Mark Brown <[email protected]>
 R:	Matti Vaittinen <[email protected]>
-F:	lib/linear_ranges.c
-F:	lib/test_linear_ranges.c
+F:	lib/linear_ranges*.c
 F:	include/linux/linear_range.h
 
 LINUX FOR POWER MACINTOSH

_

author maintainer
#26 lore raw

PATCH 20/95 lib/bits_kunit: follow new file name convention for KUnit tests

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/bits_kunit: follow new file name convention for KUnit tests

Follow new file name convention for the KUnit tests. Since we have lib/*test*.c in a few variations, use 'kunit' suffix to distinguish usual test cases with KUnit-based ones.

---

 lib/Makefile     |    2 
 lib/bits_kunit.c |   75 ++++
 lib/list-test.c  |  748 ---------------------------------------------
 lib/list_kunit.c |  748 +++++++++++++++++++++++++++++++++++++++++++++
 lib/test_bits.c  |   75 ----
 5 files changed, 824 insertions(+), 824 deletions(-)
lib/bits_kunit.c new file +75 -0
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test cases for functions and macros in bits.h
+ */
+
+#include <kunit/test.h>
+#include <linux/bits.h>
+
+
+static void genmask_test(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1ul, GENMASK(0, 0));
+	KUNIT_EXPECT_EQ(test, 3ul, GENMASK(1, 0));
+	KUNIT_EXPECT_EQ(test, 6ul, GENMASK(2, 1));
+	KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, GENMASK(31, 0));
+
+#ifdef TEST_GENMASK_FAILURES
+	/* these should fail compilation */
+	GENMASK(0, 1);
+	GENMASK(0, 10);
+	GENMASK(9, 10);
+#endif
+
+
+}
+
+static void genmask_ull_test(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1ull, GENMASK_ULL(0, 0));
+	KUNIT_EXPECT_EQ(test, 3ull, GENMASK_ULL(1, 0));
+	KUNIT_EXPECT_EQ(test, 0x000000ffffe00000ull, GENMASK_ULL(39, 21));
+	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_ULL(63, 0));
+
+#ifdef TEST_GENMASK_FAILURES
+	/* these should fail compilation */
+	GENMASK_ULL(0, 1);
+	GENMASK_ULL(0, 10);
+	GENMASK_ULL(9, 10);
+#endif
+}
+
+static void genmask_input_check_test(struct kunit *test)
+{
+	unsigned int x, y;
+	int z, w;
+
+	/* Unknown input */
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, 0));
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, x));
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, y));
+
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, 0));
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, z));
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, w));
+
+	/* Valid input */
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(1, 1));
+	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(39, 21));
+}
+
+
+static struct kunit_case bits_test_cases[] = {
+	KUNIT_CASE(genmask_test),
+	KUNIT_CASE(genmask_ull_test),
+	KUNIT_CASE(genmask_input_check_test),
+	{}
+};
+
+static struct kunit_suite bits_test_suite = {
+	.name = "bits-test",
+	.test_cases = bits_test_cases,
+};
+kunit_test_suite(bits_test_suite);
+
+MODULE_LICENSE("GPL");
lib/list_kunit.c new file +748 -0

748 lines not on this page · raw message

lib/list-test.c deleted +0 -748

748 lines not on this page · raw message

lib/Makefile renamed from lib/Makefile~lib-bits_kunit-follow-new-file-name-convention-for-kunit-tests +1 -1
@@ -350,6 +350,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
+obj-$(CONFIG_BITS_TEST) += bits_kunit.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += linear_ranges_kunit.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o
-obj-$(CONFIG_BITS_TEST) += test_bits.o
lib/test_bits.c deleted +0 -75

75 lines not on this page · raw message

_

author maintainer
#27 lore raw

PATCH 21/95 lib/cmdline: fix get_option() for strings starting with hyphen

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/cmdline: fix get_option() for strings starting with hyphen

When string doesn't have an integer and starts from hyphen get_option() may return interesting results. Fix it to return 0.

The simple_strtoull() is used due to absence of simple_strtoul() in a boot code on some architectures.

Note, the Fixes tag below is rather for anthropological curiosity.

Fixes f68565831e72 ("Import 2.4.0-test2pre3")
Cc Brendan Higgins <[email protected]>
Cc David Gow <[email protected]>
Cc Mark Brown <[email protected]>
Cc Matti Vaittinen <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Vitor Massaru Iha <[email protected]>

---

 lib/cmdline.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/cmdline.c renamed from lib/cmdline.c~lib-cmdline-fix-get_option-for-strings-starting-with-hyphen +7 -1
@@ -45,6 +45,9 @@ static int get_range(char **str, int *pi
  *	1 - int found, no subsequent comma
  *	2 - int found including a subsequent comma
  *	3 - hyphen found to denote a range
+ *
+ *	Leading hyphen without integer is no integer case, but we consume it
+ *	for the sake of simplification.
  */
 
 int get_option(char **str, int *pint)
@@ -53,7 +56,10 @@ int get_option(char **str, int *pint)
 
 	if (!cur || !(*cur))
 		return 0;
-	*pint = simple_strtol(cur, str, 0);
+	if (*cur == '-')
+		*pint = -simple_strtoull(++cur, str, 0);
+	else
+		*pint = simple_strtoull(cur, str, 0);
 	if (cur == *str)
 		return 0;
 	if (**str == ',') {

_

author maintainer
#28 lore raw

PATCH 22/95 lib/cmdline: allow NULL to be an output for get_option()

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/cmdline: allow NULL to be an output for get_option()

In the future we would like to use get_option() to only validate the string and parse it separately. To achieve this, allow NULL to be an output for get_option().

---

 lib/cmdline.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
lib/cmdline.c renamed from lib/cmdline.c~lib-cmdline-allow-null-to-be-an-output-for-get_option +9 -3
@@ -35,11 +35,14 @@ static int get_range(char **str, int *pi
 /**
  *	get_option - Parse integer from an option string
  *	@str: option string
- *	@pint: (output) integer value parsed from @str
+ *	@pint: (optional output) integer value parsed from @str
  *
  *	Read an int from an option string; if available accept a subsequent
  *	comma as well.
  *
+ *	When @pint is NULL the function can be used as a validator of
+ *	the current option in the string.
+ *
  *	Return values:
  *	0 - no int in string
  *	1 - int found, no subsequent comma
@@ -53,13 +56,16 @@ static int get_range(char **str, int *pi
 int get_option(char **str, int *pint)
 {
 	char *cur = *str;
+	int value;
 
 	if (!cur || !(*cur))
 		return 0;
 	if (*cur == '-')
-		*pint = -simple_strtoull(++cur, str, 0);
+		value = -simple_strtoull(++cur, str, 0);
 	else
-		*pint = simple_strtoull(cur, str, 0);
+		value = simple_strtoull(cur, str, 0);
+	if (pint)
+		*pint = value;
 	if (cur == *str)
 		return 0;
 	if (**str == ',') {

_

author maintainer
#29 lore raw

PATCH 23/95 lib/cmdline_kunit: add a new test suite for cmdline API

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: lib/cmdline_kunit: add a new test suite for cmdline API

Test get_option() for a starter which is provided by cmdline.c.

[[email protected]: fix warning by constifying cmdline_test_values]
[[email protected]: type of expected returned values should be int]
[[email protected]: provide meaningful MODULE_LICENSE()]
Cc Shuah Khan <[email protected]>
Cc Vitor Massaru Iha <[email protected]>
Cc Mark Brown <[email protected]>
Cc Brendan Higgins <[email protected]>
Cc David Gow <[email protected]>
Cc Matti Vaittinen <[email protected]>

---

 lib/Kconfig.debug   |   11 ++++
 lib/Makefile        |    1 
 lib/cmdline_kunit.c |  100 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
lib/cmdline_kunit.c new file +100 -0
@@ -0,0 +1,100 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test cases for API provided by cmdline.c
+ */
+
+#include <kunit/test.h>
+#include <linux/kernel.h>
+#include <linux/random.h>
+#include <linux/string.h>
+
+static const char *cmdline_test_strings[] = {
+	"\"\"", ""  , "=" , "\"-", ","    , "-,"   , ",-"   , "-" ,
+	"+,"  , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
+};
+
+static const int cmdline_test_values[] = {
+	1, 1, 1, 1, 2, 3, 2, 3,
+	1, 3, 2, 1, 1, 1, 3, 1,
+};
+
+static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
+{
+	const char *fmt = "Pattern: %s";
+	const char *out = in;
+	int dummy;
+	int ret;
+
+	ret = get_option((char **)&out, &dummy);
+
+	KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
+	KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
+}
+
+static void cmdline_test_noint(struct kunit *test)
+{
+	unsigned int i = 0;
+
+	do {
+		const char *str = cmdline_test_strings[i];
+		int rc = 0;
+		int offset;
+
+		/* Only first and leading '-' will advance the pointer */
+		offset = !!(*str == '-');
+		cmdline_do_one_test(test, str, rc, offset);
+	} while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static void cmdline_test_lead_int(struct kunit *test)
+{
+	unsigned int i = 0;
+	char in[32];
+
+	do {
+		const char *str = cmdline_test_strings[i];
+		int rc = cmdline_test_values[i];
+		int offset;
+
+		sprintf(in, "%u%s", get_random_int() % 256, str);
+		/* Only first '-' after the number will advance the pointer */
+		offset = strlen(in) - strlen(str) + !!(rc == 2);
+		cmdline_do_one_test(test, in, rc, offset);
+	} while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static void cmdline_test_tail_int(struct kunit *test)
+{
+	unsigned int i = 0;
+	char in[32];
+
+	do {
+		const char *str = cmdline_test_strings[i];
+		/* When "" or "-" the result will be valid integer */
+		int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
+		int offset;
+
+		sprintf(in, "%s%u", str, get_random_int() % 256);
+		/*
+		 * Only first and leading '-' not followed by integer
+		 * will advance the pointer.
+		 */
+		offset = rc ? strlen(in) : !!(*str == '-');
+		cmdline_do_one_test(test, in, rc, offset);
+	} while (++i < ARRAY_SIZE(cmdline_test_strings));
+}
+
+static struct kunit_case cmdline_test_cases[] = {
+	KUNIT_CASE(cmdline_test_noint),
+	KUNIT_CASE(cmdline_test_lead_int),
+	KUNIT_CASE(cmdline_test_tail_int),
+	{}
+};
+
+static struct kunit_suite cmdline_test_suite = {
+	.name = "cmdline",
+	.test_cases = cmdline_test_cases,
+};
+kunit_test_suite(cmdline_test_suite);
+
+MODULE_LICENSE("GPL");
lib/Kconfig.debug renamed from lib/Kconfig.debug~lib-cmdline_kunit-add-a-new-test-suite-for-cmdline-api +11 -0
@@ -2311,6 +2311,17 @@ config LINEAR_RANGES_TEST
 
 	  If unsure, say N.
 
+config CMDLINE_KUNIT_TEST
+	tristate "KUnit test for cmdline API"
+	depends on KUNIT
+	help
+	  This builds the cmdline API unit test.
+	  Tests the logic of API provided by cmdline.c.
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config BITS_TEST
 	tristate "KUnit test for bits.h"
 	depends on KUNIT
lib/Makefile renamed from lib/Makefile~lib-cmdline_kunit-add-a-new-test-suite-for-cmdline-api +1 -0
@@ -351,5 +351,6 @@ obj-$(CONFIG_PLDMFW) += pldmfw/
 # KUnit tests
 obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
 obj-$(CONFIG_BITS_TEST) += bits_kunit.o
+obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += linear_ranges_kunit.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list_kunit.o

_

author maintainer
#30 lore raw

PATCH 24/95 ilog2: improve ilog2 for constant arguments

In reply to Andrew Morton

From: Jakub Jelinek <[email protected]>
Subject: ilog2: improve ilog2 for constant arguments

As discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97445 the const_ilog2 macro generates a lot of code which interferes badly with GCC inlining heuristics, until it can be proven that the ilog2 argument can or can't be simplified into a constant.

It can be expressed using __builtin_clzll builtin which is supported by GCC 3.4 and later and when used only in the __builtin_constant_p guarded code it ought to always fold back to a constant. Other compilers support the same builtin for many years too.

Other option would be to change the const_ilog2 macro, though as the description says it is meant to be used also in C constant expressions, and while GCC will fold it to constant with constant argument even in those, perhaps it is better to avoid using extensions in that case.

---

 include/linux/log2.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/log2.h renamed from include/linux/log2.h~ilog2-improve-ilog2-for-constant-arguments +2 -1
@@ -156,7 +156,8 @@ unsigned long __rounddown_pow_of_two(uns
 #define ilog2(n) \
 ( \
 	__builtin_constant_p(n) ?	\
-	const_ilog2(n) :		\
+	((n) < 2 ? 0 :			\
+	 63 - __builtin_clzll(n)) :	\
 	(sizeof(n) <= 4) ?		\
 	__ilog2_u32(n) :		\
 	__ilog2_u64(n)			\

_

author maintainer
#31 lore raw

PATCH 25/95 lib/string: remove unnecessary #undefs

In reply to Andrew Morton

From: Nick Desaulniers <[email protected]>
Subject: lib/string: remove unnecessary #undefs

A few architecture specific string.h functions used to be implemented in terms of preprocessor defines to the corresponding compiler builtins. Since this is no longer the case, remove unused #undefs.

Only memcmp is still defined in terms of builtins for a few arches.

Fixes 5f074f3e192f ("lib/string.c: implement a basic bcmp")
Cc Andy Shevchenko <[email protected]>
Cc Yury Norov <[email protected]>
Cc Alexandru Ardelean <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Masahiro Yamada <[email protected]>

---

 lib/string.c |    4 ----
 1 file changed, 4 deletions(-)
lib/string.c renamed from lib/string.c~lib-string-remove-unnecessary-undefs +0 -4
@@ -85,7 +85,6 @@ EXPORT_SYMBOL(strcasecmp);
  * @dest: Where to copy the string to
  * @src: Where to copy the string from
  */
-#undef strcpy
 char *strcpy(char *dest, const char *src)
 {
 	char *tmp = dest;
@@ -302,7 +301,6 @@ EXPORT_SYMBOL(stpcpy);
  * @dest: The string to be appended to
  * @src: The string to append to it
  */
-#undef strcat
 char *strcat(char *dest, const char *src)
 {
 	char *tmp = dest;
@@ -378,7 +376,6 @@ EXPORT_SYMBOL(strlcat);
  * @cs: One string
  * @ct: Another string
  */
-#undef strcmp
 int strcmp(const char *cs, const char *ct)
 {
 	unsigned char c1, c2;
@@ -958,7 +955,6 @@ EXPORT_SYMBOL(memcmp);
  * while this particular implementation is a simple (tail) call to memcmp, do
  * not rely on anything but whether the return value is zero or non-zero.
  */
-#undef bcmp
 int bcmp(const void *a, const void *b, size_t len)
 {
 	return memcmp(a, b, len);

_

author maintainer
#32 lore raw

PATCH 26/95 lib: string.h: detect intra-object overflow in fortified string functions

In reply to Andrew Morton

From: Daniel Axtens <[email protected]>
Subject: lib: string.h: detect intra-object overflow in fortified string functions

Patch series "Fortify strscpy()", v7.

This patch implements a fortified version of strscpy() enabled by setting CONFIG_FORTIFY_SOURCE=y. The new version ensures the following before calling vanilla strscpy():

1. There is no read overflow because either size is smaller than src length or we shrink size to src length by calling fortified strnlen().

2. There is no write overflow because we either failed during compilation or at runtime by checking that size is smaller than dest size. Note that, if src and dst size cannot be got, the patch defaults to call vanilla strscpy().

The patches adds the following:

1. Implement the fortified version of strscpy().

2. Add a new LKDTM test to ensures the fortified version still returns the same value as the vanilla one while panic'ing when there is a write overflow.

3. Correct some typos in LKDTM related file.

I based my modifications on top of two patches from Daniel Axtens which modify calls to __builtin_object_size, in fortified string functions, to ensure the true size of char * are returned and not the surrounding structure size.

About performance, I measured the slow down of fortified strscpy(), using the vanilla one as baseline. The hardware I used is an Intel i3 2130 CPU clocked at 3.4 GHz. I ran "Linux 5.10.0-rc4+ SMP PREEMPT" inside qemu 3.10 with 4 CPU cores. The following code, called through LKDTM, was used as a benchmark:

#define TIMES 10000
	char *src;
	char dst[7];
	int i;
	ktime_t begin;

	src = kstrdup("foobar", GFP_KERNEL);

	if (src == NULL)
		return;

	begin = ktime_get();
	for (i = 0; i < TIMES; i++)
		strscpy(dst, src, strlen(src));
	pr_info("%d fortified strscpy() tooks %lld", TIMES, ktime_get() - begin);

	begin = ktime_get();
	for (i = 0; i < TIMES; i++)
		__real_strscpy(dst, src, strlen(src));
	pr_info("%d vanilla strscpy() tooks %lld", TIMES, ktime_get() - begin);

	kfree(src);

I called the above code 30 times to compute stats for each version (in ns, round to int):

| version   | mean    | std    | median  | 95th    |
| --------- | ------- | ------ | ------- | ------- |
| fortified | 245_069 | 54_657 | 216_230 | 331_122 |
| vanilla   | 172_501 | 70_281 | 143_539 | 219_553 |

On average, fortified strscpy() is approximately 1.42 times slower than vanilla strscpy(). For the 95th percentile, the fortified version is about 1.50 times slower.

So, clearly the stats are not in favor of fortified strscpy(). But, the fortified version loops the string twice (one in strnlen() and another in vanilla strscpy()) while the vanilla one only loops once. This can explain why fortified strscpy() is slower than the vanilla one.

This patch (of 5):

When the fortify feature was first introduced in commit 6974f0c4555e ("include/linux/string.h: add the option of fortified string.h functions"), Daniel Micay observed:

* It should be possible to optionally use __builtin_object_size(x, 1) for some functions (C strings) to detect intra-object overflows (like glibc's _FORTIFY_SOURCE=2), but for now this takes the conservative approach to avoid likely compatibility issues.

This is a case that often cannot be caught by KASAN. Consider:

struct foo {
    char a[10];
    char b[10];
}
void test() {
    char *msg;
    struct foo foo;

    msg = kmalloc(16, GFP_KERNEL);
    strcpy(msg, "Hello world!!");
    // this copy overwrites foo.b
    strcpy(foo.a, msg);
}

The questionable copy overflows foo.a and writes to foo.b as well. It cannot be detected by KASAN. Currently it is also not detected by fortify, because strcpy considers __builtin_object_size(x, 0), which considers the size of the surrounding object (here, struct foo). However, if we switch the string functions over to use __builtin_object_size(x, 1), the compiler will measure the size of the closest surrounding subobject (here, foo.a), rather than the size of the surrounding object as a whole. See https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html for more info.

Only do this for string functions: we cannot use it on things like memcpy, memmove, memcmp and memchr_inv due to code like this which purposefully operates on multiple structure members: (arch/x86/kernel/traps.c)

	/*
	 * regs->sp points to the failing IRET frame on the
	 * ESPFIX64 stack.  Copy it to the entry stack.  This fills
	 * in gpregs->ss through gpregs->ip.
	 *
	 */
	memmove(&gpregs->ip, (void *)regs->sp, 5*8);

This change passes an allyesconfig on powerpc and x86, and an x86 kernel built with it survives running with syz-stress from syzkaller, so it seems safe so far.

---

 include/linux/string.h |   27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
include/linux/string.h renamed from include/linux/string.h~stringh-detect-intra-object-overflow-in-fortified-string-functions +16 -11
@@ -292,7 +292,7 @@ extern char *__underlying_strncpy(char *
 
 __FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size)
 {
-	size_t p_size = __builtin_object_size(p, 0);
+	size_t p_size = __builtin_object_size(p, 1);
 	if (__builtin_constant_p(size) && p_size < size)
 		__write_overflow();
 	if (p_size < size)
@@ -302,7 +302,7 @@ __FORTIFY_INLINE char *strncpy(char *p,
 
 __FORTIFY_INLINE char *strcat(char *p, const char *q)
 {
-	size_t p_size = __builtin_object_size(p, 0);
+	size_t p_size = __builtin_object_size(p, 1);
 	if (p_size == (size_t)-1)
 		return __underlying_strcat(p, q);
 	if (strlcat(p, q, p_size) >= p_size)
@@ -313,7 +313,7 @@ __FORTIFY_INLINE char *strcat(char *p, c
 __FORTIFY_INLINE __kernel_size_t strlen(const char *p)
 {
 	__kernel_size_t ret;
-	size_t p_size = __builtin_object_size(p, 0);
+	size_t p_size = __builtin_object_size(p, 1);
 
 	/* Work around gcc excess stack consumption issue */
 	if (p_size == (size_t)-1 ||
@@ -328,7 +328,7 @@ __FORTIFY_INLINE __kernel_size_t strlen(
 extern __kernel_size_t __real_strnlen(const char *, __kernel_size_t) __RENAME(strnlen);
 __FORTIFY_INLINE __kernel_size_t strnlen(const char *p, __kernel_size_t maxlen)
 {
-	size_t p_size = __builtin_object_size(p, 0);
+	size_t p_size = __builtin_object_size(p, 1);
 	__kernel_size_t ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
 	if (p_size <= ret && maxlen != ret)
 		fortify_panic(__func__);
@@ -340,8 +340,8 @@ extern size_t __real_strlcpy(char *, con
 __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
 {
 	size_t ret;
-	size_t p_size = __builtin_object_size(p, 0);
-	size_t q_size = __builtin_object_size(q, 0);
+	size_t p_size = __builtin_object_size(p, 1);
+	size_t q_size = __builtin_object_size(q, 1);
 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
 		return __real_strlcpy(p, q, size);
 	ret = strlen(q);
@@ -361,8 +361,8 @@ __FORTIFY_INLINE size_t strlcpy(char *p,
 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
 {
 	size_t p_len, copy_len;
-	size_t p_size = __builtin_object_size(p, 0);
-	size_t q_size = __builtin_object_size(q, 0);
+	size_t p_size = __builtin_object_size(p, 1);
+	size_t q_size = __builtin_object_size(q, 1);
 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
 		return __underlying_strncat(p, q, count);
 	p_len = strlen(p);
@@ -475,11 +475,16 @@ __FORTIFY_INLINE void *kmemdup(const voi
 /* defined after fortified strlen and memcpy to reuse them */
 __FORTIFY_INLINE char *strcpy(char *p, const char *q)
 {
-	size_t p_size = __builtin_object_size(p, 0);
-	size_t q_size = __builtin_object_size(q, 0);
+	size_t p_size = __builtin_object_size(p, 1);
+	size_t q_size = __builtin_object_size(q, 1);
+	size_t size;
 	if (p_size == (size_t)-1 && q_size == (size_t)-1)
 		return __underlying_strcpy(p, q);
-	memcpy(p, q, strlen(q) + 1);
+	size = strlen(q) + 1;
+	/* test here to use the more stringent object size */
+	if (p_size < size)
+		fortify_panic(__func__);
+	memcpy(p, q, size);
 	return p;
 }
 

_

author maintainer
#33 lore raw

PATCH 27/95 lkdtm: tests for FORTIFY_SOURCE

In reply to Andrew Morton

From: Daniel Axtens <[email protected]>
Subject: lkdtm: tests for FORTIFY_SOURCE

Add code to test both:

- runtime detection of the overrun of a structure. This covers the __builtin_object_size(x, 0) case. This test is called FORTIFY_OBJECT.

- runtime detection of the overrun of a char array within a structure. This covers the __builtin_object_size(x, 1) case which can be used for some string functions. This test is called FORTIFY_SUBOBJECT.

---

 drivers/misc/lkdtm/bugs.c  |   50 +++++++++++++++++++++++++++++++++++
 drivers/misc/lkdtm/core.c  |    2 +
 drivers/misc/lkdtm/lkdtm.h |    2 +
 3 files changed, 54 insertions(+)
drivers/misc/lkdtm/bugs.c renamed from drivers/misc/lkdtm/bugs.c~lkdtm-tests-for-fortify_source +50 -0
@@ -482,3 +482,53 @@ noinline void lkdtm_CORRUPT_PAC(void)
 	pr_err("XFAIL: this test is arm64-only\n");
 #endif
 }
+
+void lkdtm_FORTIFY_OBJECT(void)
+{
+	struct target {
+		char a[10];
+	} target[2] = {};
+	int result;
+
+	/*
+	 * Using volatile prevents the compiler from determining the value of
+	 * 'size' at compile time. Without that, we would get a compile error
+	 * rather than a runtime error.
+	 */
+	volatile int size = 11;
+
+	pr_info("trying to read past the end of a struct\n");
+
+	result = memcmp(&target[0], &target[1], size);
+
+	/* Print result to prevent the code from being eliminated */
+	pr_err("FAIL: fortify did not catch an object overread!\n"
+	       "\"%d\" was the memcmp result.\n", result);
+}
+
+void lkdtm_FORTIFY_SUBOBJECT(void)
+{
+	struct target {
+		char a[10];
+		char b[10];
+	} target;
+	char *src;
+
+	src = kmalloc(20, GFP_KERNEL);
+	strscpy(src, "over ten bytes", 20);
+
+	pr_info("trying to strcpy past the end of a member of a struct\n");
+
+	/*
+	 * strncpy(target.a, src, 20); will hit a compile error because the
+	 * compiler knows at build time that target.a < 20 bytes. Use strcpy()
+	 * to force a runtime error.
+	 */
+	strcpy(target.a, src);
+
+	/* Use target.a to prevent the code from being eliminated */
+	pr_err("FAIL: fortify did not catch an sub-object overrun!\n"
+	       "\"%s\" was copied.\n", target.a);
+
+	kfree(src);
+}
drivers/misc/lkdtm/core.c renamed from drivers/misc/lkdtm/core.c~lkdtm-tests-for-fortify_source +2 -0
@@ -117,6 +117,8 @@ static const struct crashtype crashtypes
 	CRASHTYPE(UNSET_SMEP),
 	CRASHTYPE(CORRUPT_PAC),
 	CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
+	CRASHTYPE(FORTIFY_OBJECT),
+	CRASHTYPE(FORTIFY_SUBOBJECT),
 	CRASHTYPE(OVERWRITE_ALLOCATION),
 	CRASHTYPE(WRITE_AFTER_FREE),
 	CRASHTYPE(READ_AFTER_FREE),
drivers/misc/lkdtm/lkdtm.h renamed from drivers/misc/lkdtm/lkdtm.h~lkdtm-tests-for-fortify_source +2 -0
@@ -32,6 +32,8 @@ void lkdtm_STACK_GUARD_PAGE_TRAILING(voi
 void lkdtm_UNSET_SMEP(void);
 void lkdtm_DOUBLE_FAULT(void);
 void lkdtm_CORRUPT_PAC(void);
+void lkdtm_FORTIFY_OBJECT(void);
+void lkdtm_FORTIFY_SUBOBJECT(void);
 
 /* lkdtm_heap.c */
 void __init lkdtm_heap_init(void);

_

author maintainer
#34 lore raw

PATCH 28/95 string.h: add FORTIFY coverage for strscpy()

In reply to Andrew Morton

From: Francis Laniel <[email protected]>
Subject: string.h: add FORTIFY coverage for strscpy()

The fortified version of strscpy ensures the following before vanilla strscpy is called:

1. There is no read overflow because we either size is smaller than src length or we shrink size to src length by calling fortified strnlen.

2. There is no write overflow because we either failed during compilation or at runtime by checking that size is smaller than dest size.

---

 include/linux/string.h |   48 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
include/linux/string.h renamed from include/linux/string.h~stringh-add-fortify-coverage-for-strscpy +48 -0
@@ -6,6 +6,7 @@
 #include <linux/compiler.h>	/* for inline */
 #include <linux/types.h>	/* for size_t */
 #include <linux/stddef.h>	/* for NULL */
+#include <linux/errno.h>	/* for E2BIG */
 #include <stdarg.h>
 #include <uapi/linux/string.h>
 
@@ -357,6 +358,53 @@ __FORTIFY_INLINE size_t strlcpy(char *p,
 	return ret;
 }
 
+/* defined after fortified strnlen to reuse it */
+extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
+__FORTIFY_INLINE ssize_t strscpy(char *p, const char *q, size_t size)
+{
+	size_t len;
+	/* Use string size rather than possible enclosing struct size. */
+	size_t p_size = __builtin_object_size(p, 1);
+	size_t q_size = __builtin_object_size(q, 1);
+
+	/* If we cannot get size of p and q default to call strscpy. */
+	if (p_size == (size_t) -1 && q_size == (size_t) -1)
+		return __real_strscpy(p, q, size);
+
+	/*
+	 * If size can be known at compile time and is greater than
+	 * p_size, generate a compile time write overflow error.
+	 */
+	if (__builtin_constant_p(size) && size > p_size)
+		__write_overflow();
+
+	/*
+	 * This call protects from read overflow, because len will default to q
+	 * length if it smaller than size.
+	 */
+	len = strnlen(q, size);
+	/*
+	 * If len equals size, we will copy only size bytes which leads to
+	 * -E2BIG being returned.
+	 * Otherwise we will copy len + 1 because of the final '\O'.
+	 */
+	len = len == size ? size : len + 1;
+
+	/*
+	 * Generate a runtime write overflow error if len is greater than
+	 * p_size.
+	 */
+	if (len > p_size)
+		fortify_panic(__func__);
+
+	/*
+	 * We can now safely call vanilla strscpy because we are protected from:
+	 * 1. Read overflow thanks to call to strnlen().
+	 * 2. Write overflow thanks to above ifs.
+	 */
+	return __real_strscpy(p, q, len);
+}
+
 /* defined after fortified strlen and strnlen to reuse them */
 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
 {

_

#35 lore raw

Re: [patch 28/95] string.h: add FORTIFY coverage for strscpy()

Quoted 5 lines · Andrew Morton

From: Francis Laniel <[email protected]>
Subject: string.h: add FORTIFY coverage for strscpy()

The fortified version of strscpy ensures the following [..]

So I've taken this, because it follows the pattern of what we already have, but I have to say that the "fortify" stuff is now about half of string.h.

I think it should probably be split out into its own header file, and then string.h could just do

#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) &&
defined(CONFIG_FORTIFY_SOURCE)

     #include <linux/fortify-string.h>

#endif

or similar.

That won't help my merge window build times (I build with allmodconfig, so I'll have CONFIG_FORTIFY_SOURCE=y and so my poor compiler will end up always going off and seeing all that every time somebody glances at string.h), but I think it would be a cleanup regardless.

And people who _don't_ build with FORTIFY_SOURCE wouldn't have to read and parse that big chunk every time <linux/string.h> gets included.

(And yes, just reading the source file and tokenizing it - even if it's all inside an #ifdef that turns off all the other phases - is actually quite noticeable overhead in a C compiler. Surprisingly so)

Linus

author maintainer
#36 lore raw

PATCH 29/95 drivers/misc/lkdtm: add new file in LKDTM to test fortified strscpy

In reply to Andrew Morton

From: Francis Laniel <[email protected]>
Subject: drivers/misc/lkdtm: add new file in LKDTM to test fortified strscpy

This new test ensures that fortified strscpy has the same behavior than vanilla strscpy (e.g. returning -E2BIG when src content is truncated). Finally, it generates a crash at runtime because there is a write overflow in destination string.

---

 drivers/misc/lkdtm/Makefile             |    1 
 drivers/misc/lkdtm/core.c               |    1 
 drivers/misc/lkdtm/fortify.c            |   82 ++++++++++++++++++++++
 drivers/misc/lkdtm/lkdtm.h              |    3 
 tools/testing/selftests/lkdtm/tests.txt |    1 
 5 files changed, 88 insertions(+)
drivers/misc/lkdtm/core.c renamed from drivers/misc/lkdtm/core.c~add-new-file-in-lkdtm-to-test-fortified-strscpy +1 -0
@@ -175,6 +175,7 @@ static const struct crashtype crashtypes
 	CRASHTYPE(USERCOPY_KERNEL),
 	CRASHTYPE(STACKLEAK_ERASING),
 	CRASHTYPE(CFI_FORWARD_PROTO),
+	CRASHTYPE(FORTIFIED_STRSCPY),
 #ifdef CONFIG_X86_32
 	CRASHTYPE(DOUBLE_FAULT),
 #endif
drivers/misc/lkdtm/fortify.c new file +82 -0

82 lines not on this page · raw message

drivers/misc/lkdtm/lkdtm.h renamed from drivers/misc/lkdtm/lkdtm.h~add-new-file-in-lkdtm-to-test-fortified-strscpy +3 -0
@@ -104,4 +104,7 @@ void lkdtm_STACKLEAK_ERASING(void);
 /* cfi.c */
 void lkdtm_CFI_FORWARD_PROTO(void);
 
+/* fortify.c */
+void lkdtm_FORTIFIED_STRSCPY(void);
+
 #endif
drivers/misc/lkdtm/Makefile renamed from drivers/misc/lkdtm/Makefile~add-new-file-in-lkdtm-to-test-fortified-strscpy +1 -0
@@ -10,6 +10,7 @@ lkdtm-$(CONFIG_LKDTM)		+= rodata_objcopy
 lkdtm-$(CONFIG_LKDTM)		+= usercopy.o
 lkdtm-$(CONFIG_LKDTM)		+= stackleak.o
 lkdtm-$(CONFIG_LKDTM)		+= cfi.o
+lkdtm-$(CONFIG_LKDTM)		+= fortify.o
 
 KASAN_SANITIZE_rodata.o		:= n
 KASAN_SANITIZE_stackleak.o	:= n
tools/testing/selftests/lkdtm/tests.txt renamed from tools/testing/selftests/lkdtm/tests.txt~add-new-file-in-lkdtm-to-test-fortified-strscpy +1 -0
@@ -68,3 +68,4 @@ USERCOPY_STACK_BEYOND
 USERCOPY_KERNEL
 STACKLEAK_ERASING OK: the rest of the thread stack is properly erased
 CFI_FORWARD_PROTO
+FORTIFIED_STRSCPY

_

author maintainer
#37 lore raw

PATCH 30/95 drivers/misc/lkdtm/lkdtm.h: correct wrong filenames in comment

In reply to Andrew Morton

From: Francis Laniel <[email protected]>
Subject: drivers/misc/lkdtm/lkdtm.h: correct wrong filenames in comment

In lkdtm.h, files targeted in comments are named "lkdtm_file.c" while there are named "file.c" in directory.

---

 drivers/misc/lkdtm/lkdtm.h |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/misc/lkdtm/lkdtm.h renamed from drivers/misc/lkdtm/lkdtm.h~correct-wrong-filenames-in-comment +7 -7
@@ -6,7 +6,7 @@
 
 #include <linux/kernel.h>
 
-/* lkdtm_bugs.c */
+/* bugs.c */
 void __init lkdtm_bugs_init(int *recur_param);
 void lkdtm_PANIC(void);
 void lkdtm_BUG(void);
@@ -35,7 +35,7 @@ void lkdtm_CORRUPT_PAC(void);
 void lkdtm_FORTIFY_OBJECT(void);
 void lkdtm_FORTIFY_SUBOBJECT(void);
 
-/* lkdtm_heap.c */
+/* heap.c */
 void __init lkdtm_heap_init(void);
 void __exit lkdtm_heap_exit(void);
 void lkdtm_OVERWRITE_ALLOCATION(void);
@@ -47,7 +47,7 @@ void lkdtm_SLAB_FREE_DOUBLE(void);
 void lkdtm_SLAB_FREE_CROSS(void);
 void lkdtm_SLAB_FREE_PAGE(void);
 
-/* lkdtm_perms.c */
+/* perms.c */
 void __init lkdtm_perms_init(void);
 void lkdtm_WRITE_RO(void);
 void lkdtm_WRITE_RO_AFTER_INIT(void);
@@ -62,7 +62,7 @@ void lkdtm_EXEC_NULL(void);
 void lkdtm_ACCESS_USERSPACE(void);
 void lkdtm_ACCESS_NULL(void);
 
-/* lkdtm_refcount.c */
+/* refcount.c */
 void lkdtm_REFCOUNT_INC_OVERFLOW(void);
 void lkdtm_REFCOUNT_ADD_OVERFLOW(void);
 void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void);
@@ -83,10 +83,10 @@ void lkdtm_REFCOUNT_SUB_AND_TEST_SATURAT
 void lkdtm_REFCOUNT_TIMING(void);
 void lkdtm_ATOMIC_TIMING(void);
 
-/* lkdtm_rodata.c */
+/* rodata.c */
 void lkdtm_rodata_do_nothing(void);
 
-/* lkdtm_usercopy.c */
+/* usercopy.c */
 void __init lkdtm_usercopy_init(void);
 void __exit lkdtm_usercopy_exit(void);
 void lkdtm_USERCOPY_HEAP_SIZE_TO(void);
@@ -98,7 +98,7 @@ void lkdtm_USERCOPY_STACK_FRAME_FROM(voi
 void lkdtm_USERCOPY_STACK_BEYOND(void);
 void lkdtm_USERCOPY_KERNEL(void);
 
-/* lkdtm_stackleak.c */
+/* stackleak.c */
 void lkdtm_STACKLEAK_ERASING(void);
 
 /* cfi.c */

_

author maintainer
#38 lore raw

PATCH 31/95 lib: cleanup kstrto*() usage

In reply to Andrew Morton

From: Alexey Dobriyan <[email protected]>
Subject: lib: cleanup kstrto*() usage

Use proper conversion functions. kstrto*() variants exist for all standard types.

---

 lib/test_firmware.c |    9 +++------
 lib/test_kmod.c     |   26 ++++++++++----------------
 2 files changed, 13 insertions(+), 22 deletions(-)
lib/test_firmware.c renamed from lib/test_firmware.c~lib-cleanup-kstrto-usage +3 -6
@@ -364,18 +364,15 @@ static ssize_t test_dev_config_show_int(
 
 static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
 {
+	u8 val;
 	int ret;
-	long new;
 
-	ret = kstrtol(buf, 10, &new);
+	ret = kstrtou8(buf, 10, &val);
 	if (ret)
 		return ret;
 
-	if (new > U8_MAX)
-		return -EINVAL;
-
 	mutex_lock(&test_fw_mutex);
-	*(u8 *)cfg = new;
+	*(u8 *)cfg = val;
 	mutex_unlock(&test_fw_mutex);
 
 	/* Always return full write size even if we didn't consume all */
lib/test_kmod.c renamed from lib/test_kmod.c~lib-cleanup-kstrto-usage +10 -16
@@ -877,20 +877,17 @@ static int test_dev_config_update_uint_s
 					    int (*test_sync)(struct kmod_test_device *test_dev))
 {
 	int ret;
-	unsigned long new;
+	unsigned int val;
 	unsigned int old_val;
 
-	ret = kstrtoul(buf, 10, &new);
+	ret = kstrtouint(buf, 10, &val);
 	if (ret)
 		return ret;
 
-	if (new > UINT_MAX)
-		return -EINVAL;
-
 	mutex_lock(&test_dev->config_mutex);
 
 	old_val = *config;
-	*(unsigned int *)config = new;
+	*(unsigned int *)config = val;
 
 	ret = test_sync(test_dev);
 	if (ret) {
@@ -914,18 +911,18 @@ static int test_dev_config_update_uint_r
 					     unsigned int min,
 					     unsigned int max)
 {
+	unsigned int val;
 	int ret;
-	unsigned long new;
 
-	ret = kstrtoul(buf, 10, &new);
+	ret = kstrtouint(buf, 10, &val);
 	if (ret)
 		return ret;
 
-	if (new < min || new > max)
+	if (val < min || val > max)
 		return -EINVAL;
 
 	mutex_lock(&test_dev->config_mutex);
-	*config = new;
+	*config = val;
 	mutex_unlock(&test_dev->config_mutex);
 
 	/* Always return full write size even if we didn't consume all */
@@ -936,18 +933,15 @@ static int test_dev_config_update_int(st
 				      const char *buf, size_t size,
 				      int *config)
 {
+	int val;
 	int ret;
-	long new;
 
-	ret = kstrtol(buf, 10, &new);
+	ret = kstrtoint(buf, 10, &val);
 	if (ret)
 		return ret;
 
-	if (new < INT_MIN || new > INT_MAX)
-		return -EINVAL;
-
 	mutex_lock(&test_dev->config_mutex);
-	*config = new;
+	*config = val;
 	mutex_unlock(&test_dev->config_mutex);
 	/* Always return full write size even if we didn't consume all */
 	return size;

_

author maintainer
#39 lore raw

PATCH 32/95 lib/lz4: explicitly support in-place decompression

In reply to Andrew Morton

From: Gao Xiang <[email protected]>
Subject: lib/lz4: explicitly support in-place decompression

LZ4 final literal copy could be overlapped when doing in-place decompression, so it's unsafe to just use memcpy() on an optimized memcpy approach but memmove() instead.

Upstream LZ4 has updated this years ago [1] (and the impact is non-sensible [2] plus only a few bytes remain), this commit just synchronizes LZ4 upstream code to the kernel side as well.

It can be observed as EROFS in-place decompression failure on specific files when X86_FEATURE_ERMS is unsupported, memcpy() optimization of commit 59daa706fbec ("x86, mem:
Optimize memcpy by avoiding memory false dependece") will be enabled then.

Currently most modern x86-CPUs support ERMS, these CPUs just use "rep movsb" approach so no problem at all. However, it can still be verified with forcely disabling ERMS feature...

arch/x86/lib/memcpy_64.S:

        ALTERNATIVE_2 "jmp memcpy_orig", "", X86_FEATURE_REP_GOOD, \

- "jmp memcpy_erms", X86_FEATURE_ERMS
+ "jmp memcpy_orig", X86_FEATURE_ERMS

We didn't observe any strange on arm64/arm/x86 platform before since most memcpy() would behave in an increasing address order ("copy upwards" [3]) and it's the correct order of in-place decompression but it really needs an update to memmove() for sure considering it's an undefined behavior according to the standard and some unique optimization already exists in the kernel.

[1] https://github.com/lz4/lz4/commit/33cb8518ac385835cc17be9a770b27b40cd0e15b
[2] https://github.com/lz4/lz4/pull/717#issuecomment-497818921
[3] https://sourceware.org/bugzilla/show_bug.cgi?id=12518

---

 lib/lz4/lz4_decompress.c |    6 +++++-
 lib/lz4/lz4defs.h        |    1 +
 2 files changed, 6 insertions(+), 1 deletion(-)
lib/lz4/lz4_decompress.c renamed from lib/lz4/lz4_decompress.c~lib-lz4-explicitly-support-in-place-decompression +5 -1
@@ -263,7 +263,11 @@ static FORCE_INLINE int LZ4_decompress_g
 				}
 			}
 
-			LZ4_memcpy(op, ip, length);
+			/*
+			 * supports overlapping memory regions; only matters
+			 * for in-place decompression scenarios
+			 */
+			LZ4_memmove(op, ip, length);
 			ip += length;
 			op += length;
 
lib/lz4/lz4defs.h renamed from lib/lz4/lz4defs.h~lib-lz4-explicitly-support-in-place-decompression +1 -0
@@ -146,6 +146,7 @@ static FORCE_INLINE void LZ4_writeLE16(v
  * environments. This is needed when decompressing the Linux Kernel, for example.
  */
 #define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
+#define LZ4_memmove(dst, src, size) __builtin_memmove(dst, src, size)
 
 static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
 {

_

author maintainer
#40 lore raw

PATCH 33/95 bitops: introduce the for_each_set_clump macro

In reply to Andrew Morton

From: Syed Nayyar Waris <[email protected]>
Subject: bitops: introduce the for_each_set_clump macro

Patch series "Introduce the for_each_set_clump macro", v12.

This patchset introduces a new generic version of for_each_set_clump. The previous version of for_each_set_clump8 used a fixed size 8-bit clump, but the new generic version can work with clump (n-bits) having size between 1 and BITS_PER_LONG inclusive. size less than 1 or more than BITS_PER_LONG causes undefined behaviour. The patchset utilizes the new macro in some GPIO drivers.

The earlier 8-bit for_each_set_clump8 facilitated a for-loop syntax that iterates over a memory region entire groups of set bits at a time.

For example, suppose you would like to iterate over a 32-bit integer 8 bits at a time, skipping over 8-bit groups with no set bit, where XXXXXXXX represents the current 8-bit group:

    Example:        10111110 00000000 11111111 00110011
    First loop:     10111110 00000000 11111111 XXXXXXXX
    Second loop:    10111110 00000000 XXXXXXXX 00110011
    Third loop:     XXXXXXXX 00000000 11111111 00110011

Each iteration of the loop returns the next 8-bit group that has at least one set bit.

But with the new for_each_set_clump the clump size can be different from 8 bits. Moreover, the clump can be split at word boundary in situations where word size is not multiple of clump size. Following are examples showing the working of new macro for clump sizes of 24 bits and 6 bits.

Example 1:
clump size: 24 bits, Number of clumps (or ports): 10
bitmap stores the bit information from where successive clumps are retrieved.

     /* bitmap memory region */
        0x00aa0000ff000000;  /* Most significant bits */
        0xaaaaaa0000ff0000;
        0x000000aa000000aa;
        0xbbbbabcdeffedcba;  /* Least significant bits */

Different iterations of for_each_set_clump:-
'offset' is the bit position and 'clump' is the 24 bit clump from the above bitmap.

Iteration first:        offset: 0 clump: 0xfedcba
Iteration second:       offset: 24 clump: 0xabcdef
Iteration third:        offset: 48 clump: 0xaabbbb
Iteration fourth:       offset: 96 clump: 0xaa
Iteration fifth:        offset: 144 clump: 0xff
Iteration sixth:        offset: 168 clump: 0xaaaaaa
Iteration seventh:      offset: 216 clump: 0xff

Loop breaks because in the end the remaining bits (0x00aa) size was less than clump size of 24 bits.

In above example it can be seen that in iteration third, the 24 bit clump that was retrieved was split between bitmap[0] and bitmap[1]. This example also shows that 24 bit zeroes if present in between, were skipped (preserving the previous for_each_set_macro8 behaviour).

Example 2:
clump size = 6 bits, Number of clumps (or ports) = 3.

     /* bitmap memory region */
        0x00aa0000ff000000;  /* Most significant bits */
        0xaaaaaa0000ff0000;
        0x0f00000000000000;
        0x0000000000000ac0;  /* Least significant bits */

Different iterations of for_each_set_clump:
'offset' is the bit position and 'clump' is the 6 bit clump from the above bitmap.
Iteration first: offset: 6 clump: 0x2b
Loop breaks because 6 * 3 = 18 bits traversed in bitmap. Here 6 * 3 is clump size * no. of clumps.

This patch (of 4):

This macro iterates for each group of bits (clump) with set bits, within a bitmap memory region. For each iteration, "start" is set to the bit offset of the found clump, while the respective clump value is stored to the location pointed by "clump".

Additionally, the bitmap_get_value() and bitmap_set_value() functions are introduced to respectively get and set a value of n-bits in a bitmap memory region. The n-bits can have any size from 1 to BITS_PER_LONG. size less than 1 or more than BITS_PER_LONG causes undefined behaviour.

Moreover, during setting value of n-bit in bitmap, if a situation arise that the width of next n-bit is exceeding the word boundary, then it will divide itself such that some portion of it is stored in that word, while the remaining portion is stored in the next higher word. Similar situation occurs while retrieving the value from bitmap.

---

 include/asm-generic/bitops/find.h |   19 ++++++++
 include/linux/bitmap.h            |   61 ++++++++++++++++++++++++++++
 include/linux/bitops.h            |   13 +++++
 lib/find_bit.c                    |   14 ++++++
 4 files changed, 107 insertions(+)
include/asm-generic/bitops/find.h renamed from include/asm-generic/bitops/find.h~bitops-introduce-the-for_each_set_clump-macro +19 -0
@@ -97,4 +97,23 @@ extern unsigned long find_next_clump8(un
 #define find_first_clump8(clump, bits, size) \
 	find_next_clump8((clump), (bits), (size), 0)
 
+/**
+ * find_next_clump - find next clump with set bits in a memory region
+ * @clump: location to store copy of found clump
+ * @addr: address to base the search on
+ * @size: bitmap size in number of bits
+ * @offset: bit offset at which to start searching
+ * @clump_size: clump size in bits
+ *
+ * Returns the bit offset for the next set clump; the found clump value is
+ * copied to the location pointed by @clump. If no bits are set, returns @size.
+ */
+extern unsigned long find_next_clump(unsigned long *clump,
+				      const unsigned long *addr,
+				      unsigned long size, unsigned long offset,
+				      unsigned long clump_size);
+
+#define find_first_clump(clump, bits, size, clump_size) \
+	find_next_clump((clump), (bits), (size), 0, (clump_size))
+
 #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */
include/linux/bitmap.h renamed from include/linux/bitmap.h~bitops-introduce-the-for_each_set_clump-macro +61 -0

80 lines not on this page · raw message

include/linux/bitops.h renamed from include/linux/bitops.h~bitops-introduce-the-for_each_set_clump-macro +13 -0
@@ -65,6 +65,19 @@ extern unsigned long __sw_hweight64(__u6
 	     (start) < (size); \
 	     (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
 
+/**
+ * for_each_set_clump - iterate over bitmap for each clump with set bits
+ * @start: bit offset to start search and to store the current iteration offset
+ * @clump: location to store copy of current 8-bit clump
+ * @bits: bitmap address to base the search on
+ * @size: bitmap size in number of bits
+ * @clump_size: clump size in bits
+ */
+#define for_each_set_clump(start, clump, bits, size, clump_size) \
+	for ((start) = find_first_clump(&(clump), (bits), (size), (clump_size)); \
+	     (start) < (size); \
+	     (start) = find_next_clump(&(clump), (bits), (size), (start) + (clump_size), (clump_size)))
+
 static inline int get_bitmask_order(unsigned int count)
 {
 	int order;
lib/find_bit.c renamed from lib/find_bit.c~bitops-introduce-the-for_each_set_clump-macro +14 -0
@@ -192,3 +192,17 @@ unsigned long find_next_clump8(unsigned
 	return offset;
 }
 EXPORT_SYMBOL(find_next_clump8);
+
+unsigned long find_next_clump(unsigned long *clump, const unsigned long *addr,
+			       unsigned long size, unsigned long offset,
+			       unsigned long clump_size)
+{
+	offset = find_next_bit(addr, size, offset);
+	if (offset == size)
+		return size;
+
+	offset = rounddown(offset, clump_size);
+	*clump = bitmap_get_value(addr, offset, clump_size);
+	return offset;
+}
+EXPORT_SYMBOL(find_next_clump);

_

#41 lore raw

Re: [patch 33/95] bitops: introduce the for_each_set_clump macro

Quoted 5 lines · Andrew Morton

From: Syed Nayyar Waris <[email protected]>
Subject: bitops: introduce the for_each_set_clump macro

Patch series "Introduce the for_each_set_clump macro", v12.

I'm going to skip this series too.

I don't like how it adds a _really_ obscure function to a _very_ core header file that is basically included by everything.

If I read the patches right, there is exactly one user. Maybe there will be more, but it really isn't appropriate to add this kind of super-rare specialized thing to everybody.

Yes, I may be more sensitive than most, because all my CPU time is spent compiling. Particularly during the merge window. But I really don't want to see random stuff added to core headers.

Start off making this a local "clump_bits.h" header just in the GPIO driver directory. If there is ever another user, maybe it can become a generic clump_bits.h header at that point. But I have to say, I can't recall ever having seen or needed this kind of generic bit clumping interface.

Linus

author maintainer
#42 lore raw

PATCH 34/95 lib/test_bitmap.c: add for_each_set_clump test cases

In reply to Andrew Morton

From: Syed Nayyar Waris <[email protected]>
Subject: lib/test_bitmap.c: add for_each_set_clump test cases

The introduction of the generic for_each_set_clump macro need test cases to verify the implementation. This patch adds test cases for scenarios in which clump sizes are 8 bits, 24 bits, 30 bits and 6 bits. The cases contain situations where clump is getting split at the word boundary and also when zeroes are present in the start and middle of bitmap.

---

 lib/test_bitmap.c |  143 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 143 insertions(+)
lib/test_bitmap.c renamed from lib/test_bitmap.c~lib-test_bitmapc-add-for_each_set_clump-test-cases +143 -0
@@ -155,6 +155,37 @@ static bool __init __check_eq_clump8(con
 	return true;
 }
 
+static bool __init __check_eq_clump(const char *srcfile, unsigned int line,
+				    const unsigned int offset,
+				    const unsigned int size,
+				    const unsigned long *const clump_exp,
+				    const unsigned long *const clump,
+				    const unsigned long clump_size)
+{
+	unsigned long exp;
+
+	if (offset >= size) {
+		pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n",
+			srcfile, line, size, offset);
+		return false;
+	}
+
+	exp = clump_exp[offset / clump_size];
+	if (!exp) {
+		pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0",
+			srcfile, line, offset);
+		return false;
+	}
+
+	if (*clump != exp) {
+		pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX",
+			srcfile, line, exp, *clump);
+		return false;
+	}
+
+	return true;
+}
+
 #define __expect_eq(suffix, ...)					\
 	({								\
 		int result = 0;						\
@@ -172,6 +203,7 @@ static bool __init __check_eq_clump8(con
 #define expect_eq_pbl(...)		__expect_eq(pbl, ##__VA_ARGS__)
 #define expect_eq_u32_array(...)	__expect_eq(u32_array, ##__VA_ARGS__)
 #define expect_eq_clump8(...)		__expect_eq(clump8, ##__VA_ARGS__)
+#define expect_eq_clump(...)		__expect_eq(clump, ##__VA_ARGS__)
 
 static void __init test_zero_clear(void)
 {
@@ -530,6 +562,28 @@ static void noinline __init test_mem_opt
 	}
 }
 
+static const unsigned long clump_bitmap_data[] __initconst = {
+	0x38000201,
+	0x05ff0f38,
+	0xeffedcba,
+	0xbbbbabcd,
+	0x000000aa,
+	0x000000aa,
+	0x00ff0000,
+	0xaaaaaa00,
+	0xff000000,
+	0x00aa0000,
+	0x00000000,
+	0x00000000,
+	0x00000000,
+	0x0f000000,
+	0x00ff0000,
+	0xaaaaaa00,
+	0xff000000,
+	0x00aa0000,
+	0x00000ac0,
+};
+
 static const unsigned char clump_exp[] __initconst = {
 	0x01,	/* 1 bit set */
 	0x02,	/* non-edge 1 bit set */
@@ -541,6 +595,94 @@ static const unsigned char clump_exp[] _
 	0x05,	/* non-adjacent 2 bits set */
 };
 
+static const unsigned long clump_exp1[] __initconst = {
+	0x01,	/* 1 bit set */
+	0x02,	/* non-edge 1 bit set */
+	0x00,	/* zero bits set */
+	0x38,	/* 3 bits set across 4-bit boundary */
+	0x38,	/* Repeated clump */
+	0x0F,	/* 4 bits set */
+	0xFF,	/* all bits set */
+	0x05,	/* non-adjacent 2 bits set */
+};
+
+static const unsigned long clump_exp2[] __initconst = {
+	0xfedcba,	/* 24 bits */
+	0xabcdef,
+	0xaabbbb,	/* Clump split between 2 words */
+	0x000000,	/* zeroes in between */
+	0x0000aa,
+	0x000000,
+	0x0000ff,
+	0xaaaaaa,
+	0x000000,
+	0x0000ff,
+};
+
+static const unsigned long clump_exp3[] __initconst = {
+	0x00000000,	/* starting with 0s*/
+	0x00000000,	/* All 0s */
+	0x00000000,
+	0x00000000,
+	0x3f00000f,     /* Non zero set */
+	0x2aa80003,
+	0x00000aaa,
+	0x00003fc0,
+};
+
+static const unsigned long clump_exp4[] __initconst = {
+	0x00,
+	0x2b,
+};
+
+struct clump_test_data_params {
+	DECLARE_BITMAP(data, 256);
+	unsigned long count;
+	unsigned long offset;
+	unsigned long limit;
+	unsigned long clump_size;
+	unsigned long const *exp;
+};
+
+static struct clump_test_data_params clump_test_data[] __initdata =
+				      { {{0}, 2, 0, 64, 8, clump_exp1},
+					{{0}, 8, 2, 240, 24, clump_exp2},
+					{{0}, 8, 10, 240, 30, clump_exp3},
+					{{0}, 1, 18, 18, 6, clump_exp4} };
+
+static void __init prepare_test_data(unsigned int index)
+{
+	int i;
+	unsigned long width = 0;
+
+	for (i = 0; i < clump_test_data[index].count; i++) {
+		bitmap_set_value(clump_test_data[index].data,
+			clump_bitmap_data[(clump_test_data[index].offset)++], width, 32);
+		width += 32;
+	}
+}
+
+static void __init execute_for_each_set_clump_test(unsigned int index)
+{
+	unsigned long start, clump;
+
+	for_each_set_clump(start, clump, clump_test_data[index].data,
+						clump_test_data[index].limit,
+						clump_test_data[index].clump_size)
+	expect_eq_clump(start, clump_test_data[index].limit, clump_test_data[index].exp,
+						&clump, clump_test_data[index].clump_size);
+}
+
+static void __init test_for_each_set_clump(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(clump_test_data); i++) {
+		prepare_test_data(i);
+		execute_for_each_set_clump_test(i);
+	}
+}
+
 static void __init test_for_each_set_clump8(void)
 {
 #define CLUMP_EXP_NUMBITS 64
@@ -631,6 +773,7 @@ static void __init selftest(void)
 	test_bitmap_parselist();
 	test_mem_optimisations();
 	test_for_each_set_clump8();
+	test_for_each_set_clump();
 	test_bitmap_cut();
 }
 

_

author maintainer
#43 lore raw

PATCH 35/95 gpio: thunderx: utilize for_each_set_clump macro

In reply to Andrew Morton

From: Syed Nayyar Waris <[email protected]>
Subject: gpio: thunderx: utilize for_each_set_clump macro

Reimplement the thunderx_gpio_set_multiple function in drivers/gpio/gpio-thunderx.c to use the new for_each_set_clump macro. Instead of looping for each bank in thunderx_gpio_set_multiple function, now we can skip bank which is not set and save cycles.

---

 drivers/gpio/gpio-thunderx.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpio/gpio-thunderx.c renamed from drivers/gpio/gpio-thunderx.c~gpio-thunderx-utilize-for_each_set_clump-macro +7 -4
@@ -275,12 +275,15 @@ static void thunderx_gpio_set_multiple(s
 				       unsigned long *bits)
 {
 	int bank;
-	u64 set_bits, clear_bits;
+	unsigned long set_bits, clear_bits, gpio_mask;
+	unsigned long offset;
+
 	struct thunderx_gpio *txgpio = gpiochip_get_data(chip);
 
-	for (bank = 0; bank <= chip->ngpio / 64; bank++) {
-		set_bits = bits[bank] & mask[bank];
-		clear_bits = ~bits[bank] & mask[bank];
+	for_each_set_clump(offset, gpio_mask, mask, chip->ngpio, 64) {
+		bank = offset / 64;
+		set_bits = bits[bank] & gpio_mask;
+		clear_bits = ~bits[bank] & gpio_mask;
 		writeq(set_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_SET);
 		writeq(clear_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_CLR);
 	}

_

author maintainer
#44 lore raw

PATCH 36/95 gpio: xilinx: utilize generic bitmap_get_value and _set_value

In reply to Andrew Morton

From: Syed Nayyar Waris <[email protected]>
Subject: gpio: xilinx: utilize generic bitmap_get_value and _set_value

Reimplement the xgpio_set_multiple() function in drivers/gpio/gpio-xilinx.c to use the new generic functions:
bitmap_get_value() and bitmap_set_value(). The code is now simpler to read and understand. Moreover, instead of looping for each bit in xgpio_set_multiple() function, now we can check each channel at a time and save cycles.

---

 drivers/gpio/gpio-xilinx.c |   61 +++++++++++++++++------------------
 1 file changed, 30 insertions(+), 31 deletions(-)
drivers/gpio/gpio-xilinx.c renamed from drivers/gpio/gpio-xilinx.c~gpio-xilinx-utilize-generic-bitmap_get_value-and-_set_value +30 -31
@@ -138,37 +138,37 @@ static void xgpio_set_multiple(struct gp
 {
 	unsigned long flags;
 	struct xgpio_instance *chip = gpiochip_get_data(gc);
-	int index = xgpio_index(chip, 0);
-	int offset, i;
+	u32 *const state = chip->gpio_state;
+	unsigned int *const width = chip->gpio_width;
 
-	spin_lock_irqsave(&chip->gpio_lock[index], flags);
-
-	/* Write to GPIO signals */
-	for (i = 0; i < gc->ngpio; i++) {
-		if (*mask == 0)
-			break;
-		/* Once finished with an index write it out to the register */
-		if (index !=  xgpio_index(chip, i)) {
-			xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
-				       index * XGPIO_CHANNEL_OFFSET,
-				       chip->gpio_state[index]);
-			spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
-			index =  xgpio_index(chip, i);
-			spin_lock_irqsave(&chip->gpio_lock[index], flags);
-		}
-		if (__test_and_clear_bit(i, mask)) {
-			offset =  xgpio_offset(chip, i);
-			if (test_bit(i, bits))
-				chip->gpio_state[index] |= BIT(offset);
-			else
-				chip->gpio_state[index] &= ~BIT(offset);
-		}
-	}
+	DECLARE_BITMAP(old, 64);
+	DECLARE_BITMAP(new, 64);
+	DECLARE_BITMAP(changed, 64);
+
+	spin_lock_irqsave(&chip->gpio_lock[0], flags);
+	spin_lock(&chip->gpio_lock[1]);
+
+	bitmap_set_value(old, state[0], 0, width[0]);
+	bitmap_set_value(old, state[1], width[0], width[1]);
+	bitmap_replace(new, old, bits, mask, gc->ngpio);
+
+	bitmap_set_value(old, state[0], 0, 32);
+	bitmap_set_value(old, state[1], 32, 32);
+	state[0] = bitmap_get_value(new, 0, width[0]);
+	state[1] = bitmap_get_value(new, width[0], width[1]);
+	bitmap_set_value(new, state[0], 0, 32);
+	bitmap_set_value(new, state[1], 32, 32);
+	bitmap_xor(changed, old, new, 64);
+
+	if (((u32 *)changed)[0])
+		xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET,
+				state[0]);
+	if (((u32 *)changed)[1])
+		xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
+				XGPIO_CHANNEL_OFFSET, state[1]);
 
-	xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET +
-		       index * XGPIO_CHANNEL_OFFSET, chip->gpio_state[index]);
-
-	spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
+	spin_unlock(&chip->gpio_lock[1]);
+	spin_unlock_irqrestore(&chip->gpio_lock[0], flags);
 }
 
 /**
@@ -292,6 +292,7 @@ static int xgpio_probe(struct platform_d
 		chip->gpio_width[0] = 32;
 
 	spin_lock_init(&chip->gpio_lock[0]);
+	spin_lock_init(&chip->gpio_lock[1]);
 
 	if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
 		is_dual = 0;
@@ -313,8 +314,6 @@ static int xgpio_probe(struct platform_d
 		if (of_property_read_u32(np, "xlnx,gpio2-width",
 					 &chip->gpio_width[1]))
 			chip->gpio_width[1] = 32;
-
-		spin_lock_init(&chip->gpio_lock[1]);
 	}
 
 	chip->gc.base = -1;

_

author maintainer
#45 lore raw

PATCH 37/95 checkpatch: add new exception to repeated word check

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: add new exception to repeated word check

Recently, commit 4f6ad8aa1eac ("checkpatch: move repeated word test") moved the repeated word test to check for more file types. But after this, if checkpatch.pl is run on MAINTAINERS, it generates several new warnings of the type:

WARNING: Possible repeated word: 'git'

For example:
WARNING: Possible repeated word: 'git'
+T: git git://git.kernel.org/pub/scm/linux/kernel/git/rw/uml.git

So, the pattern "git git://..." is a false positive in this case.

There are several other combinations which may produce a wrong warning message, such as "@size size", ":Begin begin", etc.

Extend repeated word check to compare the characters before and after the word matches.

If there is a non whitespace character before the first word or a non whitespace character excluding punctuation characters after the second word, then the check is skipped and the warning is avoided.

Also add case insensitive word matching to the repeated word check.

---

 scripts/checkpatch.pl |   15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-new-exception-to-repeated-word-check +13 -2
@@ -3050,19 +3050,30 @@ sub process {
 
 # check for repeated words separated by a single space
 		if ($rawline =~ /^\+/ || $in_commit_log) {
+			pos($rawline) = 1 if (!$in_commit_log);
 			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
 
 				my $first = $1;
 				my $second = $2;
-
+				my $start_pos = $-[1];
+				my $end_pos = $+[2];
 				if ($first =~ /(?:struct|union|enum)/) {
 					pos($rawline) += length($first) + length($second) + 1;
 					next;
 				}
 
-				next if ($first ne $second);
+				next if (lc($first) ne lc($second));
 				next if ($first eq 'long');
 
+				# check for character before and after the word matches
+				my $start_char = '';
+				my $end_char = '';
+				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
+				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
+
+				next if ($start_char =~ /^\S$/);
+				next if (index(" \t.,;?!", $end_char) == -1);
+
 				if (WARN("REPEATED_WORD",
 					 "Possible repeated word: '$first'\n" . $herecurr) &&
 				    $fix) {

_

author maintainer
#46 lore raw

PATCH 38/95 checkpatch: fix false positives in REPEATED_WORD warning

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: fix false positives in REPEATED_WORD warning

Presence of hexadecimal address or symbol results in false warning message by checkpatch.pl.

For example, running checkpatch on commit b8ad540dd4e4 ("mptcp: fix memory leak in mptcp_subflow_create_socket()") results in warning:

WARNING:REPEATED_WORD: Possible repeated word: 'ff'

    00 00 00 00 00 00 00 00 00 2f 30 0a 81 88 ff ff  ........./0.....

Similarly, the presence of list command output in commit results in an unnecessary warning.

For example, running checkpatch on commit 899e5ffbf246 ("perf record:
Introduce --switch-output-event") gives:

WARNING:REPEATED_WORD: Possible repeated word: 'root'
dr-xr-x---. 12 root root 4096 Apr 27 17:46 ..

Here, it reports 'ff' and 'root' to be repeated, but it is in fact part of some address or code, where it has to be repeated.

In these cases, the intent of the warning to find stylistic issues in commit messages is not met and the warning is just completely wrong in this case.

To avoid these warnings, add an additional regex check for the directory permission pattern and avoid checking the line for this class of warning. Similarly, to avoid hex pattern, check if the word consists of hex symbols and skip this warning if it is not among the common english words formed using hex letters.

A quick evaluation on v5.6..v5.8 showed that this fix reduces REPEATED_WORD warnings by the frequency of 1890.

A quick manual check found all cases are related to hex output or list command outputs in commit messages.

---

 scripts/checkpatch.pl |   16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-fix-false-positives-in-repeated_word-warning +15 -1
@@ -853,6 +853,13 @@ our $declaration_macros = qr{(?x:
 	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
 )};
 
+our %allow_repeated_words = (
+	add => '',
+	added => '',
+	bad => '',
+	be => '',
+);
+
 sub deparenthesize {
 	my ($string) = @_;
 	return "" if (!defined($string));
@@ -3049,7 +3056,9 @@ sub process {
 		}
 
 # check for repeated words separated by a single space
-		if ($rawline =~ /^\+/ || $in_commit_log) {
+# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
+		if (($rawline =~ /^\+/ || $in_commit_log) &&
+		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
 			pos($rawline) = 1 if (!$in_commit_log);
 			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
 
@@ -3074,6 +3083,11 @@ sub process {
 				next if ($start_char =~ /^\S$/);
 				next if (index(" \t.,;?!", $end_char) == -1);
 
+                                # avoid repeating hex occurrences like 'ff ff fe 09 ...'
+                                if ($first =~ /\b[0-9a-f]{2,}\b/i) {
+                                        next if (!exists($allow_repeated_words{lc($first)}));
+                                }
+
 				if (WARN("REPEATED_WORD",
 					 "Possible repeated word: '$first'\n" . $herecurr) &&
 				    $fix) {

_

author maintainer
#47 lore raw

PATCH 39/95 checkpatch: ignore generated CamelCase defines and enum values

In reply to Andrew Morton

From: Łukasz Stelmach <[email protected]>
Subject: checkpatch: ignore generated CamelCase defines and enum values

Ignore autogenerated CamelCase-like defines and enum values like DRM_MODE_CONNECTOR_Unknown or ETHTOOL_LINK_MODE_Asym_Pause_BIT.

---

 scripts/checkpatch.pl |    2 ++
 1 file changed, 2 insertions(+)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-ignore-generated-camelcase-defines-and-enum-values +2 -0
@@ -5320,6 +5320,8 @@ sub process {
 #CamelCase
 			if ($var !~ /^$Constant$/ &&
 			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
+#Ignore some autogenerated defines and enum values
+			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
 #Ignore Page<foo> variants
 			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
 #Ignore SI style variants like nS, mV and dB

_

author maintainer
#48 lore raw

PATCH 40/95 checkpatch: prefer static const declarations

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: checkpatch: prefer static const declarations

There are about 100,000 uses of 'static const <type>' but about 400 uses of 'static <type> const' in the kernel where type is not a pointer.

The kernel almost always uses "static const" over "const static" as there is a compiler warning for that declaration style.

But there is no compiler warning for "static <type> const".

So add a checkpatch warning for the atypical declaration uses of.

const static <type> <foo>
and
static <type> const <foo>

For example:

$ ./scripts/checkpatch.pl -f --emacs --quiet --nosummary -types=static_const arch/arm/crypto/aes-ce-glue.c
arch/arm/crypto/aes-ce-glue.c:75: WARNING: Move const after static - use 'static const u8'
#75: FILE: arch/arm/crypto/aes-ce-glue.c:75:
+	static u8 const rcon[] = {

---

 scripts/checkpatch.pl |   12 ++++++++++++
 1 file changed, 12 insertions(+)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-prefer-static-const-declarations +12 -0
@@ -4232,6 +4232,18 @@ sub process {
 			}
 		}
 
+# check for const static or static <non ptr type> const declarations
+# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
+		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
+		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
+			if (WARN("STATIC_CONST",
+				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
+			    $fix) {
+				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
+				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
+			}
+		}
+
 # check for non-global char *foo[] = {"bar", ...} declarations.
 		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
 			WARN("STATIC_CONST_CHAR_ARRAY",

_

author maintainer
#49 lore raw

PATCH 41/95 checkpatch: allow --fix removal of unnecessary break statements

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: checkpatch: allow --fix removal of unnecessary break statements

switch/case use of break after a return, goto or break is unnecessary.

There is an existing warning for the return and goto uses, so add break and a --fix option too.

---

 scripts/checkpatch.pl |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-allow-fix-removal-of-unnecessary-break-statements +8 -4
@@ -3699,12 +3699,16 @@ sub process {
 		}
 
 # check indentation of a line with a break;
-# if the previous line is a goto or return and is indented the same # of tabs
+# if the previous line is a goto, return or break
+# and is indented the same # of tabs
 		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
 			my $tabs = $1;
-			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
-				WARN("UNNECESSARY_BREAK",
-				     "break is not useful after a goto or return\n" . $hereprev);
+			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
+				if (WARN("UNNECESSARY_BREAK",
+					 "break is not useful after a $1\n" . $hereprev) &&
+				    $fix) {
+					fix_delete_line($fixlinenr, $rawline);
+				}
 			}
 		}
 

_

author maintainer
#50 lore raw

PATCH 42/95 checkpatch: extend attributes check to handle more patterns

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: extend attributes check to handle more patterns

It is generally preferred that the macros from include/linux/compiler_attributes.h are used, unless there is a reason not to.

checkpatch currently checks __attribute__ for each of packed, aligned, section, printf, scanf, and weak. Other declarations in compiler_attributes.h are not handled.

Add a generic test to check the presence of such attributes. Some attributes require more specific handling and are kept separate.

Also add fixes to the generic attributes check to substitute the correct conversions.

New attributes which are now handled are:

__always_inline__
__assume_aligned__(a, ## __VA_ARGS__)
__cold__
__const__
__copy__(symbol)
__designated_init__
__externally_visible__
__gnu_inline__
__malloc__
__mode__(x)
__no_caller_saved_registers__
__noclone__
__noinline__
__nonstring__
__noreturn__
__pure__
__unused__
__used__

Declarations which contain multiple attributes like __attribute__((__packed__, __cold__)) are also handled except when proper conversions for one or more attributes of the list cannot be determined.

---

 scripts/checkpatch.pl |  109 ++++++++++++++++++++++++++--------------
 1 file changed, 71 insertions(+), 38 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-extend-attributes-check-to-handle-more-patterns +71 -38
@@ -6187,50 +6187,83 @@ sub process {
 			}
 		}
 
-# Check for __attribute__ packed, prefer __packed
+# Check for compiler attributes
 		if ($realfile !~ m@\binclude/uapi/@ &&
-		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
-			WARN("PREFER_PACKED",
-			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
-		}
-
-# Check for __attribute__ aligned, prefer __aligned
-		if ($realfile !~ m@\binclude/uapi/@ &&
-		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
-			WARN("PREFER_ALIGNED",
-			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
-		}
-
-# Check for __attribute__ section, prefer __section
-		if ($realfile !~ m@\binclude/uapi/@ &&
-		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
-			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
-			my $new = substr($old, 1, -1);
-			if (WARN("PREFER_SECTION",
-				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
-			    $fix) {
-				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
+		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
+			my $attr = $1;
+			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
+
+			my %attr_list = (
+				"aligned"			=> "__aligned",
+				"always_inline"			=> "__always_inline",
+				"assume_aligned"		=> "__assume_aligned",
+				"cold"				=> "__cold",
+				"const"				=> "__attribute_const__",
+				"copy"				=> "__copy",
+				"designated_init"		=> "__designated_init",
+				"externally_visible"		=> "__visible",
+				"format"			=> "printf|scanf",
+				"gnu_inline"			=> "__gnu_inline",
+				"malloc"			=> "__malloc",
+				"mode"				=> "__mode",
+				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
+				"noclone"			=> "__noclone",
+				"noinline"			=> "noinline",
+				"nonstring"			=> "__nonstring",
+				"noreturn"			=> "__noreturn",
+				"packed"			=> "__packed",
+				"pure"				=> "__pure",
+				"used"				=> "__used"
+			);
+
+			my @conv_array = ();
+			my $conv_possible = 1;
+
+			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
+				my $curr_attr = $1;
+				my $params = '';
+				$params = $2 if defined($2);
+				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
+
+				if (exists($attr_list{$curr_attr})) {
+					if ($curr_attr eq "format" && $params) {
+						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
+						push(@conv_array, "__$1\($2");
+					} else {
+						my $new = $attr_list{$curr_attr};
+						push(@conv_array, "$new$params");
+					}
+				} else {
+					$conv_possible = 0;
+					last;
+				}
 			}
-		}
 
-# Check for __attribute__ format(printf, prefer __printf
-		if ($realfile !~ m@\binclude/uapi/@ &&
-		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
-			if (WARN("PREFER_PRINTF",
-				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
-			    $fix) {
-				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
+			if (scalar @conv_array > 0 && $conv_possible == 1) {
+				my $replace = join(' ', @conv_array);
+				if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
+				         "$replace is preferred over __attribute__(($attr))\n" . $herecurr) &&
+					$fix) {
+					$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*\Q$attr\E\s*\)\s*\)/$replace/;
+					$fixed[$fixlinenr] =~ s/\}\Q$replace\E/} $replace/;
+				}
+			}
 
+			# Check for __attribute__ section, prefer __section
+			if ($attr =~ /^_*section_*\s*\(\s*("[^"]*")/) {
+				my $old = substr($attr, $-[1], $+[1] - $-[1]);
+				my $new = substr($old, 1, -1);
+				if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
+				         "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
+					$fix) {
+					$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
+				}
 			}
-		}
 
-# Check for __attribute__ format(scanf, prefer __scanf
-		if ($realfile !~ m@\binclude/uapi/@ &&
-		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
-			if (WARN("PREFER_SCANF",
-				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
-			    $fix) {
-				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
+			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
+			if ($attr =~ /^_*unused/) {
+				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
+				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
 			}
 		}
 

_

author maintainer
#51 lore raw

PATCH 43/95 checkpatch: add a fixer for missing newline at eof

In reply to Andrew Morton

From: Tom Rix <[email protected]>
Subject: checkpatch: add a fixer for missing newline at eof

Remove the trailing error message from the fixed lines.

---

 scripts/checkpatch.pl |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-a-fixer-for-missing-newline-at-eof +5 -2
@@ -3418,8 +3418,11 @@ sub process {
 
 # check for adding lines without a newline.
 		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
-			WARN("MISSING_EOF_NEWLINE",
-			     "adding a line without newline at end of file\n" . $herecurr);
+			if (WARN("MISSING_EOF_NEWLINE",
+			         "adding a line without newline at end of file\n" . $herecurr) &&
+			    $fix) {
+				fix_delete_line($fixlinenr+1, "No newline at end of file");
+			}
 		}
 
 # check we are in a valid source file C or perl if not then ignore this hunk

_

author maintainer
#52 lore raw

PATCH 44/95 checkpatch: update __attribute__((section("name"))) quote removal

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: checkpatch: update __attribute__((section("name"))) quote removal

commit 33def8498fdd ("treewide: Convert macro and uses of __section(foo) to __section("foo")") removed the stringification of the section name and now requires quotes around the named section.

Update checkpatch to not remove any quotes when suggesting conversion of __attribute__((section("name"))) to __section("name")

Miscellanea:

o Add section to the hash with __section replacement
o Remove separate test for __attribute__((section
o Remove the limitation on converting attributes containing only known, possible conversions. Any unknown attribute types are now left as-is and known types are converted and moved before __attribute__ and removed from within the __attribute__((list...)).

---

 scripts/checkpatch.pl |   47 ++++++++++++----------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-update-__attribute__sectionname-quote-removal +15 -32
@@ -6216,50 +6216,33 @@ sub process {
 				"noreturn"			=> "__noreturn",
 				"packed"			=> "__packed",
 				"pure"				=> "__pure",
+				"section"			=> "__section",
 				"used"				=> "__used"
 			);
 
-			my @conv_array = ();
-			my $conv_possible = 1;
-
 			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
-				my $curr_attr = $1;
+				my $orig_attr = $1;
 				my $params = '';
 				$params = $2 if defined($2);
+				my $curr_attr = $orig_attr;
 				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
-
 				if (exists($attr_list{$curr_attr})) {
+					my $new = $attr_list{$curr_attr};
 					if ($curr_attr eq "format" && $params) {
 						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
-						push(@conv_array, "__$1\($2");
+						$new = "__$1\($2";
 					} else {
-						my $new = $attr_list{$curr_attr};
-						push(@conv_array, "$new$params");
+						$new = "$new$params";
+					}
+					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
+						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
+					    $fix) {
+						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
+						$fixed[$fixlinenr] =~ s/$remove//;
+						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
+						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
+						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
 					}
-				} else {
-					$conv_possible = 0;
-					last;
-				}
-			}
-
-			if (scalar @conv_array > 0 && $conv_possible == 1) {
-				my $replace = join(' ', @conv_array);
-				if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
-				         "$replace is preferred over __attribute__(($attr))\n" . $herecurr) &&
-					$fix) {
-					$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*\Q$attr\E\s*\)\s*\)/$replace/;
-					$fixed[$fixlinenr] =~ s/\}\Q$replace\E/} $replace/;
-				}
-			}
-
-			# Check for __attribute__ section, prefer __section
-			if ($attr =~ /^_*section_*\s*\(\s*("[^"]*")/) {
-				my $old = substr($attr, $-[1], $+[1] - $-[1]);
-				my $new = substr($old, 1, -1);
-				if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
-				         "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
-					$fix) {
-					$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
 				}
 			}
 

_

author maintainer
#53 lore raw

PATCH 45/95 checkpatch: add fix option for GERRIT_CHANGE_ID

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: add fix option for GERRIT_CHANGE_ID

Currently, whenever a Gerrit Change-Id is present in a commit, checkpatch.pl warns to remove the Change-Id before submitting the patch.

E.g., running checkpatch on commit adc311a5bbf6 ("iwlwifi: bump FW API to 53 for 22000 series") reports this error:

ERROR: Remove Gerrit Change-Id's before submitting upstream
Change-Id: I5725e46394f3f53c3069723fd513cc53c7df383d

Provide a simple fix option by simply deleting the indicated line.

---

 scripts/checkpatch.pl |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-fix-option-for-gerrit_change_id +5 -2
@@ -2852,8 +2852,11 @@ sub process {
 
 # Check for Gerrit Change-Ids not in any patch context
 		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
-			ERROR("GERRIT_CHANGE_ID",
-			      "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr);
+			if (ERROR("GERRIT_CHANGE_ID",
+			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
+			    $fix) {
+                                fix_delete_line($fixlinenr, $rawline);
+                        }
 		}
 
 # Check if the commit log is in a possible stack dump

_

author maintainer
#54 lore raw

PATCH 46/95 checkpatch: add __alias and __weak to suggested __attribute__ conversions

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: checkpatch: add __alias and __weak to suggested __attribute__ conversions

Add __alias and __weak to the suggested __attribute__((<foo>)) conversions.

---

 scripts/checkpatch.pl |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-__alias-and-__weak-to-suggested-__attribute__-conversions +3 -1
@@ -6200,6 +6200,7 @@ sub process {
 			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
 
 			my %attr_list = (
+				"alias"				=> "__alias",
 				"aligned"			=> "__aligned",
 				"always_inline"			=> "__always_inline",
 				"assume_aligned"		=> "__assume_aligned",
@@ -6220,7 +6221,8 @@ sub process {
 				"packed"			=> "__packed",
 				"pure"				=> "__pure",
 				"section"			=> "__section",
-				"used"				=> "__used"
+				"used"				=> "__used",
+				"weak"				=> "__weak"
 			);
 
 			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {

_

author maintainer
#55 lore raw

PATCH 47/95 checkpatch: improve email parsing

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: improve email parsing

checkpatch doesn't report warnings for many common mistakes in emails. Some of which are trailing commas and incorrect use of email comments.

At the same time several false positives are reported due to incorrect handling of mail comments. The most common of which is due to the pattern:

<[email protected]> # X.X

Improve email parsing in checkpatch.

Some general email rules are defined:

- Multiple name comments should not be allowed.
- Comments inside address should not be allowed.
- In general comments should be enclosed within parentheses. Relaxation is given to comments beginning with #.
- Stable addresses should not begin with a name.
- Comments in stable addresses should begin only with a #.

Improvements to parsing:

- Detect and report unexpected content after email.
- Quoted names are excluded from comment parsing.
- Trailing dots, commas or quotes in email are removed during formatting. Correspondingly a BAD_SIGN_OFF warning is emitted.
- Improperly quoted email like '"name <address>"' are now warned about.

In addition, added fixes for all the possible rules.

---

 scripts/checkpatch.pl |  108 +++++++++++++++++++++++++++++++++-------
 1 file changed, 91 insertions(+), 17 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-improve-email-parsing +91 -17
@@ -1159,6 +1159,7 @@ sub parse_email {
 	my ($formatted_email) = @_;
 
 	my $name = "";
+	my $quoted = "";
 	my $name_comment = "";
 	my $address = "";
 	my $comment = "";
@@ -1190,14 +1191,20 @@ sub parse_email {
 		}
 	}
 
-	$comment = trim($comment);
-	$name = trim($name);
-	$name =~ s/^\"|\"$//g;
-	if ($name =~ s/(\s*\([^\)]+\))\s*//) {
-		$name_comment = trim($1);
+	# Extract comments from names excluding quoted parts
+	# "John D. (Doe)" - Do not extract
+	if ($name =~ s/\"(.+)\"//) {
+		$quoted = $1;
+	}
+	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
+		$name_comment .= trim($1);
 	}
+	$name =~ s/^[ \"]+|[ \"]+$//g;
+	$name = trim("$quoted $name");
+
 	$address = trim($address);
 	$address =~ s/^\<|\>$//g;
+	$comment = trim($comment);
 
 	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
 		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
@@ -1212,17 +1219,20 @@ sub format_email {
 
 	my $formatted_email;
 
-	$name_comment = trim($name_comment);
-	$comment = trim($comment);
-	$name = trim($name);
-	$name =~ s/^\"|\"$//g;
+	$name =~ s/^[ \"]+|[ \"]+$//g;
 	$address = trim($address);
+	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
 
 	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
 		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
 		$name = "\"$name\"";
 	}
 
+	$name_comment = trim($name_comment);
+	$name_comment = " $name_comment" if ($name_comment ne "");
+	$comment = trim($comment);
+	$comment = " $comment" if ($comment ne "");
+
 	if ("$name" eq "") {
 		$formatted_email = "$address";
 	} else {
@@ -1240,15 +1250,11 @@ sub reformat_email {
 }
 
 sub same_email_addresses {
-	my ($email1, $email2, $match_comment) = @_;
+	my ($email1, $email2) = @_;
 
 	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
 	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
 
-	if ($match_comment != 1) {
-		return $email1_name eq $email2_name &&
-		       $email1_address eq $email2_address;
-	}
 	return $email1_name eq $email2_name &&
 	       $email1_address eq $email2_address &&
 	       $name1_comment eq $name2_comment &&
@@ -2711,7 +2717,7 @@ sub process {
 			$signoff++;
 			$in_commit_log = 0;
 			if ($author ne ''  && $authorsignoff != 1) {
-				if (same_email_addresses($1, $author, 1)) {
+				if (same_email_addresses($1, $author)) {
 					$authorsignoff = 1;
 				} else {
 					my $ctx = $1;
@@ -2807,9 +2813,77 @@ sub process {
 				$dequoted =~ s/" </ </;
 				# Don't force email to have quotes
 				# Allow just an angle bracketed address
-				if (!same_email_addresses($email, $suggested_email, 0)) {
+				if (!same_email_addresses($email, $suggested_email)) {
+					if (WARN("BAD_SIGN_OFF",
+						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
+					    $fix) {
+						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
+					}
+				}
+
+				# Address part shouldn't have comments
+				my $stripped_address = $email_address;
+				$stripped_address =~ s/\([^\(\)]*\)//g;
+				if ($email_address ne $stripped_address) {
+					if (WARN("BAD_SIGN_OFF",
+						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
+					    $fix) {
+						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
+					}
+				}
+
+				# Only one name comment should be allowed
+				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
+				if ($comment_count > 1) {
 					WARN("BAD_SIGN_OFF",
-					     "email address '$email' might be better as '$suggested_email'\n" . $herecurr);
+					     "Use a single name comment in email: '$email'\n" . $herecurr);
+				}
+
+
+				# [email protected] or [email protected] shouldn't
+				# have an email name. In addition commments should strictly
+				# begin with a #
+				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
+					if (($comment ne "" && $comment !~ /^#.+/) ||
+					    ($email_name ne "")) {
+						my $cur_name = $email_name;
+						my $new_comment = $comment;
+						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
+
+						# Remove brackets enclosing comment text
+						# and # from start of comments to get comment text
+						$new_comment =~ s/^\((.*)\)$/$1/;
+						$new_comment =~ s/^\[(.*)\]$/$1/;
+						$new_comment =~ s/^[\s\#]+|\s+$//g;
+
+						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
+						$new_comment = " # $new_comment" if ($new_comment ne "");
+						my $new_email = "$email_address$new_comment";
+
+						if (WARN("BAD_STABLE_ADDRESS_STYLE",
+							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
+						    $fix) {
+							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
+						}
+					}
+				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
+					my $new_comment = $comment;
+
+					# Extract comment text from within brackets or
+					# c89 style /*...*/ comments
+					$new_comment =~ s/^\[(.*)\]$/$1/;
+					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
+
+					$new_comment = trim($new_comment);
+					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
+					$new_comment = "($new_comment)" if ($new_comment ne "");
+					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
+
+					if (WARN("BAD_SIGN_OFF",
+						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
+					    $fix) {
+						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
+					}
 				}
 			}
 

_

author maintainer
#56 lore raw

PATCH 48/95 checkpatch: fix spelling errors and remove repeated word

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: fix spelling errors and remove repeated word

Delete repeated word in scripts/checkpatch.pl:
"are are" -> "are"

Fix typos:
"commments" -> "comments"
"falsly" -> "falsely"

---

 scripts/checkpatch.pl |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-fix-spelling-errors-and-remove-repeated-word +3 -3
@@ -2841,7 +2841,7 @@ sub process {
 
 
 				# [email protected] or [email protected] shouldn't
-				# have an email name. In addition commments should strictly
+				# have an email name. In addition comments should strictly
 				# begin with a #
 				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
 					if (($comment ne "" && $comment !~ /^#.+/) ||
@@ -5000,7 +5000,7 @@ sub process {
 ## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
 ##
 ## 			# Remove any bracketed sections to ensure we do not
-## 			# falsly report the parameters of functions.
+## 			# falsely report the parameters of functions.
 ## 			my $ln = $line;
 ## 			while ($ln =~ s/\([^\(\)]*\)//g) {
 ## 			}
@@ -7109,7 +7109,7 @@ sub process {
 		exit(0);
 	}
 
-	# This is not a patch, and we are are in 'no-patch' mode so
+	# This is not a patch, and we are in 'no-patch' mode so
 	# just keep quiet.
 	if (!$chk_patch && !$is_patch) {
 		exit(0);

_

author maintainer
#57 lore raw

PATCH 49/95 checkpatch: avoid COMMIT_LOG_LONG_LINE warning for signature tags

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: avoid COMMIT_LOG_LONG_LINE warning for signature tags

Currently checkpatch warns us for long lines in commits even for signature tag lines.

Generally these lines exceed the 75-character limit because of:
1) long names and long email address
2) some comments on scoped review and acknowledgement, i.e., for a dedicated pointer on what was reported by the identity in 'Reported-by'
3) some additional comments on CC: [email protected] tags

Exclude signature tag lines from this class of warning.

There were 1896 COMMIT_LOG_LONG_LINE warnings in v5.6..v5.8 before this patch application and 1879 afterwards.

A quick manual check found all the dropped warnings related to signature tags.

---

 scripts/checkpatch.pl |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-avoid-commit_log_long_line-warning-for-signature-tags +2 -2
@@ -2952,8 +2952,8 @@ sub process {
 					# file delta changes
 		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
 					# filename then :
-		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
-					# A Fixes: or Link: line
+		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
+					# A Fixes: or Link: line or signature tag line
 		      $commit_log_possible_stack_dump)) {
 			WARN("COMMIT_LOG_LONG_LINE",
 			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);

_

author maintainer
#58 lore raw

PATCH 50/95 checkpatch: fix unescaped left brace

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: fix unescaped left brace

There is an unescaped left brace in a regex in OPEN_BRACE check. This throws a runtime error when checkpatch is run with --fix flag and the OPEN_BRACE check is executed.

Fix it by escaping the left brace.

Fixes 8d1824780f2f ("checkpatch: add --fix option for a couple OPEN_BRACE misuses")

---

 scripts/checkpatch.pl |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-fix-unescaped-left-brace +1 -1
@@ -4505,7 +4505,7 @@ sub process {
 			    $fix) {
 				fix_delete_line($fixlinenr, $rawline);
 				my $fixed_line = $rawline;
-				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
+				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
 				my $line1 = $1;
 				my $line2 = $2;
 				fix_insert_line($fixlinenr, ltrim($line1));

_

author maintainer
#59 lore raw

PATCH 51/95 checkpatch: add fix option for ASSIGNMENT_CONTINUATIONS

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: add fix option for ASSIGNMENT_CONTINUATIONS

Currently, checkpatch warns us if an assignment operator is placed at the start of a line and not at the end of previous line.

E.g., running checkpatch on commit 8195b1396ec8 ("hv_netvsc: fix deadlock on hotplug") reports:

CHECK: Assignment operator '=' should be on the previous line
+ struct netvsc_device *nvdev
+ = container_of(w, struct netvsc_device, subchan_work);

Provide a simple fix by appending assignment operator to the previous line and removing from the current line, if both the lines are additions (ie start with '+')

---

 scripts/checkpatch.pl |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-fix-option-for-assignment_continuations +8 -2
@@ -3533,8 +3533,14 @@ sub process {
 
 # check for assignments on the start of a line
 		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
-			CHK("ASSIGNMENT_CONTINUATIONS",
-			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
+			my $operator = $1;
+			if (CHK("ASSIGNMENT_CONTINUATIONS",
+				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
+			    $fix && $prevrawline =~ /^\+/) {
+				# add assignment operator to the previous line, remove from current line
+				$fixed[$fixlinenr - 1] .= " $operator";
+				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
+			}
 		}
 
 # check for && or || at the start of a line

_

author maintainer
#60 lore raw

PATCH 52/95 checkpatch: add fix option for LOGICAL_CONTINUATIONS

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: add fix option for LOGICAL_CONTINUATIONS

Currently, checkpatch warns if logical continuations are placed at the start of a line and not at the end of previous line.

E.g., running checkpatch on commit 3485507fc272 ("staging: bcm2835-camera:
Reduce length of enum names") reports:

CHECK:LOGICAL_CONTINUATIONS: Logical continuations should be on the previous line
+ if (!ret
+ && camera_port ==

Provide a simple fix by inserting logical operator at the last non-comment, non-whitespace char of the previous line and removing from current line, if both the lines are additions(ie start with '+')

---

 scripts/checkpatch.pl |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-fix-option-for-logical_continuations +10 -2
@@ -3545,8 +3545,16 @@ sub process {
 
 # check for && or || at the start of a line
 		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
-			CHK("LOGICAL_CONTINUATIONS",
-			    "Logical continuations should be on the previous line\n" . $hereprev);
+			my $operator = $1;
+			if (CHK("LOGICAL_CONTINUATIONS",
+				"Logical continuations should be on the previous line\n" . $hereprev) &&
+			    $fix && $prevrawline =~ /^\+/) {
+				# insert logical operator at last non-comment, non-whitepsace char on previous line
+				$prevline =~ /[\s$;]*$/;
+				my $line_end = substr($prevrawline, $-[0]);
+				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
+				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
+			}
 		}
 
 # check indentation starts on a tab stop

_

author maintainer
#61 lore raw

PATCH 53/95 checkpatch: add fix and improve warning msg for non-standard signature

In reply to Andrew Morton

From: Aditya Srivastava <[email protected]>
Subject: checkpatch: add fix and improve warning msg for non-standard signature

Currently checkpatch warns for BAD_SIGN_OFF on non-standard signature styles.

A large number of these warnings occur because of typo mistakes in signature tags. An evaluation over v4.13..v5.8 showed that out of 539 warnings due to non-standard signatures, 87 are due to typo mistakes.

Following are the standard signature tags which are often incorrectly used, along with their individual counts of incorrect use (over v4.13..v5.8):

Reviewed-by 42
Signed-off-by 25
Reported-by 6
Acked-by 4
Tested-by 4
Suggested-by 4

Provide a fix by calculating levenshtein distance for the signature tag with all the standard signatures and suggest a fix with a signature, whose edit distance is less than or equal to 2 with the misspelled signature.

Out of the 86 mispelled signatures fixed with this approach, 85 were found to be good corrections and 1 was bad correction.

Following was found to be a bad correction:
Tweeted-by (count: 1) => Tested-by

---

 scripts/checkpatch.pl |   71 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 69 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-fix-and-improve-warning-msg-for-non-standard-signature +69 -2
@@ -506,6 +506,64 @@ our $signature_tags = qr{(?xi:
 	Cc:
 )};
 
+sub edit_distance_min {
+	my (@arr) = @_;
+	my $len = scalar @arr;
+	if ((scalar @arr) < 1) {
+		# if underflow, return
+		return;
+	}
+	my $min = $arr[0];
+	for my $i (0 .. ($len-1)) {
+		if ($arr[$i] < $min) {
+			$min = $arr[$i];
+		}
+	}
+	return $min;
+}
+
+sub get_edit_distance {
+	my ($str1, $str2) = @_;
+	$str1 = lc($str1);
+	$str2 = lc($str2);
+	$str1 =~ s/-//g;
+	$str2 =~ s/-//g;
+	my $len1 = length($str1);
+	my $len2 = length($str2);
+	# two dimensional array storing minimum edit distance
+	my @distance;
+	for my $i (0 .. $len1) {
+		for my $j (0 .. $len2) {
+			if ($i == 0) {
+				$distance[$i][$j] = $j;
+			} elsif ($j == 0) {
+				$distance[$i][$j] = $i;
+			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
+				$distance[$i][$j] = $distance[$i - 1][$j - 1];
+			} else {
+				my $dist1 = $distance[$i][$j - 1]; #insert distance
+				my $dist2 = $distance[$i - 1][$j]; # remove
+				my $dist3 = $distance[$i - 1][$j - 1]; #replace
+				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
+			}
+		}
+	}
+	return $distance[$len1][$len2];
+}
+
+sub find_standard_signature {
+	my ($sign_off) = @_;
+	my @standard_signature_tags = (
+		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
+		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
+	);
+	foreach my $signature (@standard_signature_tags) {
+		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
+	}
+
+	return "";
+}
+
 our @typeListMisordered = (
 	qr{char\s+(?:un)?signed},
 	qr{int\s+(?:(?:un)?signed\s+)?short\s},
@@ -2773,8 +2831,17 @@ sub process {
 			my $ucfirst_sign_off = ucfirst(lc($sign_off));
 
 			if ($sign_off !~ /$signature_tags/) {
-				WARN("BAD_SIGN_OFF",
-				     "Non-standard signature: $sign_off\n" . $herecurr);
+				my $suggested_signature = find_standard_signature($sign_off);
+				if ($suggested_signature eq "") {
+					WARN("BAD_SIGN_OFF",
+					     "Non-standard signature: $sign_off\n" . $herecurr);
+				} else {
+					if (WARN("BAD_SIGN_OFF",
+						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
+					    $fix) {
+						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
+					}
+				}
 			}
 			if (defined $space_before && $space_before ne "") {
 				if (WARN("BAD_SIGN_OFF",

_

author maintainer
#62 lore raw

PATCH 54/95 checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]

Modifiers %h and %hh should never be used.

Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") specifies that:

"Standard integer promotion is already done and %hx and %hhx is useless so do not encourage the use of %hh[xudi] or %h[xudi]."

"The "h" and "hh" things should never be used. The only reason for them being used if you have an "int", but you want to print it out as a "char" (and honestly, that is a really bad reason, you'd be better off just using a proper cast to make the code more obvious)."

Add a new check to emit a warning on finding an unneeded use of %h or %hh modifier.

Also add a fix option to the check.

---

 scripts/checkpatch.pl |   22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-warning-for-unnecessary-use-of-%h-and-%hh +22 -0
@@ -6102,6 +6102,28 @@ sub process {
 			     "Avoid logging continuation uses where feasible\n" . $herecurr);
 		}
 
+# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
+		if (defined $stat &&
+		    $line =~ /\b$logFunctions\s*\(/ &&
+		    index($stat, '"') >= 0) {
+			my $lc = $stat =~ tr@\n@@;
+			$lc = $lc + $linenr;
+			my $stat_real = get_stat_real($linenr, $lc);
+			pos($stat_real) = index($stat_real, '"');
+			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
+				my $pspec = $1;
+				my $h = $2;
+				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
+				if (WARN("UNNECESSARY_MODIFIER",
+					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
+				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
+					my $nspec = $pspec;
+					$nspec =~ s/h//g;
+					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
+				}
+			}
+		}
+
 # check for mask then right shift without a parentheses
 		if ($perl_version_ok &&
 		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&

_

author maintainer
#63 lore raw

PATCH 55/95 checkpatch: add warning for lines starting with a '#' in commit log

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: add warning for lines starting with a '#' in commit log

Commit log lines starting with '#' are dropped by git as comments. Add a check to emit a warning for these lines.

Also add a --fix option to insert a space before the leading '#' in such lines.

---

 scripts/checkpatch.pl |    9 +++++++++
 1 file changed, 9 insertions(+)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-warning-for-lines-starting-with-a-in-commit-log +9 -0
@@ -3033,6 +3033,15 @@ sub process {
 			$commit_log_possible_stack_dump = 0;
 		}
 
+# Check for lines starting with a #
+		if ($in_commit_log && $line =~ /^#/) {
+			if (WARN("COMMIT_COMMENT_SYMBOL",
+				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
+			    $fix) {
+				$fixed[$fixlinenr] =~ s/^/ /;
+			}
+		}
+
 # Check for git id commit length and improperly formed commit descriptions
 		if ($in_commit_log && !$commit_log_possible_stack_dump &&
 		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&

_

author maintainer
#64 lore raw

PATCH 56/95 checkpatch: fix TYPO_SPELLING check for words with apostrophe

In reply to Andrew Morton

From: Dwaipayan Ray <[email protected]>
Subject: checkpatch: fix TYPO_SPELLING check for words with apostrophe

checkpatch reports a false TYPO_SPELLING warning for some words containing an apostrophe when run with --codespell option.

A false positive is "doesn't". Occurrence of the word causes checkpatch to emit the following warning:

"WARNING: 'doesn'' may be misspelled - perhaps 'doesn't'?"

Modify the regex pattern to be more in line with the codespell default word matching regex. This fixes the word capture and avoids the false warning.

In addition, highlight the misspelled word location by adding a caret below the word.

---

 scripts/checkpatch.pl |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-fix-typo_spelling-check-for-words-with-apostrophe +5 -2
@@ -3182,15 +3182,18 @@ sub process {
 # Check for various typo / spelling mistakes
 		if (defined($misspellings) &&
 		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
-			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
+			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
 				my $typo = $1;
+				my $blank = copy_spacing($rawline);
+				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
+				my $hereptr = "$hereline$ptr\n";
 				my $typo_fix = $spelling_fix{lc($typo)};
 				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
 				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
 				my $msg_level = \&WARN;
 				$msg_level = \&CHK if ($file);
 				if (&{$msg_level}("TYPO_SPELLING",
-						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
+						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
 				    $fix) {
 					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
 				}

_

author maintainer
#65 lore raw

PATCH 57/95 checkpatch: add printk_once and printk_ratelimit to prefer pr_<level> warning

In reply to Andrew Morton

From: Joe Perches <[email protected]>
Subject: checkpatch: add printk_once and printk_ratelimit to prefer pr_<level> warning

Add the _once and _ratelimited variants to the test for printk(KERN_<LEVEL> that should prefer pr_<level>.

Miscellanea:

o Add comment description for the conversions

---

 scripts/checkpatch.pl |   13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
scripts/checkpatch.pl renamed from scripts/checkpatch.pl~checkpatch-add-printk_once-and-printk_ratelimit-to-prefer-pr_level-warning +10 -3
@@ -4543,16 +4543,23 @@ sub process {
 			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
 		}
 
-		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
-			my $orig = $1;
+# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
+		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
+			my $printk = $1;
+			my $modifier = $2;
+			my $orig = $3;
+			$modifier = "" if (!defined($modifier));
 			my $level = lc($orig);
 			$level = "warn" if ($level eq "warning");
 			my $level2 = $level;
 			$level2 = "dbg" if ($level eq "debug");
+			$level .= $modifier;
+			$level2 .= $modifier;
 			WARN("PREFER_PR_LEVEL",
-			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
+			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
 		}
 
+# prefer dev_<level> to dev_printk(KERN_<LEVEL>
 		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
 			my $orig = $1;
 			my $level = lc($orig);

_

author maintainer
#66 lore raw

PATCH 58/95 fs/nilfs2: remove some unused macros to tame gcc

In reply to Andrew Morton

From: Alex Shi <[email protected]>
Subject: fs/nilfs2: remove some unused macros to tame gcc

There some macros are unused and cause gcc warning. Remove them.

fs/nilfs2/segment.c:137:0: warning: macro "nilfs_cnt32_gt" is not used [-Wunused-macros]
fs/nilfs2/segment.c:144:0: warning: macro "nilfs_cnt32_le" is not used [-Wunused-macros]
fs/nilfs2/segment.c:143:0: warning: macro "nilfs_cnt32_lt" is not used [-Wunused-macros]

---

 fs/nilfs2/segment.c |    5 -----
 1 file changed, 5 deletions(-)
fs/nilfs2/segment.c renamed from fs/nilfs2/segment.c~fs-nilfs2-remove-some-unused-macros-to-tame-gcc +0 -5
@@ -134,14 +134,9 @@ static void nilfs_segctor_do_flush(struc
 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
 static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
 
-#define nilfs_cnt32_gt(a, b)   \
-	(typecheck(__u32, a) && typecheck(__u32, b) && \
-	 ((__s32)(b) - (__s32)(a) < 0))
 #define nilfs_cnt32_ge(a, b)   \
 	(typecheck(__u32, a) && typecheck(__u32, b) && \
 	 ((__s32)(a) - (__s32)(b) >= 0))
-#define nilfs_cnt32_lt(a, b)  nilfs_cnt32_gt(b, a)
-#define nilfs_cnt32_le(a, b)  nilfs_cnt32_ge(b, a)
 
 static int nilfs_prepare_segment_lock(struct super_block *sb,
 				      struct nilfs_transaction_info *ti)

_

author maintainer
#67 lore raw

PATCH 59/95 kdump: append uts_namespace.name offset to VMCOREINFO

In reply to Andrew Morton

From: Alexander Egorenkov <[email protected]>
Subject: kdump: append uts_namespace.name offset to VMCOREINFO

The offset of the field 'init_uts_ns.name' has changed since commit 9a56493f6942 ("uts: Use generic ns_common::count").

Make the offset of the field 'uts_namespace.name' available in VMCOREINFO because tools like 'crash-utility' and 'makedumpfile' must be able to read it from crash dumps.

---

 Documentation/admin-guide/kdump/vmcoreinfo.rst |    6 ++++++
 kernel/crash_core.c                            |    1 +
 2 files changed, 7 insertions(+)
Documentation/admin-guide/kdump/vmcoreinfo.rst renamed from Documentation/admin-guide/kdump/vmcoreinfo.rst~kdump-append-uts_namespacename-offset-to-vmcoreinfo +6 -0
@@ -39,6 +39,12 @@ call.
 User-space tools can get the kernel name, host name, kernel release
 number, kernel version, architecture name and OS type from it.
 
+(uts_namespace, name)
+---------------------
+
+Offset of the name's member. Crash Utility and Makedumpfile get
+the start address of the init_uts_ns.name from this.
+
 node_online_map
 ---------------
 
kernel/crash_core.c renamed from kernel/crash_core.c~kdump-append-uts_namespacename-offset-to-vmcoreinfo +1 -0
@@ -447,6 +447,7 @@ static int __init crash_save_vmcoreinfo_
 	VMCOREINFO_PAGESIZE(PAGE_SIZE);
 
 	VMCOREINFO_SYMBOL(init_uts_ns);
+	VMCOREINFO_OFFSET(uts_namespace, name);
 	VMCOREINFO_SYMBOL(node_online_map);
 #ifdef CONFIG_MMU
 	VMCOREINFO_SYMBOL_ARRAY(swapper_pg_dir);

_

author maintainer
#68 lore raw

PATCH 60/95 rapidio: remove unused rio_get_asm() and rio_get_device()

In reply to Andrew Morton

From: Sebastian Andrzej Siewior <[email protected]>
Subject: rapidio: remove unused rio_get_asm() and rio_get_device()

The functions rio_get_asm() and rio_get_device() are globally exported but have almost no users in tree. The only user is rio_init_mports() which invokes it via rio_init().

rio_init() iterates over every registered device and invokes rio_fixup_device(). It looks like a fixup function which should perform a "change" to the device but does nothing. It has been like this since its introduction in commit 394b701ce4fbf ("[PATCH] RapidIO support: core base") which was merged into v2.6.15-rc1.

Remove rio_init() because the performed fixup function (rio_fixup_device()) does nothing. Remove rio_get_asm() and rio_get_device() which have no callers now.

---

 drivers/rapidio/rio.c   |   81 --------------------------------------
 include/linux/rio_drv.h |    3 -
 2 files changed, 84 deletions(-)
drivers/rapidio/rio.c renamed from drivers/rapidio/rio.c~rapidio-remove-unused-rio_get_asm-and-rio_get_device +0 -81
@@ -1413,71 +1413,6 @@ rio_mport_get_feature(struct rio_mport *
 EXPORT_SYMBOL_GPL(rio_mport_get_feature);
 
 /**
- * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
- * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
- * @did: RIO did to match or %RIO_ANY_ID to match all dids
- * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
- * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
- * @from: Previous RIO device found in search, or %NULL for new search
- *
- * Iterates through the list of known RIO devices. If a RIO device is
- * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
- * count to the device is incrememted and a pointer to its device
- * structure is returned. Otherwise, %NULL is returned. A new search
- * is initiated by passing %NULL to the @from argument. Otherwise, if
- * @from is not %NULL, searches continue from next device on the global
- * list. The reference count for @from is always decremented if it is
- * not %NULL.
- */
-struct rio_dev *rio_get_asm(u16 vid, u16 did,
-			    u16 asm_vid, u16 asm_did, struct rio_dev *from)
-{
-	struct list_head *n;
-	struct rio_dev *rdev;
-
-	WARN_ON(in_interrupt());
-	spin_lock(&rio_global_list_lock);
-	n = from ? from->global_list.next : rio_devices.next;
-
-	while (n && (n != &rio_devices)) {
-		rdev = rio_dev_g(n);
-		if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
-		    (did == RIO_ANY_ID || rdev->did == did) &&
-		    (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
-		    (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
-			goto exit;
-		n = n->next;
-	}
-	rdev = NULL;
-      exit:
-	rio_dev_put(from);
-	rdev = rio_dev_get(rdev);
-	spin_unlock(&rio_global_list_lock);
-	return rdev;
-}
-EXPORT_SYMBOL_GPL(rio_get_asm);
-
-/**
- * rio_get_device - Begin or continue searching for a RIO device by vid/did
- * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
- * @did: RIO did to match or %RIO_ANY_ID to match all dids
- * @from: Previous RIO device found in search, or %NULL for new search
- *
- * Iterates through the list of known RIO devices. If a RIO device is
- * found with a matching @vid and @did, the reference count to the
- * device is incrememted and a pointer to its device structure is returned.
- * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
- * to the @from argument. Otherwise, if @from is not %NULL, searches
- * continue from next device on the global list. The reference count for
- * @from is always decremented if it is not %NULL.
- */
-struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
-{
-	return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
-}
-EXPORT_SYMBOL_GPL(rio_get_device);
-
-/**
  * rio_std_route_add_entry - Add switch route table entry using standard
  *   registers defined in RIO specification rev.1.3
  * @mport: Master port to issue transaction
@@ -2106,20 +2041,6 @@ found:
 	return rc;
 }
 
-static void rio_fixup_device(struct rio_dev *dev)
-{
-}
-
-static int rio_init(void)
-{
-	struct rio_dev *dev = NULL;
-
-	while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
-		rio_fixup_device(dev);
-	}
-	return 0;
-}
-
 static struct workqueue_struct *rio_wq;
 
 struct rio_disc_work {
@@ -2206,8 +2127,6 @@ int rio_init_mports(void)
 	kfree(work);
 
 no_disc:
-	rio_init();
-
 	return 0;
 }
 EXPORT_SYMBOL_GPL(rio_init_mports);
include/linux/rio_drv.h renamed from include/linux/rio_drv.h~rapidio-remove-unused-rio_get_asm-and-rio_get_device +0 -3
@@ -444,9 +444,6 @@ static inline void rio_set_drvdata(struc
 /* Misc driver helpers */
 extern u16 rio_local_get_device_id(struct rio_mport *port);
 extern void rio_local_set_device_id(struct rio_mport *port, u16 did);
-extern struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from);
-extern struct rio_dev *rio_get_asm(u16 vid, u16 did, u16 asm_vid, u16 asm_did,
-				   struct rio_dev *from);
 extern int rio_init_mports(void);
 
 #endif				/* LINUX_RIO_DRV_H */

_

author maintainer
#69 lore raw

PATCH 61/95 gcov: remove support for GCC < 4.9

In reply to Andrew Morton

From: Nick Desaulniers <[email protected]>
Subject: gcov: remove support for GCC < 4.9

Since commit 0bddd227f3dc ("Documentation: update for gcc 4.9 requirement") the minimum supported version of GCC is gcc-4.9. It's now safe to remove this code.

Similar to commit 10415533a906 ("gcov: Remove old GCC 3.4 support") but that was for GCC 4.8 and this is for GCC 4.9.

---

 kernel/gcov/gcc_4_7.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/gcov/gcc_4_7.c renamed from kernel/gcov/gcc_4_7.c~gcov-remove-support-for-gcc-49 +1 -3
@@ -25,10 +25,8 @@
 #define GCOV_COUNTERS			9
 #elif (__GNUC__ > 5) || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
 #define GCOV_COUNTERS			10
-#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
-#define GCOV_COUNTERS			9
 #else
-#define GCOV_COUNTERS			8
+#define GCOV_COUNTERS			9
 #endif
 
 #define GCOV_TAG_FUNCTION_LENGTH	3

_

author maintainer
#70 lore raw

PATCH 62/95 gcov: fix kernel-doc markup issue

In reply to Andrew Morton

From: Alex Shi <[email protected]>
Subject: gcov: fix kernel-doc markup issue

Fix the following kernel-doc issue in gcov:
kernel/gcov/gcc_4_7.c:238: warning: Function parameter or member 'dst' not described in 'gcov_info_add'
kernel/gcov/gcc_4_7.c:238: warning: Function parameter or member 'src' not described in 'gcov_info_add'
kernel/gcov/gcc_4_7.c:238: warning: Excess function parameter 'dest' description in 'gcov_info_add'
kernel/gcov/gcc_4_7.c:238: warning: Excess function parameter 'source' description in 'gcov_info_add'

---

 kernel/gcov/gcc_4_7.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/gcov/gcc_4_7.c renamed from kernel/gcov/gcc_4_7.c~gcov-fix-kernel-doc-markup-issue +3 -3
@@ -227,10 +227,10 @@ int gcov_info_is_compatible(struct gcov_
 
 /**
  * gcov_info_add - add up profiling data
- * @dest: profiling data set to which data is added
- * @source: profiling data set which is added
+ * @dst: profiling data set to which data is added
+ * @src: profiling data set which is added
  *
- * Adds profiling counts of @source to @dest.
+ * Adds profiling counts of @src to @dst.
  */
 void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
 {

_

author maintainer
#71 lore raw

PATCH 63/95 bfs: don't use WARNING: string when it's just info.

In reply to Andrew Morton

From: Randy Dunlap <[email protected]>
Subject: bfs: don't use WARNING: string when it's just info.

Make the printk() [bfs "printf" macro] seem less severe by changing "WARNING:" to "NOTE:".

<asm-generic/bug.h> warns us about using WARNING or BUG in a format string other than in WARN() or BUG() family macros. bfs/inode.c is doing just that in a normal printk() call, so change the "WARNING" string to be "NOTE".

---

 fs/bfs/inode.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bfs/inode.c renamed from fs/bfs/inode.c~bfs-dont-use-warning-string-when-its-just-info +1 -1
@@ -350,7 +350,7 @@ static int bfs_fill_super(struct super_b
 
 	info->si_lasti = (le32_to_cpu(bfs_sb->s_start) - BFS_BSIZE) / sizeof(struct bfs_inode) + BFS_ROOT_INO - 1;
 	if (info->si_lasti == BFS_MAX_LASTI)
-		printf("WARNING: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id);
+		printf("NOTE: filesystem %s was created with 512 inodes, the real maximum is 511, mounting anyway\n", s->s_id);
 	else if (info->si_lasti > BFS_MAX_LASTI) {
 		printf("Impossible last inode number %lu > %d on %s\n", info->si_lasti, BFS_MAX_LASTI, s->s_id);
 		goto out1;

_

author maintainer
#72 lore raw

PATCH 64/95 relay: remove unused buf_mapped and buf_unmapped callbacks

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: relay: remove unused buf_mapped and buf_unmapped callbacks

Patch series "relay: cleanup and const callbacks", v2.

None of the relay users require the use of mutable structs for callbacks, however the relay code does. Instead of assigning default callbacks when there is none, add callback wrappers to conditionally call the client callbacks if available, and fall back to default behaviour (typically no-op) otherwise.

This lets all relay users make their struct rchan_callbacks const data.

This series starts with a number of cleanups first based on Christoph's feedback.

This patch (of 9):

No relay client uses the buf_mapped or buf_unmapped callbacks. Remove them. This makes relay's vm_operations_struct close callback a dummy, remove it as well.

---

 include/linux/relay.h |   19 -------------------
 kernel/relay.c        |   34 ----------------------------------
 2 files changed, 53 deletions(-)
include/linux/relay.h renamed from include/linux/relay.h~relay-remove-unused-buf_mapped-and-buf_unmapped-callbacks +0 -19
@@ -102,25 +102,6 @@ struct rchan_callbacks
 			     size_t prev_padding);
 
 	/*
-	 * buf_mapped - relay buffer mmap notification
-	 * @buf: the channel buffer
-	 * @filp: relay file pointer
-	 *
-	 * Called when a relay file is successfully mmapped
-	 */
-        void (*buf_mapped)(struct rchan_buf *buf,
-			   struct file *filp);
-
-	/*
-	 * buf_unmapped - relay buffer unmap notification
-	 * @buf: the channel buffer
-	 * @filp: relay file pointer
-	 *
-	 * Called when a relay file is successfully unmapped
-	 */
-        void (*buf_unmapped)(struct rchan_buf *buf,
-			     struct file *filp);
-	/*
 	 * create_buf_file - create file to represent a relay channel buffer
 	 * @filename: the name of the file to create
 	 * @parent: the parent of the file to create
kernel/relay.c renamed from kernel/relay.c~relay-remove-unused-buf_mapped-and-buf_unmapped-callbacks +0 -34
@@ -28,15 +28,6 @@ static DEFINE_MUTEX(relay_channels_mutex
 static LIST_HEAD(relay_channels);
 
 /*
- * close() vm_op implementation for relay file mapping.
- */
-static void relay_file_mmap_close(struct vm_area_struct *vma)
-{
-	struct rchan_buf *buf = vma->vm_private_data;
-	buf->chan->cb->buf_unmapped(buf, vma->vm_file);
-}
-
-/*
  * fault() vm_op implementation for relay file mapping.
  */
 static vm_fault_t relay_buf_fault(struct vm_fault *vmf)
@@ -62,7 +53,6 @@ static vm_fault_t relay_buf_fault(struct
  */
 static const struct vm_operations_struct relay_file_mmap_ops = {
 	.fault = relay_buf_fault,
-	.close = relay_file_mmap_close,
 };
 
 /*
@@ -96,7 +86,6 @@ static void relay_free_page_array(struct
 static int relay_mmap_buf(struct rchan_buf *buf, struct vm_area_struct *vma)
 {
 	unsigned long length = vma->vm_end - vma->vm_start;
-	struct file *filp = vma->vm_file;
 
 	if (!buf)
 		return -EBADF;
@@ -107,7 +96,6 @@ static int relay_mmap_buf(struct rchan_b
 	vma->vm_ops = &relay_file_mmap_ops;
 	vma->vm_flags |= VM_DONTEXPAND;
 	vma->vm_private_data = buf;
-	buf->chan->cb->buf_mapped(buf, filp);
 
 	return 0;
 }
@@ -284,22 +272,6 @@ static int subbuf_start_default_callback
 }
 
 /*
- * buf_mapped() default callback.  Does nothing.
- */
-static void buf_mapped_default_callback(struct rchan_buf *buf,
-					struct file *filp)
-{
-}
-
-/*
- * buf_unmapped() default callback.  Does nothing.
- */
-static void buf_unmapped_default_callback(struct rchan_buf *buf,
-					  struct file *filp)
-{
-}
-
-/*
  * create_buf_file_create() default callback.  Does nothing.
  */
 static struct dentry *create_buf_file_default_callback(const char *filename,
@@ -322,8 +294,6 @@ static int remove_buf_file_default_callb
 /* relay channel default callbacks */
 static struct rchan_callbacks default_channel_callbacks = {
 	.subbuf_start = subbuf_start_default_callback,
-	.buf_mapped = buf_mapped_default_callback,
-	.buf_unmapped = buf_unmapped_default_callback,
 	.create_buf_file = create_buf_file_default_callback,
 	.remove_buf_file = remove_buf_file_default_callback,
 };
@@ -509,10 +479,6 @@ static void setup_callbacks(struct rchan
 
 	if (!cb->subbuf_start)
 		cb->subbuf_start = subbuf_start_default_callback;
-	if (!cb->buf_mapped)
-		cb->buf_mapped = buf_mapped_default_callback;
-	if (!cb->buf_unmapped)
-		cb->buf_unmapped = buf_unmapped_default_callback;
 	if (!cb->create_buf_file)
 		cb->create_buf_file = create_buf_file_default_callback;
 	if (!cb->remove_buf_file)

_

author maintainer
#73 lore raw

PATCH 65/95 relay: require non-NULL callbacks in relay_open()

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: relay: require non-NULL callbacks in relay_open()

There are no clients passing NULL callbacks, which makes sense as it wouldn't even create a file. Require non-NULL callbacks, and throw away the handling for NULL callbacks.

---

 kernel/relay.c |   14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
kernel/relay.c renamed from kernel/relay.c~relay-require-non-null-callbacks-in-relay_open +2 -12
@@ -291,13 +291,6 @@ static int remove_buf_file_default_callb
 	return -EINVAL;
 }
 
-/* relay channel default callbacks */
-static struct rchan_callbacks default_channel_callbacks = {
-	.subbuf_start = subbuf_start_default_callback,
-	.create_buf_file = create_buf_file_default_callback,
-	.remove_buf_file = remove_buf_file_default_callback,
-};
-
 /**
  *	wakeup_readers - wake up readers waiting on a channel
  *	@work: contains the channel buffer
@@ -472,11 +465,6 @@ static void relay_close_buf(struct rchan
 static void setup_callbacks(struct rchan *chan,
 				   struct rchan_callbacks *cb)
 {
-	if (!cb) {
-		chan->cb = &default_channel_callbacks;
-		return;
-	}
-
 	if (!cb->subbuf_start)
 		cb->subbuf_start = subbuf_start_default_callback;
 	if (!cb->create_buf_file)
@@ -542,6 +530,8 @@ struct rchan *relay_open(const char *bas
 		return NULL;
 	if (subbuf_size > UINT_MAX / n_subbufs)
 		return NULL;
+	if (!cb)
+		return NULL;
 
 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
 	if (!chan)

_

author maintainer
#74 lore raw

PATCH 66/95 relay: make create_buf_file and remove_buf_file callbacks mandatory

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: relay: make create_buf_file and remove_buf_file callbacks mandatory

All clients provide create_buf_file and remove_buf_file callbacks, and they're required for relay to make sense. There is no point in them being optional.

Also document whether each callback is mandatory/optional.

---

 include/linux/relay.h |    6 ++++++
 kernel/relay.c        |   26 +-------------------------
 2 files changed, 7 insertions(+), 25 deletions(-)
include/linux/relay.h renamed from include/linux/relay.h~relay-make-create_buf_file-and-remove_buf_file-callbacks-mandatory +6 -0
@@ -89,6 +89,8 @@ struct rchan_callbacks
 	 * The client should return 1 to continue logging, 0 to stop
 	 * logging.
 	 *
+	 * This callback is optional.
+	 *
 	 * NOTE: subbuf_start will also be invoked when the buffer is
 	 *       created, so that the first sub-buffer can be initialized
 	 *       if necessary.  In this case, prev_subbuf will be NULL.
@@ -122,6 +124,8 @@ struct rchan_callbacks
 	 * cause relay_open() to create a single global buffer rather
 	 * than the default set of per-cpu buffers.
 	 *
+	 * This callback is mandatory.
+	 *
 	 * See Documentation/filesystems/relay.rst for more info.
 	 */
 	struct dentry *(*create_buf_file)(const char *filename,
@@ -139,6 +143,8 @@ struct rchan_callbacks
 	 * channel buffer.
 	 *
 	 * The callback should return 0 if successful, negative if not.
+	 *
+	 * This callback is mandatory.
 	 */
 	int (*remove_buf_file)(struct dentry *dentry);
 };
kernel/relay.c renamed from kernel/relay.c~relay-make-create_buf_file-and-remove_buf_file-callbacks-mandatory +1 -25
@@ -271,26 +271,6 @@ static int subbuf_start_default_callback
 	return 1;
 }
 
-/*
- * create_buf_file_create() default callback.  Does nothing.
- */
-static struct dentry *create_buf_file_default_callback(const char *filename,
-						       struct dentry *parent,
-						       umode_t mode,
-						       struct rchan_buf *buf,
-						       int *is_global)
-{
-	return NULL;
-}
-
-/*
- * remove_buf_file() default callback.  Does nothing.
- */
-static int remove_buf_file_default_callback(struct dentry *dentry)
-{
-	return -EINVAL;
-}
-
 /**
  *	wakeup_readers - wake up readers waiting on a channel
  *	@work: contains the channel buffer
@@ -467,10 +447,6 @@ static void setup_callbacks(struct rchan
 {
 	if (!cb->subbuf_start)
 		cb->subbuf_start = subbuf_start_default_callback;
-	if (!cb->create_buf_file)
-		cb->create_buf_file = create_buf_file_default_callback;
-	if (!cb->remove_buf_file)
-		cb->remove_buf_file = remove_buf_file_default_callback;
 	chan->cb = cb;
 }
 
@@ -530,7 +506,7 @@ struct rchan *relay_open(const char *bas
 		return NULL;
 	if (subbuf_size > UINT_MAX / n_subbufs)
 		return NULL;
-	if (!cb)
+	if (!cb || !cb->create_buf_file || !cb->remove_buf_file)
 		return NULL;
 
 	chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);

_

author maintainer
#75 lore raw

PATCH 67/95 relay: allow the use of const callback structs

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: relay: allow the use of const callback structs

None of the relay users require the use of mutable structs for callbacks, however the relay code does. Instead of assigning the default callback for subbuf_start, add a wrapper to conditionally call the client callback if available, and fall back to default behaviour otherwise.

This lets all relay users make their struct rchan_callbacks const data.

---

 include/linux/relay.h |    4 ++--
 kernel/relay.c        |   37 ++++++++++---------------------------
 2 files changed, 12 insertions(+), 29 deletions(-)
include/linux/relay.h renamed from include/linux/relay.h~relay-allow-the-use-of-const-callback-structs +2 -2
@@ -62,7 +62,7 @@ struct rchan
 	size_t subbuf_size;		/* sub-buffer size */
 	size_t n_subbufs;		/* number of sub-buffers per buffer */
 	size_t alloc_size;		/* total buffer size allocated */
-	struct rchan_callbacks *cb;	/* client callbacks */
+	const struct rchan_callbacks *cb; /* client callbacks */
 	struct kref kref;		/* channel refcount */
 	void *private_data;		/* for user-defined data */
 	size_t last_toobig;		/* tried to log event > subbuf size */
@@ -157,7 +157,7 @@ struct rchan *relay_open(const char *bas
 			 struct dentry *parent,
 			 size_t subbuf_size,
 			 size_t n_subbufs,
-			 struct rchan_callbacks *cb,
+			 const struct rchan_callbacks *cb,
 			 void *private_data);
 extern int relay_late_setup_files(struct rchan *chan,
 				  const char *base_filename,
kernel/relay.c renamed from kernel/relay.c~relay-allow-the-use-of-const-callback-structs +10 -27
@@ -252,23 +252,14 @@ EXPORT_SYMBOL_GPL(relay_buf_full);
  * High-level relay kernel API and associated functions.
  */
 
-/*
- * rchan_callback implementations defining default channel behavior.  Used
- * in place of corresponding NULL values in client callback struct.
- */
-
-/*
- * subbuf_start() default callback.  Does nothing.
- */
-static int subbuf_start_default_callback (struct rchan_buf *buf,
-					  void *subbuf,
-					  void *prev_subbuf,
-					  size_t prev_padding)
+static int relay_subbuf_start(struct rchan_buf *buf, void *subbuf,
+			      void *prev_subbuf, size_t prev_padding)
 {
-	if (relay_buf_full(buf))
-		return 0;
+	if (!buf->chan->cb->subbuf_start)
+		return !relay_buf_full(buf);
 
-	return 1;
+	return buf->chan->cb->subbuf_start(buf, subbuf,
+					   prev_subbuf, prev_padding);
 }
 
 /**
@@ -314,7 +305,7 @@ static void __relay_reset(struct rchan_b
 	for (i = 0; i < buf->chan->n_subbufs; i++)
 		buf->padding[i] = 0;
 
-	buf->chan->cb->subbuf_start(buf, buf->data, NULL, 0);
+	relay_subbuf_start(buf, buf->data, NULL, 0);
 }
 
 /**
@@ -442,14 +433,6 @@ static void relay_close_buf(struct rchan
 	kref_put(&buf->kref, relay_remove_buf);
 }
 
-static void setup_callbacks(struct rchan *chan,
-				   struct rchan_callbacks *cb)
-{
-	if (!cb->subbuf_start)
-		cb->subbuf_start = subbuf_start_default_callback;
-	chan->cb = cb;
-}
-
 int relay_prepare_cpu(unsigned int cpu)
 {
 	struct rchan *chan;
@@ -495,7 +478,7 @@ struct rchan *relay_open(const char *bas
 			 struct dentry *parent,
 			 size_t subbuf_size,
 			 size_t n_subbufs,
-			 struct rchan_callbacks *cb,
+			 const struct rchan_callbacks *cb,
 			 void *private_data)
 {
 	unsigned int i;
@@ -529,7 +512,7 @@ struct rchan *relay_open(const char *bas
 		chan->has_base_filename = 1;
 		strlcpy(chan->base_filename, base_filename, NAME_MAX);
 	}
-	setup_callbacks(chan, cb);
+	chan->cb = cb;
 	kref_init(&chan->kref);
 
 	mutex_lock(&relay_channels_mutex);
@@ -712,7 +695,7 @@ size_t relay_switch_subbuf(struct rchan_
 	new_subbuf = buf->subbufs_produced % buf->chan->n_subbufs;
 	new = buf->start + new_subbuf * buf->chan->subbuf_size;
 	buf->offset = 0;
-	if (!buf->chan->cb->subbuf_start(buf, new, old, buf->prev_padding)) {
+	if (!relay_subbuf_start(buf, new, old, buf->prev_padding)) {
 		buf->offset = buf->chan->subbuf_size + 1;
 		return 0;
 	}

_

author maintainer
#76 lore raw

PATCH 68/95 drm/i915: make relay callbacks const

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: drm/i915: make relay callbacks const

Now that relay_open() accepts const callbacks, make relay callbacks const.

---

 drivers/gpu/drm/i915/gt/uc/intel_guc_log.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i915/gt/uc/intel_guc_log.c renamed from drivers/gpu/drm/i915/gt/uc/intel_guc_log.c~drm-i915-make-relay-callbacks-const +1 -1
@@ -134,7 +134,7 @@ static int remove_buf_file_callback(stru
 }
 
 /* relay channel callbacks */
-static struct rchan_callbacks relay_callbacks = {
+static const struct rchan_callbacks relay_callbacks = {
 	.subbuf_start = subbuf_start_callback,
 	.create_buf_file = create_buf_file_callback,
 	.remove_buf_file = remove_buf_file_callback,

_

author maintainer
#77 lore raw

PATCH 69/95 ath10k: make relay callbacks const

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: ath10k: make relay callbacks const

Now that relay_open() accepts const callbacks, make relay callbacks const.

---

 drivers/net/wireless/ath/ath10k/spectral.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath10k/spectral.c renamed from drivers/net/wireless/ath/ath10k/spectral.c~ath10k-make-relay-callbacks-const +1 -1
@@ -497,7 +497,7 @@ static int remove_buf_file_handler(struc
 	return 0;
 }
 
-static struct rchan_callbacks rfs_spec_scan_cb = {
+static const struct rchan_callbacks rfs_spec_scan_cb = {
 	.create_buf_file = create_buf_file_handler,
 	.remove_buf_file = remove_buf_file_handler,
 };

_

author maintainer
#78 lore raw

PATCH 70/95 ath11k: make relay callbacks const

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: ath11k: make relay callbacks const

Now that relay_open() accepts const callbacks, make relay callbacks const.

---

 drivers/net/wireless/ath/ath11k/spectral.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath11k/spectral.c renamed from drivers/net/wireless/ath/ath11k/spectral.c~ath11k-make-relay-callbacks-const +1 -1
@@ -148,7 +148,7 @@ static int remove_buf_file_handler(struc
 	return 0;
 }
 
-static struct rchan_callbacks rfs_scan_cb = {
+static const struct rchan_callbacks rfs_scan_cb = {
 	.create_buf_file = create_buf_file_handler,
 	.remove_buf_file = remove_buf_file_handler,
 };

_

author maintainer
#79 lore raw

PATCH 71/95 ath9k: make relay callbacks const

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: ath9k: make relay callbacks const

Now that relay_open() accepts const callbacks, make relay callbacks const.

---

 drivers/net/wireless/ath/ath9k/common-spectral.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath9k/common-spectral.c renamed from drivers/net/wireless/ath/ath9k/common-spectral.c~ath9k-make-relay-callbacks-const +1 -1
@@ -1053,7 +1053,7 @@ static int remove_buf_file_handler(struc
 	return 0;
 }
 
-static struct rchan_callbacks rfs_spec_scan_cb = {
+static const struct rchan_callbacks rfs_spec_scan_cb = {
 	.create_buf_file = create_buf_file_handler,
 	.remove_buf_file = remove_buf_file_handler,
 };

_

author maintainer
#80 lore raw

PATCH 72/95 blktrace: make relay callbacks const

In reply to Andrew Morton

From: Jani Nikula <[email protected]>
Subject: blktrace: make relay callbacks const

Now that relay_open() accepts const callbacks, make relay callbacks const.

---

 kernel/trace/blktrace.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/blktrace.c renamed from kernel/trace/blktrace.c~blktrace-make-relay-callbacks-const +1 -1
@@ -449,7 +449,7 @@ static struct dentry *blk_create_buf_fil
 					&relay_file_operations);
 }
 
-static struct rchan_callbacks blk_relay_callbacks = {
+static const struct rchan_callbacks blk_relay_callbacks = {
 	.subbuf_start		= blk_subbuf_start_callback,
 	.create_buf_file	= blk_create_buf_file_callback,
 	.remove_buf_file	= blk_remove_buf_file_callback,

_

author maintainer
#81 lore raw

PATCH 73/95 kernel/resource.c: fix kernel-doc markups

In reply to Andrew Morton

From: Mauro Carvalho Chehab <[email protected]>
Subject: kernel/resource.c: fix kernel-doc markups

Kernel-doc markups should use this format:
identifier - description

While here, fix a kernel-doc tag that was using, instead, a normal comment block.

---

 kernel/resource.c |   24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
kernel/resource.c renamed from kernel/resource.c~resource-fix-kernel-doc-markups +14 -10
@@ -320,9 +320,8 @@ int release_resource(struct resource *ol
 EXPORT_SYMBOL(release_resource);
 
 /**
- * Finds the lowest iomem resource that covers part of [@start..@end].  The
- * caller must specify @start, @end, @flags, and @desc (which may be
- * IORES_DESC_NONE).
+ * find_next_iomem_res - Finds the lowest iomem resource that covers part of
+ *			 [@start..@end].
  *
  * If a resource is found, returns 0 and @*res is overwritten with the part
  * of the resource that's within [@start..@end]; if none is found, returns
@@ -337,6 +336,9 @@ EXPORT_SYMBOL(release_resource);
  * @desc:	descriptor the resource must have
  * @first_lvl:	walk only the first level children, if set
  * @res:	return ptr, if resource found
+ *
+ * The caller must specify @start, @end, @flags, and @desc
+ * (which may be IORES_DESC_NONE).
  */
 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
 			       unsigned long flags, unsigned long desc,
@@ -416,11 +418,9 @@ static int __walk_iomem_res_desc(resourc
 }
 
 /**
- * Walks through iomem resources and calls func() with matching resource
- * ranges. This walks through whole tree and not just first level children.
- * All the memory ranges which overlap start,end and also match flags and
- * desc are valid candidates.
- *
+ * walk_iomem_res_desc - Walks through iomem resources and calls func()
+ *			 with matching resource ranges.
+ * *
  * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
  * @flags: I/O resource flags
  * @start: start addr
@@ -428,6 +428,10 @@ static int __walk_iomem_res_desc(resourc
  * @arg: function argument for the callback @func
  * @func: callback function that is called for each qualifying resource area
  *
+ * This walks through whole tree and not just first level children.
+ * All the memory ranges which overlap start,end and also match flags and
+ * desc are valid candidates.
+ *
  * NOTE: For a new descriptor search, define a new IORES_DESC in
  * <linux/ioport.h> and set it in 'desc' of a target resource entry.
  */
@@ -1372,9 +1376,9 @@ static bool system_ram_resources_mergeab
 	       !r1->child && !r2->child;
 }
 
-/*
+/**
  * merge_system_ram_resource - mark the System RAM resource mergeable and try to
- * merge it with adjacent, mergeable resources
+ *	merge it with adjacent, mergeable resources
  * @res: resource descriptor
  *
  * This interface is intended for memory hotplug, whereby lots of contiguous

_

author maintainer
#82 lore raw

PATCH 74/95 ubsan: remove redundant -Wno-maybe-uninitialized

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: remove redundant -Wno-maybe-uninitialized

Patch series "Clean up UBSAN Makefile", v2.

This series attempts to address the issues seen with UBSAN's object-size sanitizer causing problems under GCC. In the process, the Kconfig and Makefile are refactored to do all the cc-option calls in the Kconfig. Additionally start to detangle -Wno-maybe-uninitialized, disable UBSAN_TRAP under COMPILE_TEST for wider build coverage, and expand the libusan tests.

This patch (of 7):

In commit 78a5255ffb6a ("Stop the ad-hoc games with -Wno-maybe-initialized") -Wmaybe-uninitialized was disabled globally, so keeping the disabling logic here too doesn't make sense.

Cc Linus Torvalds <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Marco Elver <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc George Popescu <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Andrey Ryabinin <[email protected]>

---

 lib/Kconfig.ubsan      |    4 ----
 scripts/Makefile.ubsan |    4 ----
 2 files changed, 8 deletions(-)
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-remove-redundant-wno-maybe-uninitialized +0 -4
@@ -72,10 +72,6 @@ config UBSAN_MISC
 config UBSAN_SANITIZE_ALL
 	bool "Enable instrumentation for the entire kernel"
 	depends on ARCH_HAS_UBSAN_SANITIZE_ALL
-
-	# We build with -Wno-maybe-uninitilzed, but we still want to
-	# use -Wmaybe-uninitilized in allmodconfig builds.
-	# So dependsy bellow used to disable this option in allmodconfig
 	depends on !COMPILE_TEST
 	default y
 	help
scripts/Makefile.ubsan renamed from scripts/Makefile.ubsan~ubsan-remove-redundant-wno-maybe-uninitialized +0 -4
@@ -31,7 +31,3 @@ endif
 ifdef CONFIG_UBSAN_TRAP
       CFLAGS_UBSAN += $(call cc-option, -fsanitize-undefined-trap-on-error)
 endif
-
-      # -fsanitize=* options makes GCC less smart than usual and
-      # increase number of 'maybe-uninitialized false-positives
-      CFLAGS_UBSAN += $(call cc-option, -Wno-maybe-uninitialized)

_

author maintainer
#83 lore raw

PATCH 75/95 ubsan: move cc-option tests into Kconfig

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: move cc-option tests into Kconfig

Instead of doing if/endif blocks with cc-option calls in the UBSAN Makefile, move all the tests into Kconfig and use the Makefile to collect the results.

---

 lib/Kconfig.ubsan      |   61 +++++++++++++++++++++++++++++++++++++--
 scripts/Makefile.ubsan |   45 +++++++++-------------------
 2 files changed, 73 insertions(+), 33 deletions(-)
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-move-cc-option-tests-into-kconfig +58 -3
@@ -36,10 +36,17 @@ config UBSAN_KCOV_BROKEN
 	  See https://bugs.llvm.org/show_bug.cgi?id=45831 for the status
 	  in newer releases.
 
+config CC_HAS_UBSAN_BOUNDS
+	def_bool $(cc-option,-fsanitize=bounds)
+
+config CC_HAS_UBSAN_ARRAY_BOUNDS
+	def_bool $(cc-option,-fsanitize=array-bounds)
+
 config UBSAN_BOUNDS
 	bool "Perform array index bounds checking"
 	default UBSAN
 	depends on !UBSAN_KCOV_BROKEN
+	depends on CC_HAS_UBSAN_ARRAY_BOUNDS || CC_HAS_UBSAN_BOUNDS
 	help
 	  This option enables detection of directly indexed out of bounds
 	  array accesses, where the array size is known at compile time.
@@ -47,15 +54,30 @@ config UBSAN_BOUNDS
 	  to the {str,mem}*cpy() family of functions (that is addressed
 	  by CONFIG_FORTIFY_SOURCE).
 
+config UBSAN_ONLY_BOUNDS
+	def_bool CC_HAS_UBSAN_BOUNDS && !CC_HAS_UBSAN_ARRAY_BOUNDS
+	depends on UBSAN_BOUNDS
+	help
+	  This is a weird case: Clang's -fsanitize=bounds includes
+	  -fsanitize=local-bounds, but it's trapping-only, so for
+	  Clang, we must use -fsanitize=array-bounds when we want
+	  traditional array bounds checking enabled. For GCC, we
+	  want -fsanitize=bounds.
+
+config UBSAN_ARRAY_BOUNDS
+	def_bool CC_HAS_UBSAN_ARRAY_BOUNDS
+	depends on UBSAN_BOUNDS
+
 config UBSAN_LOCAL_BOUNDS
 	bool "Perform array local bounds checking"
 	depends on UBSAN_TRAP
-	depends on CC_IS_CLANG
 	depends on !UBSAN_KCOV_BROKEN
+	depends on $(cc-option,-fsanitize=local-bounds)
 	help
 	  This option enables -fsanitize=local-bounds which traps when an
-	  exception/error is detected. Therefore, it should be enabled only
-	  if trapping is expected.
+	  exception/error is detected. Therefore, it may only be enabled
+	  with CONFIG_UBSAN_TRAP.
+
 	  Enabling this option detects errors due to accesses through a
 	  pointer that is derived from an object of a statically-known size,
 	  where an added offset (which may not be known statically) is
@@ -69,6 +91,38 @@ config UBSAN_MISC
 	  own Kconfig options. Disable this if you only want to have
 	  individually selected checks.
 
+config UBSAN_SHIFT
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=shift)
+
+config UBSAN_DIV_ZERO
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=integer-divide-by-zero)
+
+config UBSAN_UNREACHABLE
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=unreachable)
+
+config UBSAN_SIGNED_OVERFLOW
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=signed-integer-overflow)
+
+config UBSAN_UNSIGNED_OVERFLOW
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=unsigned-integer-overflow)
+
+config UBSAN_OBJECT_SIZE
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=object-size)
+
+config UBSAN_BOOL
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=bool)
+
+config UBSAN_ENUM
+	def_bool UBSAN_MISC
+	depends on $(cc-option,-fsanitize=enum)
+
 config UBSAN_SANITIZE_ALL
 	bool "Enable instrumentation for the entire kernel"
 	depends on ARCH_HAS_UBSAN_SANITIZE_ALL
@@ -85,6 +139,7 @@ config UBSAN_ALIGNMENT
 	bool "Enable checks for pointers alignment"
 	default !HAVE_EFFICIENT_UNALIGNED_ACCESS
 	depends on !UBSAN_TRAP
+	depends on $(cc-option,-fsanitize=alignment)
 	help
 	  This option enables the check of unaligned memory accesses.
 	  Enabling this option on architectures that support unaligned
scripts/Makefile.ubsan renamed from scripts/Makefile.ubsan~ubsan-move-cc-option-tests-into-kconfig +15 -30

48 lines not on this page · raw message

_

author maintainer
#84 lore raw

PATCH 76/95 ubsan: disable object-size sanitizer under GCC

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: disable object-size sanitizer under GCC

GCC's -fsanitize=object-size (as part of CONFIG_UBSAN_MISC) greatly increases stack utilization. Do not allow this under GCC.

---

 lib/Kconfig.ubsan |    3 +++
 1 file changed, 3 insertions(+)
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-disable-object-size-sanitizer-under-gcc +3 -0
@@ -113,6 +113,9 @@ config UBSAN_UNSIGNED_OVERFLOW
 
 config UBSAN_OBJECT_SIZE
 	def_bool UBSAN_MISC
+	# gcc hugely expands stack usage with -fsanitize=object-size
+	# https://lore.kernel.org/lkml/CAHk-=wjPasyJrDuwDnpHJS2TuQfExwe=px-SzLeN8GFMAQJPmQ@mail.gmail.com/
+	depends on !CC_IS_GCC
 	depends on $(cc-option,-fsanitize=object-size)
 
 config UBSAN_BOOL

_

author maintainer
#85 lore raw

PATCH 77/95 ubsan: disable UBSAN_TRAP for all*config

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: disable UBSAN_TRAP for all*config

Doing all*config builds attempts to build as much as possible. UBSAN_TRAP effectively short-circuits lib/usban.c, so it should be disabled for COMPILE_TEST so that the lib/ubsan.c code gets built.

Cc Andrey Ryabinin <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc George Popescu <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Marco Elver <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Randy Dunlap <[email protected]>

---

 lib/Kconfig.ubsan |    1 +
 1 file changed, 1 insertion(+)
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-disable-ubsan_trap-for-allconfig +1 -0
@@ -14,6 +14,7 @@ if UBSAN
 
 config UBSAN_TRAP
 	bool "On Sanitizer warnings, abort the running kernel code"
+	depends on !COMPILE_TEST
 	depends on $(cc-option, -fsanitize-undefined-trap-on-error)
 	help
 	  Building kernels with Sanitizer features enabled tends to grow

_

author maintainer
#86 lore raw

PATCH 78/95 ubsan: enable for all*config builds

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: enable for all*config builds

With UBSAN_OBJECT_SIZE disabled for GCC, only UBSAN_ALIGNMENT remained a noisy UBSAN option. Disable it for COMPILE_TEST so the rest of UBSAN can be used for full all*config builds or other large combinations.

[[email protected]: add .data..Lubsan_data*/.data..Lubsan_type* sections explicitly]
Cc Andrey Ryabinin <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc George Popescu <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Marco Elver <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Randy Dunlap <[email protected]>

---

 arch/powerpc/kernel/vmlinux.lds.S |    4 ++++
 lib/Kconfig.ubsan                 |    3 +--
 2 files changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/kernel/vmlinux.lds.S renamed from arch/powerpc/kernel/vmlinux.lds.S~ubsan-enable-for-allconfig-builds +4 -0
@@ -313,6 +313,10 @@ SECTIONS
 #else
 	.data : AT(ADDR(.data) - LOAD_OFFSET) {
 		DATA_DATA
+#ifdef CONFIG_UBSAN
+		*(.data..Lubsan_data*)
+		*(.data..Lubsan_type*)
+#endif
 		*(.data.rel*)
 		*(.toc1)
 		*(.branch_lt)
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-enable-for-allconfig-builds +1 -2
@@ -130,7 +130,6 @@ config UBSAN_ENUM
 config UBSAN_SANITIZE_ALL
 	bool "Enable instrumentation for the entire kernel"
 	depends on ARCH_HAS_UBSAN_SANITIZE_ALL
-	depends on !COMPILE_TEST
 	default y
 	help
 	  This option activates instrumentation for the entire kernel.
@@ -142,7 +141,7 @@ config UBSAN_SANITIZE_ALL
 config UBSAN_ALIGNMENT
 	bool "Enable checks for pointers alignment"
 	default !HAVE_EFFICIENT_UNALIGNED_ACCESS
-	depends on !UBSAN_TRAP
+	depends on !UBSAN_TRAP && !COMPILE_TEST
 	depends on $(cc-option,-fsanitize=alignment)
 	help
 	  This option enables the check of unaligned memory accesses.

_

author maintainer
#87 lore raw

PATCH 79/95 ubsan: remove UBSAN_MISC in favor of individual options

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: remove UBSAN_MISC in favor of individual options

Make each UBSAN option individually selectable and remove UBSAN_MISC which no longer has any purpose. Add help text for each Kconfig, and include a reference to the Clang sanitizer documentation. Disable unsigned overflow by default (not available with GCC and makes x86 unbootable with Clang). Disable unreachable when objtool is in use (redundant and confuses things:
instrumentation appears at unreachable locations).

Cc Andrey Ryabinin <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc George Popescu <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Marco Elver <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Randy Dunlap <[email protected]>

---

 Documentation/dev-tools/ubsan.rst |    1 
 lib/Kconfig.ubsan                 |   82 +++++++++++++++++++---------
 2 files changed, 57 insertions(+), 26 deletions(-)
Documentation/dev-tools/ubsan.rst renamed from Documentation/dev-tools/ubsan.rst~ubsan-remove-ubsan_misc-in-favor-of-individual-options +1 -0
@@ -86,3 +86,4 @@ References
 
 .. _1: https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Debugging-Options.html
 .. _2: https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
+.. _3: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
lib/Kconfig.ubsan renamed from lib/Kconfig.ubsan~ubsan-remove-ubsan_misc-in-favor-of-individual-options +56 -26

120 lines not on this page · raw message

_

author maintainer
#88 lore raw

PATCH 80/95 ubsan: expand tests and reporting

In reply to Andrew Morton

From: Kees Cook <[email protected]>
Subject: ubsan: expand tests and reporting

Expand the UBSAN tests to include some additional UB cases. Notably the out-of-bounds enum loading appears not to work. Also include per-test reporting, including the relevant CONFIG_UBSAN... Kconfigs.

Cc Andrey Ryabinin <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc George Popescu <[email protected]>
Cc Herbert Xu <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Marco Elver <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Michal Marek <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Oberparleiter <[email protected]>
Cc Randy Dunlap <[email protected]>

---

 lib/test_ubsan.c |   74 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 66 insertions(+), 8 deletions(-)
lib/test_ubsan.c renamed from lib/test_ubsan.c~ubsan-expand-tests-and-reporting +66 -8
@@ -5,32 +5,54 @@
 
 typedef void(*test_ubsan_fp)(void);
 
+#define UBSAN_TEST(config, ...)	do {					\
+		pr_info("%s " __VA_ARGS__ "%s(%s=%s)\n", __func__,	\
+			sizeof(" " __VA_ARGS__) > 2 ? " " : "",		\
+			#config, IS_ENABLED(config) ? "y" : "n");	\
+	} while (0)
+
 static void test_ubsan_add_overflow(void)
 {
 	volatile int val = INT_MAX;
+	volatile unsigned int uval = UINT_MAX;
 
+	UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
 	val += 2;
+
+	UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
+	uval += 2;
 }
 
 static void test_ubsan_sub_overflow(void)
 {
 	volatile int val = INT_MIN;
+	volatile unsigned int uval = 0;
 	volatile int val2 = 2;
 
+	UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
 	val -= val2;
+
+	UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
+	uval -= val2;
 }
 
 static void test_ubsan_mul_overflow(void)
 {
 	volatile int val = INT_MAX / 2;
+	volatile unsigned int uval = UINT_MAX / 2;
 
+	UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
 	val *= 3;
+
+	UBSAN_TEST(CONFIG_UBSAN_UNSIGNED_OVERFLOW);
+	uval *= 3;
 }
 
 static void test_ubsan_negate_overflow(void)
 {
 	volatile int val = INT_MIN;
 
+	UBSAN_TEST(CONFIG_UBSAN_SIGNED_OVERFLOW);
 	val = -val;
 }
 
@@ -39,37 +61,67 @@ static void test_ubsan_divrem_overflow(v
 	volatile int val = 16;
 	volatile int val2 = 0;
 
+	UBSAN_TEST(CONFIG_UBSAN_DIV_ZERO);
 	val /= val2;
 }
 
 static void test_ubsan_shift_out_of_bounds(void)
 {
-	volatile int val = -1;
-	int val2 = 10;
+	volatile int neg = -1, wrap = 4;
+	int val1 = 10;
+	int val2 = INT_MAX;
+
+	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "negative exponent");
+	val1 <<= neg;
 
-	val2 <<= val;
+	UBSAN_TEST(CONFIG_UBSAN_SHIFT, "left overflow");
+	val2 <<= wrap;
 }
 
 static void test_ubsan_out_of_bounds(void)
 {
-	volatile int i = 4, j = 5;
+	volatile int i = 4, j = 5, k = -1;
+	volatile char above[4] = { }; /* Protect surrounding memory. */
 	volatile int arr[4];
+	volatile char below[4] = { }; /* Protect surrounding memory. */
 
+	above[0] = below[0];
+
+	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
 	arr[j] = i;
+
+	UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
+	arr[k] = i;
 }
 
+enum ubsan_test_enum {
+	UBSAN_TEST_ZERO = 0,
+	UBSAN_TEST_ONE,
+	UBSAN_TEST_MAX,
+};
+
 static void test_ubsan_load_invalid_value(void)
 {
 	volatile char *dst, *src;
 	bool val, val2, *ptr;
-	char c = 4;
+	enum ubsan_test_enum eval, eval2, *eptr;
+	unsigned char c = 0xff;
 
+	UBSAN_TEST(CONFIG_UBSAN_BOOL, "bool");
 	dst = (char *)&val;
 	src = &c;
 	*dst = *src;
 
 	ptr = &val2;
 	val2 = val;
+
+	UBSAN_TEST(CONFIG_UBSAN_ENUM, "enum");
+	dst = (char *)&eval;
+	src = &c;
+	*dst = *src;
+
+	eptr = &eval2;
+	eval2 = eval;
 }
 
 static void test_ubsan_null_ptr_deref(void)
@@ -77,6 +129,7 @@ static void test_ubsan_null_ptr_deref(vo
 	volatile int *ptr = NULL;
 	int val;
 
+	UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
 	val = *ptr;
 }
 
@@ -85,6 +138,7 @@ static void test_ubsan_misaligned_access
 	volatile char arr[5] __aligned(4) = {1, 2, 3, 4, 5};
 	volatile int *ptr, val = 6;
 
+	UBSAN_TEST(CONFIG_UBSAN_ALIGNMENT);
 	ptr = (int *)(arr + 1);
 	*ptr = val;
 }
@@ -95,6 +149,7 @@ static void test_ubsan_object_size_misma
 	volatile int val __aligned(8) = 4;
 	volatile long long *ptr, val2;
 
+	UBSAN_TEST(CONFIG_UBSAN_OBJECT_SIZE);
 	ptr = (long long *)&val;
 	val2 = *ptr;
 }
@@ -104,15 +159,19 @@ static const test_ubsan_fp test_ubsan_ar
 	test_ubsan_sub_overflow,
 	test_ubsan_mul_overflow,
 	test_ubsan_negate_overflow,
-	test_ubsan_divrem_overflow,
 	test_ubsan_shift_out_of_bounds,
 	test_ubsan_out_of_bounds,
 	test_ubsan_load_invalid_value,
-	//test_ubsan_null_ptr_deref, /* exclude it because there is a crash */
 	test_ubsan_misaligned_access,
 	test_ubsan_object_size_mismatch,
 };
 
+/* Excluded because they Oops the module. */
+static const test_ubsan_fp skip_ubsan_array[] = {
+	test_ubsan_divrem_overflow,
+	test_ubsan_null_ptr_deref,
+};
+
 static int __init test_ubsan_init(void)
 {
 	unsigned int i;
@@ -120,7 +179,6 @@ static int __init test_ubsan_init(void)
 	for (i = 0; i < ARRAY_SIZE(test_ubsan_array); i++)
 		test_ubsan_array[i]();
 
-	(void)test_ubsan_null_ptr_deref; /* to avoid unsed-function warning */
 	return 0;
 }
 module_init(test_ubsan_init);

_

author maintainer
#89 lore raw

PATCH 81/95 kcov: don't instrument with UBSAN

In reply to Andrew Morton

From: Dmitry Vyukov <[email protected]>
Subject: kcov: don't instrument with UBSAN

Both KCOV and UBSAN use compiler instrumentation. If UBSAN detects a bug in KCOV, it may cause infinite recursion via printk and other common functions. We already don't instrument KCOV with KASAN/KCSAN for this reason, don't instrument it with UBSAN as well.

As a side effect this also resolves the following gcc warning:

conflicting types for built-in function '__sanitizer_cov_trace_switch'; expected 'void(long unsigned int, void *)' [-Wbuiltin-declaration-mismatch]

It's only reported when kcov.c is compiled with any of the sanitizers enabled. Size of the arguments is correct, it's just that gcc uses 'long' on 64-bit arches and 'long long' on 32-bit arches, while kernel type is always 'long long'.

---

 kernel/Makefile |    3 +++
 1 file changed, 3 insertions(+)
kernel/Makefile renamed from kernel/Makefile~kcov-dont-instrument-with-ubsan +3 -0
@@ -34,8 +34,11 @@ KCOV_INSTRUMENT_extable.o := n
 KCOV_INSTRUMENT_stacktrace.o := n
 # Don't self-instrument.
 KCOV_INSTRUMENT_kcov.o := n
+# If sanitizers detect any issues in kcov, it may lead to recursion
+# via printk, etc.
 KASAN_SANITIZE_kcov.o := n
 KCSAN_SANITIZE_kcov.o := n
+UBSAN_SANITIZE_kcov.o := n
 CFLAGS_kcov.o := $(call cc-option, -fno-conserve-stack) -fno-stack-protector
 
 obj-y += sched/

_

author maintainer
#90 lore raw

PATCH 82/95 lib/ubsan.c: mark type_check_kinds with static keyword

In reply to Andrew Morton

From: Zou Wei <[email protected]>
Subject: lib/ubsan.c: mark type_check_kinds with static keyword

Fix the following sparse warning:

lib/ubsan.c:20:12: warning: symbol 'type_check_kinds' was not declared. Should it be static?

---

 lib/ubsan.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/ubsan.c renamed from lib/ubsan.c~lib-ubsanc-mark-type_check_kinds-with-static-keyword +1 -1
@@ -17,7 +17,7 @@
 
 #include "ubsan.h"
 
-const char *type_check_kinds[] = {
+static const char * const type_check_kinds[] = {
 	"load of",
 	"store to",
 	"reference binding to",

_

author maintainer
#91 lore raw

PATCH 83/95 reboot: refactor and comment the cpu selection code

In reply to Andrew Morton

From: Matteo Croce <[email protected]>
Subject: reboot: refactor and comment the cpu selection code

Small improvements to the code, without changing the way it works:
- use a local variable, to avoid a small time lapse where reboot_cpu can have an invalid value
- comment the code which is not easy to understand at a glance
- merge two identical code blocks into one
- replace pointer arithmetics with equivalent array syntax

Cc Arnd Bergmann <[email protected]>
Cc Fabian Frederick <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Kees Cook <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Robin Holt <[email protected]>

---

 kernel/reboot.c |   30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
kernel/reboot.c renamed from kernel/reboot.c~reboot-refactor-and-comment-the-cpu-selection-code +17 -13
@@ -553,20 +553,24 @@ static int __init reboot_setup(char *str
 			break;
 
 		case 's':
-			if (isdigit(*(str+1)))
-				reboot_cpu = simple_strtoul(str+1, NULL, 0);
-			else if (str[1] == 'm' && str[2] == 'p' &&
-							isdigit(*(str+3)))
-				reboot_cpu = simple_strtoul(str+3, NULL, 0);
-			else
+			/*
+			 * reboot_cpu is s[mp]#### with #### being the processor
+			 * to be used for rebooting. Skip 's' or 'smp' prefix.
+			 */
+			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
+
+			if (isdigit(str[0])) {
+				int cpu = simple_strtoul(str, NULL, 0);
+
+				if (cpu >= num_possible_cpus()) {
+					pr_err("Ignoring the CPU number in reboot= option. "
+					"CPU %d exceeds possible cpu number %d\n",
+					cpu, num_possible_cpus());
+					break;
+				}
+				reboot_cpu = cpu;
+			} else
 				*mode = REBOOT_SOFT;
-			if (reboot_cpu >= num_possible_cpus()) {
-				pr_err("Ignoring the CPU number in reboot= option. "
-				       "CPU %d exceeds possible cpu number %d\n",
-				       reboot_cpu, num_possible_cpus());
-				reboot_cpu = 0;
-				break;
-			}
 			break;
 
 		case 'g':

_

author maintainer
#92 lore raw

PATCH 84/95 reboot: allow to specify reboot mode via sysfs

In reply to Andrew Morton

From: Matteo Croce <[email protected]>
Subject: reboot: allow to specify reboot mode via sysfs

The kernel cmdline reboot= option offers some sort of control on how the reboot is issued.

We don't always know in advance what type of reboot to perform.

Sometimes a warm reboot is preferred to persist certain memory regions across the reboot. Others a cold one is needed to apply a future system update that makes a memory memory model change, like changing the base page size or resizing a persistent memory region.

Or simply we want to enable reboot_force because we noticed that something bad happened.

Add handles in sysfs to allow setting these reboot options, so they can be changed when the system is booted, other than at boot time.

The handlers are under <sysfs>/kernel/reboot, can be read to get the current configuration and written to alter it.

	# cd /sys/kernel/reboot/
	# grep . *
	cpu:0
	force:0
	mode:cold
	type:acpi
	# echo 2 >cpu
	# echo yes >force
	# echo soft >mode
	# echo bios >type
	# grep . *
	cpu:2
	force:1
	mode:soft
	type:bios

Before setting anything, check for CAP_SYS_BOOT capability, so it's possible to allow an unpriviledged process to change these settings simply by relaxing the handles permissions, without opening them to the world.

---

 Documentation/ABI/testing/sysfs-kernel-reboot |   32 ++
 kernel/reboot.c                               |  206 ++++++++++++++++
 2 files changed, 238 insertions(+)
Documentation/ABI/testing/sysfs-kernel-reboot new file +32 -0
@@ -0,0 +1,32 @@
+What:		/sys/kernel/reboot
+Date:		November 2020
+KernelVersion:	5.11
+Contact:	Matteo Croce <[email protected]>
+Description:	Interface to set the kernel reboot behavior, similarly to
+		what can be done via the reboot= cmdline option.
+		(see Documentation/admin-guide/kernel-parameters.txt)
+
+What:		/sys/kernel/reboot/mode
+Date:		November 2020
+KernelVersion:	5.11
+Contact:	Matteo Croce <[email protected]>
+Description:	Reboot mode. Valid values are: cold warm hard soft gpio
+
+What:		/sys/kernel/reboot/type
+Date:		November 2020
+KernelVersion:	5.11
+Contact:	Matteo Croce <[email protected]>
+Description:	Reboot type. Valid values are: bios acpi kbd triple efi pci
+
+What:		/sys/kernel/reboot/cpu
+Date:		November 2020
+KernelVersion:	5.11
+Contact:	Matteo Croce <[email protected]>
+Description:	CPU number to use to reboot.
+
+What:		/sys/kernel/reboot/force
+Date:		November 2020
+KernelVersion:	5.11
+Contact:	Matteo Croce <[email protected]>
+Description:	Don't wait for any other CPUs on reboot and
+		avoid anything that could hang.
kernel/reboot.c renamed from kernel/reboot.c~reboot-allow-to-specify-reboot-mode-via-sysfs +206 -0

209 lines not on this page · raw message

_

author maintainer
#93 lore raw

PATCH 85/95 reboot: remove cf9_safe from allowed types and rename cf9_force

In reply to Andrew Morton

From: Matteo Croce <[email protected]>
Subject: reboot: remove cf9_safe from allowed types and rename cf9_force

BOOT_CF9_SAFE_STR is an internal value used only by the x86 code and it's not possible to set it from userspace.

Remove it, and rename 'cf9_force' to 'pci', so to make it coherent with the kernel command line reboot= option.

Tested with this script:

cd /sys/kernel/reboot/

for i in cold warm hard soft gpio; do
echo $i >mode
read j <mode
[ $i = $j ] || echo "mode $i != $j"
done

for i in bios acpi kbd triple efi pci; do
echo $i >type
read j <type
[ $i = $j ] || echo "type $i != $j"
done

for i in $(seq 0 $(nproc --ignore=1)); do
echo $i >cpu
read j <cpu
[ $i = $j ] || echo "cpu $i != $j"
done

for i in 0 1; do
echo $i >force
read j <force
[ $i = $j ] || echo "force $i != $j"
done

Fixes eab8da48579d ("reboot: allow to specify reboot mode via sysfs")
Cc Arnd Bergmann <[email protected]>
Cc Guenter Roeck <[email protected]>
Cc Kees Cook <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Pavel Tatashin <[email protected]>
Cc Petr Mladek <[email protected]>
Cc Tyler Hicks <[email protected]>

---

 kernel/reboot.c |   12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
kernel/reboot.c renamed from kernel/reboot.c~reboot-remove-cf9_safe-from-allowed-types-and-rename-cf9_force +3 -9
@@ -615,8 +615,7 @@ __setup("reboot=", reboot_setup);
 #define BOOT_BIOS_STR		"bios"
 #define BOOT_ACPI_STR		"acpi"
 #define BOOT_EFI_STR		"efi"
-#define BOOT_CF9_FORCE_STR	"cf9_force"
-#define BOOT_CF9_SAFE_STR	"cf9_safe"
+#define BOOT_PCI_STR		"pci"
 
 static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
@@ -688,10 +687,7 @@ static ssize_t type_show(struct kobject
 		val = BOOT_EFI_STR;
 		break;
 	case BOOT_CF9_FORCE:
-		val = BOOT_CF9_FORCE_STR;
-		break;
-	case BOOT_CF9_SAFE:
-		val = BOOT_CF9_SAFE_STR;
+		val = BOOT_PCI_STR;
 		break;
 	default:
 		val = REBOOT_UNDEFINED_STR;
@@ -715,10 +711,8 @@ static ssize_t type_store(struct kobject
 		reboot_type = BOOT_ACPI;
 	else if (!strncmp(buf, BOOT_EFI_STR, strlen(BOOT_EFI_STR)))
 		reboot_type = BOOT_EFI;
-	else if (!strncmp(buf, BOOT_CF9_FORCE_STR, strlen(BOOT_CF9_FORCE_STR)))
+	else if (!strncmp(buf, BOOT_PCI_STR, strlen(BOOT_PCI_STR)))
 		reboot_type = BOOT_CF9_FORCE;
-	else if (!strncmp(buf, BOOT_CF9_SAFE_STR, strlen(BOOT_CF9_SAFE_STR)))
-		reboot_type = BOOT_CF9_SAFE;
 	else
 		return -EINVAL;
 

_

author maintainer
#94 lore raw

PATCH 86/95 reboot: allow to override reboot type if quirks are found

In reply to Andrew Morton

From: Matteo Croce <[email protected]>
Subject: reboot: allow to override reboot type if quirks are found

Patch series "reboot: sysfs improvements".

Some improvements to the sysfs reboot interface: hide not working settings and support machines with known reboot quirks.

This patch (of 2):

On some machines a quirk can force a specific reboot type. Quirks are found during a DMI scan, the list of machines which need special reboot handling is defined in reboot_dmi_table.

The kernel command line reboot= option overrides this via a global variable reboot_default, so that the reboot type requested in the command line is really performed.

This was not true when setting the reboot type via the new sysfs interface. Fix this by setting reboot_default upon the first change, like reboot_setup() does for the command line.

---

 kernel/reboot.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/reboot.c renamed from kernel/reboot.c~reboot-allow-to-override-reboot-type-if-quirks-are-found +6 -0
@@ -662,6 +662,8 @@ static ssize_t mode_store(struct kobject
 	else
 		return -EINVAL;
 
+	reboot_default = 0;
+
 	return count;
 }
 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
@@ -716,6 +718,8 @@ static ssize_t type_store(struct kobject
 	else
 		return -EINVAL;
 
+	reboot_default = 0;
+
 	return count;
 }
 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
@@ -741,6 +745,7 @@ static ssize_t cpu_store(struct kobject
 	if (cpunum >= num_possible_cpus())
 		return -ERANGE;
 
+	reboot_default = 0;
 	reboot_cpu = cpunum;
 
 	return count;
@@ -762,6 +767,7 @@ static ssize_t force_store(struct kobjec
 	if (kstrtobool(buf, &res))
 		return -EINVAL;
 
+	reboot_default = 0;
 	reboot_force = res;
 
 	return count;

_

author maintainer
#95 lore raw

PATCH 87/95 reboot: hide from sysfs not applicable settings

In reply to Andrew Morton

From: Matteo Croce <[email protected]>
Subject: reboot: hide from sysfs not applicable settings

Not all the reboot settings from both the kernel command line or sysfs interface are available to all platforms.

Filter out reboot_type and reboot_force which are x86 only, and also remove reboot_cpu on kernels without SMP support.

This saves some space, and avoid confusing the user with settings which will have no effect.

---

 kernel/reboot.c |   54 ++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 23 deletions(-)
kernel/reboot.c renamed from kernel/reboot.c~reboot-hide-from-sysfs-not-applicable-settings +31 -23
@@ -668,6 +668,29 @@ static ssize_t mode_store(struct kobject
 }
 static struct kobj_attribute reboot_mode_attr = __ATTR_RW(mode);
 
+#ifdef CONFIG_X86
+static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%d\n", reboot_force);
+}
+static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
+			  const char *buf, size_t count)
+{
+	bool res;
+
+	if (!capable(CAP_SYS_BOOT))
+		return -EPERM;
+
+	if (kstrtobool(buf, &res))
+		return -EINVAL;
+
+	reboot_default = 0;
+	reboot_force = res;
+
+	return count;
+}
+static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
+
 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
 	const char *val;
@@ -723,7 +746,9 @@ static ssize_t type_store(struct kobject
 	return count;
 }
 static struct kobj_attribute reboot_type_attr = __ATTR_RW(type);
+#endif
 
+#ifdef CONFIG_SMP
 static ssize_t cpu_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%d\n", reboot_cpu);
@@ -751,34 +776,17 @@ static ssize_t cpu_store(struct kobject
 	return count;
 }
 static struct kobj_attribute reboot_cpu_attr = __ATTR_RW(cpu);
-
-static ssize_t force_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
-{
-	return sprintf(buf, "%d\n", reboot_force);
-}
-static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
-			  const char *buf, size_t count)
-{
-	bool res;
-
-	if (!capable(CAP_SYS_BOOT))
-		return -EPERM;
-
-	if (kstrtobool(buf, &res))
-		return -EINVAL;
-
-	reboot_default = 0;
-	reboot_force = res;
-
-	return count;
-}
-static struct kobj_attribute reboot_force_attr = __ATTR_RW(force);
+#endif
 
 static struct attribute *reboot_attrs[] = {
 	&reboot_mode_attr.attr,
+#ifdef CONFIG_X86
+	&reboot_force_attr.attr,
 	&reboot_type_attr.attr,
+#endif
+#ifdef CONFIG_SMP
 	&reboot_cpu_attr.attr,
-	&reboot_force_attr.attr,
+#endif
 	NULL,
 };
 

_

author maintainer
#96 lore raw

PATCH 88/95 fault-injection: handle EI_ETYPE_TRUE

In reply to Andrew Morton

From: Barnabás Pőcze <[email protected]>
Subject: fault-injection: handle EI_ETYPE_TRUE

Commit af3b854492f351d1 ("mm/page_alloc.c: allow error injection") introduced EI_ETYPE_TRUE, but did not extend

* lib/error-inject.c:error_type_string(), and
* kernel/fail_function.c:adjust_error_retval()

to accommodate for this change.

Handle EI_ETYPE_TRUE in both functions appropriately by
* returning "TRUE" in error_type_string(),
* adjusting the return value to true (1) in adjust_error_retval().

Furthermore, simplify the logic of handling EI_ETYPE_NULL in adjust_error_retval().

---

 kernel/fail_function.c |    6 +++---
 lib/error-inject.c     |    2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)
kernel/fail_function.c renamed from kernel/fail_function.c~fault-injection-handle-ei_etype_true +3 -3
@@ -37,9 +37,7 @@ static unsigned long adjust_error_retval
 {
 	switch (get_injectable_error_type(addr)) {
 	case EI_ETYPE_NULL:
-		if (retv != 0)
-			return 0;
-		break;
+		return 0;
 	case EI_ETYPE_ERRNO:
 		if (retv < (unsigned long)-MAX_ERRNO)
 			return (unsigned long)-EINVAL;
@@ -48,6 +46,8 @@ static unsigned long adjust_error_retval
 		if (retv != 0 && retv < (unsigned long)-MAX_ERRNO)
 			return (unsigned long)-EINVAL;
 		break;
+	case EI_ETYPE_TRUE:
+		return 1;
 	}
 
 	return retv;
lib/error-inject.c renamed from lib/error-inject.c~fault-injection-handle-ei_etype_true +2 -0
@@ -180,6 +180,8 @@ static const char *error_type_string(int
 		return "ERRNO";
 	case EI_ETYPE_ERRNO_NULL:
 		return "ERRNO_NULL";
+	case EI_ETYPE_TRUE:
+		return "TRUE";
 	default:
 		return "(unknown)";
 	}

_

author maintainer
#97 lore raw

PATCH 89/95 lib/lzo/lzo1x_compress.c: make lzogeneric1x_1_compress() static

In reply to Andrew Morton

From: Jason Yan <[email protected]>
Subject: lib/lzo/lzo1x_compress.c: make lzogeneric1x_1_compress() static

Fix the following sparse warning:

lib/lzo/lzo1x_compress.c:304:5: warning: symbol 'lzogeneric1x_1_compress' was not declared. Should it be static?

---

 lib/lzo/lzo1x_compress.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/lzo/lzo1x_compress.c renamed from lib/lzo/lzo1x_compress.c~lib-lzo-make-lzogeneric1x_1_compress-static +1 -1
@@ -301,7 +301,7 @@ finished_writing_instruction:
 	return in_end - (ii - ti);
 }
 
-int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
+static int lzogeneric1x_1_compress(const unsigned char *in, size_t in_len,
 		     unsigned char *out, size_t *out_len,
 		     void *wrkmem, const unsigned char bitstream_version)
 {

_

author maintainer
#98 lore raw

PATCH 90/95 apparmor: remove duplicate macro list_entry_is_head()

In reply to Andrew Morton

From: Andy Shevchenko <[email protected]>
Subject: apparmor: remove duplicate macro list_entry_is_head()

Strangely I hadn't had noticed the existence of the list_entry_is_head() in apparmor code when added the same one in the list.h. Luckily it's fully identical and didn't break builds. In any case we don't need a duplicate anymore, thus remove it from apparmor code.

Fixes e130816164e244 ("include/linux/list.h: add a macro to test if entry is pointing to the head")
Cc James Morris <[email protected]>
Cc "Serge E . Hallyn " <[email protected]>

---

 security/apparmor/apparmorfs.c |    3 ---
 1 file changed, 3 deletions(-)
security/apparmor/apparmorfs.c renamed from security/apparmor/apparmorfs.c~apparmor-remove-duplicate-macro-list_entry_is_head +0 -3
@@ -2046,9 +2046,6 @@ fail2:
 	return error;
 }
 
-
-#define list_entry_is_head(pos, head, member) (&pos->member == (head))
-
 /**
  * __next_ns - find the next namespace to list
  * @root: root namespace to stop search at (NOT NULL)

_

author maintainer
#99 lore raw

PATCH 91/95 mm: unexport follow_pte_pmd

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: mm: unexport follow_pte_pmd

Patch series "simplify follow_pte a bit".

This small series drops the not needed follow_pte_pmd exports, and simplifies the follow_pte family of functions a bit.

This patch (of 2):

follow_pte_pmd() is only used by the DAX code, which can't be modular.

---

 mm/memory.c |    1 -
 1 file changed, 1 deletion(-)
mm/memory.c renamed from mm/memory.c~mm-unexport-follow_pte_pmd +0 -1
@@ -4798,7 +4798,6 @@ int follow_pte_pmd(struct mm_struct *mm,
 						    ptepp, pmdpp, ptlp)));
 	return res;
 }
-EXPORT_SYMBOL(follow_pte_pmd);
 
 /**
  * follow_pfn - look up PFN at a user virtual address

_

author maintainer
#100 lore raw

PATCH 92/95 mm: simplify follow_pte{,pmd}

In reply to Andrew Morton

From: Christoph Hellwig <[email protected]>
Subject: mm: simplify follow_pte{,pmd}

Merge __follow_pte_pmd, follow_pte_pmd and follow_pte into a single follow_pte function and just pass two additional NULL arguments for the two previous follow_pte callers.

---

 arch/s390/pci/pci_mmio.c |    4 ++--
 fs/dax.c                 |    9 ++++-----
 include/linux/mm.h       |    6 +++---
 mm/memory.c              |   35 +++++------------------------------
 4 files changed, 14 insertions(+), 40 deletions(-)
arch/s390/pci/pci_mmio.c renamed from arch/s390/pci/pci_mmio.c~mm-simplify-follow_ptepmd +2 -2
@@ -170,7 +170,7 @@ SYSCALL_DEFINE3(s390_pci_mmio_write, uns
 	if (!(vma->vm_flags & VM_WRITE))
 		goto out_unlock_mmap;
 
-	ret = follow_pte_pmd(vma->vm_mm, mmio_addr, NULL, &ptep, NULL, &ptl);
+	ret = follow_pte(vma->vm_mm, mmio_addr, NULL, &ptep, NULL, &ptl);
 	if (ret)
 		goto out_unlock_mmap;
 
@@ -311,7 +311,7 @@ SYSCALL_DEFINE3(s390_pci_mmio_read, unsi
 	if (!(vma->vm_flags & VM_WRITE))
 		goto out_unlock_mmap;
 
-	ret = follow_pte_pmd(vma->vm_mm, mmio_addr, NULL, &ptep, NULL, &ptl);
+	ret = follow_pte(vma->vm_mm, mmio_addr, NULL, &ptep, NULL, &ptl);
 	if (ret)
 		goto out_unlock_mmap;
 
fs/dax.c renamed from fs/dax.c~mm-simplify-follow_ptepmd +4 -5
@@ -810,12 +810,11 @@ static void dax_entry_mkclean(struct add
 		address = pgoff_address(index, vma);
 
 		/*
-		 * Note because we provide range to follow_pte_pmd it will
-		 * call mmu_notifier_invalidate_range_start() on our behalf
-		 * before taking any lock.
+		 * Note because we provide range to follow_pte it will call
+		 * mmu_notifier_invalidate_range_start() on our behalf before
+		 * taking any lock.
 		 */
-		if (follow_pte_pmd(vma->vm_mm, address, &range,
-				   &ptep, &pmdp, &ptl))
+		if (follow_pte(vma->vm_mm, address, &range, &ptep, &pmdp, &ptl))
 			continue;
 
 		/*
include/linux/mm.h renamed from include/linux/mm.h~mm-simplify-follow_ptepmd +3 -3
@@ -1641,9 +1641,9 @@ void free_pgd_range(struct mmu_gather *t
 		unsigned long end, unsigned long floor, unsigned long ceiling);
 int
 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
-int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
-		   struct mmu_notifier_range *range,
-		   pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp);
+int follow_pte(struct mm_struct *mm, unsigned long address,
+		struct mmu_notifier_range *range, pte_t **ptepp, pmd_t **pmdpp,
+		spinlock_t **ptlp);
 int follow_pfn(struct vm_area_struct *vma, unsigned long address,
 	unsigned long *pfn);
 int follow_phys(struct vm_area_struct *vma, unsigned long address,
mm/memory.c renamed from mm/memory.c~mm-simplify-follow_ptepmd +5 -30
@@ -4707,9 +4707,9 @@ int __pmd_alloc(struct mm_struct *mm, pu
 }
 #endif /* __PAGETABLE_PMD_FOLDED */
 
-static int __follow_pte_pmd(struct mm_struct *mm, unsigned long address,
-			    struct mmu_notifier_range *range,
-			    pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
+int follow_pte(struct mm_struct *mm, unsigned long address,
+	       struct mmu_notifier_range *range, pte_t **ptepp, pmd_t **pmdpp,
+	       spinlock_t **ptlp)
 {
 	pgd_t *pgd;
 	p4d_t *p4d;
@@ -4774,31 +4774,6 @@ out:
 	return -EINVAL;
 }
 
-static inline int follow_pte(struct mm_struct *mm, unsigned long address,
-			     pte_t **ptepp, spinlock_t **ptlp)
-{
-	int res;
-
-	/* (void) is needed to make gcc happy */
-	(void) __cond_lock(*ptlp,
-			   !(res = __follow_pte_pmd(mm, address, NULL,
-						    ptepp, NULL, ptlp)));
-	return res;
-}
-
-int follow_pte_pmd(struct mm_struct *mm, unsigned long address,
-		   struct mmu_notifier_range *range,
-		   pte_t **ptepp, pmd_t **pmdpp, spinlock_t **ptlp)
-{
-	int res;
-
-	/* (void) is needed to make gcc happy */
-	(void) __cond_lock(*ptlp,
-			   !(res = __follow_pte_pmd(mm, address, range,
-						    ptepp, pmdpp, ptlp)));
-	return res;
-}
-
 /**
  * follow_pfn - look up PFN at a user virtual address
  * @vma: memory mapping
@@ -4819,7 +4794,7 @@ int follow_pfn(struct vm_area_struct *vm
 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
 		return ret;
 
-	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
+	ret = follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl);
 	if (ret)
 		return ret;
 	*pfn = pte_pfn(*ptep);
@@ -4840,7 +4815,7 @@ int follow_phys(struct vm_area_struct *v
 	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
 		goto out;
 
-	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
+	if (follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl))
 		goto out;
 	pte = *ptep;
 

_

author maintainer
#101 lore raw

PATCH 93/95 mm: fix some spelling mistakes in comments

In reply to Andrew Morton

From: Haitao Shi <[email protected]>
Subject: mm: fix some spelling mistakes in comments

Fix some spelling mistakes in comments:

	udpate ==> update
	succesful ==> successful
	exmaple ==> example
	unneccessary ==> unnecessary
	stoping ==> stopping
	uknown ==> unknown

---

 mm/filemap.c     |    2 +-
 mm/huge_memory.c |    2 +-
 mm/khugepaged.c  |    2 +-
 mm/memblock.c    |    2 +-
 mm/migrate.c     |    2 +-
 mm/page_ext.c    |    2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)
mm/filemap.c renamed from mm/filemap.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -1359,7 +1359,7 @@ static int __wait_on_page_locked_async(s
 	else
 		ret = PageLocked(page);
 	/*
-	 * If we were succesful now, we know we're still on the
+	 * If we were successful now, we know we're still on the
 	 * waitqueue as we're still under the lock. This means it's
 	 * safe to remove and return success, we know the callback
 	 * isn't going to trigger.
mm/huge_memory.c renamed from mm/huge_memory.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -2391,7 +2391,7 @@ static void __split_huge_page_tail(struc
 	 * Clone page flags before unfreezing refcount.
 	 *
 	 * After successful get_page_unless_zero() might follow flags change,
-	 * for exmaple lock_page() which set PG_waiters.
+	 * for example lock_page() which set PG_waiters.
 	 */
 	page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
 	page_tail->flags |= (head->flags &
mm/khugepaged.c renamed from mm/khugepaged.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -1275,7 +1275,7 @@ static int khugepaged_scan_pmd(struct mm
 			 * PTEs are armed with uffd write protection.
 			 * Here we can also mark the new huge pmd as
 			 * write protected if any of the small ones is
-			 * marked but that could bring uknown
+			 * marked but that could bring unknown
 			 * userfault messages that falls outside of
 			 * the registered range.  So, just be simple.
 			 */
mm/memblock.c renamed from mm/memblock.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -871,7 +871,7 @@ int __init_memblock memblock_physmem_add
  * @base: base address of the region
  * @size: size of the region
  * @set: set or clear the flag
- * @flag: the flag to udpate
+ * @flag: the flag to update
  *
  * This function isolates region [@base, @base + @size), and sets/clears flag
  *
mm/migrate.c renamed from mm/migrate.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -2594,7 +2594,7 @@ static bool migrate_vma_check_page(struc
 		 * will bump the page reference count. Sadly there is no way to
 		 * differentiate a regular pin from migration wait. Hence to
 		 * avoid 2 racing thread trying to migrate back to CPU to enter
-		 * infinite loop (one stoping migration because the other is
+		 * infinite loop (one stopping migration because the other is
 		 * waiting on pte migration entry). We always return true here.
 		 *
 		 * FIXME proper solution is to rework migration_entry_wait() so
mm/page_ext.c renamed from mm/page_ext.c~mm-fix-some-spelling-mistakes-in-comments +1 -1
@@ -34,7 +34,7 @@
  *
  * The need callback is used to decide whether extended memory allocation is
  * needed or not. Sometimes users want to deactivate some features in this
- * boot and extra memory would be unneccessary. In this case, to avoid
+ * boot and extra memory would be unnecessary. In this case, to avoid
  * allocating huge chunk of memory, each clients represent their need of
  * extra memory through the need callback. If one of the need callbacks
  * returns true, it means that someone needs extra memory so that

_

author maintainer
#102 lore raw

PATCH 94/95 mmap locking API: don't check locking if the mm isn't live yet

In reply to Andrew Morton

From: Jann Horn <[email protected]>
Subject: mmap locking API: don't check locking if the mm isn't live yet

In preparation for adding a mmap_assert_locked() check in __get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine to operate on an mm without locking in the middle of execve() as long as it hasn't been installed on a process yet.

Existing code paths that do this are (reverse callgraph):

get_user_pages_remote
get_arg_page
copy_strings
copy_string_kernel
remove_arg_zero
tomoyo_dump_page
tomoyo_print_bprm
tomoyo_scan_bprm
tomoyo_environ

---

 fs/exec.c                 |    8 ++++++++
 include/linux/mm_types.h  |   10 ++++++++++
 include/linux/mmap_lock.h |   16 ++++++++++++----
 3 files changed, 30 insertions(+), 4 deletions(-)
fs/exec.c renamed from fs/exec.c~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +8 -0
@@ -1001,6 +1001,14 @@ static int exec_mmap(struct mm_struct *m
 		}
 	}
 
+#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
+	/*
+	 * From here on, the mm may be accessed concurrently, and proper locking
+	 * is required for things like get_user_pages_remote().
+	 */
+	mm->mmap_lock_required = 1;
+#endif
+
 	task_lock(tsk);
 	membarrier_exec_mmap(mm);
 
include/linux/mmap_lock.h renamed from include/linux/mmap_lock.h~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +12 -4
@@ -161,14 +161,22 @@ static inline void mmap_read_unlock_non_
 
 static inline void mmap_assert_locked(struct mm_struct *mm)
 {
-	lockdep_assert_held(&mm->mmap_lock);
-	VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
+#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
+	if (mm->mmap_lock_required) {
+		lockdep_assert_held(&mm->mmap_lock);
+		VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
+	}
+#endif
 }
 
 static inline void mmap_assert_write_locked(struct mm_struct *mm)
 {
-	lockdep_assert_held_write(&mm->mmap_lock);
-	VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
+#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
+	if (mm->mmap_lock_required) {
+		lockdep_assert_held_write(&mm->mmap_lock);
+		VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
+	}
+#endif
 }
 
 static inline int mmap_lock_is_contended(struct mm_struct *mm)
include/linux/mm_types.h renamed from include/linux/mm_types.h~mmap-locking-api-dont-check-locking-if-the-mm-isnt-live-yet +10 -0
@@ -561,6 +561,16 @@ struct mm_struct {
 #ifdef CONFIG_IOMMU_SUPPORT
 		u32 pasid;
 #endif
+
+#if defined(CONFIG_LOCKDEP) || defined(CONFIG_DEBUG_VM)
+		/*
+		 * Notes whether this mm has been installed on a process yet.
+		 * If not, only the task going through execve() can access this
+		 * mm, and no locking is needed around get_user_pages_remote().
+		 * This flag is only used for debug checks.
+		 */
+		bool mmap_lock_required;
+#endif
 	} __randomize_layout;
 
 	/*

_

#103 lore raw

Re: [patch 94/95] mmap locking API: don't check locking if the mm isn't live yet

Quoted 7 lines · Andrew Morton

In preparation for adding a mmap_assert_locked() check in __get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine to operate on an mm without locking in the middle of execve() as long as it hasn't been installed on a process yet.

Existing code paths that do this are (reverse callgraph):

Quoted 9 lines

get_user_pages_remote
get_arg_page
copy_strings
copy_string_kernel
remove_arg_zero
tomoyo_dump_page
tomoyo_print_bprm
tomoyo_scan_bprm
tomoyo_environ

Sorry, can you please kill both this patch and the following one ("mm/gup: assert that the mmap lock is held in __get_user_pages()") from the mm tree?

I'll send new stuff (a new iteration of
https://lore.kernel.org/linux-mm/[email protected]/
"[PATCH resend v3 0/2] Broad write-locking of nascent mm in execve", followed by a resend of "mm/gup: assert that the mmap lock is held in __get_user_pages()") when it's ready.

As I noted in the cover letter of that thing, which was meant to replace this patch (but isn't ready yet - yeah, I should get back to that...), this approach doesn't really work because bprm->vma is used after the switch to the new mm.

Sorry about the mess... :/

I guess the next time this happens, I should just immediately email you requesting that the queued-up patches should be dropped once an issue is identified...

#104 lore raw

Re: [patch 94/95] mmap locking API: don't check locking if the mm isn't live yet

Quoted 22 lines · Jann Horn
Quoted 7 lines · Andrew Morton

In preparation for adding a mmap_assert_locked() check in __get_user_pages(), teach the mmap_assert_*locked() helpers that it's fine to operate on an mm without locking in the middle of execve() as long as it hasn't been installed on a process yet.

Existing code paths that do this are (reverse callgraph):

Quoted 9 lines

get_user_pages_remote
get_arg_page
copy_strings
copy_string_kernel
remove_arg_zero
tomoyo_dump_page
tomoyo_print_bprm
tomoyo_scan_bprm
tomoyo_environ

Sorry, can you please kill both this patch and the following one ("mm/gup: assert that the mmap lock is held in __get_user_pages()") from the mm tree?

Quoted 5 lines

I'll send new stuff (a new iteration of
https://lore.kernel.org/linux-mm/[email protected]/
"[PATCH resend v3 0/2] Broad write-locking of nascent mm in execve", followed by a resend of "mm/gup: assert that the mmap lock is held in __get_user_pages()") when it's ready.

I'm glad you are still working on it, I think finally being able to add lockdep to get_user_pages will be a help. I've fixed a number of wrongly locked get_user_pages users :(

Thanks,
Jason

author maintainer
#105 lore raw

PATCH 95/95 mm/gup: assert that the mmap lock is held in __get_user_pages()

In reply to Andrew Morton

From: Jann Horn <[email protected]>
Subject: mm/gup: assert that the mmap lock is held in __get_user_pages()

After having cleaned up all GUP callers (except for the atomisp staging driver, which currently gets mmap locking completely wrong [1]) to always ensure that they hold the mmap lock when calling into GUP (unless the mm is not yet globally visible), add an assertion to make sure it stays that way going forward.

[1] https://lore.kernel.org/lkml/CAG48ez3tZAb9JVhw4T5e-i=h2_DUZxfNRTDsagSRCVazNXx5qA@mail.gmail.com/

---

 mm/gup.c |    2 ++
 1 file changed, 2 insertions(+)
mm/gup.c renamed from mm/gup.c~mm-gup-assert-that-the-mmap-lock-is-held-in-__get_user_pages +2 -0
@@ -996,6 +996,8 @@ static long __get_user_pages(struct mm_s
 	struct vm_area_struct *vma = NULL;
 	struct follow_page_context ctx = { NULL };
 
+	mmap_assert_locked(mm);
+
 	if (!nr_pages)
 		return 0;
 

_