Skip to content

Commit 24789c5

Browse files
frowandrobherring
authored andcommitted
of: overlay: detect cases where device tree may become corrupt
When an attempt to apply an overlay changeset fails, an effort is made to revert any partial application of the changeset. When an attempt to remove an overlay changeset fails, an effort is made to re-apply any partial reversion of the changeset. The existing code does not check for failure to recover a failed overlay changeset application or overlay changeset revert. Add the missing checks and flag the devicetree as corrupt if the state of the devicetree can not be determined. Improve and expand the returned errors to more fully reflect the result of the effort to undo the partial effects of a failed attempt to apply or remove an overlay changeset. If the device tree might be corrupt, do not allow further attempts to apply or remove an overlay changeset. When creating an overlay changeset from an overlay device tree, add some additional warnings if the state of the overlay device tree is not as expected. Signed-off-by: Frank Rowand <frank.rowand@sony.com> Signed-off-by: Rob Herring <robh@kernel.org>
1 parent 61b4de4 commit 24789c5

6 files changed

Lines changed: 372 additions & 96 deletions

File tree

‎drivers/gpu/drm/tilcdc/tilcdc_slave_compat.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static void __init tilcdc_convert_slave_node(void)
204204
/* For all memory needed for the overlay tree. This memory can
205205
be freed after the overlay has been applied. */
206206
struct kfree_table kft;
207-
int ret;
207+
int ovcs_id, ret;
208208

209209
if (kfree_table_init(&kft))
210210
return;
@@ -247,7 +247,8 @@ static void __init tilcdc_convert_slave_node(void)
247247

248248
tilcdc_node_disable(slave);
249249

250-
ret = of_overlay_apply(overlay);
250+
ovcs_id = 0;
251+
ret = of_overlay_apply(overlay, &ovcs_id);
251252
if (ret)
252253
pr_err("%s: Applying overlay changeset failed: %d\n",
253254
__func__, ret);

‎drivers/of/dynamic.c‎

Lines changed: 115 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,12 @@ static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
491491
}
492492
}
493493

494-
static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
494+
static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
495+
bool revert)
495496
{
496497
struct of_reconfig_data rd;
497498
struct of_changeset_entry ce_inverted;
498-
int ret;
499+
int ret = 0;
499500

500501
if (revert) {
501502
__of_changeset_entry_invert(ce, &ce_inverted);
@@ -517,11 +518,12 @@ static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool reve
517518
default:
518519
pr_err("invalid devicetree changeset action: %i\n",
519520
(int)ce->action);
520-
return;
521+
ret = -EINVAL;
521522
}
522523

523524
if (ret)
524525
pr_err("changeset notifier error @%pOF\n", ce->np);
526+
return ret;
525527
}
526528

527529
static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
@@ -655,32 +657,82 @@ void of_changeset_destroy(struct of_changeset *ocs)
655657
}
656658
EXPORT_SYMBOL_GPL(of_changeset_destroy);
657659

658-
int __of_changeset_apply(struct of_changeset *ocs)
660+
/*
661+
* Apply the changeset entries in @ocs.
662+
* If apply fails, an attempt is made to revert the entries that were
663+
* successfully applied.
664+
*
665+
* If multiple revert errors occur then only the final revert error is reported.
666+
*
667+
* Returns 0 on success, a negative error value in case of an error.
668+
* If a revert error occurs, it is returned in *ret_revert.
669+
*/
670+
int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
659671
{
660672
struct of_changeset_entry *ce;
661-
int ret;
673+
int ret, ret_tmp;
662674

663-
/* perform the rest of the work */
664675
pr_debug("changeset: applying...\n");
665676
list_for_each_entry(ce, &ocs->entries, node) {
666677
ret = __of_changeset_entry_apply(ce);
667678
if (ret) {
668679
pr_err("Error applying changeset (%d)\n", ret);
669-
list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
670-
__of_changeset_entry_revert(ce);
680+
list_for_each_entry_continue_reverse(ce, &ocs->entries,
681+
node) {
682+
ret_tmp = __of_changeset_entry_revert(ce);
683+
if (ret_tmp)
684+
*ret_revert = ret_tmp;
685+
}
671686
return ret;
672687
}
673688
}
674-
pr_debug("changeset: applied, emitting notifiers.\n");
689+
690+
return 0;
691+
}
692+
693+
/*
694+
* Returns 0 on success, a negative error value in case of an error.
695+
*
696+
* If multiple changset entry notification errors occur then only the
697+
* final notification error is reported.
698+
*/
699+
int __of_changeset_apply_notify(struct of_changeset *ocs)
700+
{
701+
struct of_changeset_entry *ce;
702+
int ret = 0, ret_tmp;
703+
704+
pr_debug("changeset: emitting notifiers.\n");
675705

676706
/* drop the global lock while emitting notifiers */
677707
mutex_unlock(&of_mutex);
678-
list_for_each_entry(ce, &ocs->entries, node)
679-
__of_changeset_entry_notify(ce, 0);
708+
list_for_each_entry(ce, &ocs->entries, node) {
709+
ret_tmp = __of_changeset_entry_notify(ce, 0);
710+
if (ret_tmp)
711+
ret = ret_tmp;
712+
}
680713
mutex_lock(&of_mutex);
681714
pr_debug("changeset: notifiers sent.\n");
682715

683-
return 0;
716+
return ret;
717+
}
718+
719+
/*
720+
* Returns 0 on success, a negative error value in case of an error.
721+
*
722+
* If a changeset entry apply fails, an attempt is made to revert any
723+
* previous entries in the changeset. If any of the reverts fails,
724+
* that failure is not reported. Thus the state of the device tree
725+
* is unknown if an apply error occurs.
726+
*/
727+
static int __of_changeset_apply(struct of_changeset *ocs)
728+
{
729+
int ret, ret_revert = 0;
730+
731+
ret = __of_changeset_apply_entries(ocs, &ret_revert);
732+
if (!ret)
733+
ret = __of_changeset_apply_notify(ocs);
734+
735+
return ret;
684736
}
685737

