Skip to contentSkip to conversation

gcc-plugins: Add a plugin to ease debugging of memory ordering bugs

RFC PATCH New Gautam Menghani 2 participants 2 messages Oct 18 – Oct 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 gcc-plugins: Add a plugin to ease debugging of memory ordering bugs

On CPUs that support out of order execution, barriers are used to make sure that if a cpu updates a variable, all other cpus see the correct value. However, sometimes developers forget to put the barriers in the right place and this ends up causing bugs.

Introduce a new plugin that can insert barriers after every load/store to memory to make it easy to verify if a given bug is due to a missing barrier.

Testing:
To test this plugin on IBM Power systems, I reverted the commit bb9b52bd51dc ("KVM: PPC: Book3S HV: Add missing barriers to XIVE code and document them").

I was able to see lockups in the KVM guest with the commit reverted. I then used the barriers plugin to insert barriers, and noticed that the lockups were no longer observed in the KVM guest.

Assisted-by LLM IBM-Bob
---
 scripts/Makefile.gcc-plugins          |   2 +
 scripts/gcc-plugins/Kconfig           |   6 ++
 scripts/gcc-plugins/barriers_plugin.c | 134 ++++++++++++++++++++++++++
 3 files changed, 142 insertions(+)
 create mode 100644 scripts/gcc-plugins/barriers_plugin.c
scripts/Makefile.gcc-plugins +2 -0
index b0e1423b09c2..cefc1cdad8df 100644
@@ -8,6 +8,8 @@ ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
 endif
 export DISABLE_LATENT_ENTROPY_PLUGIN
 
+gcc-plugin-$(CONFIG_GCC_PLUGIN_BARRIERS)	+= barriers_plugin.so
+
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
 GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) -DGCC_PLUGINS
scripts/gcc-plugins/Kconfig +6 -0
index 6b34ba19358d..d73e00fbb732 100644
@@ -36,4 +36,10 @@ config GCC_PLUGIN_LATENT_ENTROPY
 	   * https://grsecurity.net/
 	   * https://pax.grsecurity.net/
 
+config GCC_PLUGIN_BARRIERS
+	bool "Insert barriers after every load/store"
+	help
+	  By saying Y here barriers will be inserted after every load/store
+	  instruction.
+
 endif
scripts/gcc-plugins/barriers_plugin.c new file +134 -0

135 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw
kernel test robot noticed the following build errors:

