Skip to content

Instantly share code, notes, and snippets.

@tixxdz
Created February 23, 2015 13:59
Show Gist options
  • Save tixxdz/79d035d51ac2b7abf8ee to your computer and use it in GitHub Desktop.
Save tixxdz/79d035d51ac2b7abf8ee to your computer and use it in GitHub Desktop.
kdbus: per message number accouting
diff --git a/connection.c b/connection.c
index ec0f6b0..c135419 100644
--- a/connection.c
+++ b/connection.c
@@ -640,7 +640,9 @@ static int kdbus_conn_quota(struct kdbus_conn *c, struct kdbus_user *u,
/* per user-accounting is expensive, so we keep state small */
BUILD_BUG_ON(sizeof(quota->memory) != 4);
+ BUILD_BUG_ON(sizeof(quota->msgs) != 1);
BUILD_BUG_ON(sizeof(quota->fds) != 1);
+ BUILD_BUG_ON(KDBUS_CONN_MAX_MSGS_PER_USER > U8_MAX);
BUILD_BUG_ON(KDBUS_CONN_MAX_FDS_PER_USER > U8_MAX);
if (u->id >= c->n_quota) {
@@ -661,7 +663,8 @@ static int kdbus_conn_quota(struct kdbus_conn *c, struct kdbus_user *u,
if (available < quota->memory ||
available - quota->memory < memory ||
- quota->memory + memory > U32_MAX)
+ quota->memory + memory > U32_MAX ||
+ quota->msgs >= KDBUS_CONN_MAX_MSGS_PER_USER)
return -ENOBUFS;
if (quota->fds + fds < quota->fds ||
@@ -670,6 +673,7 @@ static int kdbus_conn_quota(struct kdbus_conn *c, struct kdbus_user *u,
quota->memory += memory;
quota->fds += fds;
+ quota->msgs++;
return 0;
}
diff --git a/connection.h b/connection.h
index ca3b97b..1a5e7f9 100644
--- a/connection.h
+++ b/connection.h
@@ -33,10 +33,12 @@
/**
* struct kdbus_quota - per user quota state
* @memory: total amount of memory in target pool
+ * @msgs: total number of messages in target pool
* @fds: total number of fds in target queue
*/
struct kdbus_quota {
uint32_t memory;
+ uint8_t msgs;
uint8_t fds;
};
diff --git a/limits.h b/limits.h
index 13e0129..f6adccc 100644
--- a/limits.h
+++ b/limits.h
@@ -49,6 +49,9 @@
/* maximum number of queued messages in a connection */
#define KDBUS_CONN_MAX_MSGS 256
+/* maximum number of queued messages from the same indvidual user */
+#define KDBUS_CONN_MAX_MSGS_PER_USER 64
+
/* maximum number of inflight fds in a target queue per user */
#define KDBUS_CONN_MAX_FDS_PER_USER 16
diff --git a/queue.c b/queue.c
index eb6be02..fe25a01 100644
--- a/queue.c
+++ b/queue.c
@@ -150,6 +150,9 @@ void kdbus_queue_entry_remove(struct kdbus_conn *conn,
entry->user = kdbus_user_unref(entry->user);
+ if (!WARN_ON(quota->msgs == 0))
+ quota->msgs--;
+
n = kdbus_pool_slice_size(entry->slice);
if (!WARN_ON(quota->memory < n))
quota->memory -= n;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment