]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix lock assertions in dshash.c.
authorThomas Munro <tmunro@postgresql.org>
Mon, 11 Jul 2022 02:47:16 +0000 (14:47 +1200)
committerThomas Munro <tmunro@postgresql.org>
Mon, 11 Jul 2022 03:54:24 +0000 (15:54 +1200)
dshash.c previously maintained flags to be able to assert that you
didn't hold any partition lock.  These flags could get out of sync with
reality in error scenarios.

Get rid of all that, and make assertions about the locks themselves
instead.  Since LWLockHeldByMe() loops internally, we don't want to put
that inside another loop over all partition locks.  Introduce a new
debugging-only interface LWLockAnyHeldByMe() to avoid that.

This problem was noted by Tom and Andres while reviewing changes to
support the new shared memory stats system, and later showed up in
reality while working on commit 389869af.

Back-patch to 11, where dshash.c arrived.

Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reported-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
Reviewed-by: Zhihong Yu <zyu@yugabyte.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20220311012712.botrpsikaufzteyt@alap3.anarazel.de
Discussion: https://postgr.es/m/CA%2BhUKGJ31Wce6HJ7xnVTKWjFUWQZPBngxfJVx4q0E98pDr3kAw%40mail.gmail.com

src/backend/lib/dshash.c
src/backend/storage/lmgr/lwlock.c
src/include/storage/lwlock.h

index f86b2f97a5895673d809e9d5e00dd4f728aa3241..442c99efb57fc80d9baf9157cad58541b3b1fdb9 100644 (file)
@@ -110,8 +110,6 @@ struct dshash_table
        dshash_table_control *control;  /* Control object in DSM. */
        dsa_pointer *buckets;           /* Current bucket pointers in DSM. */
        size_t          size_log2;              /* log2(number of buckets) */
-       bool            find_locked;    /* Is any partition lock held by 'find'? */
-       bool            find_exclusively_locked;        /* ... exclusively? */
 };
 
 /* Given a pointer to an item, find the entry (user data) it holds. */
@@ -186,6 +184,10 @@ static inline bool equal_keys(dshash_table *hash_table,
 #define PARTITION_LOCK(hash_table, i)                  \
        (&(hash_table)->control->partitions[(i)].lock)
 
+#define ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table) \
+       Assert(!LWLockAnyHeldByMe(&(hash_table)->control->partitions[0].lock, \
+                  DSHASH_NUM_PARTITIONS, sizeof(dshash_partition)))
+
 /*
  * Create a new hash table backed by the given dynamic shared area, with the
  * given parameters.  The returned object is allocated in backend-local memory
@@ -226,9 +228,6 @@ dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
                }
        }
 
-       hash_table->find_locked = false;
-       hash_table->find_exclusively_locked = false;
-
        /*
         * Set up the initial array of buckets.  Our initial size is the same as
         * the number of partitions.
@@ -277,8 +276,6 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
        hash_table->params = *params;
        hash_table->arg = arg;
        hash_table->control = dsa_get_address(area, control);
-       hash_table->find_locked = false;
-       hash_table->find_exclusively_locked = false;
        Assert(hash_table->control->magic == DSHASH_MAGIC);
 
        /*
@@ -301,7 +298,7 @@ dshash_attach(dsa_area *area, const dshash_parameters *params,
 void
 dshash_detach(dshash_table *hash_table)
 {
-       Assert(!hash_table->find_locked);
+       ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table);
 
        /* The hash table may have been destroyed.  Just free local memory. */
        pfree(hash_table);
@@ -392,7 +389,7 @@ dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
        partition = PARTITION_FOR_HASH(hash);
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(!hash_table->find_locked);
+       ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table);
 
        LWLockAcquire(PARTITION_LOCK(hash_table, partition),
                                  exclusive ? LW_EXCLUSIVE : LW_SHARED);
@@ -409,9 +406,7 @@ dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
        }
        else
        {
-               /* The caller will free the lock by calling dshash_release. */
-               hash_table->find_locked = true;
-               hash_table->find_exclusively_locked = exclusive;
+               /* The caller will free the lock by calling dshash_release_lock. */
                return ENTRY_FROM_ITEM(item);
        }
 }
