uint first = (uint)((char *)sp - (char *)bp->b_addr);
 
                *sp += delta;
+               if (mp->m_rsum_cache) {
+                       if (*sp == 0 && log == mp->m_rsum_cache[bbno])
+                               mp->m_rsum_cache[bbno]++;
+                       if (*sp != 0 && log < mp->m_rsum_cache[bbno])
+                               mp->m_rsum_cache[bbno] = log;
+               }
                xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
        }
        if (sum)
 
        int                     m_logbsize;     /* size of each log buffer */
        uint                    m_rsumlevels;   /* rt summary levels */
        uint                    m_rsumsize;     /* size of rt summary, bytes */
+       /*
+        * Optional cache of rt summary level per bitmap block with the
+        * invariant that m_rsum_cache[bbno] <= the minimum i for which
+        * rsum[i][bbno] != 0. Reads and writes are serialized by the rsumip
+        * inode lock.
+        */
+       uint8_t                 *m_rsum_cache;
        struct xfs_inode        *m_rbmip;       /* pointer to bitmap inode */
        struct xfs_inode        *m_rsumip;      /* pointer to summary inode */
        struct xfs_inode        *m_rootip;      /* pointer to root directory */
 
        int             log;            /* loop counter, log2 of ext. size */
        xfs_suminfo_t   sum;            /* summary data */
 
+       /* There are no extents at levels < m_rsum_cache[bbno]. */
+       if (mp->m_rsum_cache && low < mp->m_rsum_cache[bbno])
+               low = mp->m_rsum_cache[bbno];
+
        /*
-        * Loop over logs of extent sizes.  Order is irrelevant.
+        * Loop over logs of extent sizes.
         */
        for (log = low; log <= high; log++) {
                /*
                 */
                if (sum) {
                        *stat = 1;
-                       return 0;
+                       goto out;
                }
        }
        /*
         * Found nothing, return failure.
         */
        *stat = 0;
+out:
+       /* There were no extents at levels < log. */
+       if (mp->m_rsum_cache && log > mp->m_rsum_cache[bbno])
+               mp->m_rsum_cache[bbno] = log;
        return 0;
 }
 
 }
 
 /*
- * Get the bitmap and summary inodes into the mount structure
- * at mount time.
+ * Get the bitmap and summary inodes and the summary cache into the mount
+ * structure at mount time.
  */
 int                                    /* error */
 xfs_rtmount_inodes(
                return error;
        }
        ASSERT(mp->m_rsumip != NULL);
+       /*
+        * The rsum cache is initialized to all zeroes, which is trivially a
+        * lower bound on the minimum level with any free extents. We can
+        * continue without the cache if it couldn't be allocated.
+        */
+       mp->m_rsum_cache = kmem_zalloc_large(sbp->sb_rbmblocks, KM_SLEEP);
+       if (!mp->m_rsum_cache)
+               xfs_warn(mp, "could not allocate realtime summary cache");
        return 0;
 }
 
 xfs_rtunmount_inodes(
        struct xfs_mount        *mp)
 {
+       kmem_free(mp->m_rsum_cache);
        if (mp->m_rbmip)
                xfs_irele(mp->m_rbmip);
        if (mp->m_rsumip)