From e01473adc4c392aa947417cbde478161b2d741bb Mon Sep 17 00:00:00 2001
Date: Fri, 12 Jul 2019 16:10:56 +0800
Subject: [PATCH] Backport of JDK-8060463

Summary: ParNew:
SurvivorAlignmentInBytes greater then YoungPLABSize cause assert(obj != NULL
|| plab->words_remaining() < word_sz) failed: Else should have been able to
allocate.
LLT: hotspot/test/gc/survivorAlignment/TestPromotionLABLargeSurvivorAlignment.java
Bug url: https://bugs.openjdk.java.net/browse/JDK-8060463

---
 .../parNew/parNewGeneration.cpp               | 15 ++--
 ...estPromotionLABLargeSurvivorAlignment.java | 72 +++++++++++++++++++
 2 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 hotspot/test/gc/survivorAlignment/TestPromotionLABLargeSurvivorAlignment.java

diff --git a/hotspot/src/share/vm/gc_implementation/parNew/parNewGeneration.cpp b/hotspot/src/share/vm/gc_implementation/parNew/parNewGeneration.cpp
index 8ef1bd2cda..67b0421ebf 100644
--- a/hotspot/src/share/vm/gc_implementation/parNew/parNewGeneration.cpp
+++ b/hotspot/src/share/vm/gc_implementation/parNew/parNewGeneration.cpp
@@ -233,11 +233,15 @@ HeapWord* ParScanThreadState::alloc_in_to_space_slow(size_t word_sz) {
         ParallelGCBufferWastePct * plab->word_sz()) {
       // Is small enough; abandon this buffer and start a new one.
       plab->retire(false, false);
-      size_t buf_size = plab->word_sz();
+      // The minimum size has to be twice SurvivorAlignmentInBytes to
+      // allow for padding used in the alignment of 1 word.  A padding
+      // of 1 is too small for a filler word so the padding size will
+      // be increased by SurvivorAlignmentInBytes.
+      size_t min_usable_size = 2 * static_cast<size_t>(SurvivorAlignmentInBytes >> LogHeapWordSize);
+      size_t buf_size = MAX2(plab->word_sz(), min_usable_size);
       HeapWord* buf_space = sp->par_allocate(buf_size);
       if (buf_space == NULL) {
-        const size_t min_bytes =
-          ParGCAllocBuffer::min_size() << LogHeapWordSize;
+        const size_t min_bytes = MAX2(ThreadLocalAllocBuffer::min_size(), min_usable_size) << LogHeapWordSize;
         size_t free_bytes = sp->free();
         while(buf_space == NULL && free_bytes >= min_bytes) {
           buf_size = free_bytes >> LogHeapWordSize;
@@ -255,7 +259,10 @@ HeapWord* ParScanThreadState::alloc_in_to_space_slow(size_t word_sz) {
         // Note that we cannot compare buf_size < word_sz below
         // because of AlignmentReserve (see ParGCAllocBuffer::allocate()).
         assert(obj != NULL || plab->words_remaining() < word_sz,
-               "Else should have been able to allocate");
+               err_msg("Else should have been able to allocate requested object size "
+               SIZE_FORMAT ", PLAB size " SIZE_FORMAT ", SurvivorAlignmentInBytes "
+               SIZE_FORMAT ", words_remaining " SIZE_FORMAT,
+               word_sz, buf_size, SurvivorAlignmentInBytes, plab->words_remaining()));
         // It's conceivable that we may be able to use the
         // buffer we just grabbed for subsequent small requests
         // even if not for this one.
diff --git a/hotspot/test/gc/survivorAlignment/TestPromotionLABLargeSurvivorAlignment.java b/hotspot/test/gc/survivorAlignment/TestPromotionLABLargeSurvivorAlignment.java
new file mode 100644
index 0000000000..397b647489
--- /dev/null
+++ b/hotspot/test/gc/survivorAlignment/TestPromotionLABLargeSurvivorAlignment.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8060463
+ * @summary Verify that objects promoted from eden space to survivor space
+ *          with large values for SurvivorAlignmentInBytes succeed.
+ * @requires vm.opt.ExplicitGCInvokesConcurrent != true
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=8 -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=16 -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=512 -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=1k -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=4k -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ * @run main/othervm -Xmx128m
+ *                   -XX:+UnlockExperimentalVMOptions
+ *                   -XX:SurvivorAlignmentInBytes=16k -XX:SurvivorRatio=1
+ *                   -XX:-ExplicitGCInvokesConcurrent -XX:-ResizePLAB
+ *                   TestPromotionLABLargeSurvivorAlignment
+ */
+public class TestPromotionLABLargeSurvivorAlignment {
+    public static void main(String args[]) {
+        Object garbage[] = new Object[1000000];
+        for (int i = 0; i < garbage.length; i++) {
+            garbage[i] = new byte[0];
+        }
+        for (int i = 0; i < 2; i++) {
+            System.gc();
+        }
+    }
+}
-- 
2.19.0-rc1

