mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] bootchooser: put bootchooser after creating a boot entry
@ 2018-10-18 11:43 Sascha Hauer
  2018-10-18 11:43 ` [PATCH 2/2] bootchooser: Add reference counting Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2018-10-18 11:43 UTC (permalink / raw)
  To: Barebox List

Between creating a boot entry for bootchooser and actually using it the
underlying state may have been modified, so make sure to get a current
bootchooser when booting it, thus do a bootchooser_put after having
created the entry and a bootchooser_get again when booting it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/bootchooser.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/common/bootchooser.c b/common/bootchooser.c
index 83b15e0f78..29f61aec7f 100644
--- a/common/bootchooser.c
+++ b/common/bootchooser.c
@@ -52,7 +52,6 @@ static int retry;
 static int last_boot_successful;
 
 struct bootchooser {
-	struct bootentry entry;
 	struct list_head targets;
 	struct bootchooser_target *last_chosen;
 
@@ -841,20 +840,26 @@ int bootchooser_boot(struct bootchooser *bc)
 
 static int bootchooser_entry_boot(struct bootentry *entry, int verbose, int dryrun)
 {
-	struct bootchooser *bc = container_of(entry, struct bootchooser,
-						       entry);
+	struct bootchooser *bc;
+	int ret;
+
+	bc = bootchooser_get();
+	if (IS_ERR(bc))
+		return PTR_ERR(bc);
+
 	bc->verbose = verbose;
 	bc->dryrun = dryrun;
 
-	return bootchooser_boot(bc);
+	ret = bootchooser_boot(bc);
+
+	bootchooser_put(bc);
+
+	return ret;
 }
 
 static void bootchooser_release(struct bootentry *entry)
 {
-	struct bootchooser *bc = container_of(entry, struct bootchooser,
-						       entry);
-
-	bootchooser_put(bc);
+	free(entry);
 }
 
 /**
@@ -869,6 +874,7 @@ static void bootchooser_release(struct bootentry *entry)
 static int bootchooser_add_entry(struct bootentries *entries, const char *name)
 {
 	struct bootchooser *bc;
+	struct bootentry *entry;
 
 	if (strcmp(name, "bootchooser"))
 		return 0;
@@ -877,12 +883,16 @@ static int bootchooser_add_entry(struct bootentries *entries, const char *name)
 	if (IS_ERR(bc))
 		return PTR_ERR(bc);
 
-	bc->entry.boot = bootchooser_entry_boot;
-	bc->entry.release = bootchooser_release;
-	bc->entry.title = xstrdup("bootchooser");
-	bc->entry.description = xstrdup("bootchooser");
+	entry = xzalloc(sizeof(*entry));
+
+	entry->boot = bootchooser_entry_boot;
+	entry->release = bootchooser_release;
+	entry->title = xstrdup("bootchooser");
+	entry->description = xstrdup("bootchooser");
 
-	bootentries_add_entry(entries, &bc->entry);
+	bootentries_add_entry(entries, entry);
+
+	bootchooser_put(bc);
 
 	return 1;
 }
-- 
2.19.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] bootchooser: Add reference counting
  2018-10-18 11:43 [PATCH 1/2] bootchooser: put bootchooser after creating a boot entry Sascha Hauer
@ 2018-10-18 11:43 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2018-10-18 11:43 UTC (permalink / raw)
  To: Barebox List

This adds reference counting to the bootchooser. Instead of creating a
new bootchooser instance with each bootchooser_get() we return a
reference to the existing bootchooser.
This makes the behaviour consistent when bootchooser_get() is called
multiple times.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/bootchooser.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/common/bootchooser.c b/common/bootchooser.c
index 29f61aec7f..c47c734c9c 100644
--- a/common/bootchooser.c
+++ b/common/bootchooser.c
@@ -57,6 +57,7 @@ struct bootchooser {
 
 	struct state *state;
 	char *state_prefix;
+	int refs;
 
 	int verbose;
 	int dryrun;
@@ -344,11 +345,15 @@ static void bootchooser_reset_priorities(struct bootchooser *bc)
 		bootchooser_target_set_priority(target, -1);
 }
 
+static struct bootchooser *bootchooser;
+
 /**
- * bootchooser_get - get a bootchooser instance
+ * bootchooser_get - get a reference to the bootchooser
  *
- * This evaluates the different globalvars and eventually state variables,
- * creates a bootchooser instance from it and returns it.
+ * When no bootchooser is initialized this function allocates the bootchooser
+ * and initializes it with the different globalvars and state variables. The
+ * bootchooser is returned. Subsequent calls will return a reference to the same
+ * bootchooser.
  */
 struct bootchooser *bootchooser_get(void)
 {
@@ -359,6 +364,11 @@ struct bootchooser *bootchooser_get(void)
 	uint32_t last_chosen;
 	static int attempts_resetted;
 
+	if (bootchooser) {
+		bootchooser->refs++;
+		return bootchooser;
+	}
+
 	bc = xzalloc(sizeof(*bc));
 
 	if (*state_prefix) {
@@ -473,6 +483,10 @@ struct bootchooser *bootchooser_get(void)
 
 	}
 
+	bootchooser = bc;
+
+	bootchooser->refs = 1;
+
 	return bc;
 
 err:
@@ -528,16 +542,26 @@ int bootchooser_save(struct bootchooser *bc)
 }
 
 /**
- * bootchooser_put - release a bootchooser instance
+ * bootchooser_put - return a bootchooser reference
  * @bc: The bootchooser instance
  *
- * This releases a bootchooser instance and the memory associated with it.
+ * This returns a reference to the bootchooser. If it is the last reference the
+ * bootchooser is saved and the associated memory is freed.
+ *
+ * Return: 0 for success or a negative error code. An error can occur when
+ *         bootchooser_save fails to write to the storage, nevertheless the
+ *         bootchooser reference is still released.
  */
 int bootchooser_put(struct bootchooser *bc)
 {
 	struct bootchooser_target *target, *tmp;
 	int ret;
 
+	bc->refs--;
+
+	if (bc->refs)
+		return 0;
+
 	ret = bootchooser_save(bc);
 	if (ret)
 		pr_err("Failed to save bootchooser state: %s\n", strerror(-ret));
@@ -552,6 +576,8 @@ int bootchooser_put(struct bootchooser *bc)
 
 	free(bc);
 
+	bootchooser = NULL;
+
 	return ret;
 }
 
-- 
2.19.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-18 11:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-18 11:43 [PATCH 1/2] bootchooser: put bootchooser after creating a boot entry Sascha Hauer
2018-10-18 11:43 ` [PATCH 2/2] bootchooser: Add reference counting Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox