In subsection(s) 7.5 (and 4.1) of Topics in Optimal Transportation, Cedric Villani states the following (I paraphrase):
Take a probability measure $\mu \in \mathcal P_2(\mathbb R^n)$ with finite second moment and strictly positive density $f$ with respect to the Lebesgue measure on $\mathbb R^n$. Consider the linear differential operator $$ L := - \Delta + \nabla (- \log(f))\cdot \nabla. $$ Then $L$ satisfies the following integration-by-parts formula with respect to $\mu$: $$ \int_{\mathbb R^d} (L h_1)(x) h_2(x) \, \text{d}\mu(x) = \int_{\mathbb R^d} (L h_2)(x) h_1(x) \, \text{d}\mu(x) = \int_{\mathbb R^d} \nabla h_1(x) \cdot \nabla h_2(x) \, \text{d}\mu(x) $$ for all $h_1, h_2 \colon \mathbb R^d \to \mathbb R$ with certain regularity assumptions. In particular, $L$ is a non-negative self-adjoint operator on the set of those $h$ with respect to the $L^2(\mathbb R^d, d \mu)$ scalar product.
Inspired by $\| h \|_{L^2(\mathbb R^d)}^2 = \int_{\mathbb R^d} | h(x) |^2 \, \text{d}x$ and $\| h \|_{\dot{H}^1(\mathbb R^d)}^2 = \int_{\mathbb R^d} \| \nabla h(x) \|_2^2 \, \text{d}x$ we define weighted square "norms" $$ \| h \|_{L^2(\mathbb R^d, d \mu)}^2 = \int_{\mathbb R^d} | h(x) |^2 \, \text{d}\mu(x) \quad \text{and} \quad \| h \|_{\dot{H}^1(\mathbb R^d, d\mu)}^2 = \int_{\mathbb R^d} h(x)(L h)(x) \|_2^2 \, \text{d}\mu(x). $$ Since $f > 0$ everywhere, $\ker(L)$ only contains constant functions. Under certain regularity conditions which we prefer not to discuss here, one can define the inverse $L^{-1}$ of $L$ on space of functions $h$ with $\int_{\mathbb R^d} h(x) \, \text{d}\mu(x) = 0$ and then establish \begin{align} \| h \|_{H^{-1}(\mathbb R^d, d \mu)} & := \sup\left\{ \int_{\mathbb R^d} h(x) k(x) \, \text{d}\mu(x): k \in \mathcal{C}_c^{\infty}(\mathbb R^n), \| k \|_{\dot{H}^1(\mathbb R^d; d\mu)} = 1 \right\} \\ & = \int_{\mathbb R^d} h(x) (L^{-1} h)(x) \, \text{d}\mu(x), \end{align} so $H^{-1}(\mathbb R^d, d \mu)$ is dual to $\dot{H}^1(\mathbb R^d, d \mu)$.
My work so far. I don't know how to show that $\ker(L)$ only contains constant functions, partly because I don't know on which space $L$ is defined. If we could take $f = 1$ (we can't since then $\mu$ would not be a probability measure), then $L = - \Delta$ and surely $\ker(L)$ also contains linear functions. If $f(x) = e^{-x^2} \in L^2(\mathbb R)$ up to a normalization constant, then $-\log(f) = x^2$ and thus $L h = - h''(x) + 2 x h'(x)$ and $L h = 0$ implies $h(x) = c_1 \text{erfi}(x) + c_2$, where erfi is the imaginary error function, according to WolframAlpha, which is not constant.
My question. I am wondering I what way "$\ker(L)$ contains only constant functions" is meant and how to show it.
Let $h:\mathbb R^d\to \mathbb R \in\ker(L)$ satisfy the "certain regularity assumptions" mentioned in the OP (which certainly include differentiability). From the integration by parts formula, we get :
\begin{align*} \|\nabla h\|^2_{L^2(\mathbb R^d,d\mu)} :&= \int_{\mathbb R^d} | \nabla h(x) |^2 \, \text{d}\mu(x) \\ &=\int_{\mathbb R^d} \nabla h(x) \cdot \nabla h(x)\, \text{d}\mu(x) \\ &=\int_{\mathbb R^d} [L h](x)\cdot h(x) \, \text{d}\mu(x)\\ &=\int_{\mathbb R^d} 0\cdot h(x) \, \text{d}\mu(x)=0.\end{align*}
Hence $h\in\ker (L)\implies \nabla h\equiv 0$ for $\mu$-almost all $x$. Since it is said that $f>0$ everywhere and $h$ is differentiable, it follows that the kernel of $L$ only contains constant functions.
As for why you get that $Lh(x) \equiv 0$ for $h:x\mapsto c_1 \text{erfi}(x) + c_2$ with $c_1\ne 0$ in the case $f\propto e^{-x^2}$, my reasonable guess is that the domain of $L$ consists of functions which are square-integrable with respect to $d\mu$, which in particular implies (since $\mu$ is finite) that these functions are also absolutely integrable with respect to $d\mu$. But if $c_1\ne 0$ then your $h$ will not be absolutely integrable with respect to $d\mu$, indeed, for any $t>0$ :
$$\int_{-t}^t |\text{erfi}(x)|\ d\mu(x)=\frac2Z\int_0^t \text{erfi}(x)e^{-x^2}\ dx = Ct^2\ _2F_2\left(1,1;\frac32,2;-t^2\right)\tag 1 $$ Where $Z:=\int_{\mathbb R}e^{-x^2}\ dx$, $C$ is a positive constant and $_2F_2$ is a generalized hypergeometric function. The right-hand side of $(1)$ diverges according to wolfram alpha, thus we see that $h:x\mapsto c_1\text{erfi}(x) + c_2$ is not in the domain of $L$ for $c_1\ne 0$, and so there is no contradiction.