686738
/**
@@ -707,31 +759,74 @@ int of_changeset_apply(struct of_changeset *ocs)
707759
}
708760
EXPORT_SYMBOL_GPL(of_changeset_apply);
709761

710-
int __of_changeset_revert(struct of_changeset *ocs)
762+
/*
763+
* Revert the changeset entries in @ocs.
764+
* If revert fails, an attempt is made to re-apply the entries that were
765+
* successfully removed.
766+
*
767+
* If multiple re-apply errors occur then only the final apply error is
768+
* reported.
769+
*
770+
* Returns 0 on success, a negative error value in case of an error.
771+
* If an apply error occurs, it is returned in *ret_apply.
772+
*/
773+
int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
711774
{
712775
struct of_changeset_entry *ce;
713-
int ret;
776+
int ret, ret_tmp;
714777

715778
pr_debug("changeset: reverting...\n");
716779
list_for_each_entry_reverse(ce, &ocs->entries, node) {
717780
ret = __of_changeset_entry_revert(ce);
718781
if (ret) {
719782
pr_err("Error reverting changeset (%d)\n", ret);
720-
list_for_each_entry_continue(ce, &ocs->entries, node)
721-
__of_changeset_entry_apply(ce);
783+
list_for_each_entry_continue(ce, &ocs->entries, node) {
784+
ret_tmp = __of_changeset_entry_apply(ce);
785+
if (ret_tmp)
786+
*ret_apply = ret_tmp;
787+
}
722788
return ret;
723789
}
724790
}
725-
pr_debug("changeset: reverted, emitting notifiers.\n");
791+
792+
return 0;
793+
}
794+
795+
/*
796+
* If multiple changset entry notification errors occur then only the
797+
* final notification error is reported.
798+
*/
799+
int __of_changeset_revert_notify(struct of_changeset *ocs)
800+
{
801+
struct of_changeset_entry *ce;
802+
int ret = 0, ret_tmp;
803+
804+
pr_debug("changeset: emitting notifiers.\n");
726805

727806
/* drop the global lock while emitting notifiers */
728807
mutex_unlock(&of_mutex);
729-
list_for_each_entry_reverse(ce, &ocs->entries, node)
730-
__of_changeset_entry_notify(ce, 1);
808+
list_for_each_entry_reverse(ce, &ocs->entries, node) {
809+
ret_tmp = __of_changeset_entry_notify(ce, 1);
810+
if (ret_tmp)
811+
ret = ret_tmp;
812+
}
731813
mutex_lock(&of_mutex);
732814
pr_debug("changeset: notifiers sent.\n");
733815

734-
return 0;
816+
return ret;
817+
}
818+
819+
static int __of_changeset_revert(struct of_changeset *ocs)
820+
{
821+
int ret, ret_reply;
822+
823+
ret_reply = 0;
824+
ret = __of_changeset_revert_entries(ocs, &ret_reply);
825+
826+
if (!ret)
827+
ret = __of_changeset_revert_notify(ocs);
828+
829+
return ret;
735830
}
736831

737832
/**

‎drivers/of/of_private.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ extern struct kset *of_kset;
3939
extern int of_property_notify(int action, struct device_node *np,
4040
struct property *prop, struct property *old_prop);
4141
extern void of_node_release(struct kobject *kobj);
42-
extern int __of_changeset_apply(struct of_changeset *ocs);
43-
extern int __of_changeset_revert(struct of_changeset *ocs);
42+
extern int __of_changeset_apply_entries(struct of_changeset *ocs,
43+
int *ret_revert);
44+
extern int __of_changeset_apply_notify(struct of_changeset *ocs);
45+
extern int __of_changeset_revert_entries(struct of_changeset *ocs,
46+
int *ret_apply);
47+
extern int __of_changeset_revert_notify(struct of_changeset *ocs);
4448
#else /* CONFIG_OF_DYNAMIC */
4549
static inline int of_property_notify(int action, struct device_node *np,
4650
struct property *prop, struct property *old_prop)

0 commit comments

Comments
 (0)