@@ -441,7 +436,7 @@ dshash_find_or_insert(dshash_table *hash_table,
        partition = &hash_table->control->partitions[partition_index];
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(!hash_table->find_locked);
+       ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table);
 
 restart:
        LWLockAcquire(PARTITION_LOCK(hash_table, partition_index),
@@ -486,8 +481,6 @@ restart:
        }
 
        /* The caller must release the lock with dshash_release_lock. */
-       hash_table->find_locked = true;
-       hash_table->find_exclusively_locked = true;
        return ENTRY_FROM_ITEM(item);
 }
 
@@ -506,7 +499,7 @@ dshash_delete_key(dshash_table *hash_table, const void *key)
        bool            found;
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(!hash_table->find_locked);
+       ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table);
 
        hash = hash_key(hash_table, key);
        partition = PARTITION_FOR_HASH(hash);
@@ -543,14 +536,10 @@ dshash_delete_entry(dshash_table *hash_table, void *entry)
        size_t          partition = PARTITION_FOR_HASH(item->hash);
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(hash_table->find_locked);
-       Assert(hash_table->find_exclusively_locked);
        Assert(LWLockHeldByMeInMode(PARTITION_LOCK(hash_table, partition),
                                                                LW_EXCLUSIVE));
 
        delete_item(hash_table, item);
-       hash_table->find_locked = false;
-       hash_table->find_exclusively_locked = false;
        LWLockRelease(PARTITION_LOCK(hash_table, partition));
 }
 
@@ -564,13 +553,7 @@ dshash_release_lock(dshash_table *hash_table, void *entry)
        size_t          partition_index = PARTITION_FOR_HASH(item->hash);
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(hash_table->find_locked);
-       Assert(LWLockHeldByMeInMode(PARTITION_LOCK(hash_table, partition_index),
-                                                               hash_table->find_exclusively_locked
-                                                               ? LW_EXCLUSIVE : LW_SHARED));
 
-       hash_table->find_locked = false;
-       hash_table->find_exclusively_locked = false;
        LWLockRelease(PARTITION_LOCK(hash_table, partition_index));
 }
 
@@ -603,7 +586,7 @@ dshash_dump(dshash_table *hash_table)
        size_t          j;
 
        Assert(hash_table->control->magic == DSHASH_MAGIC);
-       Assert(!hash_table->find_locked);
+       ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table);
 
        for (i = 0; i < DSHASH_NUM_PARTITIONS; ++i)
        {
index 952a877e5fd13e187abd37de68cd8554db405c50..473c9a4f164bba4f167e8eb53f9af57e81f28513 100644 (file)
@@ -1842,6 +1842,32 @@ LWLockHeldByMe(LWLock *l)
        return false;
 }
 
+/*
+ * LWLockHeldByMe - test whether my process holds any of an array of locks
+ *
+ * This is meant as debug support only.
+ */
+bool
+LWLockAnyHeldByMe(LWLock *l, int nlocks, size_t stride)
+{
+       char       *held_lock_addr;
+       char       *begin;
+       char       *end;
+       int                     i;
+
+       begin = (char *) l;
+       end = begin + nlocks * stride;
+       for (i = 0; i < num_held_lwlocks; i++)
+       {
+               held_lock_addr = (char *) held_lwlocks[i].lock;
+               if (held_lock_addr >= begin &&
+                       held_lock_addr < end &&
+                       (held_lock_addr - begin) % stride == 0)
+                       return true;
+       }
+       return false;
+}
+
 /*
  * LWLockHeldByMeInMode - test whether my process holds a lock in given mode
  *
index c21bfe2f66626b9fbc706885fec736280c03a21d..07b8468035f165c044aad658412bf4377ae50da9 100644 (file)
@@ -150,6 +150,7 @@ extern void LWLockRelease(LWLock *lock);
 extern void LWLockReleaseClearVar(LWLock *lock, uint64 *valptr, uint64 val);
 extern void LWLockReleaseAll(void);
 extern bool LWLockHeldByMe(LWLock *lock);
+extern bool LWLockAnyHeldByMe(LWLock *lock, int nlocks, size_t stride);
 extern bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode);
 
 extern bool LWLockWaitForVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval);