Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

soc: qcom: smem_state: Add devm_qcom_smem_state_get()

It is easy to forget to call qcom_smem_state_put() after
a qcom_smem_state_get(). Introduce a devm_qcom_smem_state_get()
helper function that automates this so that qcom_smem_state_put()
is automatically called when a device is removed.

Signed-off-by: Stephan Gerhold <stephan@gerhold.net>
Reviewed-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Link: https://lore.kernel.org/r/20210618111556.53416-1-stephan@gerhold.net
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

authored by

Stephan Gerhold and committed by
Bjorn Andersson
61d1961a 30da589d

+44
+36
drivers/soc/qcom/smem_state.c
··· 151 151 } 152 152 EXPORT_SYMBOL_GPL(qcom_smem_state_put); 153 153 154 + static void devm_qcom_smem_state_release(struct device *dev, void *res) 155 + { 156 + qcom_smem_state_put(*(struct qcom_smem_state **)res); 157 + } 158 + 159 + /** 160 + * devm_qcom_smem_state_get() - acquire handle to a devres managed state 161 + * @dev: client device pointer 162 + * @con_id: name of the state to lookup 163 + * @bit: flags from the state reference, indicating which bit's affected 164 + * 165 + * Returns handle to the state, or ERR_PTR(). qcom_smem_state_put() is called 166 + * automatically when @dev is removed. 167 + */ 168 + struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev, 169 + const char *con_id, 170 + unsigned *bit) 171 + { 172 + struct qcom_smem_state **ptr, *state; 173 + 174 + ptr = devres_alloc(devm_qcom_smem_state_release, sizeof(*ptr), GFP_KERNEL); 175 + if (!ptr) 176 + return ERR_PTR(-ENOMEM); 177 + 178 + state = qcom_smem_state_get(dev, con_id, bit); 179 + if (!IS_ERR(state)) { 180 + *ptr = state; 181 + devres_add(dev, ptr); 182 + } else { 183 + devres_free(ptr); 184 + } 185 + 186 + return state; 187 + } 188 + EXPORT_SYMBOL_GPL(devm_qcom_smem_state_get); 189 + 154 190 /** 155 191 * qcom_smem_state_register() - register a new state 156 192 * @of_node: of_node used for matching client lookups
+8
include/linux/soc/qcom/smem_state.h
··· 14 14 #ifdef CONFIG_QCOM_SMEM_STATE 15 15 16 16 struct qcom_smem_state *qcom_smem_state_get(struct device *dev, const char *con_id, unsigned *bit); 17 + struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev, const char *con_id, unsigned *bit); 17 18 void qcom_smem_state_put(struct qcom_smem_state *); 18 19 19 20 int qcom_smem_state_update_bits(struct qcom_smem_state *state, u32 mask, u32 value); ··· 26 25 27 26 static inline struct qcom_smem_state *qcom_smem_state_get(struct device *dev, 28 27 const char *con_id, unsigned *bit) 28 + { 29 + return ERR_PTR(-EINVAL); 30 + } 31 + 32 + static inline struct qcom_smem_state *devm_qcom_smem_state_get(struct device *dev, 33 + const char *con_id, 34 + unsigned *bit) 29 35 { 30 36 return ERR_PTR(-EINVAL); 31 37 }