Bug 338995

Summary: shmat with hugepages (SHM_HUGETLB) fails with EINVAL when executed on valgrind
Product: [Developer tools] valgrind Reporter: Hideaki Kimura <hideaki.kimura>
Component: memcheckAssignee: Julian Seward <jseward>
Status: RESOLVED FIXED    
Severity: normal CC: philippe.waroquiers
Priority: NOR    
Version First Reported In: 3.9.0   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Hideaki Kimura 2014-09-10 23:19:35 UTC
This seems like a new variant of shmat-related bug reports on valgrind.
I tested with valgrind 3.9.0 and 3.8.1. Both cause EINVAL error if a user code calls shmat with SHM_HUGETLB parameter to use hugepages.

If the user code doesn't have SHM_HUGETLB option or runs it without valgrind, it goes fine.

int main(int argc, char **argv) {
  int shmid = ::shmget(
    ::getpid(),
    1ULL << 21,
    // IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);  // <-- this works fine
    IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | SHM_HUGETLB);  // <-- This causes EINVAL
  if (shmid == -1) {
    std::cerr << "shmget failed!"
      << "[Errno " << errno << "] " << std::strerror(errno) << std::endl;
    return 1;
  } else {
    std::cout << "shmget() ok" << std::endl;
  }

  void* block = ::shmat(shmid, NULL, 0);
  if (block == ((void*) -1)) {
    std::cerr << "shmat failed!"
      << "[Errno " << errno << "] " << std::strerror(errno) << std::endl;
    ::shmctl(shmid, IPC_RMID, NULL);
    return 1;
  } else {
    std::cout << "shmat() ok" << std::endl;
  }
  ::shmdt(block);
  ::shmctl(shmid, IPC_RMID, NULL);
  return 0;
}

My environment is Fedora 19, x86_64.

Reproducible: Always

Steps to Reproduce:
1. Sample code above
2.
3.

Actual Results:  
EINVAL error at shmat

Expected Results:  
Should work fine.
Comment 1 Philippe Waroquiers 2014-11-01 22:08:51 UTC
Bug is fixed in revision 14683
or rather, this revision ensures that SHM_HUGETLB flag is ignored.
A warning is given (once) to the user if such a flag is ignored
Comment 2 Hideaki Kimura 2014-11-02 23:41:41 UTC
Thanks for the fix. Yes, ignoring SHM_HUGETLB is sufficient for my usecase (and probably everyone's, who cares TLB performance in valgrind runs).
Is it in 3.10.0 release?
Comment 3 Philippe Waroquiers 2014-11-04 13:26:10 UTC
(In reply to Hideaki Kimura from comment #2)
> Thanks for the fix. Yes, ignoring SHM_HUGETLB is sufficient for my usecase
> (and probably everyone's, who cares TLB performance in valgrind runs).
> Is it in 3.10.0 release?
It is in the SVN version (the future 3.11.0).