Mesa (staging/21.2): util/fossilize_db: Add extra flock mutex.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 9 21:46:59 UTC 2021


Module: Mesa
Branch: staging/21.2
Commit: ccf4743977bfaec039b3ea38a866ed6a0e119512
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=ccf4743977bfaec039b3ea38a866ed6a0e119512

Author: Bas Nieuwenhuizen <bas at basnieuwenhuizen.nl>
Date:   Sat Aug  7 23:31:00 2021 +0200

util/fossilize_db: Add extra flock mutex.

The flock is per-fd, not per thread, and we do it outside of the main mutex. This was
done to avoid having to wait in the mutex, but we can get a case where one ends up running
the body with the flock unlocked.

Fix this by adding a mutex that doesn't need to be locked for reads.

Fixes: 4f0f8133a35 "util/fossilize_db: Do not lock the fossilize db permanently."
Reviewed-by: Timothy Arceri <tarceri at itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12266>
(cherry picked from commit 30a359d633f2eebf28f3e464e53b00befffbe86f)

---

 .pick_status.json       |  2 +-
 src/util/fossilize_db.c | 12 +++++++++++-
 src/util/fossilize_db.h |  1 +
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/.pick_status.json b/.pick_status.json
index cc3ce6a52c6..df7276757e1 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -562,7 +562,7 @@
         "description": "util/fossilize_db: Add extra flock mutex.",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "main_sha": null,
         "because_sha": "4f0f8133a35ec2fec8d99936cd7425e40d092169"
     },
diff --git a/src/util/fossilize_db.c b/src/util/fossilize_db.c
index 013575a67a1..e1709a1ff64 100644
--- a/src/util/fossilize_db.c
+++ b/src/util/fossilize_db.c
@@ -286,6 +286,7 @@ foz_prepare(struct foz_db *foz_db, char *cache_path)
       return false;
 
    simple_mtx_init(&foz_db->mtx, mtx_plain);
+   simple_mtx_init(&foz_db->flock_mtx, mtx_plain);
    foz_db->mem_ctx = ralloc_context(NULL);
    foz_db->index_db = _mesa_hash_table_u64_create(NULL);
 
@@ -348,6 +349,7 @@ foz_destroy(struct foz_db *foz_db)
    if (foz_db->mem_ctx) {
       _mesa_hash_table_u64_destroy(foz_db->index_db);
       ralloc_free(foz_db->mem_ctx);
+      simple_mtx_destroy(&foz_db->flock_mtx);
       simple_mtx_destroy(&foz_db->mtx);
    }
 }
@@ -434,7 +436,12 @@ foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,
    if (!foz_db->alive)
       return false;
 
-   /* Wait for 1 second. This is done outside of the mutex as I believe there is more potential
+   /* The flock is per-fd, not per thread, we do it outside of the main mutex to avoid having to
+    * wait in the mutex potentially blocking reads. We use the secondary flock_mtx to stop race
+    * conditions between the write threads sharing the same file descriptor. */
+   simple_mtx_lock(&foz_db->flock_mtx);
+
+   /* Wait for 1 second. This is done outside of the main mutex as I believe there is more potential
     * for file contention than mtx contention of significant length. */
    int err = lock_file_with_timeout(foz_db->file[0], 1000000000);
    if (err == -1)
@@ -449,6 +456,7 @@ foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,
    if (entry) {
       simple_mtx_unlock(&foz_db->mtx);
       flock(fileno(foz_db->file[0]), LOCK_UN);
+      simple_mtx_unlock(&foz_db->flock_mtx);
       return NULL;
    }
 
@@ -511,6 +519,7 @@ foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,
 
    simple_mtx_unlock(&foz_db->mtx);
    flock(fileno(foz_db->file[0]), LOCK_UN);
+   simple_mtx_unlock(&foz_db->flock_mtx);
 
    return true;
 
@@ -518,6 +527,7 @@ fail:
    simple_mtx_unlock(&foz_db->mtx);
 fail_file:
    flock(fileno(foz_db->file[0]), LOCK_UN);
+   simple_mtx_unlock(&foz_db->flock_mtx);
    return false;
 }
 #else
diff --git a/src/util/fossilize_db.h b/src/util/fossilize_db.h
index 9b6d34122aa..e05aef9e4ca 100644
--- a/src/util/fossilize_db.h
+++ b/src/util/fossilize_db.h
@@ -76,6 +76,7 @@ struct foz_db {
    FILE *file[FOZ_MAX_DBS];          /* An array of all foz dbs */
    FILE *db_idx;                     /* The default writable foz db idx */
    simple_mtx_t mtx;                 /* Mutex for file/hash table read/writes */
+   simple_mtx_t flock_mtx;           /* Mutex for flocking the file for writes */
    void *mem_ctx;
    struct hash_table_u64 *index_db;  /* Hash table of all foz db entries */
    bool alive;



More information about the mesa-commit mailing list