1. 27 5月, 2020 6 次提交
    • C
      bcache: use bio_{start,end}_io_acct · 85750aeb
      Christoph Hellwig 提交于
      Switch bcache to use the nicer bio accounting helpers, and call the
      routines where we also sample the start time to give coherent accounting
      results.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Reviewed-by: NKonstantin Khlebnikov <khlebnikov@yandex-team.ru>
      Acked-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      85750aeb
    • C
      bcache: configure the asynchronous registertion to be experimental · 0c8d3fce
      Coly Li 提交于
      In order to avoid the experimental async registration interface to
      be treated as new kernel ABI for common users, this patch makes it
      as an experimental kernel configure BCACHE_ASYNC_REGISTRAION.
      
      This interface is for extreme large cached data situation, to make sure
      the bcache device can always created without the udev timeout issue. For
      normal users the async or sync registration does not make difference.
      
      In future when we decide to use the asynchronous registration as default
      behavior, this experimental interface may be removed.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      0c8d3fce
    • C
      bcache: asynchronous devices registration · 9e23ccf8
      Coly Li 提交于
      When there is a lot of data cached on cache device, the bcach internal
      btree can take a very long to validate during the backing device and
      cache device registration. In my test, it may takes 55+ minutes to check
      all the internal btree nodes.
      
      The problem is that the registration is invoked by udev rules and the
      udevd has 180 seconds timeout by default. If the btree node checking
      time is longer than udevd timeout, the registering  process will be
      killed by udevd with SIGKILL. If the registering process has pending
      sigal, creating kthread for bcache will fail and the device registration
      will fail. The result is, for bcache device which cached a lot of data
      on cache device, the bcache device node like /dev/bcache<N> won't create
      always due to the very long btree checking time.
      
      A solution to avoid the udevd 180 seconds timeout is to register devices
      in an asynchronous way. Which is, after writing cache or backing device
      path into /sys/fs/bcache/register_async, the kernel code will create a
      kworker and move all the btree node checking (for cache device) or dirty
      data counting (for cached device) in the kwork context. Then the kworder
      is scheduled on system_wq and the registration code just returned to
      user space udev rule task. By this asynchronous way, the udev task for
      bcache rule will complete in seconds, no matter how long time spent in
      the kworker context, it won't be killed by udevd for a timeout.
      
      After all the checking and counting are done asynchronously in the
      kworker, the bcache device will eventually be created successfully.
      
      This patch does the above chagne and add a register sysfs file
      /sys/fs/bcache/register_async. Writing the registering device path into
      this sysfs file will do the asynchronous registration.
      
      The register_async interface is for very rare condition and won't be
      used for common users. In future I plan to make the asynchronous
      registration as default behavior, which depends on feedback for this
      patch.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      9e23ccf8
    • C
      bcache: fix refcount underflow in bcache_device_free() · 86da9f73
      Coly Li 提交于
      The problematic code piece in bcache_device_free() is,
      
       785 static void bcache_device_free(struct bcache_device *d)
       786 {
       787     struct gendisk *disk = d->disk;
       [snipped]
       799     if (disk) {
       800             if (disk->flags & GENHD_FL_UP)
       801                     del_gendisk(disk);
       802
       803             if (disk->queue)
       804                     blk_cleanup_queue(disk->queue);
       805
       806             ida_simple_remove(&bcache_device_idx,
       807                               first_minor_to_idx(disk->first_minor));
       808             put_disk(disk);
       809         }
       [snipped]
       816 }
      
      At line 808, put_disk(disk) may encounter kobject refcount of 'disk'
      being underflow.
      
      Here is how to reproduce the issue,
      - Attche the backing device to a cache device and do random write to
        make the cache being dirty.
      - Stop the bcache device while the cache device has dirty data of the
        backing device.
      - Only register the backing device back, NOT register cache device.
      - The bcache device node /dev/bcache0 won't show up, because backing
        device waits for the cache device shows up for the missing dirty
        data.
      - Now echo 1 into /sys/fs/bcache/pendings_cleanup, to stop the pending
        backing device.
      - After the pending backing device stopped, use 'dmesg' to check kernel
        message, a use-after-free warning from KASA reported the refcount of
        kobject linked to the 'disk' is underflow.
      
      The dropping refcount at line 808 in the above code piece is added by
      add_disk(d->disk) in bch_cached_dev_run(). But in the above condition
      the cache device is not registered, bch_cached_dev_run() has no chance
      to be called and the refcount is not added. The put_disk() for a non-
      added refcount of gendisk kobject triggers a underflow warning.
      
      This patch checks whether GENHD_FL_UP is set in disk->flags, if it is
      not set then the bcache device was not added, don't call put_disk()
      and the the underflow issue can be avoided.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      86da9f73
    • J
      bcache: Convert pr_<level> uses to a more typical style · 46f5aa88
      Joe Perches 提交于
      Remove the trailing newline from the define of pr_fmt and add newlines
      to the uses.
      
      Miscellanea:
      
      o Convert bch_bkey_dump from multiple uses of pr_err to pr_cont
        as the earlier conversion was inappropriate done causing multiple
        lines to be emitted where only a single output line was desired
      o Use vsprintf extension %pV in bch_cache_set_error to avoid multiple
        line output where only a single line output was desired
      o Coalesce formats
      
      Fixes: 6ae63e35 ("bcache: replace printk() by pr_*() routines")
      Signed-off-by: NJoe Perches <joe@perches.com>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      46f5aa88
    • C
      bcache: remove redundant variables i and n · 3b5b7b1f
      Colin Ian King 提交于
      Variables i and n are being assigned but are never used. They are
      redundant and can be removed.
      Signed-off-by: NColin Ian King <colin.king@canonical.com>
      Signed-off-by: NColy Li <colyli@suse.de>
      Addresses-Coverity: ("Unused value")
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      3b5b7b1f
  2. 25 4月, 2020 1 次提交
  3. 28 3月, 2020 2 次提交
  4. 25 3月, 2020 1 次提交
  5. 23 3月, 2020 7 次提交
    • C
      bcache: optimize barrier usage for atomic operations · eb9b6666
      Coly Li 提交于
      The idea of this patch is from Davidlohr Bueso, he posts a patch
      for bcache to optimize barrier usage for read-modify-write atomic
      bitops. Indeed such optimization can also apply on other locations
      where smp_mb() is used before or after an atomic operation.
      
      This patch replaces smp_mb() with smp_mb__before_atomic() or
      smp_mb__after_atomic() in btree.c and writeback.c,  where it is used
      to synchronize memory cache just earlier on other cores. Although
      the locations are not on hot code path, it is always not bad to mkae
      things a little better.
      Signed-off-by: NColy Li <colyli@suse.de>
      Cc: Davidlohr Bueso <dave@stgolabs.net>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      eb9b6666
    • D
      bcache: optimize barrier usage for Rmw atomic bitops · b004aa86
      Davidlohr Bueso 提交于
      We can avoid the unnecessary barrier on non LL/SC architectures,
      such as x86. Instead, use the smp_mb__after_atomic().
      Signed-off-by: NDavidlohr Bueso <dbueso@suse.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      b004aa86
    • T
      bcache: Use scnprintf() for avoiding potential buffer overflow · 9876e386
      Takashi Iwai 提交于
      Since snprintf() returns the would-be-output size instead of the
      actual output size, the succeeding calls may go beyond the given
      buffer limit.  Fix it by replacing with scnprintf().
      Signed-off-by: NTakashi Iwai <tiwai@suse.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      9876e386
    • C
      bcache: make bch_sectors_dirty_init() to be multithreaded · b144e45f
      Coly Li 提交于
      When attaching a cached device (a.k.a backing device) to a cache
      device, bch_sectors_dirty_init() is called to count dirty sectors
      and stripes (see what bcache_dev_sectors_dirty_add() does) on the
      cache device.
      
      The counting is done by a single thread recursive function
      bch_btree_map_keys() to iterate all the bcache btree nodes.
      If the btree has huge number of nodes, bch_sectors_dirty_init() will
      take quite long time. In my testing, if the registering cache set has
      a existed UUID which matches a already registered cached device, the
      automatical attachment during the registration may take more than
      55 minutes. This is too long for waiting the bcache to work in real
      deployment.
      
      Fortunately when bch_sectors_dirty_init() is called, no other thread
      will access the btree yet, it is safe to do a read-only parallelized
      dirty sectors counting by multiple threads.
      
      This patch tries to create multiple threads, and each thread tries to
      one-by-one count dirty sectors from the sub-tree indexed by a root
      node key which the thread fetched. After the sub-tree is counted, the
      counting thread will continue to fetch another root node key, until
      the fetched key is NULL. How many threads in parallel depends on
      the number of keys from the btree root node, and the number of online
      CPU core. The thread number will be the less number but no more than
      BCH_DIRTY_INIT_THRD_MAX. If there are only 2 keys in root node, it
      can only be 2x times faster by this patch. But if there are 10 keys
      in the root node, with this patch it can be 10x times faster.
      Signed-off-by: NColy Li <colyli@suse.de>
      Cc: Christoph Hellwig <hch@infradead.org>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      b144e45f
    • C
      bcache: make bch_btree_check() to be multithreaded · 8e710227
      Coly Li 提交于
      When registering a cache device, bch_btree_check() is called to check
      all btree nodes, to make sure the btree is consistent and not
      corrupted.
      
      bch_btree_check() is recursively executed in a single thread, when there
      are a lot of data cached and the btree is huge, it may take very long
      time to check all the btree nodes. In my testing, I observed it took
      around 50 minutes to finish bch_btree_check().
      
      When checking the bcache btree nodes, the cache set is not running yet,
      and indeed the whole tree is in read-only state, it is safe to create
      multiple threads to check the btree in parallel.
      
      This patch tries to create multiple threads, and each thread tries to
      one-by-one check the sub-tree indexed by a key from the btree root node.
      The parallel thread number depends on how many keys in the btree root
      node. At most BCH_BTR_CHKTHREAD_MAX (64) threads can be created, but in
      practice is should be min(cpu-number/2, root-node-keys-number).
      Signed-off-by: NColy Li <colyli@suse.de>
      Cc: Christoph Hellwig <hch@infradead.org>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      8e710227
    • C
      bcache: add bcache_ prefix to btree_root() and btree() macros · feac1a70
      Coly Li 提交于
      This patch changes macro btree_root() and btree() to bcache_btree_root()
      and bcache_btree(), to avoid potential generic name clash in future.
      
      NOTE: for product kernel maintainers, this patch can be skipped if
      you feel the rename stuffs introduce inconvenince to patch backport.
      Suggested-by: NChristoph Hellwig <hch@infradead.org>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      feac1a70
    • C
      bcache: move macro btree() and btree_root() into btree.h · 253a99d9
      Coly Li 提交于
      In order to accelerate bcache registration speed, the macro btree()
      and btree_root() will be referenced out of btree.c. This patch moves
      them from btree.c into btree.h with other relative function declaration
      in btree.h, for the following changes.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      253a99d9
  6. 03 3月, 2020 1 次提交
  7. 13 2月, 2020 3 次提交
    • C
      bcache: remove macro nr_to_fifo_front() · 4ec31cb6
      Coly Li 提交于
      Macro nr_to_fifo_front() is only used once in btree_flush_write(),
      it is unncessary indeed. This patch removes this macro and does
      calculation directly in place.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      4ec31cb6
    • C
      bcache: Revert "bcache: shrink btree node cache after bch_btree_check()" · 309cc719
      Coly Li 提交于
      This reverts commit 1df3877f.
      
      In my testing, sometimes even all the cached btree nodes are freed,
      creating gc and allocator kernel threads may still fail. Finally it
      turns out that kthread_run() may fail if there is pending signal for
      current task. And the pending signal is sent from OOM killer which
      is triggered by memory consuption in bch_btree_check().
      
      Therefore explicitly shrinking bcache btree node here does not help,
      and after the shrinker callback is improved, as well as pending signals
      are ignored before creating kernel threads, now such operation is
      unncessary anymore.
      
      This patch reverts the commit 1df3877f ("bcache: shrink btree node
      cache after bch_btree_check()") because we have better improvement now.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      309cc719
    • C
      bcache: ignore pending signals when creating gc and allocator thread · 0b96da63
      Coly Li 提交于
      When run a cache set, all the bcache btree node of this cache set will
      be checked by bch_btree_check(). If the bcache btree is very large,
      iterating all the btree nodes will occupy too much system memory and
      the bcache registering process might be selected and killed by system
      OOM killer. kthread_run() will fail if current process has pending
      signal, therefore the kthread creating in run_cache_set() for gc and
      allocator kernel threads are very probably failed for a very large
      bcache btree.
      
      Indeed such OOM is safe and the registering process will exit after
      the registration done. Therefore this patch flushes pending signals
      during the cache set start up, specificly in bch_cache_allocator_start()
      and bch_gc_thread_start(), to make sure run_cache_set() won't fail for
      large cahced data set.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      0b96da63
  8. 01 2月, 2020 5 次提交
    • C
      bcache: check return value of prio_read() · 49d08d59
      Coly Li 提交于
      Now if prio_read() failed during starting a cache set, we can print
      out error message in run_cache_set() and handle the failure properly.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      49d08d59
    • C
      bcache: fix incorrect data type usage in btree_flush_write() · d1c3cc34
      Coly Li 提交于
      Dan Carpenter points out that from commit 2aa8c529 ("bcache: avoid
      unnecessary btree nodes flushing in btree_flush_write()"), there is a
      incorrect data type usage which leads to the following static checker
      warning:
      	drivers/md/bcache/journal.c:444 btree_flush_write()
      	warn: 'ref_nr' unsigned <= 0
      
      drivers/md/bcache/journal.c
         422  static void btree_flush_write(struct cache_set *c)
         423  {
         424          struct btree *b, *t, *btree_nodes[BTREE_FLUSH_NR];
         425          unsigned int i, nr, ref_nr;
                                          ^^^^^^
      
         426          atomic_t *fifo_front_p, *now_fifo_front_p;
         427          size_t mask;
         428
         429          if (c->journal.btree_flushing)
         430                  return;
         431
         432          spin_lock(&c->journal.flush_write_lock);
         433          if (c->journal.btree_flushing) {
         434                  spin_unlock(&c->journal.flush_write_lock);
         435                  return;
         436          }
         437          c->journal.btree_flushing = true;
         438          spin_unlock(&c->journal.flush_write_lock);
         439
         440          /* get the oldest journal entry and check its refcount */
         441          spin_lock(&c->journal.lock);
         442          fifo_front_p = &fifo_front(&c->journal.pin);
         443          ref_nr = atomic_read(fifo_front_p);
         444          if (ref_nr <= 0) {
                          ^^^^^^^^^^^
      Unsigned can't be less than zero.
      
         445                  /*
         446                   * do nothing if no btree node references
         447                   * the oldest journal entry
         448                   */
         449                  spin_unlock(&c->journal.lock);
         450                  goto out;
         451          }
         452          spin_unlock(&c->journal.lock);
      
      As the warning information indicates, local varaible ref_nr in unsigned
      int type is wrong, which does not matche atomic_read() and the "<= 0"
      checking.
      
      This patch fixes the above error by defining local variable ref_nr as
      int type.
      
      Fixes: 2aa8c529 ("bcache: avoid unnecessary btree nodes flushing in btree_flush_write()")
      Reported-by: NDan Carpenter <dan.carpenter@oracle.com>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      d1c3cc34
    • C
      bcache: add readahead cache policy options via sysfs interface · 038ba8cc
      Coly Li 提交于
      In year 2007 high performance SSD was still expensive, in order to
      save more space for real workload or meta data, the readahead I/Os
      for non-meta data was bypassed and not cached on SSD.
      
      In now days, SSD price drops a lot and people can find larger size
      SSD with more comfortable price. It is unncessary to alway bypass
      normal readahead I/Os to save SSD space for now.
      
      This patch adds options for readahead data cache policies via sysfs
      file /sys/block/bcache<N>/readahead_cache_policy, the options are,
      - "all": cache all readahead data I/Os.
      - "meta-only": only cache meta data, and bypass other regular I/Os.
      
      If users want to make bcache continue to only cache readahead request
      for metadata and bypass regular data readahead, please set "meta-only"
      to this sysfs file. By default, bcache will back to cache all read-
      ahead requests now.
      
      Cc: stable@vger.kernel.org
      Signed-off-by: NColy Li <colyli@suse.de>
      Acked-by: NEric Wheeler <bcache@linux.ewheeler.net>
      Cc: Michael Lyle <mlyle@lyle.org>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      038ba8cc
    • C
      bcache: explicity type cast in bset_bkey_last() · 7c02b005
      Coly Li 提交于
      In bset.h, macro bset_bkey_last() is defined as,
          bkey_idx((struct bkey *) (i)->d, (i)->keys)
      
      Parameter i can be variable type of data structure, the macro always
      works once the type of struct i has member 'd' and 'keys'.
      
      bset_bkey_last() is also used in macro csum_set() to calculate the
      checksum of a on-disk data structure. When csum_set() is used to
      calculate checksum of on-disk bcache super block, the parameter 'i'
      data type is struct cache_sb_disk. Inside struct cache_sb_disk (also in
      struct cache_sb) the member keys is __u16 type. But bkey_idx() expects
      unsigned int (a 32bit width), so there is problem when sending
      parameters via stack to call bkey_idx().
      
      Sparse tool from Intel 0day kbuild system reports this incompatible
      problem. bkey_idx() is part of user space API, so the simplest fix is
      to cast the (i)->keys to unsigned int type in macro bset_bkey_last().
      Reported-by: Nkbuild test robot <lkp@intel.com>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      7c02b005
    • C
      bcache: fix memory corruption in bch_cache_accounting_clear() · 5bebf748
      Coly Li 提交于
      Commit 83ff9318 ("bcache: not use hard coded memset size in
      bch_cache_accounting_clear()") tries to make the code more easy to
      understand by removing the hard coded number with following change,
      	void bch_cache_accounting_clear(...)
      	{
      		memset(&acc->total.cache_hits,
      			0,
      	-		sizeof(unsigned long) * 7);
      	+		sizeof(struct cache_stats));
      	}
      
      Unfortunately the change was wrong (it also tells us the original code
      was not easy to correctly understand). The hard coded number 7 is used
      because in struct cache_stats,
       15 struct cache_stats {
       16         struct kobject          kobj;
       17
       18         unsigned long cache_hits;
       19         unsigned long cache_misses;
       20         unsigned long cache_bypass_hits;
       21         unsigned long cache_bypass_misses;
       22         unsigned long cache_readaheads;
       23         unsigned long cache_miss_collisions;
       24         unsigned long sectors_bypassed;
       25
       26         unsigned int            rescale;
       27 };
      only members in LINE 18-24 want to be set to 0. It is wrong to use
      'sizeof(struct cache_stats)' to replace 'sizeof(unsigned long) * 7), the
      memory objects behind acc->total is staled by this change.
      
      Сорокин Артем Сергеевич reports that by the following steps, kernel
      panic will be triggered,
      1. Create new set: make-bcache -B /dev/nvme1n1 -C /dev/sda --wipe-bcache
      2. Run in /sys/fs/bcache/<uuid>:
         echo 1 > clear_stats && cat stats_five_minute/cache_bypass_hits
      
      I can reproduce the panic and get following dmesg with KASAN enabled,
      [22613.172742] ==================================================================
      [22613.172862] BUG: KASAN: null-ptr-deref in sysfs_kf_seq_show+0x117/0x230
      [22613.172864] Read of size 8 at addr 0000000000000000 by task cat/6753
      
      [22613.172870] CPU: 1 PID: 6753 Comm: cat Not tainted 5.5.0-rc7-lp151.28.16-default+ #11
      [22613.172872] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/29/2019
      [22613.172873] Call Trace:
      [22613.172964]  dump_stack+0x8b/0xbb
      [22613.172968]  ? sysfs_kf_seq_show+0x117/0x230
      [22613.172970]  ? sysfs_kf_seq_show+0x117/0x230
      [22613.173031]  __kasan_report+0x176/0x192
      [22613.173064]  ? pr_cont_kernfs_name+0x40/0x60
      [22613.173067]  ? sysfs_kf_seq_show+0x117/0x230
      [22613.173070]  kasan_report+0xe/0x20
      [22613.173072]  sysfs_kf_seq_show+0x117/0x230
      [22613.173105]  seq_read+0x199/0x6d0
      [22613.173110]  vfs_read+0xa5/0x1a0
      [22613.173113]  ksys_read+0x110/0x160
      [22613.173115]  ? kernel_write+0xb0/0xb0
      [22613.173177]  do_syscall_64+0x77/0x290
      [22613.173238]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
      [22613.173241] RIP: 0033:0x7fc2c886ac61
      [22613.173244] Code: fe ff ff 48 8d 3d c7 a0 09 00 48 83 ec 08 e8 46 03 02 00 66 0f 1f 44 00 00 8b 05 ca fb 2c 00 48 63 ff 85 c0 75 13 31 c0 0f 05 <48> 3d 00 f0 ff ff 77 57 f3 c3 0f 1f 44 00 00 55 53 48 89 d5 48 89
      [22613.173245] RSP: 002b:00007ffebe776d68 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
      [22613.173248] RAX: ffffffffffffffda RBX: 0000000000020000 RCX: 00007fc2c886ac61
      [22613.173249] RDX: 0000000000020000 RSI: 00007fc2c8cca000 RDI: 0000000000000003
      [22613.173250] RBP: 0000000000020000 R08: ffffffffffffffff R09: 0000000000000000
      [22613.173251] R10: 000000000000038c R11: 0000000000000246 R12: 00007fc2c8cca000
      [22613.173253] R13: 0000000000000003 R14: 00007fc2c8cca00f R15: 0000000000020000
      [22613.173255] ==================================================================
      [22613.173256] Disabling lock debugging due to kernel taint
      [22613.173350] BUG: kernel NULL pointer dereference, address: 0000000000000000
      [22613.178380] #PF: supervisor read access in kernel mode
      [22613.180959] #PF: error_code(0x0000) - not-present page
      [22613.183444] PGD 0 P4D 0
      [22613.184867] Oops: 0000 [#1] SMP KASAN PTI
      [22613.186797] CPU: 1 PID: 6753 Comm: cat Tainted: G    B             5.5.0-rc7-lp151.28.16-default+ #11
      [22613.191253] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/29/2019
      [22613.196706] RIP: 0010:sysfs_kf_seq_show+0x117/0x230
      [22613.199097] Code: ff 48 8b 0b 48 8b 44 24 08 48 01 e9 eb a6 31 f6 48 89 cf ba 00 10 00 00 48 89 4c 24 10 e8 b1 e6 e9 ff 4c 89 ff e8 19 07 ea ff <49> 8b 07 48 85 c0 48 89 44 24 08 0f 84 91 00 00 00 49 8b 6d 00 48
      [22613.208016] RSP: 0018:ffff8881d4f8fd78 EFLAGS: 00010246
      [22613.210448] RAX: 0000000000000000 RBX: ffff8881eb99b180 RCX: ffffffff810d9ef6
      [22613.213691] RDX: 0000000000000001 RSI: 0000000000000246 RDI: 0000000000000246
      [22613.216893] RBP: 0000000000001000 R08: fffffbfff072ddcd R09: fffffbfff072ddcd
      [22613.220075] R10: 0000000000000001 R11: fffffbfff072ddcc R12: ffff8881de5c0200
      [22613.223256] R13: ffff8881ed175500 R14: ffff8881eb99b198 R15: 0000000000000000
      [22613.226290] FS:  00007fc2c8d3d500(0000) GS:ffff8881f2a80000(0000) knlGS:0000000000000000
      [22613.229637] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      [22613.231993] CR2: 0000000000000000 CR3: 00000001ec89a004 CR4: 00000000003606e0
      [22613.234909] Call Trace:
      [22613.235931]  seq_read+0x199/0x6d0
      [22613.237259]  vfs_read+0xa5/0x1a0
      [22613.239229]  ksys_read+0x110/0x160
      [22613.240590]  ? kernel_write+0xb0/0xb0
      [22613.242040]  do_syscall_64+0x77/0x290
      [22613.243625]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
      [22613.245450] RIP: 0033:0x7fc2c886ac61
      [22613.246706] Code: fe ff ff 48 8d 3d c7 a0 09 00 48 83 ec 08 e8 46 03 02 00 66 0f 1f 44 00 00 8b 05 ca fb 2c 00 48 63 ff 85 c0 75 13 31 c0 0f 05 <48> 3d 00 f0 ff ff 77 57 f3 c3 0f 1f 44 00 00 55 53 48 89 d5 48 89
      [22613.253296] RSP: 002b:00007ffebe776d68 EFLAGS: 00000246 ORIG_RAX: 0000000000000000
      [22613.255835] RAX: ffffffffffffffda RBX: 0000000000020000 RCX: 00007fc2c886ac61
      [22613.258472] RDX: 0000000000020000 RSI: 00007fc2c8cca000 RDI: 0000000000000003
      [22613.260807] RBP: 0000000000020000 R08: ffffffffffffffff R09: 0000000000000000
      [22613.263188] R10: 000000000000038c R11: 0000000000000246 R12: 00007fc2c8cca000
      [22613.265598] R13: 0000000000000003 R14: 00007fc2c8cca00f R15: 0000000000020000
      [22613.268729] Modules linked in: scsi_transport_iscsi af_packet iscsi_ibft iscsi_boot_sysfs vmw_vsock_vmci_transport vsock fuse bnep kvm_intel kvm irqbypass crc32_pclmul crc32c_intel ghash_clmulni_intel snd_ens1371 snd_ac97_codec ac97_bus bcache snd_pcm btusb btrtl btbcm btintel crc64 aesni_intel glue_helper crypto_simd vmw_balloon cryptd bluetooth snd_timer snd_rawmidi snd joydev pcspkr e1000 rfkill vmw_vmci soundcore ecdh_generic ecc gameport i2c_piix4 mptctl ac button hid_generic usbhid sr_mod cdrom ata_generic ehci_pci vmwgfx uhci_hcd drm_kms_helper syscopyarea serio_raw sysfillrect sysimgblt fb_sys_fops ttm ehci_hcd mptspi scsi_transport_spi mptscsih ata_piix mptbase ahci usbcore libahci drm sg dm_multipath dm_mod scsi_dh_rdac scsi_dh_emc scsi_dh_alua
      [22613.292429] CR2: 0000000000000000
      [22613.293563] ---[ end trace a074b26a8508f378 ]---
      [22613.295138] RIP: 0010:sysfs_kf_seq_show+0x117/0x230
      [22613.296769] Code: ff 48 8b 0b 48 8b 44 24 08 48 01 e9 eb a6 31 f6 48 89 cf ba 00 10 00 00 48 89 4c 24 10 e8 b1 e6 e9 ff 4c 89 ff e8 19 07 ea ff <49> 8b 07 48 85 c0 48 89 44 24 08 0f 84 91 00 00 00 49 8b 6d 00 48
      [22613.303553] RSP: 0018:ffff8881d4f8fd78 EFLAGS: 00010246
      [22613.305280] RAX: 0000000000000000 RBX: ffff8881eb99b180 RCX: ffffffff810d9ef6
      [22613.307924] RDX: 0000000000000001 RSI: 0000000000000246 RDI: 0000000000000246
      [22613.310272] RBP: 0000000000001000 R08: fffffbfff072ddcd R09: fffffbfff072ddcd
      [22613.312685] R10: 0000000000000001 R11: fffffbfff072ddcc R12: ffff8881de5c0200
      [22613.315076] R13: ffff8881ed175500 R14: ffff8881eb99b198 R15: 0000000000000000
      [22613.318116] FS:  00007fc2c8d3d500(0000) GS:ffff8881f2a80000(0000) knlGS:0000000000000000
      [22613.320743] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      [22613.322628] CR2: 0000000000000000 CR3: 00000001ec89a004 CR4: 00000000003606e0
      
      Here this patch fixes the following problem by explicity set all the 7
      members to 0 in bch_cache_accounting_clear().
      Reported-by: NСорокин Артем Сергеевич <a.sorokin@bank-hlynov.ru>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      5bebf748
  9. 24 1月, 2020 14 次提交
    • C
      bcache: reap from tail of c->btree_cache in bch_mca_scan() · e3de0446
      Coly Li 提交于
      When shrink btree node cache from c->btree_cache in bch_mca_scan(),
      no matter the selected node is reaped or not, it will be rotated from
      the head to the tail of c->btree_cache list. But in bcache journal
      code, when flushing the btree nodes with oldest journal entry, btree
      nodes are iterated and slected from the tail of c->btree_cache list in
      btree_flush_write(). The list_rotate_left() in bch_mca_scan() will
      make btree_flush_write() iterate more nodes in c->btree_list in reverse
      order.
      
      This patch just reaps the selected btree node cache, and not move it
      from the head to the tail of c->btree_cache list. Then bch_mca_scan()
      will not mess up c->btree_cache list to btree_flush_write().
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      e3de0446
    • C
      bcache: reap c->btree_cache_freeable from the tail in bch_mca_scan() · d5c9c470
      Coly Li 提交于
      In order to skip the most recently freed btree node cahce, currently
      in bch_mca_scan() the first 3 caches in c->btree_cache_freeable list
      are skipped when shrinking bcache node caches in bch_mca_scan(). The
      related code in bch_mca_scan() is,
      
       737 list_for_each_entry_safe(b, t, &c->btree_cache_freeable, list) {
       738         if (nr <= 0)
       739                 goto out;
       740
       741         if (++i > 3 &&
       742             !mca_reap(b, 0, false)) {
                   		lines free cache memory
       746         }
       747         nr--;
       748 }
      
      The problem is, if virtual memory code calls bch_mca_scan() and
      the calculated 'nr' is 1 or 2, then in the above loop, nothing will
      be shunk. In such case, if slub/slab manager calls bch_mca_scan()
      for many times with small scan number, it does not help to shrink
      cache memory and just wasts CPU cycles.
      
      This patch just selects btree node caches from tail of the
      c->btree_cache_freeable list, then the newly freed host cache can
      still be allocated by mca_alloc(), and at least 1 node can be shunk.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      d5c9c470
    • C
      bcache: remove member accessed from struct btree · 125d98ed
      Coly Li 提交于
      The member 'accessed' of struct btree is used in bch_mca_scan() when
      shrinking btree node caches. The original idea is, if b->accessed is
      set, clean it and look at next btree node cache from c->btree_cache
      list, and only shrink the caches whose b->accessed is cleaned. Then
      only cold btree node cache will be shrunk.
      
      But when I/O pressure is high, it is very probably that b->accessed
      of a btree node cache will be set again in bch_btree_node_get()
      before bch_mca_scan() selects it again. Then there is no chance for
      bch_mca_scan() to shrink enough memory back to slub or slab system.
      
      This patch removes member accessed from struct btree, then once a
      btree node ache is selected, it will be immediately shunk. By this
      change, bch_mca_scan() may release btree node cahce more efficiently.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      125d98ed
    • C
      bcache: avoid unnecessary btree nodes flushing in btree_flush_write() · 2aa8c529
      Coly Li 提交于
      the commit 91be66e1 ("bcache: performance improvement for
      btree_flush_write()") was an effort to flushing btree node with oldest
      btree node faster in following methods,
      - Only iterate dirty btree nodes in c->btree_cache, avoid scanning a lot
        of clean btree nodes.
      - Take c->btree_cache as a LRU-like list, aggressively flushing all
        dirty nodes from tail of c->btree_cache util the btree node with
        oldest journal entry is flushed. This is to reduce the time of holding
        c->bucket_lock.
      
      Guoju Fang and Shuang Li reported that they observe unexptected extra
      write I/Os on cache device after applying the above patch. Guoju Fang
      provideed more detailed diagnose information that the aggressive
      btree nodes flushing may cause 10x more btree nodes to flush in his
      workload. He points out when system memory is large enough to hold all
      btree nodes in memory, c->btree_cache is not a LRU-like list any more.
      Then the btree node with oldest journal entry is very probably not-
      close to the tail of c->btree_cache list. In such situation much more
      dirty btree nodes will be aggressively flushed before the target node
      is flushed. When slow SATA SSD is used as cache device, such over-
      aggressive flushing behavior will cause performance regression.
      
      After spending a lot of time on debug and diagnose, I find the real
      condition is more complicated, aggressive flushing dirty btree nodes
      from tail of c->btree_cache list is not a good solution.
      - When all btree nodes are cached in memory, c->btree_cache is not
        a LRU-like list, the btree nodes with oldest journal entry won't
        be close to the tail of the list.
      - There can be hundreds dirty btree nodes reference the oldest journal
        entry, before flushing all the nodes the oldest journal entry cannot
        be reclaimed.
      When the above two conditions mixed together, a simply flushing from
      tail of c->btree_cache list is really NOT a good idea.
      
      Fortunately there is still chance to make btree_flush_write() work
      better. Here is how this patch avoids unnecessary btree nodes flushing,
      - Only acquire c->journal.lock when getting oldest journal entry of
        fifo c->journal.pin. In rested locations check the journal entries
        locklessly, so their values can be changed on other cores
        in parallel.
      - In loop list_for_each_entry_safe_reverse(), checking latest front
        point of fifo c->journal.pin. If it is different from the original
        point which we get with locking c->journal.lock, it means the oldest
        journal entry is reclaim on other cores. At this moment, all selected
        dirty nodes recorded in array btree_nodes[] are all flushed and clean
        on other CPU cores, it is unncessary to iterate c->btree_cache any
        longer. Just quit the list_for_each_entry_safe_reverse() loop and
        the following for-loop will skip all the selected clean nodes.
      - Find a proper time to quit the list_for_each_entry_safe_reverse()
        loop. Check the refcount value of orignial fifo front point, if the
        value is larger than selected node number of btree_nodes[], it means
        more matching btree nodes should be scanned. Otherwise it means no
        more matching btee nodes in rest of c->btree_cache list, the loop
        can be quit. If the original oldest journal entry is reclaimed and
        fifo front point is updated, the refcount of original fifo front point
        will be 0, then the loop will be quit too.
      - Not hold c->bucket_lock too long time. c->bucket_lock is also required
        for space allocation for cached data, hold it for too long time will
        block regular I/O requests. When iterating list c->btree_cache, even
        there are a lot of maching btree nodes, in order to not holding
        c->bucket_lock for too long time, only BTREE_FLUSH_NR nodes are
        selected and to flush in following for-loop.
      With this patch, only btree nodes referencing oldest journal entry
      are flushed to cache device, no aggressive flushing for  unnecessary
      btree node any more. And in order to avoid blocking regluar I/O
      requests, each time when btree_flush_write() called, at most only
      BTREE_FLUSH_NR btree nodes are selected to flush, even there are more
      maching btree nodes in list c->btree_cache.
      
      At last, one more thing to explain: Why it is safe to read front point
      of c->journal.pin without holding c->journal.lock inside the
      list_for_each_entry_safe_reverse() loop ?
      
      Here is my answer: When reading the front point of fifo c->journal.pin,
      we don't need to know the exact value of front point, we just want to
      check whether the value is different from the original front point
      (which is accurate value because we get it while c->jouranl.lock is
      held). For such purpose, it works as expected without holding
      c->journal.lock. Even the front point is changed on other CPU core and
      not updated to local core, and current iterating btree node has
      identical journal entry local as original fetched fifo front point, it
      is still safe. Because after holding mutex b->write_lock (with memory
      barrier) this btree node can be found as clean and skipped, the loop
      will quite latter when iterate on next node of list c->btree_cache.
      
      Fixes: 91be66e1 ("bcache: performance improvement for btree_flush_write()")
      Reported-by: NGuoju Fang <fangguoju@gmail.com>
      Reported-by: NShuang Li <psymon@bonuscloud.io>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      2aa8c529
    • C
      bcache: add code comments for state->pool in __btree_sort() · 7a0bc2a8
      Coly Li 提交于
      To explain the pages allocated from mempool state->pool can be
      swapped in __btree_sort(), because state->pool is a page pool,
      which allocates pages by alloc_pages() indeed.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      7a0bc2a8
    • C
      bcache: use read_cache_page_gfp to read the superblock · 6321bef0
      Christoph Hellwig 提交于
      Avoid a pointless dependency on buffer heads in bcache by simply open
      coding reading a single page.  Also add a SB_OFFSET define for the
      byte offset of the superblock instead of using magic numbers.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      6321bef0
    • C
      bcache: store a pointer to the on-disk sb in the cache and cached_dev structures · 475389ae
      Christoph Hellwig 提交于
      This allows to properly build the superblock bio including the offset in
      the page using the normal bio helpers.  This fixes writing the superblock
      for page sizes larger than 4k where the sb write bio would need an offset
      in the bio_vec.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      475389ae
    • C
      bcache: return a pointer to the on-disk sb from read_super · cfa0c56d
      Christoph Hellwig 提交于
      Returning the properly typed actual data structure insteaf of the
      containing struct page will save the callers some work going
      forward.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      cfa0c56d
    • C
      bcache: transfer the sb_page reference to register_{bdev,cache} · fc8f19cc
      Christoph Hellwig 提交于
      Avoid an extra reference count roundtrip by transferring the sb_page
      ownership to the lower level register helpers.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      fc8f19cc
    • C
      bcache: fix use-after-free in register_bcache() · ae3cd299
      Coly Li 提交于
      The patch "bcache: rework error unwinding in register_bcache" introduces
      a use-after-free regression in register_bcache(). Here are current code,
      	2510 out_free_path:
      	2511         kfree(path);
      	2512 out_module_put:
      	2513         module_put(THIS_MODULE);
      	2514 out:
      	2515         pr_info("error %s: %s", path, err);
      	2516         return ret;
      If some error happens and the above code path is executed, at line 2511
      path is released, but referenced at line 2515. Then KASAN reports a use-
      after-free error message.
      
      This patch changes line 2515 in the following way to fix the problem,
      	2515         pr_info("error %s: %s", path?path:"", err);
      Signed-off-by: NColy Li <colyli@suse.de>
      Cc: Christoph Hellwig <hch@lst.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      ae3cd299
    • C
      bcache: properly initialize 'path' and 'err' in register_bcache() · 29cda393
      Coly Li 提交于
      Patch "bcache: rework error unwinding in register_bcache" from
      Christoph Hellwig changes the local variables 'path' and 'err'
      in undefined initial state. If the code in register_bcache() jumps
      to label 'out:' or 'out_module_put:' by goto, these two variables
      might be reference with undefined value by the following line,
      
      	out_module_put:
      	        module_put(THIS_MODULE);
      	out:
      	        pr_info("error %s: %s", path, err);
      	        return ret;
      
      Therefore this patch initializes these two local variables properly
      in register_bcache() to avoid such issue.
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      29cda393
    • C
      bcache: rework error unwinding in register_bcache · 50246693
      Christoph Hellwig 提交于
      Split the successful and error return path, and use one goto label for each
      resource to unwind.  This also fixes some small errors like leaking the
      module reference count in the reboot case (which seems entirely harmless)
      or printing the wrong warning messages for early failures.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      50246693
    • C
      bcache: use a separate data structure for the on-disk super block · a702a692
      Christoph Hellwig 提交于
      Split out an on-disk version struct cache_sb with the proper endianness
      annotations.  This fixes a fair chunk of sparse warnings, but there are
      some left due to the way the checksum is defined.
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      a702a692
    • L
      bcache: cached_dev_free needs to put the sb page · e8547d42
      Liang Chen 提交于
      Same as cache device, the buffer page needs to be put while
      freeing cached_dev.  Otherwise a page would be leaked every
      time a cached_dev is stopped.
      Signed-off-by: NLiang Chen <liangchen.linux@gmail.com>
      Signed-off-by: NChristoph Hellwig <hch@lst.de>
      Signed-off-by: NColy Li <colyli@suse.de>
      Signed-off-by: NJens Axboe <axboe@kernel.dk>
      e8547d42