From nobody Sun May 10 15:07:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 904BDC433EF for ; Mon, 2 May 2022 18:18:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386833AbiEBSV2 (ORCPT ); Mon, 2 May 2022 14:21:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1386789AbiEBSVY (ORCPT ); Mon, 2 May 2022 14:21:24 -0400 Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96641F3F; Mon, 2 May 2022 11:17:54 -0700 (PDT) Received: by mail-il1-x134.google.com with SMTP id z12so8376218ilp.8; Mon, 02 May 2022 11:17:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oljhvr5L//T9Vy02GcvXRkCXJypJDloAClOtYKsY/0E=; b=m8g+klr4FSoI2I5mvjR+ATb/3XQ5Mh5s9DwzGed58GsXWVUbF4mq3GH+lVjr5f5AhU 12UWNwsWDOb2Z85I9rHqYmC7DiU6MfAtWZSZFKKEorv7U9IWVh27fTnexF3l28nJ1Ug+ S9KqUcHmyh6hQPhhJeZ6XuV7LcBjwN8QlsiioG8w5lJTI+4zsWhhro/6dbWe7VtNa/rn XCvpgt3BwNQghGs+iV3DSWZgIN72E832AVZlcD+eVAWueyMwZUP6TwwFTIDXnazn6+wg dxLfzJNn0A+uRWTjF+e+nhEpFh3U+l5ANpQqY42f/ReFqR1XF0cLlTVvjfgE+lWzsJIn aFXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oljhvr5L//T9Vy02GcvXRkCXJypJDloAClOtYKsY/0E=; b=ztsXn1I8JCZwO6aL7Lwf6gyqgomRgFuU1ktFFvH103q8Gexy/pLQxUVZxMY6tMEg5Y GcifaNs58bSX/z25SRc9/4OLWC2fOKvrCWsMH7O0arDEqIgHTZwSOaMcjCc6lUT/5cSi vSy9davqeevioCRJAZ8bhp+YMawXNrpKIPyn6ADfGZG2V/DLbAZ3xi82IZDqyN/0DTO5 tSkWO8twFnuvWhOF6Nr+adZR+9A/dszlKCGy3yyXxTrxWTfyAXoEHFAI2eXrLJ2zI1Kr C26eUEdt+KQY/SZkW0CVDsUiZQuwNLRzw5v3ZhoPlwvL4kf+AlqMnamz43jkuqOLqRkn ONlA== X-Gm-Message-State: AOAM532NxHcnZo5eN9XmJ4padbDM2zhZkAhqiNEhGR6URNC3xBSbZLlx JoSNz9u5K941p0Yk/m33oYwZ21Vmgv0= X-Google-Smtp-Source: ABdhPJx/NXLucnD6P0oMjFgEU5sNoFUlCqSo2QDN0SEVxjEh4UX3XcYl4M8nazibbtYM3Sfdi3FFJA== X-Received: by 2002:a92:c26a:0:b0:2cd:9acf:c42d with SMTP id h10-20020a92c26a000000b002cd9acfc42dmr5153571ild.143.1651515474007; Mon, 02 May 2022 11:17:54 -0700 (PDT) Received: from xps8900.attlocal.net ([2600:1700:2442:6db0:2d25:3fcb:9ffb:5d5a]) by smtp.gmail.com with ESMTPSA id f43-20020a02242b000000b0032b3a7817a9sm3230678jaa.109.2022.05.02.11.17.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 11:17:53 -0700 (PDT) From: frowand.list@gmail.com To: Rob Herring , pantelis.antoniou@konsulko.com, Slawomir Stepien , Dan Carpenter Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Slawomir Stepien , Jan Kiszka , Geert Uytterhoeven Subject: [PATCH v2 1/3] of: overlay: add entry to of_overlay_action_name[] Date: Mon, 2 May 2022 13:17:40 -0500 Message-Id: <20220502181742.1402826-2-frowand.list@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220502181742.1402826-1-frowand.list@gmail.com> References: <20220502181742.1402826-1-frowand.list@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Frank Rowand The values of enum of_overlay_notify_action are used to index into array of_overlay_action_name. Add an entry to of_overlay_action_name for the value recently added to of_overlay_notify_action. Array of_overlay_action_name[] is moved into include/linux/of.h adjacent to enum of_overlay_notify_action to make the connection between the two more obvious if either is modified in the future. The only use of of_overlay_action_name is for error reporting in overlay_notify(). All callers of overlay_notify() report the same error, but with fewer details. Remove the redundant error reports in the callers. Fixes: 067c098766c6 ("of: overlay: rework overlay apply and remove kfree()s= ") Reported-by: Dan Carpenter Signed-off-by: Frank Rowand --- Changes since version 1: - patch 1/1 v1 did not apply on Rob's dt/next branch, rebase on top of 5f756a2eaa44 of: overlay: do not break notify on NOTIFY_{OK|STOP} drivers/of/overlay.c | 27 +++++---------------------- include/linux/of.h | 13 +++++++++++++ 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c index bd522da7f76b..ae5ea5b1079b 100644 --- a/drivers/of/overlay.c +++ b/drivers/of/overlay.c @@ -152,13 +152,6 @@ int of_overlay_notifier_unregister(struct notifier_blo= ck *nb) } EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); =20 -static char *of_overlay_action_name[] =3D { - "pre-apply", - "post-apply", - "pre-remove", - "post-remove", -}; - static int overlay_notify(struct overlay_changeset *ovcs, enum of_overlay_notify_action action) { @@ -178,7 +171,7 @@ static int overlay_notify(struct overlay_changeset *ovc= s, if (notifier_to_errno(ret)) { ret =3D notifier_to_errno(ret); pr_err("overlay changeset %s notifier error %d, target: %pOF\n", - of_overlay_action_name[action], ret, nd.target); + of_overlay_action_name(action), ret, nd.target); return ret; } } @@ -925,10 +918,8 @@ static int of_overlay_apply(struct overlay_changeset *= ovcs) goto out; =20 ret =3D overlay_notify(ovcs, OF_OVERLAY_PRE_APPLY); - if (ret) { - pr_err("overlay changeset pre-apply notify error %d\n", ret); + if (ret) goto out; - } =20 ret =3D build_changeset(ovcs); if (ret) @@ -951,12 +942,9 @@ static int of_overlay_apply(struct overlay_changeset *= ovcs) /* notify failure is not fatal, continue */ =20 ret_tmp =3D overlay_notify(ovcs, OF_OVERLAY_POST_APPLY); - if (ret_tmp) { - pr_err("overlay changeset post-apply notify error %d\n", - ret_tmp); + if (ret_tmp) if (!ret) ret =3D ret_tmp; - } =20 out: pr_debug("%s() err=3D%d\n", __func__, ret); @@ -1192,10 +1180,8 @@ int of_overlay_remove(int *ovcs_id) } =20 ret =3D overlay_notify(ovcs, OF_OVERLAY_PRE_REMOVE); - if (ret) { - pr_err("overlay changeset pre-remove notify error %d\n", ret); + if (ret) goto err_unlock; - } =20 ret_apply =3D 0; ret =3D __of_changeset_revert_entries(&ovcs->cset, &ret_apply); @@ -1218,12 +1204,9 @@ int of_overlay_remove(int *ovcs_id) * OF_OVERLAY_POST_REMOVE returns an error. */ ret_tmp =3D overlay_notify(ovcs, OF_OVERLAY_POST_REMOVE); - if (ret_tmp) { - pr_err("overlay changeset post-remove notify error %d\n", - ret_tmp); + if (ret_tmp) if (!ret) ret =3D ret_tmp; - } =20 free_overlay_changeset(ovcs); =20 diff --git a/include/linux/of.h b/include/linux/of.h index 17741eee0ca4..f0a5d6b10c5a 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -1550,6 +1550,19 @@ enum of_overlay_notify_action { OF_OVERLAY_POST_REMOVE, }; =20 +static inline char *of_overlay_action_name(enum of_overlay_notify_action a= ction) +{ + static char *of_overlay_action_name[] =3D { + "init", + "pre-apply", + "post-apply", + "pre-remove", + "post-remove", + }; + + return of_overlay_action_name[action]; +} + struct of_overlay_notify_data { struct device_node *overlay; struct device_node *target; --=20 Frank Rowand From nobody Sun May 10 15:07:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B817CC433EF for ; Mon, 2 May 2022 18:18:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386839AbiEBSVj (ORCPT ); Mon, 2 May 2022 14:21:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1386817AbiEBSV0 (ORCPT ); Mon, 2 May 2022 14:21:26 -0400 Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62D58F3F; Mon, 2 May 2022 11:17:56 -0700 (PDT) Received: by mail-io1-xd2b.google.com with SMTP id 125so16774141iov.10; Mon, 02 May 2022 11:17:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qVgMpTIl9iQg5YJJ9TzUWoyytbC2W64xMtFFqbe8+oQ=; b=mo281XLplFde2Uevx2e1SmFvHtz16iYgXCd/FCwKC+B5cydf78innB427zFSMhuknh P1laaxVfaq8axwcq6tGhDsW8/LMd72TAUetRDiAQABFAviB3BA/Pprl1HK8cjNYtXAfY lTBRUVP40xS8s99lAxvEPnXr4UTdp3qATbOT600RpvidDnkZnZqHNNEZ98szhoZg/xke yuL9/LoSykres7x97VkxIChnNNeovsh5VXDLrXSl0t+9oASWIXK2bw0A3SL3LIor2wNx Xq6wi2ej4sN2aOdyDTOsiz030qy/Wb9aV2HR3R4atAK6tPK95sBLb4loB4JdABz8VHEc IMCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qVgMpTIl9iQg5YJJ9TzUWoyytbC2W64xMtFFqbe8+oQ=; b=vFdx3JXsjcmCE10jM58/Yc93ylskmqAhxt0rD6kfS4iRWFY0ZD+ZmmePYCgPy7xo2t GKdgkLJBa+aayhbjV/qI3MmWSjRB5gpdQAs+QASrRKKUakWYfserjFurUDpsPCcnvZUk sCRy3gFMfyN63eDAe4uszLEYnEOK/xVPcf9ymgjdozOTunHrJOofWz1paF7zQuOoxVKd S0AqE+BMUttiQtnTyVLj0JsIrPur7P/XHH7HC9CKVPvWg8AH/sN9BrTSjqe/wcgYiatD IjbJVpTCN7pgzv2uKlzmGX8Q5N7XI/7/VH9rL7DSf1oZ3wGXvCiIyMvzwdKoDaCmnUHa nGSg== X-Gm-Message-State: AOAM533k4Tunv1qg3aRD4Lxb1Q8zP57sgBfWVWZ9zoz9zWpSPANi5f57 48HCJ5kBxGpj7mDoHcYLHHs= X-Google-Smtp-Source: ABdhPJzcB5WFENIfrVgECNZDhATotVvTaHceo7m4FvjGk47lGjaTMc+VWsQxogEz3Y0YAITn0PPEXA== X-Received: by 2002:a05:6638:37a8:b0:32b:5d5c:7f9d with SMTP id w40-20020a05663837a800b0032b5d5c7f9dmr4099724jal.3.1651515474931; Mon, 02 May 2022 11:17:54 -0700 (PDT) Received: from xps8900.attlocal.net ([2600:1700:2442:6db0:2d25:3fcb:9ffb:5d5a]) by smtp.gmail.com with ESMTPSA id f43-20020a02242b000000b0032b3a7817a9sm3230678jaa.109.2022.05.02.11.17.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 11:17:54 -0700 (PDT) From: frowand.list@gmail.com To: Rob Herring , pantelis.antoniou@konsulko.com, Slawomir Stepien , Dan Carpenter Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Slawomir Stepien , Jan Kiszka , Geert Uytterhoeven Subject: [PATCH v2 2/3] of: overlay: unittest: add tests for overlay notifiers Date: Mon, 2 May 2022 13:17:41 -0500 Message-Id: <20220502181742.1402826-3-frowand.list@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220502181742.1402826-1-frowand.list@gmail.com> References: <20220502181742.1402826-1-frowand.list@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Frank Rowand Add tests for overlay apply and remove notifiers. Trigger errors for each of the notifier actions. These tests will reveal a memory leak problem when a notifier returns an error for action OF_OVERLAY_POST_APPLY. The pr_err() message is: OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17 Signed-off-by: Frank Rowand --- Changes since version 1: - patch 1/1 v1 did not apply on Rob's dt/next branch, rebase on top of 5f756a2eaa44 of: overlay: do not break notify on NOTIFY_{OK|STOP} Output of the new overlay notifier unittests, as filtered by scripts/dtc/of_unittest_expect: ### dt-test ### pass of_unittest_overlay_notify():2825 ok OF: overlay: overlay changeset pre-apply notifier error -16, target: /te= stcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2846 ### dt-test ### pass of_unittest_overlay_notify():2851 ok OF: overlay: overlay changeset post-apply notifier error -17, target: /t= estcase-data/overlay-node/test-bus OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/= of_node_put() unbalanced - destroy cset entry: attach overlay node /testcas= e-data/overlay-node/test-bus/test-unittest17 ### dt-test ### pass of_unittest_overlay_notify():2857 ### dt-test ### pass of_unittest_overlay_notify():2862 ### dt-test ### pass of_unittest_overlay_notify():2866 ### dt-test ### pass of_unittest_overlay_notify():2869 ok OF: overlay: overlay changeset pre-remove notifier error -18, target: /t= estcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2880 ### dt-test ### pass of_unittest_overlay_notify():2888 ### dt-test ### pass of_unittest_overlay_notify():2892 ### dt-test ### pass of_unittest_overlay_notify():2895 ok OF: overlay: overlay changeset post-remove notifier error -19, target: /= testcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2902 ### dt-test ### pass of_unittest_overlay_notify():2909 ### dt-test ### pass of_unittest_overlay_notify():2914 ### dt-test ### pass of_unittest_overlay_notify():2926 Note that four new kernel error messages are triggered by the new tests, and of_unittest_expect labels them as expected errors with the string 'ok ' in the first three columns. A fifth new kernel error message is a real bug, which will be fixed in patch 3/3: OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/= of_node_put() unbalanced - destroy cset entry: attach overlay node /testcas= e-data/overlay-node/test-bus/test-unittest17 drivers/of/unittest-data/Makefile | 10 ++ drivers/of/unittest-data/overlay_16.dts | 15 ++ drivers/of/unittest-data/overlay_17.dts | 15 ++ drivers/of/unittest-data/overlay_18.dts | 15 ++ drivers/of/unittest-data/overlay_19.dts | 15 ++ drivers/of/unittest-data/overlay_20.dts | 15 ++ drivers/of/unittest.c | 198 ++++++++++++++++++++++++ 7 files changed, 283 insertions(+) create mode 100644 drivers/of/unittest-data/overlay_16.dts create mode 100644 drivers/of/unittest-data/overlay_17.dts create mode 100644 drivers/of/unittest-data/overlay_18.dts create mode 100644 drivers/of/unittest-data/overlay_19.dts create mode 100644 drivers/of/unittest-data/overlay_20.dts diff --git a/drivers/of/unittest-data/Makefile b/drivers/of/unittest-data/M= akefile index fbded24c608c..d072f3ba3971 100644 --- a/drivers/of/unittest-data/Makefile +++ b/drivers/of/unittest-data/Makefile @@ -17,6 +17,11 @@ obj-$(CONFIG_OF_OVERLAY) +=3D overlay.dtb.o \ overlay_12.dtb.o \ overlay_13.dtb.o \ overlay_15.dtb.o \ + overlay_16.dtb.o \ + overlay_17.dtb.o \ + overlay_18.dtb.o \ + overlay_19.dtb.o \ + overlay_20.dtb.o \ overlay_bad_add_dup_node.dtb.o \ overlay_bad_add_dup_prop.dtb.o \ overlay_bad_phandle.dtb.o \ @@ -75,6 +80,11 @@ apply_static_overlay_1 :=3D overlay_0.dtbo \ overlay_12.dtbo \ overlay_13.dtbo \ overlay_15.dtbo \ + overlay_16.dtbo \ + overlay_17.dtbo \ + overlay_18.dtbo \ + overlay_19.dtbo \ + overlay_20.dtbo \ overlay_gpio_01.dtbo \ overlay_gpio_02a.dtbo \ overlay_gpio_02b.dtbo \ diff --git a/drivers/of/unittest-data/overlay_16.dts b/drivers/of/unittest-= data/overlay_16.dts new file mode 100644 index 000000000000..80a46dc02581 --- /dev/null +++ b/drivers/of/unittest-data/overlay_16.dts @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_16 - notify test */ + +&unittest_test_bus { + #address-cells =3D <1>; + #size-cells =3D <0>; + + test-unittest16 { + compatible =3D "unittest"; + reg =3D <16>; + }; +}; diff --git a/drivers/of/unittest-data/overlay_17.dts b/drivers/of/unittest-= data/overlay_17.dts new file mode 100644 index 000000000000..5b8a2209177f --- /dev/null +++ b/drivers/of/unittest-data/overlay_17.dts @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_17 - notify test */ + +&unittest_test_bus { + #address-cells =3D <1>; + #size-cells =3D <0>; + + test-unittest17 { + compatible =3D "unittest"; + reg =3D <17>; + }; +}; diff --git a/drivers/of/unittest-data/overlay_18.dts b/drivers/of/unittest-= data/overlay_18.dts new file mode 100644 index 000000000000..dcddd5f6d301 --- /dev/null +++ b/drivers/of/unittest-data/overlay_18.dts @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_18 - notify test */ + +&unittest_test_bus { + #address-cells =3D <1>; + #size-cells =3D <0>; + + test-unittest18 { + compatible =3D "unittest"; + reg =3D <18>; + }; +}; diff --git a/drivers/of/unittest-data/overlay_19.dts b/drivers/of/unittest-= data/overlay_19.dts new file mode 100644 index 000000000000..32b3ba0b66a3 --- /dev/null +++ b/drivers/of/unittest-data/overlay_19.dts @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_19 - notify test */ + +&unittest_test_bus { + #address-cells =3D <1>; + #size-cells =3D <0>; + + test-unittest19 { + compatible =3D "unittest"; + reg =3D <19>; + }; +}; diff --git a/drivers/of/unittest-data/overlay_20.dts b/drivers/of/unittest-= data/overlay_20.dts new file mode 100644 index 000000000000..d4a4f2f32ec7 --- /dev/null +++ b/drivers/of/unittest-data/overlay_20.dts @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 +/dts-v1/; +/plugin/; + +/* overlay_20 - notify test */ + +&unittest_test_bus { + #address-cells =3D <1>; + #size-cells =3D <0>; + + test-unittest20 { + compatible =3D "unittest"; + reg =3D <20>; + }; +}; diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c index c4106de9f137..e28c3df2c4c2 100644 --- a/drivers/of/unittest.c +++ b/drivers/of/unittest.c @@ -2741,6 +2741,192 @@ static inline void of_unittest_overlay_i2c_15(void)= { } =20 #endif =20 +static int of_notify(struct notifier_block *nb, unsigned long action, + void *arg) +{ + struct of_overlay_notify_data *nd =3D arg; + struct device_node *found; + int ret; + + /* + * For overlay_16 .. overlay_19, check that returning an error + * works for each of the actions by setting an arbitrary return + * error number that matches the test number. e.g. for unittest16, + * ret =3D -EBUSY which is -16. + * + * OVERLAY_INFO() for the overlays is declared to expect the same + * error number, so overlay_data_apply() will return no error. + * + * overlay_20 will return NOTIFY_DONE + */ + + ret =3D 0; + of_node_get(nd->overlay); + + switch (action) { + + case OF_OVERLAY_PRE_APPLY: + found =3D of_find_node_by_name(nd->overlay, "test-unittest16"); + if (found) { + of_node_put(found); + ret =3D -EBUSY; + } + break; + + case OF_OVERLAY_POST_APPLY: + found =3D of_find_node_by_name(nd->overlay, "test-unittest17"); + if (found) { + of_node_put(found); + ret =3D -EEXIST; + } + break; + + case OF_OVERLAY_PRE_REMOVE: + found =3D of_find_node_by_name(nd->overlay, "test-unittest18"); + if (found) { + of_node_put(found); + ret =3D -EXDEV; + } + break; + + case OF_OVERLAY_POST_REMOVE: + found =3D of_find_node_by_name(nd->overlay, "test-unittest19"); + if (found) { + of_node_put(found); + ret =3D -ENODEV; + } + break; + + default: /* should not happen */ + of_node_put(nd->overlay); + ret =3D -EINVAL; + break; + } + + if (ret) + return notifier_from_errno(ret); + + return NOTIFY_DONE; +} + +static struct notifier_block of_nb =3D { + .notifier_call =3D of_notify, +}; + + + + +static void __init of_unittest_overlay_notify(void) +{ + int ovcs_id; + int ret; + + ret =3D of_overlay_notifier_register(&of_nb); + unittest(!ret, + "of_overlay_notifier_register() failed, ret =3D %d\n", ret); + if (ret) + return; + + /* + * The overlays are applied by overlay_data_apply() + * instead of of_unittest_apply_overlay() so that they + * will not be tracked. Thus they will not be removed + * by of_unittest_remove_tracked_overlays(). + * + * Applying overlays 16 - 19 will each trigger an error for a + * different action in of_notify(). + * + * Applying overlay 20 will not trigger any error in of_notify(). + */ + + /* --- overlay 16 --- */ + + EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifie= r error -16, target: /testcase-data/overlay-node/test-bus"); + + unittest(overlay_data_apply("overlay_16", &ovcs_id), + "test OF_OVERLAY_PRE_APPLY notify injected error\n"); + + EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier = error -16, target: /testcase-data/overlay-node/test-bus"); + + unittest(!ovcs_id, "ovcs_id created for overlay_16\n"); + + /* --- overlay 17 --- */ + + EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-apply notifi= er error -17, target: /testcase-data/overlay-node/test-bus"); + + unittest(overlay_data_apply("overlay_17", &ovcs_id), + "test OF_OVERLAY_POST_APPLY notify injected error\n"); + + EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier= error -17, target: /testcase-data/overlay-node/test-bus"); + + unittest(!ovcs_id, "ovcs_id created for overlay_17\n"); + + /* --- overlay 18 --- */ + + unittest(overlay_data_apply("overlay_18", &ovcs_id), + "OF_OVERLAY_PRE_REMOVE notify injected error\n"); + + unittest(ovcs_id, "ovcs_id not created for overlay_18\n"); + + if (ovcs_id) { + EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset pre-remove notif= ier error -18, target: /testcase-data/overlay-node/test-bus"); + + ret =3D of_overlay_remove(&ovcs_id); + EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-remove notifie= r error -18, target: /testcase-data/overlay-node/test-bus"); + if (ret =3D=3D -EXDEV) { + /* + * change set ovcs_id should still exist + */ + unittest(1, "overlay_18 of_overlay_remove() injected error for OF_OVERL= AY_PRE_REMOVE\n"); + } else { + unittest(0, "overlay_18 of_overlay_remove() injected error for OF_OVERL= AY_PRE_REMOVE not returned\n"); + } + } else { + unittest(1, "ovcs_id not created for overlay_18\n"); + } + + unittest(ovcs_id, "ovcs_id removed for overlay_18\n"); + + /* --- overlay 19 --- */ + + unittest(overlay_data_apply("overlay_19", &ovcs_id), + "OF_OVERLAY_POST_REMOVE notify injected error\n"); + + unittest(ovcs_id, "ovcs_id not created for overlay_19\n"); + + if (ovcs_id) { + EXPECT_BEGIN(KERN_INFO, "OF: overlay: overlay changeset post-remove noti= fier error -19, target: /testcase-data/overlay-node/test-bus"); + ret =3D of_overlay_remove(&ovcs_id); + EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-remove notifi= er error -19, target: /testcase-data/overlay-node/test-bus"); + if (ret =3D=3D -ENODEV) + unittest(1, "overlay_19 of_overlay_remove() injected error for OF_OVERL= AY_POST_REMOVE\n"); + else + unittest(0, "overlay_19 of_overlay_remove() injected error for OF_OVERL= AY_POST_REMOVE not returned\n"); + } else { + unittest(1, "ovcs_id removed for overlay_19\n"); + } + + unittest(!ovcs_id, "changeset ovcs_id =3D %d not removed for overlay_19\n= ", + ovcs_id); + + /* --- overlay 20 --- */ + + unittest(overlay_data_apply("overlay_20", &ovcs_id), + "overlay notify no injected error\n"); + + if (ovcs_id) { + ret =3D of_overlay_remove(&ovcs_id); + if (ret) + unittest(1, "overlay_20 failed to be destroyed, ret =3D %d\n", + ret); + } else { + unittest(1, "ovcs_id not created for overlay_20\n"); + } + + unittest(!of_overlay_notifier_unregister(&of_nb), + "of_overlay_notifier_unregister() failed, ret =3D %d\n", ret); +} + static void __init of_unittest_overlay(void) { struct device_node *bus_np =3D NULL; @@ -2804,6 +2990,8 @@ static void __init of_unittest_overlay(void) =20 of_unittest_remove_tracked_overlays(); =20 + of_unittest_overlay_notify(); + out: of_node_put(bus_np); } @@ -2855,6 +3043,11 @@ OVERLAY_INFO_EXTERN(overlay_11); OVERLAY_INFO_EXTERN(overlay_12); OVERLAY_INFO_EXTERN(overlay_13); OVERLAY_INFO_EXTERN(overlay_15); +OVERLAY_INFO_EXTERN(overlay_16); +OVERLAY_INFO_EXTERN(overlay_17); +OVERLAY_INFO_EXTERN(overlay_18); +OVERLAY_INFO_EXTERN(overlay_19); +OVERLAY_INFO_EXTERN(overlay_20); OVERLAY_INFO_EXTERN(overlay_gpio_01); OVERLAY_INFO_EXTERN(overlay_gpio_02a); OVERLAY_INFO_EXTERN(overlay_gpio_02b); @@ -2885,6 +3078,11 @@ static struct overlay_info overlays[] =3D { OVERLAY_INFO(overlay_12, 0), OVERLAY_INFO(overlay_13, 0), OVERLAY_INFO(overlay_15, 0), + OVERLAY_INFO(overlay_16, -EBUSY), + OVERLAY_INFO(overlay_17, -EEXIST), + OVERLAY_INFO(overlay_18, 0), + OVERLAY_INFO(overlay_19, 0), + OVERLAY_INFO(overlay_20, 0), OVERLAY_INFO(overlay_gpio_01, 0), OVERLAY_INFO(overlay_gpio_02a, 0), OVERLAY_INFO(overlay_gpio_02b, 0), --=20 Frank Rowand From nobody Sun May 10 15:07:18 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A280AC433F5 for ; Mon, 2 May 2022 18:18:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1386853AbiEBSVf (ORCPT ); Mon, 2 May 2022 14:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1386818AbiEBSV0 (ORCPT ); Mon, 2 May 2022 14:21:26 -0400 Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8F1109B; Mon, 2 May 2022 11:17:56 -0700 (PDT) Received: by mail-il1-x12f.google.com with SMTP id d3so8376935ilr.10; Mon, 02 May 2022 11:17:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TJq9Y2j2hvjQOKUEqQsEklHfEq8wyddeCa52aAthpsM=; b=MS4i9i1M056zAMef/ia3Kh2Ot8KqLzlLj/J45yrFWVW24uDlE6Z0y2tVYgqVda2Cn+ C6WZYHLf8VGwgAjuTIIwJYqeVdiDAaKiZ2kcJ3pZIaS2E23A3ZEFrRQ+Udt7ogYJGTNx 7Mf7Vhyp8dOy2FQwegmEH3mkNxXempihpLIAVn8/l4D6ocM5lWJu/0OX14RaJWOMQeac T+GcTUpqwQVE3JMFWAH7QMzX5ZdyWQ3YiWKo2F4drMn27LDVxnc87pN2BGlbQQttctqu QX7hJhI1iMRST6wgZDyPjUn3Duwyi0S2G8d6X//9lo1pNysN899eGHtx51DqHzeIXRCV VYcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TJq9Y2j2hvjQOKUEqQsEklHfEq8wyddeCa52aAthpsM=; b=K9uhBvcSjrPbUp7WtOfY9+ENNl7XUg3L6n+tf2jbGzLhOlzMTVcmkZxxr7MVOgFtvq sqMgKnct5Pi/xWq7oPFcpHG+56c0i6HTmlOjjD/SeXRCHWPrNeMTAz083Ukk+IhD95Q0 xtvZ3fCmzEP4qlXMbDIOnEJmTgSa1YhXDryVc7Rt6F/K9K6uvd2fPKS79/rFl03UT2/7 Jf3+MkXXRp+ja2ZId68Ukar1DAYcizxYem2n2UunUR9QuJ+YnL/pdSCvpujWz8ae4vxc fUOwOC9Je2rG4MCCOMSw5TgyqauOaVgAWbwUidO1luD4F6fx4BRi1R/BflsAcudqJcdf pYwQ== X-Gm-Message-State: AOAM530Mv21mbNakcgKUHgtsbwOy//yh4sn+v+5DLfKbjkwDcZith0Qr SEgs4d3qA00adaHagnlCej8= X-Google-Smtp-Source: ABdhPJxd7u0XnbmXPnGFaxJCsYXOLdMq3rXoAV+PBXFZY5be3h+QpLTuU5xzRSvXpmRQouSr1Clojw== X-Received: by 2002:a05:6e02:152b:b0:2cd:ffb6:59bf with SMTP id i11-20020a056e02152b00b002cdffb659bfmr4617792ilu.180.1651515476048; Mon, 02 May 2022 11:17:56 -0700 (PDT) Received: from xps8900.attlocal.net ([2600:1700:2442:6db0:2d25:3fcb:9ffb:5d5a]) by smtp.gmail.com with ESMTPSA id f43-20020a02242b000000b0032b3a7817a9sm3230678jaa.109.2022.05.02.11.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 11:17:55 -0700 (PDT) From: frowand.list@gmail.com To: Rob Herring , pantelis.antoniou@konsulko.com, Slawomir Stepien , Dan Carpenter Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Slawomir Stepien , Jan Kiszka , Geert Uytterhoeven Subject: [PATCH v2 3/3] of: overlay: do not free changeset when of_overlay_apply returns error Date: Mon, 2 May 2022 13:17:42 -0500 Message-Id: <20220502181742.1402826-4-frowand.list@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220502181742.1402826-1-frowand.list@gmail.com> References: <20220502181742.1402826-1-frowand.list@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Frank Rowand New unittests for overlay notifiers reveal a memory leak in of_overlay_apply() when a notifier returns an error for action OF_OVERLAY_POST_APPLY. The pr_err() message is: OF: ERROR: memory leak, expected refcount 1 instead of 3, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node /testcase-data/overlay-node/test-bus/test-unittest17 Change the error path to no longer call free_overlay_changeset(), and document that the caller of of_overlay_fdt_apply() may choose to remove the overlay. Update the unittest that triggered the error to expect the changed return values and to call of_overlay_remove(). Signed-off-by: Frank Rowand --- Changes since version 1: - patch 1/1 v1 did not apply on Rob's dt/next branch, rebase on top of 5f756a2eaa44 of: overlay: do not break notify on NOTIFY_{OK|STOP} Output of the new overlay notifier unittests, as filtered by scripts/dtc/of_unittest_expect: ### dt-test ### pass of_unittest_overlay_notify():2825 ok OF: overlay: overlay changeset pre-apply notifier error -16, target: /te= stcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2846 ### dt-test ### pass of_unittest_overlay_notify():2851 ok OF: overlay: overlay changeset post-apply notifier error -17, target: /t= estcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2857 ### dt-test ### pass of_unittest_overlay_notify():2862 ### dt-test ### pass of_unittest_overlay_notify():2866 ### dt-test ### pass of_unittest_overlay_notify():2872 ### dt-test ### pass of_unittest_overlay_notify():2875 ok OF: overlay: overlay changeset pre-remove notifier error -18, target: /t= estcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2886 ### dt-test ### pass of_unittest_overlay_notify():2894 ### dt-test ### pass of_unittest_overlay_notify():2898 ### dt-test ### pass of_unittest_overlay_notify():2901 ok OF: overlay: overlay changeset post-remove notifier error -19, target: /= testcase-data/overlay-node/test-bus ### dt-test ### pass of_unittest_overlay_notify():2908 ### dt-test ### pass of_unittest_overlay_notify():2915 ### dt-test ### pass of_unittest_overlay_notify():2920 ### dt-test ### pass of_unittest_overlay_notify():2932 drivers/of/overlay.c | 29 ++++++++++++++++++++++++++--- drivers/of/unittest.c | 10 ++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c index ae5ea5b1079b..4044ddcb02c6 100644 --- a/drivers/of/overlay.c +++ b/drivers/of/overlay.c @@ -952,6 +952,25 @@ static int of_overlay_apply(struct overlay_changeset *= ovcs) return ret; } =20 +/* + * of_overlay_fdt_apply() - Create and apply an overlay changeset + * @overlay_fdt: pointer to overlay FDT + * @overlay_fdt_size: number of bytes in @overlay_fdt + * @ret_ovcs_id: pointer for returning created changeset id + * + * Creates and applies an overlay changeset. + * + * See of_overlay_apply() for important behavior information. + * + * Return: 0 on success, or a negative error number. *@ret_ovcs_id is set= to + * the value of overlay changeset id, which can be passed to of_overlay_re= move() + * to remove the overlay. + * + * On error return, the changeset may be partially applied. This is espec= ially + * likely if an OF_OVERLAY_POST_APPLY notifier returns an error. In this = case + * the caller should call of_overlay_remove() with the value in *@ret_ovcs= _id. + */ + int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, int *ret_ovcs_id) { @@ -1019,15 +1038,19 @@ int of_overlay_fdt_apply(const void *overlay_fdt, u= 32 overlay_fdt_size, ovcs->overlay_mem =3D overlay_mem; =20 ret =3D of_overlay_apply(ovcs); - if (ret < 0) - goto err_free_ovcs; + /* + * If of_overlay_apply() error, calling free_overlay_changeset() may + * result in a memory leak if the apply partly succeeded, so do NOT + * goto err_free_ovcs. Instead, the caller of of_overlay_fdt_apply() + * can call of_overlay_remove(); + */ =20 mutex_unlock(&of_mutex); of_overlay_mutex_unlock(); =20 *ret_ovcs_id =3D ovcs->id; =20 - return 0; + return ret; =20 err_free_ovcs: free_overlay_changeset(ovcs); diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c index e28c3df2c4c2..dff55ae09d97 100644 --- a/drivers/of/unittest.c +++ b/drivers/of/unittest.c @@ -2848,7 +2848,7 @@ static void __init of_unittest_overlay_notify(void) =20 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset pre-apply notifier = error -16, target: /testcase-data/overlay-node/test-bus"); =20 - unittest(!ovcs_id, "ovcs_id created for overlay_16\n"); + unittest(ovcs_id, "ovcs_id not created for overlay_16\n"); =20 /* --- overlay 17 --- */ =20 @@ -2859,7 +2859,13 @@ static void __init of_unittest_overlay_notify(void) =20 EXPECT_END(KERN_INFO, "OF: overlay: overlay changeset post-apply notifier= error -17, target: /testcase-data/overlay-node/test-bus"); =20 - unittest(!ovcs_id, "ovcs_id created for overlay_17\n"); + unittest(ovcs_id, "ovcs_id not created for overlay_17\n"); + + if (ovcs_id) { + ret =3D of_overlay_remove(&ovcs_id); + unittest(!ret, + "overlay_17 of_overlay_remove(), ret =3D %d\n", ret); + } =20 /* --- overlay 18 --- */ =20 --=20 Frank Rowand