Redirect loop when forwarding to mobile site

You need to check that you aren’t already on the mobile site:

function redirecting_to_mobile_site() {
    if ( $_SERVER['SERVER_NAME'] !== 'm.example.com' && wp_is_mobile() ) {
        wp_redirect( 'http://m.example.com' . $_SERVER['REQUEST_URI'], 301 );
        exit;
    }
}

add_action( 'template_redirect', 'redirecting_to_mobile_site' );

You’ll need to clear your browser cache to properly test the fix.