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

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 9 07:32:38 UTC 2021


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

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 a6cd8631bab..9bc4e75feb1 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -139,7 +139,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 0bca743f425..65ff85ad60f 100644
--- a/src/util/fossilize_db.c
+++ b/src/util/fossilize_db.c
@@ -292,6 +292,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);
 
@@ -354,6 +355,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);
    }
 }
@@ -440,7 +442,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)
@@ -459,6 +466,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;
    }
 
@@ -522,6 +530,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->db_idx), LOCK_UN);
    flock(fileno(foz_db->file[0]), LOCK_UN);
+   simple_mtx_unlock(&foz_db->flock_mtx);
 
    return true;
 
@@ -530,6 +539,7 @@ fail:
 fail_file:
    flock(fileno(foz_db->db_idx), LOCK_UN);
    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