Hi Gautam,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on kees/for-next/hardening]
[also build test ERROR on v7.3]
[cannot apply to linus/master next-20261021]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Gautam-Menghani/gcc-plugins-Add-a-plugin-to-ease-debugging-of-memory-ordering-bugs/20261018-205606
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
patch link:    https://lore.kernel.org/r/20261018152608.1323494-1-gautam%40linux.ibm.com
patch subject: [RFC PATCH] gcc-plugins: Add a plugin to ease debugging of memory ordering bugs
config: um-allyesconfig (https://download.01.org/0day-ci/archive/20261023/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from <command-line>:
net/core/skbuff.c: In function 'skb_extensions_init':

>> include/linux/compiler_types.h:702:45: error: call to '__compiletime_assert_1377' declared with attribute error: BUILD_BUG_ON failed: skb_ext_total_length() > 255
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |                                             ^
   include/linux/compiler_types.h:683:25: note: in definition of macro '__compiletime_assert'
     683 |                         prefix ## suffix();                             \
         |                         ^~~~~~
   include/linux/compiler_types.h:702:9: note: in expansion of macro '_compiletime_assert'
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:40:37: note: in expansion of macro 'compiletime_assert'
      40 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
         |                                     ^~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:51:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
      51 |         BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
         |         ^~~~~~~~~~~~~~~~
   net/core/skbuff.c:5188:9: note: in expansion of macro 'BUILD_BUG_ON'
    5188 |         BUILD_BUG_ON(skb_ext_total_length() > 255);
         |         ^~~~~~~~~~~~
--
   In file included from <command-line>:
   lib/tests/ffs_kunit.c: In function 'build_test___fls.isra':
>> include/linux/compiler_types.h:702:45: error: call to '__compiletime_assert_246' declared with attribute error: BUILD_BUG_ON failed: init___fls < 32
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |                                             ^
   include/linux/compiler_types.h:683:25: note: in definition of macro '__compiletime_assert'
     683 |                         prefix ## suffix();                             \
         |                         ^~~~~~
   include/linux/compiler_types.h:702:9: note: in expansion of macro '_compiletime_assert'
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:40:37: note: in expansion of macro 'compiletime_assert'
      40 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
         |                                     ^~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:51:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
      51 |         BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
         |         ^~~~~~~~~~~~~~~~
   lib/tests/ffs_kunit.c:510:9: note: in expansion of macro 'BUILD_BUG_ON'
     510 |         BUILD_BUG_ON(init_##func < 32);                                 \
         |         ^~~~~~~~~~~~
   lib/tests/ffs_kunit.c:521:1: note: in expansion of macro 'CREATE_WRAPPER'
     521 | CREATE_WRAPPER(__fls)
         | ^~~~~~~~~~~~~~
   lib/tests/ffs_kunit.c: In function 'build_test_fls.isra':
>> include/linux/compiler_types.h:702:45: error: call to '__compiletime_assert_244' declared with attribute error: BUILD_BUG_ON failed: init_fls < 32
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |                                             ^
   include/linux/compiler_types.h:683:25: note: in definition of macro '__compiletime_assert'
     683 |                         prefix ## suffix();                             \
         |                         ^~~~~~
   include/linux/compiler_types.h:702:9: note: in expansion of macro '_compiletime_assert'
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:40:37: note: in expansion of macro 'compiletime_assert'
      40 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
         |                                     ^~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:51:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
      51 |         BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
         |         ^~~~~~~~~~~~~~~~
   lib/tests/ffs_kunit.c:510:9: note: in expansion of macro 'BUILD_BUG_ON'
     510 |         BUILD_BUG_ON(init_##func < 32);                                 \
         |         ^~~~~~~~~~~~
   lib/tests/ffs_kunit.c:519:1: note: in expansion of macro 'CREATE_WRAPPER'
     519 | CREATE_WRAPPER(fls)
         | ^~~~~~~~~~~~~~
--
   In file included from <command-line>:
   drivers/vfio/pci/virtio/legacy_io.c: In function 'virtiovf_init_legacy_io':
>> include/linux/compiler_types.h:702:45: error: call to '__compiletime_assert_1024' declared with attribute error: BUILD_BUG_ON failed: !is_power_of_2(virtvdev->bar0_virtual_buf_size)
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |                                             ^
   include/linux/compiler_types.h:683:25: note: in definition of macro '__compiletime_assert'
     683 |                         prefix ## suffix();                             \
         |                         ^~~~~~
   include/linux/compiler_types.h:702:9: note: in expansion of macro '_compiletime_assert'
     702 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:40:37: note: in expansion of macro 'compiletime_assert'
      40 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
         |                                     ^~~~~~~~~~~~~~~~~~
   include/linux/build_bug.h:51:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
      51 |         BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
         |         ^~~~~~~~~~~~~~~~
   drivers/vfio/pci/virtio/legacy_io.c:377:9: note: in expansion of macro 'BUILD_BUG_ON'
     377 |         BUILD_BUG_ON(!is_power_of_2(virtvdev->bar0_virtual_buf_size));
         |         ^~~~~~~~~~~~

vim +/__compiletime_assert_1377 +702 include/linux/compiler_types.h

eb5c2d4b45e3d2 Will Deacon 2020-07-21  688  
eb5c2d4b45e3d2 Will Deacon 2020-07-21  689  #define _compiletime_assert(condition, msg, prefix, suffix) \
eb5c2d4b45e3d2 Will Deacon 2020-07-21  690  	__compiletime_assert(condition, msg, prefix, suffix)
eb5c2d4b45e3d2 Will Deacon 2020-07-21  691  
eb5c2d4b45e3d2 Will Deacon 2020-07-21  692  /**
eb5c2d4b45e3d2 Will Deacon 2020-07-21  693   * compiletime_assert - break build and emit msg if condition is false
eb5c2d4b45e3d2 Will Deacon 2020-07-21  694   * @condition: a compile-time constant condition to check
eb5c2d4b45e3d2 Will Deacon 2020-07-21  695   * @msg:       a message to emit if condition is false
eb5c2d4b45e3d2 Will Deacon 2020-07-21  696   *
eb5c2d4b45e3d2 Will Deacon 2020-07-21  697   * In tradition of POSIX assert, this macro will break the build if the
eb5c2d4b45e3d2 Will Deacon 2020-07-21  698   * supplied condition is *false*, emitting the supplied error message if the
eb5c2d4b45e3d2 Will Deacon 2020-07-21  699   * compiler has support to do so.
eb5c2d4b45e3d2 Will Deacon 2020-07-21  700   */
eb5c2d4b45e3d2 Will Deacon 2020-07-21  701  #define compiletime_assert(condition, msg) \

eb5c2d4b45e3d2 Will Deacon 2020-07-21 @702 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
eb5c2d4b45e3d2 Will Deacon 2020-07-21 703

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki