Solution by gghx.
We claim the only solution is f(x)≡cx for some real constant c, which obviously works. We now show it is the only solution.
It is easy to check that the only linear function is f(x)=cx.
We now prove a more general statement, which is linked above.
Statement: Suppose f,g,h:R→R are functions such that for any reals x,y, f(xy+g(x))=xf(y)+h(x). Show that f is linear.
Proof. Let P(x,y) denote the assertion that f(xy+g(x))=xf(y)+h(x).
Claim 1: If f(y+c)=f(y)+d for constants c=0,d and all y∈R, then f is linear.
Proof. Using P(x,y+c) gives
f(xy+xc+g(x))=xf(y+c)+h(x)=xf(y)+xd+h(x)=f(xy+g(x))+xd.
Hence, for any x=0 we have f(y+g(x)+xc)=f(y+g(x))+xd. This is still obviously true when x=0. Hence, taking y=−g(x), we get f(xc)=f(0)+xd, thus f is linear.
Claim 2: If xg(−1)+2g(x)=g(−1) for some x=0, then f is linear.
Proof. P(−1,y):f(−y+g(−1))=−f(y)+h(−1). Hence
f(−xy+xg(−1)+g(x))=xf(−y+g(−1))+h(x)=−xf(y)+xh(−1)+h(x).
The RHS is actually −f(xy+g(x))+xh(−1)+2h(x).
Simplifying,
f(−y+xg(−1)+g(x))=−f(y+g(x))+xh(−1)+2h(x) for all x=0
⟺f(−y+xg(−1)+2g(x))=−f(y)+xh(−1)+2h(x) for all x=0.
Suppose tg(−1)+2g(t)=g(−1) for some t=0, then letting tg(−1)+2g(t)=a and th(−1)+2h(t)=b we get f(−y+a)=−f(y)+b, but
f(−y+a)=f(−(y−a+g(−1))+g(−1))=−f(y−a+g(−1))+h(−1).
This means f(y)−f(y−a+g(−1))−h(−1)+b, which means f is linear from claim 1.
Else, we have xg(−1)+2g(x)=g(−1) for all x=0, which means xh(−1)+2h(x)=h(−1) for all x=0. Hence g,h are both linear (except at 0).
Now in the original equation, picking y constant such that it is not the negative of the gradient of g, we get that f is linear everywhere except at one point.
We are almost done, suppose f(x)=ax+b for all x=k, g(x)=c(x−1) and h(x)=d(x−1) for all x=0. Then taking any x=0 and y really large such that ∣xy+g(x)∣,∣y∣>k gives axy+ag(x)+b=axy+xb+h(x), so ac(x−1)+b=xb+d(x−1), which forces ac=b+d.
If k=−c, then we can take y=k, which gives f(xk+c(x−1))=xf(k)+d(x−1). By taking x sufficiently large, we get axk+ac(x−1)+b=xf(k)+d(x−1). This simplifies to axk+b(x−1)+b=xf(k) so f(k)=ax+b as well, done.
If k=−c, then if we take y=k, we get f(xk+(−k)(x−1))=xf(k)+d(x−1), so f(k)=xf(k)+d(x−1). By taking x sufficiently large, f(k)=−d. But this means f(k)=f(−c)=−d=b−ac=a(−c)+b as well, so we are done too!
This completes the proof.