  We derive the equations of linear cosmological perturbations for the general
Lagrangian density $f (R,\phi, X)/2+L_c$, where $R$ is a Ricci scalar, $\phi$
is a scalar field, and $X=-(\nabla \phi)^2/2$ is a field kinetic energy. We
take into account a nonlinear self-interaction term $L_c$ recently studied in
the context of "Galileon" cosmology, which keeps the field equations at second
order. Taking into account a scalar-field mass explicitly, the equations of
matter density perturbations and gravitational potentials are obtained under a
quasi-static approximation on sub-horizon scales. We also derive conditions for
the avoidance of ghosts and Laplacian instabilities associated with propagation
speeds. Our analysis includes most of modified gravity models of dark energy
proposed in literature and thus it is convenient to test the viability of such
models from both theoretical and observational points of view.
