struct pglist_data *pgdat;
        unsigned long eviction;
 
-       rcu_read_lock();
-
        if (lru_gen_enabled()) {
-               bool recent = lru_gen_test_recent(shadow, file,
-                               &eviction_lruvec, &eviction, workingset);
+               bool recent;
 
+               rcu_read_lock();
+               recent = lru_gen_test_recent(shadow, file, &eviction_lruvec,
+                                            &eviction, workingset);
                rcu_read_unlock();
                return recent;
        }
 
-
+       rcu_read_lock();
        unpack_shadow(shadow, &memcgid, &pgdat, &eviction, workingset);
        eviction <<= bucket_order;
 
         * configurations instead.
         */
        eviction_memcg = mem_cgroup_from_id(memcgid);
-       if (!mem_cgroup_disabled() &&
-           (!eviction_memcg || !mem_cgroup_tryget(eviction_memcg))) {
-               rcu_read_unlock();
-               return false;
-       }
-
+       if (!mem_cgroup_tryget(eviction_memcg))
+               eviction_memcg = NULL;
        rcu_read_unlock();
 
+       if (!mem_cgroup_disabled() && !eviction_memcg)
+               return false;
        /*
         * Flush stats (and potentially sleep) outside the RCU read section.
         *
        bool workingset;
        long nr;
 
+       VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
+
        if (lru_gen_enabled()) {
                lru_gen_refault(folio, shadow);
                return;
         * is actually experiencing the refault event. Make sure the folio is
         * locked to guarantee folio_memcg() stability throughout.
         */
-       VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
        nr = folio_nr_pages(folio);
        memcg = folio_memcg(folio);
        pgdat = folio_pgdat(folio);