We prove that, in the optimal transportation problem with general costs and positive continuous densities, the potential function is always of class Wloc2,p for any p≥1 outside of a closed singular set of measure zero. We also establish global W2,p estimates when the cost is a small perturbation of the quadratic cost. The latter result is new even when the cost is exactly the quadratic cost.