xref: /openbmc/linux/scripts/gfp-translate (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1608e8e66SMel Gorman#!/bin/bash
24f19048fSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only
3608e8e66SMel Gorman# Translate the bits making up a GFP mask
4608e8e66SMel Gorman# (c) 2009, Mel Gorman <mel@csn.ul.ie>
5608e8e66SMel GormanSOURCE=
6608e8e66SMel GormanGFPMASK=none
7608e8e66SMel Gorman
8608e8e66SMel Gorman# Helper function to report failures and exit
9608e8e66SMel Gormandie() {
10608e8e66SMel Gorman	echo ERROR: $@
11608e8e66SMel Gorman	if [ "$TMPFILE" != "" ]; then
12608e8e66SMel Gorman		rm -f $TMPFILE
13608e8e66SMel Gorman	fi
14608e8e66SMel Gorman	exit -1
15608e8e66SMel Gorman}
16608e8e66SMel Gorman
17608e8e66SMel Gormanusage() {
18608e8e66SMel Gorman	echo "usage: gfp-translate [-h] [ --source DIRECTORY ] gfpmask"
19608e8e66SMel Gorman	exit 0
20608e8e66SMel Gorman}
21608e8e66SMel Gorman
223ad2f3fbSDaniel Mack# Parse command-line arguments
23608e8e66SMel Gormanwhile [ $# -gt 0 ]; do
24608e8e66SMel Gorman	case $1 in
25608e8e66SMel Gorman		--source)
26608e8e66SMel Gorman			SOURCE=$2
27608e8e66SMel Gorman			shift 2
28608e8e66SMel Gorman			;;
29608e8e66SMel Gorman		-h)
30608e8e66SMel Gorman			usage
31608e8e66SMel Gorman			;;
32608e8e66SMel Gorman		--help)
33608e8e66SMel Gorman			usage
34608e8e66SMel Gorman			;;
35608e8e66SMel Gorman		*)
36608e8e66SMel Gorman			GFPMASK=$1
37608e8e66SMel Gorman			shift
38608e8e66SMel Gorman			;;
39608e8e66SMel Gorman	esac
40608e8e66SMel Gormandone
41608e8e66SMel Gorman
42608e8e66SMel Gorman# Guess the kernel source directory if it's not set. Preference is in order of
43608e8e66SMel Gorman# o current directory
44608e8e66SMel Gorman# o /usr/src/linux
45608e8e66SMel Gormanif [ "$SOURCE" = "" ]; then
46608e8e66SMel Gorman	if [ -r "/usr/src/linux/Makefile" ]; then
47608e8e66SMel Gorman		SOURCE=/usr/src/linux
48608e8e66SMel Gorman	fi
49608e8e66SMel Gorman	if [ -r "`pwd`/Makefile" ]; then
50608e8e66SMel Gorman		SOURCE=`pwd`
51608e8e66SMel Gorman	fi
52608e8e66SMel Gormanfi
53608e8e66SMel Gorman
54608e8e66SMel Gorman# Confirm that a source directory exists
55608e8e66SMel Gormanif [ ! -r "$SOURCE/Makefile" ]; then
56608e8e66SMel Gorman	die "Could not locate kernel source directory or it is invalid"
57608e8e66SMel Gormanfi
58608e8e66SMel Gorman
59608e8e66SMel Gorman# Confirm that a GFP mask has been specified
60608e8e66SMel Gormanif [ "$GFPMASK" = "none" ]; then
61608e8e66SMel Gorman	usage
62608e8e66SMel Gormanfi
63608e8e66SMel Gorman
64608e8e66SMel Gorman# Extract GFP flags from the kernel source
65608e8e66SMel GormanTMPFILE=`mktemp -t gfptranslate-XXXXXX` || exit 1
66*2049a7d0SPrathu Baroniagrep -q ___GFP $SOURCE/include/linux/gfp_types.h
6727af0384SMel Gormanif [ $? -eq 0 ]; then
68*2049a7d0SPrathu Baronia	grep "^#define ___GFP" $SOURCE/include/linux/gfp_types.h | sed -e 's/u$//' | grep -v GFP_BITS > $TMPFILE
6927af0384SMel Gormanelse
70*2049a7d0SPrathu Baronia	grep "^#define __GFP" $SOURCE/include/linux/gfp_types.h | sed -e 's/(__force gfp_t)//' | sed -e 's/u)/)/' | grep -v GFP_BITS | sed -e 's/)\//) \//' > $TMPFILE
7127af0384SMel Gormanfi
72608e8e66SMel Gorman
73608e8e66SMel Gorman# Parse the flags
74608e8e66SMel GormanIFS="
75608e8e66SMel Gorman"
76608e8e66SMel Gormanecho Source: $SOURCE
77608e8e66SMel Gormanecho Parsing: $GFPMASK
78608e8e66SMel Gormanfor LINE in `cat $TMPFILE`; do
79608e8e66SMel Gorman	MASK=`echo $LINE | awk '{print $3}'`
80608e8e66SMel Gorman	if [ $(($GFPMASK&$MASK)) -ne 0 ]; then
81608e8e66SMel Gorman		echo $LINE
82608e8e66SMel Gorman	fi
83608e8e66SMel Gormandone
84608e8e66SMel Gorman
85608e8e66SMel Gormanrm -f $TMPFILE
86608e8e66SMel Gormanexit 